Search in sources :

Example 1 with MessagePacker

use of org.msgpack.core.MessagePacker in project fluency by komamitsu.

the class FluencyTest method testWithAckResponseWithProperToken.

@Theory
public void testWithAckResponseWithProperToken(final boolean sslEnabled) throws Throwable {
    Exception exception = new ConfigurableTestServer(sslEnabled).run(new ConfigurableTestServer.WithClientSocket() {

        @Override
        public void run(Socket clientSocket) throws Exception {
            MessageUnpacker unpacker = MessagePack.newDefaultUnpacker(clientSocket.getInputStream());
            assertEquals(3, unpacker.unpackArrayHeader());
            assertEquals("foo.bar", unpacker.unpackString());
            ImmutableRawValue rawValue = unpacker.unpackValue().asRawValue();
            Map<Value, Value> map = unpacker.unpackValue().asMapValue().map();
            assertEquals(2, map.size());
            assertEquals(rawValue.asByteArray().length, map.get(KEY_OPTION_SIZE).asIntegerValue().asInt());
            String ackResponseToken = map.get(KEY_OPTION_CHUNK).asRawValue().asString();
            assertNotNull(ackResponseToken);
            MessagePacker packer = MessagePack.newDefaultPacker(clientSocket.getOutputStream());
            packer.packMapHeader(1).packString("ack").packString(ackResponseToken).close();
            // Close the input stream after closing the output stream to avoid closing a socket too early
            unpacker.close();
        }
    }, new ConfigurableTestServer.WithServerPort() {

        @Override
        public void run(int serverPort) throws Exception {
            Fluency fluency = Fluency.defaultFluency(serverPort, new Fluency.Config().setSslEnabled(sslEnabled).setAckResponseMode(true));
            fluency.emit("foo.bar", new HashMap<String, Object>());
            fluency.close();
        }
    }, 5000);
    assertNull(exception);
}
Also used : HashMap(java.util.HashMap) ImmutableRawValue(org.msgpack.value.ImmutableRawValue) TimeoutException(java.util.concurrent.TimeoutException) IOException(java.io.IOException) MessagePacker(org.msgpack.core.MessagePacker) MessageUnpacker(org.msgpack.core.MessageUnpacker) Map(java.util.Map) HashMap(java.util.HashMap) Socket(java.net.Socket) Theory(org.junit.experimental.theories.Theory)

Example 2 with MessagePacker

use of org.msgpack.core.MessagePacker in project fluency by komamitsu.

the class PackedForwardBuffer method flushInternal.

@Override
public void flushInternal(Sender sender, boolean force) throws IOException {
    moveRetentionBuffersToFlushable(force);
    TaggableBuffer flushableBuffer;
    ByteArrayOutputStream header = new ByteArrayOutputStream();
    MessagePacker messagePacker = MessagePack.newDefaultPacker(header);
    while (!Thread.currentThread().isInterrupted() && (flushableBuffer = flushableBuffers.poll()) != null) {
        boolean keepBuffer = false;
        try {
            LOG.trace("flushInternal(): bufferUsage={}, flushableBuffer={}", getBufferUsage(), flushableBuffer);
            String tag = flushableBuffer.getTag();
            ByteBuffer dataBuffer = flushableBuffer.getByteBuffer();
            int dataLength = dataBuffer.limit();
            messagePacker.packArrayHeader(3);
            messagePacker.packString(tag);
            messagePacker.packRawStringHeader(dataLength);
            messagePacker.flush();
            ByteBuffer headerBuffer = ByteBuffer.wrap(header.toByteArray());
            try {
                if (config.isAckResponseMode()) {
                    byte[] uuidBytes = UUID.randomUUID().toString().getBytes(CHARSET);
                    ByteBuffer optionBuffer = ByteBuffer.wrap(objectMapper.writeValueAsBytes(new RequestOption(dataLength, uuidBytes)));
                    List<ByteBuffer> buffers = Arrays.asList(headerBuffer, dataBuffer, optionBuffer);
                    synchronized (sender) {
                        sender.sendWithAck(buffers, uuidBytes);
                    }
                } else {
                    ByteBuffer optionBuffer = ByteBuffer.wrap(objectMapper.writeValueAsBytes(new RequestOption(dataLength, null)));
                    List<ByteBuffer> buffers = Arrays.asList(headerBuffer, dataBuffer, optionBuffer);
                    synchronized (sender) {
                        sender.send(buffers);
                    }
                }
            } catch (IOException e) {
                LOG.warn("Failed to send data. The data is going to be saved into the buffer again: data={}", flushableBuffer);
                keepBuffer = true;
                throw e;
            }
        } finally {
            header.reset();
            if (keepBuffer) {
                try {
                    flushableBuffers.put(flushableBuffer);
                } catch (InterruptedException e1) {
                    LOG.warn("Failed to save the data into the buffer. Trying to save it in extra buffer: chunk={}", flushableBuffer);
                    backupBuffers.add(flushableBuffer);
                }
            } else {
                bufferPool.returnBuffer(flushableBuffer.getByteBuffer());
            }
        }
    }
}
Also used : MessagePacker(org.msgpack.core.MessagePacker) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException) ByteBuffer(java.nio.ByteBuffer) MappedByteBuffer(java.nio.MappedByteBuffer) RequestOption(org.komamitsu.fluency.format.RequestOption)

Example 3 with MessagePacker

use of org.msgpack.core.MessagePacker in project zeebe by zeebe-io.

the class MsgPackConverterTest method createMsgPack.

protected static byte[] createMsgPack() {
    byte[] msgPack = null;
    try (ByteArrayOutputStream outputStream = new ByteArrayOutputStream()) {
        final MessagePacker payloadPacker = MessagePack.newDefaultPacker(outputStream);
        payloadPacker.packMapHeader(2).packString("key1").packInt(1).packString("key2").packInt(2);
        payloadPacker.flush();
        msgPack = outputStream.toByteArray();
    } catch (Exception e) {
        LangUtil.rethrowUnchecked(e);
    }
    return msgPack;
}
Also used : MessagePacker(org.msgpack.core.MessagePacker) ByteArrayOutputStream(java.io.ByteArrayOutputStream) ExpectedException(org.junit.rules.ExpectedException)

Example 4 with MessagePacker

use of org.msgpack.core.MessagePacker in project LogHub by fbacchella.

the class TestMsgpack method testmapsimple.

@Test
public void testmapsimple() throws IOException, DecodeException {
    Decoder d = new Msgpack();
    Map<Value, Value> destination = new HashMap<>();
    destination.put(ValueFactory.newString("a"), ValueFactory.newString("0"));
    destination.put(ValueFactory.newString("b"), ValueFactory.newInteger(1));
    destination.put(ValueFactory.newString("c"), ValueFactory.newBoolean(false));
    Value[] subdestination = new Value[4];
    subdestination[0] = ValueFactory.newString("0");
    subdestination[1] = ValueFactory.newInteger(1);
    subdestination[2] = ValueFactory.newFloat(2.0);
    subdestination[3] = ValueFactory.newNil();
    destination.put(ValueFactory.newString("d"), ValueFactory.newArray(subdestination));
    Value v = ValueFactory.newMap(destination);
    ByteArrayOutputStream out = new ByteArrayOutputStream();
    MessagePacker packer = MessagePack.newDefaultPacker(out);
    packer.packValue(v);
    packer.close();
    byte[] packed = out.toByteArray();
    Map<String, Object> e = d.decode(ConnectionContext.EMPTY, packed);
    testContent(e);
}
Also used : MessagePacker(org.msgpack.core.MessagePacker) HashMap(java.util.HashMap) Value(org.msgpack.value.Value) ByteArrayOutputStream(java.io.ByteArrayOutputStream) Decoder(loghub.Decoder) Test(org.junit.Test)

Example 5 with MessagePacker

use of org.msgpack.core.MessagePacker in project fluency by komamitsu.

the class FluencyTest method testWithAckResponseButWrongReceiveToken.

@Theory
public void testWithAckResponseButWrongReceiveToken(final boolean sslEnabled) throws Throwable {
    Exception exception = new ConfigurableTestServer(sslEnabled).run(new ConfigurableTestServer.WithClientSocket() {

        @Override
        public void run(Socket clientSocket) throws Exception {
            MessageUnpacker unpacker = MessagePack.newDefaultUnpacker(clientSocket.getInputStream());
            assertEquals(3, unpacker.unpackArrayHeader());
            assertEquals("foo.bar", unpacker.unpackString());
            ImmutableRawValue rawValue = unpacker.unpackValue().asRawValue();
            Map<Value, Value> map = unpacker.unpackValue().asMapValue().map();
            assertEquals(2, map.size());
            assertEquals(rawValue.asByteArray().length, map.get(KEY_OPTION_SIZE).asIntegerValue().asInt());
            assertNotNull(map.get(KEY_OPTION_CHUNK).asRawValue().asString());
            MessagePacker packer = MessagePack.newDefaultPacker(clientSocket.getOutputStream());
            packer.packMapHeader(1).packString("ack").packString(UUID.randomUUID().toString()).close();
            // Close the input stream after closing the output stream to avoid closing a socket too early
            unpacker.close();
        }
    }, new ConfigurableTestServer.WithServerPort() {

        @Override
        public void run(int serverPort) throws Exception {
            Fluency fluency = Fluency.defaultFluency(serverPort, new Fluency.Config().setSslEnabled(sslEnabled).setAckResponseMode(true));
            fluency.emit("foo.bar", new HashMap<String, Object>());
            fluency.close();
        }
    }, 5000);
    assertEquals(exception.getClass(), TimeoutException.class);
}
Also used : HashMap(java.util.HashMap) ImmutableRawValue(org.msgpack.value.ImmutableRawValue) TimeoutException(java.util.concurrent.TimeoutException) IOException(java.io.IOException) MessagePacker(org.msgpack.core.MessagePacker) MessageUnpacker(org.msgpack.core.MessageUnpacker) Map(java.util.Map) HashMap(java.util.HashMap) Socket(java.net.Socket) Theory(org.junit.experimental.theories.Theory)

Aggregations

MessagePacker (org.msgpack.core.MessagePacker)6 ByteArrayOutputStream (java.io.ByteArrayOutputStream)4 IOException (java.io.IOException)4 HashMap (java.util.HashMap)3 MessageUnpacker (org.msgpack.core.MessageUnpacker)3 Socket (java.net.Socket)2 Map (java.util.Map)2 TimeoutException (java.util.concurrent.TimeoutException)2 Test (org.junit.Test)2 Theory (org.junit.experimental.theories.Theory)2 ImmutableRawValue (org.msgpack.value.ImmutableRawValue)2 Value (org.msgpack.value.Value)2 TDBulkImportSession (com.treasuredata.client.model.TDBulkImportSession)1 TDColumn (com.treasuredata.client.model.TDColumn)1 TDTable (com.treasuredata.client.model.TDTable)1 File (java.io.File)1 InputStream (java.io.InputStream)1 OutputStream (java.io.OutputStream)1 ByteBuffer (java.nio.ByteBuffer)1 MappedByteBuffer (java.nio.MappedByteBuffer)1