io.aeron.logbuffer.ControlledFragmentHandler Java Examples

The following examples show how to use io.aeron.logbuffer.ControlledFragmentHandler. 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: StressSessionHandler.java    From artio with Apache License 2.0 6 votes vote down vote up
public ControlledFragmentHandler.Action onMessage(
    final DirectBuffer buffer,
    final int offset,
    final int length,
    final int libraryId,
    final Session session,
    final int sequenceIndex,
    final long messageType,
    final long timestampInNs,
    final long position,
    final OnMessageInfo messageInfo)
{
    if (StressConfiguration.PRINT_EXCHANGE)
    {
        string.wrap(buffer);
        System.out.printf("%d -> %s%n", session.id(), printer.toString(string, offset, length, messageType));
    }

    return CONTINUE;
}
 
Example #2
Source File: Subscription.java    From aeron with Apache License 2.0 6 votes vote down vote up
/**
 * Poll in a controlled manner the {@link Image}s under the subscription for available message fragments.
 * Control is applied to fragments in the stream. If more fragments can be read on another stream
 * they will even if BREAK or ABORT is returned from the fragment handler.
 * <p>
 * Each fragment read will be a whole message if it is under MTU length. If larger than MTU then it will come
 * as a series of fragments ordered within a session.
 * <p>
 * To assemble messages that span multiple fragments then use {@link ControlledFragmentAssembler}.
 *
 * @param fragmentHandler callback for handling each message fragment as it is read.
 * @param fragmentLimit   number of message fragments to limit when polling across multiple {@link Image}s.
 * @return the number of fragments received
 * @see ControlledFragmentHandler
 */
public int controlledPoll(final ControlledFragmentHandler fragmentHandler, final int fragmentLimit)
{
    final Image[] images = this.images;
    final int length = images.length;
    int fragmentsRead = 0;

    int startingIndex = roundRobinIndex++;
    if (startingIndex >= length)
    {
        roundRobinIndex = startingIndex = 0;
    }

    for (int i = startingIndex; i < length && fragmentsRead < fragmentLimit; i++)
    {
        fragmentsRead += images[i].controlledPoll(fragmentHandler, fragmentLimit - fragmentsRead);
    }

    for (int i = 0; i < startingIndex && fragmentsRead < fragmentLimit; i++)
    {
        fragmentsRead += images[i].controlledPoll(fragmentHandler, fragmentLimit - fragmentsRead);
    }

    return fragmentsRead;
}
 
Example #3
Source File: SubscriptionSlowPeeker.java    From artio with Apache License 2.0 6 votes vote down vote up
int peek(final ControlledFragmentHandler handler)
{
    try
    {
        return super.peek(handler);
    }
    catch (final IllegalStateException e)
    {
        if (peekImage.isClosed() || normalImage.isClosed())
        {
            removeLibrary();
            return 1;
        }
        else
        {
            throw e;
        }
    }
}
 
Example #4
Source File: ProtocolSubscription.java    From artio with Apache License 2.0 6 votes vote down vote up
public static ControlledFragmentHandler of(
    final ProtocolHandler protocolHandler, final ControlledFragmentHandler other)
{
    final ProtocolSubscription subscription = new ProtocolSubscription(protocolHandler, UNKNOWN_TEMPLATE);
    return (buffer, offset, length, header) ->
    {
        final Action action = subscription.onFragment(buffer, offset, length, header);

        if (action == UNKNOWN_TEMPLATE)
        {
            return other.onFragment(buffer, offset, length, header);
        }

        return action;
    };
}
 
Example #5
Source File: LiveRecordingMessageTransceiver.java    From benchmarks with Apache License 2.0 5 votes vote down vote up
public ControlledFragmentHandler.Action onFragment(
    final DirectBuffer buffer, final int offset, final int length, final Header header)
{
    if (recordingPositionConsumed == recordingPosition)
    {
        return ABORT;
    }

    final long timestamp = buffer.getLong(offset, LITTLE_ENDIAN);
    final long checksum = buffer.getLong(offset + length - SIZE_OF_LONG, LITTLE_ENDIAN);
    onMessageReceived(timestamp, checksum);
    recordingPositionConsumed += align(length, FRAME_ALIGNMENT);

    return COMMIT;
}
 
Example #6
Source File: ControlledFragmentAssembler.java    From aeron with Apache License 2.0 5 votes vote down vote up
/**
 * Construct an adapter to reassemble message fragments and delegate on whole messages.
 *
 * @param delegate            onto which whole messages are forwarded.
 * @param initialBufferLength to be used for each session.
 * @param isDirectByteBuffer  is the underlying buffer to be a direct {@link java.nio.ByteBuffer}?
 */
public ControlledFragmentAssembler(
    final ControlledFragmentHandler delegate, final int initialBufferLength, final boolean isDirectByteBuffer)
{
    this.initialBufferLength = initialBufferLength;
    this.delegate = delegate;
    this.isDirectByteBuffer = isDirectByteBuffer;
}
 
Example #7
Source File: ConsensusModuleAgent.java    From aeron with Apache License 2.0 5 votes vote down vote up
ControlledFragmentAssembler.Action onIngressMessage(
    final long leadershipTermId,
    final long clusterSessionId,
    final DirectBuffer buffer,
    final int offset,
    final int length)
{
    if (leadershipTermId != this.leadershipTermId || Cluster.Role.LEADER != role)
    {
        return ControlledFragmentHandler.Action.CONTINUE;
    }

    final ClusterSession session = sessionByIdMap.get(clusterSessionId);
    if (null == session || session.state() == CLOSED)
    {
        return ControlledFragmentHandler.Action.CONTINUE;
    }

    if (session.state() == OPEN)
    {
        final long now = clusterClock.time();
        if (logPublisher.appendMessage(leadershipTermId, clusterSessionId, now, buffer, offset, length) > 0)
        {
            session.timeOfLastActivityNs(clusterTimeUnit.toNanos(now));
            return ControlledFragmentHandler.Action.CONTINUE;
        }
    }

    return ControlledFragmentHandler.Action.ABORT;
}
 
Example #8
Source File: InternalSession.java    From artio with Apache License 2.0 5 votes vote down vote up
public ControlledFragmentHandler.Action onInvalidMessage(
    final int refSeqNum,
    final int refTagId,
    final char[] refMsgType,
    final int refMsgTypeLength,
    final int rejectReason, final long position)
{
    return super.onInvalidMessage(refSeqNum, refTagId, refMsgType, refMsgTypeLength, rejectReason, position);
}
 
Example #9
Source File: StressSessionHandler.java    From artio with Apache License 2.0 5 votes vote down vote up
public ControlledFragmentHandler.Action onDisconnect(
    final int libraryId, final Session session, final DisconnectReason reason)
{
    if (StressConfiguration.PRINT_EXCHANGE)
    {
        System.out.printf("%d Disconnected: %s%n", session.id(), reason);
    }

    return CONTINUE;
}
 
Example #10
Source File: ReportFactory.java    From artio with Apache License 2.0 5 votes vote down vote up
public ControlledFragmentHandler.Action sendReport(final Session session, final Side side)
{
    encodedLength = encoder.putLongAscii(0, session.lastSentMsgSeqNum());

    executionReport.orderID(encodeBuffer, encodedLength)
        .execID(encodeBuffer, encodedLength);

    executionReport.execType(ExecType.FILL)
        .ordStatus(OrdStatus.FILLED)
        .side(side);

    executionReport.instrument().symbol(MSFT.getBytes(US_ASCII));

    return Pressure.apply(session.trySend(executionReport));
}
 
Example #11
Source File: ReplayerTest.java    From artio with Apache License 2.0 5 votes vote down vote up
@Test
public void shouldSupportConcurrentReplayRequests()
{
    onReplay(END_SEQ_NO, inv -> true);

    onReplayOtherSession(END_SEQ_NO);

    // Two queries means two handlers
    final List<MessageTracker> messageTrackers = messageTracker.getAllValues();
    assertEquals(2, messageTrackers.size());
    final ControlledFragmentHandler firstHandler = messageTrackers.get(0);
    final ControlledFragmentHandler secondHandler = messageTrackers.get(1);
    assertNotSame(firstHandler, secondHandler);

    // First replay
    bufferContainsExampleMessage(true);
    final int srcLength = fragmentLength();
    setupMessage(srcLength);
    onFragment(srcLength, CONTINUE, firstHandler);

    // Second replay
    bufferContainsExampleMessage(true, SESSION_ID_2, SEQUENCE_NUMBER, SEQUENCE_INDEX);
    setupMessage(srcLength);
    onFragment(srcLength, CONTINUE, secondHandler);

    assertHasResentWithPossDupFlag(srcLength, times(2));
}
 
Example #12
Source File: SubscriptionSlowPeeker.java    From artio with Apache License 2.0 5 votes vote down vote up
int peek(final ControlledFragmentHandler handler)
{
    int bytesRead = 0;
    for (final LibrarySlowPeeker slowPeeker : sessionIdToImagePeeker.values())
    {
        bytesRead += slowPeeker.peek(handler);
    }
    return bytesRead;
}
 
Example #13
Source File: ReplayerTest.java    From artio with Apache License 2.0 4 votes vote down vote up
private void onFragment(final int length, final Action expectedAction, final ControlledFragmentHandler handler)
{
    final Action action = handler.onFragment(buffer, START, length, fragmentHeader);
    assertEquals(expectedAction, action);
}
 
Example #14
Source File: ILink3MessageTracker.java    From artio with Apache License 2.0 4 votes vote down vote up
public ILink3MessageTracker(final ControlledFragmentHandler messageHandler)
{
    super(LogTag.REPLAY, messageHandler);
}
 
Example #15
Source File: RecordingSignalAdapter.java    From aeron with Apache License 2.0 4 votes vote down vote up
private ControlledFragmentHandler.Action onFragment(
    final DirectBuffer buffer, final int offset, final int length, final Header header)
{
    if (isDone)
    {
        return ABORT;
    }

    messageHeaderDecoder.wrap(buffer, offset);

    final int schemaId = messageHeaderDecoder.schemaId();
    if (schemaId != MessageHeaderDecoder.SCHEMA_ID)
    {
        throw new ArchiveException("expected schemaId=" + MessageHeaderDecoder.SCHEMA_ID + ", actual=" + schemaId);
    }

    final int templateId = messageHeaderDecoder.templateId();
    switch (templateId)
    {
        case ControlResponseDecoder.TEMPLATE_ID:
            controlResponseDecoder.wrap(
                buffer,
                offset + MessageHeaderDecoder.ENCODED_LENGTH,
                messageHeaderDecoder.blockLength(),
                messageHeaderDecoder.version());

            if (controlResponseDecoder.controlSessionId() == controlSessionId)
            {
                controlEventListener.onResponse(
                    controlSessionId,
                    controlResponseDecoder.correlationId(),
                    controlResponseDecoder.relevantId(),
                    controlResponseDecoder.code(),
                    controlResponseDecoder.errorMessage());

                isDone = true;
                return BREAK;
            }
            break;

        case RecordingSignalEventDecoder.TEMPLATE_ID:
            recordingSignalEventDecoder.wrap(
                buffer,
                offset + MessageHeaderDecoder.ENCODED_LENGTH,
                messageHeaderDecoder.blockLength(),
                messageHeaderDecoder.version());

            if (recordingSignalEventDecoder.controlSessionId() == controlSessionId)
            {
                recordingSignalConsumer.onSignal(
                    recordingSignalEventDecoder.controlSessionId(),
                    recordingSignalEventDecoder.correlationId(),
                    recordingSignalEventDecoder.recordingId(),
                    recordingSignalEventDecoder.subscriptionId(),
                    recordingSignalEventDecoder.position(),
                    recordingSignalEventDecoder.signal());

                isDone = true;
                return BREAK;
            }
            break;
    }

    return CONTINUE;
}
 
Example #16
Source File: FixMessageTracker.java    From artio with Apache License 2.0 4 votes vote down vote up
public FixMessageTracker(final LogTag logTag, final ControlledFragmentHandler messageHandler, final long sessionId)
{
    super(logTag, messageHandler);
    this.sessionId = sessionId;
}
 
Example #17
Source File: FixMessageTracker.java    From artio with Apache License 2.0 4 votes vote down vote up
public ControlledFragmentHandler.Action onFragment(
    final DirectBuffer buffer, final int offset, final int length, final Header header)
{
    messageHeaderDecoder.wrap(buffer, offset);

    if (messageHeaderDecoder.templateId() == FixMessageDecoder.TEMPLATE_ID)
    {
        final int messageOffset = offset + MessageHeaderDecoder.ENCODED_LENGTH;
        if (sessionId != UNK_SESSION)
        {
            messageDecoder.wrap(
                buffer,
                messageOffset,
                messageHeaderDecoder.blockLength(),
                messageHeaderDecoder.version()
            );

            if (messageDecoder.session() != sessionId)
            {
                return CONTINUE;
            }
        }

        if (DebugLogger.isEnabled(logTag))
        {
            messageDecoder.skipMetaData();
            final int bodyLength = messageDecoder.bodyLength();
            final int bodyOffset = messageDecoder.limit();
            final CharFormatter formatter = FOUND_REPLAY_MESSAGE.get();
            formatter.clear();
            DebugLogger.log(logTag, formatter, buffer, bodyOffset, bodyLength);
        }

        final ControlledFragmentHandler.Action action = messageHandler.onFragment(buffer, offset, length, header);
        if (action != ABORT)
        {
            count++;
        }
        return action;
    }

    return CONTINUE;
}
 
Example #18
Source File: MessageTracker.java    From artio with Apache License 2.0 4 votes vote down vote up
MessageTracker(final LogTag logTag, final ControlledFragmentHandler messageHandler)
{
    this.logTag = logTag;
    this.messageHandler = messageHandler;
}
 
Example #19
Source File: ReplayerTest.java    From artio with Apache License 2.0 4 votes vote down vote up
private ControlledFragmentHandler getMessageTracker()
{
    return messageTracker.getValue();
}
 
Example #20
Source File: RetryManagerTest.java    From artio with Apache License 2.0 4 votes vote down vote up
private void attemptResultsIn(final ControlledFragmentHandler.Action action)
{
    when(unitOfWork.attemptToAction()).thenReturn(action);
}
 
Example #21
Source File: ControlledEgressListener.java    From aeron with Apache License 2.0 3 votes vote down vote up
/**
 * Message event returned from the clustered service.
 *
 * @param clusterSessionId to which the message belongs.
 * @param timestamp        at which the correlated ingress was sequenced in the cluster.
 * @param buffer           containing the message.
 * @param offset           at which the message begins.
 * @param length           of the message in bytes.
 * @param header           Aeron header associated with the message fragment.
 * @return what action should be taken regarding advancement of the stream.
 */
ControlledFragmentHandler.Action onMessage(
    long clusterSessionId,
    long timestamp,
    DirectBuffer buffer,
    int offset,
    int length,
    Header header);
 
Example #22
Source File: ImageControlledFragmentAssembler.java    From aeron with Apache License 2.0 3 votes vote down vote up
/**
 * Construct an adapter to reassemble message fragments and delegate on whole messages.
 *
 * @param delegate            onto which whole messages are forwarded.
 * @param initialBufferLength to be used for the session.
 * @param isDirectByteBuffer  is the underlying buffer to be a direct {@link java.nio.ByteBuffer}?
 */
public ImageControlledFragmentAssembler(
    final ControlledFragmentHandler delegate, final int initialBufferLength, final boolean isDirectByteBuffer)
{
    this.delegate = delegate;
    this.builder = new BufferBuilder(initialBufferLength, isDirectByteBuffer);
}
 
Example #23
Source File: ControlledFragmentAssembler.java    From aeron with Apache License 2.0 2 votes vote down vote up
/**
 * Construct an adapter to reassemble message fragments and delegate on whole messages.
 *
 * @param delegate onto which whole messages are forwarded.
 */
public ControlledFragmentAssembler(final ControlledFragmentHandler delegate)
{
    this(delegate, 0, false);
}
 
Example #24
Source File: ControlledFragmentAssembler.java    From aeron with Apache License 2.0 2 votes vote down vote up
/**
 * Construct an adapter to reassemble message fragments and delegate on whole messages.
 *
 * @param delegate            onto which whole messages are forwarded.
 * @param initialBufferLength to be used for each session.
 */
public ControlledFragmentAssembler(final ControlledFragmentHandler delegate, final int initialBufferLength)
{
    this(delegate, initialBufferLength, false);
}
 
Example #25
Source File: ControlledFragmentAssembler.java    From aeron with Apache License 2.0 2 votes vote down vote up
/**
 * Get the delegate unto which assembled messages are delegated.
 *
 * @return the delegate unto which assembled messages are delegated.
 */
public ControlledFragmentHandler delegate()
{
    return delegate;
}
 
Example #26
Source File: ImageControlledFragmentAssembler.java    From aeron with Apache License 2.0 2 votes vote down vote up
/**
 * Construct an adapter to reassemble message fragments and delegate on whole messages.
 *
 * @param delegate onto which whole messages are forwarded.
 */
public ImageControlledFragmentAssembler(final ControlledFragmentHandler delegate)
{
    this(delegate, 0, false);
}
 
Example #27
Source File: ImageControlledFragmentAssembler.java    From aeron with Apache License 2.0 2 votes vote down vote up
/**
 * Construct an adapter to reassemble message fragments and delegate on whole messages.
 *
 * @param delegate            onto which whole messages are forwarded.
 * @param initialBufferLength to be used for the session.
 */
public ImageControlledFragmentAssembler(final ControlledFragmentHandler delegate, final int initialBufferLength)
{
    this(delegate, initialBufferLength, false);
}
 
Example #28
Source File: ImageControlledFragmentAssembler.java    From aeron with Apache License 2.0 2 votes vote down vote up
/**
 * Get the delegate unto which assembled messages are delegated.
 *
 * @return the delegate unto which assembled messages are delegated.
 */
public ControlledFragmentHandler delegate()
{
    return delegate;
}
 
Example #29
Source File: ReplayProtocolHandler.java    From artio with Apache License 2.0 votes vote down vote up
ControlledFragmentHandler.Action onReplayComplete(long connectionId);