Search in sources :

Example 41 with Message

use of com.google.protobuf.Message in project voldemort by voldemort.

the class ProtoBuffRequestHandler method handleRequest.

@Override
public StreamRequestHandler handleRequest(final DataInputStream inputStream, final DataOutputStream outputStream, final ByteBufferContainer outputContainer) throws IOException {
    VoldemortRequest.Builder request = ProtoUtils.readToBuilder(inputStream, VoldemortRequest.newBuilder());
    boolean shouldRoute = request.getShouldRoute();
    RequestRoutingType type = RequestRoutingType.getRequestRoutingType(shouldRoute, false);
    if (request.hasRequestRouteType()) {
        type = RequestRoutingType.getRequestRoutingType(request.getRequestRouteType());
    }
    String storeName = request.getStore();
    Store<ByteArray, byte[], byte[]> store = getStore(storeName, type);
    Message response;
    if (store == null) {
        response = unknownStore(storeName, request.getType());
    } else {
        switch(request.getType()) {
            case GET:
                response = handleGet(request.getGet(), store);
                break;
            case GET_ALL:
                response = handleGetAll(request.getGetAll(), store);
                break;
            case PUT:
                response = handlePut(request.getPut(), store);
                break;
            case DELETE:
                response = handleDelete(request.getDelete(), store);
                break;
            case GET_VERSION:
                response = handleGetVersion(request.getGet(), store);
                break;
            default:
                throw new VoldemortException("Unknown operation " + request.getType());
        }
    }
    if (outputContainer != null) {
        outputContainer.getBuffer().clear();
        outputContainer.ensureSpace(response.getSerializedSize());
    }
    ProtoUtils.writeMessage(outputStream, response);
    return null;
}
Also used : Message(com.google.protobuf.Message) RequestRoutingType(voldemort.server.RequestRoutingType) ByteArray(voldemort.utils.ByteArray) ByteString(com.google.protobuf.ByteString) VoldemortRequest(voldemort.client.protocol.pb.VProto.VoldemortRequest) VoldemortException(voldemort.VoldemortException)

Example 42 with Message

use of com.google.protobuf.Message in project voldemort by voldemort.

the class FullScanFetchKeysRequestHandler method handleRequest.

@Override
public StreamRequestHandlerState handleRequest(DataInputStream inputStream, DataOutputStream outputStream) throws IOException {
    if (!keyIterator.hasNext()) {
        return StreamRequestHandlerState.COMPLETE;
    }
    // NOTE: Storage time is accounted for somewhat incorrectly because
    // .hasNext() is invoked at end of method for the common case.
    long startNs = System.nanoTime();
    ByteArray key = keyIterator.next();
    reportStorageOpTime(startNs);
    throttler.maybeThrottle(key.length());
    if (isItemAccepted(key.get())) {
        if (filter.accept(key, null)) {
            accountForFetchedKey(key.get());
            VAdminProto.FetchPartitionEntriesResponse.Builder response = VAdminProto.FetchPartitionEntriesResponse.newBuilder();
            response.setKey(ProtoUtils.encodeBytes(key));
            Message message = response.build();
            sendMessage(outputStream, message);
        }
    }
    accountForScanProgress("keys");
    return determineRequestHandlerState("keys");
}
Also used : Message(com.google.protobuf.Message) ByteArray(voldemort.utils.ByteArray)

Example 43 with Message

use of com.google.protobuf.Message in project heron by twitter.

the class HeronServer method handlePacket.

/**
   * Handle an incomingPacket and invoke either onRequest or
   * onMessage() to handle it
   */
private void handlePacket(SelectableChannel channel, IncomingPacket incomingPacket) {
    String typeName = incomingPacket.unpackString();
    REQID rid = incomingPacket.unpackREQID();
    Message.Builder bldr = requestMap.get(typeName);
    boolean isRequest = false;
    if (bldr != null) {
        // This is a request
        isRequest = true;
    } else {
        bldr = messageMap.get(typeName);
    }
    if (bldr != null) {
        // Clear the earlier state of Message.Builder
        // Otherwise it would merge new Message with old state
        bldr.clear();
        incomingPacket.unpackMessage(bldr);
        if (bldr.isInitialized()) {
            Message msg = bldr.build();
            if (isRequest) {
                onRequest(rid, (SocketChannel) channel, msg);
            } else {
                onMessage((SocketChannel) channel, msg);
            }
        } else {
            // Message failed to be deser
            LOG.severe("Could not deserialize protobuf of type " + typeName);
            handleError(channel);
        }
        return;
    } else {
        LOG.severe("Unexpected protobuf type received " + typeName);
        handleError(channel);
    }
}
Also used : Message(com.google.protobuf.Message)

Example 44 with Message

use of com.google.protobuf.Message in project calcite-avatica by apache.

the class ProtobufTranslationImpl method serializeResponse.

@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();
    }
}
Also used : UnsynchronizedBuffer(org.apache.calcite.avatica.util.UnsynchronizedBuffer) WireMessage(org.apache.calcite.avatica.proto.Common.WireMessage) Message(com.google.protobuf.Message)

Example 45 with Message

use of com.google.protobuf.Message in project beam by apache.

the class ProcessBundleHandlerTest method testCreatingAndProcessingBeamFnDataReadRunner.

@Test
public void testCreatingAndProcessingBeamFnDataReadRunner() throws Exception {
    Map<String, Message> fnApiRegistry = ImmutableMap.of(STRING_CODER_SPEC_ID, STRING_CODER_SPEC);
    String bundleId = "57L";
    String primitiveTransformId = "100L";
    long outputId = 101L;
    List<WindowedValue<String>> outputValues = new ArrayList<>();
    BeamFnApi.Target outputTarget = BeamFnApi.Target.newBuilder().setPrimitiveTransformReference(primitiveTransformId).setName(Long.toString(outputId)).build();
    Multimap<BeamFnApi.Target, ThrowingConsumer<WindowedValue<String>>> existingConsumers = ImmutableMultimap.of(outputTarget, outputValues::add);
    Multimap<BeamFnApi.Target, ThrowingConsumer<WindowedValue<String>>> newConsumers = HashMultimap.create();
    List<ThrowingRunnable> startFunctions = new ArrayList<>();
    List<ThrowingRunnable> finishFunctions = new ArrayList<>();
    BeamFnApi.FunctionSpec functionSpec = BeamFnApi.FunctionSpec.newBuilder().setId("1L").setUrn(DATA_INPUT_URN).setData(Any.pack(REMOTE_PORT)).build();
    BeamFnApi.PrimitiveTransform primitiveTransform = BeamFnApi.PrimitiveTransform.newBuilder().setId(primitiveTransformId).setFunctionSpec(functionSpec).putInputs("input", BeamFnApi.Target.List.getDefaultInstance()).putOutputs(Long.toString(outputId), BeamFnApi.PCollection.newBuilder().setCoderReference(STRING_CODER_SPEC_ID).build()).build();
    ProcessBundleHandler handler = new ProcessBundleHandler(PipelineOptionsFactory.create(), fnApiRegistry::get, beamFnDataClient);
    handler.createConsumersForPrimitiveTransform(primitiveTransform, Suppliers.ofInstance(bundleId)::get, existingConsumers::get, newConsumers::put, startFunctions::add, finishFunctions::add);
    verifyZeroInteractions(beamFnDataClient);
    CompletableFuture<Void> completionFuture = new CompletableFuture<>();
    when(beamFnDataClient.forInboundConsumer(any(), any(), any(), any())).thenReturn(completionFuture);
    Iterables.getOnlyElement(startFunctions).run();
    verify(beamFnDataClient).forInboundConsumer(eq(REMOTE_PORT.getApiServiceDescriptor()), eq(KV.of(bundleId, BeamFnApi.Target.newBuilder().setPrimitiveTransformReference(primitiveTransformId).setName("input").build())), eq(STRING_CODER), consumerCaptor.capture());
    consumerCaptor.getValue().accept(valueInGlobalWindow("TestValue"));
    assertThat(outputValues, contains(valueInGlobalWindow("TestValue")));
    outputValues.clear();
    assertThat(newConsumers.keySet(), empty());
    completionFuture.complete(null);
    Iterables.getOnlyElement(finishFunctions).run();
    verifyNoMoreInteractions(beamFnDataClient);
}
Also used : Message(com.google.protobuf.Message) ArrayList(java.util.ArrayList) ByteString(com.google.protobuf.ByteString) ThrowingRunnable(org.apache.beam.fn.harness.fn.ThrowingRunnable) CompletableFuture(java.util.concurrent.CompletableFuture) WindowedValue(org.apache.beam.sdk.util.WindowedValue) BeamFnApi(org.apache.beam.fn.v1.BeamFnApi) CloseableThrowingConsumer(org.apache.beam.fn.harness.fn.CloseableThrowingConsumer) ThrowingConsumer(org.apache.beam.fn.harness.fn.ThrowingConsumer) Test(org.junit.Test)

Aggregations

Message (com.google.protobuf.Message)266 Test (org.junit.Test)84 Any (com.google.protobuf.Any)30 ByteString (com.google.protobuf.ByteString)17 IOException (java.io.IOException)16 ArrayList (java.util.ArrayList)15 Command (io.spine.core.Command)14 DynamicMessage (com.google.protobuf.DynamicMessage)13 FieldDescriptor (com.google.protobuf.Descriptors.FieldDescriptor)10 EntityRecord (io.spine.server.entity.EntityRecord)10 Test (org.junit.jupiter.api.Test)9 HeronTuples (com.twitter.heron.proto.system.HeronTuples)8 Event (io.spine.core.Event)8 InvocationTargetException (java.lang.reflect.InvocationTargetException)8 Method (java.lang.reflect.Method)8 Event (io.spine.base.Event)7 MockHttpInputMessage (org.springframework.http.MockHttpInputMessage)7 MockHttpOutputMessage (org.springframework.http.MockHttpOutputMessage)7 Descriptor (com.google.protobuf.Descriptors.Descriptor)6 Builder (com.google.protobuf.Message.Builder)5