@NotThreadSafe public final class RocksStore extends Object implements Closeable
For operations like closing/restart/restoring on the RocksDB, an exclusive lock should be acquired by calling one of: 1.try (RocksSharedLockHandle r = mRocksStore.checkAndAcquireSharedLock() { // perform your read/write operation }
lockForClosing()
2. lockForRewrite()
3. lockForCheckpoint()
Rule of thumb:
1. Use the proper locking methods when you access RocksDB.
2. Make each operation short. Make the locked section short.
3. If you have to make the operation long (like iteration), utilize shouldAbort(int)
to check and abort voluntarily.
See Javadoc on the locking methods for details.Modifier and Type | Field and Description |
---|---|
LongAdder |
mRefCount |
AtomicStampedReference<Boolean> |
mRocksDbStopServing |
static java.time.Duration |
ROCKS_CLOSE_WAIT_TIMEOUT |
static int |
ROCKS_OPEN_RETRY_TIMEOUT |
Constructor and Description |
---|
RocksStore(String name,
String dbPath,
String checkpointPath,
org.rocksdb.DBOptions dbOpts,
Collection<org.rocksdb.ColumnFamilyDescriptor> columnFamilyDescriptors,
List<AtomicReference<org.rocksdb.ColumnFamilyHandle>> columnHandles,
boolean reset) |
Modifier and Type | Method and Description |
---|---|
RocksSharedLockHandle |
checkAndAcquireSharedLock()
This is the core logic of the shared lock mechanism.
|
static Optional<org.rocksdb.BlockBasedTableConfig> |
checkSetTableConfig(PropertyKey cacheSize,
PropertyKey bloomFilter,
PropertyKey indexType,
PropertyKey blockIndexType,
List<org.rocksdb.RocksObject> toClose)
loads RockDB configuration options based on property key configurations.
|
void |
clear()
Clears and re-initializes the database.
|
void |
close() |
org.rocksdb.RocksDB |
getDb()
Requires the caller to acquire a shared lock by calling
checkAndAcquireSharedLock() . |
long |
getSharedLockCount()
Gets the number of shared lock on the RocksStore.
|
boolean |
isServiceStopping()
Checks whether the RocksDB is marked for exclusive access, so the operation should abort.
|
RocksExclusiveLockHandle |
lockForCheckpoint()
Before the process shuts down, acquire an exclusive lock on the RocksDB before closing.
|
RocksExclusiveLockHandle |
lockForClosing()
Before the process shuts down, acquire an exclusive lock on the RocksDB before closing.
|
RocksExclusiveLockHandle |
lockForRewrite()
Before the process shuts down, acquire an exclusive lock on the RocksDB before closing.
|
void |
restoreFromCheckpoint(CheckpointInputStream input)
Restores the database from a checkpoint.
|
void |
restoreFromCheckpoint(File directory)
Restores RocksDB state from a checkpoint at the provided location.
|
void |
shouldAbort(int lockedVersion)
Used by ongoing r/w operations to check if the operation needs to abort and yield
to the RocksDB shutdown.
|
void |
writeToCheckpoint(File directory)
Writes a checkpoint under the specified directory.
|
void |
writeToCheckpoint(OutputStream output)
Writes a checkpoint of the database's content to the given output stream.
|
public static final int ROCKS_OPEN_RETRY_TIMEOUT
public static final java.time.Duration ROCKS_CLOSE_WAIT_TIMEOUT
public final AtomicStampedReference<Boolean> mRocksDbStopServing
public volatile LongAdder mRefCount
public RocksStore(String name, String dbPath, String checkpointPath, org.rocksdb.DBOptions dbOpts, Collection<org.rocksdb.ColumnFamilyDescriptor> columnFamilyDescriptors, List<AtomicReference<org.rocksdb.ColumnFamilyHandle>> columnHandles, boolean reset)
name
- a name to distinguish what store this isdbPath
- a path for the rocks databasecheckpointPath
- a path for taking database checkpointsdbOpts
- the configured RocksDB optionscolumnFamilyDescriptors
- columns to create within the rocks databasecolumnHandles
- column handle references to populatereset
- if true, the RocksDB will be cleaned up and then new DB will be createdpublic org.rocksdb.RocksDB getDb()
checkAndAcquireSharedLock()
.public void clear()
lockForRewrite()
.public void writeToCheckpoint(File directory) throws org.rocksdb.RocksDBException
directory
- that the checkpoint will be written underorg.rocksdb.RocksDBException
- if it encounters and error when writing the checkpointpublic void writeToCheckpoint(OutputStream output) throws IOException, InterruptedException
lockForCheckpoint()
.output
- the stream to write toIOException
InterruptedException
public void restoreFromCheckpoint(File directory) throws org.rocksdb.RocksDBException, IOException
directory
- where the checkpoint is locatedorg.rocksdb.RocksDBException
- if rocks encounters a problemIOException
- if moving files around encounters a problempublic void restoreFromCheckpoint(CheckpointInputStream input) throws IOException
lockForRewrite()
.input
- the checkpoint stream to restore fromIOException
public void close()
close
in interface Closeable
close
in interface AutoCloseable
public static Optional<org.rocksdb.BlockBasedTableConfig> checkSetTableConfig(PropertyKey cacheSize, PropertyKey bloomFilter, PropertyKey indexType, PropertyKey blockIndexType, List<org.rocksdb.RocksObject> toClose)
cacheSize
- integer PropertyKey for cache sizebloomFilter
- boolean PropertyKey for bloom filterindexType
- enum PropertyKey for index typeblockIndexType
- enum PropertyKey for block index typetoClose
- the objects to be closedpublic RocksSharedLockHandle checkAndAcquireSharedLock()
PropertyKey.MASTER_METASTORE_ROCKS_EXCLUSIVE_LOCK_TIMEOUT
.
After this timeout, the exclusive lock will be forced, and the shared lock holders
are disrespected. Normally, the r/w operation should either complete or abort within
seconds so the timeout PropertyKey.MASTER_METASTORE_ROCKS_EXCLUSIVE_LOCK_TIMEOUT
should not be exceeded at all.public RocksExclusiveLockHandle lockForClosing()
public RocksExclusiveLockHandle lockForCheckpoint()
checkAndAcquireSharedLock()
for how this affects the shared lock logic.public RocksExclusiveLockHandle lockForRewrite()
checkAndAcquireSharedLock()
for how this affects the shared lock logic.public void shouldAbort(int lockedVersion)
lockedVersion
- The RocksDB version from the shared lock. This version is used to tell
if a restore or clear operation has happened on the RocksDB.public boolean isServiceStopping()
public long getSharedLockCount()
Copyright © 2023. All Rights Reserved.