use of org.agrona.collections.MutableInteger in project aeron by real-logic.
the class StatusUtil method receiveChannelStatus.
/**
* Return the read-only status indicator for the given receive channel URI.
*
* @param countersReader that holds the status indicator.
* @param channel for the receive channel.
* @return read-only status indicator that can be used to query the status of the receive channel or null.
* @see ChannelEndpointStatus for status values and indications.
*/
public static StatusIndicatorReader receiveChannelStatus(final CountersReader countersReader, final String channel) {
StatusIndicatorReader statusReader = null;
final MutableInteger id = new MutableInteger(-1);
countersReader.forEach((counterId, typeId, keyBuffer, label) -> {
if (typeId == ReceiveChannelStatus.RECEIVE_CHANNEL_STATUS_TYPE_ID) {
if (channel.startsWith(keyBuffer.getStringAscii(ChannelEndpointStatus.CHANNEL_OFFSET))) {
id.value = counterId;
}
}
});
if (Aeron.NULL_VALUE != id.value) {
statusReader = new UnsafeBufferStatusIndicator(countersReader.valuesBuffer(), id.value);
}
return statusReader;
}
use of org.agrona.collections.MutableInteger in project aeron by real-logic.
the class MultiDriverTest method shouldJoinExistingIdleStreamWithLockStepSendingReceiving.
@Test
@InterruptAfter(10)
void shouldJoinExistingIdleStreamWithLockStepSendingReceiving() throws InterruptedException {
final int numMessagesToSendPreJoin = 0;
final int numMessagesToSendPostJoin = NUM_MESSAGES_PER_TERM;
launch();
subscriptionA = clientA.addSubscription(MULTICAST_URI, STREAM_ID);
publication = clientA.addPublication(MULTICAST_URI, STREAM_ID);
while (!publication.isConnected() && !subscriptionA.isConnected()) {
Tests.yield();
}
final CountDownLatch newImageLatch = new CountDownLatch(1);
subscriptionB = clientB.addSubscription(MULTICAST_URI, STREAM_ID, (image) -> newImageLatch.countDown(), null);
newImageLatch.await();
for (int i = 0; i < numMessagesToSendPostJoin; i++) {
while (publication.offer(buffer, 0, buffer.capacity()) < 0L) {
Tests.yield();
}
final MutableInteger fragmentsRead = new MutableInteger();
Tests.executeUntil(() -> fragmentsRead.get() > 0, (j) -> {
fragmentsRead.value += subscriptionA.poll(fragmentHandlerA, 10);
Thread.yield();
}, Integer.MAX_VALUE, TimeUnit.MILLISECONDS.toNanos(500));
fragmentsRead.set(0);
Tests.executeUntil(() -> fragmentsRead.get() > 0, (j) -> {
fragmentsRead.value += subscriptionB.poll(fragmentHandlerB, 10);
Thread.yield();
}, Integer.MAX_VALUE, TimeUnit.MILLISECONDS.toNanos(500));
}
assertEquals(numMessagesToSendPreJoin + numMessagesToSendPostJoin, fragmentCountA.value);
assertEquals(numMessagesToSendPostJoin, fragmentCountB.value);
}
use of org.agrona.collections.MutableInteger in project aeron by real-logic.
the class ExclusivePublicationTest method shouldOfferTwoBuffersFromIndependentExclusivePublications.
@ParameterizedTest
@MethodSource("channels")
@InterruptAfter(10)
void shouldOfferTwoBuffersFromIndependentExclusivePublications(final String channel) {
try (Subscription subscription = aeron.addSubscription(channel, STREAM_ID);
ExclusivePublication publicationOne = aeron.addExclusivePublication(channel, STREAM_ID);
ExclusivePublication publicationTwo = aeron.addExclusivePublication(channel, STREAM_ID)) {
final int expectedNumberOfFragments = 778;
int totalFragmentsRead = 0;
final MutableInteger messageCount = new MutableInteger();
final FragmentHandler fragmentHandler = (buffer, offset, length, header) -> {
assertEquals(MESSAGE_LENGTH + SIZE_OF_INT, length);
final int publisherId = buffer.getInt(offset);
if (1 == publisherId) {
assertEquals(Byte.MIN_VALUE, buffer.getByte(offset + SIZE_OF_INT));
} else if (2 == publisherId) {
assertEquals(Byte.MAX_VALUE, buffer.getByte(offset + SIZE_OF_INT));
} else {
fail("unknown publisherId=" + publisherId);
}
messageCount.value++;
};
Tests.awaitConnections(subscription, 2);
final UnsafeBuffer pubOneHeader = new UnsafeBuffer(new byte[SIZE_OF_INT]);
pubOneHeader.putInt(0, 1);
final UnsafeBuffer pubOnePayload = new UnsafeBuffer(new byte[MESSAGE_LENGTH]);
pubOnePayload.setMemory(0, MESSAGE_LENGTH, Byte.MIN_VALUE);
final UnsafeBuffer pubTwoHeader = new UnsafeBuffer(new byte[SIZE_OF_INT]);
pubTwoHeader.putInt(0, 2);
final UnsafeBuffer pubTwoPayload = new UnsafeBuffer(new byte[MESSAGE_LENGTH]);
pubTwoPayload.setMemory(0, MESSAGE_LENGTH, Byte.MAX_VALUE);
for (int i = 0; i < expectedNumberOfFragments; i += 2) {
while (publicationOne.offer(pubOneHeader, 0, SIZE_OF_INT, pubOnePayload, 0, MESSAGE_LENGTH) < 0L) {
Tests.yield();
totalFragmentsRead += pollFragments(subscription, fragmentHandler);
}
while (publicationTwo.offer(pubTwoHeader, 0, SIZE_OF_INT, pubTwoPayload, 0, MESSAGE_LENGTH) < 0L) {
Tests.yield();
totalFragmentsRead += pollFragments(subscription, fragmentHandler);
}
totalFragmentsRead += pollFragments(subscription, fragmentHandler);
}
do {
totalFragmentsRead += pollFragments(subscription, fragmentHandler);
} while (totalFragmentsRead < expectedNumberOfFragments);
assertEquals(expectedNumberOfFragments, messageCount.value);
}
}
use of org.agrona.collections.MutableInteger in project aeron by real-logic.
the class ExclusivePublicationTest method shouldOfferTwoBuffersFromConcurrentExclusivePublications.
@ParameterizedTest
@MethodSource("channels")
@InterruptAfter(10)
void shouldOfferTwoBuffersFromConcurrentExclusivePublications(final String channel) {
try (Subscription subscription = aeron.addSubscription(channel, STREAM_ID);
ExclusivePublication publicationOne = aeron.addExclusivePublication(channel, STREAM_ID);
ExclusivePublication publicationTwo = aeron.addExclusivePublication(channel, STREAM_ID)) {
final int expectedNumberOfFragments = 20_000;
final int fragmentsPerThread = expectedNumberOfFragments / 2;
final MutableInteger messageCount = new MutableInteger();
final FragmentHandler fragmentHandler = (buffer, offset, length, header) -> {
assertEquals(MESSAGE_LENGTH + SIZE_OF_INT, length);
final int publisherId = buffer.getInt(offset);
if (1 == publisherId) {
assertEquals(Byte.MIN_VALUE, buffer.getByte(offset + SIZE_OF_INT));
} else if (2 == publisherId) {
assertEquals(Byte.MAX_VALUE, buffer.getByte(offset + SIZE_OF_INT));
} else {
fail("unknown publisherId=" + publisherId);
}
messageCount.value++;
};
Tests.awaitConnections(subscription, 2);
final UnsafeBuffer pubOneHeader = new UnsafeBuffer(new byte[SIZE_OF_INT]);
pubOneHeader.putInt(0, 1);
final UnsafeBuffer pubOnePayload = new UnsafeBuffer(new byte[MESSAGE_LENGTH]);
pubOnePayload.setMemory(0, MESSAGE_LENGTH, Byte.MIN_VALUE);
final UnsafeBuffer pubTwoHeader = new UnsafeBuffer(new byte[SIZE_OF_INT]);
pubTwoHeader.putInt(0, 2);
final UnsafeBuffer pubTwoPayload = new UnsafeBuffer(new byte[MESSAGE_LENGTH]);
pubTwoPayload.setMemory(0, MESSAGE_LENGTH, Byte.MAX_VALUE);
final ExecutorService threadPool = Executors.newFixedThreadPool(2);
try {
final CountDownLatch latch = new CountDownLatch(2);
threadPool.submit(() -> {
latch.countDown();
latch.await();
for (int count = 0; count < fragmentsPerThread; count++) {
while (publicationOne.offer(pubOneHeader, 0, SIZE_OF_INT, pubOnePayload, 0, MESSAGE_LENGTH) < 0L) {
Tests.yield();
}
}
return null;
});
threadPool.submit(() -> {
latch.countDown();
latch.await();
for (int count = 0; count < fragmentsPerThread; count++) {
while (publicationTwo.offer(pubTwoHeader, 0, SIZE_OF_INT, pubTwoPayload, 0, MESSAGE_LENGTH) < 0L) {
Tests.yield();
}
}
return null;
});
threadPool.shutdown();
int totalFragmentsRead = 0;
do {
totalFragmentsRead += pollFragments(subscription, fragmentHandler);
} while (totalFragmentsRead < expectedNumberOfFragments);
} finally {
threadPool.shutdownNow();
}
assertEquals(expectedNumberOfFragments, messageCount.value);
}
}
use of org.agrona.collections.MutableInteger in project aeron by real-logic.
the class MaxFlowControlStrategySystemTest method shouldTimeoutImageWhenBehindForTooLongWithMaxMulticastFlowControlStrategy.
@Test
@InterruptAfter(10)
void shouldTimeoutImageWhenBehindForTooLongWithMaxMulticastFlowControlStrategy() {
final int numMessagesToSend = NUM_MESSAGES_PER_TERM * 3;
driverBContext.imageLivenessTimeoutNs(TimeUnit.MILLISECONDS.toNanos(500));
driverAContext.multicastFlowControlSupplier(new MaxMulticastFlowControlSupplier());
launch();
subscriptionA = clientA.addSubscription(MULTICAST_URI, STREAM_ID);
subscriptionB = clientB.addSubscription(MULTICAST_URI, STREAM_ID);
publication = clientA.addPublication(MULTICAST_URI, STREAM_ID);
while (!subscriptionA.isConnected() || !subscriptionB.isConnected() || !publication.isConnected()) {
Tests.yield();
}
final MutableInteger fragmentsRead = new MutableInteger();
for (int i = 0; i < numMessagesToSend; i++) {
while (publication.offer(buffer, 0, buffer.capacity()) < 0L) {
Tests.yield();
}
fragmentsRead.set(0);
// A keeps up
Tests.executeUntil(() -> fragmentsRead.get() > 0, (j) -> {
fragmentsRead.value += subscriptionA.poll(fragmentHandlerA, 10);
Thread.yield();
}, Integer.MAX_VALUE, TimeUnit.MILLISECONDS.toNanos(500));
fragmentsRead.set(0);
// B receives slowly and eventually can't keep up
if (i % 10 == 0) {
Tests.executeUntil(() -> fragmentsRead.get() > 0, (j) -> {
fragmentsRead.value += subscriptionB.poll(fragmentHandlerB, 1);
Thread.yield();
}, Integer.MAX_VALUE, TimeUnit.MILLISECONDS.toNanos(500));
}
}
verify(fragmentHandlerA, times(numMessagesToSend)).onFragment(any(DirectBuffer.class), anyInt(), eq(MESSAGE_LENGTH), any(Header.class));
verify(fragmentHandlerB, atMost(numMessagesToSend - 1)).onFragment(any(DirectBuffer.class), anyInt(), eq(MESSAGE_LENGTH), any(Header.class));
}
Aggregations