Search in sources :

Example 1 with RouteTable

use of com.alipay.sofa.jraft.RouteTable 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 2 with RouteTable

use of com.alipay.sofa.jraft.RouteTable in project nacos by alibaba.

the class JRaftServer method refreshRouteTable.

void refreshRouteTable(String group) {
    if (isShutdown) {
        return;
    }
    final String groupName = group;
    Status status = null;
    try {
        RouteTable instance = RouteTable.getInstance();
        Configuration oldConf = instance.getConfiguration(groupName);
        String oldLeader = Optional.ofNullable(instance.selectLeader(groupName)).orElse(PeerId.emptyPeer()).getEndpoint().toString();
        // fix issue #3661  https://github.com/alibaba/nacos/issues/3661
        status = instance.refreshLeader(this.cliClientService, groupName, rpcRequestTimeoutMs);
        if (!status.isOk()) {
            Loggers.RAFT.error("Fail to refresh leader for group : {}, status is : {}", groupName, status);
        }
        status = instance.refreshConfiguration(this.cliClientService, groupName, rpcRequestTimeoutMs);
        if (!status.isOk()) {
            Loggers.RAFT.error("Fail to refresh route configuration for group : {}, status is : {}", groupName, status);
        }
    } catch (Exception e) {
        Loggers.RAFT.error("Fail to refresh raft metadata info for group : {}, error is : {}", groupName, e);
    }
}
Also used : Status(com.alipay.sofa.jraft.Status) RouteTable(com.alipay.sofa.jraft.RouteTable) Configuration(com.alipay.sofa.jraft.conf.Configuration) JRaftException(com.alibaba.nacos.core.distributed.raft.exception.JRaftException) ConsistencyException(com.alibaba.nacos.consistency.exception.ConsistencyException) DuplicateRaftGroupException(com.alibaba.nacos.core.distributed.raft.exception.DuplicateRaftGroupException) NoLeaderException(com.alibaba.nacos.core.distributed.raft.exception.NoLeaderException) NoSuchRaftGroupException(com.alibaba.nacos.core.distributed.raft.exception.NoSuchRaftGroupException)

Example 3 with RouteTable

use of com.alipay.sofa.jraft.RouteTable 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)

Example 4 with RouteTable

use of com.alipay.sofa.jraft.RouteTable in project sofa-jraft by sofastack.

the class AtomicClientTest method main.

public static void main(String[] args) throws Exception {
    final RouteTable table = RouteTable.getInstance();
    table.updateConfiguration("atomic_0", JRaftUtils.getConfiguration("127.0.0.1:8609,127.0.0.1:8610,127.0.0.1:8611"));
    final CliClientServiceImpl cliClientService = new CliClientServiceImpl();
    cliClientService.init(new CliOptions());
    final Status st = table.refreshLeader(cliClientService, "atomic_0", 10000);
    System.out.println(st);
    final AtomicClient cli = new AtomicClient("atomic", JRaftUtils.getConfiguration("localhost:8610"));
    final PeerId leader = table.selectLeader("atomic_0");
    cli.start();
    final int threads = 30;
    final int count = 10000;
    final CyclicBarrier barrier = new CyclicBarrier(threads + 1);
    for (int t = 0; t < threads; t++) {
        new Thread() {

            @Override
            public void run() {
                long sum = 0;
                try {
                    barrier.await();
                    final PeerId peer = new PeerId("localhost", 8611);
                    for (int i = 0; i < count; i++) {
                        sum += cli.get(leader, "a", true, false);
                    // sum += cli.addAndGet(leader, "a", i);
                    }
                    barrier.await();
                } catch (final Exception e) {
                    e.printStackTrace();
                } finally {
                    System.out.println("sum=" + sum);
                }
            }
        }.start();
    }
    final long start = System.currentTimeMillis();
    barrier.await();
    barrier.await();
    final long cost = System.currentTimeMillis() - start;
    final long tps = Math.round(threads * count * 1000.0 / cost);
    System.out.println("tps=" + tps + ",cost=" + cost + " ms.");
    cli.shutdown();
}
Also used : RouteTable(com.alipay.sofa.jraft.RouteTable) Status(com.alipay.sofa.jraft.Status) CliClientServiceImpl(com.alipay.sofa.jraft.rpc.impl.cli.CliClientServiceImpl) CliOptions(com.alipay.sofa.jraft.option.CliOptions) PeerId(com.alipay.sofa.jraft.entity.PeerId) CyclicBarrier(java.util.concurrent.CyclicBarrier)

Aggregations

RouteTable (com.alipay.sofa.jraft.RouteTable)4 Status (com.alipay.sofa.jraft.Status)4 Configuration (com.alipay.sofa.jraft.conf.Configuration)2 PeerId (com.alipay.sofa.jraft.entity.PeerId)2 RegionRouteTable (com.alipay.sofa.jraft.rhea.client.RegionRouteTable)2 RouteTableException (com.alipay.sofa.jraft.rhea.errors.RouteTableException)2 TimeoutException (java.util.concurrent.TimeoutException)2 ConsistencyException (com.alibaba.nacos.consistency.exception.ConsistencyException)1 DuplicateRaftGroupException (com.alibaba.nacos.core.distributed.raft.exception.DuplicateRaftGroupException)1 JRaftException (com.alibaba.nacos.core.distributed.raft.exception.JRaftException)1 NoLeaderException (com.alibaba.nacos.core.distributed.raft.exception.NoLeaderException)1 NoSuchRaftGroupException (com.alibaba.nacos.core.distributed.raft.exception.NoSuchRaftGroupException)1 CliOptions (com.alipay.sofa.jraft.option.CliOptions)1 RoundRobinLoadBalancer (com.alipay.sofa.jraft.rhea.client.RoundRobinLoadBalancer)1 CliClientServiceImpl (com.alipay.sofa.jraft.rpc.impl.cli.CliClientServiceImpl)1 Endpoint (com.alipay.sofa.jraft.util.Endpoint)1 CyclicBarrier (java.util.concurrent.CyclicBarrier)1