Search in sources :

Example 6 with UnsafeBuffer

use of org.agrona.concurrent.UnsafeBuffer in project Aeron by real-logic.

the class BufferBuilderTest method shouldAppendTwoBuffersAndResize.

@Test
public void shouldAppendTwoBuffersAndResize() {
    final int bufferLength = 128;
    final byte[] buffer = new byte[bufferLength];
    final int firstLength = buffer.length / 4;
    final int secondLength = buffer.length / 2;
    Arrays.fill(buffer, 0, firstLength + secondLength, (byte) 7);
    final UnsafeBuffer srcBuffer = new UnsafeBuffer(buffer);
    final BufferBuilder bufferBuilder = new BufferBuilder(bufferLength / 2);
    bufferBuilder.append(srcBuffer, 0, firstLength);
    bufferBuilder.append(srcBuffer, firstLength, secondLength);
    final byte[] temp = new byte[buffer.length];
    bufferBuilder.buffer().getBytes(0, temp, 0, secondLength + firstLength);
    assertThat(bufferBuilder.limit(), is(firstLength + secondLength));
    assertThat(bufferBuilder.capacity(), greaterThanOrEqualTo(firstLength + secondLength));
    assertArrayEquals(temp, buffer);
}
Also used : UnsafeBuffer(org.agrona.concurrent.UnsafeBuffer) Test(org.junit.Test)

Example 7 with UnsafeBuffer

use of org.agrona.concurrent.UnsafeBuffer in project Aeron by real-logic.

the class FragmentAssemblerTest method shouldAssembleFourPartMessage.

@Test
public void shouldAssembleFourPartMessage() {
    when(header.flags()).thenReturn(FrameDescriptor.BEGIN_FRAG_FLAG).thenReturn((byte) 0).thenReturn((byte) 0).thenReturn(FrameDescriptor.END_FRAG_FLAG);
    final UnsafeBuffer srcBuffer = new UnsafeBuffer(new byte[1024]);
    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));
    }
    adapter.onFragment(srcBuffer, offset, length, header);
    adapter.onFragment(srcBuffer, offset + length, length, header);
    adapter.onFragment(srcBuffer, offset + (length * 2), length, header);
    adapter.onFragment(srcBuffer, offset + (length * 3), length, header);
    final ArgumentCaptor<UnsafeBuffer> bufferArg = ArgumentCaptor.forClass(UnsafeBuffer.class);
    final ArgumentCaptor<Header> headerArg = ArgumentCaptor.forClass(Header.class);
    verify(delegateFragmentHandler, times(1)).onFragment(bufferArg.capture(), eq(offset), eq(length * 4), headerArg.capture());
    final UnsafeBuffer capturedBuffer = bufferArg.getValue();
    for (int i = 0; i < srcBuffer.capacity(); i++) {
        assertThat("same at i=" + i, capturedBuffer.getByte(i), is(srcBuffer.getByte(i)));
    }
    final Header capturedHeader = headerArg.getValue();
    assertThat(capturedHeader.sessionId(), is(SESSION_ID));
    assertThat(capturedHeader.flags(), is(FrameDescriptor.END_FRAG_FLAG));
}
Also used : Header(io.aeron.logbuffer.Header) UnsafeBuffer(org.agrona.concurrent.UnsafeBuffer) Test(org.junit.Test)

Example 8 with UnsafeBuffer

use of org.agrona.concurrent.UnsafeBuffer in project Aeron by real-logic.

the class FragmentAssemblerTest method shouldAssembleTwoPartMessage.

@Test
public void shouldAssembleTwoPartMessage() {
    when(header.flags()).thenReturn(FrameDescriptor.BEGIN_FRAG_FLAG).thenReturn(FrameDescriptor.END_FRAG_FLAG);
    final UnsafeBuffer srcBuffer = new UnsafeBuffer(new byte[1024]);
    final int offset = 0;
    final int length = srcBuffer.capacity() / 2;
    srcBuffer.setMemory(0, length, (byte) 65);
    srcBuffer.setMemory(length, length, (byte) 66);
    adapter.onFragment(srcBuffer, offset, length, header);
    adapter.onFragment(srcBuffer, length, length, header);
    final ArgumentCaptor<UnsafeBuffer> bufferArg = ArgumentCaptor.forClass(UnsafeBuffer.class);
    final ArgumentCaptor<Header> headerArg = ArgumentCaptor.forClass(Header.class);
    verify(delegateFragmentHandler, times(1)).onFragment(bufferArg.capture(), eq(offset), eq(length * 2), headerArg.capture());
    final UnsafeBuffer capturedBuffer = bufferArg.getValue();
    for (int i = 0; i < srcBuffer.capacity(); i++) {
        assertThat("same at i=" + i, capturedBuffer.getByte(i), is(srcBuffer.getByte(i)));
    }
    final Header capturedHeader = headerArg.getValue();
    assertThat(capturedHeader.sessionId(), is(SESSION_ID));
    assertThat(capturedHeader.flags(), is(FrameDescriptor.END_FRAG_FLAG));
}
Also used : Header(io.aeron.logbuffer.Header) UnsafeBuffer(org.agrona.concurrent.UnsafeBuffer) Test(org.junit.Test)

Example 9 with UnsafeBuffer

use of org.agrona.concurrent.UnsafeBuffer in project Aeron by real-logic.

the class CommonContext method saveErrorLog.

/**
     * Read the error log to a given {@link PrintStream}
     *
     * @param out to write the error log contents to.
     * @return the number of observations from the error log
     */
public int saveErrorLog(final PrintStream out) {
    final File dirFile = new File(aeronDirectoryName);
    int distinctErrorCount = 0;
    if (dirFile.exists() && dirFile.isDirectory()) {
        final File cncFile = new File(aeronDirectoryName, CncFileDescriptor.CNC_FILE);
        if (cncFile.exists()) {
            MappedByteBuffer cncByteBuffer = null;
            try {
                cncByteBuffer = IoUtil.mapExistingFile(cncFile, CncFileDescriptor.CNC_FILE);
                final UnsafeBuffer cncMetaDataBuffer = CncFileDescriptor.createMetaDataBuffer(cncByteBuffer);
                final int cncVersion = cncMetaDataBuffer.getInt(CncFileDescriptor.cncVersionOffset(0));
                if (CncFileDescriptor.CNC_VERSION != cncVersion) {
                    throw new IllegalStateException("aeron cnc file version not understood: version=" + cncVersion);
                }
                final AtomicBuffer buffer = CncFileDescriptor.createErrorLogBuffer(cncByteBuffer, cncMetaDataBuffer);
                final SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSSZ");
                distinctErrorCount = ErrorLogReader.read(buffer, (observationCount, firstObservationTimestamp, lastObservationTimestamp, encodedException) -> out.format("***%n%d observations from %s to %s for:%n %s%n", observationCount, dateFormat.format(new Date(firstObservationTimestamp)), dateFormat.format(new Date(lastObservationTimestamp)), encodedException));
                out.format("%n%d distinct errors observed.%n", distinctErrorCount);
            } catch (final Exception ex) {
                LangUtil.rethrowUnchecked(ex);
            } finally {
                IoUtil.unmap(cncByteBuffer);
            }
        }
    }
    return distinctErrorCount;
}
Also used : PrintStream(java.io.PrintStream) Date(java.util.Date) UnsafeBuffer(org.agrona.concurrent.UnsafeBuffer) SimpleDateFormat(java.text.SimpleDateFormat) UUID(java.util.UUID) IoUtil(org.agrona.IoUtil) File(java.io.File) ManyToOneRingBuffer(org.agrona.concurrent.ringbuffer.ManyToOneRingBuffer) Consumer(java.util.function.Consumer) LangUtil(org.agrona.LangUtil) System.getProperty(java.lang.System.getProperty) ErrorLogReader(org.agrona.concurrent.errors.ErrorLogReader) AtomicBuffer(org.agrona.concurrent.AtomicBuffer) MappedByteBuffer(java.nio.MappedByteBuffer) MappedByteBuffer(java.nio.MappedByteBuffer) AtomicBuffer(org.agrona.concurrent.AtomicBuffer) UnsafeBuffer(org.agrona.concurrent.UnsafeBuffer) File(java.io.File) SimpleDateFormat(java.text.SimpleDateFormat) Date(java.util.Date)

Example 10 with UnsafeBuffer

use of org.agrona.concurrent.UnsafeBuffer in project Aeron by real-logic.

the class Image method blockPoll.

/**
     * Poll for new messages in a stream. If new messages are found beyond the last consumed position then they
     * will be delivered to the {@link BlockHandler} up to a limited number of bytes.
     *
     * @param blockHandler     to which block is delivered.
     * @param blockLengthLimit up to which a block may be in length.
     * @return the number of bytes that have been consumed.
     */
public int blockPoll(final BlockHandler blockHandler, final int blockLengthLimit) {
    if (isClosed) {
        return 0;
    }
    final long position = subscriberPosition.get();
    final int termOffset = (int) position & termLengthMask;
    final UnsafeBuffer termBuffer = activeTermBuffer(position);
    final int limit = Math.min(termOffset + blockLengthLimit, termBuffer.capacity());
    final int resultingOffset = TermBlockScanner.scan(termBuffer, termOffset, limit);
    final int bytesConsumed = resultingOffset - termOffset;
    if (resultingOffset > termOffset) {
        try {
            final int termId = termBuffer.getInt(termOffset + TERM_ID_FIELD_OFFSET, LITTLE_ENDIAN);
            blockHandler.onBlock(termBuffer, termOffset, bytesConsumed, sessionId, termId);
        } catch (final Throwable t) {
            errorHandler.onError(t);
        }
        subscriberPosition.setOrdered(position + bytesConsumed);
    }
    return bytesConsumed;
}
Also used : UnsafeBuffer(org.agrona.concurrent.UnsafeBuffer)

Aggregations

UnsafeBuffer (org.agrona.concurrent.UnsafeBuffer)78 Test (org.junit.Test)42 StatusMessageFlyweight (io.aeron.protocol.StatusMessageFlyweight)8 MediaDriver (io.aeron.driver.MediaDriver)7 MutableDirectBuffer (org.agrona.MutableDirectBuffer)6 Header (io.aeron.logbuffer.Header)5 InOrder (org.mockito.InOrder)5 MappedByteBuffer (java.nio.MappedByteBuffer)4 Theory (org.junit.experimental.theories.Theory)4 ReceiveChannelEndpoint (io.aeron.driver.media.ReceiveChannelEndpoint)3 File (java.io.File)3 Date (java.util.Date)3 DirectBuffer (org.agrona.DirectBuffer)3 Before (org.junit.Before)3 DataPoint (org.junit.experimental.theories.DataPoint)3 SendChannelEndpoint (io.aeron.driver.media.SendChannelEndpoint)2 BufferClaim (io.aeron.logbuffer.BufferClaim)2 FragmentHandler (io.aeron.logbuffer.FragmentHandler)2 PrintStream (java.io.PrintStream)2 InetSocketAddress (java.net.InetSocketAddress)2