@Override protected V getNow() throws ExecutionException { if (cause != null) { // 异常 throw new ExecutionException(cause); } else if (result instanceof SofaResponse) { SofaResponse response = (SofaResponse) result; if (response.isError()) { cause = new SofaRpcException(RpcErrorType.SERVER_UNDECLARED_ERROR, response.getErrorMsg()); throw new ExecutionException(cause); } else { result = response.getAppResponse(); if (result instanceof Throwable) { throw new ExecutionException((Throwable) result); } else { return (V) result; } } } else { return (V) result; } }
@Override protected V getNow() throws ExecutionException { if (cause != null) { // 异常 throw new ExecutionException(cause); } else if (result instanceof SofaResponse) { SofaResponse response = (SofaResponse) result; if (response.isError()) { cause = new SofaRpcException(RpcErrorType.SERVER_UNDECLARED_ERROR, response.getErrorMsg()); throw new ExecutionException(cause); } else { result = response.getAppResponse(); if (result instanceof Throwable) { throw new ExecutionException((Throwable) result); } else { return (V) result; } } } else { return (V) result; } }
@Override public Object invoke(Object proxy, Method method, Object[] paramValues) throws Throwable { String methodName = method.getName(); Class[] paramTypes = method.getParameterTypes(); if ("toString".equals(methodName) && paramTypes.length == 0) { return proxyInvoker.toString(); } else if ("hashCode".equals(methodName) && paramTypes.length == 0) { return proxyInvoker.hashCode(); } else if ("equals".equals(methodName) && paramTypes.length == 1) { Object another = paramValues[0]; return proxy == another || (proxy.getClass().isInstance(another) && proxyInvoker.equals(JDKProxy.parseInvoker(another))); } SofaRequest sofaRequest = MessageBuilder.buildSofaRequest(method.getDeclaringClass(), method, paramTypes, paramValues); SofaResponse response = proxyInvoker.invoke(sofaRequest); if (response.isError()) { throw new SofaRpcException(RpcErrorType.SERVER_UNDECLARED_ERROR, response.getErrorMsg()); } Object ret = response.getAppResponse(); if (ret instanceof Throwable) { throw (Throwable) ret; } else { if (ret == null) { return ClassUtils.getDefaultPrimitiveValue(method.getReturnType()); } return ret; } }
@Override public Object invoke(Object proxy, Method method, Object[] paramValues) throws Throwable { String methodName = method.getName(); Class[] paramTypes = method.getParameterTypes(); if ("toString".equals(methodName) && paramTypes.length == 0) { return proxyInvoker.toString(); } else if ("hashCode".equals(methodName) && paramTypes.length == 0) { return proxyInvoker.hashCode(); } else if ("equals".equals(methodName) && paramTypes.length == 1) { Object another = paramValues[0]; return proxy == another || (proxy.getClass().isInstance(another) && proxyInvoker.equals(JDKProxy.parseInvoker(another))); } SofaRequest sofaRequest = MessageBuilder.buildSofaRequest(method.getDeclaringClass(), method, paramTypes, paramValues); SofaResponse response = proxyInvoker.invoke(sofaRequest); if (response.isError()) { throw new SofaRpcException(RpcErrorType.SERVER_UNDECLARED_ERROR, response.getErrorMsg()); } Object ret = response.getAppResponse(); if (ret instanceof Throwable) { throw (Throwable) ret; } else { if (ret == null) { return ClassUtils.getDefaultPrimitiveValue(method.getReturnType()); } return ret; } }
protected AbstractByteBuf encodeSofaResponse(SofaResponse sofaResponse, Map<String, String> context) throws SofaRpcException { AbstractByteBuf byteBuf; if (sofaResponse.isError()) { // rpc exception:error when body is illegal string byteBuf = encode(sofaResponse.getErrorMsg(), context); } else { //ok: when json can be deserialize correctly. Object appResponse = sofaResponse.getAppResponse(); if (appResponse instanceof Throwable) { // biz exception:error when body is illegal string byteBuf = encode(((Throwable) appResponse).getMessage(), context); } else { byteBuf = encode(appResponse, context); } } return byteBuf; }
protected AbstractByteBuf encodeSofaResponse(SofaResponse sofaResponse, Map<String, String> context) throws SofaRpcException { AbstractByteBuf byteBuf; if (sofaResponse.isError()) { // 框架异常:错误则body序列化的是错误字符串 byteBuf = encode(sofaResponse.getErrorMsg(), context); } else { // 正确返回则解析序列化的protobuf返回对象 Object appResponse = sofaResponse.getAppResponse(); if (appResponse instanceof Throwable) { // 业务异常序列化的是错误字符串 byteBuf = encode(((Throwable) appResponse).getMessage(), context); } else { byteBuf = encode(appResponse, context); } } return byteBuf; }
protected AbstractByteBuf encodeSofaResponse(SofaResponse sofaResponse, Map<String, String> context) throws SofaRpcException { AbstractByteBuf byteBuf; if (sofaResponse.isError()) { // rpc exception:error when body is illegal string byteBuf = encode(sofaResponse.getErrorMsg(), context); } else { //ok: when json can be deserialize correctly. Object appResponse = sofaResponse.getAppResponse(); if (appResponse instanceof Throwable) { // biz exception:error when body is illegal string byteBuf = encode(((Throwable) appResponse).getMessage(), context); } else { byteBuf = encode(appResponse, context); } } return byteBuf; }
protected AbstractByteBuf encodeSofaResponse(SofaResponse sofaResponse, Map<String, String> context) throws SofaRpcException { AbstractByteBuf byteBuf; if (sofaResponse.isError()) { // 框架异常:错误则body序列化的是错误字符串 byteBuf = encode(sofaResponse.getErrorMsg(), context); } else { // 正确返回则解析序列化的protobuf返回对象 Object appResponse = sofaResponse.getAppResponse(); if (appResponse instanceof Throwable) { // 业务异常序列化的是错误字符串 byteBuf = encode(((Throwable) appResponse).getMessage(), context); } else { byteBuf = encode(appResponse, context); } } return byteBuf; }
if (response.isError()) { // rpc层异常 SofaRpcException sofaRpcException = new SofaRpcException( RpcErrorType.SERVER_UNDECLARED_ERROR, response.getErrorMsg()); rpcFuture.setFailure(sofaRpcException); } else if (appResp instanceof Throwable) { // 业务层异常
if (response.isError()) { // rpc层异常 SofaRpcException sofaRpcException = new SofaRpcException( RpcErrorType.SERVER_UNDECLARED_ERROR, response.getErrorMsg()); rpcFuture.setFailure(sofaRpcException); } else if (appResp instanceof Throwable) { // 业务层异常
if (response.isError()) { // rpc层异常 SofaRpcException sofaRpcException = new SofaRpcException( RpcErrorType.SERVER_UNDECLARED_ERROR, response.getErrorMsg()); callback.onSofaException(sofaRpcException, request.getMethodName(), request); } else if (appResp instanceof Throwable) { // 业务层异常
if (response.isError()) { // rpc层异常 SofaRpcException sofaRpcException = new SofaRpcException( RpcErrorType.SERVER_UNDECLARED_ERROR, response.getErrorMsg()); callback.onSofaException(sofaRpcException, request.getMethodName(), request); } else if (appResp instanceof Throwable) { // 业务层异常
if (response.isError()) { // rpc层异常 SofaRpcException sofaRpcException = new SofaRpcException( RpcErrorType.SERVER_UNDECLARED_ERROR, response.getErrorMsg()); callback.onSofaException(sofaRpcException, request.getMethodName(), request); } else if (appResp instanceof Throwable) { // 业务层异常
if (response.isError()) { // rpc层异常 SofaRpcException sofaRpcException = new SofaRpcException( RpcErrorType.SERVER_UNDECLARED_ERROR, response.getErrorMsg()); callback.onSofaException(sofaRpcException, request.getMethodName(), request); } else if (appResp instanceof Throwable) { // 业务层异常
@Override public void decodeObjectByTemplate(AbstractByteBuf data, Map<String, String> context, SofaResponse template) throws SofaRpcException { try { UnsafeByteArrayInputStream inputStream = new UnsafeByteArrayInputStream(data.array()); Hessian2Input input = new Hessian2Input(inputStream); // 根据SerializeType信息决定序列化器 boolean genericSerialize = context != null && isGenericResponse( context.get(RemotingConstants.HEAD_GENERIC_TYPE)); if (genericSerialize) { input.setSerializerFactory(genericSerializerFactory); GenericObject genericObject = (GenericObject) input.readObject(); template.setErrorMsg((String) genericObject.getField("errorMsg")); template.setAppResponse(genericObject.getField("appResponse")); template.setResponseProps((Map<String, String>) genericObject.getField("responseProps")); } else { input.setSerializerFactory(serializerFactory); SofaResponse tmp = (SofaResponse) input.readObject(); // copy values to template template.setErrorMsg(tmp.getErrorMsg()); template.setAppResponse(tmp.getAppResponse()); template.setResponseProps(tmp.getResponseProps()); } input.close(); } catch (IOException e) { throw buildDeserializeError(e.getMessage(), e); } }
@Override public void decodeObjectByTemplate(AbstractByteBuf data, Map<String, String> context, SofaResponse template) throws SofaRpcException { try { UnsafeByteArrayInputStream inputStream = new UnsafeByteArrayInputStream(data.array()); Hessian2Input input = new Hessian2Input(inputStream); // 根据SerializeType信息决定序列化器 boolean genericSerialize = context != null && isGenericResponse( context.get(RemotingConstants.HEAD_GENERIC_TYPE)); if (genericSerialize) { input.setSerializerFactory(genericSerializerFactory); GenericObject genericObject = (GenericObject) input.readObject(); template.setErrorMsg((String) genericObject.getField("errorMsg")); template.setAppResponse(genericObject.getField("appResponse")); template.setResponseProps((Map<String, String>) genericObject.getField("responseProps")); } else { input.setSerializerFactory(serializerFactory); SofaResponse tmp = (SofaResponse) input.readObject(); // copy values to template template.setErrorMsg(tmp.getErrorMsg()); template.setAppResponse(tmp.getAppResponse()); template.setResponseProps(tmp.getResponseProps()); } input.close(); } catch (IOException e) { throw buildDeserializeError(e.getMessage(), e); } }
/** * create RpcServerLookoutModel * @param request * @param response * @return */ private RpcServerLookoutModel createServerMetricsModel(SofaRequest request, SofaResponse response) { RpcServerLookoutModel rpcServerMetricsModel = new RpcServerLookoutModel(); RpcInternalContext context = RpcInternalContext.getContext(); String app = request.getTargetAppName(); String service = request.getTargetServiceUniqueName(); String method = request.getMethodName(); String protocol = getStringAvoidNull(request.getRequestProp(RemotingConstants.HEAD_PROTOCOL)); String invokeType = request.getInvokeType(); String callerApp = getStringAvoidNull(request.getRequestProp(RemotingConstants.HEAD_APP_NAME)); Long elapsedTime = getLongAvoidNull(context.getAttachment(RpcConstants.INTERNAL_KEY_IMPL_ELAPSE)); boolean success = response != null && !response.isError() && response.getErrorMsg() == null && (!(response.getAppResponse() instanceof Throwable)); rpcServerMetricsModel.setApp(app); rpcServerMetricsModel.setService(service); rpcServerMetricsModel.setMethod(method); rpcServerMetricsModel.setProtocol(protocol); rpcServerMetricsModel.setInvokeType(invokeType); rpcServerMetricsModel.setCallerApp(callerApp); rpcServerMetricsModel.setElapsedTime(elapsedTime); rpcServerMetricsModel.setSuccess(success); return rpcServerMetricsModel; }
/** * create RpcServerLookoutModel * @param request * @param response * @return */ private RpcServerLookoutModel createServerMetricsModel(SofaRequest request, SofaResponse response) { RpcServerLookoutModel rpcServerMetricsModel = new RpcServerLookoutModel(); RpcInternalContext context = RpcInternalContext.getContext(); String app = request.getTargetAppName(); String service = request.getTargetServiceUniqueName(); String method = request.getMethodName(); String protocol = getStringAvoidNull(request.getRequestProp(RemotingConstants.HEAD_PROTOCOL)); String invokeType = request.getInvokeType(); String callerApp = getStringAvoidNull(request.getRequestProp(RemotingConstants.HEAD_APP_NAME)); Long elapsedTime = getLongAvoidNull(context.getAttachment(RpcConstants.INTERNAL_KEY_IMPL_ELAPSE)); boolean success = response != null && !response.isError() && response.getErrorMsg() == null && (!(response.getAppResponse() instanceof Throwable)); rpcServerMetricsModel.setApp(app); rpcServerMetricsModel.setService(service); rpcServerMetricsModel.setMethod(method); rpcServerMetricsModel.setProtocol(protocol); rpcServerMetricsModel.setInvokeType(invokeType); rpcServerMetricsModel.setCallerApp(callerApp); rpcServerMetricsModel.setElapsedTime(elapsedTime); rpcServerMetricsModel.setSuccess(success); return rpcServerMetricsModel; }
Long responseSize = getLongAvoidNull(context.getAttachment(RpcConstants.INTERNAL_KEY_RESP_SIZE)); Long elapsedTime = getLongAvoidNull(context.getAttachment(RpcConstants.INTERNAL_KEY_CLIENT_ELAPSE)); Boolean success = response != null && !response.isError() && response.getErrorMsg() == null && (!(response.getAppResponse() instanceof Throwable));
Long responseSize = getLongAvoidNull(context.getAttachment(RpcConstants.INTERNAL_KEY_RESP_SIZE)); Long elapsedTime = getLongAvoidNull(context.getAttachment(RpcConstants.INTERNAL_KEY_CLIENT_ELAPSE)); Boolean success = response != null && !response.isError() && response.getErrorMsg() == null && (!(response.getAppResponse() instanceof Throwable));