Java Code Examples for org.agrona.concurrent.UnsafeBuffer#putLong()

The following examples show how to use org.agrona.concurrent.UnsafeBuffer#putLong() . 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 benchmarks with Apache License 2.0 6 votes vote down vote up
static int sendMessages(
    final ExclusivePublication publication,
    final UnsafeBuffer offerBuffer,
    final int numberOfMessages,
    final int messageLength,
    final long timestamp,
    final long checksum)
{
    int count = 0;
    for (int i = 0; i < numberOfMessages; i++)
    {
        offerBuffer.putLong(0, timestamp, LITTLE_ENDIAN);
        offerBuffer.putLong(messageLength - SIZE_OF_LONG, checksum, LITTLE_ENDIAN);

        final long result = publication.offer(offerBuffer, 0, messageLength, null);
        if (result < 0)
        {
            checkPublicationResult(result);
            break;
        }
        count++;
    }

    return count;
}
 
Example 2
Source File: ArchiveEventEncoder.java    From aeron with Apache License 2.0 6 votes vote down vote up
static <E extends Enum<E>> int encodeSessionStateChange(
    final UnsafeBuffer encodingBuffer,
    final int offset,
    final int captureLength,
    final int length,
    final E from,
    final E to,
    final long id)
{
    int relativeOffset = encodeLogHeader(encodingBuffer, offset, captureLength, length);

    encodingBuffer.putLong(offset + relativeOffset, id, LITTLE_ENDIAN);
    relativeOffset += SIZE_OF_LONG;

    encodingBuffer.putInt(offset + relativeOffset, captureLength - (SIZE_OF_LONG + SIZE_OF_INT), LITTLE_ENDIAN);
    relativeOffset += SIZE_OF_INT;

    relativeOffset += encodingBuffer.putStringWithoutLengthAscii(offset + relativeOffset, from.name());
    relativeOffset += encodingBuffer.putStringWithoutLengthAscii(offset + relativeOffset, STATE_SEPARATOR);
    relativeOffset += encodingBuffer.putStringWithoutLengthAscii(offset + relativeOffset, to.name());

    return relativeOffset;
}
 
Example 3
Source File: DriverEventEncoder.java    From aeron with Apache License 2.0 6 votes vote down vote up
static void encodeImageRemoval(
    final UnsafeBuffer encodingBuffer,
    final int offset,
    final int captureLength,
    final int length,
    final String uri,
    final int sessionId,
    final int streamId,
    final long id)
{
    int relativeOffset = encodeLogHeader(encodingBuffer, offset, captureLength, length);

    encodingBuffer.putInt(offset + relativeOffset, sessionId, LITTLE_ENDIAN);
    relativeOffset += SIZE_OF_INT;

    encodingBuffer.putInt(offset + relativeOffset, streamId, LITTLE_ENDIAN);
    relativeOffset += SIZE_OF_INT;

    encodingBuffer.putLong(offset + relativeOffset, id, LITTLE_ENDIAN);
    relativeOffset += SIZE_OF_LONG;

    encodeTrailingString(
        encodingBuffer, offset + relativeOffset, captureLength - (SIZE_OF_INT * 2) - SIZE_OF_LONG, uri);
}
 
Example 4
Source File: DriverEventEncoder.java    From aeron with Apache License 2.0 6 votes vote down vote up
static void encodeSubscriptionRemoval(
    final UnsafeBuffer encodingBuffer,
    final int offset,
    final int captureLength,
    final int length,
    final String uri,
    final int streamId,
    final long id)
{
    int relativeOffset = encodeLogHeader(encodingBuffer, offset, captureLength, length);

    encodingBuffer.putInt(offset + relativeOffset, streamId, LITTLE_ENDIAN);
    relativeOffset += SIZE_OF_INT;

    encodingBuffer.putLong(offset + relativeOffset, id, LITTLE_ENDIAN);
    relativeOffset += SIZE_OF_LONG;

    encodeTrailingString(encodingBuffer, offset + relativeOffset, captureLength - SIZE_OF_INT - SIZE_OF_LONG, uri);
}
 
Example 5
Source File: CncFileDescriptor.java    From aeron with Apache License 2.0 6 votes vote down vote up
/**
 * Fill the CnC file with metadata to define its sections.
 *
 * @param cncMetaDataBuffer           that wraps the metadata section of the CnC file.
 * @param toDriverBufferLength        for sending commands to the driver.
 * @param toClientsBufferLength       for broadcasting events to the clients.
 * @param counterMetaDataBufferLength buffer length for counters metadata.
 * @param counterValuesBufferLength   buffer length for counter values.
 * @param clientLivenessTimeoutNs     timeout value in nanoseconds for client liveness and inter-service interval.
 * @param errorLogBufferLength        for recording the distinct error log.
 * @param startTimestampMs            epoch at which the driver started.
 * @param pid                         for the process hosting the driver.
 */
public static void fillMetaData(
    final UnsafeBuffer cncMetaDataBuffer,
    final int toDriverBufferLength,
    final int toClientsBufferLength,
    final int counterMetaDataBufferLength,
    final int counterValuesBufferLength,
    final long clientLivenessTimeoutNs,
    final int errorLogBufferLength,
    final long startTimestampMs,
    final long pid)
{
    cncMetaDataBuffer.putInt(toDriverBufferLengthOffset(0), toDriverBufferLength);
    cncMetaDataBuffer.putInt(toClientsBufferLengthOffset(0), toClientsBufferLength);
    cncMetaDataBuffer.putInt(countersMetaDataBufferLengthOffset(0), counterMetaDataBufferLength);
    cncMetaDataBuffer.putInt(countersValuesBufferLengthOffset(0), counterValuesBufferLength);
    cncMetaDataBuffer.putInt(errorLogBufferLengthOffset(0), errorLogBufferLength);
    cncMetaDataBuffer.putLong(clientLivenessTimeoutOffset(0), clientLivenessTimeoutNs);
    cncMetaDataBuffer.putLong(startTimestampOffset(0), startTimestampMs);
    cncMetaDataBuffer.putLong(pidOffset(0), pid);
}
 
Example 6
Source File: DataHeaderFlyweight.java    From aeron with Apache License 2.0 6 votes vote down vote up
/**
 * 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(
        BufferUtil.allocateDirectAligned(HEADER_LENGTH, CACHE_LINE_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;
}
 
Example 7
Source File: CommonEventEncoder.java    From aeron with Apache License 2.0 5 votes vote down vote up
static int internalEncodeLogHeader(
    final UnsafeBuffer encodingBuffer,
    final int offset,
    final int captureLength,
    final int length,
    final NanoClock nanoClock)
{
    if (captureLength < 0 || captureLength > length || captureLength > MAX_CAPTURE_LENGTH)
    {
        throw new IllegalArgumentException("invalid input: captureLength=" + captureLength + ", length=" + length);
    }

    int relativeOffset = 0;
    /*
     * Stream of values:
     * - capture buffer length (int)
     * - total buffer length (int)
     * - timestamp (long)
     * - buffer (until end)
     */

    encodingBuffer.putInt(offset + relativeOffset, captureLength, LITTLE_ENDIAN);
    relativeOffset += SIZE_OF_INT;

    encodingBuffer.putInt(offset + relativeOffset, length, LITTLE_ENDIAN);
    relativeOffset += SIZE_OF_INT;

    encodingBuffer.putLong(offset + relativeOffset, nanoClock.nanoTime(), LITTLE_ENDIAN);
    relativeOffset += SIZE_OF_LONG;

    return relativeOffset;
}
 
Example 8
Source File: TermRebuilder.java    From aeron with Apache License 2.0 5 votes vote down vote up
/**
 * Insert a packet of frames into the log at the appropriate termOffset as indicated by the term termOffset header.
 * <p>
 * If the packet has already been inserted then this is a noop.
 *
 * @param termBuffer into which the packet should be inserted.
 * @param termOffset in the term at which the packet should be inserted.
 * @param packet     containing a sequence of frames.
 * @param length     of the packet of frames in bytes.
 */
public static void insert(
    final UnsafeBuffer termBuffer, final int termOffset, final UnsafeBuffer packet, final int length)
{
    if (0 == termBuffer.getInt(termOffset))
    {
        termBuffer.putBytes(termOffset + HEADER_LENGTH, packet, HEADER_LENGTH, length - HEADER_LENGTH);

        termBuffer.putLong(termOffset + 24, packet.getLong(24));
        termBuffer.putLong(termOffset + 16, packet.getLong(16));
        termBuffer.putLong(termOffset + 8, packet.getLong(8));

        termBuffer.putLongOrdered(termOffset, packet.getLong(0));
    }
}
 
Example 9
Source File: RecordingLog.java    From aeron with Apache License 2.0 5 votes vote down vote up
private void writeEntryToBuffer(final Entry entry, final UnsafeBuffer buffer, final ByteBuffer byteBuffer)
{
    buffer.putLong(RECORDING_ID_OFFSET, entry.recordingId, LITTLE_ENDIAN);
    buffer.putLong(LEADERSHIP_TERM_ID_OFFSET, entry.leadershipTermId, LITTLE_ENDIAN);
    buffer.putLong(TERM_BASE_LOG_POSITION_OFFSET, entry.termBaseLogPosition, LITTLE_ENDIAN);
    buffer.putLong(LOG_POSITION_OFFSET, entry.logPosition, LITTLE_ENDIAN);
    buffer.putLong(TIMESTAMP_OFFSET, entry.timestamp, LITTLE_ENDIAN);
    buffer.putInt(SERVICE_ID_OFFSET, entry.serviceId, LITTLE_ENDIAN);
    buffer.putInt(ENTRY_TYPE_OFFSET, entry.type, LITTLE_ENDIAN);

    byteBuffer.limit(ENTRY_LENGTH).position(0);
}
 
Example 10
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 11
Source File: GapFillLossTest.java    From aeron with Apache License 2.0 4 votes vote down vote up
@Test
@Timeout(10)
public void shouldGapFillWhenLossOccurs() throws Exception
{
    final UnsafeBuffer srcBuffer = new UnsafeBuffer(ByteBuffer.allocateDirect(MSG_LENGTH));
    srcBuffer.setMemory(0, MSG_LENGTH, (byte)7);

    final MediaDriver.Context ctx = new MediaDriver.Context()
        .errorHandler(Tests::onError)
        .threadingMode(ThreadingMode.SHARED)
        .dirDeleteOnStart(true)
        .publicationTermBufferLength(LogBufferDescriptor.TERM_MIN_LENGTH);

    final LossGenerator noLossGenerator =
        DebugChannelEndpointConfiguration.lossGeneratorSupplier(0, 0);

    ctx.sendChannelEndpointSupplier((udpChannel, statusIndicator, context) -> new DebugSendChannelEndpoint(
        udpChannel, statusIndicator, context, noLossGenerator, noLossGenerator));

    TestMediaDriver.enableLossGenerationOnReceive(ctx, 0.20, 0xcafebabeL, true, false);

    try (TestMediaDriver ignore = TestMediaDriver.launch(ctx, watcher);
        Aeron aeron = Aeron.connect();
        Subscription subscription = aeron.addSubscription(UNRELIABLE_CHANNEL, STREAM_ID);
        Publication publication = aeron.addPublication(CHANNEL, STREAM_ID))
    {
        final Subscriber subscriber = new Subscriber(subscription);
        final Thread subscriberThread = new Thread(subscriber);
        subscriberThread.setDaemon(true);
        subscriberThread.start();

        long position = 0;
        for (int i = 0; i < NUM_MESSAGES; i++)
        {
            srcBuffer.putLong(0, i);

            while ((position = publication.offer(srcBuffer)) < 0L)
            {
                Tests.yield();
            }
        }

        FINAL_POSITION.set(position);
        subscriberThread.join();

        verifyLossOccurredForStream(ctx.aeronDirectoryName(), STREAM_ID);
        assertThat(subscriber.messageCount, lessThan(NUM_MESSAGES));
    }
    finally
    {
        ctx.deleteDirectory();
    }
}
 
Example 12
Source File: MemoryOrderingTest.java    From aeron with Apache License 2.0 4 votes vote down vote up
@Test
@Timeout(10)
public void shouldReceiveMessagesInOrderWithFirstLongWordIntactFromExclusivePublication() throws Exception
{
    final UnsafeBuffer srcBuffer = new UnsafeBuffer(ByteBuffer.allocate(MESSAGE_LENGTH));
    srcBuffer.setMemory(0, MESSAGE_LENGTH, (byte)7);

    try (Subscription subscription = aeron.addSubscription(CHANNEL, STREAM_ID);
        ExclusivePublication publication = aeron.addExclusivePublication(CHANNEL, STREAM_ID))
    {
        final IdleStrategy idleStrategy = YieldingIdleStrategy.INSTANCE;
        final Thread subscriberThread = new Thread(new Subscriber(subscription));
        subscriberThread.setDaemon(true);
        subscriberThread.start();

        for (int i = 0; i < NUM_MESSAGES; i++)
        {
            if (null != failedMessage)
            {
                fail(failedMessage);
            }

            srcBuffer.putLong(0, i);

            while (publication.offer(srcBuffer) < 0L)
            {
                if (null != failedMessage)
                {
                    fail(failedMessage);
                }

                idleStrategy.idle();
                Tests.checkInterruptStatus();
            }

            if (i % BURST_LENGTH == 0)
            {
                final long timeoutNs = System.nanoTime() + INTER_BURST_DURATION_NS;
                long nowNs;
                do
                {
                    nowNs = System.nanoTime();
                }
                while ((timeoutNs - nowNs) > 0);
            }
        }

        subscriberThread.join();
    }
}
 
Example 13
Source File: ExclusiveTermAppender.java    From aeron with Apache License 2.0 4 votes vote down vote up
/**
 * Append an unfragmented message to the the term buffer as a gathering of vectors.
 *
 * @param termId                for the current term.
 * @param termOffset            in the term at which to append.
 * @param header                for writing the default header.
 * @param vectors               to the buffers.
 * @param length                of the message as a sum of the vectors.
 * @param reservedValueSupplier {@link ReservedValueSupplier} for the frame.
 * @return the resulting offset of the term after the append on success otherwise {@link #FAILED}.
 */
public int appendUnfragmentedMessage(
    final int termId,
    final int termOffset,
    final HeaderWriter header,
    final DirectBufferVector[] vectors,
    final int length,
    final ReservedValueSupplier reservedValueSupplier)
{
    final int frameLength = length + HEADER_LENGTH;
    final int alignedLength = align(frameLength, FRAME_ALIGNMENT);
    final UnsafeBuffer termBuffer = this.termBuffer;
    final int termLength = termBuffer.capacity();

    int resultingOffset = termOffset + alignedLength;
    putRawTailOrdered(termId, resultingOffset);

    if (resultingOffset > termLength)
    {
        resultingOffset = handleEndOfLogCondition(termBuffer, termOffset, header, termLength, termId);
    }
    else
    {
        header.write(termBuffer, termOffset, frameLength, termId);

        int offset = termOffset + HEADER_LENGTH;
        for (final DirectBufferVector vector : vectors)
        {
            termBuffer.putBytes(offset, vector.buffer, vector.offset, vector.length);
            offset += vector.length;
        }

        if (null != reservedValueSupplier)
        {
            final long reservedValue = reservedValueSupplier.get(termBuffer, termOffset, frameLength);
            termBuffer.putLong(termOffset + RESERVED_VALUE_OFFSET, reservedValue, LITTLE_ENDIAN);
        }

        frameLengthOrdered(termBuffer, termOffset, frameLength);
    }

    return resultingOffset;
}
 
Example 14
Source File: ExclusiveTermAppender.java    From aeron with Apache License 2.0 4 votes vote down vote up
/**
 * Append a fragmented message to the the term buffer.
 * The message will be split up into fragments of MTU length minus header.
 *
 * @param termId                for the current term.
 * @param termOffset            in the term at which to append.
 * @param header                for writing the default header.
 * @param srcBuffer             containing the message.
 * @param srcOffset             at which the message begins.
 * @param length                of the message in the source buffer.
 * @param maxPayloadLength      that the message will be fragmented into.
 * @param reservedValueSupplier {@link ReservedValueSupplier} for the frame.
 * @return the resulting offset of the term after the append on success otherwise {@link #FAILED}.
 */
public int appendFragmentedMessage(
    final int termId,
    final int termOffset,
    final HeaderWriter header,
    final DirectBuffer srcBuffer,
    final int srcOffset,
    final int length,
    final int maxPayloadLength,
    final ReservedValueSupplier reservedValueSupplier)
{
    final int numMaxPayloads = length / maxPayloadLength;
    final int remainingPayload = length % maxPayloadLength;
    final int lastFrameLength = remainingPayload > 0 ? align(remainingPayload + HEADER_LENGTH, FRAME_ALIGNMENT) : 0;
    final int requiredLength = (numMaxPayloads * (maxPayloadLength + HEADER_LENGTH)) + lastFrameLength;
    final UnsafeBuffer termBuffer = this.termBuffer;
    final int termLength = termBuffer.capacity();

    int resultingOffset = termOffset + requiredLength;
    putRawTailOrdered(termId, resultingOffset);

    if (resultingOffset > termLength)
    {
        resultingOffset = handleEndOfLogCondition(termBuffer, termOffset, header, termLength, termId);
    }
    else
    {
        int frameOffset = termOffset;
        byte flags = BEGIN_FRAG_FLAG;
        int remaining = length;
        do
        {
            final int bytesToWrite = Math.min(remaining, maxPayloadLength);
            final int frameLength = bytesToWrite + HEADER_LENGTH;
            final int alignedLength = align(frameLength, FRAME_ALIGNMENT);

            header.write(termBuffer, frameOffset, frameLength, termId);
            termBuffer.putBytes(
                frameOffset + HEADER_LENGTH,
                srcBuffer,
                srcOffset + (length - remaining),
                bytesToWrite);

            if (remaining <= maxPayloadLength)
            {
                flags |= END_FRAG_FLAG;
            }

            frameFlags(termBuffer, frameOffset, flags);

            if (null != reservedValueSupplier)
            {
                final long reservedValue = reservedValueSupplier.get(termBuffer, frameOffset, frameLength);
                termBuffer.putLong(frameOffset + RESERVED_VALUE_OFFSET, reservedValue, LITTLE_ENDIAN);
            }

            frameLengthOrdered(termBuffer, frameOffset, frameLength);

            flags = 0;
            frameOffset += alignedLength;
            remaining -= bytesToWrite;
        }
        while (remaining > 0);
    }

    return resultingOffset;
}
 
Example 15
Source File: ExclusiveTermAppender.java    From aeron with Apache License 2.0 4 votes vote down vote up
/**
 * Append a fragmented message to the the term buffer.
 * The message will be split up into fragments of MTU length minus header.
 *
 * @param termId                for the current term.
 * @param termOffset            in the term at which to append.
 * @param header                for writing the default header.
 * @param vectors               to the buffers.
 * @param length                of the message in the source buffer.
 * @param maxPayloadLength      that the message will be fragmented into.
 * @param reservedValueSupplier {@link ReservedValueSupplier} for the frame.
 * @return the resulting offset of the term after the append on success otherwise {@link #FAILED}.
 */
public int appendFragmentedMessage(
    final int termId,
    final int termOffset,
    final HeaderWriter header,
    final DirectBufferVector[] vectors,
    final int length,
    final int maxPayloadLength,
    final ReservedValueSupplier reservedValueSupplier)
{
    final int numMaxPayloads = length / maxPayloadLength;
    final int remainingPayload = length % maxPayloadLength;
    final int lastFrameLength = remainingPayload > 0 ? align(remainingPayload + HEADER_LENGTH, FRAME_ALIGNMENT) : 0;
    final int requiredLength = (numMaxPayloads * (maxPayloadLength + HEADER_LENGTH)) + lastFrameLength;
    final UnsafeBuffer termBuffer = this.termBuffer;
    final int termLength = termBuffer.capacity();

    int resultingOffset = termOffset + requiredLength;
    putRawTailOrdered(termId, resultingOffset);

    if (resultingOffset > termLength)
    {
        resultingOffset = handleEndOfLogCondition(termBuffer, termOffset, header, termLength, termId);
    }
    else
    {
        int frameOffset = termOffset;
        byte flags = BEGIN_FRAG_FLAG;
        int remaining = length;
        int vectorIndex = 0;
        int vectorOffset = 0;

        do
        {
            final int bytesToWrite = Math.min(remaining, maxPayloadLength);
            final int frameLength = bytesToWrite + HEADER_LENGTH;
            final int alignedLength = align(frameLength, FRAME_ALIGNMENT);

            header.write(termBuffer, frameOffset, frameLength, termId);

            int bytesWritten = 0;
            int payloadOffset = frameOffset + HEADER_LENGTH;
            do
            {
                final DirectBufferVector vector = vectors[vectorIndex];
                final int vectorRemaining = vector.length - vectorOffset;
                final int numBytes = Math.min(bytesToWrite - bytesWritten, vectorRemaining);

                termBuffer.putBytes(payloadOffset, vector.buffer, vector.offset + vectorOffset, numBytes);

                bytesWritten += numBytes;
                payloadOffset += numBytes;
                vectorOffset += numBytes;

                if (vectorRemaining <= numBytes)
                {
                    vectorIndex++;
                    vectorOffset = 0;
                }
            }
            while (bytesWritten < bytesToWrite);

            if (remaining <= maxPayloadLength)
            {
                flags |= END_FRAG_FLAG;
            }

            frameFlags(termBuffer, frameOffset, flags);

            if (null != reservedValueSupplier)
            {
                final long reservedValue = reservedValueSupplier.get(termBuffer, frameOffset, frameLength);
                termBuffer.putLong(frameOffset + RESERVED_VALUE_OFFSET, reservedValue, LITTLE_ENDIAN);
            }

            frameLengthOrdered(termBuffer, frameOffset, frameLength);

            flags = 0;
            frameOffset += alignedLength;
            remaining -= bytesToWrite;
        }
        while (remaining > 0);
    }

    return resultingOffset;
}
 
Example 16
Source File: MemoryOrderingTest.java    From aeron with Apache License 2.0 4 votes vote down vote up
@Test
@Timeout(10)
public void shouldReceiveMessagesInOrderWithFirstLongWordIntact() throws Exception
{
    final UnsafeBuffer srcBuffer = new UnsafeBuffer(ByteBuffer.allocate(MESSAGE_LENGTH));
    srcBuffer.setMemory(0, MESSAGE_LENGTH, (byte)7);

    try (Subscription subscription = aeron.addSubscription(CHANNEL, STREAM_ID);
        Publication publication = aeron.addPublication(CHANNEL, STREAM_ID))
    {
        final IdleStrategy idleStrategy = YieldingIdleStrategy.INSTANCE;
        final Thread subscriberThread = new Thread(new Subscriber(subscription));
        subscriberThread.setDaemon(true);
        subscriberThread.start();

        for (int i = 0; i < NUM_MESSAGES; i++)
        {
            if (null != failedMessage)
            {
                fail(failedMessage);
            }

            srcBuffer.putLong(0, i);

            while (publication.offer(srcBuffer) < 0L)
            {
                if (null != failedMessage)
                {
                    fail(failedMessage);
                }

                idleStrategy.idle();
                Tests.checkInterruptStatus();
            }

            if (i % BURST_LENGTH == 0)
            {
                final long timeoutNs = System.nanoTime() + INTER_BURST_DURATION_NS;
                long nowNs;
                do
                {
                    nowNs = System.nanoTime();
                }
                while ((timeoutNs - nowNs) > 0);
            }
        }

        subscriberThread.join();
    }
}
 
Example 17
Source File: EmbeddedRecordingThroughput.java    From aeron with Apache License 2.0 4 votes vote down vote up
public long streamMessagesForRecording()
{
    try (ExclusivePublication publication = aeron.addExclusivePublication(CHANNEL, STREAM_ID))
    {
        final IdleStrategy idleStrategy = YieldingIdleStrategy.INSTANCE;
        while (!publication.isConnected())
        {
            idleStrategy.idle();
        }

        final long startNs = System.nanoTime();
        final UnsafeBuffer buffer = this.buffer;

        for (long i = 0; i < NUMBER_OF_MESSAGES; i++)
        {
            buffer.putLong(0, i);

            idleStrategy.reset();
            while (publication.offer(buffer, 0, MESSAGE_LENGTH) < 0)
            {
                idleStrategy.idle();
            }
        }

        final long stopPosition = publication.position();
        final CountersReader counters = aeron.countersReader();
        final int counterId = RecordingPos.findCounterIdBySession(counters, publication.sessionId());

        idleStrategy.reset();
        while (counters.getCounterValue(counterId) < stopPosition)
        {
            idleStrategy.idle();
        }

        final long durationMs = TimeUnit.NANOSECONDS.toMillis(System.nanoTime() - startNs);
        final double dataRate = (stopPosition * 1000.0d / durationMs) / MEGABYTE;
        final double recordingMb = stopPosition / MEGABYTE;
        final long msgRate = (NUMBER_OF_MESSAGES / durationMs) * 1000L;

        System.out.printf(
            "Recorded %.02f MB @ %.02f MB/s - %,d msg/sec - %d byte payload + 32 byte header%n",
            recordingMb, dataRate, msgRate, MESSAGE_LENGTH);

        return RecordingPos.getRecordingId(counters, counterId);
    }
}
 
Example 18
Source File: ClusterEventEncoder.java    From aeron with Apache License 2.0 4 votes vote down vote up
static int encodeNewLeadershipTerm(
    final UnsafeBuffer encodingBuffer,
    final int offset,
    final int captureLength,
    final int length,
    final long logLeadershipTermId,
    final long logTruncatePosition,
    final long leadershipTermId,
    final long logPosition,
    final long timestamp,
    final int leaderMemberId,
    final int logSessionId,
    final boolean isStartup)
{
    int relativeOffset = encodeLogHeader(encodingBuffer, offset, captureLength, length);

    encodingBuffer.putLong(offset + relativeOffset, logLeadershipTermId, LITTLE_ENDIAN);
    relativeOffset += SIZE_OF_LONG;

    encodingBuffer.putLong(offset + relativeOffset, logTruncatePosition, LITTLE_ENDIAN);
    relativeOffset += SIZE_OF_LONG;

    encodingBuffer.putLong(offset + relativeOffset, leadershipTermId, LITTLE_ENDIAN);
    relativeOffset += SIZE_OF_LONG;

    encodingBuffer.putLong(offset + relativeOffset, logPosition, LITTLE_ENDIAN);
    relativeOffset += SIZE_OF_LONG;

    encodingBuffer.putLong(offset + relativeOffset, timestamp, LITTLE_ENDIAN);
    relativeOffset += SIZE_OF_LONG;

    encodingBuffer.putInt(offset + relativeOffset, leaderMemberId, LITTLE_ENDIAN);
    relativeOffset += SIZE_OF_INT;

    encodingBuffer.putInt(offset + relativeOffset, logSessionId, LITTLE_ENDIAN);
    relativeOffset += SIZE_OF_INT;

    encodingBuffer.putInt(offset + relativeOffset, isStartup ? 1 : 0, LITTLE_ENDIAN);
    relativeOffset += SIZE_OF_INT;

    return relativeOffset;
}
 
Example 19
Source File: LogBufferDescriptor.java    From aeron with Apache License 2.0 2 votes vote down vote up
/**
 * Set the correlation ID used for this log relating to the command which created it.
 *
 * @param metadataBuffer containing the meta data.
 * @param id             value to be set.
 */
public static void correlationId(final UnsafeBuffer metadataBuffer, final long id)
{
    metadataBuffer.putLong(LOG_CORRELATION_ID_OFFSET, id);
}
 
Example 20
Source File: LogBufferDescriptor.java    From aeron with Apache License 2.0 2 votes vote down vote up
/**
 * Set the initial value for the termId in the upper bits of the tail counter.
 *
 * @param metadataBuffer contain the tail counter.
 * @param partitionIndex to be initialised.
 * @param termId         to be set.
 */
public static void initialiseTailWithTermId(
    final UnsafeBuffer metadataBuffer, final int partitionIndex, final int termId)
{
    metadataBuffer.putLong(TERM_TAIL_COUNTERS_OFFSET + (partitionIndex * SIZE_OF_LONG), packTail(termId, 0));
}