use of org.agrona.concurrent.ringbuffer.ManyToOneRingBuffer in project aeron by real-logic.
the class IpcPublicationTest method setUp.
@SuppressWarnings("unchecked")
@BeforeEach
public void setUp() {
final RingBuffer toDriverCommands = new ManyToOneRingBuffer(new UnsafeBuffer(ByteBuffer.allocate(Configuration.CONDUCTOR_BUFFER_LENGTH_DEFAULT)));
final UnsafeBuffer counterBuffer = new UnsafeBuffer(ByteBuffer.allocate(BUFFER_LENGTH));
final UnsafeBuffer metaDataBuffer = new UnsafeBuffer(ByteBuffer.allocate(Configuration.countersMetadataBufferLength(BUFFER_LENGTH)));
final CountersManager countersManager = new CountersManager(metaDataBuffer, counterBuffer, StandardCharsets.US_ASCII);
final SystemCounters systemCounters = new SystemCounters(countersManager);
final MediaDriver.Context ctx = new MediaDriver.Context().tempBuffer(new UnsafeBuffer(new byte[METADATA_LENGTH])).ipcTermBufferLength(TERM_BUFFER_LENGTH).toDriverCommands(toDriverCommands).logFactory(new TestLogFactory()).clientProxy(mock(ClientProxy.class)).driverCommandQueue(mock(ManyToOneConcurrentArrayQueue.class)).epochClock(SystemEpochClock.INSTANCE).cachedEpochClock(new CachedEpochClock()).cachedNanoClock(new CachedNanoClock()).countersManager(countersManager).systemCounters(systemCounters).nameResolver(DefaultNameResolver.INSTANCE).nanoClock(new CachedNanoClock());
ctx.countersValuesBuffer(counterBuffer);
driverProxy = new DriverProxy(toDriverCommands, CLIENT_ID);
driverConductor = new DriverConductor(ctx);
driverConductor.onStart();
driverProxy.addPublication(CommonContext.IPC_CHANNEL, STREAM_ID);
driverConductor.doWork();
ipcPublication = driverConductor.getSharedIpcPublication(STREAM_ID);
publisherLimit = new UnsafeBufferPosition(counterBuffer, ipcPublication.publisherLimitId());
}
use of org.agrona.concurrent.ringbuffer.ManyToOneRingBuffer 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.concurrent.ringbuffer.ManyToOneRingBuffer in project Aeron by real-logic.
the class CommonContext method isDriverActive.
/**
* Is a media driver active in the current Aeron directory?
*
* @param driverTimeoutMs for the driver liveness check
* @param logHandler for feedback as liveness checked
* @return true if a driver is active or false if not
*/
public boolean isDriverActive(final long driverTimeoutMs, final Consumer<String> logHandler) {
final File dirFile = new File(aeronDirectoryName);
if (dirFile.exists() && dirFile.isDirectory()) {
final File cncFile = new File(aeronDirectoryName, CncFileDescriptor.CNC_FILE);
logHandler.accept(String.format("INFO: Aeron directory %s exists", dirFile));
if (cncFile.exists()) {
MappedByteBuffer cncByteBuffer = null;
logHandler.accept(String.format("INFO: Aeron CnC file %s exists", cncFile));
try {
cncByteBuffer = IoUtil.mapExistingFile(cncFile, CncFileDescriptor.CNC_FILE);
final UnsafeBuffer cncMetaDataBuffer = CncFileDescriptor.createMetaDataBuffer(cncByteBuffer);
final int cncVersion = cncMetaDataBuffer.getInt(CncFileDescriptor.cncVersionOffset(0));
if (CncFileDescriptor.CNC_VERSION != cncVersion) {
throw new IllegalStateException("aeron cnc file version not understood: version=" + cncVersion);
}
final ManyToOneRingBuffer toDriverBuffer = new ManyToOneRingBuffer(CncFileDescriptor.createToDriverBuffer(cncByteBuffer, cncMetaDataBuffer));
final long timestamp = toDriverBuffer.consumerHeartbeatTime();
final long now = System.currentTimeMillis();
final long diff = now - timestamp;
logHandler.accept(String.format("INFO: Aeron toDriver consumer heartbeat is %d ms old", diff));
if (diff <= driverTimeoutMs) {
return true;
}
} catch (final Exception ex) {
LangUtil.rethrowUnchecked(ex);
} finally {
IoUtil.unmap(cncByteBuffer);
}
}
}
return false;
}
use of org.agrona.concurrent.ringbuffer.ManyToOneRingBuffer in project aeron by real-logic.
the class CommonContext method isDriverActive.
/**
* Is a media driver active in the current mapped CnC buffer? If the driver is mid start then it will wait for
* up to the driverTimeoutMs by checking for the cncVersion being set.
*
* @param driverTimeoutMs for the driver liveness check.
* @param logger for feedback as liveness checked.
* @param cncByteBuffer for the existing CnC file.
* @return true if a driver is active or false if not.
*/
public static boolean isDriverActive(final long driverTimeoutMs, final Consumer<String> logger, final MappedByteBuffer cncByteBuffer) {
if (null == cncByteBuffer) {
return false;
}
final UnsafeBuffer cncMetaDataBuffer = CncFileDescriptor.createMetaDataBuffer(cncByteBuffer);
final long startTimeMs = System.currentTimeMillis();
int cncVersion;
while (0 == (cncVersion = cncMetaDataBuffer.getIntVolatile(CncFileDescriptor.cncVersionOffset(0)))) {
if (System.currentTimeMillis() > (startTimeMs + driverTimeoutMs)) {
throw new DriverTimeoutException("CnC file is created but not initialised.");
}
sleep(1);
}
if (CNC_VERSION != cncVersion) {
throw new IllegalStateException("Aeron CnC version does not match: required=" + CNC_VERSION + " version=" + cncVersion);
}
final ManyToOneRingBuffer toDriverBuffer = new ManyToOneRingBuffer(CncFileDescriptor.createToDriverBuffer(cncByteBuffer, cncMetaDataBuffer));
final long timestamp = toDriverBuffer.consumerHeartbeatTime();
final long now = System.currentTimeMillis();
final long timestampAge = now - timestamp;
logger.accept("INFO: Aeron toDriver consumer heartbeat is (ms): " + timestampAge);
return timestampAge <= driverTimeoutMs;
}
use of org.agrona.concurrent.ringbuffer.ManyToOneRingBuffer in project Aeron by real-logic.
the class DriverEventLogger method logResolve.
/**
* Log a resolution for a resolver and the associated result.
*
* @param code representing the event type
* @param resolverName simple class name of the resolver
* @param name host name being resolved
* @param address address that was resolved to, can be null
*/
public void logResolve(final DriverEventCode code, final String resolverName, final String name, final InetAddress address) {
final int length = trailingStringLength(resolverName, MAX_HOST_NAME_LENGTH) + trailingStringLength(name, MAX_HOST_NAME_LENGTH) + inetAddressLength(address);
final int encodedLength = encodedLength(length);
final ManyToOneRingBuffer ringBuffer = this.ringBuffer;
final int index = ringBuffer.tryClaim(toEventCodeId(code), encodedLength);
if (index > 0) {
try {
encodeResolve((UnsafeBuffer) ringBuffer.buffer(), index, length, length, resolverName, name, address);
} finally {
ringBuffer.commit(index);
}
}
}
Aggregations