Search in sources :

Example 1 with CatchUpClosure

use of com.alipay.sofa.jraft.closure.CatchUpClosure 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 2 with CatchUpClosure

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

the class Replicator method notifyOnCaughtUp.

private void notifyOnCaughtUp(final int code, final boolean beforeDestroy) {
    if (this.catchUpClosure == null) {
        return;
    }
    if (code != RaftError.ETIMEDOUT.getNumber()) {
        if (this.nextIndex - 1 + this.catchUpClosure.getMaxMargin() < this.options.getLogManager().getLastLogIndex()) {
            return;
        }
        if (this.catchUpClosure.isErrorWasSet()) {
            return;
        }
        this.catchUpClosure.setErrorWasSet(true);
        if (code != RaftError.SUCCESS.getNumber()) {
            this.catchUpClosure.getStatus().setError(code, RaftError.describeCode(code));
        }
        if (this.catchUpClosure.hasTimer()) {
            if (!beforeDestroy && !this.catchUpClosure.getTimer().cancel(true)) {
                // on_caught_up to void ABA problem
                return;
            }
        }
    } else {
        // timed out
        if (!this.catchUpClosure.isErrorWasSet()) {
            this.catchUpClosure.getStatus().setError(code, RaftError.describeCode(code));
        }
    }
    final CatchUpClosure savedClosure = this.catchUpClosure;
    this.catchUpClosure = null;
    RpcUtils.runClosureInThread(savedClosure, savedClosure.getStatus());
}
Also used : CatchUpClosure(com.alipay.sofa.jraft.closure.CatchUpClosure)

Aggregations

CatchUpClosure (com.alipay.sofa.jraft.closure.CatchUpClosure)2 Status (com.alipay.sofa.jraft.Status)1 RpcRequests (com.alipay.sofa.jraft.rpc.RpcRequests)1 CountDownLatch (java.util.concurrent.CountDownLatch)1 Test (org.junit.Test)1