Search in sources :

Example 1 with RoundRobinLoadBalancer

use of com.alipay.sofa.jraft.rhea.client.RoundRobinLoadBalancer in project sofa-jraft by sofastack.

the class AbstractPlacementDriverClient method getLuckyPeer.

@Override
public Endpoint getLuckyPeer(final long 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(com.alipay.sofa.jraft.Status) Configuration(com.alipay.sofa.jraft.conf.Configuration) RoundRobinLoadBalancer(com.alipay.sofa.jraft.rhea.client.RoundRobinLoadBalancer) RouteTableException(com.alipay.sofa.jraft.rhea.errors.RouteTableException) Endpoint(com.alipay.sofa.jraft.util.Endpoint) RegionRouteTable(com.alipay.sofa.jraft.rhea.client.RegionRouteTable) RouteTable(com.alipay.sofa.jraft.RouteTable) Endpoint(com.alipay.sofa.jraft.util.Endpoint) TimeoutException(java.util.concurrent.TimeoutException) PeerId(com.alipay.sofa.jraft.entity.PeerId)

Aggregations

RouteTable (com.alipay.sofa.jraft.RouteTable)1 Status (com.alipay.sofa.jraft.Status)1 Configuration (com.alipay.sofa.jraft.conf.Configuration)1 PeerId (com.alipay.sofa.jraft.entity.PeerId)1 RegionRouteTable (com.alipay.sofa.jraft.rhea.client.RegionRouteTable)1 RoundRobinLoadBalancer (com.alipay.sofa.jraft.rhea.client.RoundRobinLoadBalancer)1 RouteTableException (com.alipay.sofa.jraft.rhea.errors.RouteTableException)1 Endpoint (com.alipay.sofa.jraft.util.Endpoint)1 TimeoutException (java.util.concurrent.TimeoutException)1