com.alipay.remoting.rpc.RequestCommand Java Examples
The following examples show how to use
com.alipay.remoting.rpc.RequestCommand.
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: ProtobufSerializer.java From sofa-jraft with Apache License 2.0 | 6 votes |
@Override public <T extends RequestCommand> boolean serializeHeader(T request, InvokeContext invokeContext) throws SerializationException { final RpcRequestCommand cmd = (RpcRequestCommand) request; final Message msg = (Message) cmd.getRequestObject(); if (msg instanceof RpcRequests.AppendEntriesRequest) { final RpcRequests.AppendEntriesRequest req = (RpcRequests.AppendEntriesRequest) msg; final RpcRequests.AppendEntriesRequestHeader.Builder hb = RpcRequests.AppendEntriesRequestHeader .newBuilder() // .setGroupId(req.getGroupId()) // .setPeerId(req.getPeerId()) // .setServerId(req.getServerId()); cmd.setHeader(hb.build().toByteArray()); return true; } return false; }
Example #2
Source File: SofaRpcSerialization.java From sofa-rpc with Apache License 2.0 | 6 votes |
/** * 客户端记录序列化请求的耗时和 * * @param requestCommand 请求对象 */ protected void recordSerializeRequest(RequestCommand requestCommand, InvokeContext invokeContext) { if (!RpcInternalContext.isAttachmentEnable()) { return; } RpcInternalContext context = null; if (invokeContext != null) { // 客户端异步调用的情况下,上下文会放在InvokeContext中传递 context = invokeContext.get(RemotingConstants.INVOKE_CTX_RPC_CTX); } if (context == null) { context = RpcInternalContext.getContext(); } int cost = context.getStopWatch().tick().read(); int requestSize = RpcProtocol.getRequestHeaderLength() + requestCommand.getClazzLength() + requestCommand.getContentLength() + requestCommand.getHeaderLength(); // 记录请求序列化大小和请求序列化耗时 context.setAttachment(RpcConstants.INTERNAL_KEY_REQ_SIZE, requestSize); context.setAttachment(RpcConstants.INTERNAL_KEY_REQ_SERIALIZE_TIME, cost); }
Example #3
Source File: SofaRpcSerialization.java From sofa-rpc with Apache License 2.0 | 6 votes |
@Override public <Request extends RequestCommand> boolean deserializeHeader(Request request) throws DeserializationException { if (request instanceof RpcRequestCommand) { RpcInternalContext.getContext().getStopWatch().tick(); RpcRequestCommand requestCommand = (RpcRequestCommand) request; if (requestCommand.getRequestHeader() != null) { // 代表已经提前解析过了,例如使用自定义业务线程池的时候,bolt会提前解析变长Header的数据 return true; } byte[] header = requestCommand.getHeader(); // 解析头部 Map<String, String> headerMap = mapSerializer.decode(header); requestCommand.setRequestHeader(headerMap); RpcInvokeContext.getContext().put(RpcConstants.SOFA_REQUEST_HEADER_KEY, Collections.unmodifiableMap(headerMap)); return true; } return false; }
Example #4
Source File: SofaRpcSerialization.java From sofa-rpc with Apache License 2.0 | 6 votes |
@Override public <Request extends RequestCommand> boolean serializeHeader(Request request, InvokeContext invokeContext) throws SerializationException { if (request instanceof RpcRequestCommand) { RpcInternalContext.getContext().getStopWatch().tick(); RpcRequestCommand requestCommand = (RpcRequestCommand) request; Object requestObject = requestCommand.getRequestObject(); String service = getTargetServiceName(requestObject); if (StringUtils.isNotEmpty(service)) { Map<String, String> header = new HashMap<String, String>(16); header.put(RemotingConstants.HEAD_SERVICE, service); putRequestMetadataToHeader(requestObject, header); requestCommand.setHeader(mapSerializer.encode(header)); } return true; } return false; }
Example #5
Source File: CustomHeaderSerializer.java From sofa-bolt with Apache License 2.0 | 6 votes |
/** * @see com.alipay.remoting.CustomSerializer#deserializeHeader(com.alipay.remoting.rpc.RequestCommand) */ @Override public <T extends RequestCommand> boolean deserializeHeader(T request) throws DeserializationException { if (request instanceof RpcRequestCommand) { RpcRequestCommand requestCommand = (RpcRequestCommand) request; byte[] header = requestCommand.getHeader(); try { requestCommand.setRequestHeader(new String(header, "UTF-8")); } catch (UnsupportedEncodingException e) { System.err.println("UnsupportedEncodingException"); } return true; } return false; }
Example #6
Source File: NormalRequestBodyCustomSerializer_InvokeContext.java From sofa-bolt with Apache License 2.0 | 6 votes |
/** * @see CustomSerializer#deserializeContent(RequestCommand) */ @Override public <T extends RequestCommand> boolean deserializeContent(T req) throws DeserializationException { deserialFlag.set(true); RpcRequestCommand rpcReq = (RpcRequestCommand) req; byte[] content = rpcReq.getContent(); ByteBuffer bb = ByteBuffer.wrap(content); int a = bb.getInt(); byte[] dst = new byte[content.length - 4]; bb.get(dst, 0, dst.length); try { String b = new String(dst, "UTF-8"); RequestBody bd = new RequestBody(a, b); rpcReq.setRequestObject(bd); } catch (UnsupportedEncodingException e) { e.printStackTrace(); } return true; }
Example #7
Source File: NormalRequestBodyCustomSerializer.java From sofa-bolt with Apache License 2.0 | 6 votes |
/** * @see CustomSerializer#deserializeContent(RequestCommand) */ @Override public <T extends RequestCommand> boolean deserializeContent(T req) throws DeserializationException { deserialFlag.set(true); RpcRequestCommand rpcReq = (RpcRequestCommand) req; byte[] content = rpcReq.getContent(); ByteBuffer bb = ByteBuffer.wrap(content); int a = bb.getInt(); byte[] dst = new byte[content.length - 4]; bb.get(dst, 0, dst.length); try { String b = new String(dst, "UTF-8"); RequestBody bd = new RequestBody(a, b); rpcReq.setRequestObject(bd); } catch (UnsupportedEncodingException e) { e.printStackTrace(); } contentDeserializer = rpcReq.getSerializer(); return true; }
Example #8
Source File: NormalRequestBodyCustomSerializer.java From sofa-bolt with Apache License 2.0 | 6 votes |
/** * @see CustomSerializer#serializeContent(RequestCommand, InvokeContext) */ @Override public <T extends RequestCommand> boolean serializeContent(T req, InvokeContext invokeContext) throws SerializationException { serialFlag.set(true); RpcRequestCommand rpcReq = (RpcRequestCommand) req; RequestBody bd = (RequestBody) rpcReq.getRequestObject(); int id = bd.getId(); byte[] msg; try { msg = bd.getMsg().getBytes("UTF-8"); ByteBuffer bb = ByteBuffer.allocate(4 + msg.length); bb.putInt(id); bb.put(msg); rpcReq.setContent(bb.array()); } catch (UnsupportedEncodingException e) { e.printStackTrace(); } contentSerializer = rpcReq.getSerializer(); return true; }
Example #9
Source File: ProtobufSerializer.java From sofa-jraft with Apache License 2.0 | 5 votes |
@Override public <T extends RequestCommand> boolean deserializeHeader(T request) throws DeserializationException { final RpcRequestCommand cmd = (RpcRequestCommand) request; final String className = cmd.getRequestClass(); if (className.equals(RpcRequests.AppendEntriesRequest.class.getName())) { final byte[] header = cmd.getHeader(); cmd.setRequestHeader(ProtobufMsgFactory.newMessageByJavaClassName( RpcRequests.AppendEntriesRequestHeader.class.getName(), header)); return true; } return false; }
Example #10
Source File: SofaRpcSerialization.java From sofa-rpc with Apache License 2.0 | 5 votes |
/** * 服务端记录反序列化请求的大小和耗时 * * @param requestCommand 请求对象 */ private void recordDeserializeRequest(RequestCommand requestCommand) { if (!RpcInternalContext.isAttachmentEnable()) { return; } RpcInternalContext context = RpcInternalContext.getContext(); int cost = context.getStopWatch().tick().read(); int requestSize = RpcProtocol.getRequestHeaderLength() + requestCommand.getClazzLength() + requestCommand.getContentLength() + requestCommand.getHeaderLength(); // 记录请求反序列化大小和请求反序列化耗时 context.setAttachment(RpcConstants.INTERNAL_KEY_REQ_SIZE, requestSize); context.setAttachment(RpcConstants.INTERNAL_KEY_REQ_DESERIALIZE_TIME, cost); }
Example #11
Source File: SofaRpcSerialization.java From sofa-rpc with Apache License 2.0 | 5 votes |
@Override public <Request extends RequestCommand> boolean serializeContent(Request request, InvokeContext invokeContext) throws SerializationException { if (request instanceof RpcRequestCommand) { RpcRequestCommand requestCommand = (RpcRequestCommand) request; Object requestObject = requestCommand.getRequestObject(); byte serializerCode = requestCommand.getSerializer(); try { Map<String, String> header = (Map<String, String>) requestCommand.getRequestHeader(); if (header == null) { header = new HashMap<String, String>(); } putKV(header, RemotingConstants.HEAD_GENERIC_TYPE, (String) invokeContext.get(RemotingConstants.HEAD_GENERIC_TYPE)); Serializer rpcSerializer = com.alipay.sofa.rpc.codec.SerializerFactory .getSerializer(serializerCode); AbstractByteBuf byteBuf = rpcSerializer.encode(requestObject, header); request.setContent(byteBuf.array()); return true; } catch (Exception ex) { throw new SerializationException(ex.getMessage(), ex); } finally { recordSerializeRequest(requestCommand, invokeContext); } } return false; }
Example #12
Source File: CustomHeaderSerializer.java From sofa-bolt with Apache License 2.0 | 5 votes |
/** * @see com.alipay.remoting.CustomSerializer#serializeHeader(com.alipay.remoting.rpc.RequestCommand, InvokeContext) */ @Override public <T extends RequestCommand> boolean serializeHeader(T request, InvokeContext invokeContext) throws SerializationException { if (request instanceof RpcRequestCommand) { RpcRequestCommand requestCommand = (RpcRequestCommand) request; try { requestCommand.setHeader(EXECUTOR1.getBytes("UTF-8")); } catch (UnsupportedEncodingException e) { System.err.println("UnsupportedEncodingException"); } return true; } return false; }
Example #13
Source File: ExceptionRequestBodyCustomSerializer.java From sofa-bolt with Apache License 2.0 | 5 votes |
/** * @see CustomSerializer#deserializeContent(RequestCommand) */ @Override public <T extends RequestCommand> boolean deserializeContent(T req) throws DeserializationException { deserialFlag.set(true); if (deserialRuntimeException) { throw new RuntimeException( "deserialRuntimeException in ExceptionRequestBodyCustomSerializer!"); } else if (deserialException) { throw new DeserializationException( "deserialException in ExceptionRequestBodyCustomSerializer!"); } else { return false;// use default codec } }
Example #14
Source File: ExceptionRequestBodyCustomSerializer.java From sofa-bolt with Apache License 2.0 | 5 votes |
/** * @see CustomSerializer#serializeContent(RequestCommand, InvokeContext) */ @Override public <T extends RequestCommand> boolean serializeContent(T req, InvokeContext invokeContext) throws SerializationException { serialFlag.set(true); if (serialRuntimeException) { throw new RuntimeException( "serialRuntimeException in ExceptionRequestBodyCustomSerializer!"); } else if (serialException) { throw new SerializationException( "serialException in ExceptionRequestBodyCustomSerializer!"); } else { return false;// use default codec } }
Example #15
Source File: DefaultCustomSerializer.java From sofa-bolt with Apache License 2.0 | 5 votes |
/** * @see com.alipay.remoting.CustomSerializer#serializeContent(com.alipay.remoting.rpc.RequestCommand, InvokeContext) */ @Override public <T extends RequestCommand> boolean serializeContent(T request, InvokeContext invokeContext) throws SerializationException { return false; }
Example #16
Source File: RpcCommandHandler.java From sofa-bolt with Apache License 2.0 | 5 votes |
private void processExceptionForSingleCommand(RemotingContext ctx, Object msg, Throwable t) { final int id = ((RpcCommand) msg).getId(); final String emsg = "Exception caught when processing " + ((msg instanceof RequestCommand) ? "request, id=" : "response, id="); logger.warn(emsg + id, t); if (msg instanceof RequestCommand) { final RequestCommand cmd = (RequestCommand) msg; if (cmd.getType() != RpcCommandType.REQUEST_ONEWAY) { if (t instanceof RejectedExecutionException) { final ResponseCommand response = this.commandFactory.createExceptionResponse( id, ResponseStatus.SERVER_THREADPOOL_BUSY); // RejectedExecutionException here assures no response has been sent back // Other exceptions should be processed where exception was caught, because here we don't known whether ack had been sent back. ctx.getChannelContext().writeAndFlush(response) .addListener(new ChannelFutureListener() { @Override public void operationComplete(ChannelFuture future) throws Exception { if (future.isSuccess()) { if (logger.isInfoEnabled()) { logger .info( "Write back exception response done, requestId={}, status={}", id, response.getResponseStatus()); } } else { logger.error( "Write back exception response failed, requestId={}", id, future.cause()); } } }); } } } }
Example #17
Source File: ProtobufSerializer.java From sofa-jraft with Apache License 2.0 | 5 votes |
@Override public <T extends RequestCommand> boolean deserializeContent(T request) throws DeserializationException { final RpcRequestCommand cmd = (RpcRequestCommand) request; final String className = cmd.getRequestClass(); cmd.setRequestObject(ProtobufMsgFactory.newMessageByJavaClassName(className, request.getContent())); return true; }
Example #18
Source File: ProtobufSerializer.java From sofa-jraft with Apache License 2.0 | 5 votes |
@Override public <T extends RequestCommand> boolean serializeContent(T request, InvokeContext invokeContext) throws SerializationException { final RpcRequestCommand cmd = (RpcRequestCommand) request; final Message msg = (Message) cmd.getRequestObject(); cmd.setContent(msg.toByteArray()); return true; }
Example #19
Source File: DefaultCustomSerializer.java From sofa-bolt with Apache License 2.0 | 4 votes |
/** * @see com.alipay.remoting.CustomSerializer#deserializeContent(com.alipay.remoting.rpc.RequestCommand) */ @Override public <T extends RequestCommand> boolean deserializeContent(T request) throws DeserializationException { return false; }
Example #20
Source File: DefaultCustomSerializer.java From sofa-bolt with Apache License 2.0 | 4 votes |
/** * @see com.alipay.remoting.CustomSerializer#deserializeHeader(com.alipay.remoting.rpc.RequestCommand) */ @Override public <T extends RequestCommand> boolean deserializeHeader(T request) throws DeserializationException { return false; }
Example #21
Source File: DefaultCustomSerializer.java From sofa-bolt with Apache License 2.0 | 4 votes |
/** * @see com.alipay.remoting.CustomSerializer#serializeHeader(com.alipay.remoting.rpc.RequestCommand, InvokeContext) */ @Override public <T extends RequestCommand> boolean serializeHeader(T request, InvokeContext invokeContext) throws SerializationException { return false; }
Example #22
Source File: RpcCommandEncoder.java From sofa-bolt with Apache License 2.0 | 4 votes |
/** * @see com.alipay.remoting.CommandEncoder#encode(io.netty.channel.ChannelHandlerContext, java.io.Serializable, io.netty.buffer.ByteBuf) */ @Override public void encode(ChannelHandlerContext ctx, Serializable msg, ByteBuf out) throws Exception { try { if (msg instanceof RpcCommand) { /* * ver: version for protocol * type: request/response/request oneway * cmdcode: code for remoting command * ver2:version for remoting command * requestId: id of request * codec: code for codec * (req)timeout: request timeout. * (resp)respStatus: response status * classLen: length of request or response class name * headerLen: length of header * cotentLen: length of content * className * header * content */ RpcCommand cmd = (RpcCommand) msg; out.writeByte(RpcProtocol.PROTOCOL_CODE); out.writeByte(cmd.getType()); out.writeShort(((RpcCommand) msg).getCmdCode().value()); out.writeByte(cmd.getVersion()); out.writeInt(cmd.getId()); out.writeByte(cmd.getSerializer()); if (cmd instanceof RequestCommand) { //timeout out.writeInt(((RequestCommand) cmd).getTimeout()); } if (cmd instanceof ResponseCommand) { //response status ResponseCommand response = (ResponseCommand) cmd; out.writeShort(response.getResponseStatus().getValue()); } out.writeShort(cmd.getClazzLength()); out.writeShort(cmd.getHeaderLength()); out.writeInt(cmd.getContentLength()); if (cmd.getClazzLength() > 0) { out.writeBytes(cmd.getClazz()); } if (cmd.getHeaderLength() > 0) { out.writeBytes(cmd.getHeader()); } if (cmd.getContentLength() > 0) { out.writeBytes(cmd.getContent()); } } else { String warnMsg = "msg type [" + msg.getClass() + "] is not subclass of RpcCommand"; logger.warn(warnMsg); } } catch (Exception e) { logger.error("Exception caught!", e); throw e; } }
Example #23
Source File: RpcCommandEncoderV2.java From sofa-bolt with Apache License 2.0 | 4 votes |
/** * @see CommandEncoder#encode(ChannelHandlerContext, Serializable, ByteBuf) */ @Override public void encode(ChannelHandlerContext ctx, Serializable msg, ByteBuf out) throws Exception { try { if (msg instanceof RpcCommand) { /* * proto: magic code for protocol * ver: version for protocol * type: request/response/request oneway * cmdcode: code for remoting command * ver2:version for remoting command * requestId: id of request * codec: code for codec * switch: function switch * (req)timeout: request timeout. * (resp)respStatus: response status * classLen: length of request or response class name * headerLen: length of header * cotentLen: length of content * className * header * content * crc (optional) */ int index = out.writerIndex(); RpcCommand cmd = (RpcCommand) msg; out.writeByte(RpcProtocolV2.PROTOCOL_CODE); Attribute<Byte> version = ctx.channel().attr(Connection.VERSION); byte ver = RpcProtocolV2.PROTOCOL_VERSION_1; if (version != null && version.get() != null) { ver = version.get(); } out.writeByte(ver); out.writeByte(cmd.getType()); out.writeShort(((RpcCommand) msg).getCmdCode().value()); out.writeByte(cmd.getVersion()); out.writeInt(cmd.getId()); out.writeByte(cmd.getSerializer()); out.writeByte(cmd.getProtocolSwitch().toByte()); if (cmd instanceof RequestCommand) { //timeout out.writeInt(((RequestCommand) cmd).getTimeout()); } if (cmd instanceof ResponseCommand) { //response status ResponseCommand response = (ResponseCommand) cmd; out.writeShort(response.getResponseStatus().getValue()); } out.writeShort(cmd.getClazzLength()); out.writeShort(cmd.getHeaderLength()); out.writeInt(cmd.getContentLength()); if (cmd.getClazzLength() > 0) { out.writeBytes(cmd.getClazz()); } if (cmd.getHeaderLength() > 0) { out.writeBytes(cmd.getHeader()); } if (cmd.getContentLength() > 0) { out.writeBytes(cmd.getContent()); } if (ver == RpcProtocolV2.PROTOCOL_VERSION_2 && cmd.getProtocolSwitch().isOn(ProtocolSwitch.CRC_SWITCH_INDEX)) { // compute the crc32 and write to out byte[] frame = new byte[out.readableBytes()]; out.getBytes(index, frame); out.writeInt(CrcUtil.crc32(frame)); } } else { String warnMsg = "msg type [" + msg.getClass() + "] is not subclass of RpcCommand"; logger.warn(warnMsg); } } catch (Exception e) { logger.error("Exception caught!", e); throw e; } }
Example #24
Source File: SofaRpcSerialization.java From sofa-rpc with Apache License 2.0 | 4 votes |
@Override public <Request extends RequestCommand> boolean deserializeContent(Request request) throws DeserializationException { if (request instanceof RpcRequestCommand) { RpcRequestCommand requestCommand = (RpcRequestCommand) request; Object header = requestCommand.getRequestHeader(); if (!(header instanceof Map)) { throw new DeserializationException("Head of request is null or is not map"); } Map<String, String> headerMap = (Map<String, String>) header; byte[] content = requestCommand.getContent(); if (content == null || content.length == 0) { throw new DeserializationException("Content of request is null"); } try { String service = headerMap.get(RemotingConstants.HEAD_SERVICE); ClassLoader oldClassLoader = Thread.currentThread().getContextClassLoader(); ClassLoader serviceClassLoader = ReflectCache.getServiceClassLoader(service); try { Thread.currentThread().setContextClassLoader(serviceClassLoader); Serializer rpcSerializer = com.alipay.sofa.rpc.codec.SerializerFactory .getSerializer(requestCommand.getSerializer()); Object sofaRequest = ClassUtils.forName(requestCommand.getRequestClass()).newInstance(); rpcSerializer.decode(new ByteArrayWrapperByteBuf(requestCommand.getContent()), sofaRequest, headerMap); //for service mesh or other scene, we need to add more info from header if (sofaRequest instanceof SofaRequest) { for (Map.Entry<String, String> entry : headerMap.entrySet()) { ((SofaRequest) sofaRequest).addRequestProp(entry.getKey(), entry.getValue()); } } requestCommand.setRequestObject(sofaRequest); } finally { Thread.currentThread().setContextClassLoader(oldClassLoader); } return true; } catch (Exception ex) { throw new DeserializationException(ex.getMessage(), ex); } finally { recordDeserializeRequest(requestCommand); } } return false; }
Example #25
Source File: CustomSerializer.java From sofa-bolt with Apache License 2.0 | 2 votes |
/** * Deserialize the content of RequestCommand. * * @param request * @return * @throws CodecException */ <T extends RequestCommand> boolean deserializeContent(T request) throws DeserializationException;
Example #26
Source File: CustomSerializer.java From sofa-bolt with Apache License 2.0 | 2 votes |
/** * Serialize the content of RequestCommand. * * @param request * @param invokeContext * @return * @throws CodecException */ <T extends RequestCommand> boolean serializeContent(T request, InvokeContext invokeContext) throws SerializationException;
Example #27
Source File: CustomSerializer.java From sofa-bolt with Apache License 2.0 | 2 votes |
/** * Deserialize the header of RequestCommand. * * @param request * @return * @throws CodecException */ <T extends RequestCommand> boolean deserializeHeader(T request) throws DeserializationException;
Example #28
Source File: CustomSerializer.java From sofa-bolt with Apache License 2.0 | 2 votes |
/** * Serialize the header of RequestCommand. * * @param request * @param invokeContext * @return * @throws CodecException */ <T extends RequestCommand> boolean serializeHeader(T request, InvokeContext invokeContext) throws SerializationException;