@ThreadSafe public abstract class SnapshotKVDatabase extends Object implements KVDatabase
KVDatabase
implementation based on an underlying AtomicKVStore
that uses
snapshot views and optimistic locking to provide concurrent
transactions and linearizable ACID consistency.
Instances implement a simple optimistic locking scheme for MVCC using AtomicKVStore.snapshot()
. 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 RetryTransactionException
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.
AtomicKVDatabase
Constructor and Description |
---|
SnapshotKVDatabase()
Default constructor.
|
SnapshotKVDatabase(AtomicKVStore kvstore)
Constructor.
|
Modifier and Type | Method and Description |
---|---|
protected void |
closeTransactions()
Forcibly fail all outstanding transactions due to
stop() being invoked. |
protected SnapshotKVTransaction |
createSnapshotKVTransaction(MutableView view,
long baseVersion)
Instantiate a new
SnapshotKVTransaction instance. |
SnapshotKVTransaction |
createTransaction()
Create a new transaction.
|
SnapshotKVTransaction |
createTransaction(Map<String,?> options)
Create a new transaction with the specified options.
|
long |
getCurrentVersion()
Get the current MVCC version number.
|
protected AtomicKVStore |
getKVStore()
Get the underlying
AtomicKVStore . |
protected KVTransactionException |
logException(KVTransactionException e)
Log specified exception.
|
protected void |
setKVStore(AtomicKVStore kvstore)
Configure the underlying
AtomicKVStore . |
void |
start()
Start this instance.
|
void |
stop()
Stop this instance.
|
String |
toString() |
protected RuntimeException |
wrapException(SnapshotKVTransaction tx,
RuntimeException e)
Wrap a
RuntimeException as needed. |
protected final Logger log
public SnapshotKVDatabase()
The underlying key/value store must still be configured before starting this instance.
public SnapshotKVDatabase(AtomicKVStore kvstore)
kvstore
- underlying key/value storeprotected AtomicKVStore getKVStore()
AtomicKVStore
.protected void setKVStore(AtomicKVStore kvstore)
AtomicKVStore
.
Required property; must be configured before start()
ing.
kvstore
- underlying key/value storeIllegalStateException
- if this instance is already startedpublic long getCurrentVersion()
@PostConstruct public void start()
KVDatabase
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.
start
in interface KVDatabase
@PreDestroy public void stop()
KVDatabase
This method is idempotent: if this instance has not been started, or is already stopped, nothing happens.
stop
in interface KVDatabase
public SnapshotKVTransaction createTransaction(Map<String,?> options)
KVDatabase
createTransaction
in interface KVDatabase
options
- optional transaction options; may be nullpublic SnapshotKVTransaction createTransaction()
createTransaction
in interface KVDatabase
IllegalStateException
- if not start()
ed or stop()
ingprotected SnapshotKVTransaction createSnapshotKVTransaction(MutableView view, long baseVersion)
SnapshotKVTransaction
instance.
The implementation in SnapshotKVDatabase
just invokes the SnapshotKVTransaction
constructor using this
. Subclasses may want to override this method to create a more specific subclass.
view
- mutable view to be used for this transactionbaseVersion
- the database version associated with base
KVTransactionException
- if an error occursprotected void closeTransactions()
stop()
being invoked.
Can be used by subclasses during the shutdown sequence to ensure everything is properly cleaned up.
protected KVTransactionException logException(KVTransactionException e)
e
- exception to logprotected RuntimeException wrapException(SnapshotKVTransaction tx, RuntimeException e)
RuntimeException
as needed.
The implementation in SnapshotKVDatabase
just returns e
.
tx
- transaction in which the exception occurrede
- original exceptione
Copyright © 2022. All rights reserved.