Java Code Examples for com.alipay.sofa.jraft.storage.snapshot.SnapshotReader#getPath()

The following examples show how to use com.alipay.sofa.jraft.storage.snapshot.SnapshotReader#getPath() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: AtomicStateMachine.java    From sofa-jraft with Apache License 2.0 6 votes vote down vote up
@Override
public boolean onSnapshotLoad(final SnapshotReader reader) {
    if (isLeader()) {
        LOG.warn("Leader is not supposed to load snapshot");
        return false;
    }
    if (reader.getFileMeta("data") == null) {
        LOG.error("Fail to find data file in {}", reader.getPath());
        return false;
    }
    final AtomicSnapshotFile snapshot = new AtomicSnapshotFile(reader.getPath() + File.separator + "data");
    try {
        final Map<String, Long> values = snapshot.load();
        this.counters.clear();
        if (values != null) {
            for (final Map.Entry<String, Long> entry : values.entrySet()) {
                this.counters.put(entry.getKey(), new AtomicLong(entry.getValue()));
            }
        }
        return true;
    } catch (final IOException e) {
        LOG.error("Fail to load snapshot from {}", snapshot.getPath());
        return false;
    }

}
 
Example 2
Source File: CounterStateMachine.java    From sofa-jraft with Apache License 2.0 6 votes vote down vote up
@Override
public boolean onSnapshotLoad(final SnapshotReader reader) {
    if (isLeader()) {
        LOG.warn("Leader is not supposed to load snapshot");
        return false;
    }
    if (reader.getFileMeta("data") == null) {
        LOG.error("Fail to find data file in {}", reader.getPath());
        return false;
    }
    final CounterSnapshotFile snapshot = new CounterSnapshotFile(reader.getPath() + File.separator + "data");
    try {
        this.value.set(snapshot.load());
        return true;
    } catch (final IOException e) {
        LOG.error("Fail to load snapshot from {}", snapshot.getPath());
        return false;
    }

}
 
Example 3
Source File: AbstractKVStoreSnapshotFile.java    From sofa-jraft with Apache License 2.0 5 votes vote down vote up
@Override
public boolean load(final SnapshotReader reader, final Region region) {
    final LocalFileMeta meta = (LocalFileMeta) reader.getFileMeta(SNAPSHOT_ARCHIVE);
    final String readerPath = reader.getPath();
    if (meta == null) {
        LOG.error("Can't find kv snapshot file, path={}.", readerPath);
        return false;
    }
    final String snapshotPath = Paths.get(readerPath, SNAPSHOT_DIR).toString();
    try {
        decompressSnapshot(readerPath, meta);
        doSnapshotLoad(snapshotPath, meta, region);
        final File tmp = new File(snapshotPath);
        // Delete the decompressed temporary file. If the deletion fails (although it is a small probability
        // event), it may affect the next snapshot decompression result. Therefore, the safest way is to
        // terminate the state machine immediately. Users can choose to manually delete and restart according
        // to the log information.
        if (tmp.exists()) {
            FileUtils.forceDelete(new File(snapshotPath));
        }
        return true;
    } catch (final Throwable t) {
        LOG.error("Fail to load snapshot, path={}, file list={}, {}.", readerPath, reader.listFiles(),
            StackTraceUtil.stackTrace(t));
        return false;
    }
}
 
Example 4
Source File: MockStateMachine.java    From sofa-jraft with Apache License 2.0 5 votes vote down vote up
@Override
public boolean onSnapshotLoad(final SnapshotReader reader) {
    this.lastAppliedIndex.set(0);
    this.loadSnapshotTimes++;
    final String path = reader.getPath() + File.separator + "data";
    final File file = new File(path);
    if (!file.exists()) {
        return false;
    }
    try (FileInputStream fin = new FileInputStream(file); BufferedInputStream in = new BufferedInputStream(fin)) {
        this.lock.lock();
        this.logs.clear();
        try {
            while (true) {
                final byte[] bs = new byte[4];
                if (in.read(bs) == 4) {
                    final int len = Bits.getInt(bs, 0);
                    final byte[] buf = new byte[len];
                    if (in.read(buf) != len) {
                        break;
                    }
                    this.logs.add(ByteBuffer.wrap(buf));
                } else {
                    break;
                }
            }
        } finally {
            this.lock.unlock();
        }
        System.out.println("Node<" + this.address + "> loaded snapshot from " + path);
        return true;
    } catch (final IOException e) {
        e.printStackTrace();
        return false;
    }
}
 
Example 5
Source File: MockStateMachine.java    From sofa-jraft with Apache License 2.0 5 votes vote down vote up
@Override
public boolean onSnapshotLoad(final SnapshotReader reader) {
    this.lastAppliedIndex.set(0);
    this.loadSnapshotTimes++;
    final String path = reader.getPath() + File.separator + "data";
    final File file = new File(path);
    if (!file.exists()) {
        return false;
    }
    try (FileInputStream fin = new FileInputStream(file); BufferedInputStream in = new BufferedInputStream(fin)) {
        this.lock.lock();
        this.logs.clear();
        try {
            while (true) {
                final byte[] bs = new byte[4];
                if (in.read(bs) == 4) {
                    final int len = Bits.getInt(bs, 0);
                    final byte[] buf = new byte[len];
                    if (in.read(buf) != len) {
                        break;
                    }
                    this.logs.add(ByteBuffer.wrap(buf));
                } else {
                    break;
                }
            }
        } finally {
            this.lock.unlock();
        }
        System.out.println("Node<" + this.address + "> loaded snapshot from " + path);
        return true;
    } catch (final IOException e) {
        e.printStackTrace();
        return false;
    }
}
 
Example 6
Source File: ServiceStateMachine.java    From sofa-registry with Apache License 2.0 4 votes vote down vote up
@Override
public boolean onSnapshotLoad(SnapshotReader reader) {
    if (isLeader()) {
        LOG.warn("Leader is not supposed to load snapshot");
        return false;
    }
    List<String> failServices = new ArrayList<>();
    Map<String, Object> workers = Processor.getInstance().getWorkers();
    if (workers != null) {
        outer: for (Map.Entry<String, Object> entry : workers.entrySet()) {
            String serviceId = entry.getKey();
            Object worker = entry.getValue();
            if (worker instanceof SnapshotProcess) {
                SnapshotProcess snapshotProcess = (SnapshotProcess) worker;
                Set<String> fileNames = snapshotProcess.getSnapshotFileNames();

                for (String fileName : fileNames) {
                    if (reader.getFileMeta(fileName) == null) {
                        LOG.error("Fail to find data file {} in {}", fileName, reader.getPath());
                        failServices.add(serviceId);
                        break outer;
                    }

                    String savePath = reader.getPath() + File.separator + fileName;
                    LOG.info("Begin load snapshot path {}", savePath);
                    boolean ret = snapshotProcess.load(savePath);
                    if (!ret) {
                        LOG.error("Fail to load service:{} snapshot {}", serviceId, savePath);
                        failServices.add(serviceId);
                        break outer;
                    }
                }
            }
        }
    }

    if (!failServices.isEmpty()) {
        LOG.error("Fail to load services {} snapshot!", failServices);
        return false;
    }
    return true;
}