use of com.alipay.sofa.jraft.rhea.client.pd.PlacementDriverClient 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.pd.PlacementDriverClient 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.pd.PlacementDriverClient in project sofa-jraft by sofastack.
the class RheaBenchmarkCluster method start.
protected void start() throws IOException, InterruptedException {
SystemPropertyUtil.setProperty(Configs.NETTY_BUFFER_LOW_WATERMARK, Integer.toString(256 * 1024));
SystemPropertyUtil.setProperty(Configs.NETTY_BUFFER_HIGH_WATERMARK, Integer.toString(512 * 1024));
File file = new File("benchmark_rhea_db");
if (file.exists()) {
FileUtils.forceDelete(file);
}
file = new File("benchmark_rhea_db");
if (file.mkdir()) {
this.tempDbPath = file.getAbsolutePath();
System.out.println("make dir: " + this.tempDbPath);
}
file = new File("benchmark_rhea_raft");
if (file.exists()) {
FileUtils.forceDelete(file);
}
file = new File("benchmark_rhea_raft");
if (file.mkdir()) {
this.tempRaftPath = file.getAbsolutePath();
System.out.println("make dir: " + this.tempRaftPath);
}
for (String c : CONF) {
ObjectMapper mapper = new ObjectMapper(new YAMLFactory());
final RheaKVStoreOptions opts = mapper.readValue(new File(c), RheaKVStoreOptions.class);
RheaKVStore rheaKVStore = new DefaultRheaKVStore();
if (rheaKVStore.init(opts)) {
stores.add(rheaKVStore);
} else {
throw new RuntimeException("Fail to init rhea kv store witch conf: " + c);
}
}
PlacementDriverClient pdClient = stores.get(0).getPlacementDriverClient();
Endpoint leader1 = pdClient.getLeader(1, true, 10000);
System.out.println("The region 1 leader is: " + leader1);
// Endpoint leader2 = pdClient.getLeader(2, true, 10000);
// System.out.println("The region 2 leader is: " + leader2);
// Endpoint leader3 = pdClient.getLeader(3, true, 10000);
// System.out.println("The region 3 leader is: " + leader3);
}
use of com.alipay.sofa.jraft.rhea.client.pd.PlacementDriverClient in project sofa-jraft by sofastack.
the class BenchmarkClient method rebalance.
// Because we use fake PD, so we need manual rebalance
public static void rebalance(final RheaKVStore rheaKVStore, final String initialServerList, final List<RegionRouteTableOptions> regionRouteTableOptionsList) {
final PlacementDriverClient pdClient = rheaKVStore.getPlacementDriverClient();
final Configuration configuration = new Configuration();
configuration.parse(initialServerList);
final int serverSize = configuration.size();
final int regionSize = regionRouteTableOptionsList.size();
final int regionSizePerServer = regionSize / serverSize;
final Queue<Long> regions = new ArrayDeque<>();
for (final RegionRouteTableOptions r : regionRouteTableOptionsList) {
regions.add(r.getRegionId());
}
final Map<PeerId, Integer> peerMap = Maps.newHashMap();
for (; ; ) {
final Long regionId = regions.poll();
if (regionId == null) {
break;
}
PeerId peerId;
try {
final Endpoint endpoint = pdClient.getLeader(regionId, true, 10000);
if (endpoint == null) {
continue;
}
peerId = new PeerId(endpoint, 0);
LOG.info("Region {} leader is {}", regionId, peerId);
} catch (final Exception e) {
regions.add(regionId);
continue;
}
final Integer size = peerMap.get(peerId);
if (size == null) {
peerMap.put(peerId, 1);
continue;
}
if (size < regionSizePerServer) {
peerMap.put(peerId, size + 1);
continue;
}
for (final PeerId p : configuration.listPeers()) {
final Integer pSize = peerMap.get(p);
if (pSize != null && pSize >= regionSizePerServer) {
continue;
}
try {
pdClient.transferLeader(regionId, JRaftHelper.toPeer(p), true);
LOG.info("Region {} transfer leader to {}", regionId, p);
regions.add(regionId);
break;
} catch (final Exception e) {
LOG.error("Fail to transfer leader to {}", p);
}
}
}
for (final RegionRouteTableOptions r : regionRouteTableOptionsList) {
final Long regionId = r.getRegionId();
try {
final Endpoint endpoint = pdClient.getLeader(regionId, true, 10000);
LOG.info("Finally, the region: {} leader is: {}", regionId, endpoint);
} catch (final Exception e) {
LOG.error("Fail to get leader: {}", StackTraceUtil.stackTrace(e));
}
}
}
use of com.alipay.sofa.jraft.rhea.client.pd.PlacementDriverClient in project sofa-jraft by sofastack.
the class RheaKVTestCluster method start.
protected void start() throws IOException, InterruptedException {
System.out.println("RheaKVTestCluster init ...");
File file = new File("rhea_pd_db");
if (file.exists()) {
FileUtils.forceDelete(file);
}
file = new File("rhea_pd_db");
if (file.mkdir()) {
this.tempDbPath = file.getAbsolutePath();
System.out.println("make dir: " + this.tempDbPath);
}
file = new File("rhea_pd_raft");
if (file.exists()) {
FileUtils.forceDelete(file);
}
file = new File("rhea_pd_raft");
if (file.mkdir()) {
this.tempRaftPath = file.getAbsolutePath();
System.out.println("make dir: " + this.tempRaftPath);
}
final Set<Long> regionIds = new HashSet<>();
for (final String c : CONF) {
final ObjectMapper mapper = new ObjectMapper(new YAMLFactory());
final InputStream in = RheaKVTestCluster.class.getResourceAsStream(c);
final RheaKVStoreOptions opts = mapper.readValue(in, RheaKVStoreOptions.class);
for (final RegionEngineOptions rOpts : opts.getStoreEngineOptions().getRegionEngineOptionsList()) {
regionIds.add(rOpts.getRegionId());
}
final RheaKVStore rheaKVStore = new DefaultRheaKVStore();
if (rheaKVStore.init(opts)) {
stores.add(rheaKVStore);
} else {
System.err.println("Fail to init rhea kv store witch conf: " + c);
}
}
final PlacementDriverClient pdClient = stores.get(0).getPlacementDriverClient();
for (final Long regionId : regionIds) {
final Endpoint leader = pdClient.getLeader(regionId, true, 10000);
System.out.println("The region " + regionId + " leader is: " + leader);
}
}
Aggregations