use of com.alipay.sofa.jraft.Status in project sofa-jraft by sofastack.
the class AbstractPlacementDriverClient method getLeader.
protected PeerId getLeader(final String raftGroupId, final boolean forceRefresh, final long timeoutMillis) {
final RouteTable routeTable = RouteTable.getInstance();
if (forceRefresh) {
final long deadline = System.currentTimeMillis() + timeoutMillis;
final StringBuilder error = new StringBuilder();
// A newly launched raft group may not have been successful in the election,
// or in the 'leader-transfer' state, it needs to be re-tried
Throwable lastCause = null;
for (; ; ) {
try {
final Status st = routeTable.refreshLeader(this.cliClientService, raftGroupId, 2000);
if (st.isOk()) {
break;
}
error.append(st.toString());
} catch (final InterruptedException e) {
ThrowUtil.throwException(e);
} catch (final Throwable t) {
lastCause = t;
error.append(t.getMessage());
}
if (System.currentTimeMillis() < deadline) {
LOG.debug("Fail to find leader, retry again, {}.", error);
error.append(", ");
try {
Thread.sleep(10);
} catch (final InterruptedException e) {
ThrowUtil.throwException(e);
}
} else {
throw lastCause != null ? new RouteTableException(error.toString(), lastCause) : new RouteTableException(error.toString());
}
}
// we need refresh configuration for membership change
final long leftTime = deadline - System.currentTimeMillis();
if (leftTime > AT_LEAST_REQUIRED_MILLIS) {
try {
RouteTable.getInstance().refreshConfiguration(this.cliClientService, raftGroupId, (int) leftTime);
} catch (final InterruptedException e) {
ThrowUtil.throwException(e);
} catch (final TimeoutException ignored) {
}
}
}
return routeTable.selectLeader(raftGroupId);
}
use of com.alipay.sofa.jraft.Status in project sofa-jraft by sofastack.
the class AbstractPlacementDriverClient method transferLeader.
@Override
public boolean transferLeader(final long regionId, final Peer peer, final boolean refreshConf) {
Requires.requireNonNull(peer, "peer");
Requires.requireNonNull(peer.getEndpoint(), "peer.endpoint");
final String raftGroupId = JRaftHelper.getJRaftGroupId(this.clusterName, regionId);
final Configuration conf = RouteTable.getInstance().getConfiguration(raftGroupId);
final Status status = this.cliService.transferLeader(raftGroupId, conf, JRaftHelper.toJRaftPeerId(peer));
if (status.isOk()) {
if (refreshConf) {
refreshRouteConfiguration(regionId);
}
return true;
}
LOG.error("Fail to [transferLeader], [regionId: {}, peer: {}], status: {}.", regionId, peer, status);
return false;
}
use of com.alipay.sofa.jraft.Status in project sofa-jraft by sofastack.
the class AbstractPlacementDriverClient method addReplica.
@Override
public boolean addReplica(final long regionId, final Peer peer, final boolean refreshConf) {
Requires.requireNonNull(peer, "peer");
Requires.requireNonNull(peer.getEndpoint(), "peer.endpoint");
final String raftGroupId = JRaftHelper.getJRaftGroupId(this.clusterName, regionId);
final Configuration conf = RouteTable.getInstance().getConfiguration(raftGroupId);
final Status status = this.cliService.addPeer(raftGroupId, conf, JRaftHelper.toJRaftPeerId(peer));
if (status.isOk()) {
if (refreshConf) {
refreshRouteConfiguration(regionId);
}
return true;
}
LOG.error("Fail to [addReplica], [regionId: {}, peer: {}], status: {}.", regionId, peer, status);
return false;
}
use of com.alipay.sofa.jraft.Status in project sofa-jraft by sofastack.
the class AbstractPlacementDriverClient method removeReplica.
@Override
public boolean removeReplica(final long regionId, final Peer peer, final boolean refreshConf) {
Requires.requireNonNull(peer, "peer");
Requires.requireNonNull(peer.getEndpoint(), "peer.endpoint");
final String raftGroupId = JRaftHelper.getJRaftGroupId(this.clusterName, regionId);
final Configuration conf = RouteTable.getInstance().getConfiguration(raftGroupId);
final Status status = this.cliService.removePeer(raftGroupId, conf, JRaftHelper.toJRaftPeerId(peer));
if (status.isOk()) {
if (refreshConf) {
refreshRouteConfiguration(regionId);
}
return true;
}
LOG.error("Fail to [removeReplica], [regionId: {}, peer: {}], status: {}.", regionId, peer, status);
return false;
}
use of com.alipay.sofa.jraft.Status in project sofa-jraft by sofastack.
the class HeartbeatSender method callAsyncWithRpc.
private <V> void callAsyncWithRpc(final Endpoint endpoint, final BaseRequest request, final HeartbeatClosure<V> closure) {
final com.alipay.sofa.jraft.rpc.InvokeContext invokeCtx = new com.alipay.sofa.jraft.rpc.InvokeContext();
invokeCtx.put(BoltRpcClient.BOLT_CTX, ExtSerializerSupports.getInvokeContext());
final InvokeCallback invokeCallback = new InvokeCallback() {
@SuppressWarnings("unchecked")
@Override
public void complete(final Object result, final Throwable err) {
if (err == null) {
final BaseResponse<?> response = (BaseResponse<?>) result;
if (response.isSuccess()) {
closure.setResult((V) response.getValue());
closure.run(Status.OK());
} else {
closure.setError(response.getError());
closure.run(new Status(-1, "RPC failed with address: %s, response: %s", endpoint, response));
}
} else {
closure.run(new Status(-1, err.getMessage()));
}
}
@Override
public Executor executor() {
return heartbeatRpcCallbackExecutor;
}
};
try {
this.rpcClient.invokeAsync(endpoint, request, invokeCtx, invokeCallback, this.heartbeatRpcTimeoutMillis);
} catch (final Throwable t) {
closure.run(new Status(-1, t.getMessage()));
}
}
Aggregations