forked from aeraki-mesh/meta-protocol-proxy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdubbo_codec.cc
325 lines (282 loc) · 11.2 KB
/
dubbo_codec.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
#include <any>
#include "envoy/buffer/buffer.h"
#include "source/common/common/logger.h"
#include "src/meta_protocol_proxy/codec/codec.h"
#include "src/application_protocols/dubbo/dubbo_codec.h"
#include "src/application_protocols/dubbo/protocol.h"
#include "src/application_protocols/dubbo/message.h"
#include "src/application_protocols/dubbo/message_impl.h"
namespace Envoy {
namespace Extensions {
namespace NetworkFilters {
namespace MetaProtocolProxy {
namespace Dubbo {
MetaProtocolProxy::DecodeStatus DubboCodec::decode(Buffer::Instance& buffer,
MetaProtocolProxy::Metadata& metadata) {
ENVOY_LOG(debug, "dubbo decoder: {} bytes available", buffer.length());
if (!decode_started_) {
start();
}
ENVOY_LOG(debug, "dubbo decoder: protocol {}, state {}, {} bytes available", protocol_->name(),
ProtocolStateNameValues::name(state_machine_->currentState()), buffer.length());
ProtocolState state = state_machine_->run(buffer);
if (state == ProtocolState::WaitForData) {
ENVOY_LOG(debug, "dubbo decoder: wait for data");
return DecodeStatus::WaitForData;
}
ASSERT(state == ProtocolState::Done);
toMetadata(*(state_machine_->messageMetadata()), *(state_machine_->messageContext()), metadata);
// Reset for next request.
complete();
return DecodeStatus::Done;
}
void DubboCodec::start() {
state_machine_ = std::make_unique<DecoderStateMachine>(*protocol_);
decode_started_ = true;
}
void DubboCodec::complete() {
state_machine_ = nullptr;
decode_started_ = false;
}
void DubboCodec::encode(const MetaProtocolProxy::Metadata& metadata,
const MetaProtocolProxy::Mutation& mutation, Buffer::Instance& buffer) {
ENVOY_LOG(debug, "dubbo: codec server real address: {} ",
metadata.getString(ReservedHeaders::RealServerAddress));
switch (metadata.getMessageType()) {
case MetaProtocolProxy::MessageType::Heartbeat: {
encodeHeartbeat(metadata, buffer);
break;
}
case MetaProtocolProxy::MessageType::Request: {
encodeRequest(metadata, mutation, buffer);
break;
}
case MetaProtocolProxy::MessageType::Response: {
break;
}
case MetaProtocolProxy::MessageType::Error: {
break;
}
default:
PANIC("not reached");
}
}
void DubboCodec::onError(const MetaProtocolProxy::Metadata& metadata,
const MetaProtocolProxy::Error& error, Buffer::Instance& buffer) {
ASSERT(buffer.length() == 0);
MessageMetadata msgMetadata;
toMsgMetadata(metadata, msgMetadata);
msgMetadata.setResponseStatus(ResponseStatus::Ok);
msgMetadata.setMessageType(MessageType::Response);
ResponseStatus status;
switch (error.type) {
case MetaProtocolProxy::ErrorType::RouteNotFound:
status = ResponseStatus::ServiceNotFound;
break;
case MetaProtocolProxy::ErrorType::BadResponse:
status = ResponseStatus::BadResponse;
break;
default:
status = ResponseStatus::ServerError;
}
msgMetadata.setResponseStatus(status);
ContextImpl ctx;
if (!protocol_->encode(buffer, msgMetadata, ctx, error.message,
RpcResponseType::ResponseWithException)) {
throw EnvoyException("failed to encode heartbeat message");
}
}
void DubboCodec::toMetadata(const MessageMetadata& msgMetadata,
MetaProtocolProxy::Metadata& metadata) {
if (msgMetadata.hasInvocationInfo()) {
auto* invo = const_cast<RpcInvocationImpl*>(
dynamic_cast<const RpcInvocationImpl*>(&msgMetadata.invocationInfo()));
metadata.putString("interface", invo->serviceName());
metadata.putString("method", invo->methodName());
metadata.setOperationName(invo->serviceName() + "/" + invo->methodName());
for (const auto& pair : invo->attachment().attachment()) {
const auto key = pair.first->toString();
const auto value = pair.second->toString();
if (!key.has_value() || !value.has_value()) {
continue;
}
metadata.putString(key.value(), value.value());
}
}
metadata.put("InvocationInfo", msgMetadata.invocationInfoPtr());
metadata.put("ProtocolType", msgMetadata.protocolType());
metadata.put("ProtocolVersion", msgMetadata.protocolVersion());
metadata.put("MessageType", msgMetadata.messageType());
metadata.setRequestId(msgMetadata.requestId());
auto timeout = msgMetadata.timeout();
if (timeout.has_value()) {
metadata.put("Timeout", msgMetadata.timeout());
}
metadata.put("TwoWay", msgMetadata.isTwoWay());
metadata.put("SerializationType", msgMetadata.serializationType());
if (msgMetadata.hasResponseStatus()) {
metadata.put("ResponseStatus", msgMetadata.responseStatus());
}
switch (msgMetadata.messageType()) {
case MessageType::Request:
metadata.setMessageType(MetaProtocolProxy::MessageType::Request);
break;
case MessageType::Response:
metadata.setMessageType(MetaProtocolProxy::MessageType::Response);
break;
case MessageType::Oneway:
metadata.setMessageType(MetaProtocolProxy::MessageType::Oneway);
break;
case MessageType::Exception:
metadata.setMessageType(MetaProtocolProxy::MessageType::Error);
break;
case MessageType::HeartbeatRequest:
FALLTHRU;
case MessageType::HeartbeatResponse:
metadata.setMessageType(MetaProtocolProxy::MessageType::Heartbeat);
break;
default:
PANIC("not reached");
}
if (msgMetadata.hasResponseStatus()) {
if (msgMetadata.responseStatus() == ResponseStatus::Ok) {
metadata.setResponseStatus(MetaProtocolProxy::ResponseStatus::Ok);
} else {
metadata.setResponseStatus(MetaProtocolProxy::ResponseStatus::Error);
}
}
}
void DubboCodec::toMetadata(const MessageMetadata& msgMetadata, Context& context,
MetaProtocolProxy::Metadata& metadata) {
DubboCodec::toMetadata(msgMetadata, metadata);
metadata.setHeaderSize(context.headerSize());
metadata.setBodySize(context.bodySize());
metadata.originMessage().move(context.originMessage());
}
void DubboCodec::toMsgMetadata(const MetaProtocolProxy::Metadata& metadata,
MessageMetadata& msgMetadata) {
msgMetadata.setRequestId(metadata.getRequestId());
auto ref = metadata.get("InvocationInfo");
if (ref.has_value()) {
const auto& invo = ref.value();
msgMetadata.setInvocationInfo(std::any_cast<RpcInvocationSharedPtr>(invo));
}
ref = metadata.get("ProtocolType");
assert(ref.has_value());
const auto& proto_type = ref.value();
msgMetadata.setProtocolType(std::any_cast<ProtocolType>(proto_type));
ref = metadata.get("ProtocolVersion");
assert(ref.has_value());
const auto& version = ref.value();
msgMetadata.setProtocolVersion(std::any_cast<uint8_t>(version));
ref = metadata.get("MessageType");
assert(ref.has_value());
const auto& msg_type = ref.value();
msgMetadata.setMessageType(std::any_cast<MessageType>(msg_type));
ref = metadata.get("Timeout");
if (ref.has_value()) {
const auto& timeout = ref.value();
msgMetadata.setTimeout(std::any_cast<uint32_t>(timeout));
}
ref = metadata.get("TwoWay");
assert(ref.has_value());
msgMetadata.setTwoWayFlag(metadata.getBool("TwoWay"));
ref = metadata.get("SerializationType");
assert(ref.has_value());
const auto& serial_type = ref.value();
msgMetadata.setSerializationType(std::any_cast<SerializationType>(serial_type));
ref = metadata.get("ResponseStatus");
if (ref.has_value()) {
const auto& res_status = ref.value();
msgMetadata.setResponseStatus(std::any_cast<ResponseStatus>(res_status));
}
}
void DubboCodec::encodeHeartbeat(const MetaProtocolProxy::Metadata& metadata,
Buffer::Instance& buffer) {
MessageMetadata msgMetadata;
toMsgMetadata(metadata, msgMetadata);
msgMetadata.setResponseStatus(ResponseStatus::Ok);
msgMetadata.setMessageType(MessageType::HeartbeatResponse);
ContextImpl ctx;
if (!protocol_->encode(buffer, msgMetadata, ctx, "")) {
throw EnvoyException("failed to encode heartbeat message");
}
}
void DubboCodec::encodeRequest(const MetaProtocolProxy::Metadata& metadata,
const MetaProtocolProxy::Mutation& mutation,
Buffer::Instance& buffer) {
MessageMetadata msgMetadata;
toMsgMetadata(metadata, msgMetadata);
if (msgMetadata.hasInvocationInfo()) {
auto* invo = const_cast<RpcInvocationImpl*>(
dynamic_cast<const RpcInvocationImpl*>(&msgMetadata.invocationInfo()));
for (const auto& keyValue : mutation) {
ENVOY_LOG(debug, "dubbo: codec mutation {} : {}", keyValue.first, keyValue.second);
invo->attachment().remove(keyValue.first);
invo->attachment().insert(keyValue.first, keyValue.second);
}
}
ContextImpl ctx;
ctx.setHeaderSize(metadata.getHeaderSize());
ctx.setBodySize(metadata.getBodySize());
if (!protocol_->encode(buffer, msgMetadata, ctx, "")) {
throw EnvoyException("failed to encode request message");
}
}
ProtocolState DecoderStateMachine::onDecodeStreamHeader(Buffer::Instance& buffer) {
auto ret = protocol_.decodeHeader(buffer, metadata_);
if (!ret.second) {
ENVOY_LOG(debug, "dubbo decoder: need more data for {} protocol", protocol_.name());
return ProtocolState::WaitForData;
}
context_ = ret.first;
if (metadata_->messageType() == MessageType::HeartbeatRequest ||
metadata_->messageType() == MessageType::HeartbeatResponse) {
if (buffer.length() < (context_->headerSize() + context_->bodySize())) {
ENVOY_LOG(debug, "dubbo decoder: need more data for {} protocol heartbeat", protocol_.name());
return ProtocolState::WaitForData;
}
ENVOY_LOG(debug, "dubbo decoder: this is the {} heartbeat message", protocol_.name());
context_->originMessage().move(buffer, (context_->headerSize() + context_->bodySize()));
return ProtocolState::Done;
}
context_->originMessage().move(buffer, context_->headerSize());
return ProtocolState::OnDecodeStreamData;
}
ProtocolState DecoderStateMachine::onDecodeStreamData(Buffer::Instance& buffer) {
if (!protocol_.decodeData(buffer, context_, metadata_)) {
ENVOY_LOG(debug, "dubbo decoder: need more data for {} serialization, current size {}",
protocol_.serializer()->name(), buffer.length());
return ProtocolState::WaitForData;
}
context_->originMessage().move(buffer, context_->bodySize());
ENVOY_LOG(debug, "dubbo decoder: ends the deserialization of the message");
return ProtocolState::Done;
}
ProtocolState DecoderStateMachine::handleState(Buffer::Instance& buffer) {
switch (state_) {
case ProtocolState::OnDecodeStreamHeader:
return onDecodeStreamHeader(buffer);
case ProtocolState::OnDecodeStreamData:
return onDecodeStreamData(buffer);
default:
PANIC("not reached");
}
}
ProtocolState DecoderStateMachine::run(Buffer::Instance& buffer) {
while (state_ != ProtocolState::Done) {
ENVOY_LOG(trace, "dubbo decoder: state {}, {} bytes available",
ProtocolStateNameValues::name(state_), buffer.length());
ProtocolState nextState = handleState(buffer);
if (nextState == ProtocolState::WaitForData) {
return ProtocolState::WaitForData;
}
state_ = nextState;
}
return state_;
}
} // namespace Dubbo
} // namespace MetaProtocolProxy
} // namespace NetworkFilters
} // namespace Extensions
} // namespace Envoy