use of io.aeron.ExclusivePublication 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 io.aeron.ExclusivePublication in project Aeron by real-logic.
the class ClusterTimerTest method launchReschedulingService.
private void launchReschedulingService(final AtomicLong triggeredTimersCounter) {
final ClusteredService service = new StubClusteredService() {
private int timerId = 1;
public void onTimerEvent(final long correlationId, final long timestamp) {
triggeredTimersCounter.getAndIncrement();
scheduleNext(serviceCorrelationId(timerId++), timestamp + INTERVAL_MS);
}
public void onStart(final Cluster cluster, final Image snapshotImage) {
super.onStart(cluster, snapshotImage);
this.cluster = cluster;
if (null != snapshotImage) {
final FragmentHandler fragmentHandler = (buffer, offset, length, header) -> timerId = buffer.getInt(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(SIZE_OF_INT);
buffer.putInt(0, timerId);
idleStrategy.reset();
while (snapshotPublication.offer(buffer, 0, SIZE_OF_INT) < 0) {
idleStrategy.idle();
}
}
public void onNewLeadershipTermEvent(final long leadershipTermId, final long logPosition, final long timestamp, final long termBaseLogPosition, final int leaderMemberId, final int logSessionId, final TimeUnit timeUnit, final int appVersion) {
scheduleNext(serviceCorrelationId(timerId++), timestamp + INTERVAL_MS);
}
private void scheduleNext(final long correlationId, final long deadlineMs) {
idleStrategy.reset();
while (!cluster.scheduleTimer(correlationId, deadlineMs)) {
idleStrategy.idle();
}
}
};
container = ClusteredServiceContainer.launch(new ClusteredServiceContainer.Context().clusteredService(service).terminationHook(ClusterTests.NOOP_TERMINATION_HOOK).errorHandler(ClusterTests.errorHandler(0)));
}
use of io.aeron.ExclusivePublication in project Aeron by real-logic.
the class ArchiveDeleteAndRestartTest method recordAndReplayExclusivePublication.
@InterruptAfter(10)
@Test
public void recordAndReplayExclusivePublication() {
final UnsafeBuffer buffer = new UnsafeBuffer(new byte[1024]);
buffer.setMemory(0, buffer.capacity(), (byte) 'z');
AeronArchive aeronArchive = AeronArchive.connect(new AeronArchive.Context().aeron(client));
final String uri = "aeron:ipc?term-length=16m|init-term-id=502090867|term-offset=0|term-id=502090867";
final ExclusivePublication recordedPublication1 = client.addExclusivePublication(uri, STREAM_ID);
final long subscriptionId = aeronArchive.startRecording(uri, STREAM_ID, SourceLocation.LOCAL);
for (int i = 0; i < 10; i++) {
while (recordedPublication1.offer(buffer, 0, 1024) < 0) {
Tests.yieldingIdle("Failed to offer data");
}
}
final long position1 = recordedPublication1.position();
final RecordingDescriptorCollector collector = new RecordingDescriptorCollector(10);
while (aeronArchive.listRecordings(0, Integer.MAX_VALUE, collector.reset()) < 1) {
Tests.yieldingIdle("Didn't find recording");
}
while (position1 != aeronArchive.getRecordingPosition(collector.descriptors().get(0).recordingId())) {
Tests.yieldingIdle("Failed to record data");
}
recordedPublication1.close();
aeronArchive.stopRecording(subscriptionId);
while (position1 != aeronArchive.getStopPosition(collector.descriptors().get(0).recordingId())) {
Tests.yieldingIdle("Failed to stop recording");
}
aeronArchive.close();
archive.close();
archive.context().deleteDirectory();
archive = Archive.launch(archiveContext.clone());
aeronArchive = AeronArchive.connect(new AeronArchive.Context().aeron(client));
final ExclusivePublication recordedPublication2 = client.addExclusivePublication(uri, STREAM_ID);
aeronArchive.startRecording(uri, STREAM_ID, SourceLocation.LOCAL);
for (int i = 0; i < 10; i++) {
while (recordedPublication2.offer(buffer, 0, 1024) < 0) {
Tests.yieldingIdle("Failed to offer data");
}
}
while (aeronArchive.listRecordings(0, Integer.MAX_VALUE, collector.reset()) < 1) {
Tests.yieldingIdle("Didn't find recording");
}
assertEquals(1, aeronArchive.listRecordings(0, Integer.MAX_VALUE, collector.reset()), collector.descriptors()::toString);
}
use of io.aeron.ExclusivePublication in project aeron by real-logic.
the class EmbeddedExclusiveSpiedThroughput method main.
/**
* Main method for launching the process.
*
* @param args passed to the process.
* @throws InterruptedException if interrupted during linger.
*/
public static void main(final String[] args) throws InterruptedException {
loadPropertiesFiles(args);
final RateReporter reporter = new RateReporter(TimeUnit.SECONDS.toNanos(1), EmbeddedExclusiveSpiedThroughput::printRate);
final ExecutorService executor = Executors.newFixedThreadPool(2);
final AtomicBoolean running = new AtomicBoolean(true);
final MediaDriver.Context ctx = new MediaDriver.Context().spiesSimulateConnection(true);
try (MediaDriver mediaDriver = MediaDriver.launch(ctx);
Aeron aeron = Aeron.connect(new Aeron.Context().aeronDirectoryName(mediaDriver.aeronDirectoryName()));
Subscription subscription = aeron.addSubscription(CommonContext.SPY_PREFIX + CHANNEL, STREAM_ID);
ExclusivePublication publication = aeron.addExclusivePublication(CHANNEL, STREAM_ID)) {
executor.execute(reporter);
executor.execute(() -> SamplesUtil.subscriberLoop(rateReporterHandler(reporter), FRAGMENT_COUNT_LIMIT, running).accept(subscription));
final ContinueBarrier barrier = new ContinueBarrier("Execute again?");
final IdleStrategy idleStrategy = SampleConfiguration.newIdleStrategy();
do {
System.out.format("%nStreaming %,d messages of payload length %d bytes to %s on stream id %d%n", NUMBER_OF_MESSAGES, MESSAGE_LENGTH, CHANNEL, STREAM_ID);
printingActive = true;
long backPressureCount = 0;
for (long i = 0; i < NUMBER_OF_MESSAGES; i++) {
OFFER_BUFFER.putLong(0, i);
idleStrategy.reset();
while (publication.offer(OFFER_BUFFER, 0, MESSAGE_LENGTH, null) < 0) {
backPressureCount++;
idleStrategy.idle();
}
}
System.out.println("Done streaming. backPressureRatio=" + ((double) backPressureCount / NUMBER_OF_MESSAGES));
if (LINGER_TIMEOUT_MS > 0) {
System.out.println("Lingering for " + LINGER_TIMEOUT_MS + " milliseconds...");
Thread.sleep(LINGER_TIMEOUT_MS);
}
printingActive = false;
} while (barrier.await());
running.set(false);
reporter.halt();
executor.shutdown();
}
}
use of io.aeron.ExclusivePublication in project aeron by real-logic.
the class EmbeddedRecordingThroughput method streamMessagesForRecording.
private long streamMessagesForRecording() {
try (ExclusivePublication publication = aeron.addExclusivePublication(CHANNEL, STREAM_ID)) {
final IdleStrategy idleStrategy = YieldingIdleStrategy.INSTANCE;
while (!publication.isConnected()) {
idleStrategy.idle();
}
final long startNs = System.nanoTime();
final UnsafeBuffer buffer = this.buffer;
for (long i = 0; i < NUMBER_OF_MESSAGES; i++) {
buffer.putLong(0, i);
idleStrategy.reset();
while (publication.offer(buffer, 0, MESSAGE_LENGTH) < 0) {
idleStrategy.idle();
}
}
final long stopPosition = publication.position();
final CountersReader counters = aeron.countersReader();
final int counterId = RecordingPos.findCounterIdBySession(counters, publication.sessionId());
idleStrategy.reset();
while (counters.getCounterValue(counterId) < stopPosition) {
idleStrategy.idle();
}
final long durationMs = TimeUnit.NANOSECONDS.toMillis(System.nanoTime() - startNs);
final double dataRate = (stopPosition * 1000.0d / durationMs) / MEGABYTE;
final double recordingMb = stopPosition / MEGABYTE;
final long msgRate = (NUMBER_OF_MESSAGES / durationMs) * 1000L;
System.out.printf("Recorded %.02f MB @ %.02f MB/s - %,d msg/sec - %d byte payload + 32 byte header%n", recordingMb, dataRate, msgRate, MESSAGE_LENGTH);
return RecordingPos.getRecordingId(counters, counterId);
}
}
Aggregations