Java Code Examples for org.agrona.concurrent.UnsafeBuffer#setMemory()

The following examples show how to use org.agrona.concurrent.UnsafeBuffer#setMemory() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: CommonEventEncoderTest.java    From aeron with Apache License 2.0 6 votes vote down vote up
@Test
void encodeBufferSmallerThanMaxCaptureSize()
{
    final UnsafeBuffer srcBuffer = new UnsafeBuffer(allocateDirectAligned(256, CACHE_LINE_LENGTH));
    final int offset = 24;
    final int srcOffset = 20;
    final int length = 128;
    srcBuffer.setMemory(srcOffset, length, (byte)111);

    final int encodedLength = encode(buffer, offset, length, length, srcBuffer, srcOffset);

    assertEquals(LOG_HEADER_LENGTH + length, encodedLength);
    assertEquals(length, buffer.getInt(offset, LITTLE_ENDIAN));
    assertEquals(length, buffer.getInt(offset + SIZE_OF_INT, LITTLE_ENDIAN));
    assertNotEquals(0, buffer.getLong(offset + SIZE_OF_INT * 2, LITTLE_ENDIAN));
    for (int i = 0; i < length; i++)
    {
        assertEquals(111, buffer.getByte(offset + LOG_HEADER_LENGTH + i));
    }
}
 
Example 2
Source File: FragmentAssemblerTest.java    From aeron with Apache License 2.0 6 votes vote down vote up
@Test
public void shouldFreeSessionBuffer()
{
    when(header.flags())
        .thenReturn(FrameDescriptor.BEGIN_FRAG_FLAG)
        .thenReturn(FrameDescriptor.END_FRAG_FLAG);

    final UnsafeBuffer srcBuffer = new UnsafeBuffer(new byte[1024]);
    final int offset = 0;
    final int length = srcBuffer.capacity() / 2;

    srcBuffer.setMemory(0, length, (byte)65);
    srcBuffer.setMemory(length, length, (byte)66);

    assertFalse(adapter.freeSessionBuffer(SESSION_ID));

    adapter.onFragment(srcBuffer, offset, length, header);
    adapter.onFragment(srcBuffer, length, length, header);

    assertTrue(adapter.freeSessionBuffer(SESSION_ID));
    assertFalse(adapter.freeSessionBuffer(SESSION_ID));
}
 
Example 3
Source File: SpySubscriptionTest.java    From aeron with Apache License 2.0 5 votes vote down vote up
@ParameterizedTest
@MethodSource("channels")
@Timeout(10)
public void shouldReceivePublishedMessage(final String channel)
{
    try (Subscription subscription = aeron.addSubscription(channel, STREAM_ID);
        Subscription spy = aeron.addSubscription(SPY_PREFIX + channel, STREAM_ID);
        Publication publication = aeron.addPublication(channel, STREAM_ID))
    {
        final int expectedMessageCount = 4;
        final UnsafeBuffer srcBuffer = new UnsafeBuffer(new byte[PAYLOAD_LENGTH * expectedMessageCount]);

        for (int i = 0; i < expectedMessageCount; i++)
        {
            srcBuffer.setMemory(i * PAYLOAD_LENGTH, PAYLOAD_LENGTH, (byte)(65 + i));
        }

        for (int i = 0; i < expectedMessageCount; i++)
        {
            while (publication.offer(srcBuffer, i * PAYLOAD_LENGTH, PAYLOAD_LENGTH) < 0L)
            {
                Tests.yield();
            }
        }

        int numFragments = 0;
        int numSpyFragments = 0;
        do
        {
            Tests.yield();

            numFragments += subscription.poll(fragmentHandlerSub, FRAGMENT_COUNT_LIMIT);
            numSpyFragments += spy.poll(fragmentHandlerSpy, FRAGMENT_COUNT_LIMIT);
        }
        while (numSpyFragments < expectedMessageCount || numFragments < expectedMessageCount);

        assertEquals(expectedMessageCount, fragmentCountSpy.value);
        assertEquals(expectedMessageCount, fragmentCountSub.value);
    }
}
 
Example 4
Source File: NetworkPublication.java    From aeron with Apache License 2.0 5 votes vote down vote up
private void cleanBufferTo(final long position)
{
    final long cleanPosition = this.cleanPosition;
    if (position > cleanPosition)
    {
        final UnsafeBuffer dirtyTerm = termBuffers[indexByPosition(cleanPosition, positionBitsToShift)];
        final int bytesForCleaning = (int)(position - cleanPosition);
        final int termOffset = (int)cleanPosition & termLengthMask;
        final int length = Math.min(bytesForCleaning, termBufferLength - termOffset);

        dirtyTerm.setMemory(termOffset + SIZE_OF_LONG, length - SIZE_OF_LONG, (byte)0);
        dirtyTerm.putLongOrdered(termOffset, 0);
        this.cleanPosition = cleanPosition + length;
    }
}
 
Example 5
Source File: TerminateDriverFlyweightTest.java    From aeron with Apache License 2.0 5 votes vote down vote up
@Test
void tokenBuffer()
{
    final int offset = 24;
    final UnsafeBuffer buffer = new UnsafeBuffer(allocateDirectAligned(128, 8));
    buffer.setMemory(0, offset, (byte)15);
    final TerminateDriverFlyweight flyweight = new TerminateDriverFlyweight();
    flyweight.wrap(buffer, offset);

    flyweight.tokenBuffer(newBuffer(16), 4, 8);

    assertEquals(8, flyweight.tokenBufferLength());
    assertEquals(TOKEN_BUFFER_OFFSET, flyweight.tokenBufferOffset());
    assertEquals(TOKEN_BUFFER_OFFSET + 8, flyweight.length());
}
 
Example 6
Source File: TermAppenderTest.java    From aeron with Apache License 2.0 5 votes vote down vote up
@Test
public void shouldAppendUnfragmentedFromVectorsToEmptyLog()
{
    final int headerLength = DEFAULT_HEADER.capacity();
    final UnsafeBuffer bufferOne = new UnsafeBuffer(new byte[64]);
    final UnsafeBuffer bufferTwo = new UnsafeBuffer(new byte[256]);
    bufferOne.setMemory(0, bufferOne.capacity(), (byte)'1');
    bufferTwo.setMemory(0, bufferTwo.capacity(), (byte)'2');
    final int msgLength = bufferOne.capacity() + 200;
    final int frameLength = msgLength + headerLength;
    final int alignedFrameLength = align(frameLength, FRAME_ALIGNMENT);
    final int tail = 0;

    logMetaDataBuffer.putLong(TERM_TAIL_COUNTER_OFFSET, packTail(TERM_ID, tail));

    final DirectBufferVector[] vectors = new DirectBufferVector[]
    {
        new DirectBufferVector(bufferOne, 0, bufferOne.capacity()),
        new DirectBufferVector(bufferTwo, 0, 200)
    };

    assertEquals(
        alignedFrameLength, termAppender.appendUnfragmentedMessage(headerWriter, vectors, msgLength, RVS, TERM_ID));

    assertEquals(
        packTail(TERM_ID, tail + alignedFrameLength), rawTailVolatile(logMetaDataBuffer, PARTITION_INDEX));

    final InOrder inOrder = inOrder(termBuffer, headerWriter);
    inOrder.verify(headerWriter, times(1)).write(termBuffer, tail, frameLength, TERM_ID);
    inOrder.verify(termBuffer, times(1)).putBytes(headerLength, bufferOne, 0, bufferOne.capacity());
    inOrder.verify(termBuffer, times(1)).putBytes(headerLength + bufferOne.capacity(), bufferTwo, 0, 200);
    inOrder.verify(termBuffer, times(1)).putLong(tail + RESERVED_VALUE_OFFSET, RV, LITTLE_ENDIAN);
    inOrder.verify(termBuffer, times(1)).putIntOrdered(tail, frameLength);
}
 
Example 7
Source File: FragmentAssemblerTest.java    From aeron with Apache License 2.0 5 votes vote down vote up
@Test
public void shouldAssembleFourPartMessage()
{
    when(header.flags())
        .thenReturn(FrameDescriptor.BEGIN_FRAG_FLAG)
        .thenReturn((byte)0)
        .thenReturn((byte)0)
        .thenReturn(FrameDescriptor.END_FRAG_FLAG);

    final UnsafeBuffer srcBuffer = new UnsafeBuffer(new byte[1024]);
    final int offset = 0;
    final int length = srcBuffer.capacity() / 4;

    for (int i = 0; i < 4; i++)
    {
        srcBuffer.setMemory(i * length, length, (byte)(65 + i));
    }

    adapter.onFragment(srcBuffer, offset, length, header);
    adapter.onFragment(srcBuffer, offset + length, length, header);
    adapter.onFragment(srcBuffer, offset + (length * 2), length, header);
    adapter.onFragment(srcBuffer, offset + (length * 3), length, header);

    final ArgumentCaptor<UnsafeBuffer> bufferArg = ArgumentCaptor.forClass(UnsafeBuffer.class);
    final ArgumentCaptor<Header> headerArg = ArgumentCaptor.forClass(Header.class);

    verify(delegateFragmentHandler, times(1)).onFragment(
        bufferArg.capture(), eq(offset), eq(length * 4), headerArg.capture());

    final UnsafeBuffer capturedBuffer = bufferArg.getValue();
    for (int i = 0; i < srcBuffer.capacity(); i++)
    {
        assertEquals(srcBuffer.getByte(i), capturedBuffer.getByte(i), "same at i=" + i);
    }

    final Header capturedHeader = headerArg.getValue();
    assertEquals(SESSION_ID, capturedHeader.sessionId());
    assertEquals(FrameDescriptor.END_FRAG_FLAG, capturedHeader.flags());
}
 
Example 8
Source File: FragmentAssemblerTest.java    From aeron with Apache License 2.0 5 votes vote down vote up
@Test
public void shouldAssembleTwoPartMessage()
{
    when(header.flags())
        .thenReturn(FrameDescriptor.BEGIN_FRAG_FLAG)
        .thenReturn(FrameDescriptor.END_FRAG_FLAG);

    final UnsafeBuffer srcBuffer = new UnsafeBuffer(new byte[1024]);
    final int offset = 0;
    final int length = srcBuffer.capacity() / 2;

    srcBuffer.setMemory(0, length, (byte)65);
    srcBuffer.setMemory(length, length, (byte)66);

    adapter.onFragment(srcBuffer, offset, length, header);
    adapter.onFragment(srcBuffer, length, length, header);

    final ArgumentCaptor<UnsafeBuffer> bufferArg = ArgumentCaptor.forClass(UnsafeBuffer.class);
    final ArgumentCaptor<Header> headerArg = ArgumentCaptor.forClass(Header.class);

    verify(delegateFragmentHandler, times(1)).onFragment(
        bufferArg.capture(), eq(offset), eq(length * 2), headerArg.capture());

    final UnsafeBuffer capturedBuffer = bufferArg.getValue();
    for (int i = 0; i < srcBuffer.capacity(); i++)
    {
        assertEquals(srcBuffer.getByte(i), capturedBuffer.getByte(i), "same at i=" + i);
    }

    final Header capturedHeader = headerArg.getValue();
    assertEquals(SESSION_ID, capturedHeader.sessionId());
    assertEquals(FrameDescriptor.END_FRAG_FLAG, capturedHeader.flags());
}
 
Example 9
Source File: BufferUtilTest.java    From agrona with Apache License 2.0 5 votes vote down vote up
@Test
public void freeShouldReleaseDirectBufferResources()
{
    final UnsafeBuffer buffer = new UnsafeBuffer(ByteBuffer.allocateDirect(4));
    buffer.setMemory(0, 4, (byte)111);

    BufferUtil.free(buffer);
}
 
Example 10
Source File: TwoBufferOfferMessageTest.java    From aeron with Apache License 2.0 5 votes vote down vote up
@Test
@Timeout(10)
public void shouldTransferFragmentedTwoPartMessage()
{
    final UnsafeBuffer expectedBuffer = new UnsafeBuffer(new byte[32 + driver.context().mtuLength()]);
    final UnsafeBuffer bufferOne = new UnsafeBuffer(expectedBuffer, 0, 32);
    final UnsafeBuffer bufferTwo = new UnsafeBuffer(expectedBuffer, 32, expectedBuffer.capacity() - 32);

    bufferOne.setMemory(0, bufferOne.capacity(), (byte)'a');
    bufferTwo.setMemory(0, bufferTwo.capacity(), (byte)'b');
    final String expectedMessage = expectedBuffer.getStringWithoutLengthAscii(0, expectedBuffer.capacity());

    final MutableReference<String> receivedMessage = new MutableReference<>();
    final FragmentHandler fragmentHandler = new FragmentAssembler((buffer, offset, length, header) ->
        receivedMessage.set(buffer.getStringWithoutLengthAscii(offset, length)));

    try (Subscription subscription = aeron.addSubscription(CHANNEL, STREAM_ID))
    {
        try (Publication publication = aeron.addPublication(CHANNEL, STREAM_ID))
        {
            publishMessage(bufferOne, bufferTwo, publication);
            pollForMessage(subscription, receivedMessage, fragmentHandler);

            assertEquals(expectedMessage, receivedMessage.get());
        }

        try (Publication publication = aeron.addExclusivePublication(CHANNEL, STREAM_ID))
        {
            publishMessage(bufferOne, bufferTwo, publication);
            pollForMessage(subscription, receivedMessage, fragmentHandler);

            assertEquals(expectedMessage, receivedMessage.get());
        }
    }
}
 
Example 11
Source File: TwoBufferOfferMessageTest.java    From aeron with Apache License 2.0 5 votes vote down vote up
@Test
@Timeout(10)
public void shouldTransferUnfragmentedTwoPartMessage()
{
    final UnsafeBuffer expectedBuffer = new UnsafeBuffer(new byte[256]);
    final UnsafeBuffer bufferOne = new UnsafeBuffer(expectedBuffer, 0, 32);
    final UnsafeBuffer bufferTwo = new UnsafeBuffer(expectedBuffer, 32, expectedBuffer.capacity() - 32);

    bufferOne.setMemory(0, bufferOne.capacity(), (byte)'a');
    bufferTwo.setMemory(0, bufferTwo.capacity(), (byte)'b');
    final String expectedMessage = expectedBuffer.getStringWithoutLengthAscii(0, expectedBuffer.capacity());

    final MutableReference<String> receivedMessage = new MutableReference<>();
    final FragmentHandler fragmentHandler = (buffer, offset, length, header) ->
        receivedMessage.set(buffer.getStringWithoutLengthAscii(offset, length));

    try (Subscription subscription = aeron.addSubscription(CHANNEL, STREAM_ID))
    {
        try (Publication publication = aeron.addPublication(CHANNEL, STREAM_ID))
        {
            publishMessage(bufferOne, bufferTwo, publication);
            pollForMessage(subscription, receivedMessage, fragmentHandler);

            assertEquals(expectedMessage, receivedMessage.get());
        }

        try (Publication publication = aeron.addExclusivePublication(CHANNEL, STREAM_ID))
        {
            publishMessage(bufferOne, bufferTwo, publication);
            pollForMessage(subscription, receivedMessage, fragmentHandler);

            assertEquals(expectedMessage, receivedMessage.get());
        }
    }
}
 
Example 12
Source File: PublicationImage.java    From aeron with Apache License 2.0 5 votes vote down vote up
private void cleanBufferTo(final long position)
{
    final long cleanPosition = this.cleanPosition;
    if (position > cleanPosition)
    {
        final int bytesForCleaning = (int)(position - cleanPosition);
        final UnsafeBuffer dirtyTerm = termBuffers[indexByPosition(cleanPosition, positionBitsToShift)];
        final int termOffset = (int)cleanPosition & termLengthMask;
        final int length = Math.min(bytesForCleaning, dirtyTerm.capacity() - termOffset);

        dirtyTerm.setMemory(termOffset, length - SIZE_OF_LONG, (byte)0);
        dirtyTerm.putLongOrdered(termOffset + (length - SIZE_OF_LONG), 0);
        this.cleanPosition = cleanPosition + length;
    }
}
 
Example 13
Source File: FragmentedMessageTest.java    From aeron with Apache License 2.0 4 votes vote down vote up
@ParameterizedTest
@MethodSource("channels")
@Timeout(10)
public void shouldReceivePublishedMessage(final String channel)
{
    final FragmentAssembler assembler = new FragmentAssembler(mockFragmentHandler);

    try (Subscription subscription = aeron.addSubscription(channel, STREAM_ID);
        Publication publication = aeron.addPublication(channel, STREAM_ID))
    {
        final UnsafeBuffer srcBuffer = new UnsafeBuffer(new byte[driver.context().mtuLength() * 4]);
        final int offset = 0;
        final int length = srcBuffer.capacity() / 4;

        for (int i = 0; i < 4; i++)
        {
            srcBuffer.setMemory(i * length, length, (byte)(65 + i));
        }

        while (publication.offer(srcBuffer, offset, srcBuffer.capacity()) < 0L)
        {
            Tests.yield();
        }

        final int expectedFragmentsBecauseOfHeader = 5;
        int numFragments = 0;
        do
        {
            final int fragments = subscription.poll(assembler, FRAGMENT_COUNT_LIMIT);
            if (0 == fragments)
            {
                Tests.yield();
            }
            numFragments += fragments;
        }
        while (numFragments < expectedFragmentsBecauseOfHeader);

        final ArgumentCaptor<DirectBuffer> bufferArg = ArgumentCaptor.forClass(DirectBuffer.class);
        final ArgumentCaptor<Header> headerArg = ArgumentCaptor.forClass(Header.class);

        verify(mockFragmentHandler, times(1)).onFragment(
            bufferArg.capture(), eq(offset), eq(srcBuffer.capacity()), headerArg.capture());

        final DirectBuffer capturedBuffer = bufferArg.getValue();
        for (int i = 0; i < srcBuffer.capacity(); i++)
        {
            assertEquals(srcBuffer.getByte(i), capturedBuffer.getByte(i), "same at i=" + i);
        }

        assertEquals(END_FRAG_FLAG, headerArg.getValue().flags());
    }
}
 
Example 14
Source File: ArchiveMarkFile.java    From aeron with Apache License 2.0 4 votes vote down vote up
public ArchiveMarkFile(
    final File file,
    final int totalFileLength,
    final int errorBufferLength,
    final EpochClock epochClock,
    final long timeoutMs)
{
    final boolean markFileExists = file.exists();

    markFile = new MarkFile(
        file,
        file.exists(),
        MarkFileHeaderDecoder.versionEncodingOffset(),
        MarkFileHeaderDecoder.activityTimestampEncodingOffset(),
        totalFileLength,
        timeoutMs,
        epochClock,
        (version) ->
        {
            if (SemanticVersion.major(version) != MAJOR_VERSION)
            {
                throw new IllegalArgumentException("mark file major version " + SemanticVersion.major(version) +
                    " does not match software:" + MAJOR_VERSION);
            }
        },
        null);

    buffer = markFile.buffer();

    errorBuffer = errorBufferLength > 0 ?
        new UnsafeBuffer(buffer, HEADER_LENGTH, errorBufferLength) :
        new UnsafeBuffer(buffer, 0, 0);

    headerEncoder.wrap(buffer, 0);
    headerDecoder.wrap(buffer, 0, MarkFileHeaderDecoder.BLOCK_LENGTH, MarkFileHeaderDecoder.SCHEMA_VERSION);

    if (markFileExists && headerDecoder.errorBufferLength() > 0)
    {
        final UnsafeBuffer existingErrorBuffer = new UnsafeBuffer(
            buffer, headerDecoder.headerLength(), headerDecoder.errorBufferLength());

        saveExistingErrors(file, existingErrorBuffer, System.err);
        existingErrorBuffer.setMemory(0, headerDecoder.errorBufferLength(), (byte)0);
    }

    headerEncoder.pid(SystemUtil.getPid());
}
 
Example 15
Source File: ClusterMarkFile.java    From aeron with Apache License 2.0 4 votes vote down vote up
public ClusterMarkFile(
    final File file,
    final ClusterComponentType type,
    final int errorBufferLength,
    final EpochClock epochClock,
    final long timeoutMs)
{
    final boolean markFileExists = file.exists();

    markFile = new MarkFile(
        file,
        markFileExists,
        MarkFileHeaderDecoder.versionEncodingOffset(),
        MarkFileHeaderDecoder.activityTimestampEncodingOffset(),
        HEADER_LENGTH + errorBufferLength,
        timeoutMs,
        epochClock,
        (version) ->
        {
            if (VERSION_FAILED == version && markFileExists)
            {
                System.err.println("mark file version -1 indicates error on previous startup.");
            }
            else if (SemanticVersion.major(version) != MAJOR_VERSION)
            {
                throw new ClusterException("mark file major version " + SemanticVersion.major(version) +
                    " does not match software:" + MAJOR_VERSION);
            }
        },
        null);

    buffer = markFile.buffer();
    errorBuffer = new UnsafeBuffer(this.buffer, HEADER_LENGTH, errorBufferLength);

    headerEncoder.wrap(buffer, 0);
    headerDecoder.wrap(buffer, 0, MarkFileHeaderDecoder.BLOCK_LENGTH, MarkFileHeaderDecoder.SCHEMA_VERSION);

    if (markFileExists)
    {
        final UnsafeBuffer existingErrorBuffer = new UnsafeBuffer(
            buffer, headerDecoder.headerLength(), headerDecoder.errorBufferLength());

        saveExistingErrors(file, existingErrorBuffer, System.err);
        existingErrorBuffer.setMemory(0, headerDecoder.errorBufferLength(), (byte)0);
    }
    else
    {
        headerEncoder.candidateTermId(NULL_VALUE);
    }

    final ClusterComponentType existingType = headerDecoder.componentType();

    if (existingType != ClusterComponentType.NULL && existingType != type)
    {
        if (existingType != ClusterComponentType.BACKUP || ClusterComponentType.CONSENSUS_MODULE != type)
        {
            throw new ClusterException(
                "existing Mark file type " + existingType + " not same as required type " + type);
        }
    }

    headerEncoder.componentType(type);
    headerEncoder.headerLength(HEADER_LENGTH);
    headerEncoder.errorBufferLength(errorBufferLength);
    headerEncoder.pid(SystemUtil.getPid());
    headerEncoder.startTimestamp(epochClock.time());
}
 
Example 16
Source File: GapFillLossTest.java    From aeron with Apache License 2.0 4 votes vote down vote up
@Test
@Timeout(10)
public void shouldGapFillWhenLossOccurs() throws Exception
{
    final UnsafeBuffer srcBuffer = new UnsafeBuffer(ByteBuffer.allocateDirect(MSG_LENGTH));
    srcBuffer.setMemory(0, MSG_LENGTH, (byte)7);

    final MediaDriver.Context ctx = new MediaDriver.Context()
        .errorHandler(Tests::onError)
        .threadingMode(ThreadingMode.SHARED)
        .dirDeleteOnStart(true)
        .publicationTermBufferLength(LogBufferDescriptor.TERM_MIN_LENGTH);

    final LossGenerator noLossGenerator =
        DebugChannelEndpointConfiguration.lossGeneratorSupplier(0, 0);

    ctx.sendChannelEndpointSupplier((udpChannel, statusIndicator, context) -> new DebugSendChannelEndpoint(
        udpChannel, statusIndicator, context, noLossGenerator, noLossGenerator));

    TestMediaDriver.enableLossGenerationOnReceive(ctx, 0.20, 0xcafebabeL, true, false);

    try (TestMediaDriver ignore = TestMediaDriver.launch(ctx, watcher);
        Aeron aeron = Aeron.connect();
        Subscription subscription = aeron.addSubscription(UNRELIABLE_CHANNEL, STREAM_ID);
        Publication publication = aeron.addPublication(CHANNEL, STREAM_ID))
    {
        final Subscriber subscriber = new Subscriber(subscription);
        final Thread subscriberThread = new Thread(subscriber);
        subscriberThread.setDaemon(true);
        subscriberThread.start();

        long position = 0;
        for (int i = 0; i < NUM_MESSAGES; i++)
        {
            srcBuffer.putLong(0, i);

            while ((position = publication.offer(srcBuffer)) < 0L)
            {
                Tests.yield();
            }
        }

        FINAL_POSITION.set(position);
        subscriberThread.join();

        verifyLossOccurredForStream(ctx.aeronDirectoryName(), STREAM_ID);
        assertThat(subscriber.messageCount, lessThan(NUM_MESSAGES));
    }
    finally
    {
        ctx.deleteDirectory();
    }
}
 
Example 17
Source File: MemoryOrderingTest.java    From aeron with Apache License 2.0 4 votes vote down vote up
@Test
@Timeout(10)
public void shouldReceiveMessagesInOrderWithFirstLongWordIntact() throws Exception
{
    final UnsafeBuffer srcBuffer = new UnsafeBuffer(ByteBuffer.allocate(MESSAGE_LENGTH));
    srcBuffer.setMemory(0, MESSAGE_LENGTH, (byte)7);

    try (Subscription subscription = aeron.addSubscription(CHANNEL, STREAM_ID);
        Publication publication = aeron.addPublication(CHANNEL, STREAM_ID))
    {
        final IdleStrategy idleStrategy = YieldingIdleStrategy.INSTANCE;
        final Thread subscriberThread = new Thread(new Subscriber(subscription));
        subscriberThread.setDaemon(true);
        subscriberThread.start();

        for (int i = 0; i < NUM_MESSAGES; i++)
        {
            if (null != failedMessage)
            {
                fail(failedMessage);
            }

            srcBuffer.putLong(0, i);

            while (publication.offer(srcBuffer) < 0L)
            {
                if (null != failedMessage)
                {
                    fail(failedMessage);
                }

                idleStrategy.idle();
                Tests.checkInterruptStatus();
            }

            if (i % BURST_LENGTH == 0)
            {
                final long timeoutNs = System.nanoTime() + INTER_BURST_DURATION_NS;
                long nowNs;
                do
                {
                    nowNs = System.nanoTime();
                }
                while ((timeoutNs - nowNs) > 0);
            }
        }

        subscriberThread.join();
    }
}
 
Example 18
Source File: TermAppenderTest.java    From aeron with Apache License 2.0 4 votes vote down vote up
@Test
public void shouldAppendFragmentedFromVectorsToEmptyLog()
{
    final int mtu = 2048;
    final int headerLength = DEFAULT_HEADER.capacity();
    final int maxPayloadLength = mtu - headerLength;
    final int bufferOneLength = 64;
    final int bufferTwoLength = 3000;
    final UnsafeBuffer bufferOne = new UnsafeBuffer(new byte[bufferOneLength]);
    final UnsafeBuffer bufferTwo = new UnsafeBuffer(new byte[bufferTwoLength]);
    bufferOne.setMemory(0, bufferOne.capacity(), (byte)'1');
    bufferTwo.setMemory(0, bufferTwo.capacity(), (byte)'2');
    final int msgLength = bufferOneLength + bufferTwoLength;
    int tail = 0;
    final int frameOneLength = mtu;
    final int frameTwoLength = (msgLength - (mtu - headerLength)) + headerLength;
    final int resultingOffset = frameOneLength + BitUtil.align(frameTwoLength, FRAME_ALIGNMENT);

    logMetaDataBuffer.putLong(TERM_TAIL_COUNTER_OFFSET, packTail(TERM_ID, tail));

    final DirectBufferVector[] vectors = new DirectBufferVector[]
    {
        new DirectBufferVector(bufferOne, 0, bufferOneLength),
        new DirectBufferVector(bufferTwo, 0, bufferTwoLength)
    };

    assertEquals(
        resultingOffset,
        termAppender.appendFragmentedMessage(headerWriter, vectors, msgLength, maxPayloadLength, RVS, TERM_ID));

    final InOrder inOrder = inOrder(termBuffer, headerWriter);

    inOrder.verify(headerWriter, times(1)).write(termBuffer, tail, frameOneLength, TERM_ID);
    inOrder.verify(termBuffer, times(1)).putBytes(headerLength, bufferOne, 0, bufferOneLength);
    inOrder.verify(termBuffer, times(1))
        .putBytes(headerLength + bufferOneLength, bufferTwo, 0, maxPayloadLength - bufferOneLength);
    inOrder.verify(termBuffer, times(1)).putLong(tail + RESERVED_VALUE_OFFSET, RV, LITTLE_ENDIAN);
    inOrder.verify(termBuffer, times(1)).putIntOrdered(tail, frameOneLength);

    tail += frameOneLength;
    final int bufferTwoOffset = maxPayloadLength - bufferOneLength;
    final int fragmentTwoPayloadLength = bufferTwoLength - (maxPayloadLength - bufferOneLength);

    inOrder.verify(headerWriter, times(1)).write(termBuffer, tail, frameTwoLength, TERM_ID);
    inOrder.verify(termBuffer, times(1))
        .putBytes(tail + headerLength, bufferTwo, bufferTwoOffset, fragmentTwoPayloadLength);
    inOrder.verify(termBuffer, times(1)).putLong(tail + RESERVED_VALUE_OFFSET, RV, LITTLE_ENDIAN);
    inOrder.verify(termBuffer, times(1)).putIntOrdered(tail, frameTwoLength);
}
 
Example 19
Source File: TermAppenderTest.java    From aeron with Apache License 2.0 4 votes vote down vote up
@Test
public void shouldAppendFragmentedFromVectorsWithNonZeroOffsetToEmptyLog()
{
    final int mtu = 2048;
    final int headerLength = DEFAULT_HEADER.capacity();
    final int maxPayloadLength = mtu - headerLength;
    final int bufferOneLength = 64;
    final int offset = 15;
    final int bufferTwoTotalLength = 3000;
    final int bufferTwoLength = bufferTwoTotalLength - offset;
    final UnsafeBuffer bufferOne = new UnsafeBuffer(new byte[bufferOneLength]);
    final UnsafeBuffer bufferTwo = new UnsafeBuffer(new byte[bufferTwoTotalLength]);
    bufferOne.setMemory(0, bufferOne.capacity(), (byte)'1');
    bufferTwo.setMemory(0, bufferTwo.capacity(), (byte)'2');
    final int msgLength = bufferOneLength + bufferTwoLength;
    int tail = 0;
    final int frameOneLength = mtu;
    final int frameTwoLength = (msgLength - (mtu - headerLength)) + headerLength;
    final int resultingOffset = frameOneLength + BitUtil.align(frameTwoLength, FRAME_ALIGNMENT);

    logMetaDataBuffer.putLong(TERM_TAIL_COUNTER_OFFSET, packTail(TERM_ID, tail));

    final DirectBufferVector[] vectors = new DirectBufferVector[]
    {
        new DirectBufferVector(bufferOne, 0, bufferOneLength),
        new DirectBufferVector(bufferTwo, offset, bufferTwoLength)
    };

    assertEquals(
        resultingOffset,
        termAppender.appendFragmentedMessage(headerWriter, vectors, msgLength, maxPayloadLength, RVS, TERM_ID));

    final InOrder inOrder = inOrder(termBuffer, headerWriter);

    inOrder.verify(headerWriter, times(1)).write(termBuffer, tail, frameOneLength, TERM_ID);
    inOrder.verify(termBuffer, times(1)).putBytes(headerLength, bufferOne, 0, bufferOneLength);
    inOrder.verify(termBuffer, times(1))
        .putBytes(headerLength + bufferOneLength, bufferTwo, offset, maxPayloadLength - bufferOneLength);
    inOrder.verify(termBuffer, times(1)).putLong(tail + RESERVED_VALUE_OFFSET, RV, LITTLE_ENDIAN);
    inOrder.verify(termBuffer, times(1)).putIntOrdered(tail, frameOneLength);

    tail += frameOneLength;
    final int bufferTwoOffset = maxPayloadLength - bufferOneLength + offset;
    final int fragmentTwoPayloadLength = bufferTwoLength - (maxPayloadLength - bufferOneLength);

    inOrder.verify(headerWriter, times(1)).write(termBuffer, tail, frameTwoLength, TERM_ID);
    inOrder.verify(termBuffer, times(1))
        .putBytes(tail + headerLength, bufferTwo, bufferTwoOffset, fragmentTwoPayloadLength);
    inOrder.verify(termBuffer, times(1)).putLong(tail + RESERVED_VALUE_OFFSET, RV, LITTLE_ENDIAN);
    inOrder.verify(termBuffer, times(1)).putIntOrdered(tail, frameTwoLength);
}
 
Example 20
Source File: RecordingWriterTests.java    From aeron with Apache License 2.0 4 votes vote down vote up
@Test
void onBlockShouldComputeCrcUsingTheChecksumBuffer() throws IOException
{
    final Image image = mockImage(0L);
    final Context ctx = new Context().archiveDir(archiveDir);
    final UnsafeBuffer checksumBuffer = new UnsafeBuffer(allocateDirectAligned(512, 64));
    final Checksum checksum = crc32();
    final RecordingWriter recordingWriter = new RecordingWriter(
        1, 0, SEGMENT_LENGTH, image, ctx, null, checksumBuffer, checksum);

    recordingWriter.init();

    final UnsafeBuffer termBuffer = new UnsafeBuffer(allocateDirectAligned(512, 64));
    frameType(termBuffer, 96, HDR_TYPE_DATA);
    frameTermId(termBuffer, 96, 96);
    frameLengthOrdered(termBuffer, 96, 64);
    frameSessionId(termBuffer, 96, 96);
    termBuffer.setMemory(96 + HEADER_LENGTH, 32, (byte)96);
    frameType(termBuffer, 160, HDR_TYPE_DATA);
    frameTermId(termBuffer, 160, 160);
    frameLengthOrdered(termBuffer, 160, 288);
    frameSessionId(termBuffer, 160, 160);
    termBuffer.setMemory(160 + HEADER_LENGTH, 256, (byte)160);

    recordingWriter.onBlock(termBuffer, 96, 352, -1, -1);
    recordingWriter.close();

    final File segmentFile = segmentFile(1, 0);
    assertTrue(segmentFile.exists());
    assertEquals(SEGMENT_LENGTH, segmentFile.length());

    final UnsafeBuffer fileBuffer = new UnsafeBuffer();
    fileBuffer.wrap(readAllBytes(segmentFile.toPath()));
    assertEquals(HDR_TYPE_DATA, frameType(fileBuffer, 0));
    assertEquals(96, frameTermId(fileBuffer, 0));
    assertEquals(64, frameLength(fileBuffer, 0));
    assertEquals(
        checksum.compute(termBuffer.addressOffset(), 96 + HEADER_LENGTH, 32),
        frameSessionId(fileBuffer, 0));
    assertEquals(HDR_TYPE_DATA, frameType(fileBuffer, 64));
    assertEquals(160, frameTermId(fileBuffer, 64));
    assertEquals(288, frameLength(fileBuffer, 64));
    assertEquals(
        checksum.compute(termBuffer.addressOffset(), 160 + HEADER_LENGTH, 256),
        frameSessionId(fileBuffer, 64));
    // Ensure that the source buffer was not modified
    assertEquals(96, frameSessionId(termBuffer, 96));
    assertEquals(160, frameSessionId(termBuffer, 160));
}