Search in sources :

Example 1 with DirectBufferInputStream

use of org.agrona.io.DirectBufferInputStream in project zeebe by zeebe-io.

the class PartitionsResponseTest method shouldEncodePartitions.

@Test
public void shouldEncodePartitions() throws Exception {
    // given
    final PartitionsResponse response = new PartitionsResponse();
    response.addPartition(1, BufferUtil.wrapString("default-topic"));
    response.addPartition(2, BufferUtil.wrapString("foo"));
    response.addPartition(3, BufferUtil.wrapString("foo"));
    // when
    final UnsafeBuffer buf = new UnsafeBuffer(new byte[response.getLength()]);
    response.write(buf, 0);
    // then
    final ObjectMapper objectMapper = new ObjectMapper(new MessagePackFactory());
    final JsonNode deserializedResponse = objectMapper.readTree(new DirectBufferInputStream(buf));
    assertThat(deserializedResponse.isObject()).isTrue();
    assertThat(deserializedResponse.fieldNames()).containsExactly("partitions");
    final JsonNode partitions = deserializedResponse.get("partitions");
    assertThat(partitions.isArray()).isTrue();
    assertThat(partitions.size()).isEqualTo(3);
    final JsonNode partition3 = partitions.get(0);
    assertThat(partition3.isObject()).isTrue();
    assertThat(partition3.get("topic").textValue()).isEqualTo("default-topic");
    assertThat(partition3.get("id").numberValue()).isEqualTo(1);
    final JsonNode partition1 = partitions.get(1);
    assertThat(partition1.isObject()).isTrue();
    assertThat(partition1.get("topic").textValue()).isEqualTo("foo");
    assertThat(partition1.get("id").numberValue()).isEqualTo(2);
    final JsonNode partition2 = partitions.get(2);
    assertThat(partition2.isObject()).isTrue();
    assertThat(partition2.get("topic").textValue()).isEqualTo("foo");
    assertThat(partition2.get("id").numberValue()).isEqualTo(3);
}
Also used : MessagePackFactory(org.msgpack.jackson.dataformat.MessagePackFactory) DirectBufferInputStream(org.agrona.io.DirectBufferInputStream) PartitionsResponse(io.zeebe.broker.system.log.PartitionsResponse) JsonNode(com.fasterxml.jackson.databind.JsonNode) UnsafeBuffer(org.agrona.concurrent.UnsafeBuffer) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) Test(org.junit.Test)

Example 2 with DirectBufferInputStream

use of org.agrona.io.DirectBufferInputStream in project zeebe by zeebe-io.

the class ControlMessageResponse method wrap.

@Override
public void wrap(DirectBuffer responseBuffer, int offset, int length) {
    messageHeaderDecoder.wrap(responseBuffer, 0);
    if (messageHeaderDecoder.templateId() != responseDecoder.sbeTemplateId()) {
        if (messageHeaderDecoder.templateId() == ErrorResponseDecoder.TEMPLATE_ID) {
            errorResponse.wrap(responseBuffer, offset, length);
            throw new RuntimeException("Unexpected error response from broker: " + errorResponse.getErrorCode() + " - " + errorResponse.getErrorData());
        } else {
            throw new RuntimeException("Unexpected response from broker. Template id " + messageHeaderDecoder.templateId());
        }
    }
    responseDecoder.wrap(responseBuffer, messageHeaderDecoder.encodedLength(), messageHeaderDecoder.blockLength(), messageHeaderDecoder.version());
    final int dataLength = responseDecoder.dataLength();
    final int dataOffset = messageHeaderDecoder.encodedLength() + messageHeaderDecoder.blockLength() + ControlMessageResponseDecoder.dataHeaderLength();
    try (InputStream is = new DirectBufferInputStream(responseBuffer, dataOffset, dataLength)) {
        data = msgPackHelper.readMsgPack(is);
    } catch (IOException e) {
        LangUtil.rethrowUnchecked(e);
    }
}
Also used : DirectBufferInputStream(org.agrona.io.DirectBufferInputStream) DirectBufferInputStream(org.agrona.io.DirectBufferInputStream) InputStream(java.io.InputStream) IOException(java.io.IOException)

Example 3 with DirectBufferInputStream

use of org.agrona.io.DirectBufferInputStream in project zeebe by zeebe-io.

the class SubscribedEvent method wrap.

@Override
public void wrap(DirectBuffer responseBuffer, int offset, int length) {
    headerDecoder.wrap(responseBuffer, offset);
    if (headerDecoder.templateId() != bodyDecoder.sbeTemplateId()) {
        throw new RuntimeException("Unexpected response from broker.");
    }
    bodyDecoder.wrap(responseBuffer, offset + headerDecoder.encodedLength(), headerDecoder.blockLength(), headerDecoder.version());
    final int eventLength = bodyDecoder.eventLength();
    final int eventOffset = bodyDecoder.limit() + eventHeaderLength();
    try (InputStream is = new DirectBufferInputStream(responseBuffer, offset + eventOffset, eventLength)) {
        event = msgPackHelper.readMsgPack(is);
    } catch (IOException e) {
        LangUtil.rethrowUnchecked(e);
    }
}
Also used : DirectBufferInputStream(org.agrona.io.DirectBufferInputStream) DirectBufferInputStream(org.agrona.io.DirectBufferInputStream) InputStream(java.io.InputStream) IOException(java.io.IOException)

Example 4 with DirectBufferInputStream

use of org.agrona.io.DirectBufferInputStream in project zeebe by zeebe-io.

the class CommandRequestHandler method getResult.

@Override
public EventImpl getResult(DirectBuffer buffer, int offset, int blockLength, int version) {
    decoder.wrap(buffer, offset, blockLength, version);
    final long key = decoder.key();
    final int partitionId = decoder.partitionId();
    final long position = decoder.position();
    final int eventLength = decoder.eventLength();
    final DirectBufferInputStream inStream = new DirectBufferInputStream(buffer, decoder.limit() + ExecuteCommandResponseDecoder.eventHeaderLength(), eventLength);
    final EventImpl result;
    try {
        result = objectMapper.readValue(inStream, event.getClass());
    } catch (Exception e) {
        throw new ClientException("Cannot deserialize event in response", e);
    }
    result.setKey(key);
    result.setPartitionId(partitionId);
    result.setTopicName(event.getMetadata().getTopicName());
    result.setEventPosition(position);
    if (expectedState != null && !expectedState.equals(result.getState())) {
        throw new ClientCommandRejectedException(errorFunction.apply(event, result));
    }
    return result;
}
Also used : DirectBufferInputStream(org.agrona.io.DirectBufferInputStream) EventImpl(io.zeebe.client.event.impl.EventImpl) ClientCommandRejectedException(io.zeebe.client.cmd.ClientCommandRejectedException) ClientException(io.zeebe.client.cmd.ClientException) ClientException(io.zeebe.client.cmd.ClientException) ClientCommandRejectedException(io.zeebe.client.cmd.ClientCommandRejectedException)

Example 5 with DirectBufferInputStream

use of org.agrona.io.DirectBufferInputStream in project zeebe by zeebe-io.

the class ControlMessageRequestHandler method getResult.

@SuppressWarnings({ "unchecked", "resource" })
@Override
public Object getResult(DirectBuffer buffer, int offset, int blockLength, int version) {
    decoder.wrap(buffer, offset, blockLength, version);
    final int dataLength = decoder.dataLength();
    final DirectBufferInputStream inStream = new DirectBufferInputStream(buffer, decoder.limit() + ControlMessageRequestDecoder.dataHeaderLength(), dataLength);
    final Object response;
    try {
        response = objectMapper.readValue(inStream, message.getResponseClass());
    } catch (Exception e) {
        throw new RuntimeException("Cannot deserialize event in response", e);
    }
    message.onResponse(response);
    return response;
}
Also used : DirectBufferInputStream(org.agrona.io.DirectBufferInputStream)

Aggregations

DirectBufferInputStream (org.agrona.io.DirectBufferInputStream)7 IOException (java.io.IOException)3 InputStream (java.io.InputStream)3 UnsafeBuffer (org.agrona.concurrent.UnsafeBuffer)2 JsonNode (com.fasterxml.jackson.databind.JsonNode)1 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)1 PartitionsResponse (io.zeebe.broker.system.log.PartitionsResponse)1 ClientCommandRejectedException (io.zeebe.client.cmd.ClientCommandRejectedException)1 ClientException (io.zeebe.client.cmd.ClientException)1 EventImpl (io.zeebe.client.event.impl.EventImpl)1 ControlMessageResponseDecoder (io.zeebe.protocol.clientapi.ControlMessageResponseDecoder)1 Test (org.junit.Test)1 MessagePackFactory (org.msgpack.jackson.dataformat.MessagePackFactory)1