use of com.baidu.brpc.protocol.Request in project skywalking-java by apache.
the class ClientInterceptor method beforeMethod.
@Override
public void beforeMethod(EnhancedInstance objInst, Method method, Object[] allArguments, Class<?>[] argumentsTypes, MethodInterceptResult result) throws Throwable {
Request request = (Request) allArguments[0];
InetSocketAddress remoteAddress = (InetSocketAddress) request.getChannel().remoteAddress();
InetAddress address = remoteAddress.getAddress();
final ContextCarrier contextCarrier = new ContextCarrier();
AbstractSpan span = ContextManager.createExitSpan(generateOperationName(request), contextCarrier, address.getHostAddress() + ":" + remoteAddress.getPort());
CarrierItem next = contextCarrier.items();
while (next.hasNext()) {
next = next.next();
if (request.getKvAttachment() == null) {
request.setKvAttachment(new HashMap<>());
}
request.getKvAttachment().put(next.getHeadKey(), next.getHeadValue());
}
span.setComponent(ComponentsDefine.BRPC_JAVA);
SpanLayer.asRPCFramework(span);
}
use of com.baidu.brpc.protocol.Request in project skywalking-java by apache.
the class ServerInterceptor method beforeMethod.
@Override
public void beforeMethod(EnhancedInstance objInst, Method method, Object[] allArguments, Class<?>[] argumentsTypes, MethodInterceptResult result) throws Throwable {
Request request = (Request) allArguments[0];
ContextCarrier contextCarrier = new ContextCarrier();
CarrierItem next = contextCarrier.items();
if (request.getKvAttachment() == null) {
request.setKvAttachment(new HashMap<>());
}
while (next.hasNext()) {
next = next.next();
next.setHeadValue((String) request.getKvAttachment().get(next.getHeadKey()));
}
AbstractSpan span = ContextManager.createEntrySpan(generateOperationName(request), contextCarrier);
SpanLayer.asRPCFramework(span);
span.setComponent(ComponentsDefine.BRPC_JAVA);
}
use of com.baidu.brpc.protocol.Request in project brpc-java by baidu.
the class HuluRpcProtocol method decodeRequest.
@Override
public Request decodeRequest(Object packet) throws Exception {
Request request = this.createRequest();
HuluRpcDecodePacket requestPacket = (HuluRpcDecodePacket) packet;
ByteBuf metaBuf = requestPacket.getMetaBuf();
ByteBuf protoAndAttachmentBuf = requestPacket.getProtoAndAttachmentBuf();
ByteBuf protoBuf = null;
try {
HuluRpcProto.HuluRpcRequestMeta requestMeta = (HuluRpcProto.HuluRpcRequestMeta) ProtobufUtils.parseFrom(metaBuf, defaultRpcRequestMetaInstance);
request.setCorrelationId(requestMeta.getCorrelationId());
request.setLogId(requestMeta.getLogId());
int compressType = requestMeta.getCompressType();
request.setCompressType(compressType);
// service info
ServiceManager serviceManager = ServiceManager.getInstance();
RpcMethodInfo rpcMethodInfo = serviceManager.getService(requestMeta.getServiceName(), String.valueOf(requestMeta.getMethodIndex()));
if (rpcMethodInfo == null) {
String errorMsg = String.format("Fail to find service=%s, methodIndex=%s", requestMeta.getServiceName(), requestMeta.getMethodIndex());
request.setException(new RpcException(RpcException.SERVICE_EXCEPTION, errorMsg));
return request;
}
if (requestMeta.hasTraceId()) {
request.setTraceId(requestMeta.getTraceId());
}
if (requestMeta.hasSpanId()) {
request.setSpanId(request.getSpanId());
}
if (requestMeta.hasParentSpanId()) {
request.setParentSpanId(requestMeta.getParentSpanId());
}
if (requestMeta.getExtFieldsCount() > 0) {
if (request.getKvAttachment() == null) {
request.setKvAttachment(new HashMap<String, Object>());
}
for (HuluRpcProto.HuluRpcRequestMetaExtField extField : requestMeta.getExtFieldsList()) {
request.getKvAttachment().put(extField.getKey(), extField.getValue());
}
}
request.setServiceName(rpcMethodInfo.getServiceName());
request.setMethodName(rpcMethodInfo.getMethodName());
request.setRpcMethodInfo(rpcMethodInfo);
request.setTargetMethod(rpcMethodInfo.getMethod());
request.setTarget(rpcMethodInfo.getTarget());
// proto body
try {
Compress compress = compressManager.getCompress(compressType);
int userMessageSize = requestMeta.getUserMessageSize();
if (userMessageSize > 0) {
protoBuf = protoAndAttachmentBuf.readSlice(userMessageSize);
} else {
protoBuf = protoAndAttachmentBuf;
}
Object requestProto = compress.uncompressInput(protoBuf, rpcMethodInfo);
request.setArgs(new Object[] { requestProto });
// attachment
if (userMessageSize > 0) {
request.setBinaryAttachment(protoAndAttachmentBuf);
protoAndAttachmentBuf = null;
}
} catch (Exception ex) {
String errorMsg = String.format("decode failed, msg=%s", ex.getMessage());
LOG.error(errorMsg);
throw new RpcException(RpcException.SERIALIZATION_EXCEPTION, errorMsg, ex);
}
return request;
} finally {
if (metaBuf != null) {
metaBuf.release();
}
if (protoAndAttachmentBuf != null) {
protoAndAttachmentBuf.release();
}
}
}
use of com.baidu.brpc.protocol.Request in project brpc-java by baidu.
the class PublicPbrpcProtocol method decodeRequest.
@Override
public Request decodeRequest(Object packet) throws Exception {
Request request = this.createRequest();
PublicPbRpcPacket pbPacket = (PublicPbRpcPacket) packet;
ByteBuf bodyBuf = pbPacket.getBody();
try {
PublicPbrpcRequest pbRequest = (PublicPbrpcRequest) ProtobufUtils.parseFrom(bodyBuf, PublicPbrpcRequest.getDefaultInstance());
RequestBody body = pbRequest.getRequestBody(0);
RequestHead head = pbRequest.getRequestHead();
request.setCorrelationId(body.getId());
request.setLogId(head.getLogId());
int compressType = head.getCompressType();
request.setCompressType(compressType);
// service info
ServiceManager serviceManager = ServiceManager.getInstance();
RpcMethodInfo rpcMethodInfo = serviceManager.getService(body.getService(), String.valueOf(body.getMethodId()));
if (rpcMethodInfo == null) {
String errorMsg = String.format("Fail to find service=%s, methodIndex=%s", body.getService(), body.getMethodId());
request.setException(new RpcException(RpcException.SERVICE_EXCEPTION, errorMsg));
return request;
}
request.setRpcMethodInfo(rpcMethodInfo);
request.setTargetMethod(rpcMethodInfo.getMethod());
request.setTarget(rpcMethodInfo.getTarget());
// proto body
try {
Compress compress = compressManager.getCompress(compressType);
Object requestProto = compress.uncompressInput(body.getSerializedRequest().toByteArray(), rpcMethodInfo);
request.setArgs(new Object[] { requestProto });
} catch (Exception ex) {
String errorMsg = String.format("decode failed, msg=%s", ex.getMessage());
log.error(errorMsg);
throw new RpcException(RpcException.SERIALIZATION_EXCEPTION, errorMsg, ex);
}
return request;
} finally {
if (bodyBuf != null) {
bodyBuf.release();
}
}
}
use of com.baidu.brpc.protocol.Request in project brpc-java by baidu.
the class SofaRpcProtocol method decodeRequest.
@Override
public Request decodeRequest(Object packet) throws Exception {
Request request = this.createRequest();
SofaRpcDecodePacket requestPacket = (SofaRpcDecodePacket) packet;
ByteBuf metaBuf = requestPacket.getMetaBuf();
ByteBuf protoBuf = requestPacket.getProtoBuf();
try {
SofaRpcProto.SofaRpcMeta requestMeta = (SofaRpcProto.SofaRpcMeta) ProtobufUtils.parseFrom(metaBuf, defaultRpcMetaInstance);
request.setCorrelationId(requestMeta.getSequenceId());
if (StringUtils.isBlank(requestMeta.getMethod())) {
String errorMsg = "method is null";
LOG.error(errorMsg);
request.setException(new RpcException(RpcException.SERVICE_EXCEPTION, errorMsg));
return request;
}
ServiceManager serviceManager = ServiceManager.getInstance();
RpcMethodInfo rpcMethodInfo = serviceManager.getService(requestMeta.getMethod().toLowerCase());
if (rpcMethodInfo == null) {
String errorMsg = String.format("Fail to find method=%s", requestMeta.getMethod());
LOG.error(errorMsg);
request.setException(new RpcException(RpcException.SERVICE_EXCEPTION, errorMsg));
return request;
}
request.setServiceName(rpcMethodInfo.getServiceName());
request.setMethodName(rpcMethodInfo.getMethodName());
request.setRpcMethodInfo(rpcMethodInfo);
request.setTargetMethod(rpcMethodInfo.getMethod());
request.setTarget(rpcMethodInfo.getTarget());
int compressType = getStandardCompressType(requestMeta.getCompressType());
request.setCompressType(compressType);
Compress compress = compressManager.getCompress(compressType);
try {
Object requestProto = compress.uncompressInput(protoBuf, rpcMethodInfo);
request.setArgs(new Object[] { requestProto });
} catch (Exception ex) {
String errorMsg = String.format("decode request failed, msg=%s", ex.getMessage());
LOG.error(errorMsg);
throw new RpcException(RpcException.SERIALIZATION_EXCEPTION, errorMsg);
}
return request;
} finally {
if (metaBuf != null) {
metaBuf.release();
}
if (protoBuf != null) {
protoBuf.release();
}
}
}
Aggregations