Search in sources :

Example 6 with ClientSession

use of io.aeron.cluster.service.ClientSession in project Aeron by real-logic.

the class ClusterNodeRestartTest method launchService.

private void launchService(final AtomicLong msgCounter) {
    final ClusteredService service = new StubClusteredService() {

        private int nextCorrelationId = 0;

        private int counterValue = 0;

        public void onStart(final Cluster cluster, final Image snapshotImage) {
            super.onStart(cluster, snapshotImage);
            if (null != snapshotImage) {
                final FragmentHandler fragmentHandler = (buffer, offset, length, header) -> {
                    nextCorrelationId = buffer.getInt(offset);
                    offset += SIZE_OF_INT;
                    counterValue = buffer.getInt(offset);
                    offset += SIZE_OF_INT;
                    serviceState.set(buffer.getStringAscii(offset));
                };
                while (true) {
                    final int fragments = snapshotImage.poll(fragmentHandler, 1);
                    if (fragments == 1 || snapshotImage.isEndOfStream()) {
                        break;
                    }
                    idleStrategy.idle();
                }
            }
        }

        public void onSessionMessage(final ClientSession session, final long timestamp, final DirectBuffer buffer, final int offset, final int length, final Header header) {
            final int sentValue = buffer.getInt(offset + MESSAGE_VALUE_OFFSET);
            assertEquals(counterValue, sentValue);
            counterValue++;
            serviceState.set(Integer.toString(counterValue));
            msgCounter.getAndIncrement();
            if (TIMER_MESSAGE_LENGTH == length) {
                final long correlationId = serviceCorrelationId(nextCorrelationId++);
                final long deadlineMs = timestamp + buffer.getLong(offset + TIMER_MESSAGE_DELAY_OFFSET);
                while (!cluster.scheduleTimer(correlationId, deadlineMs)) {
                    idleStrategy.idle();
                }
            }
        }

        public void onTakeSnapshot(final ExclusivePublication snapshotPublication) {
            final ExpandableArrayBuffer buffer = new ExpandableArrayBuffer();
            int length = 0;
            buffer.putInt(length, nextCorrelationId);
            length += SIZE_OF_INT;
            buffer.putInt(length, counterValue);
            length += SIZE_OF_INT;
            length += buffer.putStringAscii(length, Integer.toString(counterValue));
            snapshotPublication.offer(buffer, 0, length);
        }
    };
    container = ClusteredServiceContainer.launch(new ClusteredServiceContainer.Context().clusteredService(service).terminationHook(ClusterTests.NOOP_TERMINATION_HOOK).errorHandler(ClusterTests.errorHandler(0)));
}
Also used : ClusterTests(io.aeron.test.cluster.ClusterTests) AeronCluster(io.aeron.cluster.client.AeronCluster) BeforeEach(org.junit.jupiter.api.BeforeEach) StubClusteredService(io.aeron.test.cluster.StubClusteredService) AtomicCounter(org.agrona.concurrent.status.AtomicCounter) ClusteredServiceContainer(io.aeron.cluster.service.ClusteredServiceContainer) io.aeron.test(io.aeron.test) CountersReader(org.agrona.concurrent.status.CountersReader) CLUSTER_MEMBERS(io.aeron.cluster.ClusterTestConstants.CLUSTER_MEMBERS) ClusteredService(io.aeron.cluster.service.ClusteredService) SIZE_OF_INT(org.agrona.BitUtil.SIZE_OF_INT) AtomicReference(java.util.concurrent.atomic.AtomicReference) ExtendWith(org.junit.jupiter.api.extension.ExtendWith) SIZE_OF_LONG(org.agrona.BitUtil.SIZE_OF_LONG) Publication(io.aeron.Publication) ArchiveThreadingMode(io.aeron.archive.ArchiveThreadingMode) CloseHelper(org.agrona.CloseHelper) ExclusivePublication(io.aeron.ExclusivePublication) PrintStream(java.io.PrintStream) MediaDriver(io.aeron.driver.MediaDriver) Image(io.aeron.Image) Archive(io.aeron.archive.Archive) ExpandableArrayBuffer(org.agrona.ExpandableArrayBuffer) File(java.io.File) Mockito.verify(org.mockito.Mockito.verify) Test(org.junit.jupiter.api.Test) TimeUnit(java.util.concurrent.TimeUnit) CountDownLatch(java.util.concurrent.CountDownLatch) AtomicLong(java.util.concurrent.atomic.AtomicLong) Cluster(io.aeron.cluster.service.Cluster) AfterEach(org.junit.jupiter.api.AfterEach) Header(io.aeron.logbuffer.Header) ClientSession(io.aeron.cluster.service.ClientSession) ThreadingMode(io.aeron.driver.ThreadingMode) Assertions(org.junit.jupiter.api.Assertions) INGRESS_ENDPOINTS(io.aeron.cluster.ClusterTestConstants.INGRESS_ENDPOINTS) FragmentHandler(io.aeron.logbuffer.FragmentHandler) DirectBuffer(org.agrona.DirectBuffer) Mockito.mock(org.mockito.Mockito.mock) AeronCluster(io.aeron.cluster.client.AeronCluster) Cluster(io.aeron.cluster.service.Cluster) StubClusteredService(io.aeron.test.cluster.StubClusteredService) ClusteredService(io.aeron.cluster.service.ClusteredService) ExclusivePublication(io.aeron.ExclusivePublication) Image(io.aeron.Image) ExpandableArrayBuffer(org.agrona.ExpandableArrayBuffer) DirectBuffer(org.agrona.DirectBuffer) Header(io.aeron.logbuffer.Header) FragmentHandler(io.aeron.logbuffer.FragmentHandler) ClientSession(io.aeron.cluster.service.ClientSession) StubClusteredService(io.aeron.test.cluster.StubClusteredService) ClusteredServiceContainer(io.aeron.cluster.service.ClusteredServiceContainer)

Example 7 with ClientSession

use of io.aeron.cluster.service.ClientSession in project Aeron by real-logic.

the class ClusterNodeTest method launchTimedService.

private ClusteredServiceContainer launchTimedService() {
    final ClusteredService clusteredService = new StubClusteredService() {

        private final ExpandableArrayBuffer buffer = new ExpandableArrayBuffer();

        private long clusterSessionId;

        private int nextCorrelationId;

        private String msg;

        public void onSessionMessage(final ClientSession session, final long timestamp, final DirectBuffer buffer, final int offset, final int length, final Header header) {
            clusterSessionId = session.id();
            msg = buffer.getStringWithoutLengthAscii(offset, length);
            final long correlationId = serviceCorrelationId(nextCorrelationId++);
            idleStrategy.reset();
            while (!cluster.scheduleTimer(correlationId, timestamp + 100)) {
                idleStrategy.idle();
            }
        }

        public void onTimerEvent(final long correlationId, final long timestamp) {
            final String responseMsg = msg + "-scheduled";
            buffer.putStringWithoutLengthAscii(0, responseMsg);
            final ClientSession clientSession = cluster.getClientSession(clusterSessionId);
            idleStrategy.reset();
            while (clientSession.offer(buffer, 0, responseMsg.length()) < 0) {
                idleStrategy.idle();
            }
        }
    };
    return ClusteredServiceContainer.launch(new ClusteredServiceContainer.Context().clusteredService(clusteredService).terminationHook(ClusterTests.NOOP_TERMINATION_HOOK).errorHandler(ClusterTests.errorHandler(0)));
}
Also used : DirectBuffer(org.agrona.DirectBuffer) Header(io.aeron.logbuffer.Header) ClientSession(io.aeron.cluster.service.ClientSession) StubClusteredService(io.aeron.test.cluster.StubClusteredService) ClusteredService(io.aeron.cluster.service.ClusteredService) StubClusteredService(io.aeron.test.cluster.StubClusteredService) ExpandableArrayBuffer(org.agrona.ExpandableArrayBuffer)

Example 8 with ClientSession

use of io.aeron.cluster.service.ClientSession in project aeron by real-logic.

the class ClusterNodeTest method launchTimedService.

private ClusteredServiceContainer launchTimedService() {
    final ClusteredService clusteredService = new StubClusteredService() {

        private final ExpandableArrayBuffer buffer = new ExpandableArrayBuffer();

        private long clusterSessionId;

        private int nextCorrelationId;

        private String msg;

        public void onSessionMessage(final ClientSession session, final long timestamp, final DirectBuffer buffer, final int offset, final int length, final Header header) {
            clusterSessionId = session.id();
            msg = buffer.getStringWithoutLengthAscii(offset, length);
            final long correlationId = serviceCorrelationId(nextCorrelationId++);
            idleStrategy.reset();
            while (!cluster.scheduleTimer(correlationId, timestamp + 100)) {
                idleStrategy.idle();
            }
        }

        public void onTimerEvent(final long correlationId, final long timestamp) {
            final String responseMsg = msg + "-scheduled";
            buffer.putStringWithoutLengthAscii(0, responseMsg);
            final ClientSession clientSession = cluster.getClientSession(clusterSessionId);
            idleStrategy.reset();
            while (clientSession.offer(buffer, 0, responseMsg.length()) < 0) {
                idleStrategy.idle();
            }
        }
    };
    return ClusteredServiceContainer.launch(new ClusteredServiceContainer.Context().clusteredService(clusteredService).terminationHook(ClusterTests.NOOP_TERMINATION_HOOK).errorHandler(ClusterTests.errorHandler(0)));
}
Also used : DirectBuffer(org.agrona.DirectBuffer) Header(io.aeron.logbuffer.Header) ClientSession(io.aeron.cluster.service.ClientSession) StubClusteredService(io.aeron.test.cluster.StubClusteredService) ClusteredService(io.aeron.cluster.service.ClusteredService) StubClusteredService(io.aeron.test.cluster.StubClusteredService) ExpandableArrayBuffer(org.agrona.ExpandableArrayBuffer)

Example 9 with ClientSession

use of io.aeron.cluster.service.ClientSession in project aeron by real-logic.

the class ClusterTimerTest method shouldRescheduleTimerWhenSchedulingWithExistingCorrelationId.

@Test
@InterruptAfter(10)
void shouldRescheduleTimerWhenSchedulingWithExistingCorrelationId() {
    final AtomicLong timerCounter1 = new AtomicLong();
    final AtomicLong timerCounter2 = new AtomicLong();
    final ClusteredService service = new StubClusteredService() {

        public void onSessionOpen(final ClientSession session, final long timestamp) {
            // Too far in the future
            schedule(1, timestamp + 1_000_000);
            schedule(1, timestamp + 20);
            schedule(2, timestamp + 30);
        }

        public void onTimerEvent(final long correlationId, final long timestamp) {
            if (correlationId == 1) {
                timerCounter1.incrementAndGet();
            } else {
                timerCounter2.incrementAndGet();
            }
        }

        private void schedule(final long correlationId, final long deadlineMs) {
            idleStrategy.reset();
            while (!cluster.scheduleTimer(correlationId, deadlineMs)) {
                idleStrategy.idle();
            }
        }
    };
    container = ClusteredServiceContainer.launch(new ClusteredServiceContainer.Context().clusteredService(service).terminationHook(ClusterTests.NOOP_TERMINATION_HOOK).errorHandler(ClusterTests.errorHandler(0)));
    connectClient();
    Tests.awaitValue(timerCounter2, 1);
    assertEquals(1, timerCounter1.get());
    ClusterTests.failOnClusterError();
}
Also used : AtomicLong(java.util.concurrent.atomic.AtomicLong) ClientSession(io.aeron.cluster.service.ClientSession) StubClusteredService(io.aeron.test.cluster.StubClusteredService) ClusteredService(io.aeron.cluster.service.ClusteredService) StubClusteredService(io.aeron.test.cluster.StubClusteredService) ClusteredServiceContainer(io.aeron.cluster.service.ClusteredServiceContainer) Test(org.junit.jupiter.api.Test) InterruptAfter(io.aeron.test.InterruptAfter)

Example 10 with ClientSession

use of io.aeron.cluster.service.ClientSession in project aeron by real-logic.

the class ClusterNodeRestartTest method launchService.

private void launchService(final AtomicLong msgCounter) {
    final ClusteredService service = new StubClusteredService() {

        private int nextCorrelationId = 0;

        private int counterValue = 0;

        public void onStart(final Cluster cluster, final Image snapshotImage) {
            super.onStart(cluster, snapshotImage);
            if (null != snapshotImage) {
                final FragmentHandler fragmentHandler = (buffer, offset, length, header) -> {
                    nextCorrelationId = buffer.getInt(offset);
                    offset += SIZE_OF_INT;
                    counterValue = buffer.getInt(offset);
                    offset += SIZE_OF_INT;
                    serviceState.set(buffer.getStringAscii(offset));
                };
                while (true) {
                    final int fragments = snapshotImage.poll(fragmentHandler, 1);
                    if (fragments == 1 || snapshotImage.isEndOfStream()) {
                        break;
                    }
                    idleStrategy.idle();
                }
            }
        }

        public void onSessionMessage(final ClientSession session, final long timestamp, final DirectBuffer buffer, final int offset, final int length, final Header header) {
            final int sentValue = buffer.getInt(offset + MESSAGE_VALUE_OFFSET);
            assertEquals(counterValue, sentValue);
            counterValue++;
            serviceState.set(Integer.toString(counterValue));
            msgCounter.getAndIncrement();
            if (TIMER_MESSAGE_LENGTH == length) {
                final long correlationId = serviceCorrelationId(nextCorrelationId++);
                final long deadlineMs = timestamp + buffer.getLong(offset + TIMER_MESSAGE_DELAY_OFFSET);
                while (!cluster.scheduleTimer(correlationId, deadlineMs)) {
                    idleStrategy.idle();
                }
            }
        }

        public void onTakeSnapshot(final ExclusivePublication snapshotPublication) {
            final ExpandableArrayBuffer buffer = new ExpandableArrayBuffer();
            int length = 0;
            buffer.putInt(length, nextCorrelationId);
            length += SIZE_OF_INT;
            buffer.putInt(length, counterValue);
            length += SIZE_OF_INT;
            length += buffer.putStringAscii(length, Integer.toString(counterValue));
            snapshotPublication.offer(buffer, 0, length);
        }
    };
    container = ClusteredServiceContainer.launch(new ClusteredServiceContainer.Context().clusteredService(service).terminationHook(ClusterTests.NOOP_TERMINATION_HOOK).errorHandler(ClusterTests.errorHandler(0)));
}
Also used : ClusterTests(io.aeron.test.cluster.ClusterTests) AeronCluster(io.aeron.cluster.client.AeronCluster) BeforeEach(org.junit.jupiter.api.BeforeEach) StubClusteredService(io.aeron.test.cluster.StubClusteredService) AtomicCounter(org.agrona.concurrent.status.AtomicCounter) ClusteredServiceContainer(io.aeron.cluster.service.ClusteredServiceContainer) io.aeron.test(io.aeron.test) CountersReader(org.agrona.concurrent.status.CountersReader) CLUSTER_MEMBERS(io.aeron.cluster.ClusterTestConstants.CLUSTER_MEMBERS) ClusteredService(io.aeron.cluster.service.ClusteredService) SIZE_OF_INT(org.agrona.BitUtil.SIZE_OF_INT) AtomicReference(java.util.concurrent.atomic.AtomicReference) ExtendWith(org.junit.jupiter.api.extension.ExtendWith) SIZE_OF_LONG(org.agrona.BitUtil.SIZE_OF_LONG) Publication(io.aeron.Publication) ArchiveThreadingMode(io.aeron.archive.ArchiveThreadingMode) CloseHelper(org.agrona.CloseHelper) ExclusivePublication(io.aeron.ExclusivePublication) PrintStream(java.io.PrintStream) MediaDriver(io.aeron.driver.MediaDriver) Image(io.aeron.Image) Archive(io.aeron.archive.Archive) ExpandableArrayBuffer(org.agrona.ExpandableArrayBuffer) File(java.io.File) Mockito.verify(org.mockito.Mockito.verify) Test(org.junit.jupiter.api.Test) TimeUnit(java.util.concurrent.TimeUnit) CountDownLatch(java.util.concurrent.CountDownLatch) AtomicLong(java.util.concurrent.atomic.AtomicLong) Cluster(io.aeron.cluster.service.Cluster) AfterEach(org.junit.jupiter.api.AfterEach) Header(io.aeron.logbuffer.Header) ClientSession(io.aeron.cluster.service.ClientSession) ThreadingMode(io.aeron.driver.ThreadingMode) Assertions(org.junit.jupiter.api.Assertions) INGRESS_ENDPOINTS(io.aeron.cluster.ClusterTestConstants.INGRESS_ENDPOINTS) FragmentHandler(io.aeron.logbuffer.FragmentHandler) DirectBuffer(org.agrona.DirectBuffer) Mockito.mock(org.mockito.Mockito.mock) AeronCluster(io.aeron.cluster.client.AeronCluster) Cluster(io.aeron.cluster.service.Cluster) StubClusteredService(io.aeron.test.cluster.StubClusteredService) ClusteredService(io.aeron.cluster.service.ClusteredService) ExclusivePublication(io.aeron.ExclusivePublication) Image(io.aeron.Image) ExpandableArrayBuffer(org.agrona.ExpandableArrayBuffer) DirectBuffer(org.agrona.DirectBuffer) Header(io.aeron.logbuffer.Header) FragmentHandler(io.aeron.logbuffer.FragmentHandler) ClientSession(io.aeron.cluster.service.ClientSession) StubClusteredService(io.aeron.test.cluster.StubClusteredService) ClusteredServiceContainer(io.aeron.cluster.service.ClusteredServiceContainer)

Aggregations

ClientSession (io.aeron.cluster.service.ClientSession)11 ClusteredService (io.aeron.cluster.service.ClusteredService)11 StubClusteredService (io.aeron.test.cluster.StubClusteredService)10 Header (io.aeron.logbuffer.Header)9 DirectBuffer (org.agrona.DirectBuffer)9 ClusteredServiceContainer (io.aeron.cluster.service.ClusteredServiceContainer)8 AtomicLong (java.util.concurrent.atomic.AtomicLong)6 ExpandableArrayBuffer (org.agrona.ExpandableArrayBuffer)6 Test (org.junit.jupiter.api.Test)6 ExclusivePublication (io.aeron.ExclusivePublication)4 Image (io.aeron.Image)4 Publication (io.aeron.Publication)4 Archive (io.aeron.archive.Archive)4 ArchiveThreadingMode (io.aeron.archive.ArchiveThreadingMode)4 CLUSTER_MEMBERS (io.aeron.cluster.ClusterTestConstants.CLUSTER_MEMBERS)4 INGRESS_ENDPOINTS (io.aeron.cluster.ClusterTestConstants.INGRESS_ENDPOINTS)4 AeronCluster (io.aeron.cluster.client.AeronCluster)4 Cluster (io.aeron.cluster.service.Cluster)4 MediaDriver (io.aeron.driver.MediaDriver)4 ThreadingMode (io.aeron.driver.ThreadingMode)4