use of org.agrona.DirectBuffer in project aeron by real-logic.
the class DriverTool method main.
/**
* Main method for launching the process.
*
* @param args passed to the process.
*/
public static void main(final String[] args) {
boolean printPidOnly = false;
boolean terminateDriver = false;
if (0 != args.length) {
checkForHelp(args);
if (args[0].equals("pid")) {
printPidOnly = true;
} else if (args[0].equals("terminate")) {
terminateDriver = true;
}
}
final File cncFile = CommonContext.newDefaultCncFile();
final MappedByteBuffer cncByteBuffer = IoUtil.mapExistingFile(cncFile, "cnc");
final DirectBuffer cncMetaData = createMetaDataBuffer(cncByteBuffer);
final int cncVersion = cncMetaData.getInt(cncVersionOffset(0));
checkVersion(cncVersion);
final ManyToOneRingBuffer toDriver = new ManyToOneRingBuffer(createToDriverBuffer(cncByteBuffer, cncMetaData));
if (printPidOnly) {
System.out.println(pid(cncMetaData));
} else if (terminateDriver) {
final DriverProxy driverProxy = new DriverProxy(toDriver, toDriver.nextCorrelationId());
if (!driverProxy.terminateDriver(null, 0, 0)) {
throw new AeronException("could not send termination request.");
}
} else {
System.out.println("Command `n Control file: " + cncFile);
System.out.format("Version: %d, PID: %d%n", cncVersion, pid(cncMetaData));
printDateActivityAndStartTimestamps(startTimestampMs(cncMetaData), toDriver.consumerHeartbeatTime());
}
}
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);
}
use of org.agrona.DirectBuffer in project nd4j by deeplearning4j.
the class RoutedTransport method sendCoordinationCommand.
/**
* This method implements Shard -> Shards comms
*
* @param message
*/
@Override
protected void sendCoordinationCommand(VoidMessage message) {
// log.info("Sending [{}] to all Shards...", message.getClass().getSimpleName());
message.setOriginatorId(this.originatorId);
// if we're the only shard - we just put message into the queue
if (nodeRole == NodeRole.SHARD && voidConfiguration.getNumberOfShards() == 1) {
try {
messages.put(message);
return;
} catch (Exception e) {
throw new RuntimeException(e);
}
}
final DirectBuffer buffer = message.asUnsafeBuffer();
// TODO: check which approach is faster, lambda, direct roll through list, or queue approach
shards.parallelStream().forEach((rc) -> {
RetransmissionHandler.TransmissionStatus res;
long retr = 0;
boolean delivered = false;
long address = HashUtil.getLongHash(rc.getIp() + ":" + rc.getPort());
if (originatorId == address) {
// this is local delivery
try {
messages.put(message);
} catch (Exception e) {
throw new RuntimeException(e);
}
return;
}
// log.info("Trying to send [{}] to {}", message.getClass().getSimpleName(), address);
while (!delivered) {
synchronized (rc.locker) {
res = RetransmissionHandler.getTransmissionStatus(rc.getPublication().offer(buffer));
}
switch(res) {
case NOT_CONNECTED:
{
if (!rc.getActivated().get()) {
retr++;
if (retr > 20)
throw new ND4JIllegalStateException("Can't connect to Shard: [" + rc.getPublication().channel() + "]");
try {
Thread.sleep(voidConfiguration.getRetransmitTimeout());
} catch (Exception e) {
}
} else {
throw new ND4JIllegalStateException("Shards reassignment is to be implemented yet");
}
}
break;
case ADMIN_ACTION:
case BACKPRESSURE:
{
try {
Thread.sleep(voidConfiguration.getRetransmitTimeout());
} catch (Exception e) {
}
}
break;
case MESSAGE_SENT:
delivered = true;
rc.getActivated().set(true);
break;
}
if (!delivered)
log.info("Attempting to resend message");
}
});
}
Aggregations