Search in sources :

Example 6 with NamedThreadFactory

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

the class AbstractChaosTest method chaosGetTest.

@Test
public void chaosGetTest() throws Exception {
    ChaosTestCluster cluster = null;
    PeerId p1 = null;
    PeerId p2 = null;
    for (int l = 0; l < RETRIES; l++) {
        final ExecutorService executor = Executors.newCachedThreadPool(new NamedThreadFactory("chaos-test", true));
        final List<CompletableFuture<Boolean>> allFutures = new CopyOnWriteArrayList<>();
        try {
            cluster = new ChaosTestCluster(TestUtil.generatePeers(INITIAL_PEER_COUNT), getStorageType(), isAllowBatching(), isOnlyLeaderRead());
            cluster.start();
            // Before writing data, remove a node (node1) and add it back later to verify that read consistency is guaranteed.
            p1 = cluster.getRandomPeer();
            cluster.removePeer(p1);
            final RheaKVStore store = cluster.getLeaderStore();
            // warm up
            store.bGet("test_key");
            for (int i = 0; i < LOOP_1; i++) {
                final int index = i;
                executor.execute(() -> {
                    for (int j = 0; j < LOOP_2; j++) {
                        allFutures.add(store.put("test_" + index + "_" + j, VALUE));
                    }
                });
            }
            // In the process of writing data, remove one node (node2)
            p2 = cluster.getRandomPeer();
            cluster.removePeer(p2);
            // Waiting for the write to be completed
            CompletableFuture.allOf(allFutures.toArray(new CompletableFuture[0])).get(30, TimeUnit.SECONDS);
            break;
        } catch (final Exception e) {
            System.err.println("Fail to put data, try again...");
            e.printStackTrace();
            new FutureGroup<>(allFutures).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 {
        chaosGetCheckData(cluster, p2, p1);
    } finally {
        cluster.stopAll();
    }
}
Also used : RheaKVStore(com.alipay.sofa.jraft.rhea.client.RheaKVStore) CompletableFuture(java.util.concurrent.CompletableFuture) NamedThreadFactory(com.alipay.sofa.jraft.util.NamedThreadFactory) ExecutorService(java.util.concurrent.ExecutorService) PeerId(com.alipay.sofa.jraft.entity.PeerId) CopyOnWriteArrayList(java.util.concurrent.CopyOnWriteArrayList) FutureGroup(com.alipay.sofa.jraft.rhea.client.FutureGroup) Test(org.junit.Test)

Example 7 with NamedThreadFactory

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

the class DefaultFixedThreadsExecutorGroupFactory method newExecutorGroup.

@Override
public FixedThreadsExecutorGroup newExecutorGroup(final int nThreads, final String poolName, final int maxPendingTasksPerThread, final boolean useMpscQueue) {
    Requires.requireTrue(nThreads > 0, "nThreads must > 0");
    final boolean mpsc = useMpscQueue && Utils.USE_MPSC_SINGLE_THREAD_EXECUTOR;
    final SingleThreadExecutor[] children = new SingleThreadExecutor[nThreads];
    final ThreadFactory threadFactory = mpsc ? new NamedThreadFactory(poolName, true) : null;
    for (int i = 0; i < nThreads; i++) {
        if (mpsc) {
            children[i] = new MpscSingleThreadExecutor(maxPendingTasksPerThread, threadFactory);
        } else {
            children[i] = new DefaultSingleThreadExecutor(poolName, maxPendingTasksPerThread);
        }
    }
    return new DefaultFixedThreadsExecutorGroup(children);
}
Also used : NamedThreadFactory(com.alipay.sofa.jraft.util.NamedThreadFactory) ThreadFactory(java.util.concurrent.ThreadFactory) NamedThreadFactory(com.alipay.sofa.jraft.util.NamedThreadFactory)

Example 8 with NamedThreadFactory

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

the class FSMCallerImpl method init.

@Override
public boolean init(final FSMCallerOptions opts) {
    this.logManager = opts.getLogManager();
    this.fsm = opts.getFsm();
    this.closureQueue = opts.getClosureQueue();
    this.afterShutdown = opts.getAfterShutdown();
    this.node = opts.getNode();
    this.nodeMetrics = this.node.getNodeMetrics();
    this.lastAppliedIndex.set(opts.getBootstrapId().getIndex());
    notifyLastAppliedIndexUpdated(this.lastAppliedIndex.get());
    this.lastAppliedTerm = opts.getBootstrapId().getTerm();
    this.disruptor = // 
    DisruptorBuilder.<ApplyTask>newInstance().setEventFactory(// 
    new ApplyTaskFactory()).setRingBufferSize(// 
    opts.getDisruptorBufferSize()).setThreadFactory(// 
    new NamedThreadFactory("JRaft-FSMCaller-Disruptor-", true)).setProducerType(// 
    ProducerType.MULTI).setWaitStrategy(// 
    new BlockingWaitStrategy()).build();
    this.disruptor.handleEventsWith(new ApplyTaskHandler());
    this.disruptor.setDefaultExceptionHandler(new LogExceptionHandler<Object>(getClass().getSimpleName()));
    this.taskQueue = this.disruptor.start();
    if (this.nodeMetrics.getMetricRegistry() != null) {
        this.nodeMetrics.getMetricRegistry().register("jraft-fsm-caller-disruptor", new DisruptorMetricSet(this.taskQueue));
    }
    this.error = new RaftException(EnumOutter.ErrorType.ERROR_TYPE_NONE);
    LOG.info("Starts FSMCaller successfully.");
    return true;
}
Also used : RaftException(com.alipay.sofa.jraft.error.RaftException) BlockingWaitStrategy(com.lmax.disruptor.BlockingWaitStrategy) NamedThreadFactory(com.alipay.sofa.jraft.util.NamedThreadFactory) DisruptorMetricSet(com.alipay.sofa.jraft.util.DisruptorMetricSet)

Aggregations

NamedThreadFactory (com.alipay.sofa.jraft.util.NamedThreadFactory)8 DisruptorMetricSet (com.alipay.sofa.jraft.util.DisruptorMetricSet)4 Status (com.alipay.sofa.jraft.Status)3 PeerId (com.alipay.sofa.jraft.entity.PeerId)3 BlockingWaitStrategy (com.lmax.disruptor.BlockingWaitStrategy)3 CopyOnWriteArrayList (java.util.concurrent.CopyOnWriteArrayList)3 Configuration (com.alipay.sofa.jraft.conf.Configuration)2 ConfigurationEntry (com.alipay.sofa.jraft.conf.ConfigurationEntry)2 ConfigurationManager (com.alipay.sofa.jraft.conf.ConfigurationManager)2 LogId (com.alipay.sofa.jraft.entity.LogId)2 RaftException (com.alipay.sofa.jraft.error.RaftException)2 RheaKVStore (com.alipay.sofa.jraft.rhea.client.RheaKVStore)2 FSMCaller (com.alipay.sofa.jraft.FSMCaller)1 NodeMetrics (com.alipay.sofa.jraft.core.NodeMetrics)1 EntryType (com.alipay.sofa.jraft.entity.EnumOutter.EntryType)1 ErrorType (com.alipay.sofa.jraft.entity.EnumOutter.ErrorType)1 LogEntry (com.alipay.sofa.jraft.entity.LogEntry)1 NodeId (com.alipay.sofa.jraft.entity.NodeId)1 SnapshotMeta (com.alipay.sofa.jraft.entity.RaftOutter.SnapshotMeta)1 LogEntryCorruptedException (com.alipay.sofa.jraft.error.LogEntryCorruptedException)1