Search in sources :

Example 1 with CliOptions

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

the class AbstractChaosTest method chaosSplittingTest.

@Test
public void chaosSplittingTest() {
    final List<PeerId> peerIds = TestUtil.generatePeers(INITIAL_PEER_COUNT);
    final CliOptions opts = new CliOptions();
    opts.setTimeoutMs(30000);
    final RheaKVCliService cliService = RheaKVServiceFactory.createAndInitRheaKVCliService(opts);
    final long regionId = Constants.DEFAULT_REGION_ID;
    final long newRegionId = 2;
    final String groupId = JRaftHelper.getJRaftGroupId(ChaosTestCluster.CLUSTER_NAME, regionId);
    final Configuration conf = new Configuration(peerIds);
    ChaosTestCluster cluster = null;
    for (int l = 0; l < RETRIES; l++) {
        final ExecutorService executor = Executors.newCachedThreadPool(new NamedThreadFactory("chaos-splitting-test", true));
        final List<Future<?>> allFutures = new CopyOnWriteArrayList<>();
        try {
            cluster = new ChaosTestCluster(peerIds, getStorageType(), isAllowBatching(), isOnlyLeaderRead());
            cluster.start();
            final RheaKVStore store = cluster.getLeaderStore();
            // for least keys on split
            for (int j = 0; j < LOOP_2; j++) {
                store.bPut(j + "_split_", VALUE);
            }
            for (int i = 0; i < LOOP_1; i++) {
                final int index = i;
                final Future<?> f = executor.submit(() -> {
                    for (int j = 0; j < LOOP_2; j++) {
                        store.bPut(index + "_split_test_" + j, VALUE);
                    }
                });
                allFutures.add(f);
            }
            final Status st = cliService.rangeSplit(regionId, newRegionId, groupId, conf);
            if (!st.isOk()) {
                System.err.println("Status:" + st);
                throw new RuntimeException(st.toString());
            }
            // wait for all writes finished
            for (final Future<?> f : allFutures) {
                f.get(30, TimeUnit.SECONDS);
            }
            break;
        } catch (final Exception e) {
            System.err.println("Fail to put data, try again...");
            e.printStackTrace();
            for (final Future<?> f : allFutures) {
                f.cancel(true);
            }
            if (cluster != null) {
                cluster.stopAll();
            }
            cluster = null;
        } finally {
            ExecutorServiceHelper.shutdownAndAwaitTermination(executor);
        }
    }
    if (cluster == null) {
        throw new RuntimeException("fail to put data, can not check data");
    }
    try {
        chaosSplittingCheckData(cluster);
    } finally {
        cluster.stopAll();
    }
}
Also used : Status(com.alipay.sofa.jraft.Status) RheaKVStore(com.alipay.sofa.jraft.rhea.client.RheaKVStore) RheaKVCliService(com.alipay.sofa.jraft.rhea.client.RheaKVCliService) Configuration(com.alipay.sofa.jraft.conf.Configuration) NamedThreadFactory(com.alipay.sofa.jraft.util.NamedThreadFactory) CliOptions(com.alipay.sofa.jraft.option.CliOptions) ExecutorService(java.util.concurrent.ExecutorService) CompletableFuture(java.util.concurrent.CompletableFuture) Future(java.util.concurrent.Future) PeerId(com.alipay.sofa.jraft.entity.PeerId) CopyOnWriteArrayList(java.util.concurrent.CopyOnWriteArrayList) Test(org.junit.Test)

Example 2 with CliOptions

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

the class AtomicClient method start.

public void start() throws InterruptedException, TimeoutException {
    cliOptions = new CliOptions();
    this.cliClientService.init(cliOptions);
    this.rpcClient = this.cliClientService.getRpcClient();
    if (conf != null) {
        final Set<PeerId> peers = conf.getPeerSet();
        for (final PeerId peer : peers) {
            try {
                final BooleanCommand cmd = (BooleanCommand) this.rpcClient.invokeSync(peer.getEndpoint(), new GetSlotsCommand(), cliOptions.getRpcDefaultTimeout());
                if (cmd instanceof SlotsResponseCommand) {
                    groups = ((SlotsResponseCommand) cmd).getMap();
                    break;
                } else {
                    LOG.warn("Fail to get slots from peer {}, error: {}", peer, cmd.getErrorMsg());
                }
            } catch (final Throwable t) {
                LOG.warn("Fail to get slots from peer {}, error: {}", peer, t.getMessage());
            // continue;
            }
        }
        if (groups == null || groups.isEmpty()) {
            throw new IllegalArgumentException("Can't get slots from any peers");
        } else {
            LOG.info("All groups  is {}", groups);
        }
        for (final String groupId : groups.values()) {
            RouteTable.getInstance().updateConfiguration(groupId, conf);
            refreshLeader(groupId);
            refreshConf(groupId);
        }
    }
}
Also used : GetSlotsCommand(com.alipay.sofa.jraft.test.atomic.command.GetSlotsCommand) BooleanCommand(com.alipay.sofa.jraft.test.atomic.command.BooleanCommand) SlotsResponseCommand(com.alipay.sofa.jraft.test.atomic.command.SlotsResponseCommand) CliOptions(com.alipay.sofa.jraft.option.CliOptions) PeerId(com.alipay.sofa.jraft.entity.PeerId)

Example 3 with CliOptions

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

the class AbstractPlacementDriverClient method initCli.

protected void initCli(CliOptions cliOpts) {
    if (cliOpts == null) {
        cliOpts = new CliOptions();
        cliOpts.setTimeoutMs(5000);
        cliOpts.setMaxRetry(3);
    }
    this.cliService = RaftServiceFactory.createAndInitCliService(cliOpts);
    this.cliClientService = ((CliServiceImpl) this.cliService).getCliClientService();
    Requires.requireNonNull(this.cliClientService, "cliClientService");
    this.rpcClient = ((AbstractClientService) this.cliClientService).getRpcClient();
}
Also used : CliOptions(com.alipay.sofa.jraft.option.CliOptions)

Example 4 with CliOptions

use of com.alipay.sofa.jraft.option.CliOptions 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 5 with CliOptions

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

the class JRaftServer method init.

void init(RaftConfig config) {
    this.raftConfig = config;
    this.serializer = SerializeFactory.getDefault();
    Loggers.RAFT.info("Initializes the Raft protocol, raft-config info : {}", config);
    RaftExecutor.init(config);
    final String self = config.getSelfMember();
    String[] info = InternetAddressUtil.splitIPPortStr(self);
    selfIp = info[0];
    selfPort = Integer.parseInt(info[1]);
    localPeerId = PeerId.parsePeer(self);
    nodeOptions = new NodeOptions();
    // Set the election timeout time. The default is 5 seconds.
    int electionTimeout = Math.max(ConvertUtils.toInt(config.getVal(RaftSysConstants.RAFT_ELECTION_TIMEOUT_MS), RaftSysConstants.DEFAULT_ELECTION_TIMEOUT), RaftSysConstants.DEFAULT_ELECTION_TIMEOUT);
    rpcRequestTimeoutMs = ConvertUtils.toInt(raftConfig.getVal(RaftSysConstants.RAFT_RPC_REQUEST_TIMEOUT_MS), RaftSysConstants.DEFAULT_RAFT_RPC_REQUEST_TIMEOUT_MS);
    nodeOptions.setSharedElectionTimer(true);
    nodeOptions.setSharedVoteTimer(true);
    nodeOptions.setSharedStepDownTimer(true);
    nodeOptions.setSharedSnapshotTimer(true);
    nodeOptions.setElectionTimeoutMs(electionTimeout);
    RaftOptions raftOptions = RaftOptionsBuilder.initRaftOptions(raftConfig);
    nodeOptions.setRaftOptions(raftOptions);
    // open jraft node metrics record function
    nodeOptions.setEnableMetrics(true);
    CliOptions cliOptions = new CliOptions();
    this.cliService = RaftServiceFactory.createAndInitCliService(cliOptions);
    this.cliClientService = (CliClientServiceImpl) ((CliServiceImpl) this.cliService).getCliClientService();
}
Also used : RaftOptions(com.alipay.sofa.jraft.option.RaftOptions) CliServiceImpl(com.alipay.sofa.jraft.core.CliServiceImpl) NodeOptions(com.alipay.sofa.jraft.option.NodeOptions) Endpoint(com.alipay.sofa.jraft.util.Endpoint) CliOptions(com.alipay.sofa.jraft.option.CliOptions)

Aggregations

CliOptions (com.alipay.sofa.jraft.option.CliOptions)13 PeerId (com.alipay.sofa.jraft.entity.PeerId)6 Configuration (com.alipay.sofa.jraft.conf.Configuration)5 CliServiceImpl (com.alipay.sofa.jraft.core.CliServiceImpl)4 CliClientServiceImpl (com.alipay.sofa.jraft.rpc.impl.cli.CliClientServiceImpl)4 Status (com.alipay.sofa.jraft.Status)3 NodeOptions (com.alipay.sofa.jraft.option.NodeOptions)2 RaftOptions (com.alipay.sofa.jraft.option.RaftOptions)2 RheaKVCliService (com.alipay.sofa.jraft.rhea.client.RheaKVCliService)2 RheaKVStore (com.alipay.sofa.jraft.rhea.client.RheaKVStore)2 Endpoint (com.alipay.sofa.jraft.util.Endpoint)2 File (java.io.File)2 Before (org.junit.Before)2 Test (org.junit.Test)2 RouteTable (com.alipay.sofa.jraft.RouteTable)1 TestCluster (com.alipay.sofa.jraft.core.TestCluster)1 CliClientService (com.alipay.sofa.jraft.rpc.CliClientService)1 BooleanCommand (com.alipay.sofa.jraft.test.atomic.command.BooleanCommand)1 GetSlotsCommand (com.alipay.sofa.jraft.test.atomic.command.GetSlotsCommand)1 SlotsResponseCommand (com.alipay.sofa.jraft.test.atomic.command.SlotsResponseCommand)1