use of com.alipay.sofa.jraft.util.Endpoint in project sofa-jraft by sofastack.
the class InstructionProcessor method processTransferLeader.
private boolean processTransferLeader(final Instruction instruction) {
try {
final Instruction.TransferLeader transferLeader = instruction.getTransferLeader();
if (transferLeader == null) {
return false;
}
final Endpoint toEndpoint = transferLeader.getMoveToEndpoint();
if (toEndpoint == null) {
LOG.error("TransferLeader#toEndpoint must not be null, {}.", instruction);
return false;
}
final Region region = instruction.getRegion();
final long regionId = region.getId();
final RegionEngine engine = this.storeEngine.getRegionEngine(regionId);
if (engine == null) {
LOG.error("Could not found regionEngine, {}.", instruction);
return false;
}
if (!region.equals(engine.getRegion())) {
LOG.warn("Instruction [{}] is out of date.", instruction);
return false;
}
return engine.transferLeadershipTo(toEndpoint);
} catch (final Throwable t) {
LOG.error("Caught an exception on #processTransferLeader: {}.", StackTraceUtil.stackTrace(t));
return false;
}
}
use of com.alipay.sofa.jraft.util.Endpoint in project sofa-jraft by sofastack.
the class MetadataRpcClient method internalCreateRegionId.
private void internalCreateRegionId(final long clusterId, final Endpoint endpoint, final CompletableFuture<Long> future, final int retriesLeft, final Errors lastCause) {
final RetryRunner retryRunner = retryCause -> internalCreateRegionId(clusterId, endpoint, future, retriesLeft - 1, retryCause);
final FailoverClosure<Long> closure = new FailoverClosureImpl<>(future, retriesLeft, retryRunner);
final CreateRegionIdRequest request = new CreateRegionIdRequest();
request.setClusterId(clusterId);
request.setEndpoint(endpoint);
this.pdRpcService.callPdServerWithRpc(request, closure, lastCause);
}
use of com.alipay.sofa.jraft.util.Endpoint in project sofa-jraft by sofastack.
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 com.alipay.sofa.jraft.util.Endpoint 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.util.Endpoint in project sofa-jraft by sofastack.
the class Server2 method main.
public static void main(final String[] args) throws Exception {
final PlacementDriverOptions pdOpts = PlacementDriverOptionsConfigured.newConfigured().withFake(// use a fake pd
true).config();
final StoreEngineOptions storeOpts = //
StoreEngineOptionsConfigured.newConfigured().withStorageType(StorageType.RocksDB).withRocksDBOptions(RocksDBOptionsConfigured.newConfigured().withDbPath(Configs.DB_PATH).config()).withRaftDataPath(Configs.RAFT_DATA_PATH).withServerAddress(new Endpoint("127.0.0.1", 8182)).config();
final RheaKVStoreOptions opts = //
RheaKVStoreOptionsConfigured.newConfigured().withClusterName(//
Configs.CLUSTER_NAME).withUseParallelCompress(//
true).withInitialServerList(Configs.ALL_NODE_ADDRESSES).withStoreEngineOptions(//
storeOpts).withPlacementDriverOptions(//
pdOpts).config();
System.out.println(opts);
final Node node = new Node(opts);
node.start();
Runtime.getRuntime().addShutdownHook(new Thread(node::stop));
System.out.println("server2 start OK");
}
Aggregations