Search in sources :

Example 1 with LocalFileMeta

use of org.monkey.mmq.core.consistency.snapshot.LocalFileMeta 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)

Example 2 with LocalFileMeta

use of org.monkey.mmq.core.consistency.snapshot.LocalFileMeta in project mmqtt by MrHKing.

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

Example 3 with LocalFileMeta

use of org.monkey.mmq.core.consistency.snapshot.LocalFileMeta in project mmqtt by MrHKing.

the class MmqStateMachine method adapterToJRaftSnapshot.

private void adapterToJRaftSnapshot(Collection<SnapshotOperation> userOperates) {
    List<JSnapshotOperation> tmp = new ArrayList<>();
    for (SnapshotOperation item : userOperates) {
        if (item == null) {
            Loggers.RAFT.error("Existing SnapshotOperation for null");
            continue;
        }
        tmp.add(new JSnapshotOperation() {

            @Override
            public void onSnapshotSave(SnapshotWriter writer, Closure done) {
                final Writer wCtx = new Writer(writer.getPath());
                // Do a layer of proxy operation to shield different Raft
                // components from implementing snapshots
                final BiConsumer<Boolean, Throwable> callFinally = (result, t) -> {
                    boolean[] results = new boolean[wCtx.listFiles().size()];
                    int[] index = new int[] { 0 };
                    wCtx.listFiles().forEach((file, meta) -> {
                        try {
                            results[index[0]++] = writer.addFile(file, buildMetadata(meta));
                        } catch (Exception e) {
                            throw new ConsistencyException(e);
                        }
                    });
                    final Status status = result && BooleanUtils.and(results) ? Status.OK() : new Status(RaftError.EIO, "Fail to compress snapshot at %s, error is %s", writer.getPath(), t == null ? "" : t.getMessage());
                    done.run(status);
                };
                item.onSnapshotSave(wCtx, callFinally);
            }

            @Override
            public boolean onSnapshotLoad(SnapshotReader reader) {
                final Map<String, LocalFileMeta> metaMap = new HashMap<>(reader.listFiles().size());
                for (String fileName : reader.listFiles()) {
                    final LocalFileMetaOutter.LocalFileMeta meta = (LocalFileMetaOutter.LocalFileMeta) reader.getFileMeta(fileName);
                    byte[] bytes = meta.getUserMeta().toByteArray();
                    final LocalFileMeta fileMeta;
                    if (bytes == null || bytes.length == 0) {
                        fileMeta = new LocalFileMeta();
                    } else {
                        fileMeta = JacksonUtils.toObj(bytes, LocalFileMeta.class);
                    }
                    metaMap.put(fileName, fileMeta);
                }
                final Reader rCtx = new Reader(reader.getPath(), metaMap);
                return item.onSnapshotLoad(rCtx);
            }

            @Override
            public String info() {
                return item.toString();
            }
        });
    }
    this.operations = Collections.unmodifiableList(tmp);
}
Also used : LocalFileMetaOutter(com.alipay.sofa.jraft.entity.LocalFileMetaOutter) Reader(org.monkey.mmq.core.consistency.snapshot.Reader) SnapshotReader(com.alipay.sofa.jraft.storage.snapshot.SnapshotReader) ConsistencyException(org.monkey.mmq.core.distributed.raft.exception.ConsistencyException) RaftException(com.alipay.sofa.jraft.error.RaftException) SnapshotOperation(org.monkey.mmq.core.consistency.snapshot.SnapshotOperation) SnapshotWriter(com.alipay.sofa.jraft.storage.snapshot.SnapshotWriter) ConsistencyException(org.monkey.mmq.core.distributed.raft.exception.ConsistencyException) SnapshotReader(com.alipay.sofa.jraft.storage.snapshot.SnapshotReader) LocalFileMeta(org.monkey.mmq.core.consistency.snapshot.LocalFileMeta) Writer(org.monkey.mmq.core.consistency.snapshot.Writer) SnapshotWriter(com.alipay.sofa.jraft.storage.snapshot.SnapshotWriter) BiConsumer(java.util.function.BiConsumer)

Aggregations

LocalFileMeta (org.monkey.mmq.core.consistency.snapshot.LocalFileMeta)3 CRC64 (com.alipay.sofa.jraft.util.CRC64)2 Checksum (java.util.zip.Checksum)2 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 BiConsumer (java.util.function.BiConsumer)1 Reader (org.monkey.mmq.core.consistency.snapshot.Reader)1 SnapshotOperation (org.monkey.mmq.core.consistency.snapshot.SnapshotOperation)1 Writer (org.monkey.mmq.core.consistency.snapshot.Writer)1 ConsistencyException (org.monkey.mmq.core.distributed.raft.exception.ConsistencyException)1