use of com.alipay.sofa.rpc.context.RpcInvokeContext in project sofa-rpc by sofastack.
the class RpcSofaTracer method clientReceived.
@Override
public void clientReceived(SofaRequest request, SofaResponse response, Throwable exceptionThrow) {
// 客户端的启动
SofaTraceContext sofaTraceContext = SofaTraceContextHolder.getSofaTraceContext();
SofaTracerSpan clientSpan = sofaTraceContext.pop();
if (clientSpan == null) {
return;
}
// Record client receive event
clientSpan.log(LogData.CLIENT_RECV_EVENT_VALUE);
// rpc 上下文
RpcInternalContext context = null;
if (RpcInternalContext.isAttachmentEnable()) {
context = RpcInternalContext.getContext();
if (!clientSpan.getTagsWithStr().containsKey(RpcSpanTags.ROUTE_RECORD)) {
clientSpan.setTag(RpcSpanTags.ROUTE_RECORD, (String) context.getAttachment(RpcConstants.INTERNAL_KEY_ROUTER_RECORD));
}
clientSpan.setTag(RpcSpanTags.REQ_SERIALIZE_TIME, (Number) context.getAttachment(RpcConstants.INTERNAL_KEY_REQ_SERIALIZE_TIME));
clientSpan.setTag(RpcSpanTags.RESP_DESERIALIZE_TIME, (Number) context.getAttachment(RpcConstants.INTERNAL_KEY_RESP_DESERIALIZE_TIME));
clientSpan.setTag(RpcSpanTags.RESP_SIZE, (Number) context.getAttachment(RpcConstants.INTERNAL_KEY_RESP_SIZE));
clientSpan.setTag(RpcSpanTags.REQ_SIZE, (Number) context.getAttachment(RpcConstants.INTERNAL_KEY_REQ_SIZE));
clientSpan.setTag(RpcSpanTags.CLIENT_CONN_TIME, (Number) context.getAttachment(RpcConstants.INTERNAL_KEY_CONN_CREATE_TIME));
Long ce = (Long) context.getAttachment(RpcConstants.INTERNAL_KEY_CLIENT_ELAPSE);
if (ce != null) {
clientSpan.setTag(RpcSpanTags.CLIENT_ELAPSE_TIME, ce);
}
InetSocketAddress address = context.getLocalAddress();
if (address != null) {
clientSpan.setTag(RpcSpanTags.LOCAL_IP, NetUtils.toIpString(address));
clientSpan.setTag(RpcSpanTags.LOCAL_PORT, address.getPort());
}
// adjust for generic invoke
clientSpan.setTag(RpcSpanTags.METHOD, request.getMethodName());
}
RpcInvokeContext invokeContext = RpcInvokeContext.getContext();
clientSpan.setTag(RpcSpanTags.PHASE_TIME_COST, generateClientPhaseTimeCostSpan(invokeContext));
clientSpan.setTag(RpcSpanTags.SPECIAL_TIME_MARK, generateClientSpecialTimeMarkSpan(invokeContext));
Throwable throwableShow = exceptionThrow;
// 区分出各个异常信息
String resultCode = StringUtils.EMPTY;
// 当前应用或者目标应用
String errorSourceApp = StringUtils.EMPTY;
String tracerErrorCode = StringUtils.EMPTY;
if (throwableShow != null) {
// 客户端异常
if (throwableShow instanceof SofaRpcException) {
SofaRpcException exception = (SofaRpcException) throwableShow;
// 摘要打印
int errorType = exception.getErrorType();
switch(errorType) {
case RpcErrorType.CLIENT_TIMEOUT:
resultCode = TracerResultCode.RPC_RESULT_TIMEOUT_FAILED;
// filter 已经存放
errorSourceApp = clientSpan.getTagsWithStr().get(RpcSpanTags.LOCAL_APP);
tracerErrorCode = TracerResultCode.RPC_ERROR_TYPE_TIMEOUT_ERROR;
break;
case RpcErrorType.CLIENT_ROUTER:
resultCode = TracerResultCode.RPC_RESULT_ROUTE_FAILED;
errorSourceApp = clientSpan.getTagsWithStr().get(RpcSpanTags.LOCAL_APP);
tracerErrorCode = TracerResultCode.RPC_ERROR_TYPE_ADDRESS_ROUTE_ERROR;
break;
case RpcErrorType.CLIENT_SERIALIZE:
case RpcErrorType.CLIENT_DESERIALIZE:
resultCode = TracerResultCode.RPC_RESULT_RPC_FAILED;
errorSourceApp = clientSpan.getTagsWithStr().get(RpcSpanTags.LOCAL_APP);
tracerErrorCode = TracerResultCode.RPC_ERROR_TYPE_SERIALIZE_ERROR;
break;
default:
resultCode = TracerResultCode.RPC_RESULT_RPC_FAILED;
errorSourceApp = ExceptionUtils.isServerException(exception) ? clientSpan.getTagsWithStr().get(RpcSpanTags.REMOTE_APP) : clientSpan.getTagsWithStr().get(RpcSpanTags.LOCAL_APP);
tracerErrorCode = TracerResultCode.RPC_ERROR_TYPE_UNKNOWN_ERROR;
break;
}
} else {
// 这里是客户端的未知异常,目前不会走到这里
resultCode = TracerResultCode.RPC_RESULT_RPC_FAILED;
errorSourceApp = clientSpan.getTagsWithStr().get(RpcSpanTags.LOCAL_APP);
tracerErrorCode = TracerResultCode.RPC_ERROR_TYPE_UNKNOWN_ERROR;
}
} else if (response != null) {
// 服务端rpc异常
if (response.isError()) {
errorSourceApp = clientSpan.getTagsWithStr().get(RpcSpanTags.REMOTE_APP);
tracerErrorCode = TracerResultCode.RPC_ERROR_TYPE_UNKNOWN_ERROR;
resultCode = TracerResultCode.RPC_RESULT_RPC_FAILED;
// 客户端服务端均打印
throwableShow = new SofaRpcException(RpcErrorType.SERVER_UNDECLARED_ERROR, response.getErrorMsg());
} else {
Object ret = response.getAppResponse();
// for server throw exception ,but this class can not be found in current
if (ret instanceof Throwable || "true".equals(response.getResponseProp(RemotingConstants.HEAD_RESPONSE_ERROR))) {
errorSourceApp = clientSpan.getTagsWithStr().get(RpcSpanTags.REMOTE_APP);
// 业务异常
resultCode = TracerResultCode.RPC_RESULT_BIZ_FAILED;
tracerErrorCode = TracerResultCode.RPC_ERROR_TYPE_BIZ_ERROR;
} else {
resultCode = TracerResultCode.RPC_RESULT_SUCCESS;
}
}
}
if (throwableShow != null) {
Map<String, String> contextMap = new HashMap<String, String>();
this.generateClientErrorContext(contextMap, request, clientSpan);
clientSpan.reportError(tracerErrorCode, contextMap, throwableShow, errorSourceApp, ERROR_SOURCE);
}
clientSpan.setTag(RpcSpanTags.RESULT_CODE, resultCode);
// finish client
clientSpan.finish();
if (context != null) {
context.setAttachment(RpcConstants.INTERNAL_KEY_RESULT_CODE, resultCode);
}
// client span
if (clientSpan.getParentSofaTracerSpan() != null) {
// restore parent
sofaTraceContext.push(clientSpan.getParentSofaTracerSpan());
}
}
use of com.alipay.sofa.rpc.context.RpcInvokeContext in project sofa-rpc by sofastack.
the class DefaultClientProxyInvoker method decorateResponse.
@Override
protected void decorateResponse(SofaResponse response) {
// 公共的设置
super.decorateResponse(response);
// 上下文内转外
RpcInternalContext context = RpcInternalContext.getContext();
ResponseFuture future = context.getFuture();
RpcInvokeContext invokeCtx = null;
if (future != null) {
invokeCtx = RpcInvokeContext.getContext();
invokeCtx.setFuture(future);
}
if (RpcInvokeContext.isBaggageEnable()) {
BaggageResolver.pickupFromResponse(invokeCtx, response, true);
}
// bad code
if (RpcInternalContext.isAttachmentEnable()) {
String resultCode = (String) context.getAttachment(INTERNAL_KEY_RESULT_CODE);
if (resultCode != null) {
if (invokeCtx == null) {
invokeCtx = RpcInvokeContext.getContext();
}
invokeCtx.put(RemotingConstants.INVOKE_CTX_RPC_RESULT_CODE, resultCode);
}
}
}
use of com.alipay.sofa.rpc.context.RpcInvokeContext in project sofa-rpc by sofastack.
the class DefaultClientProxyInvoker method decorateRequest.
@Override
protected void decorateRequest(SofaRequest request) {
// 公共的设置
super.decorateRequest(request);
// 缓存是为了加快速度
request.setTargetServiceUniqueName(serviceName);
request.setSerializeType(serializeType == null ? 0 : serializeType);
if (!consumerConfig.isGeneric()) {
// 找到调用类型, generic的时候类型在filter里进行判断
request.setInvokeType(consumerConfig.getMethodInvokeType(request.getMethodName()));
}
RpcInvokeContext invokeCtx = RpcInvokeContext.peekContext();
RpcInternalContext internalContext = RpcInternalContext.getContext();
if (invokeCtx != null) {
// 如果用户设置了调用级别回调函数
SofaResponseCallback responseCallback = invokeCtx.getResponseCallback();
if (responseCallback != null) {
request.setSofaResponseCallback(responseCallback);
// 一次性用完
invokeCtx.setResponseCallback(null);
invokeCtx.put(RemotingConstants.INVOKE_CTX_IS_ASYNC_CHAIN, isSendableResponseCallback(responseCallback));
}
// 如果用户设置了调用级别超时时间
Integer timeout = invokeCtx.getTimeout();
if (timeout != null) {
request.setTimeout(timeout);
// 一次性用完
invokeCtx.setTimeout(null);
}
// 如果用户指定了调用的URL
String targetURL = invokeCtx.getTargetURL();
if (targetURL != null) {
internalContext.setAttachment(HIDDEN_KEY_PINPOINT, targetURL);
// 一次性用完
invokeCtx.setTargetURL(null);
}
// 如果用户指定了透传数据
if (RpcInvokeContext.isBaggageEnable()) {
// 需要透传
BaggageResolver.carryWithRequest(invokeCtx, request);
internalContext.setAttachment(HIDDEN_KEY_INVOKE_CONTEXT, invokeCtx);
}
}
if (RpcInternalContext.isAttachmentEnable()) {
internalContext.setAttachment(INTERNAL_KEY_APP_NAME, consumerConfig.getAppName());
internalContext.setAttachment(INTERNAL_KEY_PROTOCOL_NAME, consumerConfig.getProtocol());
}
// 额外属性通过HEAD传递给服务端
request.addRequestProp(RemotingConstants.HEAD_APP_NAME, consumerConfig.getAppName());
request.addRequestProp(RemotingConstants.HEAD_PROTOCOL, consumerConfig.getProtocol());
}
use of com.alipay.sofa.rpc.context.RpcInvokeContext in project sofa-rpc by sofastack.
the class InvocationStatDimensionStatTest method prepareInvokeContext.
private void prepareInvokeContext() {
final RpcInvokeContext context = new RpcInvokeContext();
context.setResponseCallback(new SofaResponseCallback() {
@Override
public void onAppResponse(final Object appResponse, String methodName, RequestBase request) {
// 放到 future 中方便测试.
LOGGER.info("回调成功" + appResponse);
context.setFuture(new ResponseFuture<String>() {
@Override
public ResponseFuture addListeners(List<SofaResponseCallback> sofaResponseCallbacks) {
return null;
}
@Override
public ResponseFuture addListener(SofaResponseCallback sofaResponseCallback) {
return null;
}
@Override
public boolean cancel(boolean mayInterruptIfRunning) {
return false;
}
@Override
public boolean isCancelled() {
return false;
}
@Override
public boolean isDone() {
return false;
}
@Override
public String get() throws InterruptedException, ExecutionException {
return (String) appResponse;
}
@Override
public String get(long timeout, TimeUnit unit) throws InterruptedException, ExecutionException, TimeoutException {
return null;
}
});
}
@Override
public void onAppException(Throwable throwable, String methodName, RequestBase request) {
LOGGER.info("回调发生应用异常" + throwable);
}
@Override
public void onSofaException(SofaRpcException sofaException, String methodName, RequestBase request) {
LOGGER.info("回调发生sofa异常" + sofaException);
}
});
RpcInvokeContext.setContext(context);
}
use of com.alipay.sofa.rpc.context.RpcInvokeContext in project sofa-rpc by sofastack.
the class BaggageFutureTest method doTest.
@Override
public void doTest() throws Exception {
ServerConfig serverConfig = new ServerConfig().setProtocol(RpcConstants.PROTOCOL_TYPE_BOLT).setPort(12299);
// C服务的服务端
CSampleServiceImpl refC = new CSampleServiceImpl();
ProviderConfig<SampleService> serviceBeanC = new ProviderConfig<SampleService>();
serviceBeanC.setInterfaceId(SampleService.class.getName());
serviceBeanC.setApplication(new ApplicationConfig().setAppName("CCC"));
serviceBeanC.setUniqueId("C2");
serviceBeanC.setRef(refC);
serviceBeanC.setServer(serverConfig);
serviceBeanC.setRegister(false);
serviceBeanC.export();
// D服务的服务端
DSampleServiceImpl refD = new DSampleServiceImpl();
ProviderConfig<SampleService> serviceBeanD = new ProviderConfig<SampleService>();
serviceBeanD.setInterfaceId(SampleService.class.getName());
serviceBeanD.setApplication(new ApplicationConfig().setAppName("DDD"));
serviceBeanD.setUniqueId("D2");
serviceBeanD.setRef(refD);
serviceBeanD.setServer(serverConfig);
serviceBeanD.setRegister(false);
serviceBeanD.export();
// B服务里的C服务客户端
ConsumerConfig referenceBeanC = new ConsumerConfig();
referenceBeanC.setApplication(new ApplicationConfig().setAppName("BBB"));
referenceBeanC.setInterfaceId(SampleService.class.getName());
referenceBeanC.setUniqueId("C2");
referenceBeanC.setDirectUrl("localhost:12299");
referenceBeanC.setTimeout(1000);
MethodConfig methodConfigC = new MethodConfig().setName("hello").setInvokeType(RpcConstants.INVOKER_TYPE_FUTURE);
referenceBeanC.setMethods(Collections.singletonList(methodConfigC));
SampleService sampleServiceC = (SampleService) referenceBeanC.refer();
// B服务里的D服务客户端
ConsumerConfig referenceBeanD = new ConsumerConfig();
referenceBeanD.setApplication(new ApplicationConfig().setAppName("BBB"));
referenceBeanD.setInterfaceId(SampleService.class.getName());
referenceBeanD.setUniqueId("D2");
referenceBeanD.setDirectUrl("localhost:12299?p=1&v=4.0");
referenceBeanD.setTimeout(1000);
MethodConfig methodConfigD = new MethodConfig().setName("hello").setInvokeType(RpcConstants.INVOKER_TYPE_FUTURE);
referenceBeanD.setMethods(Collections.singletonList(methodConfigD));
SampleService sampleServiceD = (SampleService) referenceBeanD.refer();
// B服务的服务端
BFutureSampleServiceImpl refB = new BFutureSampleServiceImpl(sampleServiceC, sampleServiceD);
ProviderConfig<SampleService> ServiceBeanB = new ProviderConfig<SampleService>();
ServiceBeanB.setInterfaceId(SampleService.class.getName());
ServiceBeanB.setApplication(new ApplicationConfig().setAppName("BBB"));
ServiceBeanB.setUniqueId("B2");
ServiceBeanB.setRef(refB);
ServiceBeanB.setServer(serverConfig);
ServiceBeanB.setRegister(false);
ServiceBeanB.export();
// A 服务
ConsumerConfig referenceBeanA = new ConsumerConfig();
referenceBeanA.setApplication(new ApplicationConfig().setAppName("AAA"));
referenceBeanA.setUniqueId("B2");
referenceBeanA.setInterfaceId(SampleService.class.getName());
referenceBeanA.setDirectUrl("localhost:12299");
referenceBeanA.setTimeout(3000);
MethodConfig methodConfigA = new MethodConfig().setName("hello").setInvokeType(RpcConstants.INVOKER_TYPE_FUTURE);
referenceBeanA.setMethods(Collections.singletonList(methodConfigA));
SampleService service = (SampleService) referenceBeanA.refer();
// 开始测试
RpcInvokeContext context = RpcInvokeContext.getContext();
context.putRequestBaggage("reqBaggageB", "a2bbb");
context.putRequestBaggage("reqBaggageC", "a2ccc");
context.putRequestBaggage("reqBaggageD", "a2ddd");
String ret = service.hello();
Assert.assertEquals(ret, null);
ret = (String) SofaResponseFuture.getResponse(5000, false);
Assert.assertEquals(ret, "hello world chello world d");
Assert.assertEquals(refB.getReqBaggage(), "a2bbb");
Assert.assertEquals(refC.getReqBaggage(), "a2ccc");
Assert.assertEquals(refD.getReqBaggage(), "a2ddd");
Assert.assertEquals(context.getResponseBaggage("respBaggageB"), "b2aaa");
Assert.assertEquals(context.getResponseBaggage("respBaggageC"), "c2aaa");
Assert.assertEquals(context.getResponseBaggage("respBaggageD"), "d2aaa");
Assert.assertNull(context.getResponseBaggage("respBaggageB_force"));
Assert.assertNull(context.getResponseBaggage("respBaggageC_force"));
Assert.assertNull(context.getResponseBaggage("respBaggageD_force"));
RpcInvokeContext.removeContext();
context = RpcInvokeContext.getContext();
ret = null;
ret = service.hello();
Assert.assertEquals(ret, null);
ret = (String) SofaResponseFuture.getResponse(5000, false);
Assert.assertEquals(ret, "hello world chello world d");
Assert.assertNull(refB.getReqBaggage());
Assert.assertNull(refC.getReqBaggage());
Assert.assertNull(refD.getReqBaggage());
Assert.assertNull(context.getResponseBaggage("respBaggageB"));
Assert.assertNull(context.getResponseBaggage("respBaggageC"));
Assert.assertNull(context.getResponseBaggage("respBaggageD"));
Assert.assertEquals(context.getResponseBaggage("respBaggageB_force"), "b2aaaff");
Assert.assertEquals(context.getResponseBaggage("respBaggageC_force"), "c2aaaff");
Assert.assertEquals(context.getResponseBaggage("respBaggageD_force"), "d2aaaff");
}
Aggregations