use of io.aeron.logbuffer.Header 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)));
}
use of io.aeron.logbuffer.Header in project Aeron by real-logic.
the class ClusterNodeTest method shouldSendResponseAfterServiceMessage.
@Test
@InterruptAfter(10)
public void shouldSendResponseAfterServiceMessage() {
final ExpandableArrayBuffer msgBuffer = new ExpandableArrayBuffer();
final String msg = "Hello World!";
msgBuffer.putStringWithoutLengthAscii(0, msg);
final MutableInteger messageCount = new MutableInteger();
final EgressListener listener = (clusterSessionId, timestamp, buffer, offset, length, header) -> {
assertEquals(msg, buffer.getStringWithoutLengthAscii(offset, length));
messageCount.value += 1;
};
container = launchServiceMessageIngressService();
aeronCluster = connectToCluster(listener);
offerMessage(msgBuffer, msg);
awaitResponse(messageCount);
ClusterTests.failOnClusterError();
}
use of io.aeron.logbuffer.Header 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.logbuffer.Header in project Aeron by real-logic.
the class ClusterTest method injectAdminRequestControlledEgressListener.
private MutableBoolean injectAdminRequestControlledEgressListener(final long expectedCorrelationId, final AdminRequestType expectedRequestType, final AdminResponseCode expectedResponseCode, final String expectedMessage) {
final MutableBoolean responseReceived = new MutableBoolean();
cluster.controlledEgressListener(new ControlledEgressListener() {
public ControlledFragmentHandler.Action onMessage(final long clusterSessionId, final long timestamp, final DirectBuffer buffer, final int offset, final int length, final Header header) {
return ControlledFragmentHandler.Action.ABORT;
}
public void onAdminResponse(final long clusterSessionId, final long correlationId, final AdminRequestType requestType, final AdminResponseCode responseCode, final String message, final DirectBuffer payload, final int payloadOffset, final int payloadLength) {
responseReceived.set(true);
assertEquals(expectedCorrelationId, correlationId);
assertEquals(expectedRequestType, requestType);
assertEquals(expectedResponseCode, responseCode);
assertEquals(expectedMessage, message);
assertNotNull(payload);
final int minPayloadOffset = MessageHeaderEncoder.ENCODED_LENGTH + AdminResponseEncoder.BLOCK_LENGTH + AdminResponseEncoder.messageHeaderLength() + message.length() + AdminResponseEncoder.payloadHeaderLength();
assertTrue(payloadOffset > minPayloadOffset);
assertEquals(0, payloadLength);
}
});
return responseReceived;
}
use of io.aeron.logbuffer.Header in project Aeron by real-logic.
the class EgressAdapterTest method onFragmentIsANoOpIfSessionIdDoesNotMatchOnSessionMessage.
@Test
void onFragmentIsANoOpIfSessionIdDoesNotMatchOnSessionMessage() {
final int offset = 18;
final long sessionId = 21;
final long timestamp = 1000;
sessionMessageHeaderEncoder.wrapAndApplyHeader(buffer, offset, messageHeaderEncoder).clusterSessionId(sessionId).timestamp(timestamp);
final EgressListener egressListener = mock(EgressListener.class);
final Header header = new Header(0, 0);
final EgressAdapter adapter = new EgressAdapter(egressListener, -19, mock(Subscription.class), 3);
adapter.onFragment(buffer, offset, sessionMessageHeaderEncoder.encodedLength(), header);
verifyNoInteractions(egressListener);
}
Aggregations