use of org.apache.ratis.server.storage.RaftStorageDirectory.LogPathAndIndex in project incubator-ratis by apache.
the class SegmentedRaftLog method loadLogSegments.
private void loadLogSegments(long lastIndexInSnapshot, Consumer<LogEntryProto> logConsumer) throws IOException {
try (AutoCloseableLock writeLock = writeLock()) {
List<LogPathAndIndex> paths = storage.getStorageDir().getLogSegmentFiles();
int i = 0;
for (LogPathAndIndex pi : paths) {
boolean isOpen = pi.endIndex == RaftServerConstants.INVALID_LOG_INDEX;
// During the initial loading, we can only confirm the committed
// index based on the snapshot. This means if a log segment is not kept
// in cache after the initial loading, later we have to load its content
// again for updating the state machine.
// TODO we should let raft peer persist its committed index periodically
// so that during the initial loading we can apply part of the log
// entries to the state machine
boolean keepEntryInCache = (paths.size() - i++) <= cache.getMaxCachedSegments();
cache.loadSegment(pi, isOpen, keepEntryInCache, logConsumer);
}
// committing the log and taking snapshot)
if (!cache.isEmpty() && cache.getEndIndex() < lastIndexInSnapshot) {
LOG.warn("End log index {} is smaller than last index in snapshot {}", cache.getEndIndex(), lastIndexInSnapshot);
cache.clear();
// TODO purge all segment files
}
}
}
use of org.apache.ratis.server.storage.RaftStorageDirectory.LogPathAndIndex in project incubator-ratis by apache.
the class RaftSnapshotBaseTest method testBasicInstallSnapshot.
/**
* Basic test for install snapshot: start a one node cluster and let it
* generate a snapshot. Then delete the log and restart the node, and add more
* nodes as followers.
*/
@Test
public void testBasicInstallSnapshot() throws Exception {
List<LogPathAndIndex> logs;
try {
RaftTestUtil.waitForLeader(cluster);
final RaftPeerId leaderId = cluster.getLeader().getId();
int i = 0;
try (final RaftClient client = cluster.createClient(leaderId)) {
for (; i < SNAPSHOT_TRIGGER_THRESHOLD * 2 - 1; i++) {
RaftClientReply reply = client.send(new SimpleMessage("m" + i));
Assert.assertTrue(reply.isSuccess());
}
}
// wait for the snapshot to be done
RaftStorageDirectory storageDirectory = cluster.getLeader().getState().getStorage().getStorageDir();
final File snapshotFile = getSnapshotFile(cluster, i);
logs = storageDirectory.getLogSegmentFiles();
int retries = 0;
do {
Thread.sleep(1000);
} while (!snapshotFile.exists() && retries++ < 10);
Assert.assertTrue(snapshotFile + " does not exist", snapshotFile.exists());
} finally {
cluster.shutdown();
}
// delete the log segments from the leader
for (LogPathAndIndex path : logs) {
FileUtils.deleteFile(path.path.toFile());
}
// restart the peer
LOG.info("Restarting the cluster");
cluster.restart(false);
try {
assertLeaderContent(cluster);
// generate some more traffic
try (final RaftClient client = cluster.createClient(cluster.getLeader().getId())) {
Assert.assertTrue(client.send(new SimpleMessage("test")).isSuccess());
}
// add two more peers
MiniRaftCluster.PeerChanges change = cluster.addNewPeers(new String[] { "s3", "s4" }, true);
// trigger setConfiguration
cluster.setConfiguration(change.allPeersInNewConf);
RaftServerTestUtil.waitAndCheckNewConf(cluster, change.allPeersInNewConf, 0, null);
} finally {
cluster.shutdown();
}
}
Aggregations