Search in sources :

Example 1 with Task

use of com.alipay.sofa.jraft.entity.Task in project sofa-jraft by sofastack.

the class StoreEngine method applySplit.

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);
}
Also used : Status(com.alipay.sofa.jraft.Status) Task(com.alipay.sofa.jraft.entity.Task) KVClosureAdapter(com.alipay.sofa.jraft.rhea.storage.KVClosureAdapter) KVOperation(com.alipay.sofa.jraft.rhea.storage.KVOperation) Region(com.alipay.sofa.jraft.rhea.metadata.Region)

Example 2 with Task

use of com.alipay.sofa.jraft.entity.Task in project sofa-jraft by sofastack.

the class NodeTest method testTripleNodes.

@Test
public void testTripleNodes() 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()));
    }
    // 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);
    }
    {
        // task with TaskClosure
        final ByteBuffer data = ByteBuffer.wrap("task closure".getBytes());
        final Vector<String> cbs = new Vector<>();
        final CountDownLatch latch = new CountDownLatch(1);
        final Task task = new Task(data, new TaskClosure() {

            @Override
            public void run(final Status status) {
                cbs.add("apply");
                latch.countDown();
            }

            @Override
            public void onCommitted() {
                cbs.add("commit");
            }
        });
        leader.apply(task);
        latch.await();
        assertEquals(2, cbs.size());
        assertEquals("commit", cbs.get(0));
        assertEquals("apply", cbs.get(1));
    }
    cluster.ensureSame(-1);
    assertEquals(2, cluster.getFollowers().size());
    cluster.stopAll();
}
Also used : Status(com.alipay.sofa.jraft.Status) Task(com.alipay.sofa.jraft.entity.Task) Node(com.alipay.sofa.jraft.Node) CountDownLatch(java.util.concurrent.CountDownLatch) ByteBuffer(java.nio.ByteBuffer) Vector(java.util.Vector) PeerId(com.alipay.sofa.jraft.entity.PeerId) TaskClosure(com.alipay.sofa.jraft.closure.TaskClosure) Test(org.junit.Test)

Example 3 with Task

use of com.alipay.sofa.jraft.entity.Task in project sofa-jraft by sofastack.

the class NodeTest method testRecoverFollower.

@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();
}
Also used : Task(com.alipay.sofa.jraft.entity.Task) Endpoint(com.alipay.sofa.jraft.util.Endpoint) Node(com.alipay.sofa.jraft.Node) ByteBuffer(java.nio.ByteBuffer) Endpoint(com.alipay.sofa.jraft.util.Endpoint) PeerId(com.alipay.sofa.jraft.entity.PeerId) Test(org.junit.Test)

Example 4 with Task

use of com.alipay.sofa.jraft.entity.Task in project sofa-jraft by sofastack.

the class NodeTest method testLeaderTransferBeforeLogIsCompleted.

@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();
}
Also used : Task(com.alipay.sofa.jraft.entity.Task) Node(com.alipay.sofa.jraft.Node) CountDownLatch(java.util.concurrent.CountDownLatch) PeerId(com.alipay.sofa.jraft.entity.PeerId) Test(org.junit.Test)

Example 5 with Task

use of com.alipay.sofa.jraft.entity.Task 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)

Aggregations

Task (com.alipay.sofa.jraft.entity.Task)25 PeerId (com.alipay.sofa.jraft.entity.PeerId)17 Node (com.alipay.sofa.jraft.Node)16 ByteBuffer (java.nio.ByteBuffer)15 Status (com.alipay.sofa.jraft.Status)14 CountDownLatch (java.util.concurrent.CountDownLatch)14 Test (org.junit.Test)13 Endpoint (com.alipay.sofa.jraft.util.Endpoint)12 Configuration (com.alipay.sofa.jraft.conf.Configuration)9 SynchronizedClosure (com.alipay.sofa.jraft.closure.SynchronizedClosure)7 ReadIndexClosure (com.alipay.sofa.jraft.closure.ReadIndexClosure)6 NodeOptions (com.alipay.sofa.jraft.option.NodeOptions)6 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)6 ArrayList (java.util.ArrayList)5 TaskClosure (com.alipay.sofa.jraft.closure.TaskClosure)4 LogIndexOutOfBoundsException (com.alipay.sofa.jraft.error.LogIndexOutOfBoundsException)4 LogNotFoundException (com.alipay.sofa.jraft.error.LogNotFoundException)4 RaftError (com.alipay.sofa.jraft.error.RaftError)4 RaftOptions (com.alipay.sofa.jraft.option.RaftOptions)4 TimeUnit (java.util.concurrent.TimeUnit)4