Search in sources :

Example 6 with ControlledMessageHandler

use of org.agrona.concurrent.ControlledMessageHandler in project agrona by real-logic.

the class ManyToOneRingBuffer method controlledRead.

/**
 * {@inheritDoc}
 */
public int controlledRead(final ControlledMessageHandler handler, final int messageCountLimit) {
    int messagesRead = 0;
    final AtomicBuffer buffer = this.buffer;
    final int headPositionIndex = this.headPositionIndex;
    long head = buffer.getLong(headPositionIndex);
    final int capacity = this.capacity;
    int headIndex = (int) head & (capacity - 1);
    final int maxBlockLength = capacity - headIndex;
    int bytesRead = 0;
    try {
        while ((bytesRead < maxBlockLength) && (messagesRead < messageCountLimit)) {
            final int recordIndex = headIndex + bytesRead;
            final int recordLength = buffer.getIntVolatile(lengthOffset(recordIndex));
            if (recordLength <= 0) {
                break;
            }
            final int alignedLength = align(recordLength, ALIGNMENT);
            bytesRead += alignedLength;
            final int messageTypeId = buffer.getInt(typeOffset(recordIndex));
            if (PADDING_MSG_TYPE_ID == messageTypeId) {
                continue;
            }
            final ControlledMessageHandler.Action action = handler.onMessage(messageTypeId, buffer, recordIndex + HEADER_LENGTH, recordLength - HEADER_LENGTH);
            if (ABORT == action) {
                bytesRead -= alignedLength;
                break;
            }
            ++messagesRead;
            if (BREAK == action) {
                break;
            }
            if (COMMIT == action) {
                buffer.setMemory(headIndex, bytesRead, (byte) 0);
                buffer.putLongOrdered(headPositionIndex, head + bytesRead);
                headIndex += bytesRead;
                head += bytesRead;
                bytesRead = 0;
            }
        }
    } finally {
        if (bytesRead > 0) {
            buffer.setMemory(headIndex, bytesRead, (byte) 0);
            buffer.putLongOrdered(headPositionIndex, head + bytesRead);
        }
    }
    return messagesRead;
}
Also used : Action(org.agrona.concurrent.ControlledMessageHandler.Action) ControlledMessageHandler(org.agrona.concurrent.ControlledMessageHandler) AtomicBuffer(org.agrona.concurrent.AtomicBuffer)

Example 7 with ControlledMessageHandler

use of org.agrona.concurrent.ControlledMessageHandler in project agrona by real-logic.

the class OneToOneRingBuffer method controlledRead.

/**
 * {@inheritDoc}
 */
public int controlledRead(final ControlledMessageHandler handler, final int messageCountLimit) {
    int messagesRead = 0;
    final AtomicBuffer buffer = this.buffer;
    final int headPositionIndex = this.headPositionIndex;
    long head = buffer.getLong(headPositionIndex);
    int bytesRead = 0;
    final int capacity = this.capacity;
    int headIndex = (int) head & (capacity - 1);
    final int contiguousBlockLength = capacity - headIndex;
    try {
        while ((bytesRead < contiguousBlockLength) && (messagesRead < messageCountLimit)) {
            final int recordIndex = headIndex + bytesRead;
            final int recordLength = buffer.getIntVolatile(lengthOffset(recordIndex));
            if (recordLength <= 0) {
                break;
            }
            final int alignedLength = align(recordLength, ALIGNMENT);
            bytesRead += alignedLength;
            final int messageTypeId = buffer.getInt(typeOffset(recordIndex));
            if (PADDING_MSG_TYPE_ID == messageTypeId) {
                continue;
            }
            final ControlledMessageHandler.Action action = handler.onMessage(messageTypeId, buffer, recordIndex + HEADER_LENGTH, recordLength - HEADER_LENGTH);
            if (ABORT == action) {
                bytesRead -= alignedLength;
                break;
            }
            ++messagesRead;
            if (BREAK == action) {
                break;
            }
            if (COMMIT == action) {
                buffer.putLongOrdered(headPositionIndex, head + bytesRead);
                headIndex += bytesRead;
                head += bytesRead;
                bytesRead = 0;
            }
        }
    } finally {
        if (bytesRead > 0) {
            buffer.putLongOrdered(headPositionIndex, head + bytesRead);
        }
    }
    return messagesRead;
}
Also used : Action(org.agrona.concurrent.ControlledMessageHandler.Action) ControlledMessageHandler(org.agrona.concurrent.ControlledMessageHandler) AtomicBuffer(org.agrona.concurrent.AtomicBuffer)

Example 8 with ControlledMessageHandler

use of org.agrona.concurrent.ControlledMessageHandler in project agrona by real-logic.

the class ManyToOneRingBufferTest method shouldAbortOnControlledReadOfSecondMessage.

@Test
public void shouldAbortOnControlledReadOfSecondMessage() {
    final String msg = "Hello World";
    final ExpandableArrayBuffer srcBuffer = new ExpandableArrayBuffer();
    final int srcLength = srcBuffer.putStringAscii(0, msg);
    final RingBuffer ringBuffer = new ManyToOneRingBuffer(new UnsafeBuffer(new byte[1024]));
    final MutableInteger counter = new MutableInteger();
    assertTrue(ringBuffer.write(MSG_TYPE_ID, srcBuffer, 0, srcLength));
    assertTrue(ringBuffer.write(MSG_TYPE_ID, srcBuffer, 0, srcLength));
    final ControlledMessageHandler controlledMessageHandler = (msgTypeId, buffer, index, length) -> {
        return counter.getAndIncrement() == 0 ? ControlledMessageHandler.Action.CONTINUE : ControlledMessageHandler.Action.ABORT;
    };
    final int messagesRead = ringBuffer.controlledRead(controlledMessageHandler);
    assertEquals(2, counter.get());
    assertEquals(1, messagesRead);
    assertNotEquals(ringBuffer.producerPosition(), ringBuffer.consumerPosition());
}
Also used : BeforeEach(org.junit.jupiter.api.BeforeEach) Arrays(java.util.Arrays) RingBufferDescriptor(org.agrona.concurrent.ringbuffer.RingBufferDescriptor) IntConsumer(java.util.function.IntConsumer) UnsafeBuffer(org.agrona.concurrent.UnsafeBuffer) RecordDescriptor(org.agrona.concurrent.ringbuffer.RecordDescriptor) MessageHandler(org.agrona.concurrent.MessageHandler) MIN_CAPACITY(org.agrona.concurrent.ringbuffer.ManyToOneRingBuffer.MIN_CAPACITY) ControlledMessageHandler(org.agrona.concurrent.ControlledMessageHandler) SIZE_OF_LONG(org.agrona.BitUtil.SIZE_OF_LONG) MatcherAssert.assertThat(org.hamcrest.MatcherAssert.assertThat) MutableInteger(org.agrona.collections.MutableInteger) MethodSource(org.junit.jupiter.params.provider.MethodSource) ValueSource(org.junit.jupiter.params.provider.ValueSource) InOrder(org.mockito.InOrder) PADDING_MSG_TYPE_ID(org.agrona.concurrent.ringbuffer.ManyToOneRingBuffer.PADDING_MSG_TYPE_ID) ExpandableArrayBuffer(org.agrona.ExpandableArrayBuffer) Arguments(org.junit.jupiter.params.provider.Arguments) Test(org.junit.jupiter.api.Test) Mockito(org.mockito.Mockito) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest) List(java.util.List) INSUFFICIENT_CAPACITY(org.agrona.concurrent.ringbuffer.RingBuffer.INSUFFICIENT_CAPACITY) Assertions(org.junit.jupiter.api.Assertions) Matchers.is(org.hamcrest.Matchers.is) TRUE(java.lang.Boolean.TRUE) BitUtil.align(org.agrona.BitUtil.align) ControlledMessageHandler(org.agrona.concurrent.ControlledMessageHandler) MutableInteger(org.agrona.collections.MutableInteger) UnsafeBuffer(org.agrona.concurrent.UnsafeBuffer) ExpandableArrayBuffer(org.agrona.ExpandableArrayBuffer) Test(org.junit.jupiter.api.Test) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Example 9 with ControlledMessageHandler

use of org.agrona.concurrent.ControlledMessageHandler in project agrona by real-logic.

the class OneToOneRingBufferTest method shouldAbortOnControlledRead.

@Test
public void shouldAbortOnControlledRead() {
    final String msg = "Hello World";
    final ExpandableArrayBuffer srcBuffer = new ExpandableArrayBuffer();
    final int srcLength = srcBuffer.putStringAscii(0, msg);
    final RingBuffer ringBuffer = new OneToOneRingBuffer(new UnsafeBuffer(new byte[1024]));
    assertTrue(ringBuffer.write(MSG_TYPE_ID, srcBuffer, 0, srcLength));
    final ControlledMessageHandler controlledMessageHandler = (msgTypeId, buffer, index, length) -> {
        assertEquals(MSG_TYPE_ID, msgTypeId);
        assertEquals(HEADER_LENGTH, index);
        assertEquals(srcLength, length);
        assertEquals(msg, buffer.getStringAscii(index));
        return ControlledMessageHandler.Action.ABORT;
    };
    final int messagesRead = ringBuffer.controlledRead(controlledMessageHandler, 1);
    assertEquals(0, messagesRead);
    assertEquals(0, ringBuffer.consumerPosition());
}
Also used : BeforeEach(org.junit.jupiter.api.BeforeEach) Arrays(java.util.Arrays) RingBufferDescriptor(org.agrona.concurrent.ringbuffer.RingBufferDescriptor) IntConsumer(java.util.function.IntConsumer) UnsafeBuffer(org.agrona.concurrent.UnsafeBuffer) RecordDescriptor(org.agrona.concurrent.ringbuffer.RecordDescriptor) MessageHandler(org.agrona.concurrent.MessageHandler) ControlledMessageHandler(org.agrona.concurrent.ControlledMessageHandler) SIZE_OF_LONG(org.agrona.BitUtil.SIZE_OF_LONG) MatcherAssert.assertThat(org.hamcrest.MatcherAssert.assertThat) PADDING_MSG_TYPE_ID(org.agrona.concurrent.ringbuffer.RingBuffer.PADDING_MSG_TYPE_ID) MutableInteger(org.agrona.collections.MutableInteger) MethodSource(org.junit.jupiter.params.provider.MethodSource) ValueSource(org.junit.jupiter.params.provider.ValueSource) InOrder(org.mockito.InOrder) ExpandableArrayBuffer(org.agrona.ExpandableArrayBuffer) Arguments(org.junit.jupiter.params.provider.Arguments) Test(org.junit.jupiter.api.Test) Mockito(org.mockito.Mockito) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest) List(java.util.List) INSUFFICIENT_CAPACITY(org.agrona.concurrent.ringbuffer.RingBuffer.INSUFFICIENT_CAPACITY) MIN_CAPACITY(org.agrona.concurrent.ringbuffer.OneToOneRingBuffer.MIN_CAPACITY) Assertions(org.junit.jupiter.api.Assertions) Matchers.is(org.hamcrest.Matchers.is) BitUtil.align(org.agrona.BitUtil.align) ControlledMessageHandler(org.agrona.concurrent.ControlledMessageHandler) UnsafeBuffer(org.agrona.concurrent.UnsafeBuffer) ExpandableArrayBuffer(org.agrona.ExpandableArrayBuffer) Test(org.junit.jupiter.api.Test) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Example 10 with ControlledMessageHandler

use of org.agrona.concurrent.ControlledMessageHandler in project agrona by real-logic.

the class OneToOneRingBufferTest method shouldAbortOnControlledReadOfSecondMessage.

@Test
public void shouldAbortOnControlledReadOfSecondMessage() {
    final String msg = "Hello World";
    final ExpandableArrayBuffer srcBuffer = new ExpandableArrayBuffer();
    final int srcLength = srcBuffer.putStringAscii(0, msg);
    final RingBuffer ringBuffer = new OneToOneRingBuffer(new UnsafeBuffer(new byte[1024]));
    final MutableInteger counter = new MutableInteger();
    assertTrue(ringBuffer.write(MSG_TYPE_ID, srcBuffer, 0, srcLength));
    assertTrue(ringBuffer.write(MSG_TYPE_ID, srcBuffer, 0, srcLength));
    final ControlledMessageHandler controlledMessageHandler = (msgTypeId, buffer, index, length) -> {
        return counter.getAndIncrement() == 0 ? ControlledMessageHandler.Action.CONTINUE : ControlledMessageHandler.Action.ABORT;
    };
    final int messagesRead = ringBuffer.controlledRead(controlledMessageHandler);
    assertEquals(2, counter.get());
    assertEquals(1, messagesRead);
    assertNotEquals(ringBuffer.producerPosition(), ringBuffer.consumerPosition());
}
Also used : BeforeEach(org.junit.jupiter.api.BeforeEach) Arrays(java.util.Arrays) RingBufferDescriptor(org.agrona.concurrent.ringbuffer.RingBufferDescriptor) IntConsumer(java.util.function.IntConsumer) UnsafeBuffer(org.agrona.concurrent.UnsafeBuffer) RecordDescriptor(org.agrona.concurrent.ringbuffer.RecordDescriptor) MessageHandler(org.agrona.concurrent.MessageHandler) ControlledMessageHandler(org.agrona.concurrent.ControlledMessageHandler) SIZE_OF_LONG(org.agrona.BitUtil.SIZE_OF_LONG) MatcherAssert.assertThat(org.hamcrest.MatcherAssert.assertThat) PADDING_MSG_TYPE_ID(org.agrona.concurrent.ringbuffer.RingBuffer.PADDING_MSG_TYPE_ID) MutableInteger(org.agrona.collections.MutableInteger) MethodSource(org.junit.jupiter.params.provider.MethodSource) ValueSource(org.junit.jupiter.params.provider.ValueSource) InOrder(org.mockito.InOrder) ExpandableArrayBuffer(org.agrona.ExpandableArrayBuffer) Arguments(org.junit.jupiter.params.provider.Arguments) Test(org.junit.jupiter.api.Test) Mockito(org.mockito.Mockito) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest) List(java.util.List) INSUFFICIENT_CAPACITY(org.agrona.concurrent.ringbuffer.RingBuffer.INSUFFICIENT_CAPACITY) MIN_CAPACITY(org.agrona.concurrent.ringbuffer.OneToOneRingBuffer.MIN_CAPACITY) Assertions(org.junit.jupiter.api.Assertions) Matchers.is(org.hamcrest.Matchers.is) BitUtil.align(org.agrona.BitUtil.align) ControlledMessageHandler(org.agrona.concurrent.ControlledMessageHandler) MutableInteger(org.agrona.collections.MutableInteger) UnsafeBuffer(org.agrona.concurrent.UnsafeBuffer) ExpandableArrayBuffer(org.agrona.ExpandableArrayBuffer) Test(org.junit.jupiter.api.Test) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Aggregations

ControlledMessageHandler (org.agrona.concurrent.ControlledMessageHandler)10 Arrays (java.util.Arrays)8 List (java.util.List)8 IntConsumer (java.util.function.IntConsumer)8 SIZE_OF_LONG (org.agrona.BitUtil.SIZE_OF_LONG)8 BitUtil.align (org.agrona.BitUtil.align)8 ExpandableArrayBuffer (org.agrona.ExpandableArrayBuffer)8 MutableInteger (org.agrona.collections.MutableInteger)8 MessageHandler (org.agrona.concurrent.MessageHandler)8 UnsafeBuffer (org.agrona.concurrent.UnsafeBuffer)8 RecordDescriptor (org.agrona.concurrent.ringbuffer.RecordDescriptor)8 INSUFFICIENT_CAPACITY (org.agrona.concurrent.ringbuffer.RingBuffer.INSUFFICIENT_CAPACITY)8 RingBufferDescriptor (org.agrona.concurrent.ringbuffer.RingBufferDescriptor)8 MatcherAssert.assertThat (org.hamcrest.MatcherAssert.assertThat)8 Matchers.is (org.hamcrest.Matchers.is)8 Assertions (org.junit.jupiter.api.Assertions)8 BeforeEach (org.junit.jupiter.api.BeforeEach)8 Test (org.junit.jupiter.api.Test)8 ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)8 Arguments (org.junit.jupiter.params.provider.Arguments)8