Search in sources :

Example 1 with JoinableClosure

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

the class NodeTest method testNodeTaskOverload.

@Test
public void testNodeTaskOverload() throws Exception {
    final Endpoint addr = new Endpoint(TestUtils.getMyIp(), TestUtils.INIT_PORT);
    final PeerId peer = new PeerId(addr, 0);
    NodeManager.getInstance().addAddress(addr);
    final NodeOptions nodeOptions = createNodeOptionsWithSharedTimer();
    final RaftOptions raftOptions = new RaftOptions();
    raftOptions.setDisruptorBufferSize(2);
    nodeOptions.setRaftOptions(raftOptions);
    final MockStateMachine fsm = new MockStateMachine(addr);
    nodeOptions.setFsm(fsm);
    nodeOptions.setLogUri(this.dataPath + File.separator + "log");
    nodeOptions.setRaftMetaUri(this.dataPath + File.separator + "meta");
    nodeOptions.setSnapshotUri(this.dataPath + File.separator + "snapshot");
    nodeOptions.setInitialConf(new Configuration(Collections.singletonList(peer)));
    final Node node = new NodeImpl("unittest", peer);
    assertTrue(node.init(nodeOptions));
    assertEquals(1, node.listPeers().size());
    assertTrue(node.listPeers().contains(peer));
    while (!node.isLeader()) {
        ;
    }
    final List<Task> tasks = new ArrayList<>();
    final AtomicInteger c = new AtomicInteger(0);
    for (int i = 0; i < 10; i++) {
        final ByteBuffer data = ByteBuffer.wrap(("hello" + i).getBytes());
        final Task task = new Task(data, new JoinableClosure(status -> {
            System.out.println(status);
            if (!status.isOk()) {
                assertTrue(status.getRaftError() == RaftError.EBUSY || status.getRaftError() == RaftError.EPERM);
            }
            c.incrementAndGet();
        }));
        node.apply(task);
        tasks.add(task);
    }
    try {
        Task.joinAll(tasks, TimeUnit.SECONDS.toMillis(30));
        assertEquals(10, c.get());
    } finally {
        node.shutdown();
        node.join();
    }
}
Also used : Arrays(java.util.Arrays) RaftOptions(com.alipay.sofa.jraft.option.RaftOptions) Assert.assertNotSame(org.junit.Assert.assertNotSame) LoggerFactory(org.slf4j.LoggerFactory) ByteBuffer(java.nio.ByteBuffer) Future(java.util.concurrent.Future) Vector(java.util.Vector) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) After(org.junit.After) TestUtils(com.alipay.sofa.jraft.test.TestUtils) Endpoint(com.alipay.sofa.jraft.util.Endpoint) Assert.fail(org.junit.Assert.fail) AfterClass(org.junit.AfterClass) PeerId(com.alipay.sofa.jraft.entity.PeerId) Configuration(com.alipay.sofa.jraft.conf.Configuration) StateMachine(com.alipay.sofa.jraft.StateMachine) SynchronizedClosure(com.alipay.sofa.jraft.closure.SynchronizedClosure) RpcServer(com.alipay.sofa.jraft.rpc.RpcServer) BootstrapOptions(com.alipay.sofa.jraft.option.BootstrapOptions) Set(java.util.Set) ReadIndexClosure(com.alipay.sofa.jraft.closure.ReadIndexClosure) SnapshotThrottle(com.alipay.sofa.jraft.storage.SnapshotThrottle) JRaftUtils(com.alipay.sofa.jraft.JRaftUtils) CountDownLatch(java.util.concurrent.CountDownLatch) List(java.util.List) RaftGroupService(com.alipay.sofa.jraft.RaftGroupService) Assert.assertFalse(org.junit.Assert.assertFalse) RaftException(com.alipay.sofa.jraft.error.RaftException) LogIndexOutOfBoundsException(com.alipay.sofa.jraft.error.LogIndexOutOfBoundsException) ThroughputSnapshotThrottle(com.alipay.sofa.jraft.storage.snapshot.ThroughputSnapshotThrottle) UserLog(com.alipay.sofa.jraft.entity.UserLog) BeforeClass(org.junit.BeforeClass) JoinableClosure(com.alipay.sofa.jraft.closure.JoinableClosure) Bits(com.alipay.sofa.jraft.util.Bits) LogNotFoundException(com.alipay.sofa.jraft.error.LogNotFoundException) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) Utils(com.alipay.sofa.jraft.util.Utils) EnumOutter(com.alipay.sofa.jraft.entity.EnumOutter) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) Assert.assertSame(org.junit.Assert.assertSame) SizeUnit(org.rocksdb.util.SizeUnit) StorageOptionsFactory(com.alipay.sofa.jraft.util.StorageOptionsFactory) NodeManager(com.alipay.sofa.jraft.NodeManager) TestName(org.junit.rules.TestName) ThreadLocalRandom(java.util.concurrent.ThreadLocalRandom) Assert.assertArrayEquals(org.junit.Assert.assertArrayEquals) RaftError(com.alipay.sofa.jraft.error.RaftError) TaskClosure(com.alipay.sofa.jraft.closure.TaskClosure) Iterator(com.alipay.sofa.jraft.Iterator) LinkedHashSet(java.util.LinkedHashSet) Before(org.junit.Before) Logger(org.slf4j.Logger) Assert.assertNotNull(org.junit.Assert.assertNotNull) RaftRpcServerFactory(com.alipay.sofa.jraft.rpc.RaftRpcServerFactory) Assert.assertTrue(org.junit.Assert.assertTrue) FileUtils(org.apache.commons.io.FileUtils) Test(org.junit.Test) Status(com.alipay.sofa.jraft.Status) NodeOptions(com.alipay.sofa.jraft.option.NodeOptions) File(java.io.File) TimeUnit(java.util.concurrent.TimeUnit) Rule(org.junit.Rule) Task(com.alipay.sofa.jraft.entity.Task) Assert.assertNull(org.junit.Assert.assertNull) Node(com.alipay.sofa.jraft.Node) ConsoleReporter(com.codahale.metrics.ConsoleReporter) SnapshotReader(com.alipay.sofa.jraft.storage.snapshot.SnapshotReader) Assert(org.junit.Assert) RocksDBLogStorage(com.alipay.sofa.jraft.storage.impl.RocksDBLogStorage) Collections(java.util.Collections) Assert.assertEquals(org.junit.Assert.assertEquals) RaftOptions(com.alipay.sofa.jraft.option.RaftOptions) Task(com.alipay.sofa.jraft.entity.Task) Configuration(com.alipay.sofa.jraft.conf.Configuration) Node(com.alipay.sofa.jraft.Node) ArrayList(java.util.ArrayList) NodeOptions(com.alipay.sofa.jraft.option.NodeOptions) ByteBuffer(java.nio.ByteBuffer) Endpoint(com.alipay.sofa.jraft.util.Endpoint) Endpoint(com.alipay.sofa.jraft.util.Endpoint) JoinableClosure(com.alipay.sofa.jraft.closure.JoinableClosure) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) PeerId(com.alipay.sofa.jraft.entity.PeerId) Test(org.junit.Test)

Example 2 with JoinableClosure

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

the class Task method join.

/**
 * Waiting for the task to complete, to note that throughput may be reduced,
 * which is generally not recommended.
 *
 * @return done closure
 * @throws InterruptedException if the current thread is interrupted while waiting
 * @since 1.3.1
 */
public Closure join() throws InterruptedException {
    final JoinableClosure joinable = castToJoinalbe(this.done);
    joinable.join();
    return joinable.getClosure();
}
Also used : JoinableClosure(com.alipay.sofa.jraft.closure.JoinableClosure)

Example 3 with JoinableClosure

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

the class Task method join.

/**
 * Waiting for the task to complete with a timeout millis, to note that throughput
 * may be reduced, which is generally not recommended.
 *
 * @param timeoutMillis  the maximum millis to wait
 * @return done closure
 * @throws InterruptedException if the current thread is interrupted while waiting
 * @throws TimeoutException if timeout
 * @since 1.3.1
 */
public Closure join(final long timeoutMillis) throws InterruptedException, TimeoutException {
    final JoinableClosure joinable = castToJoinalbe(this.done);
    joinable.join(timeoutMillis);
    return joinable.getClosure();
}
Also used : JoinableClosure(com.alipay.sofa.jraft.closure.JoinableClosure)

Aggregations

JoinableClosure (com.alipay.sofa.jraft.closure.JoinableClosure)3 Iterator (com.alipay.sofa.jraft.Iterator)1 JRaftUtils (com.alipay.sofa.jraft.JRaftUtils)1 Node (com.alipay.sofa.jraft.Node)1 NodeManager (com.alipay.sofa.jraft.NodeManager)1 RaftGroupService (com.alipay.sofa.jraft.RaftGroupService)1 StateMachine (com.alipay.sofa.jraft.StateMachine)1 Status (com.alipay.sofa.jraft.Status)1 ReadIndexClosure (com.alipay.sofa.jraft.closure.ReadIndexClosure)1 SynchronizedClosure (com.alipay.sofa.jraft.closure.SynchronizedClosure)1 TaskClosure (com.alipay.sofa.jraft.closure.TaskClosure)1 Configuration (com.alipay.sofa.jraft.conf.Configuration)1 EnumOutter (com.alipay.sofa.jraft.entity.EnumOutter)1 PeerId (com.alipay.sofa.jraft.entity.PeerId)1 Task (com.alipay.sofa.jraft.entity.Task)1 UserLog (com.alipay.sofa.jraft.entity.UserLog)1 LogIndexOutOfBoundsException (com.alipay.sofa.jraft.error.LogIndexOutOfBoundsException)1 LogNotFoundException (com.alipay.sofa.jraft.error.LogNotFoundException)1 RaftError (com.alipay.sofa.jraft.error.RaftError)1 RaftException (com.alipay.sofa.jraft.error.RaftException)1