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();
}
}
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();
}
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();
}
Aggregations