Search in sources :

Example 1 with UserLog

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

the class NodeImpl method readCommittedUserLog.

@Override
public UserLog readCommittedUserLog(final long index) {
    if (index <= 0) {
        throw new LogIndexOutOfBoundsException("Request index is invalid: " + index);
    }
    final long savedLastAppliedIndex = this.fsmCaller.getLastAppliedIndex();
    if (index > savedLastAppliedIndex) {
        throw new LogIndexOutOfBoundsException("Request index " + index + " is greater than lastAppliedIndex: " + savedLastAppliedIndex);
    }
    long curIndex = index;
    LogEntry entry = this.logManager.getEntry(curIndex);
    if (entry == null) {
        throw new LogNotFoundException("User log is deleted at index: " + index);
    }
    do {
        if (entry.getType() == EnumOutter.EntryType.ENTRY_TYPE_DATA) {
            return new UserLog(curIndex, entry.getData());
        } else {
            curIndex++;
        }
        if (curIndex > savedLastAppliedIndex) {
            throw new IllegalStateException("No user log between index:" + index + " and last_applied_index:" + savedLastAppliedIndex);
        }
        entry = this.logManager.getEntry(curIndex);
    } while (entry != null);
    throw new LogNotFoundException("User log is deleted at index: " + curIndex);
}
Also used : UserLog(org.apache.ignite.raft.jraft.entity.UserLog) LogIndexOutOfBoundsException(org.apache.ignite.raft.jraft.error.LogIndexOutOfBoundsException) LogNotFoundException(org.apache.ignite.raft.jraft.error.LogNotFoundException) LogEntry(org.apache.ignite.raft.jraft.entity.LogEntry)

Example 2 with UserLog

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

the class ItNodeTest method readCommittedUserLog.

@Test
public void readCommittedUserLog() throws Exception {
    // setup cluster
    List<PeerId> peers = TestUtils.generatePeers(3);
    cluster = new TestCluster("unitest", dataPath, peers, ELECTION_TIMEOUT_MILLIS, testInfo);
    for (PeerId peer : peers) assertTrue(cluster.start(peer.getEndpoint()));
    cluster.waitLeader();
    Node leader = cluster.getLeader();
    assertNotNull(leader);
    cluster.ensureLeader(leader);
    int amount = 10;
    sendTestTaskAndWait(leader, amount);
    assertTrue(waitForCondition(() -> {
        try {
            // index == 1 is a CONFIGURATION log
            UserLog userLog = leader.readCommittedUserLog(1 + amount);
            return userLog != null;
        } catch (Exception ignore) {
            // is updated, so we need to wait.
            return false;
        }
    }, 10_000));
    // index == 1 is a CONFIGURATION log, so real_index will be 2 when returned.
    UserLog userLog = leader.readCommittedUserLog(1);
    assertNotNull(userLog);
    assertEquals(2, userLog.getIndex());
    assertEquals("hello0", stringFromBytes(userLog.getData().array()));
    // index == 5 is a DATA log(a user log)
    userLog = leader.readCommittedUserLog(5);
    assertNotNull(userLog);
    assertEquals(5, userLog.getIndex());
    assertEquals("hello3", stringFromBytes(userLog.getData().array()));
    // index == 15 is greater than last_committed_index
    try {
        assertNull(leader.readCommittedUserLog(15));
        fail();
    } catch (LogIndexOutOfBoundsException e) {
        assertEquals("Request index 15 is greater than lastAppliedIndex: 11", e.getMessage());
    }
    // index == 0 invalid request
    try {
        assertNull(leader.readCommittedUserLog(0));
        fail();
    } catch (LogIndexOutOfBoundsException e) {
        assertEquals("Request index is invalid: 0", e.getMessage());
    }
    LOG.info("Trigger leader snapshot");
    CountDownLatch latch = new CountDownLatch(1);
    leader.snapshot(new ExpectClosure(latch));
    waitLatch(latch);
    // remove and add a peer to add two CONFIGURATION logs
    List<Node> followers = cluster.getFollowers();
    assertEquals(2, followers.size());
    Node testFollower = followers.get(0);
    latch = new CountDownLatch(1);
    leader.removePeer(testFollower.getNodeId().getPeerId(), new ExpectClosure(latch));
    waitLatch(latch);
    latch = new CountDownLatch(1);
    leader.addPeer(testFollower.getNodeId().getPeerId(), new ExpectClosure(latch));
    waitLatch(latch);
    sendTestTaskAndWait(leader, amount, RaftError.SUCCESS);
    // trigger leader snapshot for the second time, after this the log of index 1~11 will be deleted.
    LOG.info("Trigger leader snapshot");
    latch = new CountDownLatch(1);
    leader.snapshot(new ExpectClosure(latch));
    waitLatch(latch);
    Thread.sleep(100);
    // index == 5 log has been deleted in log_storage.
    try {
        leader.readCommittedUserLog(5);
        fail();
    } catch (LogNotFoundException e) {
        assertEquals("User log is deleted at index: 5", e.getMessage());
    }
    // index == 12、index == 13、index=14、index=15 are 4 CONFIGURATION logs(joint consensus), so real_index will be 16 when returned.
    userLog = leader.readCommittedUserLog(12);
    assertNotNull(userLog);
    assertEquals(16, userLog.getIndex());
    assertEquals("hello10", stringFromBytes(userLog.getData().array()));
    // now index == 17 is a user log
    userLog = leader.readCommittedUserLog(17);
    assertNotNull(userLog);
    assertEquals(17, userLog.getIndex());
    assertEquals("hello11", stringFromBytes(userLog.getData().array()));
    cluster.ensureSame();
    assertEquals(3, cluster.getFsms().size());
    for (MockStateMachine fsm : cluster.getFsms()) {
        assertEquals(20, fsm.getLogs().size());
        for (int i = 0; i < 20; i++) assertEquals("hello" + i, stringFromBytes(fsm.getLogs().get(i).array()));
    }
}
Also used : Node(org.apache.ignite.raft.jraft.Node) UserLog(org.apache.ignite.raft.jraft.entity.UserLog) LogIndexOutOfBoundsException(org.apache.ignite.raft.jraft.error.LogIndexOutOfBoundsException) CountDownLatch(java.util.concurrent.CountDownLatch) Endpoint(org.apache.ignite.raft.jraft.util.Endpoint) RaftException(org.apache.ignite.raft.jraft.error.RaftException) LogIndexOutOfBoundsException(org.apache.ignite.raft.jraft.error.LogIndexOutOfBoundsException) LogNotFoundException(org.apache.ignite.raft.jraft.error.LogNotFoundException) LogNotFoundException(org.apache.ignite.raft.jraft.error.LogNotFoundException) PeerId(org.apache.ignite.raft.jraft.entity.PeerId) Test(org.junit.jupiter.api.Test)

Aggregations

UserLog (org.apache.ignite.raft.jraft.entity.UserLog)2 LogIndexOutOfBoundsException (org.apache.ignite.raft.jraft.error.LogIndexOutOfBoundsException)2 LogNotFoundException (org.apache.ignite.raft.jraft.error.LogNotFoundException)2 CountDownLatch (java.util.concurrent.CountDownLatch)1 Node (org.apache.ignite.raft.jraft.Node)1 LogEntry (org.apache.ignite.raft.jraft.entity.LogEntry)1 PeerId (org.apache.ignite.raft.jraft.entity.PeerId)1 RaftException (org.apache.ignite.raft.jraft.error.RaftException)1 Endpoint (org.apache.ignite.raft.jraft.util.Endpoint)1 Test (org.junit.jupiter.api.Test)1