Search in sources :

Example 46 with UnsafeBuffer

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

the class CommonContext method isDriverActive.

/**
     * Is a media driver active in the current Aeron directory?
     *
     * @param driverTimeoutMs for the driver liveness check
     * @param logHandler      for feedback as liveness checked
     * @return true if a driver is active or false if not
     */
public boolean isDriverActive(final long driverTimeoutMs, final Consumer<String> logHandler) {
    final File dirFile = new File(aeronDirectoryName);
    if (dirFile.exists() && dirFile.isDirectory()) {
        final File cncFile = new File(aeronDirectoryName, CncFileDescriptor.CNC_FILE);
        logHandler.accept(String.format("INFO: Aeron directory %s exists", dirFile));
        if (cncFile.exists()) {
            MappedByteBuffer cncByteBuffer = null;
            logHandler.accept(String.format("INFO: Aeron CnC file %s exists", cncFile));
            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 ManyToOneRingBuffer toDriverBuffer = new ManyToOneRingBuffer(CncFileDescriptor.createToDriverBuffer(cncByteBuffer, cncMetaDataBuffer));
                final long timestamp = toDriverBuffer.consumerHeartbeatTime();
                final long now = System.currentTimeMillis();
                final long diff = now - timestamp;
                logHandler.accept(String.format("INFO: Aeron toDriver consumer heartbeat is %d ms old", diff));
                if (diff <= driverTimeoutMs) {
                    return true;
                }
            } catch (final Exception ex) {
                LangUtil.rethrowUnchecked(ex);
            } finally {
                IoUtil.unmap(cncByteBuffer);
            }
        }
    }
    return false;
}
Also used : MappedByteBuffer(java.nio.MappedByteBuffer) ManyToOneRingBuffer(org.agrona.concurrent.ringbuffer.ManyToOneRingBuffer) UnsafeBuffer(org.agrona.concurrent.UnsafeBuffer) File(java.io.File)

Example 47 with UnsafeBuffer

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

the class FragmentAssemblerTest method shouldFreeSessionBuffer.

@Test
public void shouldFreeSessionBuffer() {
    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);
    assertFalse(adapter.freeSessionBuffer(SESSION_ID));
    adapter.onFragment(srcBuffer, offset, length, header);
    adapter.onFragment(srcBuffer, length, length, header);
    assertTrue(adapter.freeSessionBuffer(SESSION_ID));
    assertFalse(adapter.freeSessionBuffer(SESSION_ID));
}
Also used : UnsafeBuffer(org.agrona.concurrent.UnsafeBuffer) Test(org.junit.Test)

Example 48 with UnsafeBuffer

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

the class FragmentAssemblerTest method shouldDoNotingIfMidArrivesWithoutBegin.

@Test
public void shouldDoNotingIfMidArrivesWithoutBegin() {
    when(header.flags()).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() / 2;
    adapter.onFragment(srcBuffer, offset, length, header);
    adapter.onFragment(srcBuffer, offset, length, header);
    verify(delegateFragmentHandler, never()).onFragment(any(), anyInt(), anyInt(), any());
}
Also used : UnsafeBuffer(org.agrona.concurrent.UnsafeBuffer) Test(org.junit.Test)

Example 49 with UnsafeBuffer

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

the class DataHeaderFlyweight method createDefaultHeader.

/**
     * Return an initialised default Data Frame Header.
     *
     * @param sessionId for the header
     * @param streamId  for the header
     * @param termId    for the header
     * @return byte array containing the header
     */
public static UnsafeBuffer createDefaultHeader(final int sessionId, final int streamId, final int termId) {
    final UnsafeBuffer buffer = new UnsafeBuffer(new byte[HEADER_LENGTH]);
    buffer.putByte(VERSION_FIELD_OFFSET, CURRENT_VERSION);
    buffer.putByte(FLAGS_FIELD_OFFSET, (byte) BEGIN_AND_END_FLAGS);
    buffer.putShort(TYPE_FIELD_OFFSET, (short) HDR_TYPE_DATA, LITTLE_ENDIAN);
    buffer.putInt(SESSION_ID_FIELD_OFFSET, sessionId, LITTLE_ENDIAN);
    buffer.putInt(STREAM_ID_FIELD_OFFSET, streamId, LITTLE_ENDIAN);
    buffer.putInt(TERM_ID_FIELD_OFFSET, termId, LITTLE_ENDIAN);
    buffer.putLong(RESERVED_VALUE_OFFSET, DEFAULT_RESERVE_VALUE);
    return buffer;
}
Also used : UnsafeBuffer(org.agrona.concurrent.UnsafeBuffer)

Example 50 with UnsafeBuffer

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

the class BufferBuilderTest method shouldGrowToMultipleOfInitialCapacity.

@Test
public void shouldGrowToMultipleOfInitialCapacity() {
    final int srcCapacity = INITIAL_CAPACITY * 5;
    final UnsafeBuffer srcBuffer = new UnsafeBuffer(new byte[srcCapacity]);
    bufferBuilder.append(srcBuffer, 0, srcBuffer.capacity());
    assertThat(bufferBuilder.limit(), is(srcCapacity));
    assertThat(bufferBuilder.capacity(), greaterThanOrEqualTo(srcCapacity));
}
Also used : UnsafeBuffer(org.agrona.concurrent.UnsafeBuffer) Test(org.junit.Test)

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