com.alipay.sofa.jraft.entity.Task Java Examples
The following examples show how to use
com.alipay.sofa.jraft.entity.Task.
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: RaftServerHandler.java From sofa-registry with Apache License 2.0 | 6 votes |
protected Task createTask(LeaderTaskClosure closure, ProcessRequest request) { final ByteArrayOutputStream byteStream = new ByteArrayOutputStream(); Hessian2Output hessianOutput = new Hessian2Output(byteStream); SerializerFactory serializerFactory = new SerializerFactory(); hessianOutput.setSerializerFactory(serializerFactory); try { hessianOutput.writeObject(request); hessianOutput.close(); } catch (IOException e) { LOGGER.error("Raft receive message serialize error!", e); } byte[] cmdBytes = byteStream.toByteArray(); ByteBuffer data = ByteBuffer.allocate(cmdBytes.length); data.put(cmdBytes); data.flip(); return new Task(data, closure); }
Example #2
Source File: CounterServiceImpl.java From sofa-jraft with Apache License 2.0 | 6 votes |
private void applyOperation(final CounterOperation op, final CounterClosure closure) { if (!isLeader()) { handlerNotLeaderError(closure); return; } try { closure.setCounterOperation(op); final Task task = new Task(); task.setData(ByteBuffer.wrap(SerializerManager.getSerializer(SerializerManager.Hessian2).serialize(op))); task.setDone(closure); this.counterServer.getNode().apply(task); } catch (CodecException e) { String errorMsg = "Fail to encode CounterOperation"; LOG.error(errorMsg, e); closure.failure(errorMsg, StringUtils.EMPTY); closure.run(new Status(RaftError.EINTERNAL, errorMsg)); } }
Example #3
Source File: BaseAsyncUserProcessor.java From sofa-jraft with Apache License 2.0 | 6 votes |
private Task createTask(RpcContext asyncCtx, T request, CommandType cmdType) { final LeaderTaskClosure closure = new LeaderTaskClosure(); closure.setCmd(request); closure.setCmdType(cmdType); closure.setDone(status -> { if (status.isOk()) { asyncCtx.sendResponse(closure.getResponse()); } else { asyncCtx.sendResponse(new BooleanCommand(false, status.getErrorMsg())); } }); final byte[] cmdBytes = CommandCodec.encodeCommand(request); final ByteBuffer data = ByteBuffer.allocate(cmdBytes.length + 1); data.put(cmdType.toByte()); data.put(cmdBytes); data.flip(); return new Task(data, closure); }
Example #4
Source File: NodeTest.java From sofa-jraft with Apache License 2.0 | 5 votes |
private void sendTestTaskAndWait(final Node node, final int start, final RaftError err) throws InterruptedException { final CountDownLatch latch = new CountDownLatch(10); for (int i = start; i < start + 10; i++) { final ByteBuffer data = ByteBuffer.wrap(("hello" + i).getBytes()); final Task task = new Task(data, new ExpectClosure(err, latch)); node.apply(task); } waitLatch(latch); }
Example #5
Source File: DBRequestProcessor.java From KitDB with Apache License 2.0 | 5 votes |
public void handle(DBCommandChunk dbCommandChunk) throws CodecException, InterruptedException, KitDBException { final DBClosure closure = new DBClosure(); closure.setChunk(dbCommandChunk); final Task task = new Task(); task.setDone(closure); task.setData(ByteBuffer .wrap(SerializerManager.getSerializer(SerializerManager.Hessian2).serialize(dbCommandChunk))); kitRaft.getNode().apply(task); synchronized (closure) { closure.wait(); } if (closure.getCode() != 0) { throw new KitDBException(ErrorType.STROE_ERROR, closure.getMsg()); } }
Example #6
Source File: CliServiceTest.java From sofa-jraft with Apache License 2.0 | 5 votes |
@SuppressWarnings("SameParameterValue") private void sendTestTaskAndWait(final Node node, final int code) throws InterruptedException { final CountDownLatch latch = new CountDownLatch(10); for (int i = 0; i < 10; i++) { final ByteBuffer data = ByteBuffer.wrap(("hello" + i).getBytes()); final Task task = new Task(data, new ExpectClosure(code, null, latch)); node.apply(task); } assertTrue(latch.await(10, TimeUnit.SECONDS)); }
Example #7
Source File: NodeTest.java From sofa-jraft with Apache License 2.0 | 5 votes |
@Test public void testLeaderTransferBeforeLogIsCompleted() throws Exception { final List<PeerId> peers = TestUtils.generatePeers(3); final TestCluster cluster = new TestCluster("unitest", this.dataPath, peers, 300); for (final PeerId peer : peers) { assertTrue(cluster.start(peer.getEndpoint(), false, 1)); } cluster.waitLeader(); Node leader = cluster.getLeader(); assertNotNull(leader); Thread.sleep(100); final List<Node> followers = cluster.getFollowers(); assertEquals(2, followers.size()); final PeerId targetPeer = followers.get(0).getNodeId().getPeerId().copy(); assertTrue(cluster.stop(targetPeer.getEndpoint())); this.sendTestTaskAndWait(leader); LOG.info("Transfer leadership from {} to {}", leader, targetPeer); assertTrue(leader.transferLeadershipTo(targetPeer).isOk()); final CountDownLatch latch = new CountDownLatch(1); final Task task = new Task(ByteBuffer.wrap("aaaaa".getBytes()), new ExpectClosure(RaftError.EBUSY, latch)); leader.apply(task); waitLatch(latch); assertTrue(cluster.start(targetPeer.getEndpoint())); Thread.sleep(5000); cluster.waitLeader(); leader = cluster.getLeader(); Assert.assertEquals(targetPeer, leader.getNodeId().getPeerId()); assertTrue(cluster.ensureSame(5)); cluster.stopAll(); }
Example #8
Source File: NodeTest.java From sofa-jraft with Apache License 2.0 | 5 votes |
@Test public void testNodeMetrics() throws Exception { final List<PeerId> peers = TestUtils.generatePeers(3); final TestCluster cluster = new TestCluster("unittest", this.dataPath, peers); for (final PeerId peer : peers) { assertTrue(cluster.start(peer.getEndpoint(), false, 300, true)); } // elect leader cluster.waitLeader(); // get leader final Node leader = cluster.getLeader(); assertNotNull(leader); assertEquals(3, leader.listPeers().size()); // apply tasks to leader this.sendTestTaskAndWait(leader); { final ByteBuffer data = ByteBuffer.wrap("no closure".getBytes()); final Task task = new Task(data, null); leader.apply(task); } cluster.ensureSame(-1); for (final Node node : cluster.getNodes()) { System.out.println("-------------" + node.getNodeId() + "-------------"); final ConsoleReporter reporter = ConsoleReporter.forRegistry(node.getNodeMetrics().getMetricRegistry()) .build(); reporter.report(); reporter.close(); System.out.println(); } // TODO check http status assertEquals(2, cluster.getFollowers().size()); cluster.stopAll(); // System.out.println(node.getNodeMetrics().getMetrics()); }
Example #9
Source File: NodeTest.java From sofa-jraft with Apache License 2.0 | 5 votes |
@SuppressWarnings("SameParameterValue") private void sendTestTaskAndWait(final String prefix, final Node node, final int code) throws InterruptedException { final CountDownLatch latch = new CountDownLatch(10); for (int i = 0; i < 10; i++) { final ByteBuffer data = ByteBuffer.wrap((prefix + i).getBytes()); final Task task = new Task(data, new ExpectClosure(code, null, latch)); node.apply(task); } waitLatch(latch); }
Example #10
Source File: NodeImpl.java From sofa-jraft with Apache License 2.0 | 5 votes |
@Override public void apply(final Task task) { if (this.shutdownLatch != null) { Utils.runClosureInThread(task.getDone(), new Status(RaftError.ENODESHUTDOWN, "Node is shutting down.")); throw new IllegalStateException("Node is shutting down"); } Requires.requireNonNull(task, "Null task"); final LogEntry entry = new LogEntry(); entry.setData(task.getData()); int retryTimes = 0; try { final EventTranslator<LogEntryAndClosure> translator = (event, sequence) -> { event.reset(); event.done = task.getDone(); event.entry = entry; event.expectedTerm = task.getExpectedTerm(); }; while (true) { if (this.applyQueue.tryPublishEvent(translator)) { break; } else { retryTimes++; if (retryTimes > MAX_APPLY_RETRY_TIMES) { Utils.runClosureInThread(task.getDone(), new Status(RaftError.EBUSY, "Node is busy, has too many tasks.")); LOG.warn("Node {} applyQueue is overload.", getNodeId()); this.metrics.recordTimes("apply-task-overload-times", 1); return; } ThreadHelper.onSpinWait(); } } } catch (final Exception e) { LOG.error("Fail to apply task.", e); Utils.runClosureInThread(task.getDone(), new Status(RaftError.EPERM, "Node is down.")); } }
Example #11
Source File: BaseAsyncUserProcessor.java From sofa-jraft with Apache License 2.0 | 5 votes |
@Override public void handleRequest(final RpcContext rpcCtx, final T request) { final AtomicRangeGroup group = server.getGroupBykey(request.getKey()); if (!group.getFsm().isLeader()) { rpcCtx.sendResponse(group.redirect()); return; } final CommandType cmdType = getCmdType(); final Task task = createTask(rpcCtx, request, cmdType); group.getNode().apply(task); }
Example #12
Source File: CliServiceTest.java From sofa-jraft with Apache License 2.0 | 5 votes |
@SuppressWarnings("SameParameterValue") private void sendTestTaskAndWait(final Node node, final int code) throws InterruptedException { final CountDownLatch latch = new CountDownLatch(10); for (int i = 0; i < 10; i++) { final ByteBuffer data = ByteBuffer.wrap(("hello" + i).getBytes()); final Task task = new Task(data, new ExpectClosure(code, null, latch)); node.apply(task); } assertTrue(latch.await(10, TimeUnit.SECONDS)); }
Example #13
Source File: RaftRawKVStore.java From sofa-jraft with Apache License 2.0 | 5 votes |
private void applyOperation(final KVOperation op, final KVStoreClosure closure) { if (!isLeader()) { closure.setError(Errors.NOT_LEADER); closure.run(new Status(RaftError.EPERM, "Not leader")); return; } final Task task = new Task(); task.setData(ByteBuffer.wrap(Serializers.getDefault().writeObject(op))); task.setDone(new KVClosureAdapter(closure, op)); this.node.apply(task); }
Example #14
Source File: StoreEngine.java From sofa-jraft with Apache License 2.0 | 4 votes |
public void applySplit(final Long regionId, final Long newRegionId, final KVStoreClosure closure) { Requires.requireNonNull(regionId, "regionId"); Requires.requireNonNull(newRegionId, "newRegionId"); if (this.regionEngineTable.containsKey(newRegionId)) { closure.setError(Errors.CONFLICT_REGION_ID); closure.run(new Status(-1, "Conflict region id %d", newRegionId)); return; } if (!this.splitting.compareAndSet(false, true)) { closure.setError(Errors.SERVER_BUSY); closure.run(new Status(-1, "Server is busy now")); return; } final RegionEngine parentEngine = getRegionEngine(regionId); if (parentEngine == null) { closure.setError(Errors.NO_REGION_FOUND); closure.run(new Status(-1, "RegionEngine[%s] not found", regionId)); this.splitting.set(false); return; } if (!parentEngine.isLeader()) { closure.setError(Errors.NOT_LEADER); closure.run(new Status(-1, "RegionEngine[%s] not leader", regionId)); this.splitting.set(false); return; } final Region parentRegion = parentEngine.getRegion(); final byte[] startKey = BytesUtil.nullToEmpty(parentRegion.getStartKey()); final byte[] endKey = parentRegion.getEndKey(); final long approximateKeys = this.rawKVStore.getApproximateKeysInRange(startKey, endKey); final long leastKeysOnSplit = this.storeOpts.getLeastKeysOnSplit(); if (approximateKeys < leastKeysOnSplit) { closure.setError(Errors.TOO_SMALL_TO_SPLIT); closure.run(new Status(-1, "RegionEngine[%s]'s keys less than %d", regionId, leastKeysOnSplit)); this.splitting.set(false); return; } final byte[] splitKey = this.rawKVStore.jumpOver(startKey, approximateKeys >> 1); if (splitKey == null) { closure.setError(Errors.STORAGE_ERROR); closure.run(new Status(-1, "Fail to scan split key")); this.splitting.set(false); return; } final KVOperation op = KVOperation.createRangeSplit(splitKey, regionId, newRegionId); final Task task = new Task(); task.setData(ByteBuffer.wrap(Serializers.getDefault().writeObject(op))); task.setDone(new KVClosureAdapter(closure, op)); parentEngine.getNode().apply(task); }
Example #15
Source File: NodeTest.java From sofa-jraft with Apache License 2.0 | 4 votes |
@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(); } }
Example #16
Source File: NodeTest.java From sofa-jraft with Apache License 2.0 | 4 votes |
@Test public void testRecoverFollower() throws Exception { final List<PeerId> peers = TestUtils.generatePeers(3); final TestCluster cluster = new TestCluster("unitest", this.dataPath, peers); for (final PeerId peer : peers) { assertTrue(cluster.start(peer.getEndpoint())); } cluster.waitLeader(); final Node leader = cluster.getLeader(); assertNotNull(leader); Thread.sleep(100); final List<Node> followers = cluster.getFollowers(); assertEquals(2, followers.size()); final Endpoint followerAddr = followers.get(0).getNodeId().getPeerId().getEndpoint().copy(); assertTrue(cluster.stop(followerAddr)); this.sendTestTaskAndWait(leader); for (int i = 10; i < 30; i++) { final ByteBuffer data = ByteBuffer.wrap(("no clusre" + i).getBytes()); final Task task = new Task(data, null); leader.apply(task); } // wait leader to compact logs Thread.sleep(5000); // restart follower assertTrue(cluster.start(followerAddr)); assertTrue(cluster.ensureSame(30)); assertEquals(3, cluster.getFsms().size()); for (final MockStateMachine fsm : cluster.getFsms()) { assertEquals(30, fsm.getLogs().size()); } cluster.stopAll(); }
Example #17
Source File: Node.java From sofa-jraft with Apache License 2.0 | 2 votes |
/** * [Thread-safe and wait-free] * * Apply task to the replicated-state-machine * * About the ownership: * |task.data|: for the performance consideration, we will take away the * content. If you want keep the content, copy it before call * this function * |task.done|: If the data is successfully committed to the raft group. We * will pass the ownership to #{@link StateMachine#onApply(Iterator)}. * Otherwise we will specify the error and call it. * * @param task task to apply */ void apply(final Task task);