use of com.alipay.sofa.rpc.context.RpcInternalContext in project sofa-rpc by sofastack.
the class AbstractCluster method doSendMsg.
/**
* 调用客户端
*
* @param transport 客户端连接
* @param request Request对象
* @return 调用结果
* @throws SofaRpcException rpc异常
*/
protected SofaResponse doSendMsg(ProviderInfo providerInfo, ClientTransport transport, SofaRequest request) throws SofaRpcException {
RpcInternalContext context = RpcInternalContext.getContext();
// 添加调用的服务端远程地址
RpcInternalContext.getContext().setRemoteAddress(providerInfo.getHost(), providerInfo.getPort());
try {
// 根据服务端版本特殊处理
checkProviderVersion(providerInfo, request);
String invokeType = request.getInvokeType();
int timeout = resolveTimeout(request, consumerConfig, providerInfo);
SofaResponse response = null;
// 同步调用
if (RpcConstants.INVOKER_TYPE_SYNC.equals(invokeType)) {
long start = RpcRuntimeContext.now();
try {
response = transport.syncSend(request, timeout);
} finally {
if (RpcInternalContext.isAttachmentEnable()) {
long elapsed = RpcRuntimeContext.now() - start;
context.setAttachment(RpcConstants.INTERNAL_KEY_CLIENT_ELAPSE, elapsed);
}
}
} else // 单向调用
if (RpcConstants.INVOKER_TYPE_ONEWAY.equals(invokeType)) {
long start = RpcRuntimeContext.now();
try {
transport.oneWaySend(request, timeout);
response = buildEmptyResponse(request);
} finally {
if (RpcInternalContext.isAttachmentEnable()) {
long elapsed = RpcRuntimeContext.now() - start;
context.setAttachment(RpcConstants.INTERNAL_KEY_CLIENT_ELAPSE, elapsed);
}
}
} else // Callback调用
if (RpcConstants.INVOKER_TYPE_CALLBACK.equals(invokeType)) {
// 调用级别回调监听器
SofaResponseCallback sofaResponseCallback = request.getSofaResponseCallback();
if (sofaResponseCallback == null) {
SofaResponseCallback methodResponseCallback = consumerConfig.getMethodOnreturn(request.getMethodName());
if (methodResponseCallback != null) {
// 方法的Callback
request.setSofaResponseCallback(methodResponseCallback);
}
}
// 记录发送开始时间
context.setAttachment(RpcConstants.INTERNAL_KEY_CLIENT_SEND_TIME, RpcRuntimeContext.now());
// 开始调用
transport.asyncSend(request, timeout);
response = buildEmptyResponse(request);
} else // Future调用
if (RpcConstants.INVOKER_TYPE_FUTURE.equals(invokeType)) {
// 记录发送开始时间
context.setAttachment(RpcConstants.INTERNAL_KEY_CLIENT_SEND_TIME, RpcRuntimeContext.now());
// 开始调用
ResponseFuture future = transport.asyncSend(request, timeout);
// 放入线程上下文
RpcInternalContext.getContext().setFuture(future);
response = buildEmptyResponse(request);
} else {
throw new SofaRpcException(RpcErrorType.CLIENT_UNDECLARED_ERROR, "Unknown invoke type:" + invokeType);
}
return response;
} catch (SofaRpcException e) {
throw e;
} catch (Throwable e) {
// 客户端其它异常
throw new SofaRpcException(RpcErrorType.CLIENT_UNDECLARED_ERROR, e);
}
}
use of com.alipay.sofa.rpc.context.RpcInternalContext in project sofa-rpc by sofastack.
the class AbstractSerializerTest method buildDeserializeError.
@Test
public void buildDeserializeError() {
RpcInternalContext old = RpcInternalContext.peekContext();
try {
RpcInternalContext.removeContext();
SofaRpcException exception = serializer.buildDeserializeError("xx");
Assert.assertEquals(RpcErrorType.UNKNOWN, exception.getErrorType());
RpcInternalContext.getContext().setProviderSide(true);
exception = serializer.buildDeserializeError("xx");
Assert.assertEquals(RpcErrorType.SERVER_DESERIALIZE, exception.getErrorType());
RpcInternalContext.getContext().setProviderSide(false);
exception = serializer.buildDeserializeError("xx");
Assert.assertEquals(RpcErrorType.CLIENT_DESERIALIZE, exception.getErrorType());
RpcInternalContext.removeContext();
exception = serializer.buildDeserializeError("xx", new RuntimeException());
Assert.assertEquals(RpcErrorType.UNKNOWN, exception.getErrorType());
RpcInternalContext.getContext().setProviderSide(true);
exception = serializer.buildDeserializeError("xx", new RuntimeException());
Assert.assertEquals(RpcErrorType.SERVER_DESERIALIZE, exception.getErrorType());
RpcInternalContext.getContext().setProviderSide(false);
exception = serializer.buildDeserializeError("xx", new RuntimeException());
Assert.assertEquals(RpcErrorType.CLIENT_DESERIALIZE, exception.getErrorType());
} finally {
RpcInternalContext.setContext(old);
}
}
use of com.alipay.sofa.rpc.context.RpcInternalContext in project sofa-rpc by sofastack.
the class TripleClientTransport method syncSend.
@Override
public SofaResponse syncSend(SofaRequest request, int timeout) throws SofaRpcException {
SofaResponse sofaResponse = null;
SofaRpcException throwable = null;
try {
RpcInternalContext context = RpcInternalContext.getContext();
beforeSend(context, request);
RpcInvokeContext invokeContext = RpcInvokeContext.getContext();
invokeContext.put(TripleContants.SOFA_REQUEST_KEY, request);
invokeContext.put(TripleContants.SOFA_CONSUMER_CONFIG_KEY, transportConfig.getConsumerConfig());
sofaResponse = tripleClientInvoker.invoke(request, timeout);
return sofaResponse;
} catch (Exception e) {
throwable = convertToRpcException(e);
throw throwable;
} finally {
if (EventBus.isEnable(ClientSyncReceiveEvent.class)) {
EventBus.post(new ClientSyncReceiveEvent(transportConfig.getConsumerConfig(), transportConfig.getProviderInfo(), request, sofaResponse, throwable));
}
}
}
use of com.alipay.sofa.rpc.context.RpcInternalContext in project sofa-rpc by sofastack.
the class ConsumerTracerFilter method invoke.
@Override
public SofaResponse invoke(FilterInvoker invoker, SofaRequest request) throws SofaRpcException {
SofaTraceContext sofaTraceContext = SofaTraceContextHolder.getSofaTraceContext();
SofaTracerSpan clientSpan = sofaTraceContext.getCurrentSpan();
clientSpan.setTag(RpcSpanTags.INVOKE_TYPE, request.getInvokeType());
RpcInternalContext context = RpcInternalContext.getContext();
clientSpan.setTag(RpcSpanTags.ROUTE_RECORD, (String) context.getAttachment(RpcConstants.INTERNAL_KEY_ROUTER_RECORD));
ProviderInfo providerInfo = context.getProviderInfo();
if (providerInfo != null) {
clientSpan.setTag(RpcSpanTags.REMOTE_APP, providerInfo.getStaticAttr(ProviderInfoAttrs.ATTR_APP_NAME));
clientSpan.setTag(RpcSpanTags.REMOTE_IP, providerInfo.getHost() + ":" + providerInfo.getPort());
}
return invoker.invoke(request);
// 因为异步的场景,所以received不写在这里
}
use of com.alipay.sofa.rpc.context.RpcInternalContext in project sofa-rpc by sofastack.
the class ProviderTracerFilter method invoke.
@Override
public SofaResponse invoke(FilterInvoker invoker, SofaRequest request) throws SofaRpcException {
SofaTracerSpan serverSpan = null;
try {
SofaTraceContext sofaTraceContext = SofaTraceContextHolder.getSofaTraceContext();
serverSpan = sofaTraceContext.getCurrentSpan();
if (serverSpan != null) {
RpcInternalContext context = RpcInternalContext.getContext();
serverSpan.setTag(RpcSpanTags.SERVICE, request.getTargetServiceUniqueName());
serverSpan.setTag(RpcSpanTags.METHOD, request.getMethodName());
// 客户端地址
serverSpan.setTag(RpcSpanTags.REMOTE_IP, context.getRemoteHostName());
// 从请求里获取ConsumerTracerFilter额外传递的信息
serverSpan.setTag(RpcSpanTags.REMOTE_APP, (String) request.getRequestProp(HEAD_APP_NAME));
serverSpan.setTag(RpcSpanTags.PROTOCOL, (String) request.getRequestProp(HEAD_PROTOCOL));
serverSpan.setTag(RpcSpanTags.INVOKE_TYPE, (String) request.getRequestProp(HEAD_INVOKE_TYPE));
ProviderConfig providerConfig = (ProviderConfig) invoker.getConfig();
serverSpan.setTag(RpcSpanTags.LOCAL_APP, providerConfig.getAppName());
serverSpan.setTag(RpcSpanTags.SERVER_THREAD_POOL_WAIT_TIME, (Number) context.getAttachment(RpcConstants.INTERNAL_KEY_PROCESS_WAIT_TIME));
}
return invoker.invoke(request);
} finally {
if (serverSpan != null) {
serverSpan.setTag(RpcSpanTags.SERVER_BIZ_TIME, (Number) RpcInternalContext.getContext().getAttachment(RpcConstants.INTERNAL_KEY_IMPL_ELAPSE));
}
}
}
Aggregations