Search in sources :

Example 1 with ReadIndexStatus

use of org.apache.ignite.raft.jraft.entity.ReadIndexStatus in project ignite-3 by apache.

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(org.apache.ignite.raft.jraft.Status) ReadIndexStatus(org.apache.ignite.raft.jraft.entity.ReadIndexStatus) Bytes(org.apache.ignite.raft.jraft.util.Bytes) ReadIndexClosure(org.apache.ignite.raft.jraft.closure.ReadIndexClosure) ReadIndexStatus(org.apache.ignite.raft.jraft.entity.ReadIndexStatus) ReadIndexState(org.apache.ignite.raft.jraft.entity.ReadIndexState) ArrayList(java.util.ArrayList) CountDownLatch(java.util.concurrent.CountDownLatch) Test(org.junit.jupiter.api.Test)

Example 2 with ReadIndexStatus

use of org.apache.ignite.raft.jraft.entity.ReadIndexStatus in project ignite-3 by apache.

the class ReadOnlyServiceImpl method onApplied.

/**
 * Called when lastAppliedIndex updates.
 *
 * @param appliedIndex applied index
 */
@Override
public void onApplied(final long appliedIndex) {
    // TODO reuse pendingStatuses list? https://issues.apache.org/jira/browse/IGNITE-14832
    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(org.apache.ignite.raft.jraft.entity.ReadIndexStatus) ArrayList(java.util.ArrayList) List(java.util.List) Map(java.util.Map) TreeMap(java.util.TreeMap)

Aggregations

ArrayList (java.util.ArrayList)2 ReadIndexStatus (org.apache.ignite.raft.jraft.entity.ReadIndexStatus)2 List (java.util.List)1 Map (java.util.Map)1 TreeMap (java.util.TreeMap)1 CountDownLatch (java.util.concurrent.CountDownLatch)1 Status (org.apache.ignite.raft.jraft.Status)1 ReadIndexClosure (org.apache.ignite.raft.jraft.closure.ReadIndexClosure)1 ReadIndexState (org.apache.ignite.raft.jraft.entity.ReadIndexState)1 Bytes (org.apache.ignite.raft.jraft.util.Bytes)1 Test (org.junit.jupiter.api.Test)1