use of org.agrona.BitUtil.SIZE_OF_INT in project aeron by real-logic.
the class ClusterNodeRestartTest method launchReschedulingService.
private void launchReschedulingService(final AtomicLong triggeredTimersCounter) {
final ClusteredService service = new StubClusteredService() {
public void onSessionMessage(final ClientSession session, final long timestamp, final DirectBuffer buffer, final int offset, final int length, final Header header) {
scheduleNext(serviceCorrelationId(7), timestamp + 200);
}
public void onTimerEvent(final long correlationId, final long timestamp) {
triggeredTimersCounter.getAndIncrement();
scheduleNext(correlationId, timestamp + 200);
}
public void onStart(final Cluster cluster, final Image snapshotImage) {
super.onStart(cluster, snapshotImage);
if (null != snapshotImage) {
final FragmentHandler fragmentHandler = (buffer, offset, length, header) -> triggeredTimersCounter.set(buffer.getLong(offset));
while (true) {
final int fragments = snapshotImage.poll(fragmentHandler, 1);
if (fragments == 1 || snapshotImage.isEndOfStream()) {
break;
}
idleStrategy.idle();
}
}
}
public void onTakeSnapshot(final ExclusivePublication snapshotPublication) {
final ExpandableArrayBuffer buffer = new ExpandableArrayBuffer();
buffer.putLong(0, triggeredTimersCounter.get());
while (snapshotPublication.offer(buffer, 0, SIZE_OF_INT) < 0) {
idleStrategy.idle();
}
}
private void scheduleNext(final long correlationId, final long deadline) {
idleStrategy.reset();
while (!cluster.scheduleTimer(correlationId, deadline)) {
idleStrategy.idle();
}
}
};
container = ClusteredServiceContainer.launch(new ClusteredServiceContainer.Context().clusteredService(service).terminationHook(ClusterTests.NOOP_TERMINATION_HOOK).errorHandler(ClusterTests.errorHandler(0)));
}
use of org.agrona.BitUtil.SIZE_OF_INT in project aeron by real-logic.
the class ClusterNodeRestartTest method launchService.
private void launchService(final AtomicLong msgCounter) {
final ClusteredService service = new StubClusteredService() {
private int nextCorrelationId = 0;
private int counterValue = 0;
public void onStart(final Cluster cluster, final Image snapshotImage) {
super.onStart(cluster, snapshotImage);
if (null != snapshotImage) {
final FragmentHandler fragmentHandler = (buffer, offset, length, header) -> {
nextCorrelationId = buffer.getInt(offset);
offset += SIZE_OF_INT;
counterValue = buffer.getInt(offset);
offset += SIZE_OF_INT;
serviceState.set(buffer.getStringAscii(offset));
};
while (true) {
final int fragments = snapshotImage.poll(fragmentHandler, 1);
if (fragments == 1 || snapshotImage.isEndOfStream()) {
break;
}
idleStrategy.idle();
}
}
}
public void onSessionMessage(final ClientSession session, final long timestamp, final DirectBuffer buffer, final int offset, final int length, final Header header) {
final int sentValue = buffer.getInt(offset + MESSAGE_VALUE_OFFSET);
assertEquals(counterValue, sentValue);
counterValue++;
serviceState.set(Integer.toString(counterValue));
msgCounter.getAndIncrement();
if (TIMER_MESSAGE_LENGTH == length) {
final long correlationId = serviceCorrelationId(nextCorrelationId++);
final long deadlineMs = timestamp + buffer.getLong(offset + TIMER_MESSAGE_DELAY_OFFSET);
while (!cluster.scheduleTimer(correlationId, deadlineMs)) {
idleStrategy.idle();
}
}
}
public void onTakeSnapshot(final ExclusivePublication snapshotPublication) {
final ExpandableArrayBuffer buffer = new ExpandableArrayBuffer();
int length = 0;
buffer.putInt(length, nextCorrelationId);
length += SIZE_OF_INT;
buffer.putInt(length, counterValue);
length += SIZE_OF_INT;
length += buffer.putStringAscii(length, Integer.toString(counterValue));
snapshotPublication.offer(buffer, 0, length);
}
};
container = ClusteredServiceContainer.launch(new ClusteredServiceContainer.Context().clusteredService(service).terminationHook(ClusterTests.NOOP_TERMINATION_HOOK).errorHandler(ClusterTests.errorHandler(0)));
}
Aggregations