use of io.dingodb.raft.entity.PeerId in project dingo by dingodb.
the class CliServiceImpl method transferLeader.
@Override
public Status transferLeader(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 = checkLeaderAndConnect(groupId, conf, leaderId);
if (!st.isOk()) {
return st;
}
final CliRequests.TransferLeaderRequest.Builder rb = //
CliRequests.TransferLeaderRequest.newBuilder().setGroupId(//
groupId).setLeaderId(leaderId.toString());
if (!peer.isEmpty()) {
rb.setPeerId(peer.toString());
}
try {
final Message result = this.cliClientService.transferLeader(leaderId.getEndpoint(), rb.build(), null).get();
return statusFromResponse(result);
} catch (final Exception e) {
return new Status(-1, e.getMessage());
}
}
use of io.dingodb.raft.entity.PeerId in project dingo by dingodb.
the class RouteTable method selectLeader.
/**
* Get the cached leader of the group, return it when found, null otherwise.
* Make sure calls {@link #refreshLeader(CliClientService, String, int)} already
* before invoke this method.
*
* @param groupId raft group id
* @return peer of leader
*/
public PeerId selectLeader(final String groupId) {
Requires.requireTrue(!StringUtils.isBlank(groupId), "Blank group id");
final GroupConf gc = this.groupConfTable.get(groupId);
if (gc == null) {
return null;
}
final StampedLock stampedLock = gc.stampedLock;
long stamp = stampedLock.tryOptimisticRead();
PeerId leader = gc.leader;
if (!stampedLock.validate(stamp)) {
stamp = stampedLock.readLock();
try {
leader = gc.leader;
} finally {
stampedLock.unlockRead(stamp);
}
}
return leader;
}
use of io.dingodb.raft.entity.PeerId in project dingo by dingodb.
the class CliServiceImpl method resetPeer.
@Override
public Status resetPeer(final String groupId, final PeerId peerId, final Configuration newPeers) {
Requires.requireTrue(!StringUtils.isBlank(groupId), "Blank group id");
Requires.requireNonNull(peerId, "Null peerId");
Requires.requireNonNull(newPeers, "Null new peers");
if (!this.cliClientService.connect(peerId.getEndpoint())) {
return new Status(-1, "Fail to init channel to %s", peerId);
}
final CliRequests.ResetPeerRequest.Builder rb = //
CliRequests.ResetPeerRequest.newBuilder().setGroupId(//
groupId).setPeerId(peerId.toString());
for (final PeerId peer : newPeers) {
rb.addNewPeers(peer.toString());
}
try {
final Message result = this.cliClientService.resetPeer(peerId.getEndpoint(), rb.build(), null).get();
return statusFromResponse(result);
} catch (final Exception e) {
return new Status(-1, e.getMessage());
}
}
use of io.dingodb.raft.entity.PeerId in project dingo by dingodb.
the class CliServiceImpl method recordConfigurationChange.
private void recordConfigurationChange(final String groupId, final List<String> oldPeersList, final List<String> newPeersList) {
final Configuration oldConf = new Configuration();
for (final String peerIdStr : oldPeersList) {
final PeerId oldPeer = new PeerId();
oldPeer.parse(peerIdStr);
oldConf.addPeer(oldPeer);
}
final Configuration newConf = new Configuration();
for (final String peerIdStr : newPeersList) {
final PeerId newPeer = new PeerId();
newPeer.parse(peerIdStr);
newConf.addPeer(newPeer);
}
LOG.info("Configuration of replication group {} changed from {} to {}.", groupId, oldConf, newConf);
}
use of io.dingodb.raft.entity.PeerId in project dingo by dingodb.
the class CliServiceImpl method changePeers.
@Override
public Status changePeers(final String groupId, final Configuration conf, final Configuration newPeers) {
Requires.requireTrue(!StringUtils.isBlank(groupId), "Blank group id");
Requires.requireNonNull(conf, "Null configuration");
Requires.requireNonNull(newPeers, "Null new peers");
final PeerId leaderId = new PeerId();
final Status st = checkLeaderAndConnect(groupId, conf, leaderId);
if (!st.isOk()) {
return st;
}
final CliRequests.ChangePeersRequest.Builder rb = //
CliRequests.ChangePeersRequest.newBuilder().setGroupId(//
groupId).setLeaderId(leaderId.toString());
for (final PeerId peer : newPeers) {
rb.addNewPeers(peer.toString());
}
try {
final Message result = this.cliClientService.changePeers(leaderId.getEndpoint(), rb.build(), null).get();
if (result instanceof CliRequests.ChangePeersResponse) {
final CliRequests.ChangePeersResponse resp = (CliRequests.ChangePeersResponse) result;
recordConfigurationChange(groupId, resp.getOldPeersList(), resp.getNewPeersList());
return Status.OK();
} else {
return statusFromResponse(result);
}
} catch (final Exception e) {
return new Status(-1, e.getMessage());
}
}
Aggregations