use of org.agrona.concurrent.ringbuffer.ManyToOneRingBuffer in project Aeron by real-logic.
the class DriverEventLogger method logFlowControlReceiver.
/**
* Log the information about receiver for the corresponding flow control event.
*
* @param code flow control event type.
* @param receiverId of the receiver.
* @param sessionId of the image.
* @param streamId of the image.
* @param channel uri of the channel.
* @param receiverCount number of the receivers after the event.
*/
public void logFlowControlReceiver(final DriverEventCode code, final long receiverId, final int sessionId, final int streamId, final String channel, final int receiverCount) {
final int length = SIZE_OF_INT * 4 + SIZE_OF_LONG + channel.length();
final int captureLength = captureLength(length);
final int encodedLength = encodedLength(captureLength);
final ManyToOneRingBuffer ringBuffer = this.ringBuffer;
final int index = ringBuffer.tryClaim(toEventCodeId(code), encodedLength);
if (index > 0) {
try {
encodeFlowControlReceiver((UnsafeBuffer) ringBuffer.buffer(), index, captureLength, length, receiverId, sessionId, streamId, channel, receiverCount);
} finally {
ringBuffer.commit(index);
}
}
}
use of org.agrona.concurrent.ringbuffer.ManyToOneRingBuffer in project Aeron by real-logic.
the class ClusterEventLogger method logNewLeadershipTerm.
/**
* Log a new leadership term event.
*
* @param logLeadershipTermId term for which log entries are present.
* @param nextLeadershipTermId next term relative to the logLeadershipTermId
* @param nextTermBaseLogPosition base log position for the next term.
* @param nextLogPosition committed log position for next term.
* @param leadershipTermId new leadership term id.
* @param termBaseLogPosition position the log reached at base of new term.
* @param logPosition position the log reached for the new term.
* @param leaderRecordingId of the log in the leader archive.
* @param timestamp of the new term.
* @param leaderMemberId member id for the new leader.
* @param logSessionId session id of the log extension.
* @param isStartup is the leader starting up fresh.
*/
public void logNewLeadershipTerm(final long logLeadershipTermId, final long nextLeadershipTermId, final long nextTermBaseLogPosition, final long nextLogPosition, final long leadershipTermId, final long termBaseLogPosition, final long logPosition, final long leaderRecordingId, final long timestamp, final int leaderMemberId, final int logSessionId, final boolean isStartup) {
final int length = newLeaderShipTermLength();
final int captureLength = captureLength(length);
final int encodedLength = encodedLength(captureLength);
final ManyToOneRingBuffer ringBuffer = this.ringBuffer;
final int index = ringBuffer.tryClaim(NEW_LEADERSHIP_TERM.toEventCodeId(), encodedLength);
if (index > 0) {
try {
encodeNewLeadershipTerm((UnsafeBuffer) ringBuffer.buffer(), index, captureLength, length, logLeadershipTermId, nextLeadershipTermId, nextTermBaseLogPosition, nextLogPosition, leadershipTermId, termBaseLogPosition, logPosition, leaderRecordingId, timestamp, leaderMemberId, logSessionId, isStartup);
} finally {
ringBuffer.commit(index);
}
}
}
use of org.agrona.concurrent.ringbuffer.ManyToOneRingBuffer in project Aeron by real-logic.
the class CommonContext method requestDriverTermination.
/**
* Request a driver to run its termination hook.
*
* @param directory for the driver.
* @param tokenBuffer containing the optional token for the request.
* @param tokenOffset within the tokenBuffer at which the token begins.
* @param tokenLength of the token in the tokenBuffer.
* @return true if request was sent or false if request could not be sent.
*/
public static boolean requestDriverTermination(final File directory, final DirectBuffer tokenBuffer, final int tokenOffset, final int tokenLength) {
final File cncFile = new File(directory, CncFileDescriptor.CNC_FILE);
if (cncFile.exists() && cncFile.length() > 0) {
final MappedByteBuffer cncByteBuffer = IoUtil.mapExistingFile(cncFile, "CnC file");
try {
final UnsafeBuffer cncMetaDataBuffer = CncFileDescriptor.createMetaDataBuffer(cncByteBuffer);
final int cncVersion = cncMetaDataBuffer.getIntVolatile(cncVersionOffset(0));
CncFileDescriptor.checkVersion(cncVersion);
final ManyToOneRingBuffer toDriverBuffer = new ManyToOneRingBuffer(CncFileDescriptor.createToDriverBuffer(cncByteBuffer, cncMetaDataBuffer));
final long clientId = toDriverBuffer.nextCorrelationId();
final DriverProxy driverProxy = new DriverProxy(toDriverBuffer, clientId);
return driverProxy.terminateDriver(tokenBuffer, tokenOffset, tokenLength);
} finally {
BufferUtil.free(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 starting 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 ByteBuffer 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);
}
CncFileDescriptor.checkVersion(cncVersion);
final ManyToOneRingBuffer toDriverBuffer = new ManyToOneRingBuffer(CncFileDescriptor.createToDriverBuffer(cncByteBuffer, cncMetaDataBuffer));
final long timestampMs = toDriverBuffer.consumerHeartbeatTime();
final long nowMs = System.currentTimeMillis();
final long timestampAgeMs = nowMs - timestampMs;
logger.accept("INFO: Aeron toDriver consumer heartbeat is (ms): " + timestampAgeMs);
return timestampAgeMs <= driverTimeoutMs;
}
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());
}
}
Aggregations