Search in sources :

Example 1 with CliClientServiceImpl

use of com.alipay.sofa.jraft.rpc.impl.cli.CliClientServiceImpl in project sofa-jraft by sofastack.

the class CounterClient method main.

public static void main(final String[] args) throws Exception {
    if (args.length != 2) {
        System.out.println("Usage : java com.alipay.sofa.jraft.example.counter.CounterClient {groupId} {conf}");
        System.out.println("Example: java com.alipay.sofa.jraft.example.counter.CounterClient counter 127.0.0.1:8081,127.0.0.1:8082,127.0.0.1:8083");
        System.exit(1);
    }
    final String groupId = args[0];
    final String confStr = args[1];
    CounterGrpcHelper.initGRpc();
    final Configuration conf = new Configuration();
    if (!conf.parse(confStr)) {
        throw new IllegalArgumentException("Fail to parse conf:" + confStr);
    }
    RouteTable.getInstance().updateConfiguration(groupId, conf);
    final CliClientServiceImpl cliClientService = new CliClientServiceImpl();
    cliClientService.init(new CliOptions());
    if (!RouteTable.getInstance().refreshLeader(cliClientService, groupId, 1000).isOk()) {
        throw new IllegalStateException("Refresh leader failed");
    }
    final PeerId leader = RouteTable.getInstance().selectLeader(groupId);
    System.out.println("Leader is " + leader);
    final int n = 1000;
    final CountDownLatch latch = new CountDownLatch(n);
    final long start = System.currentTimeMillis();
    for (int i = 0; i < n; i++) {
        incrementAndGet(cliClientService, leader, i, latch);
    }
    latch.await();
    System.out.println(n + " ops, cost : " + (System.currentTimeMillis() - start) + " ms.");
    System.exit(0);
}
Also used : Configuration(com.alipay.sofa.jraft.conf.Configuration) CliClientServiceImpl(com.alipay.sofa.jraft.rpc.impl.cli.CliClientServiceImpl) CountDownLatch(java.util.concurrent.CountDownLatch) CliOptions(com.alipay.sofa.jraft.option.CliOptions) PeerId(com.alipay.sofa.jraft.entity.PeerId)

Example 2 with CliClientServiceImpl

use of com.alipay.sofa.jraft.rpc.impl.cli.CliClientServiceImpl in project jdchain-core by blockchain-jd-com.

the class ParticipantManagerService4Raft method shuffleInvoke.

private RpcResponse shuffleInvoke(ParticipantContext context, List<NodeSettings> origConsensusNodes, Object request) {
    Collections.shuffle(origConsensusNodes);
    for (NodeSettings nodeSettings : origConsensusNodes) {
        RaftNodeSettings raftNodeSettings = (RaftNodeSettings) nodeSettings;
        Endpoint remoteEndpoint = new Endpoint(raftNodeSettings.getNetworkAddress().getHost(), raftNodeSettings.getNetworkAddress().getPort());
        CliClientServiceImpl cliClientService = createRpcClient(context);
        if (cliClientService.connect(remoteEndpoint)) {
            return invoke(context, remoteEndpoint, request);
        }
    }
    return RpcResponse.fail(-1, "not found connected nodes");
}
Also used : NodeSettings(com.jd.blockchain.consensus.NodeSettings) RaftNodeSettings(com.jd.blockchain.consensus.raft.settings.RaftNodeSettings) RaftNodeSettings(com.jd.blockchain.consensus.raft.settings.RaftNodeSettings) Endpoint(com.alipay.sofa.jraft.util.Endpoint) CliClientServiceImpl(com.alipay.sofa.jraft.rpc.impl.cli.CliClientServiceImpl)

Example 3 with CliClientServiceImpl

use of com.alipay.sofa.jraft.rpc.impl.cli.CliClientServiceImpl 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)

Example 4 with CliClientServiceImpl

use of com.alipay.sofa.jraft.rpc.impl.cli.CliClientServiceImpl in project sofa-jraft by sofastack.

the class RouteTableTest method setup.

@Before
public void setup() throws Exception {
    cliClientService = new CliClientServiceImpl();
    cliClientService.init(new CliOptions());
    this.dataPath = TestUtils.mkTempDir();
    FileUtils.forceMkdir(new File(this.dataPath));
    assertEquals(NodeImpl.GLOBAL_NUM_NODES.get(), 0);
    final List<PeerId> peers = TestUtils.generatePeers(3);
    cluster = new TestCluster(groupId, dataPath, peers);
    for (final PeerId peer : peers) {
        cluster.start(peer.getEndpoint());
    }
    cluster.waitLeader();
}
Also used : CliClientServiceImpl(com.alipay.sofa.jraft.rpc.impl.cli.CliClientServiceImpl) TestCluster(com.alipay.sofa.jraft.core.TestCluster) File(java.io.File) CliOptions(com.alipay.sofa.jraft.option.CliOptions) PeerId(com.alipay.sofa.jraft.entity.PeerId) Before(org.junit.Before)

Example 5 with CliClientServiceImpl

use of com.alipay.sofa.jraft.rpc.impl.cli.CliClientServiceImpl in project sofa-jraft by sofastack.

the class CliServiceImpl method init.

@Override
public synchronized boolean init(final CliOptions opts) {
    Requires.requireNonNull(opts, "Null cli options");
    if (this.cliClientService != null) {
        return true;
    }
    this.cliOptions = opts;
    this.cliClientService = new CliClientServiceImpl();
    return this.cliClientService.init(this.cliOptions);
}
Also used : CliClientServiceImpl(com.alipay.sofa.jraft.rpc.impl.cli.CliClientServiceImpl)

Aggregations

CliClientServiceImpl (com.alipay.sofa.jraft.rpc.impl.cli.CliClientServiceImpl)7 CliOptions (com.alipay.sofa.jraft.option.CliOptions)4 PeerId (com.alipay.sofa.jraft.entity.PeerId)3 Endpoint (com.alipay.sofa.jraft.util.Endpoint)2 RouteTable (com.alipay.sofa.jraft.RouteTable)1 Status (com.alipay.sofa.jraft.Status)1 Configuration (com.alipay.sofa.jraft.conf.Configuration)1 TestCluster (com.alipay.sofa.jraft.core.TestCluster)1 NodeSettings (com.jd.blockchain.consensus.NodeSettings)1 RaftConsensusSettings (com.jd.blockchain.consensus.raft.settings.RaftConsensusSettings)1 RaftNetworkSettings (com.jd.blockchain.consensus.raft.settings.RaftNetworkSettings)1 RaftNodeSettings (com.jd.blockchain.consensus.raft.settings.RaftNodeSettings)1 File (java.io.File)1 CountDownLatch (java.util.concurrent.CountDownLatch)1 CyclicBarrier (java.util.concurrent.CyclicBarrier)1 Before (org.junit.Before)1 SSLSecurity (utils.net.SSLSecurity)1