use of com.alipay.sofa.jraft.conf.Configuration in project sofa-jraft by sofastack.
the class AbstractPlacementDriverClient method transferLeader.
@Override
public boolean transferLeader(final long regionId, final Peer peer, final boolean refreshConf) {
Requires.requireNonNull(peer, "peer");
Requires.requireNonNull(peer.getEndpoint(), "peer.endpoint");
final String raftGroupId = JRaftHelper.getJRaftGroupId(this.clusterName, regionId);
final Configuration conf = RouteTable.getInstance().getConfiguration(raftGroupId);
final Status status = this.cliService.transferLeader(raftGroupId, conf, JRaftHelper.toJRaftPeerId(peer));
if (status.isOk()) {
if (refreshConf) {
refreshRouteConfiguration(regionId);
}
return true;
}
LOG.error("Fail to [transferLeader], [regionId: {}, peer: {}], status: {}.", regionId, peer, status);
return false;
}
use of com.alipay.sofa.jraft.conf.Configuration in project sofa-jraft by sofastack.
the class AbstractPlacementDriverClient method getLocalRegionMetadata.
protected Region getLocalRegionMetadata(final RegionEngineOptions opts) {
final long regionId = Requires.requireNonNull(opts.getRegionId(), "opts.regionId");
Requires.requireTrue(regionId >= Region.MIN_ID_WITH_MANUAL_CONF, "opts.regionId must >= " + Region.MIN_ID_WITH_MANUAL_CONF);
Requires.requireTrue(regionId < Region.MAX_ID_WITH_MANUAL_CONF, "opts.regionId must < " + Region.MAX_ID_WITH_MANUAL_CONF);
final byte[] startKey = opts.getStartKeyBytes();
final byte[] endKey = opts.getEndKeyBytes();
final String initialServerList = opts.getInitialServerList();
final Region region = new Region();
final Configuration conf = new Configuration();
// region
region.setId(regionId);
region.setStartKey(startKey);
region.setEndKey(endKey);
region.setRegionEpoch(new RegionEpoch(-1, -1));
// peers
Requires.requireTrue(Strings.isNotBlank(initialServerList), "opts.initialServerList is blank");
conf.parse(initialServerList);
region.setPeers(JRaftHelper.toPeerList(conf.listPeers()));
this.regionRouteTable.addOrUpdateRegion(region);
return region;
}
use of com.alipay.sofa.jraft.conf.Configuration in project sofa-jraft by sofastack.
the class AbstractPlacementDriverClient method addReplica.
@Override
public boolean addReplica(final long regionId, final Peer peer, final boolean refreshConf) {
Requires.requireNonNull(peer, "peer");
Requires.requireNonNull(peer.getEndpoint(), "peer.endpoint");
final String raftGroupId = JRaftHelper.getJRaftGroupId(this.clusterName, regionId);
final Configuration conf = RouteTable.getInstance().getConfiguration(raftGroupId);
final Status status = this.cliService.addPeer(raftGroupId, conf, JRaftHelper.toJRaftPeerId(peer));
if (status.isOk()) {
if (refreshConf) {
refreshRouteConfiguration(regionId);
}
return true;
}
LOG.error("Fail to [addReplica], [regionId: {}, peer: {}], status: {}.", regionId, peer, status);
return false;
}
use of com.alipay.sofa.jraft.conf.Configuration in project sofa-jraft by sofastack.
the class AbstractPlacementDriverClient method removeReplica.
@Override
public boolean removeReplica(final long regionId, final Peer peer, final boolean refreshConf) {
Requires.requireNonNull(peer, "peer");
Requires.requireNonNull(peer.getEndpoint(), "peer.endpoint");
final String raftGroupId = JRaftHelper.getJRaftGroupId(this.clusterName, regionId);
final Configuration conf = RouteTable.getInstance().getConfiguration(raftGroupId);
final Status status = this.cliService.removePeer(raftGroupId, conf, JRaftHelper.toJRaftPeerId(peer));
if (status.isOk()) {
if (refreshConf) {
refreshRouteConfiguration(regionId);
}
return true;
}
LOG.error("Fail to [removeReplica], [regionId: {}, peer: {}], status: {}.", regionId, peer, status);
return false;
}
use of com.alipay.sofa.jraft.conf.Configuration in project sofa-jraft by sofastack.
the class NodeImpl method resetPeers.
@Override
public Status resetPeers(final Configuration newPeers) {
Requires.requireNonNull(newPeers, "Null new peers");
Requires.requireTrue(!newPeers.isEmpty(), "Empty new peers");
Requires.requireTrue(newPeers.isValid(), "Invalid new peers: %s", newPeers);
this.writeLock.lock();
try {
if (newPeers.isEmpty()) {
LOG.warn("Node {} set empty peers.", getNodeId());
return new Status(RaftError.EINVAL, "newPeers is empty");
}
if (!this.state.isActive()) {
LOG.warn("Node {} is in state {}, can't set peers.", getNodeId(), this.state);
return new Status(RaftError.EPERM, "Bad state: %s", this.state);
}
// bootstrap?
if (this.conf.getConf().isEmpty()) {
LOG.info("Node {} set peers to {} from empty.", getNodeId(), newPeers);
this.conf.setConf(newPeers);
stepDown(this.currTerm + 1, false, new Status(RaftError.ESETPEER, "Set peer from empty configuration"));
return Status.OK();
}
if (this.state == State.STATE_LEADER && this.confCtx.isBusy()) {
LOG.warn("Node {} set peers need wait current conf changing.", getNodeId());
return new Status(RaftError.EBUSY, "Changing to another configuration");
}
// check equal, maybe retry direct return
if (this.conf.getConf().equals(newPeers)) {
return Status.OK();
}
final Configuration newConf = new Configuration(newPeers);
LOG.info("Node {} set peers from {} to {}.", getNodeId(), this.conf.getConf(), newPeers);
this.conf.setConf(newConf);
this.conf.getOldConf().reset();
stepDown(this.currTerm + 1, false, new Status(RaftError.ESETPEER, "Raft node set peer normally"));
return Status.OK();
} finally {
this.writeLock.unlock();
}
}
Aggregations