Search in sources :

Example 56 with Status

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);
}
Also used : RegionRouteTable(com.alipay.sofa.jraft.rhea.client.RegionRouteTable) RouteTable(com.alipay.sofa.jraft.RouteTable) Status(com.alipay.sofa.jraft.Status) RouteTableException(com.alipay.sofa.jraft.rhea.errors.RouteTableException) TimeoutException(java.util.concurrent.TimeoutException)

Example 57 with Status

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;
}
Also used : Status(com.alipay.sofa.jraft.Status) Configuration(com.alipay.sofa.jraft.conf.Configuration)

Example 58 with Status

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;
}
Also used : Status(com.alipay.sofa.jraft.Status) Configuration(com.alipay.sofa.jraft.conf.Configuration)

Example 59 with Status

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;
}
Also used : Status(com.alipay.sofa.jraft.Status) Configuration(com.alipay.sofa.jraft.conf.Configuration)

Example 60 with Status

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()));
    }
}
Also used : Status(com.alipay.sofa.jraft.Status) InvokeCallback(com.alipay.sofa.jraft.rpc.InvokeCallback) BaseResponse(com.alipay.sofa.jraft.rhea.cmd.pd.BaseResponse)

Aggregations

Status (com.alipay.sofa.jraft.Status)213 Test (org.junit.Test)63 PeerId (com.alipay.sofa.jraft.entity.PeerId)55 CountDownLatch (java.util.concurrent.CountDownLatch)36 BaseKVStoreClosure (com.alipay.sofa.jraft.rhea.storage.BaseKVStoreClosure)33 Configuration (com.alipay.sofa.jraft.conf.Configuration)25 Message (com.google.protobuf.Message)24 ReadIndexClosure (com.alipay.sofa.jraft.closure.ReadIndexClosure)22 ArrayList (java.util.ArrayList)22 Node (com.alipay.sofa.jraft.Node)21 Closure (com.alipay.sofa.jraft.Closure)17 Task (com.alipay.sofa.jraft.entity.Task)17 ByteBuffer (java.nio.ByteBuffer)16 Endpoint (com.alipay.sofa.jraft.util.Endpoint)15 List (java.util.List)15 RaftException (com.alipay.sofa.jraft.error.RaftException)14 LogEntry (com.alipay.sofa.jraft.entity.LogEntry)12 LogId (com.alipay.sofa.jraft.entity.LogId)12 KVStoreClosure (com.alipay.sofa.jraft.rhea.storage.KVStoreClosure)12 JRaftException (com.alipay.sofa.jraft.error.JRaftException)11