Search in sources :

Example 1 with RouteTable

use of io.dingodb.raft.RouteTable in project dingo by dingodb.

the class AbstractPlacementDriverClient method getLeaderForRaftGroupId.

protected PeerId getLeaderForRaftGroupId(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(io.dingodb.store.row.client.RegionRouteTable) RouteTable(io.dingodb.raft.RouteTable) Status(io.dingodb.raft.Status) RouteTableException(io.dingodb.store.row.errors.RouteTableException) TimeoutException(java.util.concurrent.TimeoutException)

Example 2 with RouteTable

use of io.dingodb.raft.RouteTable in project dingo by dingodb.

the class AbstractPlacementDriverClient method getLuckyPeer.

@Override
public Endpoint getLuckyPeer(final String regionId, final boolean forceRefresh, final long timeoutMillis, final Endpoint unExpect) {
    final String raftGroupId = JRaftHelper.getJRaftGroupId(this.clusterName, regionId);
    final RouteTable routeTable = RouteTable.getInstance();
    if (forceRefresh) {
        final long deadline = System.currentTimeMillis() + timeoutMillis;
        final StringBuilder error = new StringBuilder();
        // or in the 'leader-transfer' state, it needs to be re-tried
        for (; ; ) {
            try {
                final Status st = routeTable.refreshConfiguration(this.cliClientService, raftGroupId, 5000);
                if (st.isOk()) {
                    break;
                }
                error.append(st.toString());
            } catch (final InterruptedException e) {
                ThrowUtil.throwException(e);
            } catch (final TimeoutException e) {
                error.append(e.getMessage());
            }
            if (System.currentTimeMillis() < deadline) {
                LOG.debug("Fail to get peers, retry again, {}.", error);
                error.append(", ");
                try {
                    Thread.sleep(5);
                } catch (final InterruptedException e) {
                    ThrowUtil.throwException(e);
                }
            } else {
                throw new RouteTableException(error.toString());
            }
        }
    }
    final Configuration configs = routeTable.getConfiguration(raftGroupId);
    if (configs == null) {
        throw new RouteTableException("empty configs in group: " + raftGroupId);
    }
    final List<PeerId> peerList = configs.getPeers();
    if (peerList == null || peerList.isEmpty()) {
        throw new RouteTableException("empty peers in group: " + raftGroupId);
    }
    final int size = peerList.size();
    if (size == 1) {
        return peerList.get(0).getEndpoint();
    }
    final RoundRobinLoadBalancer balancer = RoundRobinLoadBalancer.getInstance(regionId);
    for (int i = 0; i < size; i++) {
        final PeerId candidate = balancer.select(peerList);
        final Endpoint luckyOne = candidate.getEndpoint();
        if (!luckyOne.equals(unExpect)) {
            return luckyOne;
        }
    }
    throw new RouteTableException("have no choice in group(peers): " + raftGroupId);
}
Also used : Status(io.dingodb.raft.Status) Configuration(io.dingodb.raft.conf.Configuration) RoundRobinLoadBalancer(io.dingodb.store.row.client.RoundRobinLoadBalancer) RouteTableException(io.dingodb.store.row.errors.RouteTableException) Endpoint(io.dingodb.raft.util.Endpoint) RegionRouteTable(io.dingodb.store.row.client.RegionRouteTable) RouteTable(io.dingodb.raft.RouteTable) Endpoint(io.dingodb.raft.util.Endpoint) TimeoutException(java.util.concurrent.TimeoutException) PeerId(io.dingodb.raft.entity.PeerId)

Aggregations

RouteTable (io.dingodb.raft.RouteTable)2 Status (io.dingodb.raft.Status)2 RegionRouteTable (io.dingodb.store.row.client.RegionRouteTable)2 RouteTableException (io.dingodb.store.row.errors.RouteTableException)2 TimeoutException (java.util.concurrent.TimeoutException)2 Configuration (io.dingodb.raft.conf.Configuration)1 PeerId (io.dingodb.raft.entity.PeerId)1 Endpoint (io.dingodb.raft.util.Endpoint)1 RoundRobinLoadBalancer (io.dingodb.store.row.client.RoundRobinLoadBalancer)1