Search in sources :

Example 21 with Closure

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

the class UtilsTest method testRunClosureWithStatus.

@Test
public void testRunClosureWithStatus() throws Exception {
    CountDownLatch latch = new CountDownLatch(1);
    Utils.runClosureInThread(new Closure() {

        @Override
        public void run(Status status) {
            assertFalse(status.isOk());
            Assert.assertEquals(RaftError.EACCES.getNumber(), status.getCode());
            assertEquals("test 99", status.getErrorMsg());
            latch.countDown();
        }
    }, new Status(RaftError.EACCES, "test %d", 99));
    latch.await();
}
Also used : Status(com.alipay.sofa.jraft.Status) Closure(com.alipay.sofa.jraft.Closure) CountDownLatch(java.util.concurrent.CountDownLatch) Test(org.junit.Test)

Example 22 with Closure

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

the class RocksKVStoreTest method snapshotTest.

public void snapshotTest() throws Exception {
    final File backupDir = new File("backup");
    if (backupDir.exists()) {
        FileUtils.deleteDirectory(backupDir);
    }
    FileUtils.forceMkdir(backupDir);
    for (int i = 0; i < 100000; i++) {
        final String v = String.valueOf(i);
        this.kvStore.put(makeKey(v), makeValue(v), null);
    }
    final Region region = new Region();
    KVStoreSnapshotFile kvStoreSnapshotFile = KVStoreSnapshotFileFactory.getKVStoreSnapshotFile(this.kvStore);
    final ExecutorService snapshotPool = StoreEngineHelper.createSnapshotExecutor(1, 2);
    final TestSnapshotWriter snapshotWriter = new TestSnapshotWriter(backupDir.getAbsolutePath());
    final CountDownLatch latch = new CountDownLatch(1);
    final Closure done = status -> {
        assertTrue(status.isOk());
        latch.countDown();
    };
    kvStoreSnapshotFile.save(snapshotWriter, region, done, snapshotPool);
    latch.await();
    final LocalFileMeta meta = (LocalFileMeta) snapshotWriter.getFileMeta(SNAPSHOT_ARCHIVE);
    assertNotNull(meta);
    assertNotNull(get(makeKey("1")));
    this.kvStore.put(makeKey("100001"), makeValue("100001"), null);
    assertNotNull(get(makeKey("100001")));
    this.kvStore.shutdown();
    FileUtils.deleteDirectory(new File(this.tempPath));
    FileUtils.forceMkdir(new File(this.tempPath));
    this.kvStore = new RocksRawKVStore();
    this.kvStore.init(this.dbOptions);
    assertNull(get(makeKey("1")));
    final TestSnapshotReader snapshotReader = new TestSnapshotReader(snapshotWriter.metaTable, backupDir.getAbsolutePath());
    kvStoreSnapshotFile = KVStoreSnapshotFileFactory.getKVStoreSnapshotFile(this.kvStore);
    final boolean ret = kvStoreSnapshotFile.load(snapshotReader, region);
    assertTrue(ret);
    for (int i = 0; i < 100000; i++) {
        final String v = String.valueOf(i);
        assertArrayEquals(makeValue(v), get(makeKey(v)));
    }
    // key[100001] is put after the snapshot, so key[100001] should not exist.
    assertNull(get(makeKey("100001")));
    FileUtils.deleteDirectory(backupDir);
    ExecutorServiceHelper.shutdownAndAwaitTermination(snapshotPool);
}
Also used : StoreEngineHelper(com.alipay.sofa.jraft.rhea.StoreEngineHelper) Region(com.alipay.sofa.jraft.rhea.metadata.Region) TestSnapshotReader(com.alipay.sofa.jraft.rhea.storage.TestSnapshotReader) SyncKVStore(com.alipay.sofa.jraft.rhea.storage.SyncKVStore) Lists(com.alipay.sofa.jraft.rhea.util.Lists) SstColumnFamily(com.alipay.sofa.jraft.rhea.storage.SstColumnFamily) Map(java.util.Map) After(org.junit.After) KVStoreSnapshotFileFactory(com.alipay.sofa.jraft.rhea.storage.KVStoreSnapshotFileFactory) Method(java.lang.reflect.Method) Path(java.nio.file.Path) DistributedLock(com.alipay.sofa.jraft.rhea.util.concurrent.DistributedLock) EnumMap(java.util.EnumMap) KVStoreClosure(com.alipay.sofa.jraft.rhea.storage.KVStoreClosure) RawKVStore(com.alipay.sofa.jraft.rhea.storage.RawKVStore) CountDownLatch(java.util.concurrent.CountDownLatch) List(java.util.List) KVOperation(com.alipay.sofa.jraft.rhea.storage.KVOperation) Assert.assertFalse(org.junit.Assert.assertFalse) RocksDBOptions(com.alipay.sofa.jraft.rhea.options.RocksDBOptions) TestClosure(com.alipay.sofa.jraft.rhea.storage.TestClosure) KVState(com.alipay.sofa.jraft.rhea.storage.KVState) BaseKVStoreClosure(com.alipay.sofa.jraft.rhea.storage.BaseKVStoreClosure) KVStoreSnapshotFile(com.alipay.sofa.jraft.rhea.storage.KVStoreSnapshotFile) KeyValueTool.makeKey(com.alipay.sofa.jraft.rhea.KeyValueTool.makeKey) Closure(com.alipay.sofa.jraft.Closure) KVIterator(com.alipay.sofa.jraft.rhea.storage.KVIterator) Assert.assertArrayEquals(org.junit.Assert.assertArrayEquals) ExecutorServiceHelper(com.alipay.sofa.jraft.util.ExecutorServiceHelper) LocalLock(com.alipay.sofa.jraft.rhea.storage.LocalLock) ExecutorService(java.util.concurrent.ExecutorService) Before(org.junit.Before) KVStoreAccessHelper(com.alipay.sofa.jraft.rhea.storage.KVStoreAccessHelper) TestSnapshotWriter(com.alipay.sofa.jraft.rhea.storage.TestSnapshotWriter) RocksRawKVStore(com.alipay.sofa.jraft.rhea.storage.RocksRawKVStore) Assert.assertNotNull(org.junit.Assert.assertNotNull) Assert.assertTrue(org.junit.Assert.assertTrue) IOException(java.io.IOException) FileUtils(org.apache.commons.io.FileUtils) Test(org.junit.Test) Status(com.alipay.sofa.jraft.Status) ByteArray(com.alipay.sofa.jraft.rhea.util.ByteArray) File(java.io.File) TimeUnit(java.util.concurrent.TimeUnit) Assert.assertNull(org.junit.Assert.assertNull) Paths(java.nio.file.Paths) LocalFileMeta(com.alipay.sofa.jraft.entity.LocalFileMetaOutter.LocalFileMeta) Sequence(com.alipay.sofa.jraft.rhea.storage.Sequence) KVEntry(com.alipay.sofa.jraft.rhea.storage.KVEntry) KVStateOutputList(com.alipay.sofa.jraft.rhea.storage.KVStateOutputList) KeyValueTool.makeValue(com.alipay.sofa.jraft.rhea.KeyValueTool.makeValue) Assert.assertEquals(org.junit.Assert.assertEquals) BytesUtil(com.alipay.sofa.jraft.util.BytesUtil) KVStoreClosure(com.alipay.sofa.jraft.rhea.storage.KVStoreClosure) TestClosure(com.alipay.sofa.jraft.rhea.storage.TestClosure) BaseKVStoreClosure(com.alipay.sofa.jraft.rhea.storage.BaseKVStoreClosure) Closure(com.alipay.sofa.jraft.Closure) RocksRawKVStore(com.alipay.sofa.jraft.rhea.storage.RocksRawKVStore) TestSnapshotReader(com.alipay.sofa.jraft.rhea.storage.TestSnapshotReader) CountDownLatch(java.util.concurrent.CountDownLatch) ExecutorService(java.util.concurrent.ExecutorService) Region(com.alipay.sofa.jraft.rhea.metadata.Region) KVStoreSnapshotFile(com.alipay.sofa.jraft.rhea.storage.KVStoreSnapshotFile) LocalFileMeta(com.alipay.sofa.jraft.entity.LocalFileMetaOutter.LocalFileMeta) KVStoreSnapshotFile(com.alipay.sofa.jraft.rhea.storage.KVStoreSnapshotFile) File(java.io.File) TestSnapshotWriter(com.alipay.sofa.jraft.rhea.storage.TestSnapshotWriter)

Example 23 with Closure

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

the class AtomicStateMachine method onApply.

@Override
public void onApply(final Iterator iter) {
    while (iter.hasNext()) {
        final Closure done = iter.done();
        CommandType cmdType;
        final ByteBuffer data = iter.getData();
        Object cmd = null;
        LeaderTaskClosure closure = null;
        if (done != null) {
            closure = (LeaderTaskClosure) done;
            cmdType = closure.getCmdType();
            cmd = closure.getCmd();
        } else {
            final byte b = data.get();
            final byte[] cmdBytes = new byte[data.remaining()];
            data.get(cmdBytes);
            cmdType = CommandType.parseByte(b);
            switch(cmdType) {
                case GET:
                    cmd = CommandCodec.decodeCommand(cmdBytes, GetCommand.class);
                    break;
                case SET:
                    cmd = CommandCodec.decodeCommand(cmdBytes, SetCommand.class);
                    break;
                case CAS:
                    cmd = CommandCodec.decodeCommand(cmdBytes, CompareAndSetCommand.class);
                    break;
                case INC:
                    cmd = CommandCodec.decodeCommand(cmdBytes, IncrementAndGetCommand.class);
                    break;
            }
        }
        final String key = ((BaseRequestCommand) cmd).getKey();
        final AtomicLong counter = getCounter(key, true);
        Object response = null;
        switch(cmdType) {
            case GET:
                response = new ValueCommand(counter.get());
                break;
            case SET:
                final SetCommand setCmd = (SetCommand) cmd;
                counter.set(setCmd.getValue());
                response = new BooleanCommand(true);
                break;
            case CAS:
                final CompareAndSetCommand casCmd = (CompareAndSetCommand) cmd;
                response = new BooleanCommand(counter.compareAndSet(casCmd.getExpect(), casCmd.getNewValue()));
                break;
            case INC:
                final IncrementAndGetCommand incCmd = (IncrementAndGetCommand) cmd;
                final long ret = counter.addAndGet(incCmd.getDetal());
                response = new ValueCommand(ret);
                break;
        }
        if (closure != null) {
            closure.setResponse(response);
            closure.run(Status.OK());
        }
        iter.next();
    }
}
Also used : Closure(com.alipay.sofa.jraft.Closure) BooleanCommand(com.alipay.sofa.jraft.test.atomic.command.BooleanCommand) IncrementAndGetCommand(com.alipay.sofa.jraft.test.atomic.command.IncrementAndGetCommand) ByteBuffer(java.nio.ByteBuffer) IncrementAndGetCommand(com.alipay.sofa.jraft.test.atomic.command.IncrementAndGetCommand) GetCommand(com.alipay.sofa.jraft.test.atomic.command.GetCommand) CompareAndSetCommand(com.alipay.sofa.jraft.test.atomic.command.CompareAndSetCommand) AtomicLong(java.util.concurrent.atomic.AtomicLong) BaseRequestCommand(com.alipay.sofa.jraft.test.atomic.command.BaseRequestCommand) ValueCommand(com.alipay.sofa.jraft.test.atomic.command.ValueCommand) CompareAndSetCommand(com.alipay.sofa.jraft.test.atomic.command.CompareAndSetCommand) SetCommand(com.alipay.sofa.jraft.test.atomic.command.SetCommand)

Example 24 with Closure

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

the class NodeImpl method afterShutdown.

private void afterShutdown() {
    List<Closure> savedDoneList = null;
    this.writeLock.lock();
    try {
        if (!this.shutdownContinuations.isEmpty()) {
            savedDoneList = new ArrayList<>(this.shutdownContinuations);
        }
        if (this.logStorage != null) {
            this.logStorage.shutdown();
        }
        this.state = State.STATE_SHUTDOWN;
    } finally {
        this.writeLock.unlock();
    }
    if (savedDoneList != null) {
        for (final Closure closure : savedDoneList) {
            Utils.runClosureInThread(closure);
        }
    }
}
Also used : RpcResponseClosure(com.alipay.sofa.jraft.rpc.RpcResponseClosure) CatchUpClosure(com.alipay.sofa.jraft.closure.CatchUpClosure) SynchronizedClosure(com.alipay.sofa.jraft.closure.SynchronizedClosure) RpcRequestClosure(com.alipay.sofa.jraft.rpc.RpcRequestClosure) ReadIndexClosure(com.alipay.sofa.jraft.closure.ReadIndexClosure) Closure(com.alipay.sofa.jraft.Closure)

Example 25 with Closure

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

the class ClosureQueueImpl method clear.

@Override
public void clear() {
    List<Closure> savedQueue;
    this.lock.lock();
    try {
        this.firstIndex = 0;
        savedQueue = this.queue;
        this.queue = new LinkedList<>();
    } finally {
        this.lock.unlock();
    }
    final Status status = new Status(RaftError.EPERM, "Leader stepped down");
    Utils.runInThread(() -> {
        for (final Closure done : savedQueue) {
            if (done != null) {
                done.run(status);
            }
        }
    });
}
Also used : Status(com.alipay.sofa.jraft.Status) Closure(com.alipay.sofa.jraft.Closure)

Aggregations

Closure (com.alipay.sofa.jraft.Closure)28 Status (com.alipay.sofa.jraft.Status)16 PeerId (com.alipay.sofa.jraft.entity.PeerId)11 List (java.util.List)9 Test (org.junit.Test)9 CountDownLatch (java.util.concurrent.CountDownLatch)8 ArrayList (java.util.ArrayList)7 Configuration (com.alipay.sofa.jraft.conf.Configuration)6 Map (java.util.Map)6 RaftError (com.alipay.sofa.jraft.error.RaftError)5 TimeUnit (java.util.concurrent.TimeUnit)5 LocalFileMeta (com.alipay.sofa.jraft.entity.LocalFileMetaOutter.LocalFileMeta)4 Node (com.alipay.sofa.jraft.Node)3 LeaderChangeContext (com.alipay.sofa.jraft.entity.LeaderChangeContext)3 LogEntry (com.alipay.sofa.jraft.entity.LogEntry)3 LogId (com.alipay.sofa.jraft.entity.LogId)3 NodeId (com.alipay.sofa.jraft.entity.NodeId)3 RaftException (com.alipay.sofa.jraft.error.RaftException)3 NodeOptions (com.alipay.sofa.jraft.option.NodeOptions)3 KeyValueTool.makeKey (com.alipay.sofa.jraft.rhea.KeyValueTool.makeKey)3