Search in sources :

Example 1 with LocalFileMeta

use of com.alibaba.nacos.consistency.snapshot.LocalFileMeta in project nacos by alibaba.

the class AbstractMetadataSnapshotOperation method readSnapshot.

@Override
protected boolean readSnapshot(Reader reader) throws Exception {
    final String readerPath = reader.getPath();
    final String sourceFile = Paths.get(readerPath, getSnapshotArchive()).toString();
    final Checksum checksum = new CRC64();
    byte[] snapshotBytes = DiskUtils.decompress(sourceFile, checksum);
    LocalFileMeta fileMeta = reader.getFileMeta(getSnapshotArchive());
    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");
        }
    }
    loadSnapshot(snapshotBytes);
    return true;
}
Also used : CRC64(com.alipay.sofa.jraft.util.CRC64) Checksum(java.util.zip.Checksum) LocalFileMeta(com.alibaba.nacos.consistency.snapshot.LocalFileMeta)

Example 2 with LocalFileMeta

use of com.alibaba.nacos.consistency.snapshot.LocalFileMeta 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 3 with LocalFileMeta

use of com.alibaba.nacos.consistency.snapshot.LocalFileMeta 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 4 with LocalFileMeta

use of com.alibaba.nacos.consistency.snapshot.LocalFileMeta 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 5 with LocalFileMeta

use of com.alibaba.nacos.consistency.snapshot.LocalFileMeta 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)

Aggregations

LocalFileMeta (com.alibaba.nacos.consistency.snapshot.LocalFileMeta)7 CRC64 (com.alipay.sofa.jraft.util.CRC64)6 Checksum (java.util.zip.Checksum)6 Lock (java.util.concurrent.locks.Lock)2 ReentrantReadWriteLock (java.util.concurrent.locks.ReentrantReadWriteLock)2 ConsistencyException (com.alibaba.nacos.consistency.exception.ConsistencyException)1 Reader (com.alibaba.nacos.consistency.snapshot.Reader)1 SnapshotOperation (com.alibaba.nacos.consistency.snapshot.SnapshotOperation)1 Writer (com.alibaba.nacos.consistency.snapshot.Writer)1 Closure (com.alipay.sofa.jraft.Closure)1 Status (com.alipay.sofa.jraft.Status)1 LocalFileMetaOutter (com.alipay.sofa.jraft.entity.LocalFileMetaOutter)1 RaftException (com.alipay.sofa.jraft.error.RaftException)1 SnapshotReader (com.alipay.sofa.jraft.storage.snapshot.SnapshotReader)1 SnapshotWriter (com.alipay.sofa.jraft.storage.snapshot.SnapshotWriter)1 File (java.io.File)1 InputStream (java.io.InputStream)1 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 Map (java.util.Map)1