use of org.apache.ratis.protocol.RaftPeerId 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();
}
use of org.apache.ratis.protocol.RaftPeerId in project incubator-ratis by apache.
the class RaftSnapshotBaseTest method testBasicInstallSnapshot.
/**
* Basic test for install snapshot: start a one node cluster and let it
* generate a snapshot. Then delete the log and restart the node, and add more
* nodes as followers.
*/
@Test
public void testBasicInstallSnapshot() throws Exception {
List<LogPathAndIndex> logs;
try {
RaftTestUtil.waitForLeader(cluster);
final RaftPeerId leaderId = cluster.getLeader().getId();
int i = 0;
try (final RaftClient client = cluster.createClient(leaderId)) {
for (; i < SNAPSHOT_TRIGGER_THRESHOLD * 2 - 1; i++) {
RaftClientReply reply = client.send(new SimpleMessage("m" + i));
Assert.assertTrue(reply.isSuccess());
}
}
// wait for the snapshot to be done
RaftStorageDirectory storageDirectory = cluster.getLeader().getState().getStorage().getStorageDir();
final File snapshotFile = getSnapshotFile(cluster, i);
logs = storageDirectory.getLogSegmentFiles();
int retries = 0;
do {
Thread.sleep(1000);
} while (!snapshotFile.exists() && retries++ < 10);
Assert.assertTrue(snapshotFile + " does not exist", snapshotFile.exists());
} finally {
cluster.shutdown();
}
// delete the log segments from the leader
for (LogPathAndIndex path : logs) {
FileUtils.deleteFile(path.path.toFile());
}
// restart the peer
LOG.info("Restarting the cluster");
cluster.restart(false);
try {
assertLeaderContent(cluster);
// generate some more traffic
try (final RaftClient client = cluster.createClient(cluster.getLeader().getId())) {
Assert.assertTrue(client.send(new SimpleMessage("test")).isSuccess());
}
// add two more peers
MiniRaftCluster.PeerChanges change = cluster.addNewPeers(new String[] { "s3", "s4" }, true);
// trigger setConfiguration
cluster.setConfiguration(change.allPeersInNewConf);
RaftServerTestUtil.waitAndCheckNewConf(cluster, change.allPeersInNewConf, 0, null);
} finally {
cluster.shutdown();
}
}
use of org.apache.ratis.protocol.RaftPeerId in project alluxio by Alluxio.
the class RaftJournalSystem method addQuorumServer.
/**
* Adds a server to the quorum.
*
* @param serverNetAddress the address of the server
* @throws IOException if error occurred while performing the operation
*/
public synchronized void addQuorumServer(NetAddress serverNetAddress) throws IOException {
InetSocketAddress serverAddress = InetSocketAddress.createUnresolved(serverNetAddress.getHost(), serverNetAddress.getRpcPort());
RaftPeerId peerId = RaftJournalUtils.getPeerId(serverAddress);
Collection<RaftPeer> peers = mServer.getGroups().iterator().next().getPeers();
if (peers.stream().anyMatch((peer) -> peer.getId().equals(peerId))) {
return;
}
RaftPeer newPeer = RaftPeer.newBuilder().setId(peerId).setAddress(serverAddress).build();
List<RaftPeer> newPeers = new ArrayList<>(peers);
newPeers.add(newPeer);
RaftClientReply reply = mServer.setConfiguration(new SetConfigurationRequest(mRawClientId, mPeerId, RAFT_GROUP_ID, nextCallId(), newPeers));
if (reply.getException() != null) {
throw reply.getException();
}
}
use of org.apache.ratis.protocol.RaftPeerId in project alluxio by Alluxio.
the class SnapshotReplicationManagerTest method before.
private void before(int numFollowers) throws Exception {
mLeader = Mockito.mock(RaftJournalSystem.class);
Mockito.when(mLeader.isLeader()).thenReturn(true);
Mockito.when(mLeader.getLocalPeerId()).thenReturn(RaftPeerId.getRaftPeerId("leader"));
mLeaderStore = getSimpleStateMachineStorage();
mLeaderSnapshotManager = Mockito.spy(new SnapshotReplicationManager(mLeader, mLeaderStore));
String serverName = InProcessServerBuilder.generateName();
mServer = InProcessServerBuilder.forName(serverName).directExecutor().addService(new RaftJournalServiceHandler(mLeaderSnapshotManager)).build();
mServer.start();
ManagedChannel channel = InProcessChannelBuilder.forName(serverName).directExecutor().build();
RaftJournalServiceGrpc.RaftJournalServiceStub stub = RaftJournalServiceGrpc.newStub(channel);
// mock RaftJournalServiceClient
mClient = Mockito.mock(RaftJournalServiceClient.class);
Mockito.doNothing().when(mClient).close();
// download rpc mock
Mockito.when(mClient.downloadSnapshot(any())).thenAnswer((args) -> {
StreamObserver responseObserver = args.getArgument(0, StreamObserver.class);
return stub.downloadSnapshot(responseObserver);
});
// upload rpc mock
Mockito.when(mClient.uploadSnapshot(any())).thenAnswer((args) -> {
StreamObserver responseObserver = args.getArgument(0, StreamObserver.class);
return stub.uploadSnapshot(responseObserver);
});
Mockito.doReturn(mClient).when(mLeaderSnapshotManager).getJournalServiceClient();
for (int i = 0; i < numFollowers; i++) {
Follower follower = new Follower(mClient);
mFollowers.put(follower.getRaftPeerId(), follower);
}
List<QuorumServerInfo> quorumServerInfos = mFollowers.values().stream().map(follower -> {
return QuorumServerInfo.newBuilder().setServerAddress(NetAddress.newBuilder().setHost(follower.mHost).setRpcPort(follower.mRpcPort)).build();
}).collect(Collectors.toList());
Mockito.when(mLeader.getQuorumServerInfoList()).thenReturn(quorumServerInfos);
Mockito.when(mLeader.sendMessageAsync(any(), any())).thenAnswer((args) -> {
RaftPeerId peerId = args.getArgument(0, RaftPeerId.class);
Message message = args.getArgument(1, Message.class);
JournalQueryRequest queryRequest = JournalQueryRequest.parseFrom(message.getContent().asReadOnlyByteBuffer());
Message response = mFollowers.get(peerId).mSnapshotManager.handleRequest(queryRequest);
RaftClientReply reply = Mockito.mock(RaftClientReply.class);
Mockito.when(reply.getMessage()).thenReturn(response);
return CompletableFuture.completedFuture(reply);
});
}
Aggregations