use of io.aeron.Publication in project aeron by real-logic.
the class ClusterTest method shouldRejectAnAdminRequestIfLeadershipTermIsInvalid.
@Test
@InterruptAfter(10)
void shouldRejectAnAdminRequestIfLeadershipTermIsInvalid() {
cluster = aCluster().withStaticNodes(3).start();
systemTestWatcher.cluster(cluster);
cluster.awaitLeader();
AeronCluster client = cluster.connectClient();
final long requestCorrelationId = System.nanoTime();
final long expectedLeadershipTermId = client.leadershipTermId();
final long invalidLeadershipTermId = expectedLeadershipTermId - 1000;
final AdminRequestType requestType = AdminRequestType.NULL_VAL;
final MutableBoolean responseReceived = injectAdminResponseEgressListener(requestCorrelationId, requestType, AdminResponseCode.ERROR, "Invalid leadership term: expected " + expectedLeadershipTermId + ", got " + invalidLeadershipTermId);
client = cluster.connectClient();
final AdminRequestEncoder adminRequestEncoder = new AdminRequestEncoder().wrapAndApplyHeader(cluster.msgBuffer(), 0, new MessageHeaderEncoder()).leadershipTermId(invalidLeadershipTermId).clusterSessionId(client.clusterSessionId()).correlationId(requestCorrelationId).requestType(requestType);
final Publication ingressPublication = client.ingressPublication();
while (ingressPublication.offer(adminRequestEncoder.buffer(), 0, MessageHeaderEncoder.ENCODED_LENGTH + adminRequestEncoder.encodedLength()) < 0) {
Tests.yield();
}
while (!responseReceived.get()) {
client.pollEgress();
Tests.yield();
}
}
use of io.aeron.Publication in project aeron by real-logic.
the class ControlResponseProxy method sendDescriptor.
int sendDescriptor(final long controlSessionId, final long correlationId, final UnsafeBuffer descriptorBuffer, final ControlSession session) {
final int messageLength = Catalog.descriptorLength(descriptorBuffer) + MESSAGE_HEADER_LENGTH;
final int contentLength = messageLength - recordingIdEncodingOffset() - MESSAGE_HEADER_LENGTH;
recordingDescriptorEncoder.wrapAndApplyHeader(buffer, 0, messageHeaderEncoder).controlSessionId(controlSessionId).correlationId(correlationId);
int attempts = SEND_ATTEMPTS;
final Publication publication = session.controlPublication();
do {
final long result = publication.offer(buffer, 0, DESCRIPTOR_PREFIX_LENGTH, descriptorBuffer, DESCRIPTOR_CONTENT_OFFSET, contentLength);
if (result > 0) {
return messageLength;
}
checkResult(session, result);
} while (--attempts > 0);
return 0;
}
use of io.aeron.Publication in project aeron by real-logic.
the class EmbeddedPingPong method startPong.
private static Thread startPong(final Aeron aeron) {
return new Thread(() -> {
System.out.println("Subscribing Ping at " + PING_CHANNEL + " on stream id " + PING_STREAM_ID);
System.out.println("Publishing Pong at " + PONG_CHANNEL + " on stream id " + PONG_STREAM_ID);
try (Subscription pingSubscription = aeron.addSubscription(PING_CHANNEL, PING_STREAM_ID);
Publication pongPublication = EXCLUSIVE_PUBLICATIONS ? aeron.addExclusivePublication(PONG_CHANNEL, PONG_STREAM_ID) : aeron.addPublication(PONG_CHANNEL, PONG_STREAM_ID)) {
final BufferClaim bufferClaim = new BufferClaim();
final FragmentHandler fragmentHandler = (buffer, offset, length, header) -> pingHandler(bufferClaim, pongPublication, buffer, offset, length, header);
while (RUNNING.get()) {
PING_HANDLER_IDLE_STRATEGY.idle(pingSubscription.poll(fragmentHandler, FRAME_COUNT_LIMIT));
}
System.out.println("Shutting down...");
}
});
}
use of io.aeron.Publication in project aeron by real-logic.
the class EmbeddedPingPong method runPing.
private static void runPing(final Aeron aeron) throws InterruptedException {
System.out.println("Publishing Ping at " + PING_CHANNEL + " on stream id " + PING_STREAM_ID);
System.out.println("Subscribing Pong at " + PONG_CHANNEL + " on stream id " + PONG_STREAM_ID);
System.out.println("Message payload length of " + MESSAGE_LENGTH + " bytes");
System.out.println("Using exclusive publications: " + EXCLUSIVE_PUBLICATIONS);
final FragmentAssembler dataHandler = new FragmentAssembler(EmbeddedPingPong::pongHandler);
try (Subscription pongSubscription = aeron.addSubscription(PONG_CHANNEL, PONG_STREAM_ID, EmbeddedPingPong::availablePongImageHandler, null);
Publication pingPublication = EXCLUSIVE_PUBLICATIONS ? aeron.addExclusivePublication(PING_CHANNEL, PING_STREAM_ID) : aeron.addPublication(PING_CHANNEL, PING_STREAM_ID)) {
System.out.println("Waiting for new image from Pong...");
PONG_IMAGE_LATCH.await();
System.out.format("Warming up... %d iterations of %,d messages%n", WARMUP_NUMBER_OF_ITERATIONS, WARMUP_NUMBER_OF_MESSAGES);
for (int i = 0; i < WARMUP_NUMBER_OF_ITERATIONS; i++) {
roundTripMessages(dataHandler, pingPublication, pongSubscription, WARMUP_NUMBER_OF_MESSAGES);
Thread.yield();
}
Thread.sleep(100);
final ContinueBarrier barrier = new ContinueBarrier("Execute again?");
do {
HISTOGRAM.reset();
System.out.format("Pinging %,d messages%n", NUMBER_OF_MESSAGES);
roundTripMessages(dataHandler, pingPublication, pongSubscription, NUMBER_OF_MESSAGES);
System.out.println("Histogram of RTT latencies in microseconds.");
HISTOGRAM.outputPercentileDistribution(System.out, 1000.0);
} while (barrier.await());
}
}
use of io.aeron.Publication in project aeron by real-logic.
the class MultiplePublishersWithFragmentation method main.
/**
* Main method for launching the process.
*
* @param args passed to the process.
*/
public static void main(final String[] args) {
System.out.println("Publishing to " + CHANNEL + " on stream id " + STREAM_ID_1 + " and stream id " + STREAM_ID_2);
try (Aeron aeron = Aeron.connect();
Publication publication1 = aeron.addPublication(CHANNEL, STREAM_ID_1);
Publication publication2 = aeron.addPublication(CHANNEL, STREAM_ID_2)) {
int j = 1;
int k = 1;
final String message1 = "Hello World! " + j;
BUFFER_1.putBytes(0, message1.getBytes());
final String message2 = "Hello World! " + k;
BUFFER_2.putBytes(0, message2.getBytes());
while (j <= 5000 || k <= 5000) {
boolean offerStatus1 = false;
boolean offerStatus2 = false;
long result1;
long result2;
while (!(offerStatus1 || offerStatus2)) {
if (j <= 5000) {
result1 = publication1.offer(BUFFER_1, 0, BUFFER_1.capacity());
if (result1 > 0) {
j++;
offerStatus1 = true;
System.out.println("Successfully sent data on stream id " + STREAM_ID_1 + " and data length " + BUFFER_1.capacity() + " at offset " + result1);
} else {
if (result1 == Publication.BACK_PRESSURED) {
System.out.println(" Offer failed due to back pressure for stream id " + STREAM_ID_1);
} else if (result1 == Publication.NOT_CONNECTED) {
System.out.println(" Offer failed because publisher is not yet " + "connected to subscriber for stream id " + STREAM_ID_1);
} else {
System.out.println(" Offer failed due to unexpected reason: " + result1);
}
offerStatus1 = false;
}
}
if (k <= 5000) {
result2 = publication2.offer(BUFFER_2, 0, BUFFER_2.capacity());
if (result2 > 0) {
k++;
offerStatus2 = true;
System.out.println("Successfully sent data on stream id " + STREAM_ID_2 + " and data length " + BUFFER_2.capacity() + " at offset " + result2);
} else {
if (result2 == Publication.BACK_PRESSURED) {
System.out.println(" Offer failed because publisher is not yet " + "connected to subscriber for stream id " + STREAM_ID_2);
} else if (result2 == Publication.NOT_CONNECTED) {
System.out.println("Offer failed - publisher is not yet connected to subscriber" + STREAM_ID_2);
} else {
System.out.println("Offer failed due to unexpected reason: " + result2);
}
offerStatus2 = false;
}
}
}
}
System.out.println("Done sending total messages for stream id " + STREAM_ID_1 + " = " + (j - 1) + " and stream id " + STREAM_ID_2 + " = " + (k - 1));
}
}
Aggregations