Search in sources :

Example 6 with Status

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

the class RocksKVStoreTest method getAndPutTest.

/**
 * Test method: {@link RocksRawKVStore#getAndPut(byte[], byte[], KVStoreClosure)}
 */
@Test
public void getAndPutTest() {
    final byte[] key = makeKey("put_test");
    TestClosure closure = new TestClosure();
    this.kvStore.get(key, closure);
    byte[] value = (byte[]) closure.getData();
    assertNull(value);
    value = makeValue("put_test_value");
    KVStoreClosure kvStoreClosure = new BaseKVStoreClosure() {

        @Override
        public void run(Status status) {
            assertEquals(status, Status.OK());
        }
    };
    this.kvStore.getAndPut(key, value, kvStoreClosure);
    assertNull(kvStoreClosure.getData());
    byte[] newVal = makeValue("put_test_value_new");
    this.kvStore.getAndPut(key, newVal, kvStoreClosure);
    assertArrayEquals(value, (byte[]) kvStoreClosure.getData());
}
Also used : TestClosure(com.alipay.sofa.jraft.rhea.storage.TestClosure) Status(com.alipay.sofa.jraft.Status) BaseKVStoreClosure(com.alipay.sofa.jraft.rhea.storage.BaseKVStoreClosure) KVStoreClosure(com.alipay.sofa.jraft.rhea.storage.KVStoreClosure) BaseKVStoreClosure(com.alipay.sofa.jraft.rhea.storage.BaseKVStoreClosure) Test(org.junit.Test)

Example 7 with Status

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

the class AbstractChaosTest method chaosSplittingTest.

@Test
public void chaosSplittingTest() {
    final List<PeerId> peerIds = TestUtil.generatePeers(INITIAL_PEER_COUNT);
    final CliOptions opts = new CliOptions();
    opts.setTimeoutMs(30000);
    final RheaKVCliService cliService = RheaKVServiceFactory.createAndInitRheaKVCliService(opts);
    final long regionId = Constants.DEFAULT_REGION_ID;
    final long newRegionId = 2;
    final String groupId = JRaftHelper.getJRaftGroupId(ChaosTestCluster.CLUSTER_NAME, regionId);
    final Configuration conf = new Configuration(peerIds);
    ChaosTestCluster cluster = null;
    for (int l = 0; l < RETRIES; l++) {
        final ExecutorService executor = Executors.newCachedThreadPool(new NamedThreadFactory("chaos-splitting-test", true));
        final List<Future<?>> allFutures = new CopyOnWriteArrayList<>();
        try {
            cluster = new ChaosTestCluster(peerIds, getStorageType(), isAllowBatching(), isOnlyLeaderRead());
            cluster.start();
            final RheaKVStore store = cluster.getLeaderStore();
            // for least keys on split
            for (int j = 0; j < LOOP_2; j++) {
                store.bPut(j + "_split_", VALUE);
            }
            for (int i = 0; i < LOOP_1; i++) {
                final int index = i;
                final Future<?> f = executor.submit(() -> {
                    for (int j = 0; j < LOOP_2; j++) {
                        store.bPut(index + "_split_test_" + j, VALUE);
                    }
                });
                allFutures.add(f);
            }
            final Status st = cliService.rangeSplit(regionId, newRegionId, groupId, conf);
            if (!st.isOk()) {
                System.err.println("Status:" + st);
                throw new RuntimeException(st.toString());
            }
            // wait for all writes finished
            for (final Future<?> f : allFutures) {
                f.get(30, TimeUnit.SECONDS);
            }
            break;
        } catch (final Exception e) {
            System.err.println("Fail to put data, try again...");
            e.printStackTrace();
            for (final Future<?> f : allFutures) {
                f.cancel(true);
            }
            if (cluster != null) {
                cluster.stopAll();
            }
            cluster = null;
        } finally {
            ExecutorServiceHelper.shutdownAndAwaitTermination(executor);
        }
    }
    if (cluster == null) {
        throw new RuntimeException("fail to put data, can not check data");
    }
    try {
        chaosSplittingCheckData(cluster);
    } finally {
        cluster.stopAll();
    }
}
Also used : Status(com.alipay.sofa.jraft.Status) RheaKVStore(com.alipay.sofa.jraft.rhea.client.RheaKVStore) RheaKVCliService(com.alipay.sofa.jraft.rhea.client.RheaKVCliService) Configuration(com.alipay.sofa.jraft.conf.Configuration) NamedThreadFactory(com.alipay.sofa.jraft.util.NamedThreadFactory) CliOptions(com.alipay.sofa.jraft.option.CliOptions) ExecutorService(java.util.concurrent.ExecutorService) CompletableFuture(java.util.concurrent.CompletableFuture) Future(java.util.concurrent.Future) PeerId(com.alipay.sofa.jraft.entity.PeerId) CopyOnWriteArrayList(java.util.concurrent.CopyOnWriteArrayList) Test(org.junit.Test)

Example 8 with Status

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

the class LocalSnapshotCopierTest method testCancelByRemote.

@Test
@SuppressWarnings({ "rawtypes", "unchecked" })
public void testCancelByRemote() throws Exception {
    final FutureImpl<Message> future = new FutureImpl<>();
    final RpcRequests.GetFileRequest.Builder rb = RpcRequests.GetFileRequest.newBuilder().setReaderId(99).setFilename(Snapshot.JRAFT_SNAPSHOT_META_FILE).setCount(Integer.MAX_VALUE).setOffset(0).setReadPartly(true);
    // mock get metadata
    final ArgumentCaptor<RpcResponseClosure> argument = ArgumentCaptor.forClass(RpcResponseClosure.class);
    Mockito.when(this.raftClientService.getFile(eq(new Endpoint("localhost", 8081)), eq(rb.build()), eq(this.copyOpts.getTimeoutMs()), argument.capture())).thenReturn(future);
    this.copier.start();
    Thread.sleep(500);
    final RpcResponseClosure<RpcRequests.GetFileResponse> closure = argument.getValue();
    closure.run(new Status(RaftError.ECANCELED, "test cancel"));
    this.copier.join();
    // start timer
    final SnapshotReader reader = this.copier.getReader();
    assertNull(reader);
    Assert.assertEquals(RaftError.ECANCELED.getNumber(), this.copier.getCode());
    Assert.assertEquals("test cancel", this.copier.getErrorMsg());
}
Also used : Status(com.alipay.sofa.jraft.Status) Message(com.google.protobuf.Message) FutureImpl(com.alipay.sofa.jraft.rpc.impl.FutureImpl) Endpoint(com.alipay.sofa.jraft.util.Endpoint) RpcResponseClosure(com.alipay.sofa.jraft.rpc.RpcResponseClosure) SnapshotReader(com.alipay.sofa.jraft.storage.snapshot.SnapshotReader) Test(org.junit.Test) BaseStorageTest(com.alipay.sofa.jraft.storage.BaseStorageTest)

Example 9 with Status

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

the class CopySessionTest method testOnRpcReturnedRetry.

@Test
public void testOnRpcReturnedRetry() throws Exception {
    assertNull(this.session.getTimer());
    assertNull(this.session.getRpcCall());
    final ByteBufferCollector bufRef = ByteBufferCollector.allocate(0);
    this.session.setDestBuf(bufRef);
    final FutureImpl<Message> future = new FutureImpl<>();
    final RpcRequests.GetFileRequest.Builder rb = RpcRequests.GetFileRequest.newBuilder().setReaderId(99).setFilename("data").setCount(Integer.MAX_VALUE).setOffset(0).setReadPartly(true);
    Mockito.when(this.rpcService.getFile(this.address, rb.build(), this.copyOpts.getTimeoutMs(), session.getDone())).thenReturn(future);
    this.session.onRpcReturned(new Status(RaftError.EINTR, "test"), null);
    assertNotNull(this.session.getTimer());
    Thread.sleep(this.copyOpts.getRetryIntervalMs() + 100);
    assertNotNull(this.session.getRpcCall());
    assertSame(future, this.session.getRpcCall());
    assertNull(this.session.getTimer());
}
Also used : Status(com.alipay.sofa.jraft.Status) ByteBufferCollector(com.alipay.sofa.jraft.util.ByteBufferCollector) Message(com.google.protobuf.Message) FutureImpl(com.alipay.sofa.jraft.rpc.impl.FutureImpl) Test(org.junit.Test)

Example 10 with Status

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

the class LogManagerTest method testAppendEntriesBeforeAppliedIndex.

@Test
public void testAppendEntriesBeforeAppliedIndex() throws Exception {
    // Append 0-10
    List<LogEntry> mockEntries = TestUtils.mockEntries(10);
    for (int i = 0; i < 10; i++) {
        mockEntries.get(i).getId().setTerm(1);
    }
    final CountDownLatch latch1 = new CountDownLatch(1);
    this.logManager.appendEntries(new ArrayList<>(mockEntries), new LogManager.StableClosure() {

        @Override
        public void run(final Status status) {
            assertTrue(status.isOk());
            latch1.countDown();
        }
    });
    latch1.await();
    assertEquals(1, this.logManager.getFirstLogIndex());
    assertEquals(10, this.logManager.getLastLogIndex());
    // waiting for setDiskId()
    Thread.sleep(200);
    this.logManager.setAppliedId(new LogId(9, 1));
    for (int i = 0; i < 10; i++) {
        assertNull(this.logManager.getEntryFromMemory(i));
    }
    // append 1-10 again, already applied, returns OK.
    final CountDownLatch latch2 = new CountDownLatch(1);
    mockEntries = TestUtils.mockEntries(10);
    mockEntries.remove(0);
    this.logManager.appendEntries(new ArrayList<>(mockEntries), new LogManager.StableClosure() {

        @Override
        public void run(final Status status) {
            assertTrue(status.isOk());
            latch2.countDown();
        }
    });
    latch2.await();
    assertEquals(1, this.logManager.getFirstLogIndex());
    assertEquals(10, this.logManager.getLastLogIndex());
}
Also used : Status(com.alipay.sofa.jraft.Status) CountDownLatch(java.util.concurrent.CountDownLatch) LogManager(com.alipay.sofa.jraft.storage.LogManager) LogId(com.alipay.sofa.jraft.entity.LogId) LogEntry(com.alipay.sofa.jraft.entity.LogEntry) Test(org.junit.Test) BaseStorageTest(com.alipay.sofa.jraft.storage.BaseStorageTest)

Aggregations

Status (com.alipay.sofa.jraft.Status)213 Test (org.junit.Test)63 PeerId (com.alipay.sofa.jraft.entity.PeerId)55 CountDownLatch (java.util.concurrent.CountDownLatch)36 BaseKVStoreClosure (com.alipay.sofa.jraft.rhea.storage.BaseKVStoreClosure)33 Configuration (com.alipay.sofa.jraft.conf.Configuration)25 Message (com.google.protobuf.Message)24 ReadIndexClosure (com.alipay.sofa.jraft.closure.ReadIndexClosure)22 ArrayList (java.util.ArrayList)22 Node (com.alipay.sofa.jraft.Node)21 Closure (com.alipay.sofa.jraft.Closure)17 Task (com.alipay.sofa.jraft.entity.Task)17 ByteBuffer (java.nio.ByteBuffer)16 Endpoint (com.alipay.sofa.jraft.util.Endpoint)15 List (java.util.List)15 RaftException (com.alipay.sofa.jraft.error.RaftException)14 LogEntry (com.alipay.sofa.jraft.entity.LogEntry)12 LogId (com.alipay.sofa.jraft.entity.LogId)12 KVStoreClosure (com.alipay.sofa.jraft.rhea.storage.KVStoreClosure)12 JRaftException (com.alipay.sofa.jraft.error.JRaftException)11