Search in sources :

Example 1 with Message

use of com.google.protobuf.Message in project jvm-serializers by eishay.

the class ProtoServerHandler method handle.

void handle(final OutputStream os, final InputStream is) throws IOException {
    RpcCallback<Message> done = new RpcCallback<Message>() {

        DataOutputStream dos = new DataOutputStream(os);

        public void run(Message content) {
            try {
                byte[] array = _serializer.serialize((MediaContent) content);
                dos.writeInt(array.length);
                dos.write(array);
                dos.flush();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    };
    DataInputStream dis = new DataInputStream(is);
    int index = dis.readInt();
    MethodDescriptor method = getDescriptor().getMethods().get(index);
    byte[] array = new byte[dis.readInt()];
    dis.readFully(array);
    Message request = getRequestPrototype(method).newBuilderForType().mergeFrom(array).build();
    callMethod(method, null, request, done);
}
Also used : Message(com.google.protobuf.Message) DataOutputStream(java.io.DataOutputStream) RpcCallback(com.google.protobuf.RpcCallback) DataInputStream(java.io.DataInputStream) MethodDescriptor(com.google.protobuf.Descriptors.MethodDescriptor) IOException(java.io.IOException)

Example 2 with Message

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

the class TestLlapDaemonProtocolClientProxy method testMultipleNodes.

@Test(timeout = 5000)
public void testMultipleNodes() {
    RequestManagerForTest requestManager = new RequestManagerForTest(1);
    LlapNodeId nodeId1 = LlapNodeId.getInstance("host1", 1025);
    LlapNodeId nodeId2 = LlapNodeId.getInstance("host2", 1025);
    Message mockMessage = mock(Message.class);
    LlapProtocolClientProxy.ExecuteRequestCallback mockExecuteRequestCallback = mock(LlapProtocolClientProxy.ExecuteRequestCallback.class);
    // Request two messages
    requestManager.queueRequest(new CallableRequestForTest(nodeId1, mockMessage, mockExecuteRequestCallback));
    requestManager.queueRequest(new CallableRequestForTest(nodeId2, mockMessage, mockExecuteRequestCallback));
    // Should go through in a single process call
    requestManager.process();
    assertEquals(2, requestManager.numSubmissionsCounters);
    assertNotNull(requestManager.numInvocationsPerNode.get(nodeId1));
    assertNotNull(requestManager.numInvocationsPerNode.get(nodeId2));
    Assert.assertEquals(1, requestManager.numInvocationsPerNode.get(nodeId1).getValue().intValue());
    Assert.assertEquals(1, requestManager.numInvocationsPerNode.get(nodeId2).getValue().intValue());
    assertEquals(0, requestManager.currentLoopSkippedRequests.size());
    assertEquals(0, requestManager.currentLoopSkippedRequests.size());
    assertEquals(0, requestManager.currentLoopDisabledNodes.size());
}
Also used : LlapNodeId(org.apache.hadoop.hive.llap.LlapNodeId) Message(com.google.protobuf.Message) Test(org.junit.Test)

Example 3 with Message

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

the class TestLlapDaemonProtocolClientProxy method testSingleInvocationPerNode.

@Test(timeout = 5000)
public void testSingleInvocationPerNode() {
    RequestManagerForTest requestManager = new RequestManagerForTest(1);
    LlapNodeId nodeId1 = LlapNodeId.getInstance("host1", 1025);
    Message mockMessage = mock(Message.class);
    LlapProtocolClientProxy.ExecuteRequestCallback mockExecuteRequestCallback = mock(LlapProtocolClientProxy.ExecuteRequestCallback.class);
    // First request for host.
    requestManager.queueRequest(new CallableRequestForTest(nodeId1, mockMessage, mockExecuteRequestCallback));
    requestManager.process();
    assertEquals(1, requestManager.numSubmissionsCounters);
    assertNotNull(requestManager.numInvocationsPerNode.get(nodeId1));
    Assert.assertEquals(1, requestManager.numInvocationsPerNode.get(nodeId1).getValue().intValue());
    assertEquals(0, requestManager.currentLoopSkippedRequests.size());
    // Second request for host. Single invocation since the last has not completed.
    requestManager.queueRequest(new CallableRequestForTest(nodeId1, mockMessage, mockExecuteRequestCallback));
    requestManager.process();
    assertEquals(1, requestManager.numSubmissionsCounters);
    assertNotNull(requestManager.numInvocationsPerNode.get(nodeId1));
    Assert.assertEquals(1, requestManager.numInvocationsPerNode.get(nodeId1).getValue().intValue());
    assertEquals(1, requestManager.currentLoopSkippedRequests.size());
    assertEquals(1, requestManager.currentLoopDisabledNodes.size());
    assertTrue(requestManager.currentLoopDisabledNodes.contains(nodeId1));
    // Complete first request. Second pending request should go through.
    requestManager.requestFinished(nodeId1);
    requestManager.process();
    assertEquals(2, requestManager.numSubmissionsCounters);
    assertNotNull(requestManager.numInvocationsPerNode.get(nodeId1));
    Assert.assertEquals(2, requestManager.numInvocationsPerNode.get(nodeId1).getValue().intValue());
    assertEquals(0, requestManager.currentLoopSkippedRequests.size());
    assertEquals(0, requestManager.currentLoopDisabledNodes.size());
    assertFalse(requestManager.currentLoopDisabledNodes.contains(nodeId1));
}
Also used : LlapNodeId(org.apache.hadoop.hive.llap.LlapNodeId) Message(com.google.protobuf.Message) Test(org.junit.Test)

Example 4 with Message

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

the class HbaseObjectWritableFor96Migration method tryInstantiateProtobuf.

/**
   * Try to instantiate a protocol buffer of the given message class
   * from the given input stream.
   *
   * @param protoClass the class of the generated protocol buffer
   * @param dataIn the input stream to read from
   * @return the instantiated Message instance
   * @throws IOException if an IO problem occurs
   */
static Message tryInstantiateProtobuf(Class<?> protoClass, DataInput dataIn) throws IOException {
    try {
        if (dataIn instanceof InputStream) {
            // We can use the built-in parseDelimitedFrom and not have to re-copy
            // the data
            Method parseMethod = getStaticProtobufMethod(protoClass, "parseDelimitedFrom", InputStream.class);
            return (Message) parseMethod.invoke(null, (InputStream) dataIn);
        } else {
            // Have to read it into a buffer first, since protobuf doesn't deal
            // with the DataInput interface directly.
            // Read the size delimiter that writeDelimitedTo writes
            int size = ProtoUtil.readRawVarint32(dataIn);
            if (size < 0) {
                throw new IOException("Invalid size: " + size);
            }
            byte[] data = new byte[size];
            dataIn.readFully(data);
            Method parseMethod = getStaticProtobufMethod(protoClass, "parseFrom", byte[].class);
            return (Message) parseMethod.invoke(null, data);
        }
    } catch (InvocationTargetException e) {
        if (e.getCause() instanceof IOException) {
            throw (IOException) e.getCause();
        } else {
            throw new IOException(e.getCause());
        }
    } catch (IllegalAccessException iae) {
        throw new AssertionError("Could not access parse method in " + protoClass);
    }
}
Also used : Message(com.google.protobuf.Message) ObjectInputStream(java.io.ObjectInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) Method(java.lang.reflect.Method) IOException(java.io.IOException) InvocationTargetException(java.lang.reflect.InvocationTargetException)

Example 5 with Message

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

the class HbaseObjectWritableFor96Migration method writeObject.

/**
   * Write a {@link Writable}, {@link String}, primitive type, or an array of
   * the preceding.
   * @param out
   * @param instance
   * @param declaredClass
   * @param conf
   * @throws IOException
   */
@SuppressWarnings("unchecked")
static void writeObject(DataOutput out, Object instance, Class declaredClass, Configuration conf) throws IOException {
    Object instanceObj = instance;
    Class declClass = declaredClass;
    if (instanceObj == null) {
        // null
        instanceObj = new NullInstance(declClass, conf);
        declClass = Writable.class;
    }
    writeClassCode(out, declClass);
    if (declClass.isArray()) {
        // byte-at-a-time we were previously doing.
        if (declClass.equals(byte[].class)) {
            Bytes.writeByteArray(out, (byte[]) instanceObj);
        } else {
            //if it is a Generic array, write the element's type
            if (getClassCode(declaredClass) == GENERIC_ARRAY_CODE) {
                Class<?> componentType = declaredClass.getComponentType();
                writeClass(out, componentType);
            }
            int length = Array.getLength(instanceObj);
            out.writeInt(length);
            for (int i = 0; i < length; i++) {
                Object item = Array.get(instanceObj, i);
                writeObject(out, item, item.getClass(), conf);
            }
        }
    } else if (List.class.isAssignableFrom(declClass)) {
        List list = (List) instanceObj;
        int length = list.size();
        out.writeInt(length);
        for (int i = 0; i < length; i++) {
            Object elem = list.get(i);
            writeObject(out, elem, elem == null ? Writable.class : elem.getClass(), conf);
        }
    } else if (declClass == String.class) {
        // String
        Text.writeString(out, (String) instanceObj);
    } else if (declClass.isPrimitive()) {
        // primitive type
        if (declClass == Boolean.TYPE) {
            // boolean
            out.writeBoolean(((Boolean) instanceObj).booleanValue());
        } else if (declClass == Character.TYPE) {
            // char
            out.writeChar(((Character) instanceObj).charValue());
        } else if (declClass == Byte.TYPE) {
            // byte
            out.writeByte(((Byte) instanceObj).byteValue());
        } else if (declClass == Short.TYPE) {
            // short
            out.writeShort(((Short) instanceObj).shortValue());
        } else if (declClass == Integer.TYPE) {
            // int
            out.writeInt(((Integer) instanceObj).intValue());
        } else if (declClass == Long.TYPE) {
            // long
            out.writeLong(((Long) instanceObj).longValue());
        } else if (declClass == Float.TYPE) {
            // float
            out.writeFloat(((Float) instanceObj).floatValue());
        } else if (declClass == Double.TYPE) {
            // double
            out.writeDouble(((Double) instanceObj).doubleValue());
        } else if (declClass == Void.TYPE) {
        // void
        } else {
            throw new IllegalArgumentException("Not a primitive: " + declClass);
        }
    } else if (declClass.isEnum()) {
        // enum
        Text.writeString(out, ((Enum) instanceObj).name());
    } else if (Message.class.isAssignableFrom(declaredClass)) {
        Text.writeString(out, instanceObj.getClass().getName());
        ((Message) instance).writeDelimitedTo(DataOutputOutputStream.constructOutputStream(out));
    } else if (Writable.class.isAssignableFrom(declClass)) {
        // Writable
        Class<?> c = instanceObj.getClass();
        Integer code = CLASS_TO_CODE.get(c);
        if (code == null) {
            out.writeByte(NOT_ENCODED);
            Text.writeString(out, c.getName());
        } else {
            writeClassCode(out, c);
        }
        ((Writable) instanceObj).write(out);
    } else if (Serializable.class.isAssignableFrom(declClass)) {
        Class<?> c = instanceObj.getClass();
        Integer code = CLASS_TO_CODE.get(c);
        if (code == null) {
            out.writeByte(NOT_ENCODED);
            Text.writeString(out, c.getName());
        } else {
            writeClassCode(out, c);
        }
        ByteArrayOutputStream bos = null;
        ObjectOutputStream oos = null;
        try {
            bos = new ByteArrayOutputStream();
            oos = new ObjectOutputStream(bos);
            oos.writeObject(instanceObj);
            byte[] value = bos.toByteArray();
            out.writeInt(value.length);
            out.write(value);
        } finally {
            if (bos != null)
                bos.close();
            if (oos != null)
                oos.close();
        }
    } else if (Scan.class.isAssignableFrom(declClass)) {
        Scan scan = (Scan) instanceObj;
        byte[] scanBytes = ProtobufUtil.toScan(scan).toByteArray();
        out.writeInt(scanBytes.length);
        out.write(scanBytes);
    } else {
        throw new IOException("Can't write: " + instanceObj + " as " + declClass);
    }
}
Also used : Serializable(java.io.Serializable) Message(com.google.protobuf.Message) Writable(org.apache.hadoop.io.Writable) MapWritable(org.apache.hadoop.io.MapWritable) ObjectWritable(org.apache.hadoop.io.ObjectWritable) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException) ObjectOutputStream(java.io.ObjectOutputStream) List(java.util.List) ArrayList(java.util.ArrayList) Scan(org.apache.hadoop.hbase.client.Scan)

Aggregations

Message (com.google.protobuf.Message)354 Test (org.junit.Test)137 Any (com.google.protobuf.Any)45 DynamicMessage (com.google.protobuf.DynamicMessage)31 ByteString (com.google.protobuf.ByteString)19 FieldDescriptor (com.google.protobuf.Descriptors.FieldDescriptor)18 IOException (java.io.IOException)18 ArrayList (java.util.ArrayList)18 Command (io.spine.core.Command)14 Test (org.junit.jupiter.api.Test)14 Truth.assertWithMessage (com.google.common.truth.Truth.assertWithMessage)10 EntityRecord (io.spine.server.entity.EntityRecord)10 List (java.util.List)9 HeronTuples (com.twitter.heron.proto.system.HeronTuples)8 Event (io.spine.core.Event)8 Method (java.lang.reflect.Method)8 HeronTuples (org.apache.heron.proto.system.HeronTuples)8 Descriptor (com.google.protobuf.Descriptors.Descriptor)7 Event (io.spine.base.Event)7 MockHttpInputMessage (org.springframework.http.MockHttpInputMessage)7