use of org.apache.ratis.protocol.RaftClientReply in project incubator-ratis by apache.
the class InstallSnapshotNotificationTests method testInstallSnapshotNotificationCount.
private void testInstallSnapshotNotificationCount(CLUSTER cluster) throws Exception {
leaderSnapshotInfoRef.set(null);
numSnapshotRequests.set(0);
int i = 0;
try {
RaftTestUtil.waitForLeader(cluster);
final RaftPeerId leaderId = cluster.getLeader().getId();
// Let a few heartbeats pass.
ONE_SECOND.sleep();
Assert.assertEquals(0, numSnapshotRequests.get());
// Generate data.
try (final RaftClient client = cluster.createClient(leaderId)) {
for (; i < 10; i++) {
RaftClientReply reply = client.io().send(new RaftTestUtil.SimpleMessage("m" + i));
Assert.assertTrue(reply.isSuccess());
}
}
// Wait until index has been updated
RaftTestUtil.waitFor(() -> cluster.getLeader().getStateMachine().getLastAppliedTermIndex().getIndex() == 20, 300, 15000);
// Take snapshot and check result.
long snapshotIndex = cluster.getLeader().getStateMachine().takeSnapshot();
Assert.assertEquals(20, snapshotIndex);
final SnapshotInfo leaderSnapshotInfo = cluster.getLeader().getStateMachine().getLatestSnapshot();
Assert.assertEquals(20, leaderSnapshotInfo.getIndex());
final boolean set = leaderSnapshotInfoRef.compareAndSet(null, leaderSnapshotInfo);
Assert.assertTrue(set);
// Wait for the snapshot to be done.
final RaftServer.Division leader = cluster.getLeader();
final long nextIndex = leader.getRaftLog().getNextIndex();
Assert.assertEquals(21, nextIndex);
// End index is exclusive.
final List<File> snapshotFiles = RaftSnapshotBaseTest.getSnapshotFiles(cluster, 0, nextIndex);
JavaUtils.attemptRepeatedly(() -> {
Assert.assertTrue(snapshotFiles.stream().anyMatch(RaftSnapshotBaseTest::exists));
return null;
}, 10, ONE_SECOND, "snapshotFile.exist", LOG);
// Clear all log files and reset cached log start index.
long snapshotInstallIndex = leader.getRaftLog().onSnapshotInstalled(leader.getRaftLog().getLastCommittedIndex()).get();
Assert.assertEquals(20, snapshotInstallIndex);
// Check that logs are gone.
Assert.assertEquals(0, LogSegmentPath.getLogSegmentPaths(leader.getRaftStorage()).size());
Assert.assertEquals(RaftLog.INVALID_LOG_INDEX, leader.getRaftLog().getStartIndex());
// Allow some heartbeats to go through, then make sure none of them had
// snapshot requests.
ONE_SECOND.sleep();
Assert.assertEquals(0, numSnapshotRequests.get());
// Make sure leader and followers are still up to date.
for (RaftServer.Division follower : cluster.getFollowers()) {
Assert.assertEquals(leader.getRaftLog().getNextIndex(), follower.getRaftLog().getNextIndex());
}
// Add two more peers who will need snapshots from the leader.
final MiniRaftCluster.PeerChanges change = cluster.addNewPeers(2, true, true);
// trigger setConfiguration
cluster.setConfiguration(change.allPeersInNewConf);
RaftServerTestUtil.waitAndCheckNewConf(cluster, change.allPeersInNewConf, 0, null);
// Generate more data.
try (final RaftClient client = cluster.createClient(leader.getId())) {
Assert.assertTrue(client.io().send(new RaftTestUtil.SimpleMessage("m" + i)).isSuccess());
}
// Make sure leader and followers are still up to date.
for (RaftServer.Division follower : cluster.getFollowers()) {
// Give follower slightly time to catch up
RaftTestUtil.waitFor(() -> leader.getRaftLog().getNextIndex() == follower.getRaftLog().getNextIndex(), 300, 15000);
}
// Make sure each new peer got one snapshot notification.
Assert.assertEquals(2, numSnapshotRequests.get());
} finally {
cluster.shutdown();
}
}
use of org.apache.ratis.protocol.RaftClientReply in project incubator-ratis by apache.
the class MessageStreamApiTests method runTestStreamAsync.
void runTestStreamAsync(CLUSTER cluster) throws Exception {
RaftTestUtil.waitForLeader(cluster);
ByteString bytes = ByteString.EMPTY;
for (int i = 0; i < 10; ) {
final String s = (char) ('A' + i) + "1234567";
LOG.info("s=" + s);
final ByteString b = ByteString.copyFrom(s, StandardCharsets.UTF_8);
Assert.assertEquals(8, b.size());
for (int j = 0; j < 128; j++) {
bytes = bytes.concat(b);
}
i++;
Assert.assertEquals(i * SUBMESSAGE_SIZE.getSizeInt(), bytes.size());
}
try (RaftClient client = cluster.createClient()) {
final RaftClientReply reply = client.getMessageStreamApi().streamAsync(Message.valueOf(bytes)).get();
Assert.assertTrue(reply.isSuccess());
}
// check if all the parts are streamed as a single message.
try (RaftClient client = cluster.createClient()) {
final RaftClientReply reply = client.io().sendReadOnly(new SimpleMessage(bytes.toString(StandardCharsets.UTF_8)));
Assert.assertTrue(reply.isSuccess());
}
}
use of org.apache.ratis.protocol.RaftClientReply in project incubator-ratis by apache.
the class MessageStreamApiTests method runTestStream.
void runTestStream(CLUSTER cluster) throws Exception {
RaftTestUtil.waitForLeader(cluster);
// stream multiple parts
final int numParts = 9;
final int endOfRequest = 6;
final StringBuilder key = new StringBuilder();
try (RaftClient client = cluster.createClient();
MessageOutputStream out = client.getMessageStreamApi().stream()) {
for (int i = 1; i <= numParts; i++) {
key.append(i);
out.sendAsync(new SimpleMessage(i + ""), i == endOfRequest);
}
}
// check if all the parts are streamed as a single message.
final String k = key.toString();
try (RaftClient client = cluster.createClient()) {
final String k1 = k.substring(0, endOfRequest);
final RaftClientReply r1 = client.io().sendReadOnly(new SimpleMessage(k1));
Assert.assertTrue(r1.isSuccess());
final String k2 = k.substring(endOfRequest);
final RaftClientReply r2 = client.io().sendReadOnly(new SimpleMessage(k2));
Assert.assertTrue(r2.isSuccess());
}
}
use of org.apache.ratis.protocol.RaftClientReply 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.RaftClientReply 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