use of org.apache.ratis.protocol.RaftPeer in project incubator-ratis by apache.
the class LeaderElection method submitRequests.
private int submitRequests(final long electionTerm, final TermIndex lastEntry) {
int submitted = 0;
for (final RaftPeer peer : others) {
final RequestVoteRequestProto r = server.createRequestVoteRequest(peer.getId(), electionTerm, lastEntry);
service.submit(() -> server.getServerRpc().requestVote(r));
submitted++;
}
return submitted;
}
use of org.apache.ratis.protocol.RaftPeer in project incubator-ratis by apache.
the class PeerProxyMap method resetProxy.
public void resetProxy(RaftPeerId id) {
LOG.debug("{}: reset proxy for {}", name, id);
synchronized (resetLock) {
final PeerAndProxy pp = peers.remove(id);
final RaftPeer peer = pp.getPeer();
pp.close();
computeIfAbsent(peer);
}
}
use of org.apache.ratis.protocol.RaftPeer in project incubator-ratis by apache.
the class Server method run.
@Override
public void run() throws Exception {
RaftPeerId peerId = RaftPeerId.valueOf(id);
RaftProperties properties = new RaftProperties();
RaftPeer[] peers = getPeers();
final int port = NetUtils.createSocketAddr(getPeer(peerId).getAddress()).getPort();
GrpcConfigKeys.Server.setPort(properties, port);
properties.setInt(GrpcConfigKeys.OutputStream.RETRY_TIMES_KEY, Integer.MAX_VALUE);
RaftServerConfigKeys.setStorageDir(properties, storageDir);
StateMachine stateMachine = new ArithmeticStateMachine();
RaftGroup raftGroup = new RaftGroup(RaftGroupId.valueOf(ByteString.copyFromUtf8(raftGroupId)), peers);
RaftServer raftServer = RaftServer.newBuilder().setServerId(RaftPeerId.valueOf(id)).setStateMachine(stateMachine).setProperties(properties).setGroup(raftGroup).build();
raftServer.start();
}
use of org.apache.ratis.protocol.RaftPeer in project incubator-ratis by apache.
the class ReinitializationBaseTest method runTestReinitializeMultiGroups.
public static <T extends Throwable> void runTestReinitializeMultiGroups(MiniRaftCluster cluster, int[] idIndex, int chosen, CheckedBiConsumer<MiniRaftCluster, RaftGroup, T> checker) throws IOException, InterruptedException, T {
if (chosen < 0) {
chosen = ThreadLocalRandom.current().nextInt(idIndex.length);
}
final String type = cluster.getClass().getSimpleName() + Arrays.toString(idIndex) + "chosen=" + chosen;
LOG.info("\n\nrunTestReinitializeMultiGroups with " + type + ": " + cluster.printServers());
// Start server with an empty conf
final RaftGroup emptyGroup = new RaftGroup(RaftGroupId.randomId());
final List<RaftPeerId> ids = Arrays.stream(MiniRaftCluster.generateIds(idIndex[idIndex.length - 1], 0)).map(RaftPeerId::valueOf).collect(Collectors.toList());
ids.forEach(id -> cluster.putNewServer(id, emptyGroup, true));
LOG.info("putNewServer: " + cluster.printServers());
cluster.start();
LOG.info("start: " + cluster.printServers());
// Make sure that there are no leaders.
TimeUnit.SECONDS.sleep(1);
Assert.assertNull(cluster.getLeader());
// Reinitialize servers to three groups
final List<RaftPeer> allPeers = cluster.getPeers();
Collections.sort(allPeers, Comparator.comparing(p -> p.getId().toString()));
final RaftGroup[] groups = new RaftGroup[idIndex.length];
for (int i = 0; i < idIndex.length; i++) {
final RaftGroupId gid = RaftGroupId.randomId();
final int previous = i == 0 ? 0 : idIndex[i - 1];
final RaftPeer[] peers = allPeers.subList(previous, idIndex[i]).toArray(RaftPeer.emptyArray());
groups[i] = new RaftGroup(gid, peers);
LOG.info(i + ") starting " + groups[i]);
for (RaftPeer p : peers) {
try (final RaftClient client = cluster.createClient(p.getId(), emptyGroup)) {
client.reinitialize(groups[i], p.getId());
}
}
Assert.assertNotNull(RaftTestUtil.waitForLeader(cluster, true, gid));
checker.accept(cluster, groups[i]);
}
printThreadCount(type, "start groups");
LOG.info("start groups: " + cluster.printServers());
// randomly close two of the groups (i.e. reinitialize to empty peers)
LOG.info("chosen = " + chosen + ", " + groups[chosen]);
for (int i = 0; i < groups.length; i++) {
if (i != chosen) {
final RaftGroup g = groups[i];
final RaftGroup newGroup = new RaftGroup(g.getGroupId());
LOG.info(i + ") close " + cluster.printServers(g.getGroupId()));
for (RaftPeer p : g.getPeers()) {
try (final RaftClient client = cluster.createClient(p.getId(), g)) {
client.reinitialize(newGroup, p.getId());
}
}
}
}
printThreadCount(type, "close groups");
LOG.info("close groups: " + cluster.printServers());
// update chosen group to use all the peers
final RaftGroup newGroup = new RaftGroup(groups[chosen].getGroupId());
for (int i = 0; i < groups.length; i++) {
if (i != chosen) {
LOG.info(i + ") reinitialize: " + cluster.printServers(groups[i].getGroupId()));
for (RaftPeer p : groups[i].getPeers()) {
try (final RaftClient client = cluster.createClient(p.getId(), groups[i])) {
client.reinitialize(newGroup, p.getId());
}
}
}
}
LOG.info(chosen + ") setConfiguration: " + cluster.printServers(groups[chosen].getGroupId()));
try (final RaftClient client = cluster.createClient(groups[chosen])) {
client.setConfiguration(allPeers.toArray(RaftPeer.emptyArray()));
}
Assert.assertNotNull(RaftTestUtil.waitForLeader(cluster, true));
checker.accept(cluster, groups[chosen]);
LOG.info("update groups: " + cluster.printServers());
printThreadCount(type, "update groups");
cluster.shutdown();
printThreadCount(type, "shutdown");
}
use of org.apache.ratis.protocol.RaftPeer in project incubator-ratis by apache.
the class ReinitializationBaseTest method testReinitialize.
@Test
public void testReinitialize() throws Exception {
final MiniRaftCluster cluster = getCluster(0);
LOG.info("Start testReinitialize" + cluster.printServers());
// Start server with an empty conf
final RaftGroupId groupId = RaftGroupId.randomId();
final RaftGroup group = new RaftGroup(groupId);
final List<RaftPeerId> ids = Arrays.stream(MiniRaftCluster.generateIds(3, 0)).map(RaftPeerId::valueOf).collect(Collectors.toList());
ids.forEach(id -> cluster.putNewServer(id, group, true));
LOG.info("putNewServer: " + cluster.printServers());
cluster.start();
LOG.info("start: " + cluster.printServers());
// Make sure that there are no leaders.
TimeUnit.SECONDS.sleep(1);
Assert.assertNull(cluster.getLeader());
// Reinitialize servers
final RaftGroup newGroup = new RaftGroup(groupId, cluster.getPeers());
final RaftClient client = cluster.createClient(newGroup);
for (RaftPeer p : newGroup.getPeers()) {
client.reinitialize(newGroup, p.getId());
}
Assert.assertNotNull(RaftTestUtil.waitForLeader(cluster, true));
cluster.shutdown();
}
Aggregations