io.aeron.protocol.DataHeaderFlyweight Java Examples

The following examples show how to use io.aeron.protocol.DataHeaderFlyweight. 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: FlyweightTest.java    From aeron with Apache License 2.0 6 votes vote down vote up
@Test
public void shouldWriteCorrectValuesForGenericHeaderFields()
{
    encodeHeader.wrap(aBuff);

    encodeHeader.version((short)1);
    encodeHeader.flags(DataHeaderFlyweight.BEGIN_AND_END_FLAGS);
    encodeHeader.headerType(HeaderFlyweight.HDR_TYPE_DATA);
    encodeHeader.frameLength(8);

    // little endian
    assertEquals((byte)0x08, buffer.get(0));
    assertEquals((byte)0x00, buffer.get(1));
    assertEquals((byte)0x00, buffer.get(2));
    assertEquals((byte)0x00, buffer.get(3));
    assertEquals((byte)0x01, buffer.get(4));
    assertEquals((byte)0xC0, buffer.get(5));
    assertEquals(HeaderFlyweight.HDR_TYPE_DATA, buffer.get(6));
    assertEquals((byte)0x00, buffer.get(7));
}
 
Example #2
Source File: RetransmitHandlerTest.java    From aeron with Apache License 2.0 6 votes vote down vote up
private void addReceivedDataFrame(final int msgNum)
{
    dataHeader.wrap(rcvBuffer);

    dataHeader.termId(TERM_ID)
        .streamId(STREAM_ID)
        .sessionId(SESSION_ID)
        .termOffset(offsetOfFrame(msgNum))
        .frameLength(MESSAGE_LENGTH)
        .headerType(HeaderFlyweight.HDR_TYPE_DATA)
        .flags(DataHeaderFlyweight.BEGIN_AND_END_FLAGS)
        .version(HeaderFlyweight.CURRENT_VERSION);

    rcvBuffer.putBytes(dataHeader.dataOffset(), DATA);

    TermRebuilder.insert(termBuffer, offsetOfFrame(msgNum), rcvBuffer, MESSAGE_LENGTH);
}
 
Example #3
Source File: ReceiverTest.java    From aeron with Apache License 2.0 6 votes vote down vote up
private void fillDataFrame(final DataHeaderFlyweight header, final int termOffset, final byte[] payload)
{
    header.wrap(dataBuffer);
    header
        .termOffset(termOffset)
        .termId(ACTIVE_TERM_ID)
        .streamId(STREAM_ID)
        .sessionId(SESSION_ID)
        .frameLength(DataHeaderFlyweight.HEADER_LENGTH + payload.length)
        .headerType(HeaderFlyweight.HDR_TYPE_DATA)
        .flags(DataHeaderFlyweight.BEGIN_AND_END_FLAGS)
        .version(HeaderFlyweight.CURRENT_VERSION);

    if (0 < payload.length)
    {
        dataBuffer.putBytes(header.dataOffset(), payload);
    }
}
 
Example #4
Source File: LossDetectorTest.java    From aeron with Apache License 2.0 6 votes vote down vote up
private void insertDataFrame(final int offset, final byte[] payload)
{
    dataHeader
        .termId(TERM_ID)
        .streamId(STREAM_ID)
        .sessionId(SESSION_ID)
        .termOffset(offset)
        .frameLength(payload.length + DataHeaderFlyweight.HEADER_LENGTH)
        .headerType(HeaderFlyweight.HDR_TYPE_DATA)
        .flags(DataHeaderFlyweight.BEGIN_AND_END_FLAGS)
        .version(HeaderFlyweight.CURRENT_VERSION);

    rcvBuffer.putBytes(dataHeader.dataOffset(), payload);

    TermRebuilder.insert(termBuffer, offset, rcvBuffer, payload.length + DataHeaderFlyweight.HEADER_LENGTH);
}
 
Example #5
Source File: DebugReceiveChannelEndpoint.java    From aeron with Apache License 2.0 6 votes vote down vote up
public int onDataPacket(
    final DataHeaderFlyweight header,
    final UnsafeBuffer buffer,
    final int length,
    final InetSocketAddress srcAddress,
    final int transportIndex)
{
    int result = 0;

    if (!dataLossGenerator.shouldDropFrame(srcAddress, buffer, length))
    {
        result = super.onDataPacket(header, buffer, length, srcAddress, transportIndex);
    }

    return result;
}
 
Example #6
Source File: SegmentInspector.java    From aeron with Apache License 2.0 6 votes vote down vote up
/**
 * Dump the contents of a segment file to a {@link PrintStream}.
 *
 * @param out              for the dumped contents.
 * @param messageDumpLimit for the number of bytes per message fragment to dump.
 * @param buffer           the wraps the segment file.
 */
public static void dumpSegment(final PrintStream out, final int messageDumpLimit, final UnsafeBuffer buffer)
{
    final DataHeaderFlyweight dataHeaderFlyweight = new DataHeaderFlyweight();
    final int length = buffer.capacity();
    int offset = 0;

    while (offset < length)
    {
        dataHeaderFlyweight.wrap(buffer, offset, length - offset);
        out.println(offset + ": " + dataHeaderFlyweight.toString());

        final int frameLength = dataHeaderFlyweight.frameLength();
        if (frameLength < DataHeaderFlyweight.HEADER_LENGTH)
        {
            break;
        }

        final int limit = min(frameLength - HEADER_LENGTH, messageDumpLimit);
        out.println(LogInspector.formatBytes(buffer, offset + HEADER_LENGTH, limit));
        offset += BitUtil.align(frameLength, FrameDescriptor.FRAME_ALIGNMENT);
    }
}
 
Example #7
Source File: TermGapFillerTest.java    From aeron with Apache License 2.0 6 votes vote down vote up
@Test
public void shouldFillGapAtEndOfTerm()
{
    final int gapOffset = termBuffer.capacity() - 64;
    final int gapLength = 64;

    dataFlyweight
        .sessionId(SESSION_ID)
        .termId(TERM_ID)
        .streamId(STREAM_ID)
        .flags(UNFRAGMENTED)
        .frameLength(termBuffer.capacity() - gapOffset);
    dataFlyweight.setMemory(0, gapOffset - DataHeaderFlyweight.HEADER_LENGTH, (byte)'x');

    assertTrue(TermGapFiller.tryFillGap(metaDataBuffer, termBuffer, TERM_ID, gapOffset, gapLength));

    dataFlyweight.wrap(termBuffer, gapOffset, termBuffer.capacity() - gapOffset);
    assertEquals(gapLength, dataFlyweight.frameLength());
    assertEquals(gapOffset, dataFlyweight.termOffset());
    assertEquals(SESSION_ID, dataFlyweight.sessionId());
    assertEquals(TERM_ID, dataFlyweight.termId());
    assertEquals(PADDING_FRAME_TYPE, dataFlyweight.headerType());
    assertEquals(UNFRAGMENTED, (byte)(dataFlyweight.flags()));
}
 
Example #8
Source File: TermGapFillerTest.java    From aeron with Apache License 2.0 6 votes vote down vote up
@Test
public void shouldFillGapAfterExistingFrame()
{
    final int gapOffset = 128;
    final int gapLength = 64;

    dataFlyweight
        .sessionId(SESSION_ID)
        .termId(TERM_ID)
        .streamId(STREAM_ID)
        .flags(UNFRAGMENTED)
        .frameLength(gapOffset);
    dataFlyweight.setMemory(0, gapOffset - DataHeaderFlyweight.HEADER_LENGTH, (byte)'x');

    assertTrue(TermGapFiller.tryFillGap(metaDataBuffer, termBuffer, TERM_ID, gapOffset, gapLength));

    dataFlyweight.wrap(termBuffer, gapOffset, termBuffer.capacity() - gapOffset);
    assertEquals(gapLength, dataFlyweight.frameLength());
    assertEquals(gapOffset, dataFlyweight.termOffset());
    assertEquals(SESSION_ID, dataFlyweight.sessionId());
    assertEquals(TERM_ID, dataFlyweight.termId());
    assertEquals(PADDING_FRAME_TYPE, dataFlyweight.headerType());
    assertEquals(UNFRAGMENTED, (byte)(dataFlyweight.flags()));
}
 
Example #9
Source File: ImageTest.java    From aeron with Apache License 2.0 6 votes vote down vote up
private void insertDataFrame(final int activeTermId, final int termOffset)
{
    dataHeader
        .termId(INITIAL_TERM_ID)
        .streamId(STREAM_ID)
        .sessionId(SESSION_ID)
        .termOffset(termOffset)
        .frameLength(DATA.length + HEADER_LENGTH)
        .headerType(HeaderFlyweight.HDR_TYPE_DATA)
        .flags(DataHeaderFlyweight.BEGIN_AND_END_FLAGS)
        .version(HeaderFlyweight.CURRENT_VERSION);

    rcvBuffer.putBytes(dataHeader.dataOffset(), DATA);

    final int activeIndex = indexByTerm(INITIAL_TERM_ID, activeTermId);
    TermRebuilder.insert(termBuffers[activeIndex], termOffset, rcvBuffer, ALIGNED_FRAME_LENGTH);
}
 
Example #10
Source File: FlyweightTest.java    From aeron with Apache License 2.0 6 votes vote down vote up
@Test
public void shouldReadAndWriteDataHeaderCorrectly()
{
    encodeDataHeader.wrap(aBuff);

    encodeDataHeader.version((short)1);
    encodeDataHeader.flags(DataHeaderFlyweight.BEGIN_AND_END_FLAGS);
    encodeDataHeader.headerType(HeaderFlyweight.HDR_TYPE_DATA);
    encodeDataHeader.frameLength(DataHeaderFlyweight.HEADER_LENGTH);
    encodeDataHeader.sessionId(0xdeadbeef);
    encodeDataHeader.streamId(0x44332211);
    encodeDataHeader.termId(0x99887766);

    decodeDataHeader.wrap(aBuff);
    assertEquals(1, decodeDataHeader.version());
    assertEquals(DataHeaderFlyweight.BEGIN_AND_END_FLAGS, decodeDataHeader.flags());
    assertEquals(HeaderFlyweight.HDR_TYPE_DATA, decodeDataHeader.headerType());
    assertEquals(DataHeaderFlyweight.HEADER_LENGTH, decodeDataHeader.frameLength());
    assertEquals(0xdeadbeef, decodeDataHeader.sessionId());
    assertEquals(0x44332211, decodeDataHeader.streamId());
    assertEquals(0x99887766, decodeDataHeader.termId());
    assertEquals(DataHeaderFlyweight.HEADER_LENGTH, decodeDataHeader.dataOffset());
}
 
Example #11
Source File: ArchiveToolTests.java    From aeron with Apache License 2.0 6 votes vote down vote up
private void writeToSegmentFile(
    final File file, final int fileLength, final SegmentWriter segmentWriter) throws IOException
{
    final ByteBuffer byteBuffer = allocate(MTU_LENGTH);
    final DataHeaderFlyweight dataHeaderFlyweight = new DataHeaderFlyweight(byteBuffer);
    try (FileChannel channel = FileChannel.open(file.toPath(), READ, WRITE))
    {
        segmentWriter.write(byteBuffer, dataHeaderFlyweight, channel);
        final long size = channel.size();
        if (fileLength != size)
        {
            channel.truncate(fileLength);
            if (size < fileLength)
            {
                byteBuffer.put(0, (byte)0).limit(1).position(0);
                channel.write(byteBuffer, fileLength - 1);
            }
        }
    }
}
 
Example #12
Source File: ArchiveTool.java    From aeron with Apache License 2.0 6 votes vote down vote up
private static CatalogEntryProcessor createVerifyEntryProcessor(
    final PrintStream out,
    final File archiveDir,
    final Set<VerifyOption> options,
    final Checksum checksum,
    final EpochClock epochClock,
    final MutableInteger errorCount,
    final ActionConfirmation<File> truncateFileOnPageStraddle)
{
    final ByteBuffer buffer = BufferUtil.allocateDirectAligned(FILE_IO_MAX_LENGTH_DEFAULT, CACHE_LINE_LENGTH);
    buffer.order(LITTLE_ENDIAN);
    final DataHeaderFlyweight headerFlyweight = new DataHeaderFlyweight(buffer);

    return (headerEncoder, headerDecoder, descriptorEncoder, descriptorDecoder) -> verifyRecording(
        out,
        archiveDir,
        options,
        checksum,
        epochClock,
        errorCount,
        truncateFileOnPageStraddle,
        headerFlyweight,
        headerEncoder,
        descriptorEncoder,
        descriptorDecoder);
}
 
Example #13
Source File: CatalogTest.java    From aeron with Apache License 2.0 5 votes vote down vote up
@Test
void shouldThrowExceptionAfterFailureOnPageStraddle() throws Exception
{
    final long newRecordingId = newRecording();
    final File segmentFile = new File(archiveDir, segmentFileName(newRecordingId, 0));
    try (FileChannel log = FileChannel.open(segmentFile.toPath(), READ, WRITE, CREATE))
    {
        final ByteBuffer bb = allocate(HEADER_LENGTH);
        final DataHeaderFlyweight flyweight = new DataHeaderFlyweight(bb);
        flyweight.frameLength(PAGE_SIZE - 128);
        log.write(bb);

        bb.clear();
        flyweight.frameLength(256);
        log.write(bb, PAGE_SIZE - 128);

        bb.clear();
        bb.put(0, (byte)0).limit(1).position(0);
        log.write(bb, PAGE_SIZE + 127);
    }

    final ArchiveException exception = assertThrows(
        ArchiveException.class,
        () ->
        {
            final Catalog catalog = new Catalog(archiveDir, null, 0, MAX_ENTRIES, clock, null, null);
            catalog.close();
        });
    assertThat(exception.getMessage(), containsString(segmentFile.getAbsolutePath()));
}
 
Example #14
Source File: SequenceNumberIndexWriter.java    From artio with Apache License 2.0 5 votes vote down vote up
private void checkTermRoll(final DirectBuffer buffer, final int offset, final long endPosition, final int length)
{
    final long termBufferLength = buffer.capacity();
    if (nextRollPosition == UNINITIALISED)
    {
        final long startPosition = endPosition - (length + DataHeaderFlyweight.HEADER_LENGTH);
        nextRollPosition = startPosition + termBufferLength - offset;
    }
    else if (endPosition > nextRollPosition)
    {
        nextRollPosition += termBufferLength;
        updateFile();
    }
}
 
Example #15
Source File: ReplayerTest.java    From artio with Apache License 2.0 5 votes vote down vote up
@Before
public void setUp()
{
    when(fragmentHeader.flags()).thenReturn((byte)DataHeaderFlyweight.BEGIN_AND_END_FLAGS);
    when(clock.time()).thenReturn(DATE_TIME_EPOCH_MS);
    when(publication.tryClaim(anyInt(), any())).thenReturn(1L);
    when(publication.maxPayloadLength()).thenReturn(Configuration.mtuLength() - DataHeaderFlyweight.HEADER_LENGTH);

    when(replayQuery.query(anyLong(), anyInt(), anyInt(), anyInt(), anyInt(), any(), messageTracker.capture()))
        .thenReturn(replayOperation);
    when(replayOperation.attemptReplay()).thenReturn(true);
    when(senderSequenceNumbers.bytesInBufferCounter(anyLong())).thenReturn(bytesInBufferCounter);

    setReplayedMessages(1);

    replayer = new Replayer(
        replayQuery,
        publication,
        claim,
        idleStrategy,
        errorHandler,
        MAX_CLAIM_ATTEMPTS,
        subscription,
        DEFAULT_NAME_PREFIX,
        clock,
        EngineConfiguration.DEFAULT_GAPFILL_ON_REPLAY_MESSAGE_TYPES,
        replayHandler,
        senderSequenceNumbers,
        new FakeFixSessionCodecsFactory(),
        DEFAULT_SENDER_MAX_BYTES_IN_BUFFER,
        mock(ReplayerCommandQueue.class),
        EpochFractionFormat.MILLISECONDS,
        currentReplayCounter,
        DEFAULT_MAX_CONCURRENT_SESSION_REPLAYS);
}
 
Example #16
Source File: SenderTest.java    From aeron with Apache License 2.0 5 votes vote down vote up
@Test
public void shouldNotBeAbleToSendAfterUsingUpYourWindow()
{
    final UnsafeBuffer buffer = new UnsafeBuffer(ByteBuffer.allocateDirect(PAYLOAD.length));
    buffer.putBytes(0, PAYLOAD);
    termAppenders[0].appendUnfragmentedMessage(headerWriter, buffer, 0, PAYLOAD.length, null, INITIAL_TERM_ID);

    final StatusMessageFlyweight msg = mock(StatusMessageFlyweight.class);
    when(msg.consumptionTermId()).thenReturn(INITIAL_TERM_ID);
    when(msg.consumptionTermOffset()).thenReturn(0);
    when(msg.receiverWindowLength()).thenReturn(ALIGNED_FRAME_LENGTH);

    publication.onStatusMessage(msg, rcvAddress);

    sender.doWork();

    assertThat(receivedFrames.size(), is(2));
    receivedFrames.remove();                   // skip setup

    dataHeader.wrap(new UnsafeBuffer(receivedFrames.remove()));
    assertThat(dataHeader.frameLength(), is(FRAME_LENGTH));
    assertThat(dataHeader.termId(), is(INITIAL_TERM_ID));
    assertThat(dataHeader.streamId(), is(STREAM_ID));
    assertThat(dataHeader.sessionId(), is(SESSION_ID));
    assertThat(dataHeader.termOffset(), is(offsetOfMessage(1)));
    assertThat(dataHeader.headerType(), is(HeaderFlyweight.HDR_TYPE_DATA));
    assertThat(dataHeader.flags(), is(DataHeaderFlyweight.BEGIN_AND_END_FLAGS));
    assertThat(dataHeader.version(), is((short)HeaderFlyweight.CURRENT_VERSION));

    termAppenders[0].appendUnfragmentedMessage(headerWriter, buffer, 0, PAYLOAD.length, null, INITIAL_TERM_ID);
    sender.doWork();

    assertThat(receivedFrames.size(), is(0));
}
 
Example #17
Source File: SenderTest.java    From aeron with Apache License 2.0 5 votes vote down vote up
@Test
public void shouldNotSendUntilStatusMessageReceived()
{
    final UnsafeBuffer buffer = new UnsafeBuffer(ByteBuffer.allocateDirect(PAYLOAD.length));
    buffer.putBytes(0, PAYLOAD);
    termAppenders[0].appendUnfragmentedMessage(headerWriter, buffer, 0, PAYLOAD.length, null, INITIAL_TERM_ID);

    sender.doWork();
    assertThat(receivedFrames.size(), is(1));
    setupHeader.wrap(receivedFrames.remove());
    assertThat(setupHeader.headerType(), is(HeaderFlyweight.HDR_TYPE_SETUP));

    final StatusMessageFlyweight msg = mock(StatusMessageFlyweight.class);
    when(msg.consumptionTermId()).thenReturn(INITIAL_TERM_ID);
    when(msg.consumptionTermOffset()).thenReturn(0);
    when(msg.receiverWindowLength()).thenReturn(ALIGNED_FRAME_LENGTH);

    publication.onStatusMessage(msg, rcvAddress);
    sender.doWork();

    assertThat(receivedFrames.size(), is(1));

    dataHeader.wrap(new UnsafeBuffer(receivedFrames.remove()));

    assertThat(dataHeader.frameLength(), is(FRAME_LENGTH));
    assertThat(dataHeader.termId(), is(INITIAL_TERM_ID));
    assertThat(dataHeader.streamId(), is(STREAM_ID));
    assertThat(dataHeader.sessionId(), is(SESSION_ID));
    assertThat(dataHeader.termOffset(), is(offsetOfMessage(1)));
    assertThat(dataHeader.headerType(), is(HeaderFlyweight.HDR_TYPE_DATA));
    assertThat(dataHeader.flags(), is(DataHeaderFlyweight.BEGIN_AND_END_FLAGS));
    assertThat(dataHeader.version(), is((short)HeaderFlyweight.CURRENT_VERSION));
}
 
Example #18
Source File: SenderTest.java    From aeron with Apache License 2.0 5 votes vote down vote up
@Test
public void shouldBeAbleToSendOnChannel()
{
    final StatusMessageFlyweight msg = mock(StatusMessageFlyweight.class);
    when(msg.consumptionTermId()).thenReturn(INITIAL_TERM_ID);
    when(msg.consumptionTermOffset()).thenReturn(0);
    when(msg.receiverWindowLength()).thenReturn(ALIGNED_FRAME_LENGTH);

    publication.onStatusMessage(msg, rcvAddress);

    final UnsafeBuffer buffer = new UnsafeBuffer(ByteBuffer.allocateDirect(PAYLOAD.length));
    buffer.putBytes(0, PAYLOAD);

    termAppenders[0].appendUnfragmentedMessage(headerWriter, buffer, 0, PAYLOAD.length, null, INITIAL_TERM_ID);
    sender.doWork();

    assertThat(receivedFrames.size(), is(2));
    setupHeader.wrap(new UnsafeBuffer(receivedFrames.remove()));
    assertThat(setupHeader.headerType(), is(HeaderFlyweight.HDR_TYPE_SETUP));

    dataHeader.wrap(new UnsafeBuffer(receivedFrames.remove()));
    assertThat(dataHeader.frameLength(), is(FRAME_LENGTH));
    assertThat(dataHeader.termId(), is(INITIAL_TERM_ID));
    assertThat(dataHeader.streamId(), is(STREAM_ID));
    assertThat(dataHeader.sessionId(), is(SESSION_ID));
    assertThat(dataHeader.termOffset(), is(offsetOfMessage(1)));
    assertThat(dataHeader.headerType(), is(HeaderFlyweight.HDR_TYPE_DATA));
    assertThat(dataHeader.flags(), is(DataHeaderFlyweight.BEGIN_AND_END_FLAGS));
    assertThat(dataHeader.version(), is((short)HeaderFlyweight.CURRENT_VERSION));
}
 
Example #19
Source File: DataPacketDispatcher.java    From aeron with Apache License 2.0 5 votes vote down vote up
public int onDataPacket(
    final ReceiveChannelEndpoint channelEndpoint,
    final DataHeaderFlyweight header,
    final UnsafeBuffer buffer,
    final int length,
    final InetSocketAddress srcAddress,
    final int transportIndex)
{
    final int streamId = header.streamId();
    final StreamInterest streamInterest = streamInterestByIdMap.get(streamId);

    if (null != streamInterest)
    {
        final int sessionId = header.sessionId();
        final SessionInterest sessionInterest = streamInterest.sessionInterestByIdMap.get(sessionId);

        if (null != sessionInterest)
        {
            if (null != sessionInterest.image)
            {
                return sessionInterest.image.insertPacket(
                    header.termId(), header.termOffset(), buffer, length, transportIndex, srcAddress);
            }
        }
        else if (!DataHeaderFlyweight.isEndOfStream(buffer))
        {
            if (streamInterest.isAllSessions || streamInterest.subscribedSessionIds.contains(sessionId))
            {
                streamInterest.sessionInterestByIdMap.put(sessionId, new SessionInterest(PENDING_SETUP_FRAME));
                elicitSetupMessageFromSource(channelEndpoint, transportIndex, srcAddress, streamId, sessionId);
            }
            else
            {
                streamInterest.sessionInterestByIdMap.put(sessionId, new SessionInterest(NO_INTEREST));
            }
        }
    }

    return 0;
}
 
Example #20
Source File: NetworkPublication.java    From aeron with Apache License 2.0 5 votes vote down vote up
private int heartbeatMessageCheck(
    final long nowNs, final int activeTermId, final int termOffset, final boolean signalEos)
{
    int bytesSent = 0;

    if ((timeOfLastSendOrHeartbeatNs + PUBLICATION_HEARTBEAT_TIMEOUT_NS) - nowNs < 0)
    {
        heartbeatBuffer.clear();
        heartbeatDataHeader
            .sessionId(sessionId)
            .streamId(streamId)
            .termId(activeTermId)
            .termOffset(termOffset)
            .flags((byte)(signalEos ? BEGIN_END_AND_EOS_FLAGS : BEGIN_AND_END_FLAGS));

        bytesSent = channelEndpoint.send(heartbeatBuffer);
        if (DataHeaderFlyweight.HEADER_LENGTH != bytesSent)
        {
            shortSends.increment();
        }

        timeOfLastSendOrHeartbeatNs = nowNs;
        heartbeatsSent.incrementOrdered();
    }

    return bytesSent;
}
 
Example #21
Source File: NetworkPublicationThreadLocals.java    From aeron with Apache License 2.0 5 votes vote down vote up
NetworkPublicationThreadLocals()
{
    final ByteBuffer byteBuffer = BufferUtil.allocateDirectAligned(CACHE_LINE_LENGTH * 3, CACHE_LINE_LENGTH);

    byteBuffer.limit(DataHeaderFlyweight.HEADER_LENGTH);
    heartbeatBuffer = byteBuffer.slice();
    dataHeader = new DataHeaderFlyweight(heartbeatBuffer);

    int position = CACHE_LINE_LENGTH;
    byteBuffer.limit(position + SetupFlyweight.HEADER_LENGTH).position(position);
    setupBuffer = byteBuffer.slice();
    setupHeader = new SetupFlyweight(setupBuffer);

    position += CACHE_LINE_LENGTH;
    byteBuffer.limit(position + RttMeasurementFlyweight.HEADER_LENGTH).position(position);
    rttMeasurementBuffer = byteBuffer.slice();
    rttMeasurementHeader = new RttMeasurementFlyweight(rttMeasurementBuffer);

    dataHeader
        .version(HeaderFlyweight.CURRENT_VERSION)
        .flags((byte)DataHeaderFlyweight.BEGIN_AND_END_FLAGS)
        .headerType(HeaderFlyweight.HDR_TYPE_DATA)
        .frameLength(0);

    setupHeader
        .version(HeaderFlyweight.CURRENT_VERSION)
        .headerType(HeaderFlyweight.HDR_TYPE_SETUP)
        .frameLength(SetupFlyweight.HEADER_LENGTH);

    rttMeasurementHeader
        .version(HeaderFlyweight.CURRENT_VERSION)
        .headerType(HeaderFlyweight.HDR_TYPE_RTTM)
        .frameLength(RttMeasurementFlyweight.HEADER_LENGTH);
}
 
Example #22
Source File: Configuration.java    From aeron with Apache License 2.0 5 votes vote down vote up
/**
 * Validate that the MTU is an appropriate length. MTU lengths must be a multiple of
 * {@link FrameDescriptor#FRAME_ALIGNMENT}.
 *
 * @param mtuLength to be validated.
 * @throws ConfigurationException if the MTU length is not valid.
 */
public static void validateMtuLength(final int mtuLength)
{
    if (mtuLength <= DataHeaderFlyweight.HEADER_LENGTH || mtuLength > MAX_UDP_PAYLOAD_LENGTH)
    {
        throw new ConfigurationException(
            "mtuLength must be a > HEADER_LENGTH and <= MAX_UDP_PAYLOAD_LENGTH: " + mtuLength);
    }

    if ((mtuLength & (FrameDescriptor.FRAME_ALIGNMENT - 1)) != 0)
    {
        throw new ConfigurationException("mtuLength must be a multiple of FRAME_ALIGNMENT: " + mtuLength);
    }
}
 
Example #23
Source File: TermGapFillerTest.java    From aeron with Apache License 2.0 5 votes vote down vote up
@Test
public void shouldFillGapBetweenExistingFrames()
{
    final int gapOffset = 128;
    final int gapLength = 64;

    dataFlyweight
        .sessionId(SESSION_ID)
        .termId(TERM_ID)
        .termOffset(0)
        .streamId(STREAM_ID)
        .flags(UNFRAGMENTED)
        .frameLength(gapOffset)
        .setMemory(0, gapOffset - DataHeaderFlyweight.HEADER_LENGTH, (byte)'x');

    final int secondExistingFrameOffset = gapOffset + gapLength;
    dataFlyweight
        .wrap(termBuffer, secondExistingFrameOffset, termBuffer.capacity() - secondExistingFrameOffset);
    dataFlyweight
        .sessionId(SESSION_ID)
        .termId(TERM_ID)
        .termOffset(secondExistingFrameOffset)
        .streamId(STREAM_ID)
        .flags(UNFRAGMENTED)
        .frameLength(64);

    assertTrue(TermGapFiller.tryFillGap(metaDataBuffer, termBuffer, TERM_ID, gapOffset, gapLength));

    dataFlyweight.wrap(termBuffer, gapOffset, termBuffer.capacity() - gapOffset);
    assertEquals(gapLength, dataFlyweight.frameLength());
    assertEquals(gapOffset, dataFlyweight.termOffset());
    assertEquals(SESSION_ID, dataFlyweight.sessionId());
    assertEquals(TERM_ID, dataFlyweight.termId());
    assertEquals(PADDING_FRAME_TYPE, dataFlyweight.headerType());
    assertEquals(UNFRAGMENTED, (byte)(dataFlyweight.flags()));
}
 
Example #24
Source File: ImageTest.java    From aeron with Apache License 2.0 5 votes vote down vote up
private void insertPaddingFrame(final int activeTermId, final int termOffset)
{
    dataHeader
        .termId(INITIAL_TERM_ID)
        .streamId(STREAM_ID)
        .sessionId(SESSION_ID)
        .frameLength(TERM_BUFFER_LENGTH - termOffset)
        .headerType(HeaderFlyweight.HDR_TYPE_PAD)
        .flags(DataHeaderFlyweight.BEGIN_AND_END_FLAGS)
        .version(HeaderFlyweight.CURRENT_VERSION);

    final int activeIndex = indexByTerm(INITIAL_TERM_ID, activeTermId);
    TermRebuilder.insert(termBuffers[activeIndex], termOffset, rcvBuffer, TERM_BUFFER_LENGTH - termOffset);
}
 
Example #25
Source File: SenderEndPoint.java    From artio with Apache License 2.0 5 votes vote down vote up
private Action blockPosition(final long messagePosition, final int messageLength, final StreamTracker tracker)
{
    final int frameLength = DataHeaderFlyweight.HEADER_LENGTH + messageLength + HEADER_LENGTH;
    final int alignedLength = ArchiveDescriptor.alignTerm(frameLength);
    final long messageStartPosition = messagePosition - alignedLength;
    tracker.blockablePosition.blockPosition(messageStartPosition);
    tracker.skipPosition = messagePosition;
    return Action.CONTINUE;
}
 
Example #26
Source File: LogPublisher.java    From aeron with Apache License 2.0 5 votes vote down vote up
boolean appendClusterAction(final long leadershipTermId, final long timestamp, final ClusterAction action)
{
    final int length = MessageHeaderEncoder.ENCODED_LENGTH + ClusterActionRequestEncoder.BLOCK_LENGTH;
    final int fragmentLength = DataHeaderFlyweight.HEADER_LENGTH +
        MessageHeaderEncoder.ENCODED_LENGTH +
        ClusterActionRequestEncoder.BLOCK_LENGTH;

    int attempts = SEND_ATTEMPTS;
    do
    {
        final long logPosition = publication.position() + BitUtil.align(fragmentLength, FRAME_ALIGNMENT);
        final long result = publication.tryClaim(length, bufferClaim);

        if (result > 0)
        {
            clusterActionRequestEncoder.wrapAndApplyHeader(
                bufferClaim.buffer(), bufferClaim.offset(), messageHeaderEncoder)
                .leadershipTermId(leadershipTermId)
                .logPosition(logPosition)
                .timestamp(timestamp)
                .action(action);

            bufferClaim.commit();
            return true;
        }

        checkResult(result);
    }
    while (--attempts > 0);

    return false;
}
 
Example #27
Source File: CatalogTest.java    From aeron with Apache License 2.0 5 votes vote down vote up
@Test
void shouldUseChecksumToVerifyLastFragmentAfterPageStraddle() throws Exception
{
    final long newRecordingId = newRecording();
    final File segmentFile = new File(archiveDir, segmentFileName(newRecordingId, 0));
    try (FileChannel log = FileChannel.open(segmentFile.toPath(), READ, WRITE, CREATE))
    {
        final ByteBuffer bb = allocate(HEADER_LENGTH);
        final DataHeaderFlyweight flyweight = new DataHeaderFlyweight(bb);
        flyweight.frameLength(PAGE_SIZE - 128);
        log.write(bb);

        bb.clear();
        flyweight.frameLength(256);
        flyweight.sessionId(1025596259);
        log.write(bb, PAGE_SIZE - 128);

        bb.clear();
        bb.put(0, (byte)0).limit(1).position(0);
        log.write(bb, PAGE_SIZE + 127);
    }

    currentTimeMs = 42L;

    try (Catalog catalog = new Catalog(archiveDir, null, 0, MAX_ENTRIES, clock, crc32(), null))
    {
        assertTrue(catalog.forEntry(
            newRecordingId,
            (headerEncoder, headerDecoder, descriptorEncoder, descriptorDecoder) ->
            {
                assertEquals(42L, descriptorDecoder.stopTimestamp());
                assertEquals(PAGE_SIZE + 128, descriptorDecoder.stopPosition());
            }));
    }
}
 
Example #28
Source File: CatalogTest.java    From aeron with Apache License 2.0 5 votes vote down vote up
@Test
void shouldNotThrowWhenOldRecordingLogsAreDeleted() throws IOException
{
    final File segmentFile = new File(archiveDir, segmentFileName(recordingThreeId, SEGMENT_LENGTH * 2));
    try (FileChannel log = FileChannel.open(segmentFile.toPath(), READ, WRITE, CREATE))
    {
        final ByteBuffer bb = allocate(HEADER_LENGTH);
        final DataHeaderFlyweight flyweight = new DataHeaderFlyweight(bb);
        flyweight.frameLength(256);
        log.write(bb);
    }

    final Catalog catalog = new Catalog(archiveDir, null, 0, MAX_ENTRIES, clock, null, null);
    catalog.close();
}
 
Example #29
Source File: MaxPositionPublicationTest.java    From aeron with Apache License 2.0 5 votes vote down vote up
@Test
@Timeout(10)
public void shouldPublishFromExclusivePublication()
{
    final int initialTermId = -777;
    final int termLength = 64 * 1024;
    final long maxPosition = termLength * (Integer.MAX_VALUE + 1L);
    final long lastMessagePosition = maxPosition - (MESSAGE_LENGTH + DataHeaderFlyweight.HEADER_LENGTH);

    final String channelUri = new ChannelUriStringBuilder()
        .initialPosition(lastMessagePosition, initialTermId, termLength)
        .media("ipc")
        .validate()
        .build();

    try (Subscription subscription = aeron.addSubscription(channelUri, STREAM_ID);
        ExclusivePublication publication = aeron.addExclusivePublication(channelUri, STREAM_ID))
    {
        Tests.awaitConnected(subscription);

        assertEquals(lastMessagePosition, publication.position());
        assertEquals(lastMessagePosition, subscription.imageAtIndex(0).joinPosition());

        long resultingPosition = publication.offer(srcBuffer, 0, MESSAGE_LENGTH);
        while (resultingPosition < 0)
        {
            Tests.yield();
            resultingPosition = publication.offer(srcBuffer, 0, MESSAGE_LENGTH);
        }

        assertEquals(maxPosition, publication.maxPossiblePosition());
        assertEquals(publication.maxPossiblePosition(), resultingPosition);
        assertEquals(MAX_POSITION_EXCEEDED, publication.offer(srcBuffer, 0, MESSAGE_LENGTH));
        assertEquals(MAX_POSITION_EXCEEDED, publication.offer(srcBuffer, 0, MESSAGE_LENGTH));
    }
}
 
Example #30
Source File: RecordingSessionTest.java    From aeron with Apache License 2.0 5 votes vote down vote up
@BeforeEach
public void before() throws Exception
{
    when(mockPosition.getWeak()).then((invocation) -> positionLong);
    when(mockPosition.get()).then((invocation) -> positionLong);
    doAnswer(
        (invocation) ->
        {
            positionLong = invocation.getArgument(0);
            return null;
        })
        .when(mockPosition).setOrdered(anyLong());

    termFile = File.createTempFile("test.rec", "sourceIdentity");

    mockLogBufferChannel = FileChannel.open(termFile.toPath(), CREATE, READ, WRITE);
    mockLogBufferMapped = new UnsafeBuffer(
        mockLogBufferChannel.map(FileChannel.MapMode.READ_WRITE, 0, TERM_BUFFER_LENGTH));

    final DataHeaderFlyweight headerFlyweight = new DataHeaderFlyweight();
    headerFlyweight.wrap(mockLogBufferMapped, TERM_OFFSET, DataHeaderFlyweight.HEADER_LENGTH);
    headerFlyweight
        .termOffset(TERM_OFFSET)
        .sessionId(SESSION_ID)
        .streamId(STREAM_ID)
        .headerType(DataHeaderFlyweight.HDR_TYPE_DATA)
        .frameLength(RECORDED_BLOCK_LENGTH);

    context = new Archive.Context()
        .segmentFileLength(SEGMENT_LENGTH)
        .archiveDir(archiveDir)
        .epochClock(epochClock);
}