Search in sources :

Example 6 with CRC64

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);
}
Also used : CRC64(com.alipay.sofa.jraft.util.CRC64) Checksum(java.util.zip.Checksum) InputStream(java.io.InputStream) LocalFileMeta(com.alibaba.nacos.consistency.snapshot.LocalFileMeta)

Example 7 with CRC64

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);
        }
    });
}
Also used : CRC64(com.alipay.sofa.jraft.util.CRC64) Checksum(java.util.zip.Checksum) LocalFileMeta(com.alibaba.nacos.consistency.snapshot.LocalFileMeta) ReentrantReadWriteLock(java.util.concurrent.locks.ReentrantReadWriteLock) Lock(java.util.concurrent.locks.Lock)

Example 8 with CRC64

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);
    }
}
Also used : CRC64(com.alipay.sofa.jraft.util.CRC64) Checksum(java.util.zip.Checksum) LocalFileMeta(com.alibaba.nacos.consistency.snapshot.LocalFileMeta) File(java.io.File) ReentrantReadWriteLock(java.util.concurrent.locks.ReentrantReadWriteLock) Lock(java.util.concurrent.locks.Lock)

Example 9 with CRC64

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);
}
Also used : CRC64(com.alipay.sofa.jraft.util.CRC64) Checksum(java.util.zip.Checksum) LocalFileMeta(com.alibaba.nacos.consistency.snapshot.LocalFileMeta)

Example 10 with CRC64

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;
}
Also used : CRC64(com.alipay.sofa.jraft.util.CRC64) Checksum(java.util.zip.Checksum) LocalFileMeta(com.alibaba.nacos.consistency.snapshot.LocalFileMeta)

Aggregations

CRC64 (com.alipay.sofa.jraft.util.CRC64)19 Checksum (java.util.zip.Checksum)18 LocalFileMeta (com.alibaba.nacos.consistency.snapshot.LocalFileMeta)6 File (java.io.File)5 Test (org.junit.Test)5 ByteString (com.google.protobuf.ByteString)4 LocalFileMeta (com.alipay.sofa.jraft.entity.LocalFileMetaOutter.LocalFileMeta)3 Path (java.nio.file.Path)3 Lock (java.util.concurrent.locks.Lock)2 ReentrantReadWriteLock (java.util.concurrent.locks.ReentrantReadWriteLock)2 LocalFileMeta (org.monkey.mmq.core.consistency.snapshot.LocalFileMeta)2 Status (com.alipay.sofa.jraft.Status)1 RocksRawKVStore (com.alipay.sofa.jraft.rhea.storage.RocksRawKVStore)1 IOException (java.io.IOException)1 InputStream (java.io.InputStream)1 HashMap (java.util.HashMap)1 Map (java.util.Map)1