com.alipay.sofa.jraft.conf.Configuration Java Examples

The following examples show how to use com.alipay.sofa.jraft.conf.Configuration. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: RouteTable.java    From sofa-jraft with Apache License 2.0 6 votes vote down vote up
/**
 * Update configuration of group in route table.
 *
 * @param groupId raft group id
 * @param conf    configuration to update
 * @return true on success
 */
public boolean updateConfiguration(final String groupId, final Configuration conf) {
    Requires.requireTrue(!StringUtils.isBlank(groupId), "Blank group id");
    Requires.requireNonNull(conf, "Null configuration");

    final GroupConf gc = getOrCreateGroupConf(groupId);
    final StampedLock stampedLock = gc.stampedLock;
    final long stamp = stampedLock.writeLock();
    try {
        gc.conf = conf;
        if (gc.leader != null && !gc.conf.contains(gc.leader)) {
            gc.leader = null;
        }
    } finally {
        stampedLock.unlockWrite(stamp);
    }
    return true;
}
 
Example #2
Source File: CliServiceTest.java    From sofa-jraft with Apache License 2.0 6 votes vote down vote up
@Test
public void testChangePeers() throws Exception {
    final List<PeerId> newPeers = TestUtils.generatePeers(10);
    newPeers.removeAll(this.conf.getPeerSet());
    for (final PeerId peer : newPeers) {
        assertTrue(this.cluster.start(peer.getEndpoint()));
    }
    this.cluster.waitLeader();
    final Node oldLeaderNode = this.cluster.getLeader();
    assertNotNull(oldLeaderNode);
    final PeerId oldLeader = oldLeaderNode.getNodeId().getPeerId();
    assertNotNull(oldLeader);
    assertTrue(this.cliService.changePeers(this.groupId, this.conf, new Configuration(newPeers)).isOk());
    this.cluster.waitLeader();
    final PeerId newLeader = this.cluster.getLeader().getNodeId().getPeerId();
    assertNotEquals(oldLeader, newLeader);
    assertTrue(newPeers.contains(newLeader));
}
 
Example #3
Source File: NodeImpl.java    From sofa-jraft with Apache License 2.0 6 votes vote down vote up
private void unsafeApplyConfiguration(final Configuration newConf, final Configuration oldConf,
                                      final boolean leaderStart) {
    Requires.requireTrue(this.confCtx.isBusy(), "ConfigurationContext is not busy");
    final LogEntry entry = new LogEntry(EnumOutter.EntryType.ENTRY_TYPE_CONFIGURATION);
    entry.setId(new LogId(0, this.currTerm));
    entry.setPeers(newConf.listPeers());
    entry.setLearners(newConf.listLearners());
    if (oldConf != null) {
        entry.setOldPeers(oldConf.listPeers());
        entry.setOldLearners(oldConf.listLearners());
    }
    final ConfigurationChangeDone configurationChangeDone = new ConfigurationChangeDone(this.currTerm, leaderStart);
    // Use the new_conf to deal the quorum of this very log
    if (!this.ballotBox.appendPendingTask(newConf, oldConf, configurationChangeDone)) {
        Utils.runClosureInThread(configurationChangeDone, new Status(RaftError.EINTERNAL, "Fail to append task."));
        return;
    }
    final List<LogEntry> entries = new ArrayList<>();
    entries.add(entry);
    this.logManager.appendEntries(entries, new LeaderStableClosure(entries));
    checkAndSetConfiguration(false);
}
 
Example #4
Source File: AbstractRheaKVStoreTest.java    From sofa-jraft with Apache License 2.0 6 votes vote down vote up
@Test
public void rangeSplitTest() {
    final RheaKVStore store = getRandomLeaderStore();
    final long regionId = 1;
    for (int i = 0; i < 20; i++) {
        store.bPut("a" + i, BytesUtil.writeUtf8("split"));
    }
    final CliOptions opts = new CliOptions();
    opts.setTimeoutMs(30000);
    final RheaKVCliService cliService = RheaKVServiceFactory.createAndInitRheaKVCliService(opts);
    final long newRegionId = 101;
    final String groupId = JRaftHelper.getJRaftGroupId("rhea_test", regionId);
    final Configuration conf = JRaftUtils.getConfiguration("127.0.0.1:18181,127.0.0.1:18182,127.0.0.1:18183");
    final Status st = cliService.rangeSplit(regionId, newRegionId, groupId, conf);
    System.err.println("Status:" + st);
    assertTrue(st.isOk());
    final RheaKVStore newStore = getLeaderStore(101);
    newStore.bPut("f_first_key", BytesUtil.writeUtf8("split_ok"));
    assertArrayEquals(BytesUtil.writeUtf8("split_ok"), newStore.bGet("f_first_key"));
}
 
Example #5
Source File: AbstractPlacementDriverClient.java    From sofa-jraft with Apache License 2.0 6 votes vote down vote up
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;
}
 
Example #6
Source File: AbstractPlacementDriverClient.java    From sofa-jraft with Apache License 2.0 6 votes vote down vote up
protected void initRouteTableByRegion(final RegionRouteTableOptions opts) {
    final long regionId = Requires.requireNonNull(opts.getRegionId(), "opts.regionId");
    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()));
    // update raft route table
    RouteTable.getInstance().updateConfiguration(JRaftHelper.getJRaftGroupId(clusterName, regionId), conf);
    this.regionRouteTable.addOrUpdateRegion(region);
}
 
Example #7
Source File: AbstractPlacementDriverClient.java    From sofa-jraft with Apache License 2.0 6 votes vote down vote up
@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;
}
 
Example #8
Source File: AbstractPlacementDriverClient.java    From sofa-jraft with Apache License 2.0 6 votes vote down vote up
@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;
}
 
Example #9
Source File: NodeImpl.java    From sofa-jraft with Apache License 2.0 6 votes vote down vote up
private void checkDeadNodes(final Configuration conf, final long monotonicNowMs) {
    // Check learner replicators at first.
    for (PeerId peer : conf.getLearners()) {
        checkReplicator(peer);
    }
    // Ensure quorum nodes alive.
    final List<PeerId> peers = conf.listPeers();
    final Configuration deadNodes = new Configuration();
    if (checkDeadNodes0(peers, monotonicNowMs, true, deadNodes)) {
        return;
    }
    LOG.warn("Node {} steps down when alive nodes don't satisfy quorum, term={}, deadNodes={}, conf={}.",
        getNodeId(), this.currTerm, deadNodes, conf);
    final Status status = new Status();
    status.setError(RaftError.ERAFTTIMEDOUT, "Majority of the group dies: %d/%d", deadNodes.size(), peers.size());
    stepDown(this.currTerm, false, status);
}
 
Example #10
Source File: AbstractPlacementDriverClient.java    From sofa-jraft with Apache License 2.0 6 votes vote down vote up
@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;
}
 
Example #11
Source File: NodeImpl.java    From sofa-jraft with Apache License 2.0 6 votes vote down vote up
/**
 * Invoked when this node becomes the leader, write a configuration change log as the first log.
 */
void flush(final Configuration conf, final Configuration oldConf) {
    Requires.requireTrue(!isBusy(), "Flush when busy");
    this.newPeers = conf.listPeers();
    this.newLearners = conf.listLearners();
    if (oldConf == null || oldConf.isEmpty()) {
        this.stage = Stage.STAGE_STABLE;
        this.oldPeers = this.newPeers;
        this.oldLearners = this.newLearners;
    } else {
        this.stage = Stage.STAGE_JOINT;
        this.oldPeers = oldConf.listPeers();
        this.oldLearners = oldConf.listLearners();
    }
    this.node.unsafeApplyConfiguration(conf, oldConf == null || oldConf.isEmpty() ? null : oldConf, true);
}
 
Example #12
Source File: NodeImpl.java    From sofa-jraft with Apache License 2.0 6 votes vote down vote up
private void addNewPeers(final Configuration adding) {
    this.addingPeers = adding.listPeers();
    LOG.info("Adding peers: {}.", this.addingPeers);
    for (final PeerId newPeer : this.addingPeers) {
        if (!this.node.replicatorGroup.addReplicator(newPeer)) {
            LOG.error("Node {} start the replicator failed, peer={}.", this.node.getNodeId(), newPeer);
            onCaughtUp(this.version, newPeer, false);
            return;
        }
        final OnCaughtUp caughtUp = new OnCaughtUp(this.node, this.node.currTerm, newPeer, this.version);
        final long dueTime = Utils.nowMs() + this.node.options.getElectionTimeoutMs();
        if (!this.node.replicatorGroup.waitCaughtUp(newPeer, this.node.options.getCatchupMargin(), dueTime,
            caughtUp)) {
            LOG.error("Node {} waitCaughtUp, peer={}.", this.node.getNodeId(), newPeer);
            onCaughtUp(this.version, newPeer, false);
            return;
        }
    }
}
 
Example #13
Source File: CliServiceImpl.java    From sofa-jraft with Apache License 2.0 6 votes vote down vote up
@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 ResetPeerRequest.Builder rb = 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());
    }
}
 
Example #14
Source File: BallotBox.java    From sofa-jraft with Apache License 2.0 6 votes vote down vote up
/**
 * Called by leader, otherwise the behavior is undefined
 * Store application context before replication.
 *
 * @param conf      current configuration
 * @param oldConf   old configuration
 * @param done      callback
 * @return          returns true on success
 */
public boolean appendPendingTask(final Configuration conf, final Configuration oldConf, final Closure done) {
    final Ballot bl = new Ballot();
    if (!bl.init(conf, oldConf)) {
        LOG.error("Fail to init ballot.");
        return false;
    }
    final long stamp = this.stampedLock.writeLock();
    try {
        if (this.pendingIndex <= 0) {
            LOG.error("Fail to appendingTask, pendingIndex={}.", this.pendingIndex);
            return false;
        }
        this.pendingMetaQueue.add(bl);
        this.closureQueue.appendPendingClosure(done);
        return true;
    } finally {
        this.stampedLock.unlockWrite(stamp);
    }
}
 
Example #15
Source File: ReplicatorGroupTest.java    From sofa-jraft with Apache License 2.0 6 votes vote down vote up
@Test
public void testFindTheNextCandidateWithPriority2() {
    final PeerId p1 = new PeerId("localhost", 18881, 0, 0);
    final PeerId p2 = new PeerId("localhost", 18882, 0, 0);
    final PeerId p3 = new PeerId("localhost", 18883, 0, -1);
    Mockito.when(this.rpcService.connect(p1.getEndpoint())).thenReturn(true);
    Mockito.when(this.rpcService.connect(p2.getEndpoint())).thenReturn(true);
    Mockito.when(this.rpcService.connect(p3.getEndpoint())).thenReturn(true);
    this.replicatorGroup.resetTerm(1);
    this.replicatorGroup.addReplicator(p1);
    this.replicatorGroup.addReplicator(p2);
    this.replicatorGroup.addReplicator(p3);
    final ConfigurationEntry conf = new ConfigurationEntry();
    conf.setConf(new Configuration(Arrays.asList(p1, p2, p3)));
    final PeerId p = this.replicatorGroup.findTheNextCandidate(conf);
    assertEquals(p3, p);
}
 
Example #16
Source File: StartupConf.java    From sofa-jraft with Apache License 2.0 6 votes vote down vote up
private boolean verify() {
    if (StringUtils.isBlank(groupId)) {
        throw new IllegalArgumentException("Blank groupId");
    }
    if (StringUtils.isBlank(dataPath)) {
        throw new IllegalArgumentException("Blank dataPath");
    }
    if (StringUtils.isBlank(conf)) {
        throw new IllegalArgumentException("Blank conf");
    }
    Configuration initConf = JRaftUtils.getConfiguration(conf);
    if (initConf.isEmpty()) {
        throw new IllegalArgumentException("Blank conf");
    }
    if (minSlot < 0) {
        throw new IllegalArgumentException("Invalid min slot");
    }
    if (minSlot > maxSlot) {
        throw new IllegalArgumentException("Invalid slot range.");
    }
    if (this.totalSlots <= 0) {
        throw new IllegalArgumentException("Invalid total slots");
    }
    return true;
}
 
Example #17
Source File: ResetPeerRequestProcessor.java    From sofa-jraft with Apache License 2.0 6 votes vote down vote up
@Override
protected Message processRequest0(final CliRequestContext ctx, final ResetPeerRequest request,
                                  final RpcRequestClosure done) {
    final Configuration newConf = new Configuration();
    for (final String peerIdStr : request.getNewPeersList()) {
        final PeerId peer = new PeerId();
        if (peer.parse(peerIdStr)) {
            newConf.addPeer(peer);
        } else {
            return RpcFactoryHelper //
                .responseFactory() //
                .newResponse(defaultResp(), RaftError.EINVAL, "Fail to parse peer id %s", peerIdStr);
        }
    }
    LOG.info("Receive ResetPeerRequest to {} from {}, new conf is {}", ctx.node.getNodeId(), done.getRpcCtx()
        .getRemoteAddress(), newConf);
    final Status st = ctx.node.resetPeers(newConf);
    return RpcFactoryHelper //
        .responseFactory() //
        .newResponse(defaultResp(), st);
}
 
Example #18
Source File: RouteTable.java    From sofa-jraft with Apache License 2.0 6 votes vote down vote up
/**
 * Get the configuration by groupId, returns null when not found.
 *
 * @param groupId raft group id
 * @return configuration of the group id
 */
public Configuration getConfiguration(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();
    Configuration conf = gc.conf;
    if (!stampedLock.validate(stamp)) {
        stamp = stampedLock.readLock();
        try {
            conf = gc.conf;
        } finally {
            stampedLock.unlockRead(stamp);
        }
    }
    return conf;
}
 
Example #19
Source File: NodeImpl.java    From sofa-jraft with Apache License 2.0 5 votes vote down vote up
/**
 * Start change configuration.
 */
void start(final Configuration oldConf, final Configuration newConf, final Closure done) {
    if (isBusy()) {
        if (done != null) {
            Utils.runClosureInThread(done, new Status(RaftError.EBUSY, "Already in busy stage."));
        }
        throw new IllegalStateException("Busy stage");
    }
    if (this.done != null) {
        if (done != null) {
            Utils.runClosureInThread(done, new Status(RaftError.EINVAL, "Already have done closure."));
        }
        throw new IllegalArgumentException("Already have done closure");
    }
    this.done = done;
    this.stage = Stage.STAGE_CATCHING_UP;
    this.oldPeers = oldConf.listPeers();
    this.newPeers = newConf.listPeers();
    this.oldLearners = oldConf.listLearners();
    this.newLearners = newConf.listLearners();
    final Configuration adding = new Configuration();
    final Configuration removing = new Configuration();
    newConf.diff(oldConf, adding, removing);
    this.nchanges = adding.size() + removing.size();

    addNewLearners();
    if (adding.isEmpty()) {
        nextStage();
        return;
    }
    addNewPeers(adding);
}
 
Example #20
Source File: NodeImpl.java    From sofa-jraft with Apache License 2.0 5 votes vote down vote up
private boolean checkDeadNodes0(final List<PeerId> peers, final long monotonicNowMs, final boolean checkReplicator,
                                final Configuration deadNodes) {
    final int leaderLeaseTimeoutMs = this.options.getLeaderLeaseTimeoutMs();
    int aliveCount = 0;
    long startLease = Long.MAX_VALUE;
    for (final PeerId peer : peers) {
        if (peer.equals(this.serverId)) {
            aliveCount++;
            continue;
        }
        if (checkReplicator) {
            checkReplicator(peer);
        }
        final long lastRpcSendTimestamp = this.replicatorGroup.getLastRpcSendTimestamp(peer);
        if (monotonicNowMs - lastRpcSendTimestamp <= leaderLeaseTimeoutMs) {
            aliveCount++;
            if (startLease > lastRpcSendTimestamp) {
                startLease = lastRpcSendTimestamp;
            }
            continue;
        }
        if (deadNodes != null) {
            deadNodes.addPeer(peer);
        }
    }
    if (aliveCount >= peers.size() / 2 + 1) {
        updateLastLeaderTimestamp(startLease);
        return true;
    }
    return false;
}
 
Example #21
Source File: NodeImpl.java    From sofa-jraft with Apache License 2.0 5 votes vote down vote up
void nextStage() {
    Requires.requireTrue(isBusy(), "Not in busy stage");
    switch (this.stage) {
        case STAGE_CATCHING_UP:
            if (this.nchanges > 1) {
                this.stage = Stage.STAGE_JOINT;
                this.node.unsafeApplyConfiguration(new Configuration(this.newPeers, this.newLearners),
                    new Configuration(this.oldPeers), false);
                return;
            }
            // Skip joint consensus since only one peers has been changed here. Make
            // it a one-stage change to be compatible with the legacy
            // implementation.
        case STAGE_JOINT:
            this.stage = Stage.STAGE_STABLE;
            this.node.unsafeApplyConfiguration(new Configuration(this.newPeers, this.newLearners), null, false);
            break;
        case STAGE_STABLE:
            final boolean shouldStepDown = !this.newPeers.contains(this.node.serverId);
            reset(new Status());
            if (shouldStepDown) {
                this.node.stepDown(this.node.currTerm, true, new Status(RaftError.ELEADERREMOVED,
                    "This node was removed."));
            }
            break;
        case STAGE_NONE:
            // noinspection ConstantConditions
            Requires.requireTrue(false, "Can't reach here");
            break;
    }
}
 
Example #22
Source File: NodeTest.java    From sofa-jraft with Apache License 2.0 5 votes vote down vote up
@Test
public void testAutoSnapshot() throws Exception {
    final Endpoint addr = new Endpoint(TestUtils.getMyIp(), TestUtils.INIT_PORT);
    NodeManager.getInstance().addAddress(addr);
    final NodeOptions nodeOptions = createNodeOptionsWithSharedTimer();
    final MockStateMachine fsm = new MockStateMachine(addr);
    nodeOptions.setFsm(fsm);
    nodeOptions.setLogUri(this.dataPath + File.separator + "log");
    nodeOptions.setSnapshotUri(this.dataPath + File.separator + "snapshot");
    nodeOptions.setRaftMetaUri(this.dataPath + File.separator + "meta");
    nodeOptions.setSnapshotIntervalSecs(10);
    nodeOptions.setInitialConf(new Configuration(Collections.singletonList(new PeerId(addr, 0))));

    final Node node = new NodeImpl("unittest", new PeerId(addr, 0));
    assertTrue(node.init(nodeOptions));
    // wait node elect self as leader
    Thread.sleep(2000);

    sendTestTaskAndWait(node);

    // wait for auto snapshot
    Thread.sleep(10000);
    // first snapshot will be triggered randomly
    final int times = fsm.getSaveSnapshotTimes();
    assertTrue("snapshotTimes=" + times, times >= 1);
    assertTrue(fsm.getSnapshotIndex() > 0);

    final CountDownLatch latch = new CountDownLatch(1);
    node.shutdown(new ExpectClosure(latch));
    node.join();
    waitLatch(latch);
}
 
Example #23
Source File: RaftClient.java    From sofa-registry with Apache License 2.0 5 votes vote down vote up
/**
 * @param groupId
 * @param confStr  Example: 127.0.0.1:8081,127.0.0.1:8082,127.0.0.1:8083
 */
public RaftClient(String groupId, String confStr) {

    this.groupId = groupId;
    conf = new Configuration();
    if (!conf.parse(confStr)) {
        throw new IllegalArgumentException("Fail to parse conf:" + confStr);
    }
    cliOptions = new CliOptions();
    cliClientService = new BoltCliClientService();
}
 
Example #24
Source File: CliServiceImpl.java    From sofa-jraft with Apache License 2.0 5 votes vote down vote up
@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 = 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);
    }

    final TransferLeaderRequest.Builder rb = 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());
    }
}
 
Example #25
Source File: NodeImpl.java    From sofa-jraft with Apache License 2.0 5 votes vote down vote up
@Override
public void addPeer(final PeerId peer, final Closure done) {
    Requires.requireNonNull(peer, "Null peer");
    this.writeLock.lock();
    try {
        Requires.requireTrue(!this.conf.getConf().contains(peer), "Peer already exists in current configuration");

        final Configuration newConf = new Configuration(this.conf.getConf());
        newConf.addPeer(peer);
        unsafeRegisterConfChange(this.conf.getConf(), newConf, done);
    } finally {
        this.writeLock.unlock();
    }
}
 
Example #26
Source File: RaftExchanger.java    From sofa-registry with Apache License 2.0 5 votes vote down vote up
/**
 * api for reset meta node
 */
public void resetPeer(List<String> ipAddressList) {
    try {
        if (cliService != null) {
            Configuration peersConf = new Configuration();
            for (String ipAddress : ipAddressList) {
                PeerId peer = new PeerId(ipAddress, metaServerConfig.getRaftServerPort());
                peersConf.addPeer(peer);
            }
            String ip = NetUtil.getLocalAddress().getHostAddress();
            PeerId localPeer = new PeerId(ip, metaServerConfig.getRaftServerPort());
            Status status = cliService.resetPeer(getGroup(), localPeer, peersConf);
            if (!status.isOk()) {
                LOGGER.error("CliService reset peer fail!error message {}",
                    status.getErrorMsg());
                throw new RuntimeException("CliService reset peer fail!error message "
                                           + status.getErrorMsg());
            }
        } else {
            LOGGER.error("cliService can't be null,it must be init first!");
            throw new RuntimeException("cliService can't be null,it must be init first!");
        }
    } catch (Exception e) {
        LOGGER.error("CliService reset peer error!", e);
        throw new RuntimeException("CliService reset peer error!", e);
    }
}
 
Example #27
Source File: RocksDBLogStorage.java    From sofa-jraft with Apache License 2.0 5 votes vote down vote up
private void load(final ConfigurationManager confManager) {
    checkState();
    try (final RocksIterator it = this.db.newIterator(this.confHandle, this.totalOrderReadOptions)) {
        it.seekToFirst();
        while (it.isValid()) {
            final byte[] ks = it.key();
            final byte[] bs = it.value();

            // LogEntry index
            if (ks.length == 8) {
                final LogEntry entry = this.logEntryDecoder.decode(bs);
                if (entry != null) {
                    if (entry.getType() == EntryType.ENTRY_TYPE_CONFIGURATION) {
                        final ConfigurationEntry confEntry = new ConfigurationEntry();
                        confEntry.setId(new LogId(entry.getId().getIndex(), entry.getId().getTerm()));
                        confEntry.setConf(new Configuration(entry.getPeers(), entry.getLearners()));
                        if (entry.getOldPeers() != null) {
                            confEntry.setOldConf(new Configuration(entry.getOldPeers(), entry.getOldLearners()));
                        }
                        if (confManager != null) {
                            confManager.add(confEntry);
                        }
                    }
                } else {
                    LOG.warn("Fail to decode conf entry at index {}, the log data is: {}.", Bits.getLong(ks, 0),
                        BytesUtil.toHex(bs));
                }
            } else {
                if (Arrays.equals(FIRST_LOG_IDX_KEY, ks)) {
                    setFirstLogIndex(Bits.getLong(bs, 0));
                    truncatePrefixInBackground(0L, this.firstLogIndex);
                } else {
                    LOG.warn("Unknown entry in configuration storage key={}, value={}.", BytesUtil.toHex(ks),
                        BytesUtil.toHex(bs));
                }
            }
            it.next();
        }
    }
}
 
Example #28
Source File: RouteTable.java    From sofa-jraft with Apache License 2.0 5 votes vote down vote up
/**
 * Update configuration of group in route table.
 *
 * @param groupId raft group id
 * @param confStr configuration string
 * @return true on success
 */
public boolean updateConfiguration(final String groupId, final String confStr) {
    Requires.requireTrue(!StringUtils.isBlank(groupId), "Blank group id");
    Requires.requireTrue(!StringUtils.isBlank(confStr), "Blank configuration");

    final Configuration conf = new Configuration();
    if (conf.parse(confStr)) {
        return updateConfiguration(groupId, conf);
    } else {
        LOG.error("Fail to parse confStr: {}", confStr);
        return false;
    }
}
 
Example #29
Source File: NodeImpl.java    From sofa-jraft with Apache License 2.0 5 votes vote down vote up
private void unsafeRegisterConfChange(final Configuration oldConf, final Configuration newConf, final Closure done) {

        Requires.requireTrue(newConf.isValid(), "Invalid new conf: %s", newConf);
        // The new conf entry(will be stored in log manager) should be valid
        Requires.requireTrue(new ConfigurationEntry(null, newConf, oldConf).isValid(), "Invalid conf entry: %s",
            newConf);

        if (this.state != State.STATE_LEADER) {
            LOG.warn("Node {} refused configuration changing as the state={}.", getNodeId(), this.state);
            if (done != null) {
                final Status status = new Status();
                if (this.state == State.STATE_TRANSFERRING) {
                    status.setError(RaftError.EBUSY, "Is transferring leadership.");
                } else {
                    status.setError(RaftError.EPERM, "Not leader");
                }
                Utils.runClosureInThread(done, status);
            }
            return;
        }
        // check concurrent conf change
        if (this.confCtx.isBusy()) {
            LOG.warn("Node {} refused configuration concurrent changing.", getNodeId());
            if (done != null) {
                Utils.runClosureInThread(done, new Status(RaftError.EBUSY, "Doing another configuration change."));
            }
            return;
        }
        // Return immediately when the new peers equals to current configuration
        if (this.conf.getConf().equals(newConf)) {
            Utils.runClosureInThread(done);
            return;
        }
        this.confCtx.start(oldConf, newConf, done);
    }
 
Example #30
Source File: RouteTableTest.java    From sofa-jraft with Apache License 2.0 5 votes vote down vote up
@Test
public void testUpdateConfSelectLeader() throws Exception {
    final RouteTable rt = RouteTable.getInstance();
    assertNull(rt.getConfiguration(groupId));
    rt.updateConfiguration(groupId, new Configuration(cluster.getPeers()));
    assertEquals(rt.getConfiguration(groupId), new Configuration(cluster.getPeers()));
    assertNull(rt.selectLeader(groupId));
    assertTrue(rt.refreshLeader(cliClientService, groupId, 10000).isOk());

    final PeerId leader = rt.selectLeader(groupId);
    assertEquals(leader, cluster.getLeader().getNodeId().getPeerId());
}