Search in sources :

Example 1 with Session

use of com.alipay.sofa.jraft.storage.snapshot.remote.Session in project sofa-jraft by sofastack.

the class LocalSnapshotCopier method loadMetaTable.

private void loadMetaTable() throws InterruptedException {
    final ByteBufferCollector metaBuf = ByteBufferCollector.allocate(0);
    Session session = null;
    try {
        this.lock.lock();
        try {
            if (this.cancelled) {
                if (isOk()) {
                    setError(RaftError.ECANCELED, "ECANCELED");
                }
                return;
            }
            session = this.copier.startCopy2IoBuffer(Snapshot.JRAFT_SNAPSHOT_META_FILE, metaBuf, null);
            this.curSession = session;
        } finally {
            this.lock.unlock();
        }
        // join out of lock.
        session.join();
        this.lock.lock();
        try {
            this.curSession = null;
        } finally {
            this.lock.unlock();
        }
        if (!session.status().isOk() && isOk()) {
            LOG.warn("Fail to copy meta file: {}", session.status());
            setError(session.status().getCode(), session.status().getErrorMsg());
            return;
        }
        if (!this.remoteSnapshot.getMetaTable().loadFromIoBufferAsRemote(metaBuf.getBuffer())) {
            LOG.warn("Bad meta_table format");
            setError(-1, "Bad meta_table format from remote");
            return;
        }
        Requires.requireTrue(this.remoteSnapshot.getMetaTable().hasMeta(), "Invalid remote snapshot meta:%s", this.remoteSnapshot.getMetaTable().getMeta());
    } finally {
        if (session != null) {
            Utils.closeQuietly(session);
        }
    }
}
Also used : ByteBufferCollector(com.alipay.sofa.jraft.util.ByteBufferCollector) Session(com.alipay.sofa.jraft.storage.snapshot.remote.Session)

Example 2 with Session

use of com.alipay.sofa.jraft.storage.snapshot.remote.Session in project sofa-jraft by sofastack.

the class LocalSnapshotCopier method copyFile.

void copyFile(final String fileName) throws IOException, InterruptedException {
    if (this.writer.getFileMeta(fileName) != null) {
        LOG.info("Skipped downloading {}", fileName);
        return;
    }
    if (!checkFile(fileName)) {
        return;
    }
    final String filePath = this.writer.getPath() + File.separator + fileName;
    final Path subPath = Paths.get(filePath);
    if (!subPath.equals(subPath.getParent()) && !subPath.getParent().getFileName().toString().equals(".")) {
        final File parentDir = subPath.getParent().toFile();
        if (!parentDir.exists() && !parentDir.mkdirs()) {
            LOG.error("Fail to create directory for {}", filePath);
            setError(RaftError.EIO, "Fail to create directory");
            return;
        }
    }
    final LocalFileMeta meta = (LocalFileMeta) this.remoteSnapshot.getFileMeta(fileName);
    Session session = null;
    try {
        this.lock.lock();
        try {
            if (this.cancelled) {
                if (isOk()) {
                    setError(RaftError.ECANCELED, "ECANCELED");
                }
                return;
            }
            session = this.copier.startCopyToFile(fileName, filePath, null);
            if (session == null) {
                LOG.error("Fail to copy {}", fileName);
                setError(-1, "Fail to copy %s", fileName);
                return;
            }
            this.curSession = session;
        } finally {
            this.lock.unlock();
        }
        // join out of lock
        session.join();
        this.lock.lock();
        try {
            this.curSession = null;
        } finally {
            this.lock.unlock();
        }
        if (!session.status().isOk() && isOk()) {
            setError(session.status().getCode(), session.status().getErrorMsg());
            return;
        }
        if (!this.writer.addFile(fileName, meta)) {
            setError(RaftError.EIO, "Fail to add file to writer");
            return;
        }
        if (!this.writer.sync()) {
            setError(RaftError.EIO, "Fail to sync writer");
        }
    } finally {
        if (session != null) {
            Utils.closeQuietly(session);
        }
    }
}
Also used : Path(java.nio.file.Path) LocalFileMeta(com.alipay.sofa.jraft.entity.LocalFileMetaOutter.LocalFileMeta) File(java.io.File) Session(com.alipay.sofa.jraft.storage.snapshot.remote.Session)

Aggregations

Session (com.alipay.sofa.jraft.storage.snapshot.remote.Session)2 LocalFileMeta (com.alipay.sofa.jraft.entity.LocalFileMetaOutter.LocalFileMeta)1 ByteBufferCollector (com.alipay.sofa.jraft.util.ByteBufferCollector)1 File (java.io.File)1 Path (java.nio.file.Path)1