Class SnapshotKVDatabase
- All Implemented Interfaces:
KVDatabase
- Direct Known Subclasses:
ArrayKVDatabase
,LevelDBKVDatabase
,MVStoreKVDatabase
KVDatabase
implementation based on an underlying AtomicKVStore
using
snapshot views and optimistic locking to provide concurrent
transactions and linearizable ACID consistency.
Instances implement a simple optimistic locking scheme for MVCC using AtomicKVStore.readOnlySnapshot()
. Concurrent
transactions do not contend for any locks until commit time. During each transaction, reads are noted and derive from the
snapshot, while writes are batched up. At commit time, if any other transaction has committed writes since the transaction's
snapshot was created, and any of those writes conflict with any of the committing
transaction's reads, a RetryKVTransactionException
is thrown. Otherwise, the transaction is committed and its
writes are applied.
Each outstanding transaction's mutations are batched up in memory using a Writes
instance. Therefore,
the transaction load supported by this class is limited to what can fit in memory.
Key watches are supported.
-
Field Summary
-
Constructor Summary
-
Method Summary
Modifier and TypeMethodDescriptionprotected void
Forcibly fail all outstanding transactions due tostop()
being invoked.protected SnapshotKVTransaction
createSnapshotKVTransaction
(MutableView view, long baseVersion) Instantiate a newSnapshotKVTransaction
instance.Create a new transaction.createTransaction
(Map<String, ?> options) Create a new transaction with the specified options.long
Get the current MVCC version number.protected AtomicKVStore
Get the underlyingAtomicKVStore
.protected KVTransactionException
Log specified exception.protected void
setKVStore
(AtomicKVStore kvstore) Configure the underlyingAtomicKVStore
.void
start()
Start this instance.void
stop()
Stop this instance.toString()
protected RuntimeException
Wrap aRuntimeException
as needed.
-
Field Details
-
log
-
-
Constructor Details
-
SnapshotKVDatabase
public SnapshotKVDatabase()Default constructor.The underlying key/value store must still be configured before starting this instance.
-
SnapshotKVDatabase
Constructor.- Parameters:
kvstore
- underlying key/value store
-
-
Method Details
-
getKVStore
Get the underlyingAtomicKVStore
.- Returns:
- underlying key/value store
-
setKVStore
Configure the underlyingAtomicKVStore
.Required property; must be configured before
start()
ing.- Parameters:
kvstore
- underlying key/value store- Throws:
IllegalStateException
- if this instance is already started
-
getCurrentVersion
public long getCurrentVersion()Get the current MVCC version number.- Returns:
- MVCC database version number
-
start
@PostConstruct public void start()Description copied from interface:KVDatabase
Start this instance. This method must be called prior to creating any transactions.This method is idempotent: if this instance is already started, nothing happens.
Whether an instance that has been started and stopped can be restarted is implementation-dependent.
- Specified by:
start
in interfaceKVDatabase
-
stop
@PreDestroy public void stop()Description copied from interface:KVDatabase
Stop this instance.This method is idempotent: if this instance has not been started, or is already stopped, nothing happens.
- Specified by:
stop
in interfaceKVDatabase
-
createTransaction
Description copied from interface:KVDatabase
Create a new transaction with the specified options.- Specified by:
createTransaction
in interfaceKVDatabase
- Parameters:
options
- optional transaction options; may be null- Returns:
- newly created transaction
-
createTransaction
Create a new transaction.- Specified by:
createTransaction
in interfaceKVDatabase
- Returns:
- newly created transaction
- Throws:
IllegalStateException
- if notstart()
ed orstop()
ing
-
toString
-
createSnapshotKVTransaction
Instantiate a newSnapshotKVTransaction
instance.The implementation in
SnapshotKVDatabase
just invokes theSnapshotKVTransaction
constructor usingthis
. Subclasses may want to override this method to create a more specific subclass.- Parameters:
view
- mutable view to be used for this transactionbaseVersion
- the database version associated withbase
- Returns:
- new transaction instance
- Throws:
KVTransactionException
- if an error occurs
-
closeTransactions
protected void closeTransactions()Forcibly fail all outstanding transactions due tostop()
being invoked.Can be used by subclasses during the shutdown sequence to ensure everything is properly cleaned up.
-
logException
Log specified exception. Used to ensure exceptions are logged at the point they are thrown.- Parameters:
e
- exception to log- Returns:
- e
-
wrapException
Wrap aRuntimeException
as needed.The implementation in
SnapshotKVDatabase
just returnse
.- Parameters:
tx
- transaction in which the exception occurrede
- original exception- Returns:
- wrapped exception, or just
e
-