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

The following examples show how to use com.alipay.sofa.jraft.conf.Configuration. These examples are extracted from open source projects. 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 Project: sofa-jraft   Author: sofastack   File: AbstractPlacementDriverClient.java    License: 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 #2
Source Project: sofa-jraft   Author: sofastack   File: AbstractPlacementDriverClient.java    License: 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 #3
Source Project: sofa-jraft   Author: sofastack   File: AbstractPlacementDriverClient.java    License: 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 #4
Source Project: sofa-jraft   Author: sofastack   File: AbstractPlacementDriverClient.java    License: 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 #5
Source Project: sofa-jraft   Author: sofastack   File: AbstractPlacementDriverClient.java    License: 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 Project: sofa-jraft   Author: sofastack   File: CliServiceTest.java    License: 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 #7
Source Project: sofa-jraft   Author: sofastack   File: AbstractRheaKVStoreTest.java    License: 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 #8
Source Project: sofa-jraft   Author: sofastack   File: StartupConf.java    License: 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 #9
Source Project: sofa-jraft   Author: sofastack   File: ResetPeerRequestProcessor.java    License: 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 #10
Source Project: sofa-jraft   Author: sofastack   File: RouteTable.java    License: 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 #11
Source Project: sofa-jraft   Author: sofastack   File: RouteTable.java    License: 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 #12
Source Project: sofa-jraft   Author: sofastack   File: ReplicatorGroupTest.java    License: 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 #13
Source Project: sofa-jraft   Author: sofastack   File: BallotBox.java    License: 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 #14
Source Project: sofa-jraft   Author: sofastack   File: CliServiceImpl.java    License: 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 #15
Source Project: sofa-jraft   Author: sofastack   File: NodeImpl.java    License: 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 #16
Source Project: sofa-jraft   Author: sofastack   File: NodeImpl.java    License: 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 #17
Source Project: sofa-jraft   Author: sofastack   File: NodeImpl.java    License: 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 #18
Source Project: sofa-jraft   Author: sofastack   File: NodeImpl.java    License: 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 #19
Source Project: sofa-jraft   Author: sofastack   File: ChaosTestCluster.java    License: Apache License 2.0 5 votes vote down vote up
public synchronized void addPeer(final PeerId peerId) {
    if (this.peerIds.contains(peerId)) {
        throw new RuntimeException("peerId is exist: " + peerId);
    }
    this.peerIds.add(peerId);
    final Configuration conf = new Configuration(this.peerIds);
    final String initialServerList = conf.toString();
    final PlacementDriverOptions pdOpts = PlacementDriverOptionsConfigured.newConfigured().withFake(true) // use a fake pd
        .config();
    final StoreEngineOptions storeOpts = StoreEngineOptionsConfigured.newConfigured() //
        .withStorageType(this.storageType) //
        .withRocksDBOptions(RocksDBOptionsConfigured.newConfigured().withDbPath(DB_PATH).config()) //
        .withRaftDataPath(RAFT_DATA_PATH) //
        .withServerAddress(peerId.getEndpoint()) //
        .config();
    final RheaKVStoreOptions opts = RheaKVStoreOptionsConfigured.newConfigured() //
        .withClusterName("chaos_test") //
        .withInitialServerList(initialServerList).withStoreEngineOptions(storeOpts) //
        .withPlacementDriverOptions(pdOpts) //
        .config();
    BatchingOptions batchingOptions = opts.getBatchingOptions();
    if (batchingOptions == null) {
        batchingOptions = new BatchingOptions();
    }
    batchingOptions.setAllowBatching(this.allowBatching);
    opts.setBatchingOptions(batchingOptions);

    final RheaKVStore store = new DefaultRheaKVStore();
    if (!store.init(opts)) {
        throw new IllegalStateException("fail to init store with options: " + opts);
    }

    final RheaKVStore leader = getLeaderStore();
    final PlacementDriverClient pdClient = leader.getPlacementDriverClient();
    if (!pdClient.addReplica(Constants.DEFAULT_REGION_ID, JRaftHelper.toPeer(peerId), true)) {
        throw new RuntimeException("fail to add peer: " + peerId);
    }
    this.stores.add(store);
    awaitLeader();
}
 
Example #20
Source Project: sofa-jraft   Author: sofastack   File: CounterServer.java    License: Apache License 2.0 5 votes vote down vote up
public static void main(final String[] args) throws IOException {
    if (args.length != 4) {
        System.out
            .println("Useage : java com.alipay.sofa.jraft.example.counter.CounterServer {dataPath} {groupId} {serverId} {initConf}");
        System.out
            .println("Example: java com.alipay.sofa.jraft.example.counter.CounterServer /tmp/server1 counter 127.0.0.1:8081 127.0.0.1:8081,127.0.0.1:8082,127.0.0.1:8083");
        System.exit(1);
    }
    final String dataPath = args[0];
    final String groupId = args[1];
    final String serverIdStr = args[2];
    final String initConfStr = args[3];

    final NodeOptions nodeOptions = new NodeOptions();
    // 为了测试,调整 snapshot 间隔等参数
    // 设置选举超时时间为 1 秒
    nodeOptions.setElectionTimeoutMs(1000);
    // 关闭 CLI 服务。
    nodeOptions.setDisableCli(false);
    // 每隔30秒做一次 snapshot
    nodeOptions.setSnapshotIntervalSecs(30);
    // 解析参数
    final PeerId serverId = new PeerId();
    if (!serverId.parse(serverIdStr)) {
        throw new IllegalArgumentException("Fail to parse serverId:" + serverIdStr);
    }
    final Configuration initConf = new Configuration();
    if (!initConf.parse(initConfStr)) {
        throw new IllegalArgumentException("Fail to parse initConf:" + initConfStr);
    }
    // 设置初始集群配置
    nodeOptions.setInitialConf(initConf);

    // 启动
    final CounterServer counterServer = new CounterServer(dataPath, groupId, serverId, nodeOptions);
    System.out.println("Started counter server at port:"
                       + counterServer.getNode().getNodeId().getPeerId().getPort());
}
 
Example #21
Source Project: sofa-jraft   Author: sofastack   File: RouteTableTest.java    License: Apache License 2.0 5 votes vote down vote up
@Test
public void testRefreshLeaderWhenFirstPeerDown() throws Exception {
    final RouteTable rt = RouteTable.getInstance();
    rt.updateConfiguration(groupId, new Configuration(cluster.getPeers()));
    assertTrue(rt.refreshLeader(cliClientService, groupId, 10000).isOk());
    cluster.stop(cluster.getPeers().get(0).getEndpoint());
    Thread.sleep(1000);
    this.cluster.waitLeader();
    assertTrue(rt.refreshLeader(cliClientService, groupId, 10000).isOk());
}
 
Example #22
Source Project: sofa-jraft   Author: sofastack   File: RouteTableTest.java    License: Apache License 2.0 5 votes vote down vote up
@Test
public void testRefreshConfiguration() throws Exception {
    final RouteTable rt = RouteTable.getInstance();
    final List<PeerId> partConf = new ArrayList<>();
    partConf.add(cluster.getLeader().getLeaderId());
    // part of peers conf, only contains leader peer
    rt.updateConfiguration(groupId, new Configuration(partConf));
    // fetch all conf
    final Status st = rt.refreshConfiguration(cliClientService, groupId, 10000);
    assertTrue(st.isOk());
    final Configuration newCnf = rt.getConfiguration(groupId);
    assertArrayEquals(new HashSet<>(cluster.getPeers()).toArray(), new HashSet<>(newCnf.getPeerSet()).toArray());
}
 
Example #23
Source Project: sofa-jraft   Author: sofastack   File: RouteTableTest.java    License: Apache License 2.0 5 votes vote down vote up
@Test
public void testRefreshConfigurationFail() throws Exception {
    cluster.stopAll();
    final RouteTable rt = RouteTable.getInstance();
    rt.updateConfiguration(groupId, new Configuration(cluster.getPeers()));
    final Status st = rt.refreshConfiguration(cliClientService, groupId, 10000);
    assertFalse(st.isOk());
}
 
Example #24
Source Project: sofa-registry   Author: sofastack   File: RaftServer.java    License: Apache License 2.0 5 votes vote down vote up
/**
 *
 * @param dataPath    Example: /tmp/server1
 * @param groupId
 * @param serverIdStr Example: 127.0.0.1:8081
 * @param initConfStr Example: 127.0.0.1:8081,127.0.0.1:8082,127.0.0.1:8083
 * @throws IOException
 */
public RaftServer(String dataPath, String groupId, String serverIdStr, String initConfStr) {
    this.dataPath = dataPath;
    this.groupId = groupId;
    serverId = new PeerId();
    if (!serverId.parse(serverIdStr)) {
        throw new IllegalArgumentException("Fail to parse serverId:" + serverIdStr);
    }

    initConf = new Configuration();
    if (!initConf.parse(initConfStr)) {
        throw new IllegalArgumentException("Fail to parse initConf:" + initConfStr);
    }
}
 
Example #25
Source Project: sofa-jraft   Author: sofastack   File: CliServiceTest.java    License: Apache License 2.0 5 votes vote down vote up
@Override
public Status getLeader(final String groupId, final Configuration conf, final PeerId leaderId) {
    final PeerId ret = this.rebalancedLeaderIds.get(groupId);
    if (ret != null) {
        leaderId.parse(ret.toString());
    } else {
        leaderId.parse(this.initialLeaderId.toString());
    }
    return Status.OK();
}
 
Example #26
Source Project: sofa-jraft   Author: sofastack   File: NodeImpl.java    License: Apache License 2.0 5 votes vote down vote up
@Override
public void removePeer(final PeerId peer, final Closure done) {
    Requires.requireNonNull(peer, "Null peer");
    this.writeLock.lock();
    try {
        Requires.requireTrue(this.conf.getConf().contains(peer), "Peer not found in current configuration");

        final Configuration newConf = new Configuration(this.conf.getConf());
        newConf.removePeer(peer);
        unsafeRegisterConfChange(this.conf.getConf(), newConf, done);
    } finally {
        this.writeLock.unlock();
    }
}
 
Example #27
Source Project: sofa-registry   Author: sofastack   File: RaftExchanger.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * api for change meta node
 */
public void changePeer(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);
            }

            Status status = cliService.changePeers(getGroup(), getCurrentConfiguration(),
                peersConf);

            if (!status.isOk()) {
                LOGGER.error("CliService change peer fail!error message {}",
                    status.getErrorMsg());
                throw new RuntimeException("CliService change 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 change peer error!", e);
        throw new RuntimeException("CliService change peer error!", e);
    }
}
 
Example #28
Source Project: sofa-jraft   Author: sofastack   File: RouteTableTest.java    License: Apache License 2.0 5 votes vote down vote up
@Test
public void testRefreshConfiguration() throws Exception {
    final RouteTable rt = RouteTable.getInstance();
    final List<PeerId> partConf = new ArrayList<>();
    partConf.add(cluster.getLeader().getLeaderId());
    // part of peers conf, only contains leader peer
    rt.updateConfiguration(groupId, new Configuration(partConf));
    // fetch all conf
    final Status st = rt.refreshConfiguration(cliClientService, groupId, 10000);
    assertTrue(st.isOk());
    final Configuration newCnf = rt.getConfiguration(groupId);
    assertArrayEquals(new HashSet<>(cluster.getPeers()).toArray(), new HashSet<>(newCnf.getPeerSet()).toArray());
}
 
Example #29
Source Project: sofa-jraft   Author: sofastack   File: AtomicRangeGroup.java    License: Apache License 2.0 5 votes vote down vote up
public static AtomicRangeGroup start(StartupConf conf, RpcServer rpcServer) throws IOException {

        final NodeOptions nodeOptions = new NodeOptions();
        // Set election timeout to 1 second
        nodeOptions.setElectionTimeoutMs(1000);
        // Close cli service
        nodeOptions.setDisableCli(false);
        // A snapshot saving would be triggered every 30 seconds
        // nodeOptions.setSnapshotIntervalSecs(30);
        // Parsing Options
        final PeerId serverId = new PeerId();
        if (!serverId.parse(conf.getServerAddress())) {
            throw new IllegalArgumentException("Fail to parse serverId:" + conf.getServerAddress());
        }
        final Configuration initConf = new Configuration();
        if (!initConf.parse(conf.getConf())) {
            throw new IllegalArgumentException("Fail to parse initConf:" + conf.getConf());
        }
        // Set the initial cluster configuration
        nodeOptions.setInitialConf(initConf);
        // Startup node
        final AtomicRangeGroup node = new AtomicRangeGroup(conf.getDataPath(), conf.getGroupId(), serverId,
            conf.getMinSlot(), conf.getMaxSlot(), nodeOptions, rpcServer);
        LOG.info("Started range node[{}-{}] at port:{}", conf.getMinSlot(), conf.getMaxSlot(), node.getNode()
            .getNodeId().getPeerId().getPort());
        return node;
    }
 
Example #30
Source Project: sofa-jraft   Author: sofastack   File: NodeImpl.java    License: Apache License 2.0 5 votes vote down vote up
@Override
public void resetLearners(final List<PeerId> learners, final Closure done) {
    checkPeers(learners);
    this.writeLock.lock();
    try {
        final Configuration newConf = new Configuration(this.conf.getConf());
        newConf.setLearners(new LinkedHashSet<>(learners));
        unsafeRegisterConfChange(this.conf.getConf(), newConf, done);
    } finally {
        this.writeLock.unlock();
    }
}