Search in sources :

Example 21 with Configuration

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;
}
Also used : Status(com.alipay.sofa.jraft.Status) Configuration(com.alipay.sofa.jraft.conf.Configuration)

Example 22 with Configuration

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;
}
Also used : Configuration(com.alipay.sofa.jraft.conf.Configuration) Region(com.alipay.sofa.jraft.rhea.metadata.Region) RegionEpoch(com.alipay.sofa.jraft.rhea.metadata.RegionEpoch)

Example 23 with Configuration

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;
}
Also used : Status(com.alipay.sofa.jraft.Status) Configuration(com.alipay.sofa.jraft.conf.Configuration)

Example 24 with Configuration

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;
}
Also used : Status(com.alipay.sofa.jraft.Status) Configuration(com.alipay.sofa.jraft.conf.Configuration)

Example 25 with Configuration

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();
    }
}
Also used : Status(com.alipay.sofa.jraft.Status) Configuration(com.alipay.sofa.jraft.conf.Configuration)

Aggregations

Configuration (com.alipay.sofa.jraft.conf.Configuration)81 PeerId (com.alipay.sofa.jraft.entity.PeerId)54 Test (org.junit.Test)28 Node (com.alipay.sofa.jraft.Node)20 Endpoint (com.alipay.sofa.jraft.util.Endpoint)20 Status (com.alipay.sofa.jraft.Status)18 NodeOptions (com.alipay.sofa.jraft.option.NodeOptions)18 RaftGroupService (com.alipay.sofa.jraft.RaftGroupService)9 SynchronizedClosure (com.alipay.sofa.jraft.closure.SynchronizedClosure)8 File (java.io.File)8 ArrayList (java.util.ArrayList)8 CountDownLatch (java.util.concurrent.CountDownLatch)8 RpcServer (com.alipay.sofa.jraft.rpc.RpcServer)7 ConfigurationEntry (com.alipay.sofa.jraft.conf.ConfigurationEntry)5 Task (com.alipay.sofa.jraft.entity.Task)5 CliOptions (com.alipay.sofa.jraft.option.CliOptions)5 LogId (com.alipay.sofa.jraft.entity.LogId)4 RheaKVStore (com.alipay.sofa.jraft.rhea.client.RheaKVStore)4 LogEntry (com.alipay.sofa.jraft.entity.LogEntry)3 RaftException (com.alipay.sofa.jraft.error.RaftException)3