use of io.aeron.cluster.client.AeronCluster 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());
}
}
use of io.aeron.cluster.client.AeronCluster in project Aeron by real-logic.
the class ClusterTest method shouldRejectAnInvalidAdminRequest.
@Test
@InterruptAfter(10)
void shouldRejectAnInvalidAdminRequest() {
final AdminRequestType invalidRequestType = AdminRequestType.NULL_VAL;
final AtomicBoolean authorisationServiceCalled = new AtomicBoolean();
cluster = aCluster().withStaticNodes(3).withAuthorisationServiceSupplier(() -> (protocolId, actionId, type, encodedPrincipal) -> {
authorisationServiceCalled.set(true);
assertEquals(MessageHeaderDecoder.SCHEMA_ID, protocolId);
assertEquals(AdminRequestEncoder.TEMPLATE_ID, actionId);
assertEquals(invalidRequestType, type);
return true;
}).start();
systemTestWatcher.cluster(cluster);
cluster.awaitLeader();
final long requestCorrelationId = System.nanoTime();
final MutableBoolean responseReceived = injectAdminResponseEgressListener(requestCorrelationId, invalidRequestType, AdminResponseCode.ERROR, "Unknown request type: " + invalidRequestType);
final AeronCluster client = cluster.connectClient();
final AdminRequestEncoder adminRequestEncoder = new AdminRequestEncoder().wrapAndApplyHeader(cluster.msgBuffer(), 0, new MessageHeaderEncoder()).leadershipTermId(client.leadershipTermId()).clusterSessionId(client.clusterSessionId()).correlationId(requestCorrelationId).requestType(invalidRequestType);
final Publication ingressPublication = client.ingressPublication();
while (ingressPublication.offer(adminRequestEncoder.buffer(), 0, MessageHeaderEncoder.ENCODED_LENGTH + adminRequestEncoder.encodedLength()) < 0) {
Tests.yield();
}
Tests.await(authorisationServiceCalled::get);
while (!responseReceived.get()) {
client.pollEgress();
Tests.yield();
}
}
use of io.aeron.cluster.client.AeronCluster in project Aeron by real-logic.
the class ClusterTest method shouldTakeASnapshotAfterReceivingAdminRequestOfTypeSnapshot.
@Test
@InterruptAfter(20)
void shouldTakeASnapshotAfterReceivingAdminRequestOfTypeSnapshot() {
cluster = aCluster().withStaticNodes(3).withAuthorisationServiceSupplier(() -> AuthorisationService.ALLOW_ALL).start();
systemTestWatcher.cluster(cluster);
final TestNode leader = cluster.awaitLeader();
final long requestCorrelationId = System.nanoTime();
final MutableBoolean responseReceived = injectAdminResponseEgressListener(requestCorrelationId, AdminRequestType.SNAPSHOT, AdminResponseCode.OK, "");
final AeronCluster client = cluster.connectClient();
while (!client.sendAdminRequestToTakeASnapshot(requestCorrelationId)) {
Tests.yield();
}
while (!responseReceived.get()) {
client.pollEgress();
Tests.yield();
}
cluster.awaitSnapshotCount(1);
cluster.awaitNeutralControlToggle(leader);
}
use of io.aeron.cluster.client.AeronCluster in project Aeron by real-logic.
the class ClusterTest method shouldRejectAnAdminRequestIfLeadershipTermIsInvalid.
@Test
@InterruptAfter(10)
void shouldRejectAnAdminRequestIfLeadershipTermIsInvalid() {
cluster = aCluster().withStaticNodes(3).start();
systemTestWatcher.cluster(cluster);
cluster.awaitLeader();
AeronCluster client = cluster.connectClient();
final long requestCorrelationId = System.nanoTime();
final long expectedLeadershipTermId = client.leadershipTermId();
final long invalidLeadershipTermId = expectedLeadershipTermId - 1000;
final AdminRequestType requestType = AdminRequestType.NULL_VAL;
final MutableBoolean responseReceived = injectAdminResponseEgressListener(requestCorrelationId, requestType, AdminResponseCode.ERROR, "Invalid leadership term: expected " + expectedLeadershipTermId + ", got " + invalidLeadershipTermId);
client = cluster.connectClient();
final AdminRequestEncoder adminRequestEncoder = new AdminRequestEncoder().wrapAndApplyHeader(cluster.msgBuffer(), 0, new MessageHeaderEncoder()).leadershipTermId(invalidLeadershipTermId).clusterSessionId(client.clusterSessionId()).correlationId(requestCorrelationId).requestType(requestType);
final Publication ingressPublication = client.ingressPublication();
while (ingressPublication.offer(adminRequestEncoder.buffer(), 0, MessageHeaderEncoder.ENCODED_LENGTH + adminRequestEncoder.encodedLength()) < 0) {
Tests.yield();
}
while (!responseReceived.get()) {
client.pollEgress();
Tests.yield();
}
}
use of io.aeron.cluster.client.AeronCluster in project Aeron by real-logic.
the class BasicAuctionClusterClient method main.
/**
* Main method for launching the process.
*
* @param args passed to the process.
*/
public static void main(final String[] args) {
// <1>
final int customerId = Integer.parseInt(System.getProperty("aeron.cluster.tutorial.customerId"));
// <2>
final int numOfBids = Integer.parseInt(System.getProperty("aeron.cluster.tutorial.numOfBids"));
// <3>
final int bidIntervalMs = Integer.parseInt(System.getProperty("aeron.cluster.tutorial.bidIntervalMs"));
final String[] hostnames = System.getProperty("aeron.cluster.tutorial.hostnames", "localhost,localhost,localhost").split(",");
final String ingressEndpoints = ingressEndpoints(Arrays.asList(hostnames));
final BasicAuctionClusterClient client = new BasicAuctionClusterClient(customerId, numOfBids, bidIntervalMs);
// tag::connect[]
try (MediaDriver mediaDriver = MediaDriver.launchEmbedded(// <1>
new MediaDriver.Context().threadingMode(ThreadingMode.SHARED).dirDeleteOnStart(true).dirDeleteOnShutdown(true));
AeronCluster aeronCluster = AeronCluster.connect(new AeronCluster.Context().egressListener(// <2>
client).egressChannel(// <3>
"aeron:udp?endpoint=localhost:0").aeronDirectoryName(mediaDriver.aeronDirectoryName()).ingressChannel(// <4>
"aeron:udp").ingressEndpoints(// <5>
ingressEndpoints))) {
// end::connect[]
client.bidInAuction(aeronCluster);
}
}
Aggregations