Search in sources :

Example 11 with ThreadingMode

use of io.aeron.driver.ThreadingMode in project Aeron by real-logic.

the class MultiModuleSharedDriverTest method shouldSupportTwoSingleNodeClusters.

@Test
@InterruptAfter(20)
@SuppressWarnings({ "try", "methodlength" })
public void shouldSupportTwoSingleNodeClusters() {
    final MediaDriver.Context driverCtx = new MediaDriver.Context().threadingMode(ThreadingMode.SHARED).nameResolver(new RedirectingNameResolver(TestCluster.DEFAULT_NODE_MAPPINGS)).dirDeleteOnShutdown(false).dirDeleteOnStart(true);
    final Archive.Context archiveCtx = new Archive.Context().threadingMode(ArchiveThreadingMode.SHARED).archiveDir(new File(SystemUtil.tmpDirName(), "archive")).recordingEventsEnabled(false).deleteArchiveOnStart(true);
    try (ArchivingMediaDriver ignore = ArchivingMediaDriver.launch(driverCtx, archiveCtx)) {
        final ConsensusModule.Context moduleCtx0 = new ConsensusModule.Context().clusterId(0).deleteDirOnStart(true).clusterDir(new File(SystemUtil.tmpDirName(), "cluster-0-0")).logChannel("aeron:ipc?term-length=64k").logStreamId(100).serviceStreamId(104).consensusModuleStreamId(105).ingressChannel("aeron:udp?endpoint=localhost:9020").replicationChannel("aeron:udp?endpoint=localhost:0");
        final ClusteredServiceContainer.Context containerCtx0 = new ClusteredServiceContainer.Context().clusterId(moduleCtx0.clusterId()).clusteredService(new EchoService()).clusterDir(moduleCtx0.clusterDir()).serviceStreamId(moduleCtx0.serviceStreamId()).consensusModuleStreamId(moduleCtx0.consensusModuleStreamId());
        final ConsensusModule.Context moduleCtx1 = new ConsensusModule.Context().clusterId(1).deleteDirOnStart(true).clusterDir(new File(SystemUtil.tmpDirName(), "cluster-0-1")).logChannel("aeron:ipc?term-length=64k").logStreamId(200).serviceStreamId(204).consensusModuleStreamId(205).ingressChannel("aeron:udp?endpoint=localhost:9021").replicationChannel("aeron:udp?endpoint=localhost:0");
        final ClusteredServiceContainer.Context containerCtx1 = new ClusteredServiceContainer.Context().clusteredService(new EchoService()).clusterDir(moduleCtx1.clusterDir()).serviceStreamId(moduleCtx1.serviceStreamId()).consensusModuleStreamId(moduleCtx1.consensusModuleStreamId()).clusterId(moduleCtx1.clusterId());
        ConsensusModule consensusModule0 = null;
        ClusteredServiceContainer container0 = null;
        ConsensusModule consensusModule1 = null;
        ClusteredServiceContainer container1 = null;
        AeronCluster client0 = null;
        AeronCluster client1 = null;
        try {
            consensusModule0 = ConsensusModule.launch(moduleCtx0);
            consensusModule1 = ConsensusModule.launch(moduleCtx1);
            container0 = ClusteredServiceContainer.launch(containerCtx0);
            container1 = ClusteredServiceContainer.launch(containerCtx1);
            final MutableReference<String> egress = new MutableReference<>();
            final EgressListener egressListener = (clusterSessionId, timestamp, buffer, offset, length, header) -> egress.set(buffer.getStringWithoutLengthAscii(offset, length));
            client0 = AeronCluster.connect(new AeronCluster.Context().egressListener(egressListener).ingressChannel(moduleCtx0.ingressChannel()).egressChannel("aeron:udp?endpoint=localhost:0"));
            client1 = AeronCluster.connect(new AeronCluster.Context().egressListener(egressListener).ingressChannel(moduleCtx1.ingressChannel()).egressChannel("aeron:udp?endpoint=localhost:0"));
            echoMessage(client0, "Message 0", egress);
            echoMessage(client1, "Message 1", egress);
        } finally {
            systemTestWatcher.dataCollector().add(moduleCtx0.clusterDir());
            systemTestWatcher.dataCollector().add(moduleCtx1.clusterDir());
            CloseHelper.closeAll(client0, client1, consensusModule0, consensusModule1, container0, container1);
        }
    } finally {
        systemTestWatcher.dataCollector().add(driverCtx.aeronDirectory());
        systemTestWatcher.dataCollector().add(archiveCtx.archiveDir());
    }
}
Also used : CommonContext(io.aeron.CommonContext) AeronCluster(io.aeron.cluster.client.AeronCluster) StubClusteredService(io.aeron.test.cluster.StubClusteredService) ClusteredServiceContainer(io.aeron.cluster.service.ClusteredServiceContainer) io.aeron.test(io.aeron.test) SystemUtil(org.agrona.SystemUtil) RedirectingNameResolver(io.aeron.test.driver.RedirectingNameResolver) MutableReference(org.agrona.collections.MutableReference) ExtendWith(org.junit.jupiter.api.extension.ExtendWith) ArchivingMediaDriver(io.aeron.archive.ArchivingMediaDriver) RegisterExtension(org.junit.jupiter.api.extension.RegisterExtension) Assertions.assertEquals(org.junit.jupiter.api.Assertions.assertEquals) ArchiveThreadingMode(io.aeron.archive.ArchiveThreadingMode) CloseHelper(org.agrona.CloseHelper) MediaDriver(io.aeron.driver.MediaDriver) Archive(io.aeron.archive.Archive) ExpandableArrayBuffer(org.agrona.ExpandableArrayBuffer) CommonContext(io.aeron.CommonContext) File(java.io.File) Test(org.junit.jupiter.api.Test) EgressListener(io.aeron.cluster.client.EgressListener) Header(io.aeron.logbuffer.Header) ClientSession(io.aeron.cluster.service.ClientSession) ThreadingMode(io.aeron.driver.ThreadingMode) TestCluster(io.aeron.test.cluster.TestCluster) DirectBuffer(org.agrona.DirectBuffer) Archive(io.aeron.archive.Archive) AeronCluster(io.aeron.cluster.client.AeronCluster) MutableReference(org.agrona.collections.MutableReference) EgressListener(io.aeron.cluster.client.EgressListener) ArchivingMediaDriver(io.aeron.archive.ArchivingMediaDriver) MediaDriver(io.aeron.driver.MediaDriver) RedirectingNameResolver(io.aeron.test.driver.RedirectingNameResolver) File(java.io.File) ArchivingMediaDriver(io.aeron.archive.ArchivingMediaDriver) ClusteredServiceContainer(io.aeron.cluster.service.ClusteredServiceContainer) Test(org.junit.jupiter.api.Test)

Example 12 with ThreadingMode

use of io.aeron.driver.ThreadingMode in project aeron by real-logic.

the class ArchiveTest method shouldRecoverRecordingWithNonZeroStartPosition.

@Test
public void shouldRecoverRecordingWithNonZeroStartPosition() {
    final MediaDriver.Context driverCtx = new MediaDriver.Context().dirDeleteOnStart(true).threadingMode(ThreadingMode.SHARED);
    final Archive.Context archiveCtx = new Archive.Context().threadingMode(SHARED);
    long resultingPosition;
    final int initialPosition = DataHeaderFlyweight.HEADER_LENGTH * 9;
    final long recordingId;
    try (ArchivingMediaDriver ignore = ArchivingMediaDriver.launch(driverCtx.clone(), archiveCtx.clone());
        AeronArchive archive = AeronArchive.connect()) {
        final int termLength = 128 * 1024;
        final int initialTermId = 29;
        final String channel = new ChannelUriStringBuilder().media(CommonContext.IPC_MEDIA).initialPosition(initialPosition, initialTermId, termLength).build();
        final Publication publication = archive.addRecordedExclusivePublication(channel, 1);
        final DirectBuffer buffer = new UnsafeBuffer("Hello World".getBytes(StandardCharsets.US_ASCII));
        while ((resultingPosition = publication.offer(buffer)) <= 0) {
            Tests.yield();
        }
        final Aeron aeron = archive.context().aeron();
        int counterId;
        final int sessionId = publication.sessionId();
        final CountersReader countersReader = aeron.countersReader();
        while (Aeron.NULL_VALUE == (counterId = RecordingPos.findCounterIdBySession(countersReader, sessionId))) {
            Tests.yield();
        }
        recordingId = RecordingPos.getRecordingId(countersReader, counterId);
        while (countersReader.getCounterValue(counterId) < resultingPosition) {
            Tests.yield();
        }
    }
    try (Catalog catalog = openCatalog(archiveCtx)) {
        final Catalog.CatalogEntryProcessor catalogEntryProcessor = (recordingDescriptorOffset, headerEncoder, headerDecoder, descriptorEncoder, descriptorDecoder) -> descriptorEncoder.stopPosition(Aeron.NULL_VALUE);
        assertTrue(catalog.forEntry(recordingId, catalogEntryProcessor));
    }
    final Archive.Context archiveCtxClone = archiveCtx.clone();
    final MediaDriver.Context driverCtxClone = driverCtx.clone();
    try (ArchivingMediaDriver ignore = ArchivingMediaDriver.launch(driverCtxClone, archiveCtxClone);
        AeronArchive archive = AeronArchive.connect()) {
        assertEquals(initialPosition, archive.getStartPosition(recordingId));
        assertEquals(resultingPosition, archive.getStopPosition(recordingId));
    } finally {
        archiveCtxClone.deleteDirectory();
        driverCtxClone.deleteDirectory();
    }
}
Also used : Context(io.aeron.archive.Archive.Context) CommonContext(io.aeron.CommonContext) RecordingSubscriptionDescriptorConsumer(io.aeron.archive.client.RecordingSubscriptionDescriptorConsumer) IntConsumer(java.util.function.IntConsumer) LOCAL(io.aeron.archive.codecs.SourceLocation.LOCAL) UnsafeBuffer(org.agrona.concurrent.UnsafeBuffer) ExtendWith(org.junit.jupiter.api.extension.ExtendWith) ChannelUriStringBuilder(io.aeron.ChannelUriStringBuilder) AeronArchive(io.aeron.archive.client.AeronArchive) DEDICATED(io.aeron.archive.ArchiveThreadingMode.DEDICATED) Publication(io.aeron.Publication) MediaDriver(io.aeron.driver.MediaDriver) ManyToOneConcurrentLinkedQueue(org.agrona.concurrent.ManyToOneConcurrentLinkedQueue) AuthorisationServiceSupplier(io.aeron.security.AuthorisationServiceSupplier) InterruptingTestCallback(io.aeron.test.InterruptingTestCallback) StandardCharsets(java.nio.charset.StandardCharsets) Executors(java.util.concurrent.Executors) Test(org.junit.jupiter.api.Test) InterruptAfter(io.aeron.test.InterruptAfter) CountDownLatch(java.util.concurrent.CountDownLatch) TruncateRecordingRequestDecoder(io.aeron.archive.codecs.TruncateRecordingRequestDecoder) EXCLUDE(org.junit.jupiter.params.provider.EnumSource.Mode.EXCLUDE) DataHeaderFlyweight(io.aeron.protocol.DataHeaderFlyweight) DirectBuffer(org.agrona.DirectBuffer) Mockito.mock(org.mockito.Mockito.mock) Tests(io.aeron.test.Tests) ThreadPoolExecutor(java.util.concurrent.ThreadPoolExecutor) CountersReader(org.agrona.concurrent.status.CountersReader) EnumSource(org.junit.jupiter.params.provider.EnumSource) ArrayList(java.util.ArrayList) SystemEpochClock(org.agrona.concurrent.SystemEpochClock) Context(io.aeron.archive.Archive.Context) FrameDescriptor(io.aeron.logbuffer.FrameDescriptor) ValueSource(org.junit.jupiter.params.provider.ValueSource) RecordingPos(io.aeron.archive.status.RecordingPos) Aeron(io.aeron.Aeron) FileStore(java.nio.file.FileStore) SHARED(io.aeron.archive.ArchiveThreadingMode.SHARED) AeronArchive.segmentFileBasePosition(io.aeron.archive.client.AeronArchive.segmentFileBasePosition) IOException(java.io.IOException) Mockito.when(org.mockito.Mockito.when) CommonContext(io.aeron.CommonContext) Checksum(io.aeron.archive.checksum.Checksum) LogBufferDescriptor(io.aeron.logbuffer.LogBufferDescriptor) File(java.io.File) TimeUnit(java.util.concurrent.TimeUnit) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest) MIN_CAPACITY(io.aeron.archive.Catalog.MIN_CAPACITY) ArchiveException(io.aeron.archive.client.ArchiveException) ThreadingMode(io.aeron.driver.ThreadingMode) Assertions(org.junit.jupiter.api.Assertions) AeronArchive(io.aeron.archive.client.AeronArchive) ChannelUriStringBuilder(io.aeron.ChannelUriStringBuilder) Publication(io.aeron.Publication) AeronArchive(io.aeron.archive.client.AeronArchive) Aeron(io.aeron.Aeron) CountersReader(org.agrona.concurrent.status.CountersReader) DirectBuffer(org.agrona.DirectBuffer) MediaDriver(io.aeron.driver.MediaDriver) UnsafeBuffer(org.agrona.concurrent.UnsafeBuffer) Context(io.aeron.archive.Archive.Context) Test(org.junit.jupiter.api.Test) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Example 13 with ThreadingMode

use of io.aeron.driver.ThreadingMode in project aeron by real-logic.

the class ArchiveTest method shouldErrorWhenUnauthorised.

@Test
@InterruptAfter(10)
public void shouldErrorWhenUnauthorised() {
    final MediaDriver.Context driverCtx = new MediaDriver.Context().dirDeleteOnStart(true).threadingMode(ThreadingMode.SHARED);
    final AuthorisationServiceSupplier authorisationServiceSupplier = () -> (protocolId, actionId, type, encodedPrincipal) -> {
        return actionId != TruncateRecordingRequestDecoder.TEMPLATE_ID;
    };
    final Archive.Context archiveCtx = new Archive.Context().deleteArchiveOnStart(true).authorisationServiceSupplier(authorisationServiceSupplier).threadingMode(SHARED);
    try (ArchivingMediaDriver ignore = ArchivingMediaDriver.launch(driverCtx, archiveCtx);
        AeronArchive archive = AeronArchive.connect()) {
        try {
            archive.truncateRecording(0, 0);
        } catch (final ArchiveException ex) {
            assertEquals(ArchiveException.UNAUTHORISED_ACTION, ex.errorCode());
            return;
        }
        fail("Expected exception");
    } finally {
        archiveCtx.deleteDirectory();
        driverCtx.deleteDirectory();
    }
}
Also used : Context(io.aeron.archive.Archive.Context) CommonContext(io.aeron.CommonContext) RecordingSubscriptionDescriptorConsumer(io.aeron.archive.client.RecordingSubscriptionDescriptorConsumer) IntConsumer(java.util.function.IntConsumer) LOCAL(io.aeron.archive.codecs.SourceLocation.LOCAL) UnsafeBuffer(org.agrona.concurrent.UnsafeBuffer) ExtendWith(org.junit.jupiter.api.extension.ExtendWith) ChannelUriStringBuilder(io.aeron.ChannelUriStringBuilder) AeronArchive(io.aeron.archive.client.AeronArchive) DEDICATED(io.aeron.archive.ArchiveThreadingMode.DEDICATED) Publication(io.aeron.Publication) MediaDriver(io.aeron.driver.MediaDriver) ManyToOneConcurrentLinkedQueue(org.agrona.concurrent.ManyToOneConcurrentLinkedQueue) AuthorisationServiceSupplier(io.aeron.security.AuthorisationServiceSupplier) InterruptingTestCallback(io.aeron.test.InterruptingTestCallback) StandardCharsets(java.nio.charset.StandardCharsets) Executors(java.util.concurrent.Executors) Test(org.junit.jupiter.api.Test) InterruptAfter(io.aeron.test.InterruptAfter) CountDownLatch(java.util.concurrent.CountDownLatch) TruncateRecordingRequestDecoder(io.aeron.archive.codecs.TruncateRecordingRequestDecoder) EXCLUDE(org.junit.jupiter.params.provider.EnumSource.Mode.EXCLUDE) DataHeaderFlyweight(io.aeron.protocol.DataHeaderFlyweight) DirectBuffer(org.agrona.DirectBuffer) Mockito.mock(org.mockito.Mockito.mock) Tests(io.aeron.test.Tests) ThreadPoolExecutor(java.util.concurrent.ThreadPoolExecutor) CountersReader(org.agrona.concurrent.status.CountersReader) EnumSource(org.junit.jupiter.params.provider.EnumSource) ArrayList(java.util.ArrayList) SystemEpochClock(org.agrona.concurrent.SystemEpochClock) Context(io.aeron.archive.Archive.Context) FrameDescriptor(io.aeron.logbuffer.FrameDescriptor) ValueSource(org.junit.jupiter.params.provider.ValueSource) RecordingPos(io.aeron.archive.status.RecordingPos) Aeron(io.aeron.Aeron) FileStore(java.nio.file.FileStore) SHARED(io.aeron.archive.ArchiveThreadingMode.SHARED) AeronArchive.segmentFileBasePosition(io.aeron.archive.client.AeronArchive.segmentFileBasePosition) IOException(java.io.IOException) Mockito.when(org.mockito.Mockito.when) CommonContext(io.aeron.CommonContext) Checksum(io.aeron.archive.checksum.Checksum) LogBufferDescriptor(io.aeron.logbuffer.LogBufferDescriptor) File(java.io.File) TimeUnit(java.util.concurrent.TimeUnit) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest) MIN_CAPACITY(io.aeron.archive.Catalog.MIN_CAPACITY) ArchiveException(io.aeron.archive.client.ArchiveException) ThreadingMode(io.aeron.driver.ThreadingMode) Assertions(org.junit.jupiter.api.Assertions) AeronArchive(io.aeron.archive.client.AeronArchive) MediaDriver(io.aeron.driver.MediaDriver) AuthorisationServiceSupplier(io.aeron.security.AuthorisationServiceSupplier) ArchiveException(io.aeron.archive.client.ArchiveException) AeronArchive(io.aeron.archive.client.AeronArchive) Context(io.aeron.archive.Archive.Context) Test(org.junit.jupiter.api.Test) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest) InterruptAfter(io.aeron.test.InterruptAfter)

Example 14 with ThreadingMode

use of io.aeron.driver.ThreadingMode in project aeron by real-logic.

the class ClusterNetworkTopologyTest method connectAndSendMessages.

private void connectAndSendMessages(final String ingressChannel, final String ingressEndpoints, final Selector selector, final double messageCount) {
    final String message = "Hello World!";
    final MutableDirectBuffer messageBuffer = new UnsafeBuffer(ByteBuffer.allocate(128));
    final int length = messageBuffer.putStringAscii(0, message);
    final MutableReference<String> egressResponse = new MutableReference<>();
    final EgressListener egressListener = (clusterSessionId, timestamp, buffer, offset, length1, header) -> {
        final String stringAscii = buffer.getStringAscii(offset);
        egressResponse.set(stringAscii);
    };
    try (MediaDriver mediaDriver = MediaDriver.launchEmbedded(new MediaDriver.Context().threadingMode(ThreadingMode.SHARED).dirDeleteOnStart(true).dirDeleteOnShutdown(true));
        AeronCluster.AsyncConnect asyncConnect = AeronCluster.asyncConnect(new AeronCluster.Context().messageTimeoutNs(TimeUnit.SECONDS.toNanos(CLUSTER_START_ELECTION_TIMEOUT_S * 2)).egressListener(egressListener).egressChannel("aeron:udp?endpoint=10.42.0.1:0").aeronDirectoryName(mediaDriver.aeronDirectoryName()).ingressChannel(ingressChannel).ingressEndpoints(ingressEndpoints));
        AeronCluster aeronCluster = pollUntilConnected(asyncConnect, selector)) {
        for (int i = 0; i < messageCount; i++) {
            Tests.await(() -> {
                final long position = aeronCluster.offer(messageBuffer, 0, length);
                pollSelector(selector);
                return 0 < position;
            }, SECONDS.toNanos(5));
            Tests.await(() -> {
                aeronCluster.pollEgress();
                pollSelector(selector);
                return message.equals(egressResponse.get());
            }, SECONDS.toNanos(5));
        }
    }
}
Also used : VirtualMachineDescriptor(com.sun.tools.attach.VirtualMachineDescriptor) AeronCluster(io.aeron.cluster.client.AeronCluster) BeforeEach(org.junit.jupiter.api.BeforeEach) java.util(java.util) io.aeron.test(io.aeron.test) Selector(java.nio.channels.Selector) UnsafeBuffer(org.agrona.concurrent.UnsafeBuffer) IoUtil(org.agrona.IoUtil) FileResolveUtil(io.aeron.test.launcher.FileResolveUtil) ByteBuffer(java.nio.ByteBuffer) CoderResult(java.nio.charset.CoderResult) MutableReference(org.agrona.collections.MutableReference) ExtendWith(org.junit.jupiter.api.extension.ExtendWith) SocketChannel(java.nio.channels.SocketChannel) RegisterExtension(org.junit.jupiter.api.extension.RegisterExtension) Duration(java.time.Duration) BasicAuctionClusterClient(io.aeron.samples.cluster.tutorial.BasicAuctionClusterClient) RemoteLaunchClient(io.aeron.test.launcher.RemoteLaunchClient) MethodSource(org.junit.jupiter.params.provider.MethodSource) EchoServiceNode(io.aeron.samples.cluster.EchoServiceNode) MediaDriver(io.aeron.driver.MediaDriver) ReadableByteChannel(java.nio.channels.ReadableByteChannel) VirtualMachine(com.sun.tools.attach.VirtualMachine) CharBuffer(java.nio.CharBuffer) SelectionKey(java.nio.channels.SelectionKey) IOException(java.io.IOException) CharsetDecoder(java.nio.charset.CharsetDecoder) CommonContext(io.aeron.CommonContext) Arguments(org.junit.jupiter.params.provider.Arguments) Collectors(java.util.stream.Collectors) File(java.io.File) StandardCharsets(java.nio.charset.StandardCharsets) Test(org.junit.jupiter.api.Test) UncheckedIOException(java.io.UncheckedIOException) TimeUnit(java.util.concurrent.TimeUnit) EgressListener(io.aeron.cluster.client.EgressListener) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest) Stream(java.util.stream.Stream) ThreadingMode(io.aeron.driver.ThreadingMode) Assertions(org.junit.jupiter.api.Assertions) ClusterConfig(io.aeron.samples.cluster.ClusterConfig) MutableDirectBuffer(org.agrona.MutableDirectBuffer) Pattern(java.util.regex.Pattern) SECONDS(java.util.concurrent.TimeUnit.SECONDS) JRE(org.junit.jupiter.api.condition.JRE) CommonContext(io.aeron.CommonContext) AeronCluster(io.aeron.cluster.client.AeronCluster) MutableReference(org.agrona.collections.MutableReference) MutableDirectBuffer(org.agrona.MutableDirectBuffer) EgressListener(io.aeron.cluster.client.EgressListener) MediaDriver(io.aeron.driver.MediaDriver) UnsafeBuffer(org.agrona.concurrent.UnsafeBuffer)

Aggregations

MediaDriver (io.aeron.driver.MediaDriver)14 ThreadingMode (io.aeron.driver.ThreadingMode)14 DirectBuffer (org.agrona.DirectBuffer)12 UnsafeBuffer (org.agrona.concurrent.UnsafeBuffer)12 ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)12 CommonContext (io.aeron.CommonContext)10 FrameDescriptor (io.aeron.logbuffer.FrameDescriptor)10 LogBufferDescriptor (io.aeron.logbuffer.LogBufferDescriptor)10 DataHeaderFlyweight (io.aeron.protocol.DataHeaderFlyweight)10 InterruptAfter (io.aeron.test.InterruptAfter)10 Tests (io.aeron.test.Tests)10 File (java.io.File)10 Test (org.junit.jupiter.api.Test)10 ExtendWith (org.junit.jupiter.api.extension.ExtendWith)10 IOException (java.io.IOException)8 StandardCharsets (java.nio.charset.StandardCharsets)8 RegisterExtension (org.junit.jupiter.api.extension.RegisterExtension)8 Aeron (io.aeron.Aeron)6 ChannelUriStringBuilder (io.aeron.ChannelUriStringBuilder)6 Publication (io.aeron.Publication)6