Search in sources :

Example 26 with Header

use of io.aeron.logbuffer.Header in project aeron by real-logic.

the class ReplaySessionTest method before.

@BeforeEach
public void before() throws IOException {
    context = new Archive.Context().segmentFileLength(SEGMENT_LENGTH).archiveDir(archiveDir).epochClock(epochClock).countedErrorHandler(countedErrorHandler);
    when(recordingPositionCounter.get()).then((invocation) -> recordingPosition);
    when(mockControlSession.archiveConductor()).thenReturn(mockArchiveConductor);
    when(mockArchiveConductor.catalog()).thenReturn(mockCatalog);
    when(mockArchiveConductor.context()).thenReturn(context);
    when(mockReplayPub.termBufferLength()).thenReturn(TERM_BUFFER_LENGTH);
    when(mockReplayPub.positionBitsToShift()).thenReturn(LogBufferDescriptor.positionBitsToShift(TERM_BUFFER_LENGTH));
    when(mockReplayPub.initialTermId()).thenReturn(INITIAL_TERM_ID);
    when(mockReplayPub.availableWindow()).thenReturn((long) TERM_BUFFER_LENGTH / 2);
    when(mockImage.termBufferLength()).thenReturn(TERM_BUFFER_LENGTH);
    when(mockImage.joinPosition()).thenReturn(JOIN_POSITION);
    recordingSummary.recordingId = RECORDING_ID;
    recordingSummary.startPosition = START_POSITION;
    recordingSummary.segmentFileLength = context.segmentFileLength();
    recordingSummary.initialTermId = INITIAL_TERM_ID;
    recordingSummary.termBufferLength = TERM_BUFFER_LENGTH;
    recordingSummary.mtuLength = MTU_LENGTH;
    recordingSummary.streamId = STREAM_ID;
    recordingSummary.sessionId = SESSION_ID;
    final RecordingWriter writer = new RecordingWriter(RECORDING_ID, START_POSITION, SEGMENT_LENGTH, mockImage, context);
    writer.init();
    final UnsafeBuffer buffer = new UnsafeBuffer(allocateDirectAligned(TERM_BUFFER_LENGTH, 64));
    final DataHeaderFlyweight headerFwt = new DataHeaderFlyweight();
    final Header header = new Header(INITIAL_TERM_ID, Integer.numberOfLeadingZeros(TERM_BUFFER_LENGTH));
    header.buffer(buffer);
    recordFragment(writer, buffer, headerFwt, header, INITIAL_TERM_OFFSET, FRAME_LENGTH, 0, UNFRAGMENTED, HDR_TYPE_DATA, SESSION_ID);
    recordFragment(writer, buffer, headerFwt, header, INITIAL_TERM_OFFSET + FRAME_LENGTH, FRAME_LENGTH, 1, BEGIN_FRAG_FLAG, HDR_TYPE_DATA, SESSION_ID);
    recordFragment(writer, buffer, headerFwt, header, INITIAL_TERM_OFFSET + FRAME_LENGTH * 2, FRAME_LENGTH, 2, END_FRAG_FLAG, HDR_TYPE_DATA, SESSION_ID);
    recordFragment(writer, buffer, headerFwt, header, INITIAL_TERM_OFFSET + FRAME_LENGTH * 3, FRAME_LENGTH, 3, UNFRAGMENTED, HDR_TYPE_PAD, SESSION_ID);
    writer.close();
    recordingSummary.stopPosition = START_POSITION + 4 * FRAME_LENGTH;
}
Also used : AeronArchive(io.aeron.archive.client.AeronArchive) Header(io.aeron.logbuffer.Header) DataHeaderFlyweight(io.aeron.protocol.DataHeaderFlyweight) UnsafeBuffer(org.agrona.concurrent.UnsafeBuffer) BeforeEach(org.junit.jupiter.api.BeforeEach)

Example 27 with Header

use of io.aeron.logbuffer.Header in project aeron by real-logic.

the class ReplaySessionTest method shouldDoCrcForEachDataFrame.

@Test
public void shouldDoCrcForEachDataFrame() throws IOException {
    context.recordChecksum(crc32());
    context.recordChecksumBuffer(new UnsafeBuffer(ByteBuffer.allocateDirect(TERM_BUFFER_LENGTH)));
    final RecordingWriter writer = new RecordingWriter(RECORDING_ID, START_POSITION, SEGMENT_LENGTH, mockImage, context);
    writer.init();
    final UnsafeBuffer buffer = new UnsafeBuffer(allocateDirectAligned(TERM_BUFFER_LENGTH, 64));
    final DataHeaderFlyweight headerFwt = new DataHeaderFlyweight();
    final Header header = new Header(INITIAL_TERM_ID, Integer.numberOfLeadingZeros(TERM_BUFFER_LENGTH));
    header.buffer(buffer);
    recordFragment(writer, buffer, headerFwt, header, 0, FRAME_LENGTH, 10, UNFRAGMENTED, HDR_TYPE_DATA, checksum(FRAME_LENGTH, 10));
    recordFragment(writer, buffer, headerFwt, header, FRAME_LENGTH, FRAME_LENGTH, 20, BEGIN_FRAG_FLAG, HDR_TYPE_DATA, checksum(FRAME_LENGTH, 20));
    recordFragment(writer, buffer, headerFwt, header, FRAME_LENGTH * 2, FRAME_LENGTH, 30, END_FRAG_FLAG, HDR_TYPE_DATA, checksum(FRAME_LENGTH, 30));
    recordFragment(writer, buffer, headerFwt, header, FRAME_LENGTH * 3, FRAME_LENGTH, 40, UNFRAGMENTED, HDR_TYPE_PAD, SESSION_ID);
    writer.close();
    final long length = 4 * FRAME_LENGTH;
    final long correlationId = 1L;
    final int sessionId = Integer.MIN_VALUE;
    final int streamId = Integer.MAX_VALUE;
    try (ReplaySession replaySession = replaySession(RECORDING_POSITION, length, correlationId, mockReplayPub, mockControlSession, null, context.recordChecksum())) {
        when(mockReplayPub.isClosed()).thenReturn(false);
        when(mockReplayPub.isConnected()).thenReturn(false);
        when(mockReplayPub.sessionId()).thenReturn(sessionId);
        when(mockReplayPub.streamId()).thenReturn(streamId);
        replaySession.doWork();
        assertEquals(replaySession.state(), ReplaySession.State.INIT);
        when(mockReplayPub.isConnected()).thenReturn(true);
        final UnsafeBuffer termBuffer = new UnsafeBuffer(allocateDirectAligned(4096, 64));
        mockPublication(mockReplayPub, termBuffer);
        assertNotEquals(0, replaySession.doWork());
        assertThat(messageCounter, is(2));
        validateFrame(termBuffer, 0, FRAME_LENGTH, 10, UNFRAGMENTED, sessionId, streamId);
        validateFrame(termBuffer, FRAME_LENGTH, FRAME_LENGTH, 20, BEGIN_FRAG_FLAG, sessionId, streamId);
        validateFrame(termBuffer, 2 * FRAME_LENGTH, FRAME_LENGTH, 30, END_FRAG_FLAG, sessionId, streamId);
        verify(mockReplayPub).appendPadding(FRAME_LENGTH - HEADER_LENGTH);
        assertTrue(replaySession.isDone());
    }
}
Also used : Header(io.aeron.logbuffer.Header) DataHeaderFlyweight(io.aeron.protocol.DataHeaderFlyweight) UnsafeBuffer(org.agrona.concurrent.UnsafeBuffer) Test(org.junit.jupiter.api.Test)

Example 28 with Header

use of io.aeron.logbuffer.Header in project aeron by real-logic.

the class ReplaySessionTest method shouldCalculateBlockSizeBasedOnFullFragments.

@Test
public void shouldCalculateBlockSizeBasedOnFullFragments() throws IOException {
    context.recordChecksumBuffer(new UnsafeBuffer(ByteBuffer.allocateDirect(TERM_BUFFER_LENGTH)));
    final RecordingWriter writer = new RecordingWriter(RECORDING_ID, START_POSITION, SEGMENT_LENGTH, mockImage, context);
    writer.init();
    final UnsafeBuffer buffer = new UnsafeBuffer(allocateDirectAligned(TERM_BUFFER_LENGTH, 64));
    final DataHeaderFlyweight headerFwt = new DataHeaderFlyweight();
    final Header header = new Header(INITIAL_TERM_ID, Integer.numberOfLeadingZeros(TERM_BUFFER_LENGTH));
    header.buffer(buffer);
    recordFragment(writer, buffer, headerFwt, header, 0, 100, 11, UNFRAGMENTED, HDR_TYPE_DATA, SESSION_ID);
    recordFragment(writer, buffer, headerFwt, header, 128, 200, 22, UNFRAGMENTED, HDR_TYPE_DATA, SESSION_ID);
    recordFragment(writer, buffer, headerFwt, header, 352, 300, 33, UNFRAGMENTED, HDR_TYPE_DATA, SESSION_ID);
    recordFragment(writer, buffer, headerFwt, header, 672, 400, 44, UNFRAGMENTED, HDR_TYPE_DATA, SESSION_ID);
    recordFragment(writer, buffer, headerFwt, header, 1088, 500, 55, UNFRAGMENTED, HDR_TYPE_DATA, SESSION_ID);
    recordFragment(writer, buffer, headerFwt, header, 1600, 6000, 66, UNFRAGMENTED, HDR_TYPE_DATA, SESSION_ID);
    writer.close();
    final int sessionId = 555;
    final int streamId = 777;
    try (ReplaySession replaySession = replaySession(RECORDING_POSITION, 1_000_000, -1, mockReplayPub, mockControlSession, null, null)) {
        when(mockReplayPub.isClosed()).thenReturn(false);
        when(mockReplayPub.isConnected()).thenReturn(false);
        when(mockReplayPub.sessionId()).thenReturn(sessionId);
        when(mockReplayPub.streamId()).thenReturn(streamId);
        replaySession.doWork();
        assertEquals(replaySession.state(), ReplaySession.State.INIT);
        when(mockReplayPub.isConnected()).thenReturn(true);
        final UnsafeBuffer termBuffer = new UnsafeBuffer(allocateDirectAligned(4096, 64));
        mockPublication(mockReplayPub, termBuffer);
        assertEquals(2, replaySession.doWork());
        assertThat(messageCounter, is(1));
        validateFrame(termBuffer, 0, 100, 11, UNFRAGMENTED, sessionId, streamId);
        validateFrame(termBuffer, 128, 200, 22, UNFRAGMENTED, sessionId, streamId);
        validateFrame(termBuffer, 352, 300, 33, UNFRAGMENTED, sessionId, streamId);
        validateFrame(termBuffer, 672, 400, 44, UNFRAGMENTED, sessionId, streamId);
        validateFrame(termBuffer, 1088, 500, 55, UNFRAGMENTED, sessionId, streamId);
        verify(mockReplayPub, never()).appendPadding(anyInt());
    }
}
Also used : Header(io.aeron.logbuffer.Header) DataHeaderFlyweight(io.aeron.protocol.DataHeaderFlyweight) UnsafeBuffer(org.agrona.concurrent.UnsafeBuffer) Test(org.junit.jupiter.api.Test)

Example 29 with Header

use of io.aeron.logbuffer.Header in project aeron by real-logic.

the class ClusterNodeTest method shouldScheduleEventInService.

@Test
@InterruptAfter(10)
public void shouldScheduleEventInService() {
    final ExpandableArrayBuffer msgBuffer = new ExpandableArrayBuffer();
    final String msg = "Hello World!";
    msgBuffer.putStringWithoutLengthAscii(0, msg);
    final MutableInteger messageCount = new MutableInteger();
    final EgressListener listener = (clusterSessionId, timestamp, buffer, offset, length, header) -> {
        final String expected = msg + "-scheduled";
        assertEquals(expected, buffer.getStringWithoutLengthAscii(offset, length));
        messageCount.value += 1;
    };
    container = launchTimedService();
    aeronCluster = connectToCluster(listener);
    offerMessage(msgBuffer, msg);
    awaitResponse(messageCount);
    ClusterTests.failOnClusterError();
}
Also used : ClusterTests(io.aeron.test.cluster.ClusterTests) AeronCluster(io.aeron.cluster.client.AeronCluster) Tests(io.aeron.test.Tests) BeforeEach(org.junit.jupiter.api.BeforeEach) StubClusteredService(io.aeron.test.cluster.StubClusteredService) ClusteredServiceContainer(io.aeron.cluster.service.ClusteredServiceContainer) CLUSTER_MEMBERS(io.aeron.cluster.ClusterTestConstants.CLUSTER_MEMBERS) ClusteredService(io.aeron.cluster.service.ClusteredService) ExtendWith(org.junit.jupiter.api.extension.ExtendWith) BufferClaim(io.aeron.logbuffer.BufferClaim) Assertions.assertEquals(org.junit.jupiter.api.Assertions.assertEquals) ArchiveThreadingMode(io.aeron.archive.ArchiveThreadingMode) MutableInteger(org.agrona.collections.MutableInteger) CloseHelper(org.agrona.CloseHelper) MediaDriver(io.aeron.driver.MediaDriver) InterruptingTestCallback(io.aeron.test.InterruptingTestCallback) Archive(io.aeron.archive.Archive) ExpandableArrayBuffer(org.agrona.ExpandableArrayBuffer) Test(org.junit.jupiter.api.Test) EgressListener(io.aeron.cluster.client.EgressListener) InterruptAfter(io.aeron.test.InterruptAfter) AfterEach(org.junit.jupiter.api.AfterEach) Header(io.aeron.logbuffer.Header) ClientSession(io.aeron.cluster.service.ClientSession) ThreadingMode(io.aeron.driver.ThreadingMode) Assertions.assertTrue(org.junit.jupiter.api.Assertions.assertTrue) INGRESS_ENDPOINTS(io.aeron.cluster.ClusterTestConstants.INGRESS_ENDPOINTS) DirectBuffer(org.agrona.DirectBuffer) MutableInteger(org.agrona.collections.MutableInteger) ExpandableArrayBuffer(org.agrona.ExpandableArrayBuffer) EgressListener(io.aeron.cluster.client.EgressListener) Test(org.junit.jupiter.api.Test) InterruptAfter(io.aeron.test.InterruptAfter)

Example 30 with Header

use of io.aeron.logbuffer.Header in project aeron by real-logic.

the class ClusterNodeTest method shouldSendResponseAfterServiceMessage.

@Test
@InterruptAfter(10)
public void shouldSendResponseAfterServiceMessage() {
    final ExpandableArrayBuffer msgBuffer = new ExpandableArrayBuffer();
    final String msg = "Hello World!";
    msgBuffer.putStringWithoutLengthAscii(0, msg);
    final MutableInteger messageCount = new MutableInteger();
    final EgressListener listener = (clusterSessionId, timestamp, buffer, offset, length, header) -> {
        assertEquals(msg, buffer.getStringWithoutLengthAscii(offset, length));
        messageCount.value += 1;
    };
    container = launchServiceMessageIngressService();
    aeronCluster = connectToCluster(listener);
    offerMessage(msgBuffer, msg);
    awaitResponse(messageCount);
    ClusterTests.failOnClusterError();
}
Also used : ClusterTests(io.aeron.test.cluster.ClusterTests) AeronCluster(io.aeron.cluster.client.AeronCluster) Tests(io.aeron.test.Tests) BeforeEach(org.junit.jupiter.api.BeforeEach) StubClusteredService(io.aeron.test.cluster.StubClusteredService) ClusteredServiceContainer(io.aeron.cluster.service.ClusteredServiceContainer) CLUSTER_MEMBERS(io.aeron.cluster.ClusterTestConstants.CLUSTER_MEMBERS) ClusteredService(io.aeron.cluster.service.ClusteredService) ExtendWith(org.junit.jupiter.api.extension.ExtendWith) BufferClaim(io.aeron.logbuffer.BufferClaim) Assertions.assertEquals(org.junit.jupiter.api.Assertions.assertEquals) ArchiveThreadingMode(io.aeron.archive.ArchiveThreadingMode) MutableInteger(org.agrona.collections.MutableInteger) CloseHelper(org.agrona.CloseHelper) MediaDriver(io.aeron.driver.MediaDriver) InterruptingTestCallback(io.aeron.test.InterruptingTestCallback) Archive(io.aeron.archive.Archive) ExpandableArrayBuffer(org.agrona.ExpandableArrayBuffer) Test(org.junit.jupiter.api.Test) EgressListener(io.aeron.cluster.client.EgressListener) InterruptAfter(io.aeron.test.InterruptAfter) AfterEach(org.junit.jupiter.api.AfterEach) Header(io.aeron.logbuffer.Header) ClientSession(io.aeron.cluster.service.ClientSession) ThreadingMode(io.aeron.driver.ThreadingMode) Assertions.assertTrue(org.junit.jupiter.api.Assertions.assertTrue) INGRESS_ENDPOINTS(io.aeron.cluster.ClusterTestConstants.INGRESS_ENDPOINTS) DirectBuffer(org.agrona.DirectBuffer) MutableInteger(org.agrona.collections.MutableInteger) ExpandableArrayBuffer(org.agrona.ExpandableArrayBuffer) EgressListener(io.aeron.cluster.client.EgressListener) Test(org.junit.jupiter.api.Test) InterruptAfter(io.aeron.test.InterruptAfter)

Aggregations

Header (io.aeron.logbuffer.Header)63 Test (org.junit.jupiter.api.Test)44 DirectBuffer (org.agrona.DirectBuffer)33 ClientSession (io.aeron.cluster.service.ClientSession)21 StubClusteredService (io.aeron.test.cluster.StubClusteredService)20 ExpandableArrayBuffer (org.agrona.ExpandableArrayBuffer)20 ClusteredService (io.aeron.cluster.service.ClusteredService)18 ClusteredServiceContainer (io.aeron.cluster.service.ClusteredServiceContainer)18 MediaDriver (io.aeron.driver.MediaDriver)18 CloseHelper (org.agrona.CloseHelper)17 Subscription (io.aeron.Subscription)16 Archive (io.aeron.archive.Archive)16 ArchiveThreadingMode (io.aeron.archive.ArchiveThreadingMode)16 AeronCluster (io.aeron.cluster.client.AeronCluster)16 ThreadingMode (io.aeron.driver.ThreadingMode)16 ExtendWith (org.junit.jupiter.api.extension.ExtendWith)16 UnsafeBuffer (org.agrona.concurrent.UnsafeBuffer)15 EgressListener (io.aeron.cluster.client.EgressListener)14 BeforeEach (org.junit.jupiter.api.BeforeEach)14 CLUSTER_MEMBERS (io.aeron.cluster.ClusterTestConstants.CLUSTER_MEMBERS)12