Search in sources :

Example 46 with Status

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

the class ReplicatorTest method testOnHeartbeatReturnedTermMismatch.

@Test
public void testOnHeartbeatReturnedTermMismatch() {
    final Replicator r = getReplicator();
    final RpcRequests.AppendEntriesRequest request = createEmptyEntriesRequest();
    final RpcRequests.AppendEntriesResponse response = // 
    RpcRequests.AppendEntriesResponse.newBuilder().setSuccess(// 
    false).setLastLogIndex(// 
    12).setTerm(// 
    2).build();
    this.id.unlock();
    Replicator.onHeartbeatReturned(this.id, Status.OK(), request, response, Utils.monotonicMs());
    Mockito.verify(this.node).increaseTermTo(2, new Status(RaftError.EHIGHERTERMRESPONSE, "Leader receives higher term heartbeat_response from peer:%s", this.peerId));
    assertNull(r.id);
}
Also used : Status(com.alipay.sofa.jraft.Status) RpcRequests(com.alipay.sofa.jraft.rpc.RpcRequests) Test(org.junit.Test)

Example 47 with Status

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

the class ReplicatorTest method testOnRpcReturnedWaitMoreEntries.

@Test
public void testOnRpcReturnedWaitMoreEntries() throws Exception {
    final Replicator r = getReplicator();
    assertEquals(-1, r.getWaitId());
    final RpcRequests.AppendEntriesRequest request = createEmptyEntriesRequest();
    final RpcRequests.AppendEntriesResponse response = // 
    RpcRequests.AppendEntriesResponse.newBuilder().setSuccess(// 
    true).setLastLogIndex(// 
    10).setTerm(// 
    1).build();
    this.id.unlock();
    Mockito.when(this.logManager.wait(eq(10L), Mockito.any(), same(this.id))).thenReturn(99L);
    final CountDownLatch latch = new CountDownLatch(1);
    Replicator.waitForCaughtUp(this.id, 1, System.currentTimeMillis() + 5000, new CatchUpClosure() {

        @Override
        public void run(final Status status) {
            assertTrue(status.isOk());
            latch.countDown();
        }
    });
    Replicator.onRpcReturned(this.id, Replicator.RequestType.AppendEntries, Status.OK(), request, response, 0, 0, Utils.monotonicMs());
    assertEquals(r.statInfo.runningState, Replicator.RunningState.IDLE);
    this.id.unlock();
    assertEquals(11, Replicator.getNextIndex(this.id));
    assertEquals(99, r.getWaitId());
    // make sure catch up closure is invoked.
    latch.await();
}
Also used : Status(com.alipay.sofa.jraft.Status) CatchUpClosure(com.alipay.sofa.jraft.closure.CatchUpClosure) RpcRequests(com.alipay.sofa.jraft.rpc.RpcRequests) CountDownLatch(java.util.concurrent.CountDownLatch) Test(org.junit.Test)

Example 48 with Status

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

the class FSMCallerTest method testOnError.

@Test
public void testOnError() throws Exception {
    this.fsmCaller.onError(new RaftException(ErrorType.ERROR_TYPE_LOG, new Status(-1, "test")));
    this.fsmCaller.flush();
    assertFalse(this.fsmCaller.getError().getStatus().isOk());
    assertEquals(ErrorType.ERROR_TYPE_LOG, this.fsmCaller.getError().getType());
    Mockito.verify(this.node).onError(Mockito.any());
    Mockito.verify(this.fsm).onError(Mockito.any());
}
Also used : Status(com.alipay.sofa.jraft.Status) RaftException(com.alipay.sofa.jraft.error.RaftException) Test(org.junit.Test)

Example 49 with Status

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

the class FSMCallerTest method testOnLeaderStartStop.

@Test
public void testOnLeaderStartStop() throws Exception {
    this.fsmCaller.onLeaderStart(11);
    this.fsmCaller.flush();
    Mockito.verify(this.fsm).onLeaderStart(11);
    final Status status = new Status(-1, "test");
    this.fsmCaller.onLeaderStop(status);
    this.fsmCaller.flush();
    Mockito.verify(this.fsm).onLeaderStop(status);
}
Also used : Status(com.alipay.sofa.jraft.Status) Test(org.junit.Test)

Example 50 with Status

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

the class FSMCallerTest method testOnSnapshotSaveEmptyConf.

@Test
public void testOnSnapshotSaveEmptyConf() throws Exception {
    final CountDownLatch latch = new CountDownLatch(1);
    this.fsmCaller.onSnapshotSave(new SaveSnapshotClosure() {

        @Override
        public void run(final Status status) {
            assertFalse(status.isOk());
            assertEquals("Empty conf entry for lastAppliedIndex=10", status.getErrorMsg());
            latch.countDown();
        }

        @Override
        public SnapshotWriter start(final SnapshotMeta meta) {
            // TODO Auto-generated method stub
            return null;
        }
    });
    latch.await();
}
Also used : Status(com.alipay.sofa.jraft.Status) SnapshotWriter(com.alipay.sofa.jraft.storage.snapshot.SnapshotWriter) SaveSnapshotClosure(com.alipay.sofa.jraft.closure.SaveSnapshotClosure) SnapshotMeta(com.alipay.sofa.jraft.entity.RaftOutter.SnapshotMeta) CountDownLatch(java.util.concurrent.CountDownLatch) Test(org.junit.Test)

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