Search in sources :

Example 1 with CachedEpochClock

use of org.agrona.concurrent.CachedEpochClock in project aeron by real-logic.

the class SequencerAgentTest method shouldSuspendThenResume.

@Test
public void shouldSuspendThenResume() {
    final CachedEpochClock clock = new CachedEpochClock();
    final MutableLong stateValue = new MutableLong();
    final Counter mockState = mock(Counter.class);
    when(mockState.get()).thenAnswer((invocation) -> stateValue.value);
    doAnswer((invocation) -> {
        stateValue.value = invocation.getArgument(0);
        return null;
    }).when(mockState).set(anyLong());
    final MutableLong controlValue = new MutableLong(NEUTRAL.code());
    final Counter mockControlToggle = mock(Counter.class);
    when(mockControlToggle.get()).thenAnswer((invocation) -> controlValue.value);
    doAnswer((invocation) -> {
        controlValue.value = invocation.getArgument(0);
        return null;
    }).when(mockControlToggle).set(anyLong());
    ctx.moduleStateCounter(mockState);
    ctx.controlToggleCounter(mockControlToggle);
    ctx.epochClock(clock);
    final SequencerAgent agent = newSequencerAgent();
    agent.commitPositionCounter(mock(Counter.class));
    agent.logRecordingPositionCounter(mock(ReadableCounter.class));
    assertThat((int) stateValue.get(), is(ConsensusModule.State.INIT.code()));
    agent.state(ConsensusModule.State.ACTIVE);
    agent.role(Cluster.Role.LEADER);
    assertThat((int) stateValue.get(), is(ConsensusModule.State.ACTIVE.code()));
    controlValue.value = SUSPEND.code();
    clock.update(1);
    agent.doWork();
    assertThat((int) stateValue.get(), is(ConsensusModule.State.SUSPENDED.code()));
    assertThat((int) controlValue.get(), is(NEUTRAL.code()));
    controlValue.value = RESUME.code();
    clock.update(2);
    agent.doWork();
    assertThat((int) stateValue.get(), is(ConsensusModule.State.ACTIVE.code()));
    assertThat((int) controlValue.get(), is(NEUTRAL.code()));
    final InOrder inOrder = Mockito.inOrder(mockLogPublisher);
    inOrder.verify(mockLogPublisher).appendClusterAction(eq(ClusterAction.SUSPEND), anyLong(), anyLong(), anyLong());
    inOrder.verify(mockLogPublisher).appendClusterAction(eq(ClusterAction.RESUME), anyLong(), anyLong(), anyLong());
}
Also used : ReadableCounter(io.aeron.status.ReadableCounter) MutableLong(org.agrona.collections.MutableLong) AtomicCounter(org.agrona.concurrent.status.AtomicCounter) Counter(io.aeron.Counter) ReadableCounter(io.aeron.status.ReadableCounter) InOrder(org.mockito.InOrder) CachedEpochClock(org.agrona.concurrent.CachedEpochClock) Test(org.junit.Test)

Example 2 with CachedEpochClock

use of org.agrona.concurrent.CachedEpochClock in project aeron by real-logic.

the class SequencerAgentTest method shouldLimitActiveSessions.

@Test
public void shouldLimitActiveSessions() {
    final CachedEpochClock clock = new CachedEpochClock();
    ctx.maxConcurrentSessions(1);
    ctx.epochClock(clock);
    final SequencerAgent agent = newSequencerAgent();
    final long correlationIdOne = 1L;
    agent.state(ConsensusModule.State.ACTIVE);
    agent.role(Cluster.Role.LEADER);
    agent.commitPositionCounter(mock(Counter.class));
    agent.logRecordingPositionCounter(mock(ReadableCounter.class));
    agent.onSessionConnect(correlationIdOne, 2, RESPONSE_CHANNEL_ONE, new byte[0]);
    clock.update(1);
    agent.doWork();
    verify(mockLogPublisher).appendConnectedSession(any(ClusterSession.class), anyLong());
    final long correlationIdTwo = 2L;
    agent.onSessionConnect(correlationIdTwo, 3, RESPONSE_CHANNEL_TWO, new byte[0]);
    clock.update(2);
    agent.doWork();
    verify(mockEgressPublisher).sendEvent(any(ClusterSession.class), eq(EventCode.ERROR), eq(ConsensusModule.Configuration.SESSION_LIMIT_MSG));
}
Also used : ReadableCounter(io.aeron.status.ReadableCounter) AtomicCounter(org.agrona.concurrent.status.AtomicCounter) Counter(io.aeron.Counter) ReadableCounter(io.aeron.status.ReadableCounter) CachedEpochClock(org.agrona.concurrent.CachedEpochClock) Test(org.junit.Test)

Example 3 with CachedEpochClock

use of org.agrona.concurrent.CachedEpochClock in project aeron by real-logic.

the class SequencerAgentTest method shouldCloseInactiveSession.

@Test
public void shouldCloseInactiveSession() {
    final CachedEpochClock clock = new CachedEpochClock();
    final long startMs = 7L;
    clock.update(startMs);
    ctx.epochClock(clock);
    final SequencerAgent agent = newSequencerAgent();
    final long correlationId = 1L;
    agent.state(ConsensusModule.State.ACTIVE);
    agent.role(Cluster.Role.LEADER);
    agent.commitPositionCounter(mock(Counter.class));
    agent.logRecordingPositionCounter(mock(ReadableCounter.class));
    agent.onSessionConnect(correlationId, 2, RESPONSE_CHANNEL_ONE, new byte[0]);
    agent.doWork();
    verify(mockLogPublisher).appendConnectedSession(any(ClusterSession.class), eq(startMs));
    final long timeMs = startMs + TimeUnit.NANOSECONDS.toMillis(ConsensusModule.Configuration.sessionTimeoutNs());
    clock.update(timeMs);
    agent.doWork();
    final long timeoutMs = timeMs + 1L;
    clock.update(timeoutMs);
    agent.doWork();
    verify(mockLogPublisher).appendClosedSession(any(ClusterSession.class), eq(timeoutMs));
    verify(mockEgressPublisher).sendEvent(any(ClusterSession.class), eq(EventCode.ERROR), eq(ConsensusModule.Configuration.SESSION_TIMEOUT_MSG));
}
Also used : ReadableCounter(io.aeron.status.ReadableCounter) AtomicCounter(org.agrona.concurrent.status.AtomicCounter) Counter(io.aeron.Counter) ReadableCounter(io.aeron.status.ReadableCounter) CachedEpochClock(org.agrona.concurrent.CachedEpochClock) Test(org.junit.Test)

Example 4 with CachedEpochClock

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

the class SenderTest method setUp.

@BeforeEach
public void setUp() {
    final SendChannelEndpoint mockSendChannelEndpoint = mock(SendChannelEndpoint.class);
    when(mockSendChannelEndpoint.udpChannel()).thenReturn(udpChannel);
    when(mockSendChannelEndpoint.send(any())).thenAnswer(saveByteBufferAnswer);
    when(mockSystemCounters.get(any())).thenReturn(mock(AtomicCounter.class));
    final MediaDriver.Context ctx = new MediaDriver.Context().cachedEpochClock(new CachedEpochClock()).cachedNanoClock(nanoClock).senderCachedNanoClock(nanoClock).receiverCachedNanoClock(nanoClock).controlTransportPoller(mockTransportPoller).systemCounters(mockSystemCounters).senderCommandQueue(senderCommandQueue).nanoClock(nanoClock).errorHandler(errorHandler);
    sender = new Sender(ctx);
    LogBufferDescriptor.initialiseTailWithTermId(rawLog.metaData(), 0, INITIAL_TERM_ID);
    termAppenders = new TermAppender[PARTITION_COUNT];
    for (int i = 0; i < PARTITION_COUNT; i++) {
        termAppenders[i] = new TermAppender(rawLog.termBuffers()[i], rawLog.metaData(), i);
    }
    final PublicationParams params = new PublicationParams();
    params.entityTag = 101;
    params.mtuLength = MAX_FRAME_LENGTH;
    params.lingerTimeoutNs = Configuration.publicationLingerTimeoutNs();
    params.signalEos = true;
    publication = new NetworkPublication(1, ctx, params, mockSendChannelEndpoint, rawLog, Configuration.producerWindowLength(TERM_BUFFER_LENGTH, Configuration.publicationTermWindowLength()), new AtomicLongPosition(), new AtomicLongPosition(), new AtomicLongPosition(), new AtomicLongPosition(), mock(AtomicCounter.class), SESSION_ID, STREAM_ID, INITIAL_TERM_ID, flowControl, mockRetransmitHandler, new NetworkPublicationThreadLocals(), false);
    senderCommandQueue.offer(() -> sender.onNewNetworkPublication(publication));
}
Also used : SendChannelEndpoint(io.aeron.driver.media.SendChannelEndpoint) TermAppender(io.aeron.logbuffer.TermAppender) AtomicCounter(org.agrona.concurrent.status.AtomicCounter) CachedEpochClock(org.agrona.concurrent.CachedEpochClock) AtomicLongPosition(org.agrona.concurrent.status.AtomicLongPosition) SendChannelEndpoint(io.aeron.driver.media.SendChannelEndpoint) BeforeEach(org.junit.jupiter.api.BeforeEach)

Example 5 with CachedEpochClock

use of org.agrona.concurrent.CachedEpochClock in project aeron by real-logic.

the class SenderTest method setUp.

@BeforeEach
public void setUp() {
    final SendChannelEndpoint mockSendChannelEndpoint = mock(SendChannelEndpoint.class);
    when(mockSendChannelEndpoint.udpChannel()).thenReturn(udpChannel);
    when(mockSendChannelEndpoint.send(any())).thenAnswer(saveByteBufferAnswer);
    when(mockSystemCounters.get(any())).thenReturn(mock(AtomicCounter.class));
    final MediaDriver.Context ctx = new MediaDriver.Context().cachedEpochClock(new CachedEpochClock()).cachedNanoClock(nanoClock).senderCachedNanoClock(nanoClock).receiverCachedNanoClock(nanoClock).controlTransportPoller(mockTransportPoller).systemCounters(mockSystemCounters).senderCommandQueue(senderCommandQueue).nanoClock(nanoClock).errorHandler(errorHandler);
    sender = new Sender(ctx);
    LogBufferDescriptor.initialiseTailWithTermId(rawLog.metaData(), 0, INITIAL_TERM_ID);
    termAppenders = new TermAppender[PARTITION_COUNT];
    for (int i = 0; i < PARTITION_COUNT; i++) {
        termAppenders[i] = new TermAppender(rawLog.termBuffers()[i], rawLog.metaData(), i);
    }
    final PublicationParams params = new PublicationParams();
    params.entityTag = 101;
    params.mtuLength = MAX_FRAME_LENGTH;
    params.lingerTimeoutNs = Configuration.publicationLingerTimeoutNs();
    params.signalEos = true;
    publication = new NetworkPublication(1, ctx, params, mockSendChannelEndpoint, rawLog, Configuration.producerWindowLength(TERM_BUFFER_LENGTH, Configuration.publicationTermWindowLength()), new AtomicLongPosition(), new AtomicLongPosition(), new AtomicLongPosition(), new AtomicLongPosition(), mock(AtomicCounter.class), SESSION_ID, STREAM_ID, INITIAL_TERM_ID, flowControl, mockRetransmitHandler, new NetworkPublicationThreadLocals(), false);
    senderCommandQueue.offer(() -> sender.onNewNetworkPublication(publication));
}
Also used : SendChannelEndpoint(io.aeron.driver.media.SendChannelEndpoint) TermAppender(io.aeron.logbuffer.TermAppender) AtomicCounter(org.agrona.concurrent.status.AtomicCounter) CachedEpochClock(org.agrona.concurrent.CachedEpochClock) AtomicLongPosition(org.agrona.concurrent.status.AtomicLongPosition) SendChannelEndpoint(io.aeron.driver.media.SendChannelEndpoint) BeforeEach(org.junit.jupiter.api.BeforeEach)

Aggregations

CachedEpochClock (org.agrona.concurrent.CachedEpochClock)5 AtomicCounter (org.agrona.concurrent.status.AtomicCounter)5 Counter (io.aeron.Counter)3 ReadableCounter (io.aeron.status.ReadableCounter)3 Test (org.junit.Test)3 SendChannelEndpoint (io.aeron.driver.media.SendChannelEndpoint)2 TermAppender (io.aeron.logbuffer.TermAppender)2 AtomicLongPosition (org.agrona.concurrent.status.AtomicLongPosition)2 BeforeEach (org.junit.jupiter.api.BeforeEach)2 MutableLong (org.agrona.collections.MutableLong)1 InOrder (org.mockito.InOrder)1