use of com.alipay.sofa.rpc.core.response.SofaResponse in project sofa-rpc by sofastack.
the class SofaRpcMetricsTest method testMicrometerMetrics.
@Test
public void testMicrometerMetrics() throws NoSuchMethodException, InvocationTargetException, IllegalAccessException {
SimpleMeterRegistry registry = new SimpleMeterRegistry();
try (SofaRpcMetrics metrics = new SofaRpcMetrics()) {
metrics.bindTo(registry);
Method handleEvent = EventBus.class.getDeclaredMethod("handleEvent", Subscriber.class, Event.class);
handleEvent.setAccessible(true);
SofaRequest request = buildRequest();
SofaResponse response = buildResponse();
RpcInternalContext.getContext().setAttachment(RpcConstants.INTERNAL_KEY_CLIENT_ELAPSE, 100).setAttachment(RpcConstants.INTERNAL_KEY_IMPL_ELAPSE, 10).setAttachment(RpcConstants.INTERNAL_KEY_REQ_SIZE, 3).setAttachment(RpcConstants.INTERNAL_KEY_RESP_SIZE, 4);
handleEvent.invoke(EventBus.class, metrics, new ClientEndInvokeEvent(request, response, null));
handleEvent.invoke(EventBus.class, metrics, new ServerSendEvent(request, response, null));
ServerConfig serverConfig = new ServerConfig();
handleEvent.invoke(EventBus.class, metrics, new ServerStartedEvent(serverConfig, new ThreadPoolExecutor(1, 1, 0L, TimeUnit.MILLISECONDS, new LinkedBlockingQueue<>())));
handleEvent.invoke(EventBus.class, metrics, new ServerStoppedEvent(serverConfig));
handleEvent.invoke(EventBus.class, metrics, new ProviderPubEvent(new ProviderConfig<>()));
handleEvent.invoke(EventBus.class, metrics, new ConsumerSubEvent(new ConsumerConfig<>()));
Assert.assertEquals(12, registry.getMeters().size());
}
}
use of com.alipay.sofa.rpc.core.response.SofaResponse in project sofa-rpc by sofastack.
the class SofaAsyncHystrixCommand method buildEmptyResponse.
// Copy from AbstractCluster#buildEmptyResponse
private SofaResponse buildEmptyResponse(SofaRequest request) {
SofaResponse response = new SofaResponse();
Method method = request.getMethod();
if (method != null) {
response.setAppResponse(ClassUtils.getDefaultPrimitiveValue(method.getReturnType()));
}
return response;
}
use of com.alipay.sofa.rpc.core.response.SofaResponse in project sofa-rpc by sofastack.
the class SofaHystrixCommand method getFallback.
protected SofaResponse getFallback(SofaResponse response, Throwable t) {
FallbackFactory fallbackFactory = SofaHystrixConfig.loadFallbackFactory((ConsumerConfig) invoker.getConfig());
if (fallbackFactory == null) {
return super.getFallback();
}
Object fallback = fallbackFactory.create(new FallbackContext(invoker, request, response, t));
if (fallback == null) {
return super.getFallback();
}
try {
Object fallbackResult = request.getMethod().invoke(fallback, request.getMethodArgs());
SofaResponse actualResponse = new SofaResponse();
actualResponse.setAppResponse(fallbackResult);
return actualResponse;
} catch (IllegalAccessException e) {
throw new SofaRpcRuntimeException(LogCodes.getLog(LogCodes.ERROR_HYSTRIX_FALLBACK_FAIL), e);
} catch (InvocationTargetException e) {
throw new SofaRpcRuntimeException(LogCodes.getLog(LogCodes.ERROR_HYSTRIX_FALLBACK_FAIL), e.getTargetException());
}
}
use of com.alipay.sofa.rpc.core.response.SofaResponse in project sofa-rpc by sofastack.
the class SofaHystrixCommand method run.
@Override
protected SofaResponse run() throws Exception {
RpcInternalContext.setContext(rpcInternalContext);
RpcInvokeContext.setContext(rpcInvokeContext);
SofaResponse sofaResponse = invoker.invoke(request);
if (!sofaResponse.isError()) {
return sofaResponse;
}
return getFallback(sofaResponse, null);
}
use of com.alipay.sofa.rpc.core.response.SofaResponse in project sofa-rpc by sofastack.
the class ServerReqHeaderInterceptor method interceptCall.
@Override
public <ReqT, RespT> ServerCall.Listener<ReqT> interceptCall(final ServerCall<ReqT, RespT> call, final Metadata requestHeaders, ServerCallHandler<ReqT, RespT> next) {
final ServerServiceDefinition serverServiceDefinition = this.getServerServiceDefinition();
SofaResponse sofaResponse = new SofaResponse();
final Throwable[] throwable = { null };
SofaRequest sofaRequest = new SofaRequest();
TripleTracerAdapter.serverReceived(sofaRequest, serverServiceDefinition, call, requestHeaders);
SofaTraceContext sofaTraceContext = SofaTraceContextHolder.getSofaTraceContext();
SofaTracerSpan serverSpan = sofaTraceContext.getCurrentSpan();
Context ctxWithSpan = Context.current().withValue(TracingContextKey.getKey(), serverSpan).withValue(TracingContextKey.getSpanContextKey(), serverSpan.context()).withValue(TracingContextKey.getKeySofaRequest(), sofaRequest);
// 这里和下面不在一个线程
if (RpcRunningState.isDebugMode()) {
LOGGER.info("[1]header received from client:" + requestHeaders);
}
ServerCall<ReqT, RespT> realCall = new ForwardingServerCall.SimpleForwardingServerCall<ReqT, RespT>(call) {
@Override
public void sendHeaders(Metadata responseHeaders) {
if (RpcRunningState.isDebugMode()) {
LOGGER.info("[4]send response header:{}", responseHeaders);
}
super.sendHeaders(responseHeaders);
}
// 服务端发完了
@Override
public void sendMessage(RespT message) {
if (RpcRunningState.isDebugMode()) {
LOGGER.info("[5]send response message:{}", message);
}
super.sendMessage(message);
sofaResponse.setAppResponse(message);
}
@Override
public void close(Status status, Metadata trailers) {
if (RpcRunningState.isDebugMode()) {
LOGGER.info("[6]send response message:{},trailers:{}", status, trailers);
}
super.close(status, trailers);
}
};
ServerCall.Listener<ReqT> listenerWithContext = Contexts.interceptCall(ctxWithSpan, realCall, requestHeaders, next);
ForwardingServerCallListener.SimpleForwardingServerCallListener<ReqT> result = new ForwardingServerCallListener.SimpleForwardingServerCallListener<ReqT>(listenerWithContext) {
// 完成的时候走到这里
@Override
public void onComplete() {
// 和代码执行不一定在一个线程池
super.onComplete();
if (RpcRunningState.isDebugMode()) {
LOGGER.info("[7]server processed done received from client:" + requestHeaders);
}
TripleTracerAdapter.serverReceived(sofaRequest, serverServiceDefinition, call, requestHeaders);
// 进行一下补偿
SofaTraceContext sofaTraceContext = SofaTraceContextHolder.getSofaTraceContext();
SofaTracerSpan serverSpan = sofaTraceContext.getCurrentSpan();
SofaTracerSpan originalSpan = (SofaTracerSpan) TracingContextKey.getKey().get(ctxWithSpan);
serverSpan.setStartTime(originalSpan.getStartTime());
serverSpan.setTag("remote.ip", originalSpan.getTagsWithStr().get("remote.ip"));
long endTime = RpcRuntimeContext.now();
serverSpan.setTag("biz.impl.time", endTime - originalSpan.getStartTime());
TripleTracerAdapter.serverSend(sofaRequest, requestHeaders, sofaResponse, throwable[0]);
}
// 客户端发完了
@Override
public void onHalfClose() {
try {
doOnHalfClose();
} finally {
RpcInvokeContext.removeContext();
}
}
private void doOnHalfClose() {
if (RpcRunningState.isDebugMode()) {
LOGGER.info("[2]body received done from client:" + requestHeaders);
}
// 服务端收到所有信息
TripleTracerAdapter.serverReceived(sofaRequest, serverServiceDefinition, call, requestHeaders);
try {
super.onHalfClose();
} catch (Throwable t) {
// 统一处理异常
StatusRuntimeException exception = fromThrowable(t);
// 调用 call.close() 发送 Status 和 metadata
// 这个方式和 onError()本质是一样的
call.close(exception.getStatus(), exception.getTrailers());
throwable[0] = t;
}
}
private StatusRuntimeException fromThrowable(Throwable t) {
final Metadata trailers = new Metadata();
return new StatusRuntimeException(Status.UNKNOWN, trailers);
}
};
return result;
}
Aggregations