Search in sources :

Example 1 with CRC64

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

the class JDKZipStrategyTest method zipTest.

@Test
public void zipTest() throws Throwable {
    final String rootPath = this.sourceDir.toPath().toAbsolutePath().getParent().toString();
    final Path outPath = Paths.get(rootPath, "kv.zip");
    final Checksum c1 = new CRC64();
    zipStrategy.compress(rootPath, "zip_test", outPath.toString(), c1);
    System.out.println(Long.toHexString(c1.getValue()));
    final Checksum c2 = new CRC64();
    zipStrategy.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 2 with CRC64

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

the class ParallelZipStrategyTest method zipTest.

@Test
public void zipTest() throws Throwable {
    final String rootPath = this.sourceDir.toPath().toAbsolutePath().getParent().toString();
    final Path outPath = Paths.get(rootPath, "kv.zip");
    final Checksum c1 = new CRC64();
    zipStrategy.compress(rootPath, "zip_test", outPath.toString(), c1);
    System.out.println(Long.toHexString(c1.getValue()));
    final Checksum c2 = new CRC64();
    zipStrategy.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 3 with CRC64

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

the class AbstractKVStoreSnapshotFile method decompressSnapshot.

protected void decompressSnapshot(final String readerPath, final LocalFileMeta meta) throws Throwable {
    final String sourceFile = Paths.get(readerPath, SNAPSHOT_ARCHIVE).toString();
    final Checksum checksum = new CRC64();
    ZipStrategyManager.getDefault().deCompress(sourceFile, readerPath, checksum);
    if (meta.hasChecksum()) {
        Requires.requireTrue(meta.getChecksum().equals(Long.toHexString(checksum.getValue())), "Snapshot checksum failed");
    }
}
Also used : CRC64(com.alipay.sofa.jraft.util.CRC64) Checksum(java.util.zip.Checksum) ByteString(com.google.protobuf.ByteString)

Example 4 with CRC64

use of com.alipay.sofa.jraft.util.CRC64 in project incubator-hugegraph by apache.

the class StoreSnapshotFile method decompressSnapshot.

private String decompressSnapshot(SnapshotReader reader, String snapshotDirTar) throws IOException {
    LocalFileMeta meta = (LocalFileMeta) reader.getFileMeta(snapshotDirTar);
    if (meta == null) {
        throw new IOException("Can't find snapshot archive file, path=" + snapshotDirTar);
    }
    String diskTableKey = meta.getUserMeta().toStringUtf8();
    E.checkArgument(this.dataDisks.containsKey(diskTableKey), "The data path for '%s' should be exist", diskTableKey);
    String dataPath = this.dataDisks.get(diskTableKey);
    String parentPath = Paths.get(dataPath).getParent().toString();
    String snapshotDir = Paths.get(parentPath, StringUtils.removeEnd(snapshotDirTar, TAR)).toString();
    FileUtils.deleteDirectory(new File(snapshotDir));
    LOG.info("Delete stale snapshot dir {}", snapshotDir);
    Checksum checksum = new CRC64();
    String archiveFile = Paths.get(reader.getPath(), snapshotDirTar).toString();
    CompressUtil.decompressTar(archiveFile, parentPath, checksum);
    if (meta.hasChecksum()) {
        String expected = meta.getChecksum();
        String actual = Long.toHexString(checksum.getValue());
        E.checkArgument(expected.equals(actual), "Snapshot checksum error: '%s' != '%s'", actual, expected);
    }
    return snapshotDir;
}
Also used : CRC64(com.alipay.sofa.jraft.util.CRC64) Checksum(java.util.zip.Checksum) IOException(java.io.IOException) ByteString(com.google.protobuf.ByteString) LocalFileMeta(com.alipay.sofa.jraft.entity.LocalFileMetaOutter.LocalFileMeta) File(java.io.File)

Example 5 with CRC64

use of com.alipay.sofa.jraft.util.CRC64 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)

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