public AvaticaProtobufHandler(Service service, MetricsSystem metrics, AvaticaServerConfiguration serverConfig) { this.service = Objects.requireNonNull(service); this.metrics = Objects.requireNonNull(metrics); this.requestTimer = this.metrics.getTimer( MetricsHelper.concat(AvaticaProtobufHandler.class, MetricsAwareAvaticaHandler.REQUEST_TIMER_NAME)); this.protobufTranslation = new ProtobufTranslationImpl(); this.pbHandler = new ProtobufHandler(service, protobufTranslation, metrics); this.threadLocalBuffer = new ThreadLocal<UnsynchronizedBuffer>() { @Override public UnsynchronizedBuffer initialValue() { return new UnsynchronizedBuffer(); } }; this.serverConfig = serverConfig; }
@Override public byte[] serializeResponse(Response response) throws IOException { ByteArrayOutputStream out = new ByteArrayOutputStream(); Message responseMsg = response.serialize(); serializeMessage(out, responseMsg); return out.toByteArray(); }
@Override public Response parseResponse(byte[] bytes) throws InvalidProtocolBufferException { WireMessage wireMsg = WireMessage.parseFrom(bytes); String serializedMessageClassName = wireMsg.getName(); ResponseTranslator translator = getParserForResponse(serializedMessageClassName); return translator.transform(wireMsg.getWrappedMessage()); } }
@Override public Request parseRequest(byte[] bytes) throws InvalidProtocolBufferException { WireMessage wireMsg = WireMessage.parseFrom(bytes); String serializedMessageClassName = wireMsg.getName(); RequestTranslator translator = getParserForRequest(serializedMessageClassName); return translator.transform(wireMsg.getWrappedMessage()); }
void serializeMessage(OutputStream out, Message msg) throws IOException { // Serialize the protobuf message UnsynchronizedBuffer buffer = threadLocalBuffer.get(); ByteString serializedMsg; try { msg.writeTo(buffer); // Make a bytestring from it serializedMsg = UnsafeByteOperations.unsafeWrap(buffer.toArray()); } finally { buffer.reset(); } // Wrap the serialized message in a WireMessage WireMessage wireMsg = WireMessage.newBuilder().setNameBytes(getClassNameBytes(msg.getClass())) .setWrappedMessage(serializedMsg).build(); // Write the WireMessage to the provided OutputStream wireMsg.writeTo(out); }
@Override public Response parseResponse(byte[] bytes) throws IOException { ByteString byteString = UnsafeByteOperations.unsafeWrap(bytes); CodedInputStream inputStream = byteString.newCodedInput(); // Enable aliasing to avoid an extra copy to get at the serialized Response inside of the // WireMessage. inputStream.enableAliasing(true); WireMessage wireMsg = WireMessage.parseFrom(inputStream); String serializedMessageClassName = wireMsg.getName(); try { ResponseTranslator translator = getParserForResponse(serializedMessageClassName); return translator.transform(wireMsg.getWrappedMessage()); } catch (RuntimeException e) { if (LOG.isDebugEnabled()) { LOG.debug("Failed to parse response message '{}'", TextFormat.shortDebugString(wireMsg)); } throw e; } } }
@Override public Request parseRequest(byte[] bytes) throws IOException { ByteString byteString = UnsafeByteOperations.unsafeWrap(bytes); CodedInputStream inputStream = byteString.newCodedInput(); // Enable aliasing to avoid an extra copy to get at the serialized Request inside of the // WireMessage. inputStream.enableAliasing(true); WireMessage wireMsg = WireMessage.parseFrom(inputStream); String serializedMessageClassName = wireMsg.getName(); try { RequestTranslator translator = getParserForRequest(serializedMessageClassName); // The ByteString should be logical offsets into the original byte array return translator.transform(wireMsg.getWrappedMessage()); } catch (RuntimeException e) { if (LOG.isDebugEnabled()) { LOG.debug("Failed to parse request message '{}'", TextFormat.shortDebugString(wireMsg)); } throw e; } }
void serializeMessage(OutputStream out, Message msg) throws IOException { // Serialize the protobuf message UnsynchronizedBuffer buffer = threadLocalBuffer.get(); ByteString serializedMsg; try { msg.writeTo(buffer); // Make a bytestring from it serializedMsg = UnsafeByteOperations.unsafeWrap(buffer.toArray()); } finally { buffer.reset(); } // Wrap the serialized message in a WireMessage WireMessage wireMsg = WireMessage.newBuilder().setNameBytes(getClassNameBytes(msg.getClass())) .setWrappedMessage(serializedMsg).build(); // Write the WireMessage to the provided OutputStream wireMsg.writeTo(out); }
public AvaticaProtobufHandler(Service service, MetricsSystem metrics, AvaticaServerConfiguration serverConfig) { this.service = Objects.requireNonNull(service); this.metrics = Objects.requireNonNull(metrics); this.requestTimer = this.metrics.getTimer( MetricsHelper.concat(AvaticaProtobufHandler.class, MetricsAwareAvaticaHandler.REQUEST_TIMER_NAME)); this.protobufTranslation = new ProtobufTranslationImpl(); this.pbHandler = new ProtobufHandler(service, protobufTranslation, metrics); this.threadLocalBuffer = new ThreadLocal<UnsynchronizedBuffer>() { @Override public UnsynchronizedBuffer initialValue() { return new UnsynchronizedBuffer(); } }; this.serverConfig = serverConfig; }
@Override public byte[] serializeRequest(Request request) throws IOException { ByteArrayOutputStream out = new ByteArrayOutputStream(); Message requestMsg = request.serialize(); serializeMessage(out, requestMsg); return out.toByteArray(); }
@Override public Response parseResponse(byte[] bytes) throws IOException { ByteString byteString = UnsafeByteOperations.unsafeWrap(bytes); CodedInputStream inputStream = byteString.newCodedInput(); // Enable aliasing to avoid an extra copy to get at the serialized Response inside of the // WireMessage. inputStream.enableAliasing(true); WireMessage wireMsg = WireMessage.parseFrom(inputStream); String serializedMessageClassName = wireMsg.getName(); try { ResponseTranslator translator = getParserForResponse(serializedMessageClassName); return translator.transform(wireMsg.getWrappedMessage()); } catch (RuntimeException e) { if (LOG.isDebugEnabled()) { LOG.debug("Failed to parse response message '{}'", TextFormat.shortDebugString(wireMsg)); } throw e; } } }
@Override public Request parseRequest(byte[] bytes) throws IOException { ByteString byteString = UnsafeByteOperations.unsafeWrap(bytes); CodedInputStream inputStream = byteString.newCodedInput(); // Enable aliasing to avoid an extra copy to get at the serialized Request inside of the // WireMessage. inputStream.enableAliasing(true); WireMessage wireMsg = WireMessage.parseFrom(inputStream); String serializedMessageClassName = wireMsg.getName(); try { RequestTranslator translator = getParserForRequest(serializedMessageClassName); // The ByteString should be logical offsets into the original byte array return translator.transform(wireMsg.getWrappedMessage()); } catch (RuntimeException e) { if (LOG.isDebugEnabled()) { LOG.debug("Failed to parse request message '{}'", TextFormat.shortDebugString(wireMsg)); } throw e; } }
int maxDelay = Integer.valueOf(connectionInfo.getProperty(MAX_DELAY_NAME, DEFAULT_MAX_DELAY_BASE.toString())); FailoverRetryPolicy retryPolicy = new FailoverRetryPolicy(maxRetries, intervalMills, maxDelay); service = new HARemoteProtobufService(httpClient, new ProtobufTranslationImpl(), retryPolicy); } else { service = new RemoteProtobufService(httpClient, new ProtobufTranslationImpl());
@Override public byte[] serializeRequest(Request request) throws IOException { // Avoid BAOS for its synchronized write methods, we don't need that concurrency control UnsynchronizedBuffer out = threadLocalBuffer.get(); try { Message requestMsg = request.serialize(); // Serialization of the request may be large if (LOG.isTraceEnabled()) { LOG.trace("Serializing request '{}'", TextFormat.shortDebugString(requestMsg)); } serializeMessage(out, requestMsg); return out.toArray(); } finally { out.reset(); } }
break; case PROTOBUF: service = new RemoteProtobufService(httpClient, new ProtobufTranslationImpl()); break; default:
@Override public byte[] serializeResponse(Response response) throws IOException { // Avoid BAOS for its synchronized write methods, we don't need that concurrency control UnsynchronizedBuffer out = threadLocalBuffer.get(); try { Message responseMsg = response.serialize(); // Serialization of the response may be large if (LOG.isTraceEnabled()) { LOG.trace("Serializing response '{}'", TextFormat.shortDebugString(responseMsg)); } serializeMessage(out, responseMsg); return out.toArray(); } finally { out.reset(); } }
break; case PROTOBUF: service = new RemoteProtobufService(httpClient, new ProtobufTranslationImpl()); break; default:
@Override public byte[] serializeRequest(Request request) throws IOException { // Avoid BAOS for its synchronized write methods, we don't need that concurrency control UnsynchronizedBuffer out = threadLocalBuffer.get(); try { Message requestMsg = request.serialize(); // Serialization of the request may be large if (LOG.isTraceEnabled()) { LOG.trace("Serializing request '{}'", TextFormat.shortDebugString(requestMsg)); } serializeMessage(out, requestMsg); return out.toArray(); } finally { out.reset(); } }
/** * Creates a {@link Service} with the given {@link AvaticaConnection} and configuration. * * @param connection The {@link AvaticaConnection} to use. * @param config Configuration properties * @return A Service implementation. */ Service createService(AvaticaConnection connection, ConnectionConfig config) { final Service.Factory metaFactory = config.factory(); final Service service; if (metaFactory != null) { service = metaFactory.create(connection); } else if (config.url() != null) { final AvaticaHttpClient httpClient = getHttpClient(connection, config); final Serialization serializationType = getSerialization(config); switch (serializationType) { case JSON: service = new RemoteService(httpClient); break; case PROTOBUF: service = new RemoteProtobufService(httpClient, new ProtobufTranslationImpl()); break; default: throw new IllegalArgumentException("Unhandled serialization type: " + serializationType); } } else { service = new MockJsonService(Collections.<String, String>emptyMap()); } return service; }
@Override public byte[] serializeResponse(Response response) throws IOException { // Avoid BAOS for its synchronized write methods, we don't need that concurrency control UnsynchronizedBuffer out = threadLocalBuffer.get(); try { Message responseMsg = response.serialize(); // Serialization of the response may be large if (LOG.isTraceEnabled()) { LOG.trace("Serializing response '{}'", TextFormat.shortDebugString(responseMsg)); } serializeMessage(out, responseMsg); return out.toArray(); } finally { out.reset(); } }