io.aeron.protocol.HeaderFlyweight Java Examples
The following examples show how to use
io.aeron.protocol.HeaderFlyweight.
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: CTestMediaDriver.java From aeron with Apache License 2.0 | 6 votes |
public static void enableLossGenerationOnReceive( final MediaDriver.Context context, final double rate, final long seed, final boolean loseDataMessages, final boolean loseControlMessages) { int receiveMessageTypeMask = 0; receiveMessageTypeMask |= loseDataMessages ? 1 << HeaderFlyweight.HDR_TYPE_DATA : 0; receiveMessageTypeMask |= loseControlMessages ? 1 << HeaderFlyweight.HDR_TYPE_SM : 0; receiveMessageTypeMask |= loseControlMessages ? 1 << HeaderFlyweight.HDR_TYPE_NAK : 0; receiveMessageTypeMask |= loseControlMessages ? 1 << HeaderFlyweight.HDR_TYPE_RTTM : 0; final Object2ObjectHashMap<String, String> lossTransportEnv = new Object2ObjectHashMap<>(); final String interceptor = "loss"; final String lossArgs = "rate=" + rate + "|seed=" + seed + "|recv-msg-mask=0x" + Integer.toHexString(receiveMessageTypeMask); lossTransportEnv.put("AERON_UDP_CHANNEL_INCOMING_INTERCEPTORS", interceptor); lossTransportEnv.put("AERON_UDP_CHANNEL_TRANSPORT_BINDINGS_LOSS_ARGS", lossArgs); // This is a bit of an ugly hack to decorate the MediaDriver.Context with additional information. C_DRIVER_ADDITIONAL_ENV_VARS.get().put(context, lossTransportEnv); }
Example #2
Source File: FlyweightTest.java From aeron with Apache License 2.0 | 6 votes |
@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 #3
Source File: RetransmitHandlerTest.java From aeron with Apache License 2.0 | 6 votes |
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 #4
Source File: FlyweightTest.java From aeron with Apache License 2.0 | 6 votes |
@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 #5
Source File: FlyweightTest.java From aeron with Apache License 2.0 | 6 votes |
@Test public void shouldEncodeAndDecodeNakCorrectly() { encodeNakHeader.wrap(aBuff); encodeNakHeader.version((short)1); encodeNakHeader.flags((byte)0); encodeNakHeader.headerType(HeaderFlyweight.HDR_TYPE_NAK); encodeNakHeader.frameLength(NakFlyweight.HEADER_LENGTH); encodeNakHeader.sessionId(0xdeadbeef); encodeNakHeader.streamId(0x44332211); encodeNakHeader.termId(0x99887766); encodeNakHeader.termOffset(0x22334); encodeNakHeader.length(512); decodeNakHeader.wrap(aBuff); assertEquals(1, decodeNakHeader.version()); assertEquals(0, decodeNakHeader.flags()); assertEquals(HeaderFlyweight.HDR_TYPE_NAK, decodeNakHeader.headerType()); assertEquals(NakFlyweight.HEADER_LENGTH, decodeNakHeader.frameLength()); assertEquals(0xdeadbeef, decodeNakHeader.sessionId()); assertEquals(0x44332211, decodeNakHeader.streamId()); assertEquals(0x99887766, decodeNakHeader.termId()); assertEquals(0x22334, decodeNakHeader.termOffset()); assertEquals(512, decodeNakHeader.length()); }
Example #6
Source File: ImageTest.java From aeron with Apache License 2.0 | 6 votes |
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 #7
Source File: ReceiverTest.java From aeron with Apache License 2.0 | 6 votes |
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 #8
Source File: LossDetectorTest.java From aeron with Apache License 2.0 | 6 votes |
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 #9
Source File: UdpChannelTransport.java From aeron with Apache License 2.0 | 6 votes |
/** * Is the received frame valid. This method will do some basic checks on the header and can be * overridden in a subclass for further validation. * * @param buffer containing the frame. * @param length of the frame. * @return true if the frame is believed valid otherwise false. */ public boolean isValidFrame(final UnsafeBuffer buffer, final int length) { boolean isFrameValid = true; if (frameVersion(buffer, 0) != HeaderFlyweight.CURRENT_VERSION) { isFrameValid = false; invalidPackets.increment(); } else if (length < HeaderFlyweight.MIN_HEADER_LENGTH) { isFrameValid = false; invalidPackets.increment(); } return isFrameValid; }
Example #10
Source File: SenderTest.java From aeron with Apache License 2.0 | 6 votes |
@Test public void shouldSendSetupFrameOnChannelWhenTimeoutWithoutStatusMessage() { sender.doWork(); assertThat(receivedFrames.size(), is(1)); nanoClock.advance(Configuration.PUBLICATION_SETUP_TIMEOUT_NS - 1); sender.doWork(); assertThat(receivedFrames.size(), is(1)); nanoClock.advance(10); sender.doWork(); assertThat(receivedFrames.size(), is(2)); setupHeader.wrap(new UnsafeBuffer(receivedFrames.remove())); assertThat(setupHeader.frameLength(), is(SetupFlyweight.HEADER_LENGTH)); assertThat(setupHeader.initialTermId(), is(INITIAL_TERM_ID)); assertThat(setupHeader.activeTermId(), is(INITIAL_TERM_ID)); assertThat(setupHeader.streamId(), is(STREAM_ID)); assertThat(setupHeader.sessionId(), is(SESSION_ID)); assertThat(setupHeader.headerType(), is(HeaderFlyweight.HDR_TYPE_SETUP)); assertThat(setupHeader.flags(), is((short)0)); assertThat(setupHeader.version(), is((short)HeaderFlyweight.CURRENT_VERSION)); }
Example #11
Source File: SenderTest.java From aeron with Apache License 2.0 | 6 votes |
@Test public void shouldNotSendSetupFrameAfterReceivingStatusMessage() { final StatusMessageFlyweight msg = mock(StatusMessageFlyweight.class); when(msg.consumptionTermId()).thenReturn(INITIAL_TERM_ID); when(msg.consumptionTermOffset()).thenReturn(0); when(msg.receiverWindowLength()).thenReturn(0); publication.onStatusMessage(msg, rcvAddress); sender.doWork(); assertThat(receivedFrames.size(), is(1)); receivedFrames.remove(); nanoClock.advance(Configuration.PUBLICATION_SETUP_TIMEOUT_NS + 10); sender.doWork(); assertThat(receivedFrames.size(), is(1)); dataHeader.wrap(receivedFrames.remove()); assertThat(dataHeader.headerType(), is(HeaderFlyweight.HDR_TYPE_DATA)); // heartbeat assertThat(dataHeader.frameLength(), is(0)); assertThat(dataHeader.termOffset(), is(offsetOfMessage(1))); }
Example #12
Source File: SenderTest.java From aeron with Apache License 2.0 | 5 votes |
@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 #13
Source File: ReceiverTest.java From aeron with Apache License 2.0 | 5 votes |
private void fillSetupFrame(final SetupFlyweight header, final int termOffset) { header.wrap(setupBuffer); header .streamId(STREAM_ID) .sessionId(SESSION_ID) .initialTermId(INITIAL_TERM_ID) .activeTermId(ACTIVE_TERM_ID) .termOffset(termOffset) .frameLength(SetupFlyweight.HEADER_LENGTH) .headerType(HeaderFlyweight.HDR_TYPE_SETUP) .flags((byte)0) .version(HeaderFlyweight.CURRENT_VERSION); }
Example #14
Source File: SenderTest.java From aeron with Apache License 2.0 | 5 votes |
@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 #15
Source File: SenderTest.java From aeron with Apache License 2.0 | 5 votes |
@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 #16
Source File: SenderTest.java From aeron with Apache License 2.0 | 5 votes |
@Test public void shouldSendSetupFrameAfterReceivingStatusMessageWithSetupBit() { 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)); // setup then data receivedFrames.remove(); receivedFrames.remove(); publication.triggerSendSetupFrame(); sender.doWork(); assertThat(receivedFrames.size(), is(0)); // setup has been sent already, have to wait nanoClock.advance(Configuration.PUBLICATION_SETUP_TIMEOUT_NS + 10); sender.doWork(); assertThat(receivedFrames.size(), is(1)); setupHeader.wrap(new UnsafeBuffer(receivedFrames.remove())); assertThat(setupHeader.headerType(), is(HeaderFlyweight.HDR_TYPE_SETUP)); }
Example #17
Source File: FlyweightTest.java From aeron with Apache License 2.0 | 5 votes |
@Test public void shouldReadWhatIsWrittenToGenericHeaderFields() { encodeHeader.wrap(aBuff); encodeHeader.version((short)1); encodeHeader.flags((short)0); encodeHeader.headerType(HeaderFlyweight.HDR_TYPE_DATA); encodeHeader.frameLength(8); decodeHeader.wrap(aBuff); assertEquals(1, decodeHeader.version()); assertEquals(HeaderFlyweight.HDR_TYPE_DATA, decodeHeader.headerType()); assertEquals(8, decodeHeader.frameLength()); }
Example #18
Source File: FlyweightTest.java From aeron with Apache License 2.0 | 5 votes |
@Test public void shouldWriteAndReadMultipleFramesCorrectly() { encodeHeader.wrap(aBuff); encodeHeader.version((short)1); encodeHeader.flags((short)0); encodeHeader.headerType(HeaderFlyweight.HDR_TYPE_DATA); encodeHeader.frameLength(8); encodeHeader.wrap(aBuff, 8, aBuff.capacity() - 8); encodeHeader.version((short)2); encodeHeader.flags((short)0x01); encodeHeader.headerType(HeaderFlyweight.HDR_TYPE_SM); encodeHeader.frameLength(8); decodeHeader.wrap(aBuff); assertEquals(1, decodeHeader.version()); assertEquals(0, decodeHeader.flags()); assertEquals(HeaderFlyweight.HDR_TYPE_DATA, decodeHeader.headerType()); assertEquals(8, decodeHeader.frameLength()); decodeHeader.wrap(aBuff, 8, aBuff.capacity() - 8); assertEquals(2, decodeHeader.version()); assertEquals(0x01, decodeHeader.flags()); assertEquals(HeaderFlyweight.HDR_TYPE_SM, decodeHeader.headerType()); assertEquals(8, decodeHeader.frameLength()); }
Example #19
Source File: ImageTest.java From aeron with Apache License 2.0 | 5 votes |
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 #20
Source File: NetworkPublicationThreadLocals.java From aeron with Apache License 2.0 | 5 votes |
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 #21
Source File: SamplesUtil.java From aeron with Apache License 2.0 | 5 votes |
/** * Generic error handler that just prints message to stdout. * * @param channel for the error. * @param streamId for the error. * @param sessionId for the error, if source. * @param message indicating what the error was. * @param cause of the error. */ @SuppressWarnings("unused") public static void printError( final String channel, final int streamId, final int sessionId, final String message, final HeaderFlyweight cause) { System.out.println(message); }
Example #22
Source File: ArchiveTool.java From aeron with Apache License 2.0 | 4 votes |
private static void checksumSegmentFile( final ByteBuffer buffer, final PrintStream out, final File archiveDir, final Checksum checksum, final long recordingId, final String fileName, final long startPosition, final int termLength) { final File file = new File(archiveDir, fileName); final long startTermOffset = startPosition & (termLength - 1); final long startTermBasePosition = startPosition - startTermOffset; final long segmentFileBasePosition = parseSegmentFilePosition(fileName); try (FileChannel channel = FileChannel.open(file.toPath(), READ, WRITE)) { final HeaderFlyweight headerFlyweight = new HeaderFlyweight(buffer); final long bufferAddress = headerFlyweight.addressOffset(); final long size = channel.size(); long fileOffset = segmentFileBasePosition == startTermBasePosition ? startTermOffset : 0; while (fileOffset < size) { buffer.clear().limit(HEADER_LENGTH); if (HEADER_LENGTH != channel.read(buffer, fileOffset)) { out.println("(recordingId=" + recordingId + ", file=" + file + ") ERR: failed to read fragment header"); return; } final int frameLength = headerFlyweight.frameLength(); if (0 == frameLength) { break; } final int alignedLength = align(frameLength, FRAME_ALIGNMENT); if (HDR_TYPE_DATA == frameType(headerFlyweight, 0)) { final int dataLength = alignedLength - HEADER_LENGTH; buffer.clear().limit(dataLength); if (dataLength != channel.read(buffer, fileOffset + HEADER_LENGTH)) { out.println("(recordingId=" + recordingId + ", file=" + file + ") ERR: failed to read " + dataLength + " byte(s) of data at offset " + (fileOffset + HEADER_LENGTH)); return; } int checksumResult = checksum.compute(bufferAddress, 0, dataLength); if (NATIVE_BYTE_ORDER != LITTLE_ENDIAN) { checksumResult = Integer.reverseBytes(checksumResult); } buffer.clear(); buffer.putInt(checksumResult).flip(); channel.write(buffer, fileOffset + SESSION_ID_FIELD_OFFSET); } fileOffset += alignedLength; } } catch (final Exception ex) { out.println("(recordingId=" + recordingId + ", file=" + file + ") ERR: failed to checksum"); ex.printStackTrace(out); } }
Example #23
Source File: ReceiverTest.java From aeron with Apache License 2.0 | 4 votes |
@Test public void shouldHandleNonZeroTermOffsetCorrectly() { final int initialTermOffset = align(TERM_BUFFER_LENGTH / 16, FrameDescriptor.FRAME_ALIGNMENT); final int alignedDataFrameLength = align(DataHeaderFlyweight.HEADER_LENGTH + FAKE_PAYLOAD.length, FrameDescriptor.FRAME_ALIGNMENT); receiverProxy.registerReceiveChannelEndpoint(receiveChannelEndpoint); receiverProxy.addSubscription(receiveChannelEndpoint, STREAM_ID); receiver.doWork(); fillSetupFrame(setupHeader, initialTermOffset); receiveChannelEndpoint.onSetupMessage(setupHeader, setupBuffer, SetupFlyweight.HEADER_LENGTH, senderAddress, 0); final int commandsRead = toConductorQueue.drain( (e) -> { final PublicationImage image = new PublicationImage( CORRELATION_ID, IMAGE_LIVENESS_TIMEOUT_NS, UNTETHERED_WINDOW_LIMIT_TIMEOUT_NS, UNTETHERED_RESTING_TIMEOUT_NS, receiveChannelEndpoint, 0, senderAddress, SESSION_ID, STREAM_ID, INITIAL_TERM_ID, ACTIVE_TERM_ID, initialTermOffset, rawLog, mockFeedbackDelayGenerator, POSITIONS, mockHighestReceivedPosition, mockRebuildPosition, nanoClock, nanoClock, epochClock, mockSystemCounters, SOURCE_ADDRESS, congestionControl, lossReport, mockErrorHandler); receiverProxy.newPublicationImage(receiveChannelEndpoint, image); }); assertThat(commandsRead, is(1)); verify(mockHighestReceivedPosition).setOrdered(initialTermOffset); receiver.doWork(); fillDataFrame(dataHeader, initialTermOffset, FAKE_PAYLOAD); // initial data frame receiveChannelEndpoint.onDataPacket(dataHeader, dataBuffer, alignedDataFrameLength, senderAddress, 0); verify(mockHighestReceivedPosition).setOrdered(initialTermOffset + alignedDataFrameLength); final int readOutcome = TermReader.read( termBuffers[ACTIVE_INDEX], initialTermOffset, (buffer, offset, length, header) -> { assertThat(header.type(), is(HeaderFlyweight.HDR_TYPE_DATA)); assertThat(header.termId(), is(ACTIVE_TERM_ID)); assertThat(header.streamId(), is(STREAM_ID)); assertThat(header.sessionId(), is(SESSION_ID)); assertThat(header.termOffset(), is(initialTermOffset)); assertThat(header.frameLength(), is(DataHeaderFlyweight.HEADER_LENGTH + FAKE_PAYLOAD.length)); }, Integer.MAX_VALUE, header, mockErrorHandler, 0, mockSubscriberPosition); assertThat(readOutcome, is(1)); }
Example #24
Source File: ReceiverTest.java From aeron with Apache License 2.0 | 4 votes |
@Test public void shouldOverwriteHeartbeatWithDataFrame() { receiverProxy.registerReceiveChannelEndpoint(receiveChannelEndpoint); receiverProxy.addSubscription(receiveChannelEndpoint, STREAM_ID); receiver.doWork(); fillSetupFrame(setupHeader); receiveChannelEndpoint.onSetupMessage(setupHeader, setupBuffer, SetupFlyweight.HEADER_LENGTH, senderAddress, 0); final int commandsRead = toConductorQueue.drain( (e) -> { final PublicationImage image = new PublicationImage( CORRELATION_ID, IMAGE_LIVENESS_TIMEOUT_NS, UNTETHERED_WINDOW_LIMIT_TIMEOUT_NS, UNTETHERED_RESTING_TIMEOUT_NS, receiveChannelEndpoint, 0, senderAddress, SESSION_ID, STREAM_ID, INITIAL_TERM_ID, ACTIVE_TERM_ID, INITIAL_TERM_OFFSET, rawLog, mockFeedbackDelayGenerator, POSITIONS, mockHighestReceivedPosition, mockRebuildPosition, nanoClock, nanoClock, epochClock, mockSystemCounters, SOURCE_ADDRESS, congestionControl, lossReport, mockErrorHandler); receiverProxy.newPublicationImage(receiveChannelEndpoint, image); }); assertThat(commandsRead, is(1)); receiver.doWork(); fillDataFrame(dataHeader, 0, FAKE_PAYLOAD); // heartbeat with same term offset receiveChannelEndpoint.onDataPacket(dataHeader, dataBuffer, dataHeader.frameLength(), senderAddress, 0); fillDataFrame(dataHeader, 0, FAKE_PAYLOAD); // initial data frame receiveChannelEndpoint.onDataPacket(dataHeader, dataBuffer, dataHeader.frameLength(), senderAddress, 0); final int readOutcome = TermReader.read( termBuffers[ACTIVE_INDEX], INITIAL_TERM_OFFSET, (buffer, offset, length, header) -> { assertThat(header.type(), is(HeaderFlyweight.HDR_TYPE_DATA)); assertThat(header.termId(), is(ACTIVE_TERM_ID)); assertThat(header.streamId(), is(STREAM_ID)); assertThat(header.sessionId(), is(SESSION_ID)); assertThat(header.termOffset(), is(0)); assertThat(header.frameLength(), is(DataHeaderFlyweight.HEADER_LENGTH + FAKE_PAYLOAD.length)); }, Integer.MAX_VALUE, header, mockErrorHandler, 0, mockSubscriberPosition); assertThat(readOutcome, is(1)); }
Example #25
Source File: ReceiverTest.java From aeron with Apache License 2.0 | 4 votes |
@Test public void shouldNotOverwriteDataFrameWithHeartbeat() { receiverProxy.registerReceiveChannelEndpoint(receiveChannelEndpoint); receiverProxy.addSubscription(receiveChannelEndpoint, STREAM_ID); receiver.doWork(); fillSetupFrame(setupHeader); receiveChannelEndpoint.onSetupMessage(setupHeader, setupBuffer, SetupFlyweight.HEADER_LENGTH, senderAddress, 0); final int commandsRead = toConductorQueue.drain( (e) -> { final PublicationImage image = new PublicationImage( CORRELATION_ID, IMAGE_LIVENESS_TIMEOUT_NS, UNTETHERED_WINDOW_LIMIT_TIMEOUT_NS, UNTETHERED_RESTING_TIMEOUT_NS, receiveChannelEndpoint, 0, senderAddress, SESSION_ID, STREAM_ID, INITIAL_TERM_ID, ACTIVE_TERM_ID, INITIAL_TERM_OFFSET, rawLog, mockFeedbackDelayGenerator, POSITIONS, mockHighestReceivedPosition, mockRebuildPosition, nanoClock, nanoClock, epochClock, mockSystemCounters, SOURCE_ADDRESS, congestionControl, lossReport, mockErrorHandler); receiverProxy.newPublicationImage(receiveChannelEndpoint, image); }); assertThat(commandsRead, is(1)); receiver.doWork(); fillDataFrame(dataHeader, 0, FAKE_PAYLOAD); // initial data frame receiveChannelEndpoint.onDataPacket(dataHeader, dataBuffer, dataHeader.frameLength(), senderAddress, 0); fillDataFrame(dataHeader, 0, FAKE_PAYLOAD); // heartbeat with same term offset receiveChannelEndpoint.onDataPacket(dataHeader, dataBuffer, dataHeader.frameLength(), senderAddress, 0); final int readOutcome = TermReader.read( termBuffers[ACTIVE_INDEX], INITIAL_TERM_OFFSET, (buffer, offset, length, header) -> { assertThat(header.type(), is(HeaderFlyweight.HDR_TYPE_DATA)); assertThat(header.termId(), is(ACTIVE_TERM_ID)); assertThat(header.streamId(), is(STREAM_ID)); assertThat(header.sessionId(), is(SESSION_ID)); assertThat(header.termOffset(), is(0)); assertThat(header.frameLength(), is(DataHeaderFlyweight.HEADER_LENGTH + FAKE_PAYLOAD.length)); }, Integer.MAX_VALUE, header, mockErrorHandler, 0, mockSubscriberPosition); assertThat(readOutcome, is(1)); }
Example #26
Source File: ReceiverTest.java From aeron with Apache License 2.0 | 4 votes |
@Test public void shouldInsertDataIntoLogAfterInitialExchange() { receiverProxy.registerReceiveChannelEndpoint(receiveChannelEndpoint); receiverProxy.addSubscription(receiveChannelEndpoint, STREAM_ID); receiver.doWork(); fillSetupFrame(setupHeader); receiveChannelEndpoint.onSetupMessage(setupHeader, setupBuffer, SetupFlyweight.HEADER_LENGTH, senderAddress, 0); final int commandsRead = toConductorQueue.drain( (e) -> { final PublicationImage image = new PublicationImage( CORRELATION_ID, IMAGE_LIVENESS_TIMEOUT_NS, UNTETHERED_WINDOW_LIMIT_TIMEOUT_NS, UNTETHERED_RESTING_TIMEOUT_NS, receiveChannelEndpoint, 0, senderAddress, SESSION_ID, STREAM_ID, INITIAL_TERM_ID, ACTIVE_TERM_ID, INITIAL_TERM_OFFSET, rawLog, mockFeedbackDelayGenerator, POSITIONS, mockHighestReceivedPosition, mockRebuildPosition, nanoClock, nanoClock, epochClock, mockSystemCounters, SOURCE_ADDRESS, congestionControl, lossReport, mockErrorHandler); receiverProxy.newPublicationImage(receiveChannelEndpoint, image); }); assertThat(commandsRead, is(1)); receiver.doWork(); fillDataFrame(dataHeader, 0, FAKE_PAYLOAD); receiveChannelEndpoint.onDataPacket(dataHeader, dataBuffer, dataHeader.frameLength(), senderAddress, 0); final int readOutcome = TermReader.read( termBuffers[ACTIVE_INDEX], INITIAL_TERM_OFFSET, (buffer, offset, length, header) -> { assertThat(header.type(), is(HeaderFlyweight.HDR_TYPE_DATA)); assertThat(header.termId(), is(ACTIVE_TERM_ID)); assertThat(header.streamId(), is(STREAM_ID)); assertThat(header.sessionId(), is(SESSION_ID)); assertThat(header.termOffset(), is(0)); assertThat(header.frameLength(), is(DataHeaderFlyweight.HEADER_LENGTH + FAKE_PAYLOAD.length)); }, Integer.MAX_VALUE, header, mockErrorHandler, 0, mockSubscriberPosition); assertThat(readOutcome, is(1)); }
Example #27
Source File: SelectorAndTransportTest.java From aeron with Apache License 2.0 | 4 votes |
@Test @Timeout(10) public void shouldHandleSmFrameFromReceiverToSender() { final MutableInteger controlMessagesReceived = new MutableInteger(0); doAnswer( (invocation) -> { controlMessagesReceived.value++; return null; }) .when(mockPublication).onStatusMessage(any(), any()); receiveChannelEndpoint = new ReceiveChannelEndpoint( RCV_DST, mockDispatcher, mockReceiveStatusIndicator, context); sendChannelEndpoint = new SendChannelEndpoint(SRC_DST, mockSendStatusIndicator, context); sendChannelEndpoint.registerForSend(mockPublication); receiveChannelEndpoint.openDatagramChannel(mockReceiveStatusIndicator); receiveChannelEndpoint.registerForRead(dataTransportPoller); sendChannelEndpoint.openDatagramChannel(mockSendStatusIndicator); sendChannelEndpoint.registerForRead(controlTransportPoller); statusMessage.wrap(buffer); statusMessage .streamId(STREAM_ID) .sessionId(SESSION_ID) .consumptionTermId(TERM_ID) .receiverWindowLength(1000) .consumptionTermOffset(0) .version(HeaderFlyweight.CURRENT_VERSION) .flags((short)0) .headerType(HeaderFlyweight.HDR_TYPE_SM) .frameLength(StatusMessageFlyweight.HEADER_LENGTH); byteBuffer.position(0).limit(statusMessage.frameLength()); processLoop(dataTransportPoller, 5); receiveChannelEndpoint.sendTo(byteBuffer, rcvRemoteAddress); while (controlMessagesReceived.get() < 1) { processLoop(controlTransportPoller, 1); } verify(mockStatusMessagesReceivedCounter, times(1)).incrementOrdered(); }
Example #28
Source File: SelectorAndTransportTest.java From aeron with Apache License 2.0 | 4 votes |
@Test @Timeout(10) public void shouldSendMultipleDataFramesPerDatagramUnicastFromSourceToReceiver() { final MutableInteger dataHeadersReceived = new MutableInteger(0); doAnswer( (invocation) -> { dataHeadersReceived.value++; return null; }) .when(mockDispatcher).onDataPacket( any(ReceiveChannelEndpoint.class), any(DataHeaderFlyweight.class), any(UnsafeBuffer.class), anyInt(), any(InetSocketAddress.class), anyInt()); receiveChannelEndpoint = new ReceiveChannelEndpoint( RCV_DST, mockDispatcher, mockReceiveStatusIndicator, context); sendChannelEndpoint = new SendChannelEndpoint(SRC_DST, mockSendStatusIndicator, context); receiveChannelEndpoint.openDatagramChannel(mockReceiveStatusIndicator); receiveChannelEndpoint.registerForRead(dataTransportPoller); sendChannelEndpoint.openDatagramChannel(mockSendStatusIndicator); sendChannelEndpoint.registerForRead(controlTransportPoller); encodeDataHeader.wrap(buffer); encodeDataHeader .version(HeaderFlyweight.CURRENT_VERSION) .flags(DataHeaderFlyweight.BEGIN_AND_END_FLAGS) .headerType(HeaderFlyweight.HDR_TYPE_DATA) .frameLength(FRAME_LENGTH); encodeDataHeader .sessionId(SESSION_ID) .streamId(STREAM_ID) .termId(TERM_ID); final int alignedFrameLength = BitUtil.align(FRAME_LENGTH, FrameDescriptor.FRAME_ALIGNMENT); encodeDataHeader.wrap(buffer, alignedFrameLength, buffer.capacity() - alignedFrameLength); encodeDataHeader .version(HeaderFlyweight.CURRENT_VERSION) .flags(DataHeaderFlyweight.BEGIN_AND_END_FLAGS) .headerType(HeaderFlyweight.HDR_TYPE_DATA) .frameLength(24); encodeDataHeader .sessionId(SESSION_ID) .streamId(STREAM_ID) .termId(TERM_ID); byteBuffer.position(0).limit(2 * alignedFrameLength); processLoop(dataTransportPoller, 5); sendChannelEndpoint.send(byteBuffer); while (dataHeadersReceived.get() < 1) { processLoop(dataTransportPoller, 1); } assertEquals(1, dataHeadersReceived.get()); }
Example #29
Source File: SelectorAndTransportTest.java From aeron with Apache License 2.0 | 4 votes |
@Test @Timeout(10) public void shouldSendEmptyDataFrameUnicastFromSourceToReceiver() { final MutableInteger dataHeadersReceived = new MutableInteger(0); doAnswer( (invocation) -> { dataHeadersReceived.value++; return null; }) .when(mockDispatcher).onDataPacket( any(ReceiveChannelEndpoint.class), any(DataHeaderFlyweight.class), any(UnsafeBuffer.class), anyInt(), any(InetSocketAddress.class), anyInt()); receiveChannelEndpoint = new ReceiveChannelEndpoint( RCV_DST, mockDispatcher, mockReceiveStatusIndicator, context); sendChannelEndpoint = new SendChannelEndpoint(SRC_DST, mockSendStatusIndicator, context); receiveChannelEndpoint.openDatagramChannel(mockReceiveStatusIndicator); receiveChannelEndpoint.registerForRead(dataTransportPoller); sendChannelEndpoint.openDatagramChannel(mockSendStatusIndicator); sendChannelEndpoint.registerForRead(controlTransportPoller); encodeDataHeader.wrap(buffer); encodeDataHeader .version(HeaderFlyweight.CURRENT_VERSION) .flags(DataHeaderFlyweight.BEGIN_AND_END_FLAGS) .headerType(HeaderFlyweight.HDR_TYPE_DATA) .frameLength(FRAME_LENGTH); encodeDataHeader .sessionId(SESSION_ID) .streamId(STREAM_ID) .termId(TERM_ID); byteBuffer.position(0).limit(FRAME_LENGTH); processLoop(dataTransportPoller, 5); sendChannelEndpoint.send(byteBuffer); while (dataHeadersReceived.get() < 1) { processLoop(dataTransportPoller, 1); } assertEquals(1, dataHeadersReceived.get()); }
Example #30
Source File: ReceiveChannelEndpointThreadLocals.java From aeron with Apache License 2.0 | 4 votes |
public ReceiveChannelEndpointThreadLocals() { final int smLength = StatusMessageFlyweight.HEADER_LENGTH + SIZE_OF_LONG; final int bufferLength = BitUtil.align(smLength, CACHE_LINE_LENGTH) + BitUtil.align(NakFlyweight.HEADER_LENGTH, CACHE_LINE_LENGTH) + BitUtil.align(RttMeasurementFlyweight.HEADER_LENGTH, CACHE_LINE_LENGTH); final UUID uuid = UUID.randomUUID(); nextReceiverId = uuid.getMostSignificantBits() ^ uuid.getLeastSignificantBits(); final ByteBuffer byteBuffer = BufferUtil.allocateDirectAligned(bufferLength, CACHE_LINE_LENGTH); byteBuffer.limit(smLength); smBuffer = byteBuffer.slice(); statusMessageFlyweight = new StatusMessageFlyweight(smBuffer); final int nakMessageOffset = BitUtil.align(smLength, FRAME_ALIGNMENT); byteBuffer.limit(nakMessageOffset + NakFlyweight.HEADER_LENGTH).position(nakMessageOffset); nakBuffer = byteBuffer.slice(); nakFlyweight = new NakFlyweight(nakBuffer); final int rttMeasurementOffset = nakMessageOffset + BitUtil.align(NakFlyweight.HEADER_LENGTH, FRAME_ALIGNMENT); byteBuffer.limit(rttMeasurementOffset + RttMeasurementFlyweight.HEADER_LENGTH).position(rttMeasurementOffset); rttMeasurementBuffer = byteBuffer.slice(); rttMeasurementFlyweight = new RttMeasurementFlyweight(rttMeasurementBuffer); statusMessageFlyweight .version(HeaderFlyweight.CURRENT_VERSION) .headerType(HeaderFlyweight.HDR_TYPE_SM) .frameLength(StatusMessageFlyweight.HEADER_LENGTH); nakFlyweight .version(HeaderFlyweight.CURRENT_VERSION) .headerType(HeaderFlyweight.HDR_TYPE_NAK) .frameLength(NakFlyweight.HEADER_LENGTH); rttMeasurementFlyweight .version(HeaderFlyweight.CURRENT_VERSION) .headerType(HeaderFlyweight.HDR_TYPE_RTTM) .frameLength(RttMeasurementFlyweight.HEADER_LENGTH); }