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);
}
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()));
}
}
Aggregations