Search in sources :

Example 1 with ProtoBufFile

use of com.alipay.sofa.jraft.storage.io.ProtoBufFile in project sofa-jraft by sofastack.

the class CheckpointFile method load.

public Checkpoint load() throws IOException {
    final ProtoBufFile file = new ProtoBufFile(this.path);
    final LocalFileMeta meta = file.load();
    if (meta != null) {
        final byte[] data = meta.getUserMeta().toByteArray();
        Checkpoint checkpoint = new Checkpoint(null, -1);
        if (checkpoint.decode(data)) {
            return checkpoint;
        }
    }
    return null;
}
Also used : LocalFileMeta(com.alipay.sofa.jraft.entity.LocalFileMetaOutter.LocalFileMeta) ProtoBufFile(com.alipay.sofa.jraft.storage.io.ProtoBufFile)

Example 2 with ProtoBufFile

use of com.alipay.sofa.jraft.storage.io.ProtoBufFile in project sofa-jraft by sofastack.

the class LocalRaftMetaStorage method save.

private boolean save() {
    final long start = Utils.monotonicMs();
    final StablePBMeta meta = // 
    StablePBMeta.newBuilder().setTerm(// 
    this.term).setVotedfor(// 
    this.votedFor.toString()).build();
    final ProtoBufFile pbFile = newPbFile();
    try {
        if (!pbFile.save(meta, this.raftOptions.isSyncMeta())) {
            reportIOError();
            return false;
        }
        return true;
    } catch (final Exception e) {
        LOG.error("Fail to save raft meta", e);
        reportIOError();
        return false;
    } finally {
        final long cost = Utils.monotonicMs() - start;
        if (this.nodeMetrics != null) {
            this.nodeMetrics.recordLatency("save-raft-meta", cost);
        }
        LOG.info("Save raft meta, path={}, term={}, votedFor={}, cost time={} ms", this.path, this.term, this.votedFor, cost);
    }
}
Also used : StablePBMeta(com.alipay.sofa.jraft.entity.LocalStorageOutter.StablePBMeta) ProtoBufFile(com.alipay.sofa.jraft.storage.io.ProtoBufFile) IOException(java.io.IOException) FileNotFoundException(java.io.FileNotFoundException) RaftException(com.alipay.sofa.jraft.error.RaftException)

Example 3 with ProtoBufFile

use of com.alipay.sofa.jraft.storage.io.ProtoBufFile in project sofa-jraft by sofastack.

the class CheckpointFile method save.

public synchronized boolean save(final Checkpoint checkpoint) throws IOException {
    final ProtoBufFile file = new ProtoBufFile(this.path);
    final byte[] data = checkpoint.encode();
    final LocalFileMeta meta = // 
    LocalFileMeta.newBuilder().setUserMeta(// 
    ZeroByteStringHelper.wrap(data)).build();
    return file.save(meta, true);
}
Also used : LocalFileMeta(com.alipay.sofa.jraft.entity.LocalFileMetaOutter.LocalFileMeta) ProtoBufFile(com.alipay.sofa.jraft.storage.io.ProtoBufFile)

Example 4 with ProtoBufFile

use of com.alipay.sofa.jraft.storage.io.ProtoBufFile in project sofa-jraft by sofastack.

the class LocalSnapshotMetaTable method loadFromFile.

/**
 * Load metadata infos from a file by path.
 */
public boolean loadFromFile(String path) throws IOException {
    ProtoBufFile pbFile = new ProtoBufFile(path);
    LocalSnapshotPbMeta pbMeta = pbFile.load();
    if (pbMeta == null) {
        LOG.error("Fail to load meta from {}.", path);
        return false;
    }
    return loadFromPbMeta(pbMeta);
}
Also used : LocalSnapshotPbMeta(com.alipay.sofa.jraft.entity.LocalStorageOutter.LocalSnapshotPbMeta) ProtoBufFile(com.alipay.sofa.jraft.storage.io.ProtoBufFile)

Example 5 with ProtoBufFile

use of com.alipay.sofa.jraft.storage.io.ProtoBufFile in project sofa-jraft by sofastack.

the class LocalSnapshotMetaTable method saveToFile.

/**
 * Save metadata infos into file by path.
 */
public boolean saveToFile(String path) throws IOException {
    LocalSnapshotPbMeta.Builder pbMeta = LocalSnapshotPbMeta.newBuilder();
    if (hasMeta()) {
        pbMeta.setMeta(this.meta);
    }
    for (Map.Entry<String, LocalFileMeta> entry : this.fileMap.entrySet()) {
        File f = File.newBuilder().setName(entry.getKey()).setMeta(entry.getValue()).build();
        pbMeta.addFiles(f);
    }
    ProtoBufFile pbFile = new ProtoBufFile(path);
    return pbFile.save(pbMeta.build(), this.raftOptions.isSyncMeta());
}
Also used : LocalSnapshotPbMeta(com.alipay.sofa.jraft.entity.LocalStorageOutter.LocalSnapshotPbMeta) LocalFileMeta(com.alipay.sofa.jraft.entity.LocalFileMetaOutter.LocalFileMeta) HashMap(java.util.HashMap) Map(java.util.Map) ProtoBufFile(com.alipay.sofa.jraft.storage.io.ProtoBufFile) File(com.alipay.sofa.jraft.entity.LocalStorageOutter.LocalSnapshotPbMeta.File) ProtoBufFile(com.alipay.sofa.jraft.storage.io.ProtoBufFile)

Aggregations

ProtoBufFile (com.alipay.sofa.jraft.storage.io.ProtoBufFile)5 LocalFileMeta (com.alipay.sofa.jraft.entity.LocalFileMetaOutter.LocalFileMeta)3 LocalSnapshotPbMeta (com.alipay.sofa.jraft.entity.LocalStorageOutter.LocalSnapshotPbMeta)2 File (com.alipay.sofa.jraft.entity.LocalStorageOutter.LocalSnapshotPbMeta.File)1 StablePBMeta (com.alipay.sofa.jraft.entity.LocalStorageOutter.StablePBMeta)1 RaftException (com.alipay.sofa.jraft.error.RaftException)1 FileNotFoundException (java.io.FileNotFoundException)1 IOException (java.io.IOException)1 HashMap (java.util.HashMap)1 Map (java.util.Map)1