use of com.alipay.sofa.jraft.rhea.client.RheaKVStore 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.rhea.client.RheaKVStore 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.rhea.client.RheaKVStore in project sofa-jraft by sofastack.
the class ChaosTestCluster method getByStorePeer.
public synchronized RheaKVStore getByStorePeer(final PeerId peerId) {
awaitLeader();
final Endpoint endpoint = JRaftHelper.toPeer(peerId).getEndpoint();
for (final RheaKVStore store : this.stores) {
if (endpoint.equals(getSelfEndpoint(store))) {
return store;
}
}
throw new RuntimeException("fail to get peer: " + peerId);
}
use of com.alipay.sofa.jraft.rhea.client.RheaKVStore in project sofa-jraft by sofastack.
the class ChaosTestCluster method randomTransferLeader.
public synchronized void randomTransferLeader() {
final RheaKVStore leader = getLeaderStore();
final Endpoint leaderEndpoint = getSelfEndpoint(leader);
final PlacementDriverClient pdClient = leader.getPlacementDriverClient();
final Peer randomPeer = JRaftHelper.toPeer(getRandomPeer());
boolean result = pdClient.transferLeader(Constants.DEFAULT_REGION_ID, randomPeer, false);
if (!result) {
throw new RuntimeException("fail to transfer leader [" + leaderEndpoint + " --> " + randomPeer);
}
LOG.info("Transfer leader from {} to {}", leaderEndpoint, randomPeer.getEndpoint());
}
use of com.alipay.sofa.jraft.rhea.client.RheaKVStore in project sofa-jraft by sofastack.
the class RheaHeartbeatTest method putAndGetValue.
private void putAndGetValue() {
final RheaKVStore store = getLeaderStore(ThreadLocalRandom.current().nextInt(1, 2));
final String key = UUID.randomUUID().toString();
final byte[] value = makeValue(UUID.randomUUID().toString());
store.bPut(key, value);
final byte[] newValue = store.bGet(key);
Assert.assertArrayEquals(value, newValue);
}
Aggregations