Search in sources :

Example 81 with SofaResponse

use of com.alipay.sofa.rpc.core.response.SofaResponse in project sofa-rpc by sofastack.

the class GenericServiceImpl method generic.

@Override
public void generic(Request request, StreamObserver<Response> responseObserver) {
    SofaRequest sofaRequest = TracingContextKey.getKeySofaRequest().get(Context.current());
    String methodName = sofaRequest.getMethodName();
    String interfaceName = sofaRequest.getInterfaceName();
    ClassLoader oldClassLoader = Thread.currentThread().getContextClassLoader();
    try {
        Class proxyClass = ClassTypeUtils.getClass(interfaceName);
        ClassLoader interfaceClassLoader = proxyClass.getClassLoader();
        Thread.currentThread().setContextClassLoader(interfaceClassLoader);
        Class[] argTypes = getArgTypes(request);
        Serializer serializer = SerializerFactory.getSerializer(request.getSerializeType());
        Method declaredMethod = proxyClass.getDeclaredMethod(methodName, argTypes);
        Object[] invokeArgs = getInvokeArgs(request, argTypes, serializer);
        // fill sofaRequest
        sofaRequest.setMethod(declaredMethod);
        sofaRequest.setMethodArgs(invokeArgs);
        sofaRequest.setMethodArgSigs(ClassTypeUtils.getTypeStrs(argTypes, true));
        SofaResponse response = invoker.invoke(sofaRequest);
        Object ret = getAppResponse(declaredMethod, response);
        Response.Builder builder = Response.newBuilder();
        builder.setSerializeType(request.getSerializeType());
        builder.setType(declaredMethod.getReturnType().getName());
        builder.setData(ByteString.copyFrom(serializer.encode(ret, null).array()));
        Response build = builder.build();
        responseObserver.onNext(build);
        responseObserver.onCompleted();
    } catch (Exception e) {
        LOGGER.error("Invoke " + methodName + " error:", e);
        throw new SofaRpcRuntimeException(e);
    } finally {
        Thread.currentThread().setContextClassLoader(oldClassLoader);
    }
}
Also used : SofaRequest(com.alipay.sofa.rpc.core.request.SofaRequest) ByteString(com.google.protobuf.ByteString) Method(java.lang.reflect.Method) SofaRpcException(com.alipay.sofa.rpc.core.exception.SofaRpcException) SofaRpcRuntimeException(com.alipay.sofa.rpc.core.exception.SofaRpcRuntimeException) Response(triple.Response) SofaResponse(com.alipay.sofa.rpc.core.response.SofaResponse) SofaRpcRuntimeException(com.alipay.sofa.rpc.core.exception.SofaRpcRuntimeException) SofaResponse(com.alipay.sofa.rpc.core.response.SofaResponse) Serializer(com.alipay.sofa.rpc.codec.Serializer)

Example 82 with SofaResponse

use of com.alipay.sofa.rpc.core.response.SofaResponse in project sofa-rpc by sofastack.

the class RestLookoutAdapter method sendRestServerSendEvent.

public static void sendRestServerSendEvent(RestServerSendEvent restServerSendEvent) {
    // this is special for rest
    if (EventBus.isEnable(ServerSendEvent.class)) {
        SofaRequest request = new SofaRequest();
        String appName = (String) RpcRuntimeContext.get(RpcRuntimeContext.KEY_APPNAME);
        request.setTargetAppName(appName);
        request.addRequestProp(RemotingConstants.HEAD_APP_NAME, restServerSendEvent.getRequest().getHttpHeaders().getHeaderString(RemotingConstants.HEAD_APP_NAME));
        RpcInternalContext context = RpcInternalContext.getContext();
        request.setTargetServiceUniqueName((String) context.getAttachment(INTERNAL_KEY_PREFIX + RestConstants.REST_SERVICE_KEY));
        request.setMethodName((String) context.getAttachment(INTERNAL_KEY_PREFIX + RestConstants.REST_METHODNAME_KEY));
        request.addRequestProp(RemotingConstants.HEAD_PROTOCOL, RpcConstants.PROTOCOL_TYPE_REST);
        request.setInvokeType(RpcConstants.INVOKER_TYPE_SYNC);
        SofaResponse response = new SofaResponse();
        if (restServerSendEvent.getThrowable() != null) {
            response.setErrorMsg(restServerSendEvent.getThrowable().getMessage());
        }
        final ServerSendEvent event = new ServerSendEvent(request, response, restServerSendEvent.getThrowable());
        EventBus.post(event);
    }
}
Also used : SofaRequest(com.alipay.sofa.rpc.core.request.SofaRequest) ServerSendEvent(com.alipay.sofa.rpc.event.ServerSendEvent) RestServerSendEvent(com.alipay.sofa.rpc.event.rest.RestServerSendEvent) RpcInternalContext(com.alipay.sofa.rpc.context.RpcInternalContext) SofaResponse(com.alipay.sofa.rpc.core.response.SofaResponse)

Example 83 with SofaResponse

use of com.alipay.sofa.rpc.core.response.SofaResponse in project sofa-rpc by sofastack.

the class SyncInvokeClientHandler method doOnResponse.

@Override
public void doOnResponse(Object result) {
    if (rpcFuture == null) {
        return;
    }
    ClassLoader oldCl = Thread.currentThread().getContextClassLoader();
    SofaResponse response = (SofaResponse) result;
    try {
        Thread.currentThread().setContextClassLoader(this.classLoader);
        RpcInternalContext.setContext(context);
        decode(response);
        rpcFuture.setSuccess(response);
    } finally {
        Thread.currentThread().setContextClassLoader(oldCl);
        RpcInvokeContext.removeContext();
        RpcInternalContext.removeAllContext();
    }
}
Also used : SofaResponse(com.alipay.sofa.rpc.core.response.SofaResponse)

Example 84 with SofaResponse

use of com.alipay.sofa.rpc.core.response.SofaResponse in project sofa-rpc by sofastack.

the class UniqueIdInvokerTest method test.

@Test
public void test() {
    try {
        // Prepare data
        UniqueIdInvoker uniqueIdInvoker = new UniqueIdInvoker();
        ProviderConfig uniqueOne = new ProviderConfig();
        uniqueOne.setInterfaceId(INTERFACE_ID);
        uniqueOne.setUniqueId(UNIQUE_ID_ONE);
        uniqueIdInvoker.registerInvoker(uniqueOne, new UniqueIdTestInvoker(UNIQUE_ID_ONE));
        ProviderConfig uniqueTwo = new ProviderConfig();
        uniqueTwo.setInterfaceId(INTERFACE_ID);
        uniqueTwo.setUniqueId(UNIQUE_ID_TWO);
        uniqueIdInvoker.registerInvoker(uniqueTwo, new UniqueIdTestInvoker(UNIQUE_ID_TWO));
        // Case 1: Invoke invoker which unique id is one
        SofaRequest sofaRequest = new SofaRequest();
        sofaRequest.setInterfaceName(INTERFACE_ID);
        RpcInvokeContext.getContext().put(TripleContants.SOFA_UNIQUE_ID, UNIQUE_ID_ONE);
        SofaResponse sofaResponse = uniqueIdInvoker.invoke(sofaRequest);
        String appResponse = (String) sofaResponse.getAppResponse();
        Assert.assertEquals(appResponse, UNIQUE_ID_ONE);
        // Case 2: Invoke invoker which unique id is two
        RpcInvokeContext.getContext().put(TripleContants.SOFA_UNIQUE_ID, UNIQUE_ID_TWO);
        sofaResponse = uniqueIdInvoker.invoke(sofaRequest);
        appResponse = (String) sofaResponse.getAppResponse();
        Assert.assertEquals(appResponse, UNIQUE_ID_TWO);
        // Case 3: There was only one invoker in UniqueIdInvoker and can not find invoker without unique id,
        // invoke the last invoker.
        uniqueIdInvoker.unRegisterInvoker(uniqueTwo);
        RpcInvokeContext.getContext().put(TripleContants.SOFA_UNIQUE_ID, "");
        sofaResponse = uniqueIdInvoker.invoke(sofaRequest);
        appResponse = (String) sofaResponse.getAppResponse();
        Assert.assertEquals(appResponse, UNIQUE_ID_ONE);
        // Case 3: There was only one invoker in UniqueIdInvoker and can not find invoker with unique id,
        // invoke fail.
        RpcInvokeContext.getContext().put(TripleContants.SOFA_UNIQUE_ID, UNIQUE_ID_TWO);
        boolean throwException = false;
        try {
            uniqueIdInvoker.invoke(sofaRequest);
        } catch (SofaRpcException throwable) {
            // Except exception
            throwException = true;
        }
        Assert.assertTrue(throwException);
        // Case 4: There was more than one invoker in UniqueIdInvoker, can not find invoker with unique id two,
        // invoke fail.
        ProviderConfig uniqueThree = new ProviderConfig();
        uniqueThree.setInterfaceId(INTERFACE_ID);
        uniqueThree.setUniqueId(UNIQUE_ID_THREE);
        uniqueIdInvoker.registerInvoker(uniqueThree, new UniqueIdTestInvoker(UNIQUE_ID_THREE));
        RpcInvokeContext.getContext().put(TripleContants.SOFA_UNIQUE_ID, UNIQUE_ID_TWO);
        throwException = false;
        try {
            uniqueIdInvoker.invoke(sofaRequest);
        } catch (SofaRpcException throwable) {
            // Except exception
            throwException = true;
        }
        Assert.assertTrue(throwException);
    } finally {
        RpcInvokeContext.removeContext();
    }
}
Also used : SofaRequest(com.alipay.sofa.rpc.core.request.SofaRequest) ProviderConfig(com.alipay.sofa.rpc.config.ProviderConfig) SofaResponse(com.alipay.sofa.rpc.core.response.SofaResponse) SofaRpcException(com.alipay.sofa.rpc.core.exception.SofaRpcException) Test(org.junit.Test)

Example 85 with SofaResponse

use of com.alipay.sofa.rpc.core.response.SofaResponse in project sofa-rpc by sofastack.

the class RpcSofaTracerTest method testTracerInit.

@Test
public void testTracerInit() throws Exception {
    try {
        // 注册 digest
        AsyncCommonDigestAppenderManager asyncDigestManager = SofaTracerDigestReporterAsyncManager.getSofaTracerDigestReporterAsyncManager();
        Field tracerField = RpcSofaTracer.class.getDeclaredField("sofaTracer");
        tracerField.setAccessible(true);
        // OpenTracing tracer 标准实现
        SofaTracer tracer = (SofaTracer) tracerField.get(this.rpcSofaTracer);
        assertTrue(tracer != null);
        Reporter clientReporter = tracer.getClientReporter();
        assertNotNull(clientReporter);
        assertTrue(clientReporter instanceof DiskReporterImpl);
        DiskReporterImpl clientDisk = (DiskReporterImpl) clientReporter;
        assertEquals(clientDisk.getDigestReporterType(), RpcTracerLogEnum.RPC_CLIENT_DIGEST.getDefaultLogName());
        assertTrue(clientDisk.getStatReporter() instanceof RpcClientStatJsonReporter);
        // 修改为 lazy 初始化了
        // assertFalse(asyncDigestManager.isAppenderAndEncoderExist(clientDisk.getDigestReporterType()));
        SofaRequest sofaRequest = new SofaRequest();
        rpcSofaTracer.startRpc(sofaRequest);
        rpcSofaTracer.clientBeforeSend(sofaRequest);
        rpcSofaTracer.clientReceived(sofaRequest, new SofaResponse(), null);
        // lazy 应该注册成功了
        assertTrue(asyncDigestManager.isAppenderAndEncoderExist(clientDisk.getDigestReporterType()));
        // print
        TimeUnit.SECONDS.sleep(1);
        Reporter serverReporter = tracer.getServerReporter();
        assertTrue(serverReporter instanceof DiskReporterImpl);
        assertNotNull(serverReporter);
        DiskReporterImpl serverDisk = (DiskReporterImpl) serverReporter;
        assertEquals(serverDisk.getDigestReporterType(), RpcTracerLogEnum.RPC_SERVER_DIGEST.getDefaultLogName());
        // assertFalse(asyncDigestManager.isAppenderAndEncoderExist(serverDisk.getDigestReporterType()));
        rpcSofaTracer.serverReceived(sofaRequest);
        rpcSofaTracer.serverSend(sofaRequest, new SofaResponse(), null);
        // print
        TimeUnit.SECONDS.sleep(1);
        assertTrue(asyncDigestManager.isAppenderAndEncoderExist(serverDisk.getDigestReporterType()));
        assertTrue(serverDisk.getStatReporter() instanceof RpcServerStatJsonReporter);
    } catch (Throwable e) {
        e.printStackTrace();
        assertTrue(false);
    }
}
Also used : Field(java.lang.reflect.Field) DiskReporterImpl(com.alipay.common.tracer.core.reporter.digest.DiskReporterImpl) SofaRequest(com.alipay.sofa.rpc.core.request.SofaRequest) RpcClientStatJsonReporter(com.alipay.sofa.rpc.tracer.sofatracer.log.stat.RpcClientStatJsonReporter) RpcServerStatJsonReporter(com.alipay.sofa.rpc.tracer.sofatracer.log.stat.RpcServerStatJsonReporter) RpcClientStatJsonReporter(com.alipay.sofa.rpc.tracer.sofatracer.log.stat.RpcClientStatJsonReporter) Reporter(com.alipay.common.tracer.core.reporter.facade.Reporter) RpcServerStatJsonReporter(com.alipay.sofa.rpc.tracer.sofatracer.log.stat.RpcServerStatJsonReporter) SofaResponse(com.alipay.sofa.rpc.core.response.SofaResponse) AsyncCommonDigestAppenderManager(com.alipay.common.tracer.core.appender.manager.AsyncCommonDigestAppenderManager) SofaTracer(com.alipay.common.tracer.core.SofaTracer) Test(org.junit.Test)

Aggregations

SofaResponse (com.alipay.sofa.rpc.core.response.SofaResponse)85 SofaRpcException (com.alipay.sofa.rpc.core.exception.SofaRpcException)25 Test (org.junit.Test)21 SofaRequest (com.alipay.sofa.rpc.core.request.SofaRequest)19 RpcInternalContext (com.alipay.sofa.rpc.context.RpcInternalContext)12 Method (java.lang.reflect.Method)10 AbstractByteBuf (com.alipay.sofa.rpc.transport.AbstractByteBuf)9 HashMap (java.util.HashMap)9 ProviderConfig (com.alipay.sofa.rpc.config.ProviderConfig)6 ClientEndInvokeEvent (com.alipay.sofa.rpc.event.ClientEndInvokeEvent)6 ByteArrayWrapperByteBuf (com.alipay.sofa.rpc.transport.ByteArrayWrapperByteBuf)6 ProviderInfo (com.alipay.sofa.rpc.client.ProviderInfo)5 ClientAsyncReceiveEvent (com.alipay.sofa.rpc.event.ClientAsyncReceiveEvent)5 ClientSyncReceiveEvent (com.alipay.sofa.rpc.event.ClientSyncReceiveEvent)5 Serializer (com.alipay.sofa.rpc.codec.Serializer)4 ConsumerConfig (com.alipay.sofa.rpc.config.ConsumerConfig)4 FilterChain (com.alipay.sofa.rpc.filter.FilterChain)4 Hessian2Input (com.caucho.hessian.io.Hessian2Input)4 BlockException (com.alibaba.csp.sentinel.slots.block.BlockException)3 DeserializationException (com.alipay.remoting.exception.DeserializationException)3