Search in sources :

Example 1 with ReadIndexStatus

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

the class ReadOnlyServiceTest method testOnApplied.

@Test
public void testOnApplied() throws Exception {
    final ArrayList<ReadIndexState> states = new ArrayList<>();
    final byte[] reqContext = TestUtils.getRandomBytes();
    final CountDownLatch latch = new CountDownLatch(1);
    final ReadIndexState state = new ReadIndexState(new Bytes(reqContext), new ReadIndexClosure() {

        @Override
        public void run(final Status status, final long index, final byte[] reqCtx) {
            assertTrue(status.isOk());
            assertEquals(index, 1);
            assertArrayEquals(reqCtx, reqContext);
            latch.countDown();
        }
    }, Utils.monotonicMs());
    state.setIndex(1);
    states.add(state);
    final ReadIndexStatus readIndexStatus = new ReadIndexStatus(states, null, 1);
    this.readOnlyServiceImpl.getPendingNotifyStatus().put(1L, Arrays.asList(readIndexStatus));
    this.readOnlyServiceImpl.onApplied(2);
    latch.await();
    assertTrue(this.readOnlyServiceImpl.getPendingNotifyStatus().isEmpty());
}
Also used : Status(com.alipay.sofa.jraft.Status) ReadIndexStatus(com.alipay.sofa.jraft.entity.ReadIndexStatus) Bytes(com.alipay.sofa.jraft.util.Bytes) ReadIndexClosure(com.alipay.sofa.jraft.closure.ReadIndexClosure) ReadIndexStatus(com.alipay.sofa.jraft.entity.ReadIndexStatus) ReadIndexState(com.alipay.sofa.jraft.entity.ReadIndexState) ArrayList(java.util.ArrayList) CountDownLatch(java.util.concurrent.CountDownLatch) Test(org.junit.Test)

Example 2 with ReadIndexStatus

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

the class ReadOnlyServiceImpl method resetPendingStatusError.

private void resetPendingStatusError(final Status st) {
    this.lock.lock();
    try {
        final Iterator<List<ReadIndexStatus>> it = this.pendingNotifyStatus.values().iterator();
        while (it.hasNext()) {
            final List<ReadIndexStatus> statuses = it.next();
            for (final ReadIndexStatus status : statuses) {
                reportError(status, st);
            }
            it.remove();
        }
    } finally {
        this.lock.unlock();
    }
}
Also used : ReadIndexStatus(com.alipay.sofa.jraft.entity.ReadIndexStatus) ArrayList(java.util.ArrayList) List(java.util.List)

Example 3 with ReadIndexStatus

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

the class ReadOnlyServiceImpl method onApplied.

/**
 * Called when lastAppliedIndex updates.
 *
 * @param appliedIndex applied index
 */
@Override
public void onApplied(final long appliedIndex) {
    // TODO reuse pendingStatuses list?
    List<ReadIndexStatus> pendingStatuses = null;
    this.lock.lock();
    try {
        if (this.pendingNotifyStatus.isEmpty()) {
            return;
        }
        // Find all statuses that log index less than or equal to appliedIndex.
        final Map<Long, List<ReadIndexStatus>> statuses = this.pendingNotifyStatus.headMap(appliedIndex, true);
        if (statuses != null) {
            pendingStatuses = new ArrayList<>(statuses.size() << 1);
            final Iterator<Map.Entry<Long, List<ReadIndexStatus>>> it = statuses.entrySet().iterator();
            while (it.hasNext()) {
                final Map.Entry<Long, List<ReadIndexStatus>> entry = it.next();
                pendingStatuses.addAll(entry.getValue());
                // Remove the entry from statuses, it will also be removed in pendingNotifyStatus.
                it.remove();
            }
        }
        /*
             * Remaining pending statuses are notified by error if it is presented.
             * When the node is in error state, consider following situations:
             * 1. If commitIndex > appliedIndex, then all pending statuses should be notified by error status.
             * 2. When commitIndex == appliedIndex, there will be no more pending statuses.
             */
        if (this.error != null) {
            resetPendingStatusError(this.error.getStatus());
        }
    } finally {
        this.lock.unlock();
        if (pendingStatuses != null && !pendingStatuses.isEmpty()) {
            for (final ReadIndexStatus status : pendingStatuses) {
                notifySuccess(status);
            }
        }
    }
}
Also used : ReadIndexStatus(com.alipay.sofa.jraft.entity.ReadIndexStatus) ArrayList(java.util.ArrayList) List(java.util.List) Map(java.util.Map) TreeMap(java.util.TreeMap)

Aggregations

ReadIndexStatus (com.alipay.sofa.jraft.entity.ReadIndexStatus)3 ArrayList (java.util.ArrayList)3 List (java.util.List)2 Status (com.alipay.sofa.jraft.Status)1 ReadIndexClosure (com.alipay.sofa.jraft.closure.ReadIndexClosure)1 ReadIndexState (com.alipay.sofa.jraft.entity.ReadIndexState)1 Bytes (com.alipay.sofa.jraft.util.Bytes)1 Map (java.util.Map)1 TreeMap (java.util.TreeMap)1 CountDownLatch (java.util.concurrent.CountDownLatch)1 Test (org.junit.Test)1