use of org.apache.ignite.raft.client.service.RaftGroupService 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);
}
}
use of org.apache.ignite.raft.client.service.RaftGroupService in project ignite-3 by apache.
the class ItJraftCounterServerTest method doTestFollowerCatchUp.
/**
* Do test follower catch up.
*
* @param snapshot {@code True} to create snapshot on leader and truncate log.
* @param cleanDir {@code True} to clean persistent state on follower before restart.
* @throws Exception If failed.
*/
private void doTestFollowerCatchUp(boolean snapshot, boolean cleanDir) throws Exception {
startCluster();
RaftGroupService client1 = clients.get(0);
RaftGroupService client2 = clients.get(1);
client1.refreshLeader().get();
client2.refreshLeader().get();
Peer leader1 = client1.leader();
assertNotNull(leader1);
Peer leader2 = client2.leader();
assertNotNull(leader2);
applyIncrements(client1, 0, 10);
applyIncrements(client2, 0, 20);
// First snapshot will not truncate logs.
client1.snapshot(leader1).get();
client2.snapshot(leader2).get();
JraftServerImpl toStop = null;
// Find the follower for both groups.
for (JraftServerImpl server : servers) {
Peer peer = server.localPeer(COUNTER_GROUP_0);
if (!peer.equals(leader1) && !peer.equals(leader2)) {
toStop = server;
break;
}
}
Path serverDataPath0 = toStop.getServerDataPath(COUNTER_GROUP_0);
Path serverDataPath1 = toStop.getServerDataPath(COUNTER_GROUP_1);
final int stopIdx = servers.indexOf(toStop);
toStop.stopRaftGroup(COUNTER_GROUP_0);
toStop.stopRaftGroup(COUNTER_GROUP_1);
toStop.beforeNodeStop();
toStop.stop();
applyIncrements(client1, 11, 20);
applyIncrements(client2, 21, 30);
if (snapshot) {
client1.snapshot(leader1).get();
client2.snapshot(leader2).get();
}
if (cleanDir) {
IgniteUtils.deleteIfExists(serverDataPath0);
IgniteUtils.deleteIfExists(serverDataPath1);
}
var svc2 = startServer(stopIdx, r -> {
r.startRaftGroup(COUNTER_GROUP_0, listenerFactory.get(), INITIAL_CONF);
r.startRaftGroup(COUNTER_GROUP_1, listenerFactory.get(), INITIAL_CONF);
}, opts -> {
});
waitForCondition(() -> validateStateMachine(sum(20), svc2, COUNTER_GROUP_0), 5_000);
waitForCondition(() -> validateStateMachine(sum(30), svc2, COUNTER_GROUP_1), 5_000);
svc2.stopRaftGroup(COUNTER_GROUP_0);
svc2.stopRaftGroup(COUNTER_GROUP_1);
svc2.beforeNodeStop();
svc2.stop();
var svc3 = startServer(stopIdx, r -> {
r.startRaftGroup(COUNTER_GROUP_0, listenerFactory.get(), INITIAL_CONF);
r.startRaftGroup(COUNTER_GROUP_1, listenerFactory.get(), INITIAL_CONF);
}, opts -> {
});
waitForCondition(() -> validateStateMachine(sum(20), svc3, COUNTER_GROUP_0), 5_000);
waitForCondition(() -> validateStateMachine(sum(30), svc3, COUNTER_GROUP_1), 5_000);
}
use of org.apache.ignite.raft.client.service.RaftGroupService in project ignite-3 by apache.
the class ItJraftCounterServerTest method testApplyWithFailure.
/**
* Tests if a raft group become unavailable in case of a critical error.
*/
@Test
public void testApplyWithFailure() throws Exception {
listenerFactory = () -> new CounterListener() {
@Override
public void onWrite(Iterator<CommandClosure<WriteCommand>> iterator) {
Iterator<CommandClosure<WriteCommand>> wrapper = new Iterator<>() {
@Override
public boolean hasNext() {
return iterator.hasNext();
}
@Override
public CommandClosure<WriteCommand> next() {
CommandClosure<WriteCommand> cmd = iterator.next();
IncrementAndGetCommand command = (IncrementAndGetCommand) cmd.command();
if (command.delta() == 10) {
throw new IgniteInternalException("Very bad");
}
return cmd;
}
};
super.onWrite(wrapper);
}
};
startCluster();
RaftGroupService client1 = clients.get(0);
RaftGroupService client2 = clients.get(1);
client1.refreshLeader().get();
client2.refreshLeader().get();
NodeImpl leader = servers.stream().map(s -> ((NodeImpl) s.raftGroupService(COUNTER_GROUP_0).getRaftNode())).filter(n -> n.getState() == STATE_LEADER).findFirst().orElse(null);
assertNotNull(leader);
long val1 = applyIncrements(client1, 1, 5);
long val2 = applyIncrements(client2, 1, 7);
assertEquals(sum(5), val1);
assertEquals(sum(7), val2);
long val3 = applyIncrements(client1, 6, 9);
assertEquals(sum(9), val3);
try {
client1.<Long>run(new IncrementAndGetCommand(10)).get();
fail();
} catch (Exception e) {
// Expected.
Throwable cause = e.getCause();
assertTrue(cause instanceof RaftException);
}
NodeImpl finalLeader = leader;
waitForCondition(() -> finalLeader.getState() == STATE_ERROR, 5_000);
// Client can't switch to new leader, because only one peer in the list.
try {
client1.<Long>run(new IncrementAndGetCommand(11)).get();
} catch (Exception e) {
boolean isValid = e.getCause() instanceof TimeoutException;
if (!isValid) {
LOG.error("Got unexpected exception", e);
}
assertTrue(isValid, "Expecting the timeout");
}
}
use of org.apache.ignite.raft.client.service.RaftGroupService in project ignite-3 by apache.
the class ItJraftCounterServerTest method testClientCatchExceptionFromSm.
/**
* Tests that users related exceptions from SM are propagated to the client.
*/
@Test
public void testClientCatchExceptionFromSm() throws Exception {
listenerFactory = () -> new CounterListener() {
@Override
public void onWrite(Iterator<CommandClosure<WriteCommand>> iterator) {
while (iterator.hasNext()) {
CommandClosure<WriteCommand> clo = iterator.next();
IncrementAndGetCommand cmd0 = (IncrementAndGetCommand) clo.command();
clo.result(new RuntimeException("Expected message"));
}
}
@Override
public void onRead(Iterator<CommandClosure<ReadCommand>> iterator) {
while (iterator.hasNext()) {
CommandClosure<ReadCommand> clo = iterator.next();
assert clo.command() instanceof GetValueCommand;
clo.result(new RuntimeException("Another expected message"));
}
}
};
startCluster();
RaftGroupService client1 = clients.get(0);
RaftGroupService client2 = clients.get(1);
client1.refreshLeader().get();
client2.refreshLeader().get();
NodeImpl leader = servers.stream().map(s -> ((NodeImpl) s.raftGroupService(COUNTER_GROUP_0).getRaftNode())).filter(n -> n.getState() == STATE_LEADER).findFirst().orElse(null);
assertNotNull(leader);
try {
client1.<Long>run(new IncrementAndGetCommand(3)).get();
fail();
} catch (Exception e) {
// Expected.
Throwable cause = e.getCause();
assertTrue(cause instanceof RuntimeException);
assertEquals(cause.getMessage(), "Expected message");
}
try {
client1.<Long>run(new GetValueCommand()).get();
fail();
} catch (Exception e) {
// Expected.
Throwable cause = e.getCause();
assertTrue(cause instanceof RuntimeException);
assertEquals(cause.getMessage(), "Another expected message");
}
}
use of org.apache.ignite.raft.client.service.RaftGroupService in project ignite-3 by apache.
the class ItJraftCounterServerTest method startClient.
/**
* Starts client.
*
* @param groupId Group id.
* @return The client.
* @throws Exception If failed.
*/
private RaftGroupService startClient(String groupId) throws Exception {
var addr = new NetworkAddress(getLocalAddress(), PORT);
ClusterService clientNode = clusterService(CLIENT_PORT + clients.size(), List.of(addr), true);
RaftGroupService client = RaftGroupServiceImpl.start(groupId, clientNode, FACTORY, 10_000, List.of(new Peer(addr)), false, 200, executor).get(3, TimeUnit.SECONDS);
clients.add(client);
return client;
}
Aggregations