Search in sources :

Example 1 with DirectBuffer

use of org.agrona.DirectBuffer in project aeron by real-logic.

the class RecordingPos method countBySession.

/**
 * Count the number of counters for a given session. It is possible for different recording to exist on the
 * same session if there are images under subscriptions with different channel and stream id.
 *
 * @param countersReader to search within.
 * @param sessionId      to search for.
 * @return the count of recordings matching a session id.
 */
public static int countBySession(final CountersReader countersReader, final int sessionId) {
    int count = 0;
    final DirectBuffer buffer = countersReader.metaDataBuffer();
    for (int i = 0, size = countersReader.maxCounterId(); i < size; i++) {
        if (countersReader.getCounterState(i) == RECORD_ALLOCATED) {
            final int recordOffset = CountersReader.metaDataOffset(i);
            if (buffer.getInt(recordOffset + TYPE_ID_OFFSET) == RECORDING_POSITION_TYPE_ID && buffer.getInt(recordOffset + KEY_OFFSET + SESSION_ID_OFFSET) == sessionId) {
                ++count;
            }
        }
    }
    return count;
}
Also used : DirectBuffer(org.agrona.DirectBuffer)

Example 2 with DirectBuffer

use of org.agrona.DirectBuffer in project aeron by real-logic.

the class MultiSubscriberTest method verifyData.

private void verifyData(final UnsafeBuffer srcBuffer, final FragmentHandler mockFragmentHandler) {
    final ArgumentCaptor<DirectBuffer> bufferArg = ArgumentCaptor.forClass(DirectBuffer.class);
    final ArgumentCaptor<Integer> offsetArg = ArgumentCaptor.forClass(Integer.class);
    verify(mockFragmentHandler, times(1)).onFragment(bufferArg.capture(), offsetArg.capture(), eq(srcBuffer.capacity()), any(Header.class));
    final DirectBuffer capturedBuffer = bufferArg.getValue();
    final int offset = offsetArg.getValue();
    for (int i = 0; i < srcBuffer.capacity(); i++) {
        final int index = offset + i;
        assertThat("same at " + index, capturedBuffer.getByte(index), is(srcBuffer.getByte(i)));
    }
}
Also used : DirectBuffer(org.agrona.DirectBuffer) Header(io.aeron.logbuffer.Header)

Example 3 with DirectBuffer

use of org.agrona.DirectBuffer in project aeron by real-logic.

the class DriverTool method main.

public static void main(final String[] args) throws Exception {
    boolean printPidOnly = false;
    if (0 != args.length) {
        checkForHelp(args);
        if (args[0].equals("pid")) {
            printPidOnly = true;
        }
    }
    final File cncFile = CommonContext.newDefaultCncFile();
    final MappedByteBuffer cncByteBuffer = IoUtil.mapExistingFile(cncFile, "cnc");
    final DirectBuffer cncMetaData = createMetaDataBuffer(cncByteBuffer);
    final int cncVersion = cncMetaData.getInt(cncVersionOffset(0));
    if (CncFileDescriptor.CNC_VERSION != cncVersion) {
        throw new IllegalStateException("Aeron CnC version does not match: version=" + cncVersion + " required=" + CNC_VERSION);
    }
    final ManyToOneRingBuffer toDriver = new ManyToOneRingBuffer(createToDriverBuffer(cncByteBuffer, cncMetaData));
    if (printPidOnly) {
        System.out.println(pid(cncMetaData));
    } else {
        System.out.println("Command `n Control file %s" + cncFile);
        System.out.format("Version: %d, PID: %d%n", cncVersion, pid(cncMetaData));
        printDateActivityAndStartTimestamps(startTimestamp(cncMetaData), toDriver.consumerHeartbeatTime());
    }
}
Also used : DirectBuffer(org.agrona.DirectBuffer) MappedByteBuffer(java.nio.MappedByteBuffer) ManyToOneRingBuffer(org.agrona.concurrent.ringbuffer.ManyToOneRingBuffer) File(java.io.File)

Example 4 with DirectBuffer

use of org.agrona.DirectBuffer in project aeron by real-logic.

the class AuthenticationTest method launchService.

private void launchService(final MutableLong sessionId, final MutableReference<byte[]> encodedPrincipal, final AtomicLong msgCounter) {
    final ClusteredService service = new StubClusteredService() {

        private int counterValue = 0;

        public void onSessionOpen(final ClientSession session, final long timestampMs) {
            sessionId.value = session.id();
            encodedPrincipal.set(session.encodedPrincipal());
        }

        public void onSessionMessage(final long clusterSessionId, final long correlationId, final long timestampMs, final DirectBuffer buffer, final int offset, final int length, final Header header) {
            assertThat(buffer.getInt(offset), is(counterValue));
            msgCounter.getAndIncrement();
            counterValue++;
        }
    };
    container = null;
    container = ClusteredServiceContainer.launch(new ClusteredServiceContainer.Context().clusteredService(service).errorHandler(Throwable::printStackTrace).deleteDirOnStart(true));
}
Also used : DirectBuffer(org.agrona.DirectBuffer) Header(io.aeron.logbuffer.Header) ClientSession(io.aeron.cluster.service.ClientSession) ClusteredService(io.aeron.cluster.service.ClusteredService) ClusteredServiceContainer(io.aeron.cluster.service.ClusteredServiceContainer)

Example 5 with DirectBuffer

use of org.agrona.DirectBuffer in project aeron by real-logic.

the class ClusterNodeTest method shouldScheduleEventInService.

@Test(timeout = 10_000)
public void shouldScheduleEventInService() {
    container = launchTimedService();
    aeronCluster = connectToCluster();
    final Aeron aeron = aeronCluster.context().aeron();
    final SessionDecorator sessionDecorator = new SessionDecorator(aeronCluster.clusterSessionId());
    final Publication publication = aeronCluster.ingressPublication();
    final ExpandableArrayBuffer msgBuffer = new ExpandableArrayBuffer();
    final long msgCorrelationId = aeron.nextCorrelationId();
    final String msg = "Hello World!";
    msgBuffer.putStringWithoutLengthAscii(0, msg);
    while (sessionDecorator.offer(publication, msgCorrelationId, msgBuffer, 0, msg.length()) < 0) {
        TestUtil.checkInterruptedStatus();
        Thread.yield();
    }
    final MutableInteger messageCount = new MutableInteger();
    final EgressAdapter adapter = new EgressAdapter(new StubEgressListener() {

        public void onMessage(final long correlationId, final long clusterSessionId, final long timestamp, final DirectBuffer buffer, final int offset, final int length, final Header header) {
            assertThat(correlationId, is(msgCorrelationId));
            assertThat(buffer.getStringWithoutLengthAscii(offset, length), is(msg + "-scheduled"));
            messageCount.value += 1;
        }
    }, aeronCluster.egressSubscription(), FRAGMENT_LIMIT);
    while (messageCount.get() == 0) {
        if (adapter.poll() <= 0) {
            TestUtil.checkInterruptedStatus();
            Thread.yield();
        }
    }
}
Also used : DirectBuffer(org.agrona.DirectBuffer) Header(io.aeron.logbuffer.Header) MutableInteger(org.agrona.collections.MutableInteger) Publication(io.aeron.Publication) SessionDecorator(io.aeron.cluster.client.SessionDecorator) Aeron(io.aeron.Aeron) ExpandableArrayBuffer(org.agrona.ExpandableArrayBuffer) EgressAdapter(io.aeron.cluster.client.EgressAdapter) Test(org.junit.Test)

Aggregations

DirectBuffer (org.agrona.DirectBuffer)102 Header (io.aeron.logbuffer.Header)18 MutableDirectBuffer (org.agrona.MutableDirectBuffer)16 UnsafeBuffer (org.agrona.concurrent.UnsafeBuffer)15 File (java.io.File)14 MappedByteBuffer (java.nio.MappedByteBuffer)11 Test (org.junit.Test)11 MediaDriver (io.aeron.driver.MediaDriver)10 CountersReader (org.agrona.concurrent.status.CountersReader)10 ClusteredService (io.aeron.cluster.service.ClusteredService)8 Publication (io.aeron.Publication)7 ClientSession (io.aeron.cluster.service.ClientSession)7 ExpandableArrayBuffer (org.agrona.ExpandableArrayBuffer)7 ThreadingMode (io.aeron.driver.ThreadingMode)6 FragmentHandler (io.aeron.logbuffer.FragmentHandler)6 ByteBuffer (java.nio.ByteBuffer)6 ClusteredServiceContainer (io.aeron.cluster.service.ClusteredServiceContainer)5 InterruptAfter (io.aeron.test.InterruptAfter)5 ExtendWith (org.junit.jupiter.api.extension.ExtendWith)5 LogBufferDescriptor (io.aeron.logbuffer.LogBufferDescriptor)4