Search in sources :

Example 31 with UnsafeBuffer

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

the class PublicationTest method setUp.

@Before
public void setUp() {
    when(publicationLimit.getVolatile()).thenReturn(2L * SEND_BUFFER_CAPACITY);
    when(logBuffers.termBuffers()).thenReturn(termBuffers);
    when(logBuffers.termLength()).thenReturn(TERM_MIN_LENGTH);
    when(logBuffers.metaDataBuffer()).thenReturn(logMetaDataBuffer);
    when(conductor.clientLock()).thenReturn(conductorLock);
    initialTermId(logMetaDataBuffer, TERM_ID_1);
    mtuLength(logMetaDataBuffer, MTU_LENGTH);
    timeOfLastStatusMessage(logMetaDataBuffer, 0);
    for (int i = 0; i < PARTITION_COUNT; i++) {
        termBuffers[i] = new UnsafeBuffer(allocateDirect(TERM_MIN_LENGTH));
    }
    publication = new Publication(conductor, CHANNEL, STREAM_ID_1, SESSION_ID_1, publicationLimit, logBuffers, CORRELATION_ID);
    publication.incRef();
    initialiseTailWithTermId(logMetaDataBuffer, PARTITION_INDEX, TERM_ID_1);
}
Also used : UnsafeBuffer(org.agrona.concurrent.UnsafeBuffer) Before(org.junit.Before)

Example 32 with UnsafeBuffer

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

the class LogInspector method main.

public static void main(final String[] args) throws Exception {
    final PrintStream out = System.out;
    if (args.length < 1) {
        out.println("Usage: LogInspector <logFileName> [message dump limit]");
        return;
    }
    final String logFileName = args[0];
    final int messageDumpLimit = args.length >= 2 ? Integer.parseInt(args[1]) : Integer.MAX_VALUE;
    try (LogBuffers logBuffers = new LogBuffers(logFileName, READ_ONLY)) {
        out.println("======================================================================");
        out.format("%s Inspection dump for %s%n", new Date(), logFileName);
        out.println("======================================================================");
        final DataHeaderFlyweight dataHeaderFlyweight = new DataHeaderFlyweight();
        final UnsafeBuffer[] termBuffers = logBuffers.termBuffers();
        final int termLength = logBuffers.termLength();
        final UnsafeBuffer metaDataBuffer = logBuffers.metaDataBuffer();
        out.format("Time of last SM: %s%n", new Date(timeOfLastStatusMessage(metaDataBuffer)));
        out.format("Initial term id: %d%n", initialTermId(metaDataBuffer));
        out.format("   Active index: %d%n", activePartitionIndex(metaDataBuffer));
        out.format("    Term length: %d%n", termLength);
        out.format("     MTU length: %d%n%n", mtuLength(metaDataBuffer));
        if (!SKIP_DEFAULT_HEADER) {
            dataHeaderFlyweight.wrap(defaultFrameHeader(metaDataBuffer));
            out.format("default %s%n", dataHeaderFlyweight);
        }
        out.println();
        for (int i = 0; i < PARTITION_COUNT; i++) {
            final long rawTail = rawTailVolatile(metaDataBuffer, 0);
            final long termOffset = rawTail & 0xFFFF_FFFFL;
            out.format("Index %d Term Meta Data termOffset=%d termId=%d rawTail=%d%n", i, termOffset, termId(rawTail), rawTail);
        }
        for (int i = 0; i < PARTITION_COUNT; i++) {
            out.println("%n======================================================================");
            out.format("Index %d Term Data%n%n", i);
            final UnsafeBuffer termBuffer = termBuffers[i];
            int offset = 0;
            do {
                dataHeaderFlyweight.wrap(termBuffer, offset, termLength - offset);
                out.println(offset + ": " + dataHeaderFlyweight.toString());
                final int frameLength = dataHeaderFlyweight.frameLength();
                if (frameLength < DataHeaderFlyweight.HEADER_LENGTH) {
                    if (0 == frameLength && SCAN_OVER_ZEROES) {
                        offset += FrameDescriptor.FRAME_ALIGNMENT;
                        continue;
                    }
                    try {
                        final int limit = min(termLength - (offset + HEADER_LENGTH), messageDumpLimit);
                        out.println(formatBytes(termBuffer, offset + HEADER_LENGTH, limit));
                    } catch (final Exception ex) {
                        System.out.printf("frameLength=%d offset=%d%n", frameLength, offset);
                        ex.printStackTrace();
                    }
                    break;
                }
                final int limit = min(frameLength - HEADER_LENGTH, messageDumpLimit);
                out.println(formatBytes(termBuffer, offset + HEADER_LENGTH, limit));
                offset += BitUtil.align(frameLength, FrameDescriptor.FRAME_ALIGNMENT);
            } while (offset < termLength);
        }
    }
}
Also used : PrintStream(java.io.PrintStream) DataHeaderFlyweight(io.aeron.protocol.DataHeaderFlyweight) UnsafeBuffer(org.agrona.concurrent.UnsafeBuffer) LogBuffers(io.aeron.LogBuffers) Date(java.util.Date)

Example 33 with UnsafeBuffer

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

the class LogInspectorAsciiFormatBytesTest method shouldFormatBytesToAscii.

@Test
public void shouldFormatBytesToAscii() {
    System.setProperty(FORMAT_KEY, "ascii");
    final char[] formattedBytes = LogInspector.formatBytes(new UnsafeBuffer(new byte[] { buffer }), 0, 1);
    Assert.assertEquals(expected, formattedBytes[0]);
}
Also used : UnsafeBuffer(org.agrona.concurrent.UnsafeBuffer) Test(org.junit.Test)

Example 34 with UnsafeBuffer

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

the class BufferClaimMessageTest method shouldReceivePublishedMessageWithInterleavedAbort.

@Theory
@Test(timeout = 10000)
public void shouldReceivePublishedMessageWithInterleavedAbort(final String channel) throws Exception {
    final BufferClaim bufferClaim = new BufferClaim();
    final UnsafeBuffer srcBuffer = new UnsafeBuffer(ByteBuffer.allocateDirect(MESSAGE_LENGTH));
    final MediaDriver.Context ctx = new MediaDriver.Context();
    try (MediaDriver ignore = MediaDriver.launch(ctx);
        Aeron aeron = Aeron.connect();
        Publication publication = aeron.addPublication(channel, STREAM_ID);
        Subscription subscription = aeron.addSubscription(channel, STREAM_ID)) {
        publishMessage(srcBuffer, publication);
        while (publication.tryClaim(MESSAGE_LENGTH, bufferClaim) < 0L) {
            Thread.yield();
        }
        publishMessage(srcBuffer, publication);
        bufferClaim.abort();
        final int expectedNumberOfFragments = 2;
        int numFragments = 0;
        do {
            numFragments += subscription.poll(mockFragmentHandler, FRAGMENT_COUNT_LIMIT);
        } while (numFragments < expectedNumberOfFragments);
        verify(mockFragmentHandler, times(expectedNumberOfFragments)).onFragment(any(DirectBuffer.class), anyInt(), eq(MESSAGE_LENGTH), any(Header.class));
    } finally {
        ctx.deleteAeronDirectory();
    }
}
Also used : DirectBuffer(org.agrona.DirectBuffer) MediaDriver(io.aeron.driver.MediaDriver) Header(io.aeron.logbuffer.Header) UnsafeBuffer(org.agrona.concurrent.UnsafeBuffer) DataPoint(org.junit.experimental.theories.DataPoint) BufferClaim(io.aeron.logbuffer.BufferClaim) Theory(org.junit.experimental.theories.Theory) Test(org.junit.Test)

Example 35 with UnsafeBuffer

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

the class GapFillLossTest method shouldGapFillWhenLossOccurs.

@Test(timeout = 10000)
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().threadingMode(ThreadingMode.SHARED).publicationTermBufferLength(TERM_BUFFER_LENGTH);
    final LossReport lossReport = mock(LossReport.class);
    ctx.lossReport(lossReport);
    final LossGenerator dataLossGenerator = DebugChannelEndpointConfiguration.lossGeneratorSupplier(0.20, 0xcafebabeL);
    final LossGenerator noLossGenerator = DebugChannelEndpointConfiguration.lossGeneratorSupplier(0, 0);
    ctx.sendChannelEndpointSupplier((udpChannel, statusIndicator, context) -> new DebugSendChannelEndpoint(udpChannel, statusIndicator, context, noLossGenerator, noLossGenerator));
    ctx.receiveChannelEndpointSupplier((udpChannel, dispatcher, statusIndicator, context) -> new DebugReceiveChannelEndpoint(udpChannel, dispatcher, statusIndicator, context, dataLossGenerator, noLossGenerator));
    try (MediaDriver ignore = MediaDriver.launch(ctx);
        Aeron aeron = Aeron.connect();
        Publication publication = aeron.addPublication(CHANNEL, STREAM_ID);
        Subscription subscription = aeron.addSubscription(UNRELIABLE_CHANNEL, STREAM_ID)) {
        final IdleStrategy idleStrategy = new YieldingIdleStrategy();
        final Subscriber subscriber = new Subscriber(subscription);
        final Thread subscriberThread = new Thread(subscriber);
        subscriberThread.start();
        long position = 0;
        for (int i = 0; i < NUM_MESSAGES; i++) {
            srcBuffer.putLong(0, i);
            while ((position = publication.offer(srcBuffer)) < 0L) {
                idleStrategy.idle();
            }
        }
        FINAL_POSITION.set(position);
        subscriberThread.join();
        assertThat(subscriber.messageCount, lessThan(NUM_MESSAGES));
        verify(lossReport).createEntry(anyLong(), anyLong(), anyInt(), eq(STREAM_ID), anyString(), anyString());
    } finally {
        ctx.deleteAeronDirectory();
    }
}
Also used : YieldingIdleStrategy(org.agrona.concurrent.YieldingIdleStrategy) LossGenerator(io.aeron.driver.ext.LossGenerator) YieldingIdleStrategy(org.agrona.concurrent.YieldingIdleStrategy) IdleStrategy(org.agrona.concurrent.IdleStrategy) LossReport(io.aeron.driver.reports.LossReport) DebugSendChannelEndpoint(io.aeron.driver.ext.DebugSendChannelEndpoint) DebugSendChannelEndpoint(io.aeron.driver.ext.DebugSendChannelEndpoint) DebugReceiveChannelEndpoint(io.aeron.driver.ext.DebugReceiveChannelEndpoint) DebugReceiveChannelEndpoint(io.aeron.driver.ext.DebugReceiveChannelEndpoint) MediaDriver(io.aeron.driver.MediaDriver) UnsafeBuffer(org.agrona.concurrent.UnsafeBuffer) Test(org.junit.Test)

Aggregations

UnsafeBuffer (org.agrona.concurrent.UnsafeBuffer)78 Test (org.junit.Test)42 StatusMessageFlyweight (io.aeron.protocol.StatusMessageFlyweight)8 MediaDriver (io.aeron.driver.MediaDriver)7 MutableDirectBuffer (org.agrona.MutableDirectBuffer)6 Header (io.aeron.logbuffer.Header)5 InOrder (org.mockito.InOrder)5 MappedByteBuffer (java.nio.MappedByteBuffer)4 Theory (org.junit.experimental.theories.Theory)4 ReceiveChannelEndpoint (io.aeron.driver.media.ReceiveChannelEndpoint)3 File (java.io.File)3 Date (java.util.Date)3 DirectBuffer (org.agrona.DirectBuffer)3 Before (org.junit.Before)3 DataPoint (org.junit.experimental.theories.DataPoint)3 SendChannelEndpoint (io.aeron.driver.media.SendChannelEndpoint)2 BufferClaim (io.aeron.logbuffer.BufferClaim)2 FragmentHandler (io.aeron.logbuffer.FragmentHandler)2 PrintStream (java.io.PrintStream)2 InetSocketAddress (java.net.InetSocketAddress)2