use of org.apache.ignite.raft.jraft.util.ArrayDeque in project ignite-3 by apache.
the class LocalSnapshotCopier method filterBeforeCopy.
boolean filterBeforeCopy(final LocalSnapshotWriter writer, final SnapshotReader lastSnapshot) throws IOException {
final Set<String> existingFiles = writer.listFiles();
final ArrayDeque<String> toRemove = new ArrayDeque<>();
for (final String file : existingFiles) {
if (this.remoteSnapshot.getFileMeta(file) == null) {
toRemove.add(file);
writer.removeFile(file);
}
}
final Set<String> remoteFiles = this.remoteSnapshot.listFiles();
for (final String fileName : remoteFiles) {
final LocalFileMeta remoteMeta = (LocalFileMeta) this.remoteSnapshot.getFileMeta(fileName);
Requires.requireNonNull(remoteMeta, "remoteMeta");
if (remoteMeta.checksum() != null) {
// Re-download file if this file doesn't have checksum
writer.removeFile(fileName);
toRemove.add(fileName);
continue;
}
LocalFileMeta localMeta = (LocalFileMeta) writer.getFileMeta(fileName);
if (localMeta != null) {
if (localMeta.checksum() != null && localMeta.checksum().equals(remoteMeta.checksum())) {
LOG.info("Keep file={} checksum={} in {}", fileName, remoteMeta.checksum(), writer.getPath());
continue;
}
// Remove files from writer so that the file is to be copied from
// remote_snapshot or last_snapshot
writer.removeFile(fileName);
toRemove.add(fileName);
}
// Try find files in last_snapshot
if (lastSnapshot == null) {
continue;
}
if ((localMeta = (LocalFileMeta) lastSnapshot.getFileMeta(fileName)) == null) {
continue;
}
if (localMeta.checksum() == null || !localMeta.checksum().equals(remoteMeta.checksum())) {
continue;
}
LOG.info("Found the same file ={} checksum={} in lastSnapshot={}", fileName, remoteMeta.checksum(), lastSnapshot.getPath());
if (localMeta.source() == FileSource.FILE_SOURCE_LOCAL) {
final Path sourcePath = Paths.get(lastSnapshot.getPath(), fileName);
final Path destPath = Paths.get(writer.getPath(), fileName);
IgniteUtils.deleteIfExists(destPath);
try {
Files.createLink(destPath, sourcePath);
} catch (final IOException e) {
LOG.error("Fail to link {} to {}", e, sourcePath, destPath);
continue;
}
// Don't delete linked file
if (!toRemove.isEmpty() && toRemove.peekLast().equals(fileName)) {
toRemove.pollLast();
}
}
// Copy file from last_snapshot
writer.addFile(fileName, localMeta);
}
if (!writer.sync()) {
LOG.error("Fail to sync writer on path={}", writer.getPath());
return false;
}
for (final String fileName : toRemove) {
final Path removePath = Paths.get(writer.getPath(), fileName);
IgniteUtils.deleteIfExists(removePath);
LOG.info("Deleted file: {}", removePath);
}
return true;
}
Aggregations