Skip to content

Commit

Permalink
(feat): add learner support for rheakv (sofastack#381)
Browse files Browse the repository at this point in the history
* (feat): add learner support for rheakv

* typo sofastack#383
  • Loading branch information
fengjiachun authored Dec 30, 2019
1 parent 41a3f02 commit 94c5c06
Show file tree
Hide file tree
Showing 6 changed files with 42 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@
*/
public class ElectionBootstrap {

// 启动 3 个实例选举, 注意如果实在同一台机器启动多个实例, 那么第一个参数 dataPath 不能相同
// Start elections by 3 instance. Note that if multiple instances are started on the same machine,
// the first parameter `dataPath` should not be the same.
public static void main(final String[] args) {
if (args.length < 4) {
System.out
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,12 @@ public synchronized boolean init(final RegionEngineOptions opts) {
// metricsReportPeriod > 0 means enable metrics
nodeOpts.setEnableMetrics(true);
}
nodeOpts.setInitialConf(new Configuration(JRaftHelper.toJRaftPeerIdList(this.region.getPeers())));
final Configuration initialConf = new Configuration();
if (!initialConf.parse(opts.getInitialServerList())) {
LOG.error("Fail to parse initial configuration {}.", opts.getInitialServerList());
return false;
}
nodeOpts.setInitialConf(initialConf);
nodeOpts.setFsm(this.fsm);
final String raftDataPath = opts.getRaftDataPath();
try {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
import com.alipay.sofa.jraft.rhea.errors.NotLeaderException;
import com.alipay.sofa.jraft.rhea.options.RheaKVStoreOptions;
import com.alipay.sofa.jraft.rhea.storage.StorageType;
import com.alipay.sofa.jraft.rhea.util.Lists;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.dataformat.yaml.YAMLFactory;

Expand All @@ -47,7 +48,8 @@ public class RheaKVTestCluster {
public static String RAFT_DATA_PATH = "rhea_raft";
public static Long[] REGION_IDS = new Long[] { 1L, 2L };

private static final String[] CONF = { "/conf/rhea_test_cluster_1.yaml", "/conf/rhea_test_cluster_2.yaml" };
private static final String[] CONF = { "/conf/rhea_test_cluster_1.yaml",
"/conf/rhea_test_cluster_2.yaml", "/conf/rhea_test_cluster_3.yaml" };

private List<RheaKVStore> stores = new CopyOnWriteArrayList<>();

Expand Down Expand Up @@ -122,18 +124,23 @@ protected RheaKVStore getRandomFollowerStore() {

protected RheaKVStore getFollowerStore(final long regionId) {
for (int i = 0; i < 100; i++) {
final List<RheaKVStore> tmp = Lists.newArrayList();
for (final RheaKVStore store : stores) {
if (!((DefaultRheaKVStore) store).isLeader(regionId)) {
return store;
// maybe a learner
tmp.add(store);
}
}
if (!tmp.isEmpty()) {
return tmp.get(ThreadLocalRandom.current().nextInt(tmp.size()));
}
try {
Thread.sleep(100);
} catch (InterruptedException ignored) {
// ignored
}
}
throw new NotLeaderException("no follower");
throw new NotLeaderException("no follower/learner");
}

protected Long getRandomRegionId() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,4 @@ storeEngineOptions:
- { regionId: 2, startKey: g , nodeOptions: { timerPoolSize: 1, rpcProcessorThreadPoolSize: 4 } }
leastKeysOnSplit: 10

initialServerList: 127.0.0.1:18181,127.0.0.1:18182,127.0.0.1:18183
initialServerList: 127.0.0.1:18181,127.0.0.1:18182,127.0.0.1:18183/learner
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,4 @@ storeEngineOptions:
- { regionId: 2, startKey: g , nodeOptions: { timerPoolSize: 1, rpcProcessorThreadPoolSize: 4 } }
leastKeysOnSplit: 10

initialServerList: 127.0.0.1:18181,127.0.0.1:18182,127.0.0.1:18183
initialServerList: 127.0.0.1:18181,127.0.0.1:18182,127.0.0.1:18183/learner
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
##RheaKVStoreOptions
---
clusterName: rhea_test

placementDriverOptions:
fake: true
cliOptions:
rpcProcessorThreadPoolSize: 4

storeEngineOptions:
rocksDBOptions:
dbPath: rhea_db/
raftDataPath: rhea_raft/
serverAddress:
ip: 127.0.0.1
port: 18183
regionEngineOptionsList:
- { regionId: 1, endKey: g, nodeOptions: { timerPoolSize: 1, rpcProcessorThreadPoolSize: 4 } }
- { regionId: 2, startKey: g , nodeOptions: { timerPoolSize: 1, rpcProcessorThreadPoolSize: 4 } }
leastKeysOnSplit: 10

initialServerList: 127.0.0.1:18181,127.0.0.1:18182,127.0.0.1:18183/learner

0 comments on commit 94c5c06

Please sign in to comment.