use of io.aeron.Counter 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());
}
use of io.aeron.Counter in project Aeron by real-logic.
the class StartFromTruncatedRecordingLogTest method before.
@BeforeEach
public void before() {
for (int i = 0; i < MEMBER_COUNT; i++) {
final AtomicLong atomicLong = new AtomicLong();
final Counter mockCounter = mock(Counter.class);
snapshotCounters[i] = atomicLong;
mockSnapshotCounters[i] = mockCounter;
when(mockCounter.incrementOrdered()).thenAnswer((invocation) -> atomicLong.getAndIncrement());
echoServices[i] = new EchoService();
startNode(i, true);
}
clientMediaDriver = MediaDriver.launch(new MediaDriver.Context().threadingMode(ThreadingMode.SHARED).warnIfDirectoryExists(false).dirDeleteOnStart(true));
}
use of io.aeron.Counter in project aeron by real-logic.
the class StartFromTruncatedRecordingLogTest method findLeaderId.
private int findLeaderId() {
for (int i = 0; i < MEMBER_COUNT; i++) {
final ConsensusModule.Context context = clusteredMediaDrivers[i].consensusModule().context();
final Cluster.Role role = Cluster.Role.get(context.clusterNodeRoleCounter());
final Counter electionStateCounter = context.electionStateCounter();
if (Cluster.Role.LEADER == role && ElectionState.CLOSED == ElectionState.get(electionStateCounter)) {
return context.clusterMemberId();
}
}
return NULL_VALUE;
}
use of io.aeron.Counter in project aeron by real-logic.
the class ClusterLoggingAgentTest method testClusterEventsLogging.
private void testClusterEventsLogging(final String enabledEvents, final EnumSet<ClusterEventCode> expectedEvents) {
before(enabledEvents, expectedEvents);
final Context mediaDriverCtx = new Context().errorHandler(Tests::onError).dirDeleteOnStart(true).threadingMode(ThreadingMode.SHARED);
final AeronArchive.Context aeronArchiveContext = new AeronArchive.Context().controlRequestChannel("aeron:ipc?term-length=64k").controlRequestStreamId(AeronArchive.Configuration.localControlStreamId()).controlResponseChannel("aeron:ipc?term-length=64k").controlResponseStreamId(AeronArchive.Configuration.localControlStreamId() + 1).controlResponseStreamId(101);
final Archive.Context archiveCtx = new Archive.Context().errorHandler(Tests::onError).archiveDir(new File(testDir, "archive")).deleteArchiveOnStart(true).recordingEventsEnabled(false).threadingMode(ArchiveThreadingMode.SHARED);
final ConsensusModule.Context consensusModuleCtx = new ConsensusModule.Context().errorHandler(ClusterTests.errorHandler(0)).clusterDir(new File(testDir, "consensus-module")).archiveContext(aeronArchiveContext.clone()).clusterMemberId(0).clusterMembers("0,localhost:20110,localhost:20220,localhost:20330,localhost:20440,localhost:8010").logChannel("aeron:udp?term-length=256k|control-mode=manual|control=localhost:20550").ingressChannel("aeron:udp?term-length=64k").replicationChannel("aeron:udp?endpoint=localhost:0");
final ClusteredService clusteredService = mock(ClusteredService.class);
final ClusteredServiceContainer.Context clusteredServiceCtx = new ClusteredServiceContainer.Context().errorHandler(ClusterTests.errorHandler(0)).archiveContext(aeronArchiveContext.clone()).clusterDir(new File(testDir, "service")).clusteredService(clusteredService);
clusteredMediaDriver = ClusteredMediaDriver.launch(mediaDriverCtx, archiveCtx, consensusModuleCtx);
container = ClusteredServiceContainer.launch(clusteredServiceCtx);
Tests.await(WAIT_LIST::isEmpty);
final Counter state = clusteredMediaDriver.consensusModule().context().electionStateCounter();
final Supplier<String> message = () -> ElectionState.get(state).toString();
while (ElectionState.CLOSED != ElectionState.get(state)) {
Tests.sleep(1, message);
}
}
use of io.aeron.Counter in project aeron by real-logic.
the class ReplaySession method noNewData.
private boolean noNewData(final long replayPosition, final long oldStopPosition) {
final Counter limitPosition = this.limitPosition;
final long currentLimitPosition = limitPosition.get();
final boolean isCounterClosed = limitPosition.isClosed();
final long newStopPosition = isCounterClosed ? catalog.stopPosition(recordingId) : currentLimitPosition;
if (isCounterClosed) {
if (NULL_POSITION == newStopPosition) {
replayLimit = oldStopPosition;
} else if (newStopPosition < replayLimit) {
replayLimit = newStopPosition;
}
}
if (replayPosition >= replayLimit) {
state(State.INACTIVE);
} else if (newStopPosition > oldStopPosition) {
stopPosition = newStopPosition;
return false;
}
return true;
}
Aggregations