org.agrona.collections.MutableLong Java Examples

The following examples show how to use org.agrona.collections.MutableLong. 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: ListRecordingsForUriSessionTest.java    From aeron with Apache License 2.0 6 votes vote down vote up
@Test
public void shouldSendAllDescriptors()
{
    final ListRecordingsForUriSession session = new ListRecordingsForUriSession(
        correlationId,
        0,
        3,
        LOCALHOST_BYTES,
        1,
        catalog,
        controlResponseProxy,
        controlSession,
        descriptorBuffer,
        recordingDescriptorDecoder);

    final MutableLong counter = new MutableLong(0);
    when(controlSession.sendDescriptor(eq(correlationId), any(), eq(controlResponseProxy)))
        .then(verifySendDescriptor(counter));

    session.doWork();
    verify(controlSession, times(3)).sendDescriptor(eq(correlationId), any(), eq(controlResponseProxy));
}
 
Example #2
Source File: ListRecordingsForUriSessionTest.java    From aeron with Apache License 2.0 6 votes vote down vote up
@Test
public void shouldSend2Descriptors()
{
    final long fromRecordingId = 1;
    final ListRecordingsForUriSession session = new ListRecordingsForUriSession(
        correlationId,
        fromRecordingId,
        2,
        LOCALHOST_BYTES,
        1,
        catalog,
        controlResponseProxy,
        controlSession,
        descriptorBuffer,
        recordingDescriptorDecoder);

    final MutableLong counter = new MutableLong(fromRecordingId);
    when(controlSession.sendDescriptor(eq(correlationId), any(), eq(controlResponseProxy)))
        .then(verifySendDescriptor(counter));

    session.doWork();
    verify(controlSession, times(2)).sendDescriptor(eq(correlationId), any(), eq(controlResponseProxy));
}
 
Example #3
Source File: ClusterTool.java    From aeron with Apache License 2.0 6 votes vote down vote up
public static long nextBackupQueryDeadlineMs(final ClusterMarkFile markFile)
{
    final String aeronDirectoryName = markFile.decoder().aeronDirectory();
    final MutableLong nextQueryMs = new MutableLong(NULL_VALUE);

    try (Aeron aeron = Aeron.connect(new Aeron.Context().aeronDirectoryName(aeronDirectoryName)))
    {
        aeron.countersReader().forEach(
            (counterId, typeId, keyBuffer, label) ->
            {
                if (ClusterBackup.QUERY_DEADLINE_TYPE_ID == typeId)
                {
                    nextQueryMs.value = aeron.countersReader().getCounterValue(counterId);
                }
            });
    }

    return nextQueryMs.value;
}
 
Example #4
Source File: ListRecordingsForUriSessionTest.java    From aeron with Apache License 2.0 6 votes vote down vote up
@Test
public void shouldSend2DescriptorsAndRecordingUnknown()
{
    final ListRecordingsForUriSession session = new ListRecordingsForUriSession(
        correlationId,
        1,
        5,
        LOCALHOST_BYTES,
        1,
        catalog,
        controlResponseProxy,
        controlSession,
        descriptorBuffer,
        recordingDescriptorDecoder);

    final MutableLong counter = new MutableLong(1);
    when(controlSession.sendDescriptor(eq(correlationId), any(), eq(controlResponseProxy)))
        .then(verifySendDescriptor(counter));

    session.doWork();

    verify(controlSession, times(2)).sendDescriptor(eq(correlationId), any(), eq(controlResponseProxy));
    verify(controlSession).sendRecordingUnknown(eq(correlationId), eq(5L), eq(controlResponseProxy));
}
 
Example #5
Source File: ListRecordingsForUriSessionTest.java    From aeron with Apache License 2.0 6 votes vote down vote up
private Answer<Object> verifySendDescriptor(final MutableLong counter)
{
    return (invocation) ->
    {
        final UnsafeBuffer buffer = invocation.getArgument(1);
        recordingDescriptorDecoder.wrap(
            buffer,
            RecordingDescriptorHeaderDecoder.BLOCK_LENGTH,
            RecordingDescriptorDecoder.BLOCK_LENGTH,
            RecordingDescriptorDecoder.SCHEMA_VERSION);

        final int i = counter.intValue();
        assertEquals(matchingRecordingIds[i], recordingDescriptorDecoder.recordingId());
        counter.set(i + 1);

        return buffer.getInt(0);
    };
}
 
Example #6
Source File: ListRecordingsSessionTest.java    From aeron with Apache License 2.0 6 votes vote down vote up
@Test
public void shouldSendAllDescriptors()
{
    final ListRecordingsSession session = new ListRecordingsSession(
        correlationId,
        0,
        3,
        catalog,
        controlResponseProxy,
        controlSession,
        descriptorBuffer);

    final MutableLong counter = new MutableLong(0);
    when(controlSession.sendDescriptor(eq(correlationId), any(), eq(controlResponseProxy)))
        .then(verifySendDescriptor(counter));

    session.doWork();
    verify(controlSession, times(3)).sendDescriptor(eq(correlationId), any(), eq(controlResponseProxy));
}
 
Example #7
Source File: ListRecordingsSessionTest.java    From aeron with Apache License 2.0 6 votes vote down vote up
@Test
public void shouldSend2Descriptors()
{
    final int fromId = 1;
    final ListRecordingsSession session = new ListRecordingsSession(
        correlationId,
        fromId,
        2,
        catalog,
        controlResponseProxy,
        controlSession,
        descriptorBuffer);

    final MutableLong counter = new MutableLong(fromId);
    when(controlSession.sendDescriptor(eq(correlationId), any(), eq(controlResponseProxy)))
        .then(verifySendDescriptor(counter));

    session.doWork();
    verify(controlSession, times(2)).sendDescriptor(eq(correlationId), any(), eq(controlResponseProxy));
}
 
Example #8
Source File: ListRecordingsSessionTest.java    From aeron with Apache License 2.0 6 votes vote down vote up
@Test
public void shouldResendDescriptorWhenSendFails()
{
    final long fromRecordingId = 1;
    final ListRecordingsSession session = new ListRecordingsSession(
        correlationId,
        fromRecordingId,
        1,
        catalog,
        controlResponseProxy,
        controlSession,
        descriptorBuffer);

    when(controlSession.sendDescriptor(eq(correlationId), any(), eq(controlResponseProxy))).thenReturn(0);
    session.doWork();
    verify(controlSession, times(1)).sendDescriptor(eq(correlationId), any(), eq(controlResponseProxy));

    final MutableLong counter = new MutableLong(fromRecordingId);
    when(controlSession.sendDescriptor(eq(correlationId), any(), eq(controlResponseProxy)))
        .then(verifySendDescriptor(counter));

    session.doWork();
    verify(controlSession, times(2)).sendDescriptor(eq(correlationId), any(), eq(controlResponseProxy));
}
 
Example #9
Source File: ListRecordingsSessionTest.java    From aeron with Apache License 2.0 6 votes vote down vote up
@Test
public void shouldSendTwoDescriptorsThenRecordingUnknown()
{
    final ListRecordingsSession session = new ListRecordingsSession(
        correlationId,
        1,
        3,
        catalog,
        controlResponseProxy,
        controlSession,
        descriptorBuffer);

    final MutableLong counter = new MutableLong(1);
    when(controlSession.sendDescriptor(eq(correlationId), any(), eq(controlResponseProxy)))
        .then(verifySendDescriptor(counter));

    session.doWork();

    verify(controlSession, times(2)).sendDescriptor(eq(correlationId), any(), eq(controlResponseProxy));
    verify(controlSession).sendRecordingUnknown(eq(correlationId), eq(3L), eq(controlResponseProxy));
}
 
Example #10
Source File: ListRecordingsSessionTest.java    From aeron with Apache License 2.0 6 votes vote down vote up
private Answer<Object> verifySendDescriptor(final MutableLong counter)
{
    return (invocation) ->
    {
        final UnsafeBuffer buffer = invocation.getArgument(1);

        recordingDescriptorDecoder.wrap(
            buffer,
            RecordingDescriptorHeaderDecoder.BLOCK_LENGTH,
            RecordingDescriptorDecoder.BLOCK_LENGTH,
            RecordingDescriptorDecoder.SCHEMA_VERSION);

        final int i = counter.intValue();
        assertEquals(recordingIds[i], recordingDescriptorDecoder.recordingId());
        counter.set(i + 1);

        return buffer.getInt(0);
    };
}
 
Example #11
Source File: ReplicateRecordingTest.java    From aeron with Apache License 2.0 6 votes vote down vote up
@Test
public void shouldThrowExceptionWhenSrcRecordingIdUnknown()
{
    final long unknownId = 7L;
    final ControlEventListener listener = mock(ControlEventListener.class);
    final MutableLong dstRecordingId = new MutableLong();
    final MutableReference<RecordingSignal> signalRef = new MutableReference<>();
    final RecordingSignalAdapter adapter = newRecordingSignalAdapter(listener, signalRef, dstRecordingId);

    final long replicationId = dstAeronArchive.replicate(
        unknownId, NULL_VALUE, SRC_CONTROL_STREAM_ID, SRC_CONTROL_REQUEST_CHANNEL, null);

    awaitSignalOrResponse(signalRef, adapter);

    verify(listener).onResponse(
        eq(dstAeronArchive.controlSessionId()),
        eq(replicationId),
        eq((long)ArchiveException.UNKNOWN_RECORDING),
        eq(ControlResponseCode.ERROR),
        anyString());
}
 
Example #12
Source File: ReplicateRecordingTest.java    From aeron with Apache License 2.0 5 votes vote down vote up
private RecordingSignalAdapter newRecordingSignalAdapter(
    final MutableReference<RecordingSignal> signalRef, final MutableLong recordingIdRef)
{
    final ControlEventListener listener =
        (controlSessionId, correlationId, relevantId, code, errorMessage) ->
        {
            if (code == ControlResponseCode.ERROR)
            {
                throw new ArchiveException(errorMessage, (int)relevantId, correlationId);
            }
        };

    return newRecordingSignalAdapter(listener, signalRef, recordingIdRef);
}
 
Example #13
Source File: DeadlineTimerWheelTest.java    From agrona with Apache License 2.0 5 votes vote down vote up
@Test
public void shouldScheduleDeadlineInThePast()
{
    assertTimeoutPreemptively(ofSeconds(1), () ->
    {
        long controlTimestamp = 100 * RESOLUTION;
        final MutableLong firedTimestamp = new MutableLong(-1);
        final DeadlineTimerWheel wheel = new DeadlineTimerWheel(TIME_UNIT, controlTimestamp, RESOLUTION, 1024);

        final long deadline = controlTimestamp - 3;
        final long id = wheel.scheduleTimer(deadline);

        do
        {
            wheel.poll(
                controlTimestamp,
                (timeUnit, now, timerId) ->
                {
                    assertThat(timerId, is(id));
                    firedTimestamp.value = now;
                    return true;
                },
                Integer.MAX_VALUE);

            controlTimestamp += wheel.tickResolution();
        }
        while (-1 == firedTimestamp.value);

        assertThat(firedTimestamp.value, greaterThan(deadline));
    });
}
 
Example #14
Source File: DeadlineTimerWheelTest.java    From agrona with Apache License 2.0 5 votes vote down vote up
@Test
public void shouldHandleMultipleTimersInDifferentTicks()
{
    assertTimeoutPreemptively(ofSeconds(1), () ->
    {
        long controlTimestamp = 0;
        final MutableLong firedTimestamp1 = new MutableLong(-1);
        final MutableLong firedTimestamp2 = new MutableLong(-1);
        final DeadlineTimerWheel wheel = new DeadlineTimerWheel(TIME_UNIT, controlTimestamp, RESOLUTION, 256);

        final long id1 = wheel.scheduleTimer(controlTimestamp + (15 * wheel.tickResolution()));
        final long id2 = wheel.scheduleTimer(controlTimestamp + (23 * wheel.tickResolution()));

        do
        {
            wheel.poll(
                controlTimestamp,
                (timeUnit, now, timerId) ->
                {
                    if (timerId == id1)
                    {
                        firedTimestamp1.value = now;
                    }
                    else if (timerId == id2)
                    {
                        firedTimestamp2.value = now;
                    }

                    return true;
                },
                Integer.MAX_VALUE);

            controlTimestamp += wheel.tickResolution();
        }
        while (-1 == firedTimestamp1.value || -1 == firedTimestamp2.value);

        assertThat(firedTimestamp1.value, is(16 * wheel.tickResolution()));
        assertThat(firedTimestamp2.value, is(24 * wheel.tickResolution()));
    });
}
 
Example #15
Source File: DeadlineTimerWheelTest.java    From agrona with Apache License 2.0 5 votes vote down vote up
@Test
public void shouldHandleExpiringTimersInPreviousTicks()
{
    assertTimeoutPreemptively(ofSeconds(1), () ->
    {
        long controlTimestamp = 0;
        final MutableLong firedTimestamp = new MutableLong(-1);
        final DeadlineTimerWheel wheel = new DeadlineTimerWheel(TIME_UNIT, controlTimestamp, RESOLUTION, 256);

        final long id = wheel.scheduleTimer(controlTimestamp + (15 * wheel.tickResolution()));

        final long pollStartTimeNs = 32 * wheel.tickResolution();
        controlTimestamp += pollStartTimeNs;

        do
        {
            wheel.poll(
                controlTimestamp,
                (timeUnit, now, timerId) ->
                {
                    assertThat(timerId, is(id));
                    firedTimestamp.value = now;
                    return true;
                },
                Integer.MAX_VALUE);

            if (wheel.currentTickTime() > pollStartTimeNs)
            {
                controlTimestamp += wheel.tickResolution();
            }
        }
        while (-1 == firedTimestamp.value && controlTimestamp < (128 * wheel.tickResolution()));

        assertThat(firedTimestamp.value, is(pollStartTimeNs));
    });
}
 
Example #16
Source File: ReplayedBasicSubscriber.java    From aeron with Apache License 2.0 5 votes vote down vote up
private static long findLatestRecording(final AeronArchive archive)
{
    final MutableLong lastRecordingId = new MutableLong();

    final RecordingDescriptorConsumer consumer =
        (controlSessionId,
        correlationId,
        recordingId,
        startTimestamp,
        stopTimestamp,
        startPosition,
        stopPosition,
        initialTermId,
        segmentFileLength,
        termBufferLength,
        mtuLength,
        sessionId,
        streamId,
        strippedChannel,
        originalChannel,
        sourceIdentity) -> lastRecordingId.set(recordingId);

    final long fromRecordingId = 0L;
    final int recordCount = 100;

    final int foundCount = archive.listRecordingsForUri(fromRecordingId, recordCount, CHANNEL, STREAM_ID, consumer);

    if (foundCount == 0)
    {
        throw new IllegalStateException("no recordings found");
    }

    return lastRecordingId.get();
}
 
Example #17
Source File: EmbeddedReplayThroughput.java    From aeron with Apache License 2.0 5 votes vote down vote up
private long findRecordingId(final String expectedChannel)
{
    final MutableLong foundRecordingId = new MutableLong();

    final RecordingDescriptorConsumer consumer =
        (controlSessionId,
        correlationId,
        recordingId,
        startTimestamp,
        stopTimestamp,
        startPosition,
        stopPosition,
        initialTermId,
        segmentFileLength,
        termBufferLength,
        mtuLength,
        sessionId,
        streamId,
        strippedChannel,
        originalChannel,
        sourceIdentity) -> foundRecordingId.set(recordingId);

    final int recordingsFound = aeronArchive.listRecordingsForUri(
        0L, 10, expectedChannel, STREAM_ID, consumer);

    if (1 != recordingsFound)
    {
        throw new IllegalStateException("should have been only one recording");
    }

    return foundRecordingId.get();
}
 
Example #18
Source File: DeadlineTimerWheelTest.java    From agrona with Apache License 2.0 5 votes vote down vote up
@Test
public void shouldBeAbleToScheduleTimerOnEdgeOfTick()
{
    assertTimeoutPreemptively(ofSeconds(1), () ->
    {
        long controlTimestamp = 0;
        final MutableLong firedTimestamp = new MutableLong(-1);
        final DeadlineTimerWheel wheel = new DeadlineTimerWheel(TIME_UNIT, controlTimestamp, RESOLUTION, 1024);

        final long deadline = 5 * wheel.tickResolution();
        final long id = wheel.scheduleTimer(deadline);
        assertEquals(wheel.deadline(id), deadline);

        do
        {
            wheel.poll(
                controlTimestamp,
                (timeUnit, now, timerId) ->
                {
                    assertThat(timerId, is(id));
                    firedTimestamp.value = now;
                    return true;
                },
                Integer.MAX_VALUE);

            controlTimestamp += wheel.tickResolution();
        }
        while (-1 == firedTimestamp.value);

        // this is the first tick after the timer, so it should be on this edge
        assertThat(firedTimestamp.value, is(6 * wheel.tickResolution()));
    });
}
 
Example #19
Source File: DeadlineTimerWheelTest.java    From agrona with Apache License 2.0 5 votes vote down vote up
@Test
public void shouldHandleNonZeroStartTime()
{
    assertTimeoutPreemptively(ofSeconds(1), () ->
    {
        long controlTimestamp = 100 * RESOLUTION;
        final MutableLong firedTimestamp = new MutableLong(-1);
        final DeadlineTimerWheel wheel = new DeadlineTimerWheel(TIME_UNIT, controlTimestamp, RESOLUTION, 1024);

        final long id = wheel.scheduleTimer(controlTimestamp + (5 * wheel.tickResolution()));

        do
        {
            wheel.poll(
                controlTimestamp,
                (timeUnit, now, timerId) ->
                {
                    assertThat(timerId, is(id));
                    firedTimestamp.value = now;
                    return true;
                },
                Integer.MAX_VALUE);

            controlTimestamp += wheel.tickResolution();
        }
        while (-1 == firedTimestamp.value);

        // this is the first tick after the timer, so it should be on this edge
        assertThat(firedTimestamp.value, is(106 * wheel.tickResolution()));
    });
}
 
Example #20
Source File: DeadlineTimerWheelTest.java    From agrona with Apache License 2.0 5 votes vote down vote up
@Test
public void shouldHandleNanoTimeUnitTimers()
{
    assertTimeoutPreemptively(ofSeconds(1), () ->
    {
        long controlTimestamp = 0;
        final MutableLong firedTimestamp = new MutableLong(-1);
        final DeadlineTimerWheel wheel = new DeadlineTimerWheel(TIME_UNIT, controlTimestamp, RESOLUTION, 1024);

        final long id = wheel.scheduleTimer(controlTimestamp + (5 * wheel.tickResolution()) + 1);

        do
        {
            wheel.poll(
                controlTimestamp,
                (timeUnit, now, timerId) ->
                {
                    assertThat(timerId, is(id));
                    firedTimestamp.value = now;
                    return true;
                },
                Integer.MAX_VALUE);

            controlTimestamp += wheel.tickResolution();
        }
        while (-1 == firedTimestamp.value);

        // this is the first tick after the timer, so it should be on this edge
        assertThat(firedTimestamp.value, is(6 * wheel.tickResolution()));
    });
}
 
Example #21
Source File: DeadlineTimerWheelTest.java    From agrona with Apache License 2.0 5 votes vote down vote up
@Test
public void shouldHandleMultipleRounds()
{
    assertTimeoutPreemptively(ofSeconds(1), () ->
    {
        long controlTimestamp = 0;
        final MutableLong firedTimestamp = new MutableLong(-1);
        final DeadlineTimerWheel wheel = new DeadlineTimerWheel(TIME_UNIT, controlTimestamp, RESOLUTION, 16);

        final long id = wheel.scheduleTimer(controlTimestamp + (63 * wheel.tickResolution()));

        do
        {
            wheel.poll(
                controlTimestamp,
                (timeUnit, now, timerId) ->
                {
                    assertThat(timerId, is(id));
                    firedTimestamp.value = now;
                    return true;
                },
                Integer.MAX_VALUE);

            controlTimestamp += wheel.tickResolution();
        }
        while (-1 == firedTimestamp.value);

        // this is the first tick after the timer, so it should be on this edge
        assertThat(firedTimestamp.value, is(64 * wheel.tickResolution()));
    });
}
 
Example #22
Source File: OrderBookDirectImpl.java    From exchange-core with Apache License 2.0 5 votes vote down vote up
@Override
public long getTotalOrdersVolume(OrderAction action) {
    final LongAdaptiveRadixTreeMap<Bucket> buckets = action == OrderAction.ASK ? askPriceBuckets : bidPriceBuckets;
    final MutableLong accum = new MutableLong();
    buckets.forEach((p, b) -> accum.value += b.volume, Integer.MAX_VALUE);
    return accum.value;
}
 
Example #23
Source File: ReplicateRecordingTest.java    From aeron with Apache License 2.0 5 votes vote down vote up
@Test
public void shouldReplicateLiveRecordingAndMergeBeforeDataFlows()
{
    final String messagePrefix = "Message-Prefix-";
    final int messageCount = 10;
    final long srcRecordingId;

    final long subscriptionId = srcAeronArchive.startRecording(LIVE_CHANNEL, LIVE_STREAM_ID, LOCAL);
    final MutableReference<RecordingSignal> signalRef = new MutableReference<>();
    final RecordingSignalAdapter adapter;

    try (Publication publication = srcAeron.addPublication(LIVE_CHANNEL, LIVE_STREAM_ID))
    {
        final CountersReader srcCounters = srcAeron.countersReader();
        final int counterId = awaitRecordingCounterId(srcCounters, publication.sessionId());
        srcRecordingId = RecordingPos.getRecordingId(srcCounters, counterId);

        final MutableLong dstRecordingId = new MutableLong();
        adapter = newRecordingSignalAdapter(signalRef, dstRecordingId);

        dstAeronArchive.replicate(
            srcRecordingId, NULL_VALUE, SRC_CONTROL_STREAM_ID, SRC_CONTROL_REQUEST_CHANNEL, LIVE_CHANNEL);

        assertEquals(RecordingSignal.REPLICATE, awaitSignal(signalRef, adapter));
        assertEquals(RecordingSignal.EXTEND, awaitSignal(signalRef, adapter));
        assertEquals(RecordingSignal.MERGE, awaitSignal(signalRef, adapter));

        final CountersReader dstCounters = dstAeron.countersReader();
        final int dstCounterId = RecordingPos.findCounterIdByRecording(dstCounters, dstRecordingId.get());

        offer(publication, messageCount, messagePrefix);
        awaitPosition(dstCounters, dstCounterId, publication.position());
    }

    srcAeronArchive.stopRecording(subscriptionId);

    assertEquals(RecordingSignal.STOP, awaitSignal(signalRef, adapter));
}
 
Example #24
Source File: DeadlineTimerWheelTest.java    From agrona with Apache License 2.0 5 votes vote down vote up
@Test
public void shouldHandleMultipleTimersInSameTickSameRound()
{
    assertTimeoutPreemptively(ofSeconds(1), () ->
    {
        long controlTimestamp = 0;
        final MutableLong firedTimestamp1 = new MutableLong(-1);
        final MutableLong firedTimestamp2 = new MutableLong(-1);
        final DeadlineTimerWheel wheel = new DeadlineTimerWheel(TIME_UNIT, controlTimestamp, RESOLUTION, 8);

        final long id1 = wheel.scheduleTimer(controlTimestamp + (15 * wheel.tickResolution()));
        final long id2 = wheel.scheduleTimer(controlTimestamp + (15 * wheel.tickResolution()));

        do
        {
            wheel.poll(
                controlTimestamp,
                (timeUnit, now, timerId) ->
                {
                    if (timerId == id1)
                    {
                        firedTimestamp1.value = now;
                    }
                    else if (timerId == id2)
                    {
                        firedTimestamp2.value = now;
                    }

                    return true;
                },
                Integer.MAX_VALUE);

            controlTimestamp += wheel.tickResolution();
        }
        while (-1 == firedTimestamp1.value || -1 == firedTimestamp2.value);

        assertThat(firedTimestamp1.value, is(16 * wheel.tickResolution()));
        assertThat(firedTimestamp2.value, is(16 * wheel.tickResolution()));
    });
}
 
Example #25
Source File: DeadlineTimerWheelTest.java    From agrona with Apache License 2.0 5 votes vote down vote up
@Test
public void shouldHandleMultipleTimersInSameTickDifferentRound()
{
    assertTimeoutPreemptively(ofSeconds(1), () ->
    {
        long controlTimestamp = 0;
        final MutableLong firedTimestamp1 = new MutableLong(-1);
        final MutableLong firedTimestamp2 = new MutableLong(-1);
        final DeadlineTimerWheel wheel = new DeadlineTimerWheel(TIME_UNIT, controlTimestamp, RESOLUTION, 8);

        final long id1 = wheel.scheduleTimer(controlTimestamp + (15 * wheel.tickResolution()));
        final long id2 = wheel.scheduleTimer(controlTimestamp + (23 * wheel.tickResolution()));

        do
        {
            wheel.poll(
                controlTimestamp,
                (timeUnit, now, timerId) ->
                {
                    if (timerId == id1)
                    {
                        firedTimestamp1.value = now;
                    }
                    else if (timerId == id2)
                    {
                        firedTimestamp2.value = now;
                    }

                    return true;
                },
                Integer.MAX_VALUE);

            controlTimestamp += wheel.tickResolution();
        }
        while (-1 == firedTimestamp1.value || -1 == firedTimestamp2.value);

        assertThat(firedTimestamp1.value, is(16 * wheel.tickResolution()));
        assertThat(firedTimestamp2.value, is(24 * wheel.tickResolution()));
    });
}
 
Example #26
Source File: ReplicateRecordingTest.java    From aeron with Apache License 2.0 5 votes vote down vote up
@Test
public void shouldReplicateStoppedRecording()
{
    final String messagePrefix = "Message-Prefix-";
    final int messageCount = 10;
    final long srcRecordingId;

    final long subscriptionId = srcAeronArchive.startRecording(LIVE_CHANNEL, LIVE_STREAM_ID, LOCAL);

    try (Publication publication = srcAeron.addPublication(LIVE_CHANNEL, LIVE_STREAM_ID))
    {
        final CountersReader counters = srcAeron.countersReader();
        final int counterId = awaitRecordingCounterId(counters, publication.sessionId());
        srcRecordingId = RecordingPos.getRecordingId(counters, counterId);

        offer(publication, messageCount, messagePrefix);
        awaitPosition(counters, counterId, publication.position());
    }

    srcAeronArchive.stopRecording(subscriptionId);

    final MutableLong dstRecordingId = new MutableLong();
    final MutableReference<RecordingSignal> signalRef = new MutableReference<>();
    final RecordingSignalAdapter adapter = newRecordingSignalAdapter(signalRef, dstRecordingId);

    dstAeronArchive.replicate(
        srcRecordingId, NULL_VALUE, SRC_CONTROL_STREAM_ID, SRC_CONTROL_REQUEST_CHANNEL, null);

    assertEquals(RecordingSignal.REPLICATE, awaitSignal(signalRef, adapter));
    assertEquals(RecordingSignal.EXTEND, awaitSignal(signalRef, adapter));

    final ObjectHashSet<RecordingSignal> transitionEventsSet = new ObjectHashSet<>();
    transitionEventsSet.add(awaitSignal(signalRef, adapter));
    transitionEventsSet.add(awaitSignal(signalRef, adapter));

    assertTrue(transitionEventsSet.contains(RecordingSignal.STOP));
    assertTrue(transitionEventsSet.contains(RecordingSignal.SYNC));
}
 
Example #27
Source File: ListRecordingsForUriSessionTest.java    From aeron with Apache License 2.0 5 votes vote down vote up
@Test
public void shouldResendDescriptorWhenSendFails()
{
    final long fromRecordingId = 1;
    final ListRecordingsForUriSession session = new ListRecordingsForUriSession(
        correlationId,
        fromRecordingId,
        1,
        LOCALHOST_BYTES,
        1,
        catalog,
        controlResponseProxy,
        controlSession,
        descriptorBuffer,
        recordingDescriptorDecoder);

    when(controlSession.sendDescriptor(eq(correlationId), any(), eq(controlResponseProxy))).thenReturn(0);
    session.doWork();
    verify(controlSession, times(1)).sendDescriptor(eq(correlationId), any(), eq(controlResponseProxy));

    final MutableLong counter = new MutableLong(fromRecordingId);
    when(controlSession.sendDescriptor(eq(correlationId), any(), eq(controlResponseProxy)))
        .then(verifySendDescriptor(counter));

    session.doWork();
    verify(controlSession, times(2)).sendDescriptor(eq(correlationId), any(), eq(controlResponseProxy));
}
 
Example #28
Source File: AeronUtil.java    From benchmarks with Apache License 2.0 5 votes vote down vote up
static long findLastRecordingId(
    final AeronArchive aeronArchive, final String recordingChannel, final int recordingStreamId)
{
    final MutableLong lastRecordingId = new MutableLong();

    final RecordingDescriptorConsumer consumer =
        (controlSessionId,
        correlationId,
        recordingId,
        startTimestamp,
        stopTimestamp,
        startPosition,
        stopPosition,
        initialTermId,
        segmentFileLength,
        termBufferLength,
        mtuLength,
        sessionId,
        streamId,
        strippedChannel,
        originalChannel,
        sourceIdentity) -> lastRecordingId.set(recordingId);

    int foundCount;
    do
    {
        foundCount = aeronArchive.listRecordingsForUri(0, 1, recordingChannel, recordingStreamId, consumer);
    }
    while (0 == foundCount);

    return lastRecordingId.get();
}
 
Example #29
Source File: DeadlineTimerWheelTest.java    From agrona with Apache License 2.0 4 votes vote down vote up
@Test
public void shouldLimitExpiringTimers()
{
    assertTimeoutPreemptively(ofSeconds(1), () ->
    {
        long controlTimestamp = 0;
        final MutableLong firedTimestamp1 = new MutableLong(-1);
        final MutableLong firedTimestamp2 = new MutableLong(-1);
        final DeadlineTimerWheel wheel = new DeadlineTimerWheel(TIME_UNIT, controlTimestamp, RESOLUTION, 8);

        final long id1 = wheel.scheduleTimer(controlTimestamp + (15 * wheel.tickResolution()));
        final long id2 = wheel.scheduleTimer(controlTimestamp + (15 * wheel.tickResolution()));

        int numExpired = 0;

        do
        {
            numExpired += wheel.poll(
                controlTimestamp,
                (timeUnit, now, timerId) ->
                {
                    assertThat(timerId, is(id1));
                    firedTimestamp1.value = now;
                    return true;
                },
                1);

            controlTimestamp += wheel.tickResolution();
        }
        while (-1 == firedTimestamp1.value && -1 == firedTimestamp2.value);

        assertThat(numExpired, is(1));

        do
        {
            numExpired += wheel.poll(
                controlTimestamp,
                (timeUnit, now, timerId) ->
                {
                    assertThat(timerId, is(id2));
                    firedTimestamp2.value = now;
                    return true;
                },
                1);

            controlTimestamp += wheel.tickResolution();
        }
        while (-1 == firedTimestamp1.value && -1 == firedTimestamp2.value);

        assertThat(numExpired, is(2));

        assertThat(firedTimestamp1.value, is(16 * wheel.tickResolution()));
        assertThat(firedTimestamp2.value, is(17 * wheel.tickResolution()));
    });
}
 
Example #30
Source File: DeadlineTimerWheelTest.java    From agrona with Apache License 2.0 4 votes vote down vote up
@Test
public void shouldBeAbleToCancelTimer()
{
    assertTimeoutPreemptively(ofSeconds(1), () ->
    {
        long controlTimestamp = 0;
        final MutableLong firedTimestamp = new MutableLong(-1);
        final DeadlineTimerWheel wheel = new DeadlineTimerWheel(TIME_UNIT, controlTimestamp, RESOLUTION, 256);

        final long id = wheel.scheduleTimer(controlTimestamp + (63 * wheel.tickResolution()));

        do
        {
            wheel.poll(
                controlTimestamp,
                (timeUnit, now, timerId) ->
                {
                    assertThat(timerId, is(id));
                    firedTimestamp.value = now;
                    return true;
                },
                Integer.MAX_VALUE);

            controlTimestamp += wheel.tickResolution();
        }
        while (-1 == firedTimestamp.value && controlTimestamp < (16 * wheel.tickResolution()));

        assertTrue(wheel.cancelTimer(id));
        assertFalse(wheel.cancelTimer(id));

        do
        {
            wheel.poll(
                controlTimestamp,
                (timeUnit, now, timerId) ->
                {
                    firedTimestamp.value = now;
                    return true;
                },
                Integer.MAX_VALUE);

            controlTimestamp += wheel.tickResolution();
        }
        while (-1 == firedTimestamp.value && controlTimestamp < (128 * wheel.tickResolution()));

        assertThat(firedTimestamp.value, is(-1L));
    });
}