use of org.apache.ignite.raft.jraft.rpc.CliRequests.AddPeerResponse in project ignite-3 by apache.
the class CliServiceImpl method addPeer.
@Override
public Status addPeer(final String groupId, final Configuration conf, final PeerId peer) {
Requires.requireTrue(!StringUtils.isBlank(groupId), "Blank group id");
Requires.requireNonNull(conf, "Null configuration");
Requires.requireNonNull(peer, "Null peer");
final PeerId leaderId = new PeerId();
final Status st = getLeader(groupId, conf, leaderId);
if (!st.isOk()) {
return st;
}
if (!this.cliClientService.connect(leaderId.getEndpoint())) {
return new Status(-1, "Fail to init channel to leader %s", leaderId);
}
AddPeerRequest req = cliOptions.getRaftMessagesFactory().addPeerRequest().groupId(groupId).leaderId(leaderId.toString()).peerId(peer.toString()).build();
try {
final Message result = this.cliClientService.addPeer(leaderId.getEndpoint(), req, null).get();
if (result instanceof AddPeerResponse) {
final AddPeerResponse resp = (AddPeerResponse) result;
final Configuration oldConf = new Configuration();
for (final String peerIdStr : resp.oldPeersList()) {
final PeerId oldPeer = new PeerId();
oldPeer.parse(peerIdStr);
oldConf.addPeer(oldPeer);
}
final Configuration newConf = new Configuration();
for (final String peerIdStr : resp.newPeersList()) {
final PeerId newPeer = new PeerId();
newPeer.parse(peerIdStr);
newConf.addPeer(newPeer);
}
LOG.info("Configuration of replication group {} changed from {} to {}.", groupId, oldConf, newConf);
return Status.OK();
} else {
return statusFromResponse(result);
}
} catch (final Exception e) {
return new Status(-1, e.getMessage());
}
}
use of org.apache.ignite.raft.jraft.rpc.CliRequests.AddPeerResponse in project ignite-3 by apache.
the class RaftGroupServiceImpl method addPeer.
/**
* {@inheritDoc}
*/
@Override
public CompletableFuture<Void> addPeer(Peer peer) {
Peer leader = this.leader;
if (leader == null)
return refreshLeader().thenCompose(res -> addPeer(peer));
AddPeerRequest req = factory.addPeerRequest().groupId(groupId).peerId(PeerId.fromPeer(peer).toString()).build();
CompletableFuture<AddPeerResponse> fut = new CompletableFuture<>();
sendWithRetry(leader, req, currentTimeMillis() + timeout, fut);
return fut.thenApply(resp -> {
this.peers = parsePeerList(resp.newPeersList());
return null;
});
}
use of org.apache.ignite.raft.jraft.rpc.CliRequests.AddPeerResponse in project ignite-3 by apache.
the class AddPeerRequestProcessor method processRequest0.
@Override
protected Message processRequest0(final CliRequestContext ctx, final AddPeerRequest request, final IgniteCliRpcRequestClosure done) {
final List<PeerId> oldPeers = ctx.node.listPeers();
final String addingPeerIdStr = request.peerId();
final PeerId addingPeer = new PeerId();
if (addingPeer.parse(addingPeerIdStr)) {
LOG.info("Receive AddPeerRequest to {} from {}, adding {}", ctx.node.getNodeId(), done.getRpcCtx().getRemoteAddress(), addingPeerIdStr);
ctx.node.addPeer(addingPeer, status -> {
if (!status.isOk()) {
done.run(status);
} else {
List<String> oldPeersList = new ArrayList<>();
List<String> newPeersList = new ArrayList<>();
boolean alreadyExists = false;
for (final PeerId oldPeer : oldPeers) {
oldPeersList.add(oldPeer.toString());
newPeersList.add(oldPeer.toString());
if (oldPeer.equals(addingPeer)) {
alreadyExists = true;
}
}
if (!alreadyExists) {
newPeersList.add(addingPeerIdStr);
}
AddPeerResponse req = msgFactory().addPeerResponse().newPeersList(newPeersList).oldPeersList(oldPeersList).build();
done.sendResponse(req);
}
});
} else {
return //
RaftRpcFactory.DEFAULT.newResponse(msgFactory(), RaftError.EINVAL, "Fail to parse peer id %s", addingPeerIdStr);
}
return null;
}
Aggregations