use of org.agrona.concurrent.UnsafeBuffer in project Aeron by real-logic.
the class FragmentedMessageTest method shouldReceivePublishedMessage.
@Theory
@Test(timeout = 10000)
public void shouldReceivePublishedMessage(final String channel, final ThreadingMode threadingMode) throws Exception {
final MediaDriver.Context ctx = new MediaDriver.Context();
ctx.threadingMode(threadingMode);
final FragmentAssembler adapter = new FragmentAssembler(mockFragmentHandler);
try (MediaDriver ignore = MediaDriver.launch(ctx);
Aeron aeron = Aeron.connect();
Publication publication = aeron.addPublication(channel, STREAM_ID);
Subscription subscription = aeron.addSubscription(channel, STREAM_ID)) {
final UnsafeBuffer srcBuffer = new UnsafeBuffer(new byte[ctx.mtuLength() * 4]);
final int offset = 0;
final int length = srcBuffer.capacity() / 4;
for (int i = 0; i < 4; i++) {
srcBuffer.setMemory(i * length, length, (byte) (65 + i));
}
while (publication.offer(srcBuffer, offset, srcBuffer.capacity()) < 0L) {
Thread.yield();
}
final int expectedFragmentsBecauseOfHeader = 5;
int numFragments = 0;
do {
numFragments += subscription.poll(adapter, FRAGMENT_COUNT_LIMIT);
} while (numFragments < expectedFragmentsBecauseOfHeader);
final ArgumentCaptor<DirectBuffer> bufferArg = ArgumentCaptor.forClass(DirectBuffer.class);
final ArgumentCaptor<Header> headerArg = ArgumentCaptor.forClass(Header.class);
verify(mockFragmentHandler, times(1)).onFragment(bufferArg.capture(), eq(offset), eq(srcBuffer.capacity()), headerArg.capture());
final DirectBuffer capturedBuffer = bufferArg.getValue();
for (int i = 0; i < srcBuffer.capacity(); i++) {
assertThat("same at i=" + i, capturedBuffer.getByte(i), is(srcBuffer.getByte(i)));
}
assertThat(headerArg.getValue().flags(), is(END_FRAG_FLAG));
} finally {
ctx.deleteAeronDirectory();
}
}
use of org.agrona.concurrent.UnsafeBuffer in project Aeron by real-logic.
the class MultiSubscriberTest method shouldReceiveMessageOnSeparateSubscriptions.
@Test(timeout = 10000)
public void shouldReceiveMessageOnSeparateSubscriptions() throws Exception {
final MediaDriver.Context ctx = new MediaDriver.Context();
final FragmentHandler mockFragmentHandlerOne = mock(FragmentHandler.class);
final FragmentHandler mockFragmentHandlerTwo = mock(FragmentHandler.class);
final FragmentAssembler adapterOne = new FragmentAssembler(mockFragmentHandlerOne);
final FragmentAssembler adapterTwo = new FragmentAssembler(mockFragmentHandlerTwo);
try (MediaDriver ignore = MediaDriver.launch(ctx);
Aeron client = Aeron.connect(new Aeron.Context());
Publication publication = client.addPublication(CHANNEL_1, STREAM_ID);
Subscription subscriptionOne = client.addSubscription(CHANNEL_1, STREAM_ID);
Subscription subscriptionTwo = client.addSubscription(CHANNEL_2, STREAM_ID)) {
final byte[] expectedBytes = "Hello, World! here is a small message".getBytes();
final UnsafeBuffer srcBuffer = new UnsafeBuffer(expectedBytes);
assertThat(subscriptionOne.poll(adapterOne, FRAGMENT_COUNT_LIMIT), is(0));
assertThat(subscriptionTwo.poll(adapterTwo, FRAGMENT_COUNT_LIMIT), is(0));
while (publication.offer(srcBuffer) < 0L) {
Thread.yield();
}
while (subscriptionOne.poll(adapterOne, FRAGMENT_COUNT_LIMIT) == 0) {
Thread.yield();
}
while (subscriptionTwo.poll(adapterTwo, FRAGMENT_COUNT_LIMIT) == 0) {
Thread.yield();
}
verifyData(srcBuffer, mockFragmentHandlerOne);
verifyData(srcBuffer, mockFragmentHandlerTwo);
} finally {
ctx.deleteAeronDirectory();
}
}
use of org.agrona.concurrent.UnsafeBuffer in project Aeron by real-logic.
the class LossStat method main.
public static void main(final String[] args) {
final String aeronDirectoryName = getProperty(AERON_DIR_PROP_NAME, AERON_DIR_PROP_DEFAULT);
final File lossReportFile = LossReportUtil.file(aeronDirectoryName);
if (!lossReportFile.exists()) {
System.err.print("Loss report does not exist: " + lossReportFile);
System.exit(1);
}
final MappedByteBuffer mappedByteBuffer = IoUtil.mapExistingFile(lossReportFile, "Loss Report");
final AtomicBuffer buffer = new UnsafeBuffer(mappedByteBuffer);
final SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSSZ");
System.out.println("#OBSERVATION_COUNT, TOTAL_BYTES_LOST, FIRST_OBSERVATION," + " LAST_OBSERVATION, SESSION_ID, STREAM_ID, CHANNEL, SOURCE");
final int entriesRead = LossReportReader.read(buffer, (observationCount, totalBytesLost, firstObservationTimestamp, lastObservationTimestamp, sessionId, streamId, channel, source) -> System.out.format("%d,%d,%s,%s,%d,%d,%s,%s%n", observationCount, totalBytesLost, dateFormat.format(new Date(firstObservationTimestamp)), dateFormat.format(new Date(lastObservationTimestamp)), sessionId, streamId, channel, source));
System.out.println(entriesRead + " entries read");
}
use of org.agrona.concurrent.UnsafeBuffer in project Aeron by real-logic.
the class ReceiverTest method shouldCreateRcvTermAndSendSmOnSetup.
@Test(timeout = 10000)
public void shouldCreateRcvTermAndSendSmOnSetup() throws Exception {
receiverProxy.registerReceiveChannelEndpoint(receiveChannelEndpoint);
receiverProxy.addSubscription(receiveChannelEndpoint, STREAM_ID);
receiver.doWork();
fillSetupFrame(setupHeader);
receiveChannelEndpoint.onSetupMessage(setupHeader, setupBuffer, SetupFlyweight.HEADER_LENGTH, senderAddress);
final PublicationImage image = new PublicationImage(CORRELATION_ID, Configuration.IMAGE_LIVENESS_TIMEOUT_NS, receiveChannelEndpoint, senderAddress, SESSION_ID, STREAM_ID, INITIAL_TERM_ID, ACTIVE_TERM_ID, INITIAL_TERM_OFFSET, rawLog, mockFeedbackDelayGenerator, POSITIONS, mockHighestReceivedPosition, mockRebuildPosition, nanoClock, epochClock, mockSystemCounters, SOURCE_ADDRESS, congestionControl, lossReport, true);
final int messagesRead = toConductorQueue.drain((e) -> {
final CreatePublicationImageCmd cmd = (CreatePublicationImageCmd) e;
assertThat(cmd.channelEndpoint().udpChannel(), is(UDP_CHANNEL));
assertThat(cmd.streamId(), is(STREAM_ID));
assertThat(cmd.sessionId(), is(SESSION_ID));
assertThat(cmd.termId(), is(ACTIVE_TERM_ID));
// pass in new term buffer from conductor, which should trigger SM
receiverProxy.newPublicationImage(receiveChannelEndpoint, image);
});
assertThat(messagesRead, is(1));
receiver.doWork();
image.trackRebuild(currentTime + (2 * STATUS_MESSAGE_TIMEOUT), STATUS_MESSAGE_TIMEOUT);
image.sendPendingStatusMessage();
final ByteBuffer rcvBuffer = ByteBuffer.allocateDirect(256);
InetSocketAddress rcvAddress;
do {
rcvAddress = (InetSocketAddress) senderChannel.receive(rcvBuffer);
} while (null == rcvAddress);
statusHeader.wrap(new UnsafeBuffer(rcvBuffer));
assertNotNull(rcvAddress);
assertThat(rcvAddress.getPort(), is(UDP_CHANNEL.remoteData().getPort()));
assertThat(statusHeader.headerType(), is(HeaderFlyweight.HDR_TYPE_SM));
assertThat(statusHeader.streamId(), is(STREAM_ID));
assertThat(statusHeader.sessionId(), is(SESSION_ID));
assertThat(statusHeader.consumptionTermId(), is(ACTIVE_TERM_ID));
assertThat(statusHeader.frameLength(), is(StatusMessageFlyweight.HEADER_LENGTH));
}
use of org.agrona.concurrent.UnsafeBuffer in project Aeron by real-logic.
the class NetworkPublication method cleanBuffer.
private void cleanBuffer(final long publisherLimit) {
final long cleanPosition = this.cleanPosition;
final long dirtyRange = publisherLimit - cleanPosition;
final int bufferCapacity = termLengthMask + 1;
final int reservedRange = bufferCapacity * 2;
if (dirtyRange > reservedRange) {
final UnsafeBuffer dirtyTerm = termBuffers[indexByPosition(cleanPosition, positionBitsToShift)];
final int termOffset = (int) cleanPosition & termLengthMask;
final int bytesForCleaning = (int) (dirtyRange - reservedRange);
final int length = Math.min(bytesForCleaning, bufferCapacity - termOffset);
dirtyTerm.setMemory(termOffset, length, (byte) 0);
this.cleanPosition = cleanPosition + length;
}
}
Aggregations