use of com.alipay.sofa.jraft.conf.Configuration in project sofa-jraft by sofastack.
the class ChaosTestCluster method addPeer.
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 = // use a fake pd
PlacementDriverOptionsConfigured.newConfigured().withFake(true).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();
}
use of com.alipay.sofa.jraft.conf.Configuration in project sofa-jraft by sofastack.
the class ChaosTestCluster method start.
public synchronized void start() {
deleteFiles();
final Configuration conf = new Configuration(this.peerIds);
final String initialServerList = conf.toString();
for (final PeerId p : conf.listPeers()) {
final PlacementDriverOptions pdOpts = // use a fake pd
PlacementDriverOptionsConfigured.newConfigured().withFake(true).config();
final StoreEngineOptions storeOpts = //
StoreEngineOptionsConfigured.newConfigured().withStorageType(//
this.storageType).withRocksDBOptions(//
RocksDBOptionsConfigured.newConfigured().withDbPath(DB_PATH).config()).withRaftDataPath(//
RAFT_DATA_PATH).withServerAddress(//
p.getEndpoint()).withLeastKeysOnSplit(//
10).config();
final RheaKVStoreOptions opts = //
RheaKVStoreOptionsConfigured.newConfigured().withClusterName(//
CLUSTER_NAME).withInitialServerList(initialServerList).withOnlyLeaderRead(//
this.onlyLeaderRead).withStoreEngineOptions(//
storeOpts).withPlacementDriverOptions(//
pdOpts).withFailoverRetries(//
30).withFutureTimeoutMillis(//
TimeUnit.SECONDS.toMillis(60)).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);
}
this.stores.add(store);
}
awaitLeader();
}
use of com.alipay.sofa.jraft.conf.Configuration in project sofa-jraft by sofastack.
the class KVStateMachineTest method setup.
@Before
public void setup() throws IOException, InterruptedException {
final Region region = new Region();
region.setId(1);
final StoreEngine storeEngine = new MockStoreEngine();
final KVStoreStateMachine fsm = new KVStoreStateMachine(region, storeEngine);
final NodeOptions nodeOpts = new NodeOptions();
final Configuration conf = new Configuration();
conf.addPeer(PeerId.parsePeer("127.0.0.1:8081"));
nodeOpts.setInitialConf(conf);
nodeOpts.setFsm(fsm);
final String raftDataPath = "raft_st_test";
this.raftDataPath = new File(raftDataPath);
if (this.raftDataPath.exists()) {
FileUtils.forceDelete(this.raftDataPath);
}
FileUtils.forceMkdir(this.raftDataPath);
final Path logUri = Paths.get(raftDataPath, "log");
nodeOpts.setLogUri(logUri.toString());
final Path meteUri = Paths.get(raftDataPath, "meta");
nodeOpts.setRaftMetaUri(meteUri.toString());
final Path snapshotUri = Paths.get(raftDataPath, "snapshot");
nodeOpts.setSnapshotUri(snapshotUri.toString());
final Endpoint serverAddress = new Endpoint("127.0.0.1", 8081);
final PeerId serverId = new PeerId(serverAddress, 0);
this.raftGroupService = new RaftGroupService("st_test", serverId, nodeOpts, null, true);
final Node node = this.raftGroupService.start(false);
for (int i = 0; i < 100; i++) {
if (node.isLeader()) {
break;
}
Thread.sleep(100);
}
final RawKVStore rawKVStore = storeEngine.getRawKVStore();
this.raftRawKVStore = new RaftRawKVStore(node, rawKVStore, null);
}
use of com.alipay.sofa.jraft.conf.Configuration in project sofa-jraft by sofastack.
the class AbstractChaosTest method chaosSplittingTest.
@Test
public void chaosSplittingTest() {
final List<PeerId> peerIds = TestUtil.generatePeers(INITIAL_PEER_COUNT);
final CliOptions opts = new CliOptions();
opts.setTimeoutMs(30000);
final RheaKVCliService cliService = RheaKVServiceFactory.createAndInitRheaKVCliService(opts);
final long regionId = Constants.DEFAULT_REGION_ID;
final long newRegionId = 2;
final String groupId = JRaftHelper.getJRaftGroupId(ChaosTestCluster.CLUSTER_NAME, regionId);
final Configuration conf = new Configuration(peerIds);
ChaosTestCluster cluster = null;
for (int l = 0; l < RETRIES; l++) {
final ExecutorService executor = Executors.newCachedThreadPool(new NamedThreadFactory("chaos-splitting-test", true));
final List<Future<?>> allFutures = new CopyOnWriteArrayList<>();
try {
cluster = new ChaosTestCluster(peerIds, getStorageType(), isAllowBatching(), isOnlyLeaderRead());
cluster.start();
final RheaKVStore store = cluster.getLeaderStore();
// for least keys on split
for (int j = 0; j < LOOP_2; j++) {
store.bPut(j + "_split_", VALUE);
}
for (int i = 0; i < LOOP_1; i++) {
final int index = i;
final Future<?> f = executor.submit(() -> {
for (int j = 0; j < LOOP_2; j++) {
store.bPut(index + "_split_test_" + j, VALUE);
}
});
allFutures.add(f);
}
final Status st = cliService.rangeSplit(regionId, newRegionId, groupId, conf);
if (!st.isOk()) {
System.err.println("Status:" + st);
throw new RuntimeException(st.toString());
}
// wait for all writes finished
for (final Future<?> f : allFutures) {
f.get(30, TimeUnit.SECONDS);
}
break;
} catch (final Exception e) {
System.err.println("Fail to put data, try again...");
e.printStackTrace();
for (final Future<?> f : allFutures) {
f.cancel(true);
}
if (cluster != null) {
cluster.stopAll();
}
cluster = null;
} finally {
ExecutorServiceHelper.shutdownAndAwaitTermination(executor);
}
}
if (cluster == null) {
throw new RuntimeException("fail to put data, can not check data");
}
try {
chaosSplittingCheckData(cluster);
} finally {
cluster.stopAll();
}
}
use of com.alipay.sofa.jraft.conf.Configuration in project sofa-jraft by sofastack.
the class RouteTable method updateConfiguration.
/**
* 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;
}
}
Aggregations