use of org.apache.ratis.client.RaftClient in project incubator-ratis by apache.
the class ServerInformationBaseTest method sendMessages.
RaftClientReply sendMessages(int n, MiniRaftCluster cluster) throws Exception {
LOG.info("sendMessages: " + n);
final RaftPeerId leader = RaftTestUtil.waitForLeader(cluster).getId();
RaftClientReply reply = null;
try (final RaftClient client = cluster.createClient(leader)) {
for (int i = 0; i < n; i++) {
reply = client.send(Message.valueOf("m" + i));
}
}
return reply;
}
use of org.apache.ratis.client.RaftClient in project incubator-ratis by apache.
the class TestStateMachine method testTransactionContextIsPassedBack.
@Test
public void testTransactionContextIsPassedBack() throws Throwable {
// tests that the TrxContext set by the StateMachine in Leader is passed back to the SM
properties.setClass(MiniRaftCluster.STATEMACHINE_CLASS_KEY, SMTransactionContext.class, StateMachine.class);
startCluster();
int numTrx = 100;
final RaftTestUtil.SimpleMessage[] messages = RaftTestUtil.SimpleMessage.create(numTrx);
try (final RaftClient client = cluster.createClient()) {
for (RaftTestUtil.SimpleMessage message : messages) {
client.send(message);
}
}
// TODO: there eshould be a better way to ensure all data is replicated and applied
Thread.sleep(cluster.getMaxTimeout() + 100);
for (RaftServerProxy raftServer : cluster.getServers()) {
final SMTransactionContext sm = SMTransactionContext.get(raftServer);
sm.rethrowIfException();
assertEquals(numTrx, sm.numApplied.get());
}
// check leader
RaftServerImpl raftServer = cluster.getLeader();
// assert every transaction has obtained context in leader
final SMTransactionContext sm = SMTransactionContext.get(raftServer.getProxy());
List<Long> ll = sm.applied.stream().collect(Collectors.toList());
Collections.sort(ll);
assertEquals(ll.toString(), ll.size(), numTrx);
for (int i = 0; i < numTrx; i++) {
assertEquals(ll.toString(), Long.valueOf(i + 1), ll.get(i));
}
}
use of org.apache.ratis.client.RaftClient in project alluxio by Alluxio.
the class RaftJournalSystem method resetPriorities.
/**
* Resets RaftPeer priorities.
*
* @throws IOException
*/
public synchronized void resetPriorities() throws IOException {
List<RaftPeer> resetPeers = new ArrayList<>();
final int NEUTRAL_PRIORITY = 1;
for (RaftPeer peer : mRaftGroup.getPeers()) {
resetPeers.add(RaftPeer.newBuilder(peer).setPriority(NEUTRAL_PRIORITY).build());
}
LOG.info("Resetting RaftPeer priorities");
try (RaftClient client = createClient()) {
RaftClientReply reply = client.admin().setConfiguration(resetPeers);
processReply(reply, "failed to reset master priorities to 1");
}
}
use of org.apache.ratis.client.RaftClient in project alluxio by Alluxio.
the class RaftJournalSystem method removeQuorumServer.
/**
* Removes from RAFT quorum, a server with given address.
* For server to be removed, it should be in unavailable state in quorum.
*
* @param serverNetAddress address of the server to remove from the quorum
* @throws IOException
*/
public synchronized void removeQuorumServer(NetAddress serverNetAddress) throws IOException {
InetSocketAddress serverAddress = InetSocketAddress.createUnresolved(serverNetAddress.getHost(), serverNetAddress.getRpcPort());
RaftPeerId peerId = RaftJournalUtils.getPeerId(serverAddress);
try (RaftClient client = createClient()) {
Collection<RaftPeer> peers = mServer.getGroups().iterator().next().getPeers();
RaftClientReply reply = client.admin().setConfiguration(peers.stream().filter(peer -> !peer.getId().equals(peerId)).collect(Collectors.toList()));
if (reply.getException() != null) {
throw reply.getException();
}
}
}
use of org.apache.ratis.client.RaftClient in project alluxio by Alluxio.
the class RaftJournalSystem method sendMessageAsync.
/**
* Sends a message to a raft server asynchronously.
*
* @param server the raft peer id of the target server
* @param message the message to send
* @return a future to be completed with the client reply
*/
public synchronized CompletableFuture<RaftClientReply> sendMessageAsync(RaftPeerId server, Message message) {
RaftClient client = createClient();
RaftClientRequest request = RaftClientRequest.newBuilder().setClientId(mRawClientId).setServerId(server).setGroupId(RAFT_GROUP_ID).setCallId(nextCallId()).setMessage(message).setType(RaftClientRequest.staleReadRequestType(0)).setSlidingWindowEntry(null).build();
return client.getClientRpc().sendRequestAsync(request).whenComplete((reply, t) -> {
try {
client.close();
} catch (IOException e) {
throw new CompletionException(e);
}
});
}
Aggregations