org.agrona.BitUtil Java Examples

The following examples show how to use org.agrona.BitUtil. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: AeronUtil.java    From deeplearning4j with Apache License 2.0 6 votes vote down vote up
/**
 * Get a media driver context
 * for sending ndarrays
 * based on a given length
 * where length is the length (number of elements)
 * in the ndarrays hat are being sent
 * @param length the length to based the ipc length
 * @return the media driver context based on the given length
 */
public static MediaDriver.Context getMediaDriverContext(int length) {
    //length of array * sizeof(float)
    int ipcLength = length * 16;
    //padding for NDArrayMessage
    ipcLength += 64;
    //must be a power of 2
    ipcLength *= 2;
    //ipc length must be positive power of 2
    while (!BitUtil.isPowerOfTwo(ipcLength))
        ipcLength += 2;
    // System.setProperty("aeron.term.buffer.size",String.valueOf(ipcLength));
    final MediaDriver.Context ctx =
                    new MediaDriver.Context().threadingMode(ThreadingMode.SHARED).dirsDeleteOnStart(true)
                                    /*  .ipcTermBufferLength(ipcLength)
                                      .publicationTermBufferLength(ipcLength)
                                      .maxTermBufferLength(ipcLength)*/
                                    .conductorIdleStrategy(new BusySpinIdleStrategy())
                                    .receiverIdleStrategy(new BusySpinIdleStrategy())
                                    .senderIdleStrategy(new BusySpinIdleStrategy());
    return ctx;
}
 
Example #2
Source File: ReplayIndexDescriptor.java    From artio with Apache License 2.0 6 votes vote down vote up
static void checkIndexFileSize(final int indexFileSize)
{
    final int recordCapacity = recordCapacity(indexFileSize);
    if (!BitUtil.isPowerOfTwo(recordCapacity))
    {
        throw new IllegalStateException(
            "IndexFileSize must be a positive power of 2 + INITIAL_RECORD_OFFSET: indexFileSize=" + indexFileSize);
    }

    if ((recordCapacity % RECORD_LENGTH) != 0)
    {
        throw new IllegalStateException(
            "IndexFileSize must be a multiple of RECORD_LENGTH + INITIAL_RECORD_OFFSET: indexFileSize=" +
            indexFileSize);
    }
}
 
Example #3
Source File: HeartbeatTimestamp.java    From aeron with Apache License 2.0 6 votes vote down vote up
/**
 * Allocate a counter id for tracking the last heartbeat of an entity.
 *
 * @param tempBuffer      to be used for labels and key.
 * @param name            of the counter for the label.
 * @param typeId          of the counter for classification.
 * @param countersManager from which to allocated the underlying storage.
 * @param registrationId  to be associated with the counter.
 * @return the counter id to be used.
 */
public static int allocateCounterId(
    final MutableDirectBuffer tempBuffer,
    final String name,
    final int typeId,
    final CountersManager countersManager,
    final long registrationId)
{
    tempBuffer.putLong(REGISTRATION_ID_OFFSET, registrationId);
    final int keyLength = REGISTRATION_ID_OFFSET + SIZE_OF_LONG;

    final int labelOffset = BitUtil.align(keyLength, SIZE_OF_INT);
    int labelLength = 0;
    labelLength += tempBuffer.putStringWithoutLengthAscii(labelOffset + labelLength, name);
    labelLength += tempBuffer.putStringWithoutLengthAscii(labelOffset + labelLength, ": ");
    labelLength += tempBuffer.putLongAscii(labelOffset + labelLength, registrationId);

    return countersManager.allocate(
        typeId,
        tempBuffer,
        0,
        keyLength,
        tempBuffer,
        labelOffset,
        labelLength);
}
 
Example #4
Source File: LogBufferDescriptor.java    From aeron with Apache License 2.0 6 votes vote down vote up
/**
 * Check that term length is valid and alignment is valid.
 *
 * @param termLength to be checked.
 * @throws IllegalStateException if the length is not as expected.
 */
public static void checkTermLength(final int termLength)
{
    if (termLength < TERM_MIN_LENGTH)
    {
        throw new IllegalStateException(
            "Term length less than min length of " + TERM_MIN_LENGTH + ": length=" + termLength);
    }

    if (termLength > TERM_MAX_LENGTH)
    {
        throw new IllegalStateException(
            "Term length more than max length of " + TERM_MAX_LENGTH + ": length=" + termLength);
    }

    if (!BitUtil.isPowerOfTwo(termLength))
    {
        throw new IllegalStateException("Term length not a power of 2: length=" + termLength);
    }
}
 
Example #5
Source File: LogBufferDescriptor.java    From aeron with Apache License 2.0 6 votes vote down vote up
/**
 * Check that page size is valid and alignment is valid.
 *
 * @param pageSize to be checked.
 * @throws IllegalStateException if the size is not as expected.
 */
public static void checkPageSize(final int pageSize)
{
    if (pageSize < PAGE_MIN_SIZE)
    {
        throw new IllegalStateException(
            "Page size less than min size of " + PAGE_MIN_SIZE + ": page size=" + pageSize);
    }

    if (pageSize > PAGE_MAX_SIZE)
    {
        throw new IllegalStateException(
            "Page size more than max size of " + PAGE_MAX_SIZE + ": page size=" + pageSize);
    }

    if (!BitUtil.isPowerOfTwo(pageSize))
    {
        throw new IllegalStateException("Page size not a power of 2: page size=" + pageSize);
    }
}
 
Example #6
Source File: TermRebuilderTest.java    From aeron with Apache License 2.0 6 votes vote down vote up
@Test
public void shouldInsertLastFrameIntoBuffer()
{
    final int frameLength = BitUtil.align(256, FRAME_ALIGNMENT);
    final int srcOffset = 0;
    final int tail = TERM_BUFFER_CAPACITY - frameLength;
    final int termOffset = tail;
    final UnsafeBuffer packet = new UnsafeBuffer(ByteBuffer.allocateDirect(frameLength));
    packet.putShort(typeOffset(srcOffset), (short)PADDING_FRAME_TYPE, LITTLE_ENDIAN);
    packet.putInt(srcOffset, frameLength, LITTLE_ENDIAN);

    TermRebuilder.insert(termBuffer, termOffset, packet, frameLength);

    verify(termBuffer).putBytes(
        tail + HEADER_LENGTH, packet, srcOffset + HEADER_LENGTH, frameLength - HEADER_LENGTH);
}
 
Example #7
Source File: TermRebuilderTest.java    From aeron with Apache License 2.0 6 votes vote down vote up
@Test
public void shouldFillAfterAGap()
{
    final int frameLength = 50;
    final int alignedFrameLength = BitUtil.align(frameLength, FRAME_ALIGNMENT);
    final int srcOffset = 0;
    final UnsafeBuffer packet = new UnsafeBuffer(ByteBuffer.allocateDirect(alignedFrameLength));
    final int termOffset = alignedFrameLength * 2;

    TermRebuilder.insert(termBuffer, termOffset, packet, alignedFrameLength);

    verify(termBuffer).putBytes(
        (alignedFrameLength * 2) + HEADER_LENGTH,
        packet,
        srcOffset + HEADER_LENGTH,
        alignedFrameLength - HEADER_LENGTH);
}
 
Example #8
Source File: TermRebuilderTest.java    From aeron with Apache License 2.0 6 votes vote down vote up
@Test
public void shouldFillGapButNotMoveTailOrHwm()
{
    final int frameLength = 50;
    final int alignedFrameLength = BitUtil.align(frameLength, FRAME_ALIGNMENT);
    final int srcOffset = 0;
    final UnsafeBuffer packet = new UnsafeBuffer(ByteBuffer.allocateDirect(alignedFrameLength));
    final int termOffset = alignedFrameLength * 2;

    TermRebuilder.insert(termBuffer, termOffset, packet, alignedFrameLength);

    verify(termBuffer).putBytes(
        (alignedFrameLength * 2) + HEADER_LENGTH,
        packet,
        srcOffset + HEADER_LENGTH,
        alignedFrameLength - HEADER_LENGTH);
}
 
Example #9
Source File: TermBlockScannerTest.java    From aeron with Apache License 2.0 6 votes vote down vote up
@Test
public void shouldReadBlockOfTwoMessages()
{
    final int offset = 0;
    final int limit = termBuffer.capacity();
    final int messageLength = 50;
    final int alignedMessageLength = BitUtil.align(messageLength, FRAME_ALIGNMENT);

    when(termBuffer.getIntVolatile(lengthOffset(offset))).thenReturn(messageLength);
    when(termBuffer.getShort(typeOffset(offset))).thenReturn((short)HDR_TYPE_DATA);
    when(termBuffer.getIntVolatile(lengthOffset(alignedMessageLength))).thenReturn(messageLength);
    when(termBuffer.getShort(typeOffset(alignedMessageLength))).thenReturn((short)HDR_TYPE_DATA);

    final int newOffset = TermBlockScanner.scan(termBuffer, offset, limit);
    assertEquals(alignedMessageLength * 2, newOffset);
}
 
Example #10
Source File: TermBlockScannerTest.java    From aeron with Apache License 2.0 6 votes vote down vote up
@Test
public void shouldReadBlockOfTwoMessagesBecauseOfLimit()
{
    final int offset = 0;
    final int messageLength = 50;
    final int alignedMessageLength = BitUtil.align(messageLength, FRAME_ALIGNMENT);
    final int limit = (alignedMessageLength * 2) + 1;

    when(termBuffer.getIntVolatile(lengthOffset(offset))).thenReturn(messageLength);
    when(termBuffer.getShort(typeOffset(offset))).thenReturn((short)HDR_TYPE_DATA);
    when(termBuffer.getIntVolatile(lengthOffset(alignedMessageLength))).thenReturn(messageLength);
    when(termBuffer.getShort(typeOffset(alignedMessageLength))).thenReturn((short)HDR_TYPE_DATA);
    when(termBuffer.getIntVolatile(lengthOffset(alignedMessageLength * 2))).thenReturn(messageLength);
    when(termBuffer.getShort(typeOffset(alignedMessageLength * 2))).thenReturn((short)HDR_TYPE_DATA);

    final int newOffset = TermBlockScanner.scan(termBuffer, offset, limit);
    assertEquals(alignedMessageLength * 2, newOffset);
}
 
Example #11
Source File: SegmentInspector.java    From aeron with Apache License 2.0 6 votes vote down vote up
/**
 * Dump the contents of a segment file to a {@link PrintStream}.
 *
 * @param out              for the dumped contents.
 * @param messageDumpLimit for the number of bytes per message fragment to dump.
 * @param buffer           the wraps the segment file.
 */
public static void dumpSegment(final PrintStream out, final int messageDumpLimit, final UnsafeBuffer buffer)
{
    final DataHeaderFlyweight dataHeaderFlyweight = new DataHeaderFlyweight();
    final int length = buffer.capacity();
    int offset = 0;

    while (offset < length)
    {
        dataHeaderFlyweight.wrap(buffer, offset, length - offset);
        out.println(offset + ": " + dataHeaderFlyweight.toString());

        final int frameLength = dataHeaderFlyweight.frameLength();
        if (frameLength < DataHeaderFlyweight.HEADER_LENGTH)
        {
            break;
        }

        final int limit = min(frameLength - HEADER_LENGTH, messageDumpLimit);
        out.println(LogInspector.formatBytes(buffer, offset + HEADER_LENGTH, limit));
        offset += BitUtil.align(frameLength, FrameDescriptor.FRAME_ALIGNMENT);
    }
}
 
Example #12
Source File: Configuration.java    From aeron with Apache License 2.0 6 votes vote down vote up
/**
 * Validate that page size is valid and alignment is valid.
 *
 * @param pageSize to be checked.
 * @throws ConfigurationException if the size is not as expected.
 */
public static void validatePageSize(final int pageSize)
{
    if (pageSize < PAGE_MIN_SIZE)
    {
        throw new ConfigurationException(
            "page size less than min size of " + PAGE_MIN_SIZE + ": " + pageSize);
    }

    if (pageSize > PAGE_MAX_SIZE)
    {
        throw new ConfigurationException(
            "page size greater than max size of " + PAGE_MAX_SIZE + ": " + pageSize);
    }

    if (!BitUtil.isPowerOfTwo(pageSize))
    {
        throw new ConfigurationException("page size not a power of 2: " + pageSize);
    }
}
 
Example #13
Source File: LossReportTest.java    From aeron with Apache License 2.0 6 votes vote down vote up
@Test
public void shouldCreateEntry()
{
    final long initialBytesLost = 32;
    final int timestampMs = 7;
    final int sessionId = 3;
    final int streamId = 1;
    final String channel = "aeron:udp://stuff";
    final String source = "127.0.0.1:8888";

    assertNotNull(lossReport.createEntry(initialBytesLost, timestampMs, sessionId, streamId, channel, source));

    final InOrder inOrder = inOrder(buffer);
    inOrder.verify(buffer).putLong(TOTAL_BYTES_LOST_OFFSET, initialBytesLost);
    inOrder.verify(buffer).putLong(FIRST_OBSERVATION_OFFSET, timestampMs);
    inOrder.verify(buffer).putLong(LAST_OBSERVATION_OFFSET, timestampMs);
    inOrder.verify(buffer).putInt(SESSION_ID_OFFSET, sessionId);
    inOrder.verify(buffer).putInt(STREAM_ID_OFFSET, streamId);
    inOrder.verify(buffer).putStringAscii(CHANNEL_OFFSET, channel);
    inOrder.verify(buffer).putStringAscii(
        CHANNEL_OFFSET + BitUtil.align(SIZE_OF_INT + channel.length(), SIZE_OF_INT), source);
    inOrder.verify(buffer).putLongOrdered(OBSERVATION_COUNT_OFFSET, 1L);
}
 
Example #14
Source File: IntArrayQueue.java    From agrona with Apache License 2.0 6 votes vote down vote up
/**
 * Construct a new queue providing all the config options.
 *
 * @param initialCapacity       for the queue which will be rounded up to the nearest power of 2.
 * @param nullValue             which cannot be stored in the queue and used as a sentinel.
 * @param shouldAvoidAllocation true to cache the iterator otherwise false to allocate a new iterator each time.
 */
public IntArrayQueue(
    @DoNotSub final int initialCapacity,
    final int nullValue,
    final boolean shouldAvoidAllocation)
{
    this.nullValue = nullValue;
    this.shouldAvoidAllocation = shouldAvoidAllocation;

    if (initialCapacity < MIN_CAPACITY)
    {
        throw new IllegalArgumentException("initial capacity < MIN_INITIAL_CAPACITY : " + initialCapacity);
    }

    @DoNotSub final int capacity = BitUtil.findNextPositivePowerOfTwo(initialCapacity);
    if (capacity < MIN_CAPACITY)
    {
        throw new IllegalArgumentException("invalid initial capacity: " + initialCapacity);
    }

    elements = new int[capacity];
    Arrays.fill(elements, nullValue);
}
 
Example #15
Source File: AtomicBufferTest.java    From agrona with Apache License 2.0 6 votes vote down vote up
@ParameterizedTest
@MethodSource("buffers")
public void shouldGetByteArrayFromBuffer(final AtomicBuffer buffer)
{
    final byte[] testArray = { 'H', 'e', 'l', 'l', 'o' };

    int i = INDEX;
    for (final byte v : testArray)
    {
        buffer.putByte(i, v);
        i += BitUtil.SIZE_OF_BYTE;
    }

    final byte[] result = new byte[testArray.length];
    buffer.getBytes(INDEX, result);

    assertThat(result, is(testArray));
}
 
Example #16
Source File: AeronUtil.java    From nd4j with Apache License 2.0 6 votes vote down vote up
/**
 * Get a media driver context
 * for sending ndarrays
 * based on a given length
 * where length is the length (number of elements)
 * in the ndarrays hat are being sent
 * @param length the length to based the ipc length
 * @return the media driver context based on the given length
 */
public static MediaDriver.Context getMediaDriverContext(int length) {
    //length of array * sizeof(float)
    int ipcLength = length * 16;
    //padding for NDArrayMessage
    ipcLength += 64;
    //must be a power of 2
    ipcLength *= 2;
    //ipc length must be positive power of 2
    while (!BitUtil.isPowerOfTwo(ipcLength))
        ipcLength += 2;
    // System.setProperty("aeron.term.buffer.size",String.valueOf(ipcLength));
    final MediaDriver.Context ctx =
                    new MediaDriver.Context().threadingMode(ThreadingMode.SHARED).dirsDeleteOnStart(true)
                                    /*  .ipcTermBufferLength(ipcLength)
                                      .publicationTermBufferLength(ipcLength)
                                      .maxTermBufferLength(ipcLength)*/
                                    .conductorIdleStrategy(new BusySpinIdleStrategy())
                                    .receiverIdleStrategy(new BusySpinIdleStrategy())
                                    .senderIdleStrategy(new BusySpinIdleStrategy());
    return ctx;
}
 
Example #17
Source File: TermBlockScannerTest.java    From aeron with Apache License 2.0 6 votes vote down vote up
@Test
public void shouldReadBlockOfThreeMessagesThatFillBuffer()
{
    final int offset = 0;
    final int limit = termBuffer.capacity();
    final int messageLength = 50;
    final int alignedMessageLength = BitUtil.align(messageLength, FRAME_ALIGNMENT);
    final int thirdMessageLength = limit - (alignedMessageLength * 2);

    when(termBuffer.getIntVolatile(lengthOffset(offset))).thenReturn(messageLength);
    when(termBuffer.getShort(typeOffset(offset))).thenReturn((short)HDR_TYPE_DATA);
    when(termBuffer.getIntVolatile(lengthOffset(alignedMessageLength))).thenReturn(messageLength);
    when(termBuffer.getShort(typeOffset(alignedMessageLength))).thenReturn((short)HDR_TYPE_DATA);
    when(termBuffer.getIntVolatile(lengthOffset(alignedMessageLength * 2))).thenReturn(thirdMessageLength);
    when(termBuffer.getShort(typeOffset(alignedMessageLength * 2))).thenReturn((short)HDR_TYPE_DATA);

    final int newOffset = TermBlockScanner.scan(termBuffer, offset, limit);
    assertEquals(limit, newOffset);
}
 
Example #18
Source File: TermBlockScannerTest.java    From aeron with Apache License 2.0 6 votes vote down vote up
@Test
public void shouldReadBlockOfOneMessageThenPadding()
{
    final int offset = 0;
    final int limit = termBuffer.capacity();
    final int messageLength = 50;
    final int alignedMessageLength = BitUtil.align(messageLength, FRAME_ALIGNMENT);

    when(termBuffer.getIntVolatile(lengthOffset(offset))).thenReturn(messageLength);
    when(termBuffer.getShort(typeOffset(offset))).thenReturn((short)HDR_TYPE_DATA);
    when(termBuffer.getIntVolatile(lengthOffset(alignedMessageLength))).thenReturn(messageLength);
    when(termBuffer.getShort(typeOffset(alignedMessageLength))).thenReturn((short)HDR_TYPE_PAD);

    final int firstOffset = TermBlockScanner.scan(termBuffer, offset, limit);
    assertEquals(alignedMessageLength, firstOffset);

    final int secondOffset = TermBlockScanner.scan(termBuffer, firstOffset, limit);
    assertEquals(alignedMessageLength * 2, secondOffset);
}
 
Example #19
Source File: Configuration.java    From aeron with Apache License 2.0 5 votes vote down vote up
@SuppressWarnings("deprecation")
public static long flowControlGroupTag()
{
    final String propertyValue = getProperty(
        PreferredMulticastFlowControl.PREFERRED_ASF_PROP_NAME, PreferredMulticastFlowControl.PREFERRED_ASF_DEFAULT);
    final long legacyAsfValue = new UnsafeBuffer(BitUtil.fromHex(propertyValue)).getLong(0, LITTLE_ENDIAN);

    return getLong(FLOW_CONTROL_GROUP_TAG_PROP_NAME, legacyAsfValue);
}
 
Example #20
Source File: RingBufferDescriptor.java    From agrona with Apache License 2.0 5 votes vote down vote up
/**
 * Check the the buffer capacity is the correct size (a power of 2 + {@link RingBufferDescriptor#TRAILER_LENGTH}).
 *
 * @param capacity to be checked.
 * @throws IllegalStateException if the buffer capacity is incorrect.
 */
public static void checkCapacity(final int capacity)
{
    if (!BitUtil.isPowerOfTwo(capacity - TRAILER_LENGTH))
    {
        final String msg = "capacity must be a positive power of 2 + TRAILER_LENGTH: capacity=" + capacity;
        throw new IllegalStateException(msg);
    }
}
 
Example #21
Source File: CollectionUtil.java    From agrona with Apache License 2.0 5 votes vote down vote up
/**
 * Validate that a number is a power of two.
 *
 * @param value to be validated.
 */
public static void validatePositivePowerOfTwo(final int value)
{
    if (!BitUtil.isPowerOfTwo(value))
    {
        throw new IllegalArgumentException("value must be a positive power of two: " + value);
    }
}
 
Example #22
Source File: DistinctErrorLogTest.java    From agrona with Apache License 2.0 5 votes vote down vote up
@Test
public void shouldRecordTwoDistinctObservationsOnCause()
{
    final long timestampOne = 7;
    final long timestampTwo = 10;
    final int offset = 0;

    when(clock.time()).thenReturn(timestampOne).thenReturn(timestampTwo);

    for (int i = 0; i < 2; i++)
    {
        assertTrue(log.record(i == 1 ?
            new RuntimeException("One") :
            new RuntimeException("One", new Exception("Cause"))));
    }

    final ArgumentCaptor<Integer> lengthArg = ArgumentCaptor.forClass(Integer.class);

    final InOrder inOrder = inOrder(buffer);
    inOrder.verify(buffer).putBytes(eq(offset + ENCODED_ERROR_OFFSET), any(byte[].class));
    inOrder.verify(buffer).putLong(offset + FIRST_OBSERVATION_TIMESTAMP_OFFSET, timestampOne);
    inOrder.verify(buffer).putIntOrdered(eq(offset + LENGTH_OFFSET), lengthArg.capture());
    inOrder.verify(buffer).getAndAddInt(offset + OBSERVATION_COUNT_OFFSET, 1);
    inOrder.verify(buffer).putLongOrdered(offset + LAST_OBSERVATION_TIMESTAMP_OFFSET, timestampOne);

    final int recordTwoOffset = BitUtil.align(lengthArg.getValue(), RECORD_ALIGNMENT);

    inOrder.verify(buffer).putBytes(eq(recordTwoOffset + ENCODED_ERROR_OFFSET), any(byte[].class));
    inOrder.verify(buffer).putLong(recordTwoOffset + FIRST_OBSERVATION_TIMESTAMP_OFFSET, timestampTwo);
    inOrder.verify(buffer).putIntOrdered(eq(recordTwoOffset + LENGTH_OFFSET), anyInt());
    inOrder.verify(buffer).getAndAddInt(recordTwoOffset + OBSERVATION_COUNT_OFFSET, 1);
    inOrder.verify(buffer).putLongOrdered(recordTwoOffset + LAST_OBSERVATION_TIMESTAMP_OFFSET, timestampTwo);
}
 
Example #23
Source File: ILinkSequenceNumberExtractor.java    From artio with Apache License 2.0 5 votes vote down vote up
public void onFragment(
    final DirectBuffer buffer,
    final int srcOffset,
    final int srcLength,
    final Header header)
{
    final long endPosition = header.position();

    if ((header.flags() & BEGIN_AND_END_FLAGS) == BEGIN_AND_END_FLAGS)
    {
        int offset = srcOffset;
        messageHeader.wrap(buffer, offset);

        offset += messageHeader.encodedLength();
        final int actingBlockLength = messageHeader.blockLength();
        final int version = messageHeader.version();
        final int templateId = messageHeader.templateId();


        switch (templateId)
        {
            case ILinkMessageDecoder.TEMPLATE_ID:
            {
                final int totalLength = BitUtil.align(srcLength, FRAME_ALIGNMENT);

                onILinkMessage(
                    buffer, endPosition, offset, actingBlockLength, version, totalLength, header.sessionId());
                break;
            }

            case ILinkConnectDecoder.TEMPLATE_ID:
            {
                iLinkConnect.wrap(buffer, offset, actingBlockLength, version);
                connectionIdToILinkUuid.put(iLinkConnect.connection(), iLinkConnect.uuid());
                break;
            }
        }
    }
}
 
Example #24
Source File: DistinctErrorLogTest.java    From agrona with Apache License 2.0 5 votes vote down vote up
@Test
public void shouldRecordTwoDistinctObservations()
{
    final long timestampOne = 7;
    final long timestampTwo = 10;
    final int offset = 0;
    final RuntimeException errorOne = new RuntimeException("Test Error One");
    final IllegalStateException errorTwo = new IllegalStateException("Test Error Two");

    when(clock.time()).thenReturn(timestampOne).thenReturn(timestampTwo);

    assertTrue(log.record(errorOne));
    assertTrue(log.record(errorTwo));

    final ArgumentCaptor<Integer> lengthArg = ArgumentCaptor.forClass(Integer.class);

    final InOrder inOrder = inOrder(buffer);
    inOrder.verify(buffer).putBytes(eq(offset + ENCODED_ERROR_OFFSET), any(byte[].class));
    inOrder.verify(buffer).putLong(offset + FIRST_OBSERVATION_TIMESTAMP_OFFSET, timestampOne);
    inOrder.verify(buffer).putIntOrdered(eq(offset + LENGTH_OFFSET), lengthArg.capture());
    inOrder.verify(buffer).getAndAddInt(offset + OBSERVATION_COUNT_OFFSET, 1);
    inOrder.verify(buffer).putLongOrdered(offset + LAST_OBSERVATION_TIMESTAMP_OFFSET, timestampOne);

    final int recordTwoOffset = BitUtil.align(lengthArg.getValue(), RECORD_ALIGNMENT);

    inOrder.verify(buffer).putBytes(eq(recordTwoOffset + ENCODED_ERROR_OFFSET), any(byte[].class));
    inOrder.verify(buffer).putLong(recordTwoOffset + FIRST_OBSERVATION_TIMESTAMP_OFFSET, timestampTwo);
    inOrder.verify(buffer).putIntOrdered(eq(recordTwoOffset + LENGTH_OFFSET), anyInt());
    inOrder.verify(buffer).getAndAddInt(recordTwoOffset + OBSERVATION_COUNT_OFFSET, 1);
    inOrder.verify(buffer).putLongOrdered(recordTwoOffset + LAST_OBSERVATION_TIMESTAMP_OFFSET, timestampTwo);
}
 
Example #25
Source File: RecordingPos.java    From aeron with Apache License 2.0 5 votes vote down vote up
public static Counter allocate(
    final Aeron aeron,
    final UnsafeBuffer tempBuffer,
    final long recordingId,
    final int sessionId,
    final int streamId,
    final String strippedChannel,
    final String sourceIdentity)
{
    tempBuffer.putLong(RECORDING_ID_OFFSET, recordingId);
    tempBuffer.putInt(SESSION_ID_OFFSET, sessionId);

    final int sourceIdentityLength = Math.min(sourceIdentity.length(), MAX_KEY_LENGTH - SOURCE_IDENTITY_OFFSET);
    tempBuffer.putStringAscii(SOURCE_IDENTITY_LENGTH_OFFSET, sourceIdentity);
    final int keyLength = SOURCE_IDENTITY_OFFSET + sourceIdentityLength;

    final int labelOffset = BitUtil.align(keyLength, SIZE_OF_INT);
    int labelLength = 0;
    labelLength += tempBuffer.putStringWithoutLengthAscii(labelOffset, NAME + ": ");
    labelLength += tempBuffer.putLongAscii(labelOffset + labelLength, recordingId);
    labelLength += tempBuffer.putStringWithoutLengthAscii(labelOffset + labelLength, " ");
    labelLength += tempBuffer.putIntAscii(labelOffset + labelLength, sessionId);
    labelLength += tempBuffer.putStringWithoutLengthAscii(labelOffset + labelLength, " ");
    labelLength += tempBuffer.putIntAscii(labelOffset + labelLength, streamId);
    labelLength += tempBuffer.putStringWithoutLengthAscii(labelOffset + labelLength, " ");
    labelLength += tempBuffer.putStringWithoutLengthAscii(
        labelOffset + labelLength, strippedChannel, 0, MAX_LABEL_LENGTH - labelLength);

    return aeron.addCounter(
        RECORDING_POSITION_TYPE_ID, tempBuffer, 0, keyLength, tempBuffer, labelOffset, labelLength);
}
 
Example #26
Source File: LossReport.java    From aeron with Apache License 2.0 5 votes vote down vote up
/**
 * Create a new entry for recording loss on a given stream.
 * <p>
 * If not space is remaining in the error report then null is returned.
 *
 * @param initialBytesLost on the stream.
 * @param timestampMs      at which the first loss was observed.
 * @param sessionId        for the stream.
 * @param streamId         for the stream.
 * @param channel          for the stream.
 * @param source           of the stream.
 * @return a new record or null if the error log has insufficient space.
 */
public ReportEntry createEntry(
    final long initialBytesLost,
    final long timestampMs,
    final int sessionId,
    final int streamId,
    final String channel,
    final String source)
{
    ReportEntry reportEntry = null;

    final int requiredCapacity =
        CHANNEL_OFFSET +
        BitUtil.align(SIZE_OF_INT + channel.length(), SIZE_OF_INT) +
        SIZE_OF_INT + source.length();

    if (requiredCapacity <= (buffer.capacity() - nextRecordOffset))
    {
        final int offset = nextRecordOffset;

        buffer.putLong(offset + TOTAL_BYTES_LOST_OFFSET, initialBytesLost);
        buffer.putLong(offset + FIRST_OBSERVATION_OFFSET, timestampMs);
        buffer.putLong(offset + LAST_OBSERVATION_OFFSET, timestampMs);
        buffer.putInt(offset + SESSION_ID_OFFSET, sessionId);
        buffer.putInt(offset + STREAM_ID_OFFSET, streamId);

        final int encodedChannelLength = buffer.putStringAscii(offset + CHANNEL_OFFSET, channel);

        buffer.putStringAscii(
            offset + CHANNEL_OFFSET + BitUtil.align(encodedChannelLength, SIZE_OF_INT), source);

        buffer.putLongOrdered(offset + OBSERVATION_COUNT_OFFSET, 1);

        reportEntry = new ReportEntry(buffer, offset);
        nextRecordOffset += BitUtil.align(requiredCapacity, ENTRY_ALIGNMENT);
    }

    return reportEntry;
}
 
Example #27
Source File: UdpChannel.java    From aeron with Apache License 2.0 5 votes vote down vote up
private static void validateDataAddress(final byte[] addressAsBytes)
{
    if (BitUtil.isEven(addressAsBytes[addressAsBytes.length - 1]))
    {
        throw new IllegalArgumentException("multicast data address must be odd");
    }
}
 
Example #28
Source File: TestCluster.java    From aeron with Apache License 2.0 5 votes vote down vote up
void sendMessages(final int messageCount)
{
    for (int i = 0; i < messageCount; i++)
    {
        msgBuffer.putInt(0, i);
        pollUntilMessageSent(BitUtil.SIZE_OF_INT);
    }
}
 
Example #29
Source File: TermBlockScannerTest.java    From aeron with Apache License 2.0 5 votes vote down vote up
@Test
public void shouldReadOneMessageOnLimit()
{
    final int offset = 0;
    final int messageLength = 50;
    final int alignedMessageLength = BitUtil.align(messageLength, FRAME_ALIGNMENT);

    when(termBuffer.getIntVolatile(lengthOffset(offset))).thenReturn(messageLength);
    when(termBuffer.getShort(typeOffset(offset))).thenReturn((short)HDR_TYPE_DATA);

    final int newOffset = TermBlockScanner.scan(termBuffer, offset, alignedMessageLength);
    assertEquals(alignedMessageLength, newOffset);
}
 
Example #30
Source File: BroadcastTransmitter.java    From agrona with Apache License 2.0 5 votes vote down vote up
/**
 * Transmit a message to {@link BroadcastReceiver}s via the broadcast buffer.
 *
 * @param msgTypeId type of the message to be transmitted.
 * @param srcBuffer containing the encoded message to be transmitted.
 * @param srcIndex  srcIndex in the source buffer at which the encoded message begins.
 * @param length    in bytes of the encoded message.
 * @throws IllegalArgumentException of the msgTypeId is not valid,
 * or if the message length is greater than {@link #maxMsgLength()}.
 */
public void transmit(final int msgTypeId, final DirectBuffer srcBuffer, final int srcIndex, final int length)
{
    checkTypeId(msgTypeId);
    checkMessageLength(length);

    final AtomicBuffer buffer = this.buffer;
    long currentTail = buffer.getLong(tailCounterIndex);
    int recordOffset = (int)currentTail & (capacity - 1);
    final int recordLength = HEADER_LENGTH + length;
    final int recordLengthAligned = BitUtil.align(recordLength, RECORD_ALIGNMENT);
    final long newTail = currentTail + recordLengthAligned;

    final int toEndOfBuffer = capacity - recordOffset;
    if (toEndOfBuffer < recordLengthAligned)
    {
        signalTailIntent(buffer, newTail + toEndOfBuffer);
        insertPaddingRecord(buffer, recordOffset, toEndOfBuffer);

        currentTail += toEndOfBuffer;
        recordOffset = 0;
    }
    else
    {
        signalTailIntent(buffer, newTail);
    }

    buffer.putInt(lengthOffset(recordOffset), recordLength);
    buffer.putInt(typeOffset(recordOffset), msgTypeId);

    buffer.putBytes(msgOffset(recordOffset), srcBuffer, srcIndex, length);

    buffer.putLong(latestCounterIndex, currentTail);
    buffer.putLongOrdered(tailCounterIndex, currentTail + recordLengthAligned);
}