use of com.alipay.sofa.jraft.util.CRC64 in project nacos by alibaba.
the class AbstractMetadataSnapshotOperation method writeSnapshot.
@Override
protected boolean writeSnapshot(Writer writer) throws IOException {
final String writePath = writer.getPath();
final String outputFile = Paths.get(writePath, getSnapshotArchive()).toString();
final Checksum checksum = new CRC64();
try (InputStream inputStream = dumpSnapshot()) {
DiskUtils.compressIntoZipFile(METADATA_CHILD_NAME, inputStream, outputFile, checksum);
}
final LocalFileMeta meta = new LocalFileMeta();
meta.append(CHECK_SUM_KEY, Long.toHexString(checksum.getValue()));
return writer.addFile(getSnapshotArchive(), meta);
}
use of com.alipay.sofa.jraft.util.CRC64 in project nacos by alibaba.
the class DerbySnapshotOperation method onSnapshotSave.
@Override
public void onSnapshotSave(Writer writer, BiConsumer<Boolean, Throwable> callFinally) {
RaftExecutor.doSnapshot(() -> {
TimerContext.start(DERBY_SNAPSHOT_SAVE);
final Lock lock = writeLock;
lock.lock();
try {
final String writePath = writer.getPath();
final String parentPath = Paths.get(writePath, snapshotDir).toString();
DiskUtils.deleteDirectory(parentPath);
DiskUtils.forceMkdir(parentPath);
doDerbyBackup(parentPath);
final String outputFile = Paths.get(writePath, snapshotArchive).toString();
final Checksum checksum = new CRC64();
DiskUtils.compress(writePath, snapshotDir, outputFile, checksum);
DiskUtils.deleteDirectory(parentPath);
final LocalFileMeta meta = new LocalFileMeta();
meta.append(checkSumKey, Long.toHexString(checksum.getValue()));
callFinally.accept(writer.addFile(snapshotArchive, meta), null);
} catch (Throwable t) {
LogUtil.FATAL_LOG.error("Fail to compress snapshot, path={}, file list={}, {}.", writer.getPath(), writer.listFiles(), t);
callFinally.accept(false, t);
} finally {
lock.unlock();
TimerContext.end(DERBY_SNAPSHOT_SAVE, LogUtil.FATAL_LOG);
}
});
}
use of com.alipay.sofa.jraft.util.CRC64 in project nacos by alibaba.
the class DerbySnapshotOperation method onSnapshotLoad.
@Override
public boolean onSnapshotLoad(Reader reader) {
final String readerPath = reader.getPath();
final String sourceFile = Paths.get(readerPath, snapshotArchive).toString();
TimerContext.start(DERBY_SNAPSHOT_LOAD);
final Lock lock = writeLock;
lock.lock();
try {
final Checksum checksum = new CRC64();
DiskUtils.decompress(sourceFile, readerPath, checksum);
LocalFileMeta fileMeta = reader.getFileMeta(snapshotArchive);
if (fileMeta.getFileMeta().containsKey(checkSumKey)) {
if (!Objects.equals(Long.toHexString(checksum.getValue()), fileMeta.get(checkSumKey))) {
throw new IllegalArgumentException("Snapshot checksum failed");
}
}
final String loadPath = Paths.get(readerPath, snapshotDir, "derby-data").toString();
LogUtil.FATAL_LOG.info("snapshot load from : {}, and copy to : {}", loadPath, derbyBaseDir);
doDerbyRestoreFromBackup(() -> {
final File srcDir = new File(loadPath);
final File destDir = new File(derbyBaseDir);
DiskUtils.copyDirectory(srcDir, destDir);
LogUtil.FATAL_LOG.info("Complete database recovery");
return null;
});
DiskUtils.deleteDirectory(loadPath);
NotifyCenter.publishEvent(DerbyLoadEvent.INSTANCE);
return true;
} catch (final Throwable t) {
LogUtil.FATAL_LOG.error("Fail to load snapshot, path={}, file list={}, {}.", readerPath, reader.listFiles(), t);
return false;
} finally {
lock.unlock();
TimerContext.end(DERBY_SNAPSHOT_LOAD, LogUtil.FATAL_LOG);
}
}
use of com.alipay.sofa.jraft.util.CRC64 in project nacos by alibaba.
the class NamingSnapshotOperation method writeSnapshot.
@Override
protected boolean writeSnapshot(Writer writer) throws Exception {
final String writePath = writer.getPath();
final String parentPath = Paths.get(writePath, snapshotDir).toString();
DiskUtils.deleteDirectory(parentPath);
DiskUtils.forceMkdir(parentPath);
storage.doSnapshot(parentPath);
final String outputFile = Paths.get(writePath, snapshotArchive).toString();
final Checksum checksum = new CRC64();
DiskUtils.compress(writePath, snapshotDir, outputFile, checksum);
DiskUtils.deleteDirectory(parentPath);
final LocalFileMeta meta = new LocalFileMeta();
meta.append(CHECK_SUM_KEY, Long.toHexString(checksum.getValue()));
return writer.addFile(snapshotArchive, meta);
}
use of com.alipay.sofa.jraft.util.CRC64 in project nacos by alibaba.
the class NamingSnapshotOperation method readSnapshot.
@Override
protected boolean readSnapshot(Reader reader) throws Exception {
final String readerPath = reader.getPath();
final String sourceFile = Paths.get(readerPath, snapshotArchive).toString();
final Checksum checksum = new CRC64();
DiskUtils.decompress(sourceFile, readerPath, checksum);
LocalFileMeta fileMeta = reader.getFileMeta(snapshotArchive);
if (fileMeta.getFileMeta().containsKey(CHECK_SUM_KEY)) {
if (!Objects.equals(Long.toHexString(checksum.getValue()), fileMeta.get(CHECK_SUM_KEY))) {
throw new IllegalArgumentException("Snapshot checksum failed");
}
}
final String loadPath = Paths.get(readerPath, snapshotDir).toString();
storage.snapshotLoad(loadPath);
Loggers.RAFT.info("snapshot load from : {}", loadPath);
DiskUtils.deleteDirectory(loadPath);
return true;
}
Aggregations