Search in sources :

Example 36 with AeronCluster

use of io.aeron.cluster.client.AeronCluster in project aeron by real-logic.

the class ClusterTests method startPublisherThread.

public static Thread startPublisherThread(final TestCluster testCluster, final MutableInteger messageCounter, final long backoffIntervalNs) {
    final Thread thread = new Thread(() -> {
        final IdleStrategy idleStrategy = YieldingIdleStrategy.INSTANCE;
        final AeronCluster client = testCluster.client();
        final ExpandableArrayBuffer msgBuffer = testCluster.msgBuffer();
        msgBuffer.putStringWithoutLengthAscii(0, HELLO_WORLD_MSG);
        while (!Thread.interrupted()) {
            final long result = client.offer(msgBuffer, 0, HELLO_WORLD_MSG.length());
            if (result > 0) {
                messageCounter.increment();
            } else {
                if (Publication.CLOSED == result) {
                    break;
                }
                LockSupport.parkNanos(backoffIntervalNs);
            }
            idleStrategy.idle(client.pollEgress());
        }
    });
    thread.setDaemon(true);
    thread.setName("message-thread");
    thread.start();
    return thread;
}
Also used : YieldingIdleStrategy(org.agrona.concurrent.YieldingIdleStrategy) IdleStrategy(org.agrona.concurrent.IdleStrategy) AeronCluster(io.aeron.cluster.client.AeronCluster) ExpandableArrayBuffer(org.agrona.ExpandableArrayBuffer)

Example 37 with AeronCluster

use of io.aeron.cluster.client.AeronCluster in project aeron by real-logic.

the class ClusterTest method shouldRejectTakeSnapshotRequestWithAnAuthorisationError.

@Test
@InterruptAfter(10)
void shouldRejectTakeSnapshotRequestWithAnAuthorisationError() {
    cluster = aCluster().withStaticNodes(3).start();
    systemTestWatcher.cluster(cluster);
    final TestNode leader = cluster.awaitLeader();
    final List<TestNode> followers = cluster.followers();
    final long requestCorrelationId = System.nanoTime();
    final MutableBoolean responseReceived = injectAdminResponseEgressListener(requestCorrelationId, AdminRequestType.SNAPSHOT, AdminResponseCode.UNAUTHORISED_ACCESS, "Execution of the " + AdminRequestType.SNAPSHOT + " request was not authorised");
    final AeronCluster client = cluster.connectClient();
    while (!client.sendAdminRequestToTakeASnapshot(requestCorrelationId)) {
        Tests.yield();
    }
    while (!responseReceived.get()) {
        client.pollEgress();
        Tests.yield();
    }
    long time = System.nanoTime();
    final long deadline = time + TimeUnit.SECONDS.toNanos(2);
    do {
        assertEquals(0, cluster.getSnapshotCount(leader));
        for (final TestNode follower : followers) {
            assertEquals(0, cluster.getSnapshotCount(follower));
        }
        Tests.sleep(10);
        time = System.nanoTime();
    } while (time < deadline);
}
Also used : MutableBoolean(org.agrona.collections.MutableBoolean) AeronCluster(io.aeron.cluster.client.AeronCluster) TestNode(io.aeron.test.cluster.TestNode) Test(org.junit.jupiter.api.Test)

Example 38 with AeronCluster

use of io.aeron.cluster.client.AeronCluster 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

AeronCluster (io.aeron.cluster.client.AeronCluster)38 Test (org.junit.jupiter.api.Test)30 MediaDriver (io.aeron.driver.MediaDriver)18 ExpandableArrayBuffer (org.agrona.ExpandableArrayBuffer)15 EgressListener (io.aeron.cluster.client.EgressListener)14 ClusteredServiceContainer (io.aeron.cluster.service.ClusteredServiceContainer)14 ThreadingMode (io.aeron.driver.ThreadingMode)14 DirectBuffer (org.agrona.DirectBuffer)14 ExtendWith (org.junit.jupiter.api.extension.ExtendWith)14 Archive (io.aeron.archive.Archive)12 ArchiveThreadingMode (io.aeron.archive.ArchiveThreadingMode)12 ClientSession (io.aeron.cluster.service.ClientSession)12 Header (io.aeron.logbuffer.Header)12 StubClusteredService (io.aeron.test.cluster.StubClusteredService)12 CloseHelper (org.agrona.CloseHelper)12 Assertions.assertEquals (org.junit.jupiter.api.Assertions.assertEquals)12 InterruptAfter (io.aeron.test.InterruptAfter)10 TestNode (io.aeron.test.cluster.TestNode)10 MutableBoolean (org.agrona.collections.MutableBoolean)10 BeforeEach (org.junit.jupiter.api.BeforeEach)10