use of com.alipay.sofa.jraft.rhea.options.RegionRouteTableOptions in project sofa-jraft by sofastack.
the class MultiRegionRouteTableOptionsConfigured method getOrCreateOptsById.
private RegionRouteTableOptions getOrCreateOptsById(final Long regionId) {
Requires.requireNonNull(regionId, "regionId");
RegionRouteTableOptions opts = this.optsTable.get(regionId);
if (opts != null) {
return opts;
}
opts = new RegionRouteTableOptions();
opts.setRegionId(regionId);
this.optsTable.put(regionId, opts);
return opts;
}
use of com.alipay.sofa.jraft.rhea.options.RegionRouteTableOptions in project sofa-jraft by sofastack.
the class BenchmarkClient method main.
public static void main(final String[] args) {
if (args.length < 7) {
LOG.error("Args: [initialServerList], [configPath], [threads], [writeRatio], [readRatio], [valueSize] are needed.");
System.exit(-1);
}
final String initialServerList = args[1];
final String configPath = args[2];
final int threads = Integer.parseInt(args[3]);
final int writeRatio = Integer.parseInt(args[4]);
final int readRatio = Integer.parseInt(args[5]);
final int valueSize = Integer.parseInt(args[6]);
final RheaKVStoreOptions opts = Yaml.readConfig(configPath);
opts.setInitialServerList(initialServerList);
final RheaKVStore rheaKVStore = new DefaultRheaKVStore();
if (!rheaKVStore.init(opts)) {
LOG.error("Fail to init [RheaKVStore]");
System.exit(-1);
}
final List<RegionRouteTableOptions> regionRouteTableOptionsList = opts.getPlacementDriverOptions().getRegionRouteTableOptionsList();
rebalance(rheaKVStore, initialServerList, regionRouteTableOptionsList);
rheaKVStore.bPut("benchmark", BytesUtil.writeUtf8("benchmark start at: " + new Date()));
LOG.info(BytesUtil.readUtf8(rheaKVStore.bGet("benchmark")));
//
ConsoleReporter.forRegistry(KVMetrics.metricRegistry()).build().start(30, TimeUnit.SECONDS);
LOG.info("Start benchmark...");
startBenchmark(rheaKVStore, threads, writeRatio, readRatio, valueSize, regionRouteTableOptionsList);
}
use of com.alipay.sofa.jraft.rhea.options.RegionRouteTableOptions 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.options.RegionRouteTableOptions in project sofa-jraft by sofastack.
the class Client method init.
public void init() {
final List<RegionRouteTableOptions> regionRouteTableOptionsList = MultiRegionRouteTableOptionsConfigured.newConfigured().withInitialServerList(-1L, /* default id */
Configs.ALL_NODE_ADDRESSES).config();
final PlacementDriverOptions pdOpts = //
PlacementDriverOptionsConfigured.newConfigured().withFake(//
true).withRegionRouteTableOptionsList(//
regionRouteTableOptionsList).config();
final RheaKVStoreOptions opts = //
RheaKVStoreOptionsConfigured.newConfigured().withClusterName(//
Configs.CLUSTER_NAME).withPlacementDriverOptions(//
pdOpts).config();
System.out.println(opts);
rheaKVStore.init(opts);
}
use of com.alipay.sofa.jraft.rhea.options.RegionRouteTableOptions in project sofa-jraft by sofastack.
the class AbstractPlacementDriverClient method init.
@Override
public synchronized boolean init(final PlacementDriverOptions opts) {
initCli(opts.getCliOptions());
this.pdRpcService = new DefaultPlacementDriverRpcService(this);
RpcOptions rpcOpts = opts.getPdRpcOptions();
if (rpcOpts == null) {
rpcOpts = RpcOptionsConfigured.newDefaultConfig();
rpcOpts.setCallbackExecutorCorePoolSize(0);
rpcOpts.setCallbackExecutorMaximumPoolSize(0);
}
if (!this.pdRpcService.init(rpcOpts)) {
LOG.error("Fail to init [PlacementDriverRpcService].");
return false;
}
// region route table
final List<RegionRouteTableOptions> regionRouteTableOptionsList = opts.getRegionRouteTableOptionsList();
if (regionRouteTableOptionsList != null) {
final String initialServerList = opts.getInitialServerList();
for (final RegionRouteTableOptions regionRouteTableOpts : regionRouteTableOptionsList) {
if (Strings.isBlank(regionRouteTableOpts.getInitialServerList())) {
// if blank, extends parent's value
regionRouteTableOpts.setInitialServerList(initialServerList);
}
initRouteTableByRegion(regionRouteTableOpts);
}
}
return true;
}
Aggregations