Search in sources :

Example 1 with StateStoreException

use of org.apache.bookkeeper.statelib.api.exceptions.StateStoreException in project bookkeeper by apache.

the class RocksdbKVStore method openRocksdb.

protected static Pair<RocksDB, List<ColumnFamilyHandle>> openRocksdb(File dir, DBOptions options, ColumnFamilyOptions cfOpts) throws StateStoreException {
    // make sure the db directory's parent dir is created
    ColumnFamilyDescriptor metaDesc = new ColumnFamilyDescriptor(METADATA_CF, cfOpts);
    ColumnFamilyDescriptor dataDesc = new ColumnFamilyDescriptor(DATA_CF, cfOpts);
    try {
        Files.createDirectories(dir.toPath());
        File dbDir = new File(dir, "current");
        if (!dbDir.exists()) {
            // empty state
            String uuid = UUID.randomUUID().toString();
            Path checkpointPath = Paths.get(dir.getAbsolutePath(), "checkpoints", uuid);
            Files.createDirectories(checkpointPath);
            Files.createSymbolicLink(Paths.get(dbDir.getAbsolutePath()), checkpointPath);
        }
        List<ColumnFamilyHandle> cfHandles = Lists.newArrayListWithExpectedSize(2);
        RocksDB db = RocksDB.open(options, dbDir.getAbsolutePath(), Lists.newArrayList(metaDesc, dataDesc), cfHandles);
        return Pair.of(db, cfHandles);
    } catch (IOException ioe) {
        log.error("Failed to create parent directory {} for opening rocksdb", dir.getParentFile().toPath(), ioe);
        throw new StateStoreException(ioe);
    } catch (RocksDBException dbe) {
        log.error("Failed to open rocksdb at dir {}", dir.getAbsolutePath(), dbe);
        throw new StateStoreException(dbe);
    }
}
Also used : Path(java.nio.file.Path) StateStoreException(org.apache.bookkeeper.statelib.api.exceptions.StateStoreException) InvalidStateStoreException(org.apache.bookkeeper.statelib.api.exceptions.InvalidStateStoreException) RocksDBException(org.rocksdb.RocksDBException) RocksDB(org.rocksdb.RocksDB) IOException(java.io.IOException) ColumnFamilyDescriptor(org.rocksdb.ColumnFamilyDescriptor) File(java.io.File) ColumnFamilyHandle(org.rocksdb.ColumnFamilyHandle)

Example 2 with StateStoreException

use of org.apache.bookkeeper.statelib.api.exceptions.StateStoreException in project bookkeeper by apache.

the class RocksCheckpointer method restore.

public static CheckpointMetadata restore(String dbName, File dbPath, CheckpointStore checkpointStore) throws StateStoreException {
    try {
        String dbPrefix = String.format("%s", dbName);
        Pair<String, CheckpointMetadata> latestCheckpoint = getLatestCheckpoint(dbPrefix, checkpointStore);
        File checkpointsDir = new File(dbPath, "checkpoints");
        String checkpointId = latestCheckpoint.getLeft();
        CheckpointMetadata checkpointMetadata = latestCheckpoint.getRight();
        if (checkpointId != null) {
            RocksdbRestoreTask task = new RocksdbRestoreTask(dbName, checkpointsDir, checkpointStore);
            task.restore(checkpointId, checkpointMetadata);
        } else {
            // no checkpoints available, create an empty directory
            checkpointId = UUID.randomUUID().toString();
            Files.createDirectories(Paths.get(checkpointsDir.getAbsolutePath(), checkpointId));
        }
        Path restoredCheckpointPath = Paths.get(checkpointsDir.getAbsolutePath(), checkpointId);
        log.info("Successfully restore checkpoint {} to {}", checkpointId, restoredCheckpointPath);
        File currentDir = new File(dbPath, "current");
        Files.deleteIfExists(Paths.get(currentDir.getAbsolutePath()));
        Files.createSymbolicLink(Paths.get(currentDir.getAbsolutePath()), restoredCheckpointPath);
        // after successfully restore from remote checkpoints, cleanup other unused checkpoints
        cleanupLocalCheckpoints(checkpointsDir, checkpointId);
        return checkpointMetadata;
    } catch (IOException ioe) {
        log.error("Failed to restore rocksdb {}", dbName, ioe);
        throw new StateStoreException("Failed to restore rocksdb " + dbName, ioe);
    }
}
Also used : Path(java.nio.file.Path) StateStoreException(org.apache.bookkeeper.statelib.api.exceptions.StateStoreException) IOException(java.io.IOException) File(java.io.File) CheckpointMetadata(org.apache.bookkeeper.stream.proto.kv.store.CheckpointMetadata)

Example 3 with StateStoreException

use of org.apache.bookkeeper.statelib.api.exceptions.StateStoreException in project bookkeeper by apache.

the class RocksdbRestoreTask method restore.

public void restore(String checkpointId, CheckpointMetadata metadata) throws StateStoreException {
    File checkpointedDir = new File(checkpointDir, checkpointId);
    try {
        List<String> filesToCopy = getFilesToCopy(checkpointId, checkpointedDir, metadata);
        copyFilesFromRemote(checkpointId, checkpointedDir, filesToCopy);
    } catch (IOException ioe) {
        log.error("Failed to restore checkpoint {} to local directory {}", new Object[] { checkpointId, checkpointedDir, ioe });
        throw new StateStoreException("Failed to restore checkpoint " + checkpointId + " to local directory " + checkpointedDir, ioe);
    }
}
Also used : StateStoreException(org.apache.bookkeeper.statelib.api.exceptions.StateStoreException) IOException(java.io.IOException) File(java.io.File)

Example 4 with StateStoreException

use of org.apache.bookkeeper.statelib.api.exceptions.StateStoreException in project bookkeeper by apache.

the class RocksdbKVStore method readLastRevision.

private void readLastRevision() throws StateStoreException {
    byte[] revisionBytes;
    try {
        revisionBytes = db.get(metaCfHandle, LAST_REVISION);
    } catch (RocksDBException e) {
        throw new StateStoreException("Failed to read last revision from state store " + name(), e);
    }
    if (null == revisionBytes) {
        return;
    }
    long revision = Bytes.toLong(revisionBytes, 0);
    lastRevisionUpdater.set(this, revision);
}
Also used : StateStoreException(org.apache.bookkeeper.statelib.api.exceptions.StateStoreException) InvalidStateStoreException(org.apache.bookkeeper.statelib.api.exceptions.InvalidStateStoreException) RocksDBException(org.rocksdb.RocksDBException)

Example 5 with StateStoreException

use of org.apache.bookkeeper.statelib.api.exceptions.StateStoreException in project bookkeeper by apache.

the class RocksdbCheckpointTask method checkpoint.

public String checkpoint(byte[] txid) throws StateStoreException {
    String checkpointId = UUID.randomUUID().toString();
    File tempDir = new File(checkpointDir, checkpointId);
    log.info("Create a local checkpoint of state store {} at {}", dbName, tempDir);
    try {
        try {
            checkpoint.createCheckpoint(tempDir.getAbsolutePath());
        } catch (RocksDBException e) {
            throw new StateStoreException("Failed to create a checkpoint at " + tempDir, e);
        }
        String remoteCheckpointPath = RocksUtils.getDestCheckpointPath(dbPrefix, checkpointId);
        if (!checkpointStore.fileExists(remoteCheckpointPath)) {
            checkpointStore.createDirectories(remoteCheckpointPath);
        }
        String sstsPath = RocksUtils.getDestSstsPath(dbPrefix);
        if (!checkpointStore.fileExists(sstsPath)) {
            checkpointStore.createDirectories(sstsPath);
        }
        // get the files to copy
        List<File> filesToCopy = getFilesToCopy(tempDir);
        // copy the files
        copyFilesToDest(checkpointId, filesToCopy);
        // finalize copy files
        finalizeCopyFiles(checkpointId, filesToCopy);
        // dump the file list to checkpoint file
        finalizeCheckpoint(checkpointId, tempDir, txid);
        // clean up the remote checkpoints
        if (removeRemoteCheckpointsAfterSuccessfulCheckpoint) {
            cleanupRemoteCheckpoints(tempDir, checkpointId);
        }
        return checkpointId;
    } catch (IOException ioe) {
        log.error("Failed to checkpoint db {} to dir {}", new Object[] { dbName, tempDir, ioe });
        throw new StateStoreException("Failed to checkpoint db " + dbName + " to dir " + tempDir, ioe);
    } finally {
        if (removeLocalCheckpointAfterSuccessfulCheckpoint && tempDir.exists()) {
            try {
                MoreFiles.deleteRecursively(Paths.get(tempDir.getAbsolutePath()), RecursiveDeleteOption.ALLOW_INSECURE);
            } catch (IOException ioe) {
                log.warn("Failed to remove temporary checkpoint dir {}", tempDir, ioe);
            }
        }
    }
}
Also used : StateStoreException(org.apache.bookkeeper.statelib.api.exceptions.StateStoreException) RocksDBException(org.rocksdb.RocksDBException) IOException(java.io.IOException) File(java.io.File)

Aggregations

StateStoreException (org.apache.bookkeeper.statelib.api.exceptions.StateStoreException)5 File (java.io.File)4 IOException (java.io.IOException)4 RocksDBException (org.rocksdb.RocksDBException)3 Path (java.nio.file.Path)2 InvalidStateStoreException (org.apache.bookkeeper.statelib.api.exceptions.InvalidStateStoreException)2 CheckpointMetadata (org.apache.bookkeeper.stream.proto.kv.store.CheckpointMetadata)1 ColumnFamilyDescriptor (org.rocksdb.ColumnFamilyDescriptor)1 ColumnFamilyHandle (org.rocksdb.ColumnFamilyHandle)1 RocksDB (org.rocksdb.RocksDB)1