use of org.agrona.DirectBuffer in project Aeron by real-logic.
the class ClusterTest method injectAdminResponseEgressListener.
private MutableBoolean injectAdminResponseEgressListener(final long expectedCorrelationId, final AdminRequestType expectedRequestType, final AdminResponseCode expectedResponseCode, final String expectedMessage) {
final MutableBoolean responseReceived = new MutableBoolean();
cluster.egressListener(new EgressListener() {
public void onMessage(final long clusterSessionId, final long timestamp, final DirectBuffer buffer, final int offset, final int length, final Header header) {
}
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 org.agrona.DirectBuffer in project Aeron by real-logic.
the class MultiClusteredServicesTest method shouldSupportMultipleServicesPerNode.
@Test
@InterruptAfter(20)
public void shouldSupportMultipleServicesPerNode() {
final List<TestCluster.NodeContext> nodeContexts = new ArrayList<>();
final List<TestCluster.ServiceContext> serviceContexts = new ArrayList<>();
final List<ClusteredMediaDriver> clusteredMediaDrivers = new ArrayList<>();
final List<ClusteredServiceContainer> clusteredServiceContainers = new ArrayList<>();
nodeContexts.add(TestCluster.nodeContext(0, true));
nodeContexts.add(TestCluster.nodeContext(1, true));
nodeContexts.add(TestCluster.nodeContext(2, true));
serviceContexts.add(TestCluster.serviceContext(0, 0, nodeContexts.get(0), ServiceA::new));
serviceContexts.add(TestCluster.serviceContext(0, 1, nodeContexts.get(0), ServiceB::new));
serviceContexts.add(TestCluster.serviceContext(1, 0, nodeContexts.get(1), ServiceA::new));
serviceContexts.add(TestCluster.serviceContext(1, 1, nodeContexts.get(1), ServiceB::new));
serviceContexts.add(TestCluster.serviceContext(2, 0, nodeContexts.get(2), ServiceA::new));
serviceContexts.add(TestCluster.serviceContext(2, 1, nodeContexts.get(2), ServiceB::new));
nodeContexts.forEach((context) -> {
try {
clusteredMediaDrivers.add(ClusteredMediaDriver.launch(context.mediaDriverCtx, context.archiveCtx, context.consensusModuleCtx));
} finally {
systemTestWatcher.dataCollector().add(context.mediaDriverCtx.aeronDirectory());
systemTestWatcher.dataCollector().add(context.archiveCtx.archiveDir());
systemTestWatcher.dataCollector().add(context.consensusModuleCtx.clusterDir());
}
});
serviceContexts.forEach((context) -> {
context.serviceContainerCtx.aeronDirectoryName(context.aeronCtx.aeronDirectoryName());
try {
clusteredServiceContainers.add(ClusteredServiceContainer.launch(context.serviceContainerCtx));
} finally {
systemTestWatcher.dataCollector().add(context.serviceContainerCtx.clusterDir());
}
});
final String aeronDirName = CommonContext.getAeronDirectoryName();
final MediaDriver clientMediaDriver = MediaDriver.launch(new MediaDriver.Context().threadingMode(ThreadingMode.SHARED).dirDeleteOnStart(true).aeronDirectoryName(aeronDirName).nameResolver(new RedirectingNameResolver(TestCluster.DEFAULT_NODE_MAPPINGS)));
final AeronCluster client = AeronCluster.connect(new AeronCluster.Context().aeronDirectoryName(aeronDirName).ingressChannel(CommonContext.UDP_CHANNEL).ingressEndpoints(TestCluster.ingressEndpoints(0, 3)));
try {
final DirectBuffer buffer = new UnsafeBuffer(new byte[100]);
while (client.offer(buffer, 0, 100) < 0) {
Tests.yield();
}
Tests.awaitValue(serviceAMessageCount, 3);
Tests.awaitValue(serviceBMessageCount, 3);
} finally {
CloseHelper.closeAll(client, clientMediaDriver);
clusteredMediaDrivers.forEach((clusteredMediaDriver) -> clusteredMediaDriver.consensusModule().close());
CloseHelper.closeAll(clusteredServiceContainers);
CloseHelper.closeAll(clusteredMediaDrivers);
clientMediaDriver.context().deleteDirectory();
}
}
use of org.agrona.DirectBuffer in project Aeron by real-logic.
the class FragmentedMessageTest method shouldReceivePublishedMessage.
@Theory
@Test(timeout = 10000)
public void shouldReceivePublishedMessage(final String channel, final ThreadingMode threadingMode) throws Exception {
final MediaDriver.Context ctx = new MediaDriver.Context();
ctx.threadingMode(threadingMode);
final FragmentAssembler adapter = new FragmentAssembler(mockFragmentHandler);
try (MediaDriver ignore = MediaDriver.launch(ctx);
Aeron aeron = Aeron.connect();
Publication publication = aeron.addPublication(channel, STREAM_ID);
Subscription subscription = aeron.addSubscription(channel, STREAM_ID)) {
final UnsafeBuffer srcBuffer = new UnsafeBuffer(new byte[ctx.mtuLength() * 4]);
final int offset = 0;
final int length = srcBuffer.capacity() / 4;
for (int i = 0; i < 4; i++) {
srcBuffer.setMemory(i * length, length, (byte) (65 + i));
}
while (publication.offer(srcBuffer, offset, srcBuffer.capacity()) < 0L) {
Thread.yield();
}
final int expectedFragmentsBecauseOfHeader = 5;
int numFragments = 0;
do {
numFragments += subscription.poll(adapter, FRAGMENT_COUNT_LIMIT);
} while (numFragments < expectedFragmentsBecauseOfHeader);
final ArgumentCaptor<DirectBuffer> bufferArg = ArgumentCaptor.forClass(DirectBuffer.class);
final ArgumentCaptor<Header> headerArg = ArgumentCaptor.forClass(Header.class);
verify(mockFragmentHandler, times(1)).onFragment(bufferArg.capture(), eq(offset), eq(srcBuffer.capacity()), headerArg.capture());
final DirectBuffer capturedBuffer = bufferArg.getValue();
for (int i = 0; i < srcBuffer.capacity(); i++) {
assertThat("same at i=" + i, capturedBuffer.getByte(i), is(srcBuffer.getByte(i)));
}
assertThat(headerArg.getValue().flags(), is(END_FRAG_FLAG));
} finally {
ctx.deleteAeronDirectory();
}
}
use of org.agrona.DirectBuffer in project Aeron by real-logic.
the class AeronStat method mapCounters.
public static CountersReader mapCounters() {
final File cncFile = CommonContext.newDefaultCncFile();
System.out.println("Command `n Control file " + cncFile);
final MappedByteBuffer cncByteBuffer = IoUtil.mapExistingFile(cncFile, "cnc");
final DirectBuffer cncMetaData = createMetaDataBuffer(cncByteBuffer);
final int cncVersion = cncMetaData.getInt(cncVersionOffset(0));
if (CncFileDescriptor.CNC_VERSION != cncVersion) {
throw new IllegalStateException("CnC version not supported: file version=" + cncVersion);
}
return new CountersReader(createCountersMetaDataBuffer(cncByteBuffer, cncMetaData), createCountersValuesBuffer(cncByteBuffer, cncMetaData));
}
use of org.agrona.DirectBuffer in project nd4j by deeplearning4j.
the class BaseTransport method sendCommandToShard.
/**
* This command is possible to issue only from Client
*
* @param message
*/
protected synchronized void sendCommandToShard(VoidMessage message) {
// TODO: we want LocalTransport to be used in such cases
if (nodeRole == NodeRole.SHARD) {
message.setTargetId(shardIndex);
messages.add(message);
return;
}
// log.info("Sending CS: {}", message.getClass().getCanonicalName());
message.setTargetId(targetIndex);
DirectBuffer buffer = message.asUnsafeBuffer();
long result = publicationForShards.offer(buffer);
if (result < 0)
for (int i = 0; i < 5 && result < 0; i++) {
try {
// TODO: make this configurable
Thread.sleep(1000);
} catch (Exception e) {
}
result = publicationForShards.offer(buffer);
}
if (result < 0)
throw new RuntimeException("Unable to send message over the wire. Error code: " + result);
}
Aggregations