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);
}
}
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);
}
}
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();
}
}
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();
}
}
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);
}
}
Aggregations