use of org.apache.ignite.internal.raft.server.RaftServer in project ignite-3 by apache.
the class ItMetaStorageRaftGroupTest method testRangeNextWorksCorrectlyAfterLeaderChange.
/**
* Tests that {@link MetaStorageService#range(ByteArray, ByteArray, long)}} next command works correctly
* after leader changing.
*
* @throws Exception If failed.
*/
@Test
public void testRangeNextWorksCorrectlyAfterLeaderChange() throws Exception {
final AtomicInteger replicatorStartedCounter = new AtomicInteger(0);
final AtomicInteger replicatorStoppedCounter = new AtomicInteger(0);
when(mockStorage.range(EXPECTED_RESULT_ENTRY1.key().bytes(), new byte[] { 4 })).thenAnswer(invocation -> {
List<org.apache.ignite.internal.metastorage.server.Entry> entries = new ArrayList<>(List.of(EXPECTED_SRV_RESULT_ENTRY1, EXPECTED_SRV_RESULT_ENTRY2));
return new Cursor<org.apache.ignite.internal.metastorage.server.Entry>() {
private final Iterator<org.apache.ignite.internal.metastorage.server.Entry> it = entries.iterator();
@Override
public void close() {
}
@NotNull
@Override
public Iterator<org.apache.ignite.internal.metastorage.server.Entry> iterator() {
return it;
}
@Override
public boolean hasNext() {
return it.hasNext();
}
@Override
public org.apache.ignite.internal.metastorage.server.Entry next() {
return it.next();
}
};
});
List<Pair<RaftServer, RaftGroupService>> raftServersRaftGroups = prepareJraftMetaStorages(replicatorStartedCounter, replicatorStoppedCounter);
List<RaftServer> raftServers = raftServersRaftGroups.stream().map(p -> p.key).collect(Collectors.toList());
NetworkAddress oldLeader = raftServersRaftGroups.get(0).value.leader().address();
Optional<RaftServer> oldLeaderServer = raftServers.stream().filter(s -> s.clusterService().topologyService().localMember().address().equals(oldLeader)).findFirst();
// Server that will be alive after we stop leader.
Optional<RaftServer> liveServer = raftServers.stream().filter(s -> !s.clusterService().topologyService().localMember().address().equals(oldLeader)).findFirst();
if (oldLeaderServer.isEmpty() || liveServer.isEmpty()) {
fail();
}
RaftGroupService raftGroupServiceOfLiveServer = raftServersRaftGroups.stream().filter(p -> p.key.equals(liveServer.get())).findFirst().get().value;
MetaStorageService metaStorageSvc = new MetaStorageServiceImpl(raftGroupServiceOfLiveServer, "some_node");
Cursor<Entry> cursor = metaStorageSvc.range(EXPECTED_RESULT_ENTRY1.key(), new ByteArray(new byte[] { 4 }));
assertTrue(TestUtils.waitForCondition(() -> replicatorStartedCounter.get() == 2, 5_000), replicatorStartedCounter.get() + "");
assertTrue(cursor.hasNext());
assertEquals(EXPECTED_RESULT_ENTRY1, (cursor.iterator().next()));
// Ensure that leader has not been changed.
// In a stable topology unexpected leader election shouldn't happen.
assertTrue(TestUtils.waitForCondition(() -> replicatorStartedCounter.get() == 2, 5_000), replicatorStartedCounter.get() + "");
// stop leader
oldLeaderServer.get().stopRaftGroup(METASTORAGE_RAFT_GROUP_NAME);
oldLeaderServer.get().stop();
cluster.stream().filter(c -> c.topologyService().localMember().address().equals(oldLeader)).findFirst().get().stop();
raftGroupServiceOfLiveServer.refreshLeader().get();
assertNotSame(oldLeader, raftGroupServiceOfLiveServer.leader().address());
// ensure that leader has been changed only once
assertTrue(TestUtils.waitForCondition(() -> replicatorStartedCounter.get() == 4, 5_000), replicatorStartedCounter.get() + "");
assertTrue(TestUtils.waitForCondition(() -> replicatorStoppedCounter.get() == 2, 5_000), replicatorStoppedCounter.get() + "");
assertTrue(cursor.hasNext());
assertEquals(EXPECTED_RESULT_ENTRY2, (cursor.iterator().next()));
}
use of org.apache.ignite.internal.raft.server.RaftServer in project ignite-3 by apache.
the class ItJraftCounterServerTest method testCreateSnapshotGracefulFailure.
@Test
public void testCreateSnapshotGracefulFailure() throws Exception {
listenerFactory = () -> new CounterListener() {
@Override
public void onSnapshotSave(Path path, Consumer<Throwable> doneClo) {
doneClo.accept(new IgniteInternalException("Very bad"));
}
};
startCluster();
RaftGroupService client1 = clients.get(0);
RaftGroupService client2 = clients.get(1);
client1.refreshLeader().get();
client2.refreshLeader().get();
RaftServer server = servers.get(0);
Peer peer = server.localPeer(COUNTER_GROUP_0);
long val = applyIncrements(client1, 1, 10);
assertEquals(sum(10), val);
try {
client1.snapshot(peer).get();
fail();
} catch (Exception e) {
assertTrue(e.getCause() instanceof RaftException);
}
}
Aggregations