Search in sources :

Example 11 with CRC64

use of com.alipay.sofa.jraft.util.CRC64 in project sofa-jraft by sofastack.

the class ZipUtilTest method zipTest.

@Test
public void zipTest() throws IOException {
    final String rootPath = this.sourceDir.toPath().toAbsolutePath().getParent().toString();
    final Path outPath = Paths.get(rootPath, "kv.zip");
    final Checksum c1 = new CRC64();
    ZipUtil.compress(rootPath, "zip_test", outPath.toString(), c1);
    System.out.println(Long.toHexString(c1.getValue()));
    final Checksum c2 = new CRC64();
    ZipUtil.decompress(Paths.get(rootPath, "kv.zip").toString(), rootPath, c2);
    Assert.assertEquals(c1.getValue(), c2.getValue());
    FileUtils.forceDelete(outPath.toFile());
}
Also used : Path(java.nio.file.Path) CRC64(com.alipay.sofa.jraft.util.CRC64) Checksum(java.util.zip.Checksum) Test(org.junit.Test)

Example 12 with CRC64

use of com.alipay.sofa.jraft.util.CRC64 in project sofa-jraft by sofastack.

the class SnapshotBenchmark method doCompressSnapshot.

private void doCompressSnapshot(final String path, final LocalFileMeta.Builder metaBuilder) {
    final String outputFile = Paths.get(path, SNAPSHOT_ARCHIVE).toString();
    try {
        final Checksum checksum = new CRC64();
        ZipUtil.compress(path, SNAPSHOT_DIR, outputFile, checksum);
        metaBuilder.setChecksum(Long.toHexString(checksum.getValue()));
    } catch (final Throwable t) {
        t.printStackTrace();
    }
}
Also used : CRC64(com.alipay.sofa.jraft.util.CRC64) Checksum(java.util.zip.Checksum)

Example 13 with CRC64

use of com.alipay.sofa.jraft.util.CRC64 in project sofa-jraft by sofastack.

the class SnapshotBenchmark method snapshot.

public void snapshot(final boolean isSstSnapshot, final boolean isFastSnapshot) throws IOException {
    final File backupDir = new File("backup");
    if (backupDir.exists()) {
        FileUtils.deleteDirectory(backupDir);
    }
    FileUtils.forceMkdir(backupDir);
    final LocalFileMeta meta = doSnapshotSave(backupDir.getAbsolutePath(), isSstSnapshot, isFastSnapshot);
    this.kvStore.shutdown();
    FileUtils.deleteDirectory(new File(this.tempPath));
    FileUtils.forceMkdir(new File(this.tempPath));
    this.kvStore = new RocksRawKVStore();
    this.kvStore.init(this.dbOptions);
    final String name;
    if (isSstSnapshot) {
        name = "sst";
    } else {
        if (isFastSnapshot) {
            name = "fast";
        } else {
            name = "slow";
        }
    }
    final long decompressStart = System.nanoTime();
    final String sourceFile = Paths.get(backupDir.getAbsolutePath(), SNAPSHOT_ARCHIVE).toString();
    ZipUtil.decompress(sourceFile, backupDir.getAbsolutePath(), new CRC64());
    System.out.println(name + " decompress time cost: " + TimeUnit.NANOSECONDS.toMillis(System.nanoTime() - decompressStart));
    final long loadStart = System.nanoTime();
    doSnapshotLoad(backupDir.getAbsolutePath(), meta, isFastSnapshot);
    System.out.println(name + " load snapshot time cost: " + TimeUnit.NANOSECONDS.toMillis(System.nanoTime() - loadStart));
    FileUtils.deleteDirectory(backupDir);
}
Also used : CRC64(com.alipay.sofa.jraft.util.CRC64) RocksRawKVStore(com.alipay.sofa.jraft.rhea.storage.RocksRawKVStore) LocalFileMeta(com.alipay.sofa.jraft.entity.LocalFileMetaOutter.LocalFileMeta) File(java.io.File)

Example 14 with CRC64

use of com.alipay.sofa.jraft.util.CRC64 in project sofa-jraft by sofastack.

the class AbstractKVStoreSnapshotFile method compressSnapshot.

protected void compressSnapshot(final SnapshotWriter writer, final LocalFileMeta.Builder metaBuilder, final Closure done) {
    final String writerPath = writer.getPath();
    final String outputFile = Paths.get(writerPath, SNAPSHOT_ARCHIVE).toString();
    try {
        final Checksum checksum = new CRC64();
        ZipStrategyManager.getDefault().compress(writerPath, SNAPSHOT_DIR, outputFile, checksum);
        metaBuilder.setChecksum(Long.toHexString(checksum.getValue()));
        if (writer.addFile(SNAPSHOT_ARCHIVE, metaBuilder.build())) {
            done.run(Status.OK());
        } else {
            done.run(new Status(RaftError.EIO, "Fail to add snapshot file: %s", writerPath));
        }
    } catch (final Throwable t) {
        LOG.error("Fail to compress snapshot, path={}, file list={}, {}.", writerPath, writer.listFiles(), StackTraceUtil.stackTrace(t));
        done.run(new Status(RaftError.EIO, "Fail to compress snapshot at %s, error is %s", writerPath, t.getMessage()));
    }
}
Also used : Status(com.alipay.sofa.jraft.Status) CRC64(com.alipay.sofa.jraft.util.CRC64) Checksum(java.util.zip.Checksum) ByteString(com.google.protobuf.ByteString)

Example 15 with CRC64

use of com.alipay.sofa.jraft.util.CRC64 in project mmqtt by MrHKing.

the class MmqSnapshotOperation 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(org.monkey.mmq.core.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