Skip to content

Commit

Permalink
[IOTDB-4418] One-member group conf change using RatisConsensus (apach…
Browse files Browse the repository at this point in the history
  • Loading branch information
SzyWilliam authored Sep 17, 2022
1 parent 75ee2f3 commit 478640e
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@
import org.apache.ratis.grpc.GrpcConfigKeys;
import org.apache.ratis.grpc.GrpcFactory;
import org.apache.ratis.protocol.ClientId;
import org.apache.ratis.protocol.GroupManagementRequest;
import org.apache.ratis.protocol.Message;
import org.apache.ratis.protocol.RaftClientReply;
import org.apache.ratis.protocol.RaftClientRequest;
Expand Down Expand Up @@ -348,32 +349,24 @@ public ConsensusGenericResponse createPeer(ConsensusGroupId groupId, List<Peer>
@Override
public ConsensusGenericResponse deletePeer(ConsensusGroupId groupId) {
RaftGroupId raftGroupId = Utils.fromConsensusGroupIdToRaftGroupId(groupId);
RaftGroup raftGroup = getGroupInfo(raftGroupId);

// pre-conditions: group exists and myself in this group
if (raftGroup == null || !raftGroup.getPeers().contains(myself)) {
return failed(new PeerNotInConsensusGroupException(groupId, myself));
}

// send remove group to myself
RaftClientReply reply;
RatisClient client = null;
try {
client = getRaftClient(raftGroup);
reply =
client
.getRaftClient()
.getGroupManagementApi(myself.getId())
.remove(raftGroupId, true, false);
server.groupManagement(
GroupManagementRequest.newRemove(
localFakeId,
myself.getId(),
localFakeCallId.incrementAndGet(),
raftGroupId,
true,
false));
if (!reply.isSuccess()) {
return failed(new RatisRequestFailedException(reply.getException()));
}
} catch (IOException e) {
return failed(new RatisRequestFailedException(e));
} finally {
if (client != null) {
client.returnSelf();
}
}

return ConsensusGenericResponse.newBuilder().setSuccess(reply.isSuccess()).build();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,27 @@ public void removeMemberFromGroup() throws Exception {
doConsensus(servers.get(0), group.getGroupId(), 10, 20);
}

@Test
public void oneMemberGroupChange1() throws Exception {
oneMemberGroupChangeImpl(false);
}

@Test
public void oneMemberGroupChange2() throws Exception {
oneMemberGroupChangeImpl(true);
}

private void oneMemberGroupChangeImpl(boolean previousRemove) throws Exception {
servers.get(0).createPeer(group.getGroupId(), peers.subList(0, 1));
doConsensus(servers.get(0), group.getGroupId(), 10, 10);

servers.get(1).createPeer(group.getGroupId(), peers.subList(0, 2));
servers.get(0).addPeer(group.getGroupId(), peers.get(1));
servers.get(1).transferLeader(group.getGroupId(), peers.get(1));
servers.get(previousRemove ? 0 : 1).removePeer(group.getGroupId(), peers.get(0));
servers.get(0).deletePeer(group.getGroupId());
}

@Test
public void crashAndStart() throws Exception {
servers.get(0).createPeer(group.getGroupId(), group.getPeers());
Expand Down

0 comments on commit 478640e

Please sign in to comment.