use of io.dingodb.raft.util.Endpoint in project dingo by dingodb.
the class PeerId method parse.
/**
* Parse peerId from string that generated by {@link #toString()}
* This method can support parameter string values are below:
*
* <pre>
* PeerId.parse("a:b") = new PeerId("a", "b", 0 , -1)
* PeerId.parse("a:b:c") = new PeerId("a", "b", "c", -1)
* PeerId.parse("a:b::d") = new PeerId("a", "b", 0, "d")
* PeerId.parse("a:b:c:d") = new PeerId("a", "b", "c", "d")
* </pre>
*/
public boolean parse(final String s) {
if (StringUtils.isEmpty(s)) {
return false;
}
final String[] tmps = Utils.parsePeerId(s);
if (tmps.length < 2 || tmps.length > 4) {
return false;
}
try {
final int port = Integer.parseInt(tmps[1]);
this.endpoint = new Endpoint(tmps[0], port);
switch(tmps.length) {
case 3:
this.idx = Integer.parseInt(tmps[2]);
break;
case 4:
if (tmps[2].equals("")) {
this.idx = 0;
} else {
this.idx = Integer.parseInt(tmps[2]);
}
this.priority = Integer.parseInt(tmps[3]);
break;
default:
break;
}
this.str = null;
return true;
} catch (final Exception e) {
LOG.error("Parse peer from string failed: {}.", s, e);
return false;
}
}
use of io.dingodb.raft.util.Endpoint in project dingo by dingodb.
the class DefaultDingoRowStore method init.
@Override
public synchronized boolean init(final DingoRowStoreOptions opts) {
if (this.started) {
LOG.info("[DefaultDingoRowStore] already started.");
return true;
}
DescriberManager.getInstance().addDescriber(RouteTable.getInstance());
this.opts = opts;
// init placement driver
final PlacementDriverOptions pdOpts = opts.getPlacementDriverOptions();
final String clusterName = opts.getClusterName();
Requires.requireNonNull(pdOpts, "opts.placementDriverOptions");
Requires.requireNonNull(clusterName, "opts.clusterName");
if (Strings.isBlank(pdOpts.getInitialServerList())) {
// if blank, extends parent's value
pdOpts.setInitialServerList(opts.getInitialServerList());
}
if (pdOpts.isFake()) {
this.pdClient = new FakePlacementDriverClient(opts.getClusterId(), clusterName);
} else {
this.pdClient = new RemotePlacementDriverClient(opts.getClusterId(), clusterName);
}
if (!this.pdClient.init(pdOpts)) {
LOG.error("Fail to init [PlacementDriverClient].");
return false;
}
// init compress strategies
ZipStrategyManager.init(opts);
// init store engine
final StoreEngineOptions stOpts = opts.getStoreEngineOptions();
if (stOpts != null) {
stOpts.setInitialServerList(opts.getInitialServerList());
this.storeEngine = new StoreEngine(this.pdClient, this.stateListenerContainer);
if (!this.storeEngine.init(stOpts)) {
LOG.error("Fail to init [StoreEngine].");
return false;
}
}
final Endpoint selfEndpoint = this.storeEngine == null ? null : this.storeEngine.getSelfEndpoint();
final RpcOptions rpcOpts = opts.getRpcOptions();
Requires.requireNonNull(rpcOpts, "opts.rpcOptions");
this.dingoRowStoreRpcService = new DefaultDingoRowStoreRpcService(this.pdClient, selfEndpoint) {
@Override
public Endpoint getLeader(final String regionId, final boolean forceRefresh, final long timeoutMillis) {
final Endpoint leader = getLeaderByRegionEngine(regionId);
if (leader != null) {
return leader;
}
return super.getLeader(regionId, forceRefresh, timeoutMillis);
}
};
if (!this.dingoRowStoreRpcService.init(rpcOpts)) {
LOG.error("Fail to init [DingoRowStoreRpcService].");
return false;
}
this.failoverRetries = opts.getFailoverRetries();
this.futureTimeoutMillis = opts.getFutureTimeoutMillis();
this.onlyLeaderRead = opts.isOnlyLeaderRead();
if (opts.isUseParallelKVExecutor()) {
final int numWorkers = Utils.cpus();
final int bufSize = numWorkers << 4;
final String name = "parallel-kv-executor";
final ThreadFactory threadFactory = Constants.THREAD_AFFINITY_ENABLED ? new AffinityNamedThreadFactory(name, true) : new NamedThreadFactory(name, true);
this.kvDispatcher = new TaskDispatcher(bufSize, numWorkers, WaitStrategyType.LITE_BLOCKING_WAIT, threadFactory);
}
this.batchingOpts = opts.getBatchingOptions();
if (this.batchingOpts.isAllowBatching()) {
this.getBatching = new GetBatching(KeyEvent::new, "get_batching", new GetBatchingHandler("get", false));
this.getBatchingOnlySafe = new GetBatching(KeyEvent::new, "get_batching_only_safe", new GetBatchingHandler("get_only_safe", true));
this.putBatching = new PutBatching(KVEvent::new, "put_batching", new PutBatchingHandler("put"));
}
LOG.info("[DefaultDingoRowStore] start successfully, options: {}.", opts);
return this.started = true;
}
use of io.dingodb.raft.util.Endpoint in project dingo by dingodb.
the class JRaftHelper method toPeer.
public static Peer toPeer(final PeerId peerId) {
Requires.requireNonNull(peerId, "peerId");
final Endpoint endpoint = peerId.getEndpoint();
Requires.requireNonNull(endpoint, "peerId.endpoint");
final Peer peer = new Peer();
peer.setId("-1");
peer.setStoreId("-1");
peer.setEndpoint(endpoint.copy());
return peer;
}
use of io.dingodb.raft.util.Endpoint in project dingo by dingodb.
the class RegionHeartbeatHandler method balance.
private Instruction balance(Region region, RegionStats regionStats) {
final long clusterId = 0;
final String storeId = regionStats.getLeader().getStoreId();
final ClusterStatsManager clusterStatsManager = ClusterStatsManager.getInstance(clusterId);
clusterStatsManager.addOrUpdateLeader(storeId, region.getId());
// check if the modelWorker
final Pair<Set<String>, Integer> modelWorkers = clusterStatsManager.findModelWorkerStores(1);
final Set<String> modelWorkerStoreIds = modelWorkers.getKey();
final int modelWorkerLeaders = modelWorkers.getValue();
if (!modelWorkerStoreIds.contains(storeId)) {
return null;
}
log.info("[Cluster] model worker stores is: {}, it has {} leaders.", modelWorkerStoreIds, modelWorkerLeaders);
final List<Peer> peers = region.getPeers();
if (peers == null) {
return null;
}
final List<Endpoint> endpoints = Lists.transform(peers, Peer::getEndpoint);
final Map<String, Endpoint> storeIds = rowStoreMetaAdaptor.storeLocation();
// find lazyWorkers
final List<Pair<String, Integer>> lazyWorkers = clusterStatsManager.findLazyWorkerStores(storeIds.keySet());
if (lazyWorkers.isEmpty()) {
return null;
}
for (int i = lazyWorkers.size() - 1; i >= 0; i--) {
final Pair<String, Integer> worker = lazyWorkers.get(i);
if (modelWorkerLeaders - worker.getValue() <= 1) {
// no need to transfer
lazyWorkers.remove(i);
}
}
if (lazyWorkers.isEmpty()) {
return null;
}
final Pair<String, Integer> laziestWorker = tryToFindLaziestWorker(lazyWorkers);
if (laziestWorker == null) {
return null;
}
final String lazyWorkerStoreId = laziestWorker.getKey();
log.info("[Cluster: {}], lazy worker store is: {}, it has {} leaders.", clusterId, lazyWorkerStoreId, laziestWorker.getValue());
final Instruction.TransferLeader transferLeader = new Instruction.TransferLeader();
transferLeader.setMoveToStoreId(lazyWorkerStoreId);
transferLeader.setMoveToEndpoint(storeIds.get(lazyWorkerStoreId));
final Instruction instruction = new Instruction();
instruction.setRegion(region.copy());
instruction.setTransferLeader(transferLeader);
log.info("[Cluster: {}], send 'instruction.transferLeader': {} to region: {}.", clusterId, instruction, region);
return instruction;
}
use of io.dingodb.raft.util.Endpoint in project dingo by dingodb.
the class TableMetaAdaptorImpl method rangeLocationGroup.
@Override
public NavigableMap<byte[], LocationGroup> rangeLocationGroup() {
NavigableMap<byte[], LocationGroup> result = new TreeMap<>(BytesUtil.getDefaultByteArrayComparator());
Map<GeneralId, AppView<?, ?>> map = this.scheduleMetaAdaptor.namespaceView().appViews();
for (Map.Entry<GeneralId, AppView<?, ?>> entry : map.entrySet()) {
if (entry.getValue() instanceof RegionView) {
RegionView view = (RegionView) entry.getValue();
ExecutorView executorView = this.scheduleMetaAdaptor.executorView(view.leader());
Endpoint endpoint = executorView.stats().getLocation();
Location location = new Location(endpoint.getIp(), endpoint.getPort(), DATA_DIR);
List<Location> locationList = view.nodeResources().stream().map(id -> this.scheduleMetaAdaptor.namespaceView().<ExecutorView>getResourceView(id)).map(ExecutorView::location).collect(Collectors.toList());
LocationGroup locationGroup = new LocationGroup(location, locationList);
RegionApp regionApp = this.scheduleMetaAdaptor.regionApp(view.app());
result.put(BytesUtil.nullToEmpty(regionApp.startKey()), locationGroup);
}
}
return result;
}
Aggregations