use of org.apache.ignite.raft.jraft.core.NodeImpl in project ignite-3 by apache.
the class JRaftUtils method bootstrap.
/**
* Bootstrap a non-empty raft node.
*
* @param opts options of bootstrap
* @return true if bootstrap success
*/
public static boolean bootstrap(final BootstrapOptions opts) throws InterruptedException {
final NodeImpl node = new NodeImpl("bootstrap", new PeerId("127.0.0.1", 0));
NodeOptions nodeOpts = opts.getNodeOptions();
nodeOpts.setStripes(1);
final boolean ret = node.bootstrap(opts);
node.shutdown();
node.join();
return ret;
}
use of org.apache.ignite.raft.jraft.core.NodeImpl in project ignite-3 by apache.
the class RaftGroupService method start.
/**
* Starts the raft group service, returns the raft node.
*/
public synchronized Node start() {
if (this.started) {
return this.node;
}
if (this.serverId == null || this.serverId.getEndpoint() == null || this.serverId.getEndpoint().equals(new Endpoint(Utils.IP_ANY, 0))) {
throw new IllegalArgumentException("Blank serverId:" + this.serverId);
}
if (StringUtils.isBlank(this.groupId)) {
throw new IllegalArgumentException("Blank group id" + this.groupId);
}
assert this.nodeOptions.getRpcClient() != null;
this.node = new NodeImpl(groupId, serverId);
if (!this.node.init(this.nodeOptions)) {
LOG.warn("Stopping partially started node [groupId={}, serverId={}]", groupId, serverId);
this.node.shutdown();
try {
this.node.join();
} catch (InterruptedException e) {
throw new IgniteInternalException(e);
}
throw new IgniteInternalException("Fail to init node, please see the logs to find the reason.");
}
this.nodeManager.add(this.node);
this.started = true;
LOG.info("Start the RaftGroupService successfully {}", this.node.getNodeId());
return this.node;
}
use of org.apache.ignite.raft.jraft.core.NodeImpl in project ignite-3 by apache.
the class ItJraftCounterServerTest method hasLeader.
/**
* Returns {@code true} if a raft group has elected a leader for a some term.
*
* @param grpId Group id.
* @return {@code True} if a leader is elected.
*/
private boolean hasLeader(String grpId) {
return servers.stream().anyMatch(s -> {
NodeImpl node = (NodeImpl) s.raftGroupService(grpId).getRaftNode();
StateMachineAdapter fsm = (StateMachineAdapter) node.getOptions().getFsm();
return node.isLeader() && fsm.getLeaderTerm() == node.getCurrentTerm();
});
}
use of org.apache.ignite.raft.jraft.core.NodeImpl 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.jraft.core.NodeImpl 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");
}
}
Aggregations