@ -5,7 +5,7 @@
# ifndef ROCKSDB_LITE
# ifndef ROCKSDB_LITE
# include "utilities/transactions/transaction_db_impl .h"
# include "utilities/transactions/pessimistic_ transaction_db.h"
# include <string>
# include <string>
# include <unordered_set>
# include <unordered_set>
@ -21,8 +21,8 @@
namespace rocksdb {
namespace rocksdb {
TransactionDBImpl : : TransactionDBImpl ( DB * db ,
Pessimistic TransactionDB: : Pessimistic TransactionDB(
const TransactionDBOptions & txn_db_options )
DB * db , const TransactionDBOptions & txn_db_options )
: TransactionDB ( db ) ,
: TransactionDB ( db ) ,
db_impl_ ( static_cast_with_check < DBImpl , DB > ( db ) ) ,
db_impl_ ( static_cast_with_check < DBImpl , DB > ( db ) ) ,
txn_db_options_ ( txn_db_options ) ,
txn_db_options_ ( txn_db_options ) ,
@ -34,9 +34,9 @@ TransactionDBImpl::TransactionDBImpl(DB* db,
assert ( db_impl_ ! = nullptr ) ;
assert ( db_impl_ ! = nullptr ) ;
}
}
// Support initiliazing TransactionDBImpl from a stackable db
// Support initiliazing Pessimistic TransactionDB from a stackable db
//
//
// TransactionDBImpl
// Pessimistic TransactionDB
// ^ ^
// ^ ^
// | |
// | |
// | +
// | +
@ -50,8 +50,8 @@ TransactionDBImpl::TransactionDBImpl(DB* db,
// +
// +
// DB
// DB
//
//
TransactionDBImpl : : TransactionDBImpl ( StackableDB * db ,
Pessimistic TransactionDB: : Pessimistic TransactionDB(
const TransactionDBOptions & txn_db_options )
StackableDB * db , const TransactionDBOptions & txn_db_options )
: TransactionDB ( db ) ,
: TransactionDB ( db ) ,
db_impl_ ( static_cast_with_check < DBImpl , DB > ( db - > GetRootDB ( ) ) ) ,
db_impl_ ( static_cast_with_check < DBImpl , DB > ( db - > GetRootDB ( ) ) ) ,
txn_db_options_ ( txn_db_options ) ,
txn_db_options_ ( txn_db_options ) ,
@ -63,13 +63,13 @@ TransactionDBImpl::TransactionDBImpl(StackableDB* db,
assert ( db_impl_ ! = nullptr ) ;
assert ( db_impl_ ! = nullptr ) ;
}
}
TransactionDBImpl : : ~ TransactionDBImpl ( ) {
Pessimistic TransactionDB: : ~ Pessimistic TransactionDB( ) {
while ( ! transactions_ . empty ( ) ) {
while ( ! transactions_ . empty ( ) ) {
delete transactions_ . begin ( ) - > second ;
delete transactions_ . begin ( ) - > second ;
}
}
}
}
Status TransactionDBImpl : : Initialize (
Status Pessimistic TransactionDB: : Initialize (
const std : : vector < size_t > & compaction_enabled_cf_indices ,
const std : : vector < size_t > & compaction_enabled_cf_indices ,
const std : : vector < ColumnFamilyHandle * > & handles ) {
const std : : vector < ColumnFamilyHandle * > & handles ) {
for ( auto cf_ptr : handles ) {
for ( auto cf_ptr : handles ) {
@ -121,7 +121,7 @@ Status TransactionDBImpl::Initialize(
return s ;
return s ;
}
}
Transaction * TransactionDBImpl : : BeginTransaction (
Transaction * WriteCommittedTxnDB : : BeginTransaction (
const WriteOptions & write_options , const TransactionOptions & txn_options ,
const WriteOptions & write_options , const TransactionOptions & txn_options ,
Transaction * old_txn ) {
Transaction * old_txn ) {
if ( old_txn ! = nullptr ) {
if ( old_txn ! = nullptr ) {
@ -132,7 +132,18 @@ Transaction* TransactionDBImpl::BeginTransaction(
}
}
}
}
TransactionDBOptions TransactionDBImpl : : ValidateTxnDBOptions (
Transaction * WritePreparedTxnDB : : BeginTransaction (
const WriteOptions & write_options , const TransactionOptions & txn_options ,
Transaction * old_txn ) {
if ( old_txn ! = nullptr ) {
ReinitializeTransaction ( old_txn , write_options , txn_options ) ;
return old_txn ;
} else {
return new WritePreparedTxnImpl ( this , write_options , txn_options ) ;
}
}
TransactionDBOptions PessimisticTransactionDB : : ValidateTxnDBOptions (
const TransactionDBOptions & txn_db_options ) {
const TransactionDBOptions & txn_db_options ) {
TransactionDBOptions validated = txn_db_options ;
TransactionDBOptions validated = txn_db_options ;
@ -213,8 +224,19 @@ Status TransactionDB::WrapDB(
DB * db , const TransactionDBOptions & txn_db_options ,
DB * db , const TransactionDBOptions & txn_db_options ,
const std : : vector < size_t > & compaction_enabled_cf_indices ,
const std : : vector < size_t > & compaction_enabled_cf_indices ,
const std : : vector < ColumnFamilyHandle * > & handles , TransactionDB * * dbptr ) {
const std : : vector < ColumnFamilyHandle * > & handles , TransactionDB * * dbptr ) {
TransactionDBImpl * txn_db = new TransactionDBImpl (
PessimisticTransactionDB * txn_db ;
db , TransactionDBImpl : : ValidateTxnDBOptions ( txn_db_options ) ) ;
switch ( txn_db_options . write_policy ) {
case WRITE_UNPREPARED :
return Status : : NotSupported ( " WRITE_UNPREPARED is not implemented yet " ) ;
case WRITE_PREPARED :
txn_db = new WritePreparedTxnDB (
db , PessimisticTransactionDB : : ValidateTxnDBOptions ( txn_db_options ) ) ;
break ;
case WRITE_COMMITTED :
default :
txn_db = new WriteCommittedTxnDB (
db , PessimisticTransactionDB : : ValidateTxnDBOptions ( txn_db_options ) ) ;
}
* dbptr = txn_db ;
* dbptr = txn_db ;
Status s = txn_db - > Initialize ( compaction_enabled_cf_indices , handles ) ;
Status s = txn_db - > Initialize ( compaction_enabled_cf_indices , handles ) ;
return s ;
return s ;
@ -227,8 +249,19 @@ Status TransactionDB::WrapStackableDB(
StackableDB * db , const TransactionDBOptions & txn_db_options ,
StackableDB * db , const TransactionDBOptions & txn_db_options ,
const std : : vector < size_t > & compaction_enabled_cf_indices ,
const std : : vector < size_t > & compaction_enabled_cf_indices ,
const std : : vector < ColumnFamilyHandle * > & handles , TransactionDB * * dbptr ) {
const std : : vector < ColumnFamilyHandle * > & handles , TransactionDB * * dbptr ) {
TransactionDBImpl * txn_db = new TransactionDBImpl (
PessimisticTransactionDB * txn_db ;
db , TransactionDBImpl : : ValidateTxnDBOptions ( txn_db_options ) ) ;
switch ( txn_db_options . write_policy ) {
case WRITE_UNPREPARED :
return Status : : NotSupported ( " WRITE_UNPREPARED is not implemented yet " ) ;
case WRITE_PREPARED :
txn_db = new WritePreparedTxnDB (
db , PessimisticTransactionDB : : ValidateTxnDBOptions ( txn_db_options ) ) ;
break ;
case WRITE_COMMITTED :
default :
txn_db = new WriteCommittedTxnDB (
db , PessimisticTransactionDB : : ValidateTxnDBOptions ( txn_db_options ) ) ;
}
* dbptr = txn_db ;
* dbptr = txn_db ;
Status s = txn_db - > Initialize ( compaction_enabled_cf_indices , handles ) ;
Status s = txn_db - > Initialize ( compaction_enabled_cf_indices , handles ) ;
return s ;
return s ;
@ -236,11 +269,12 @@ Status TransactionDB::WrapStackableDB(
// Let TransactionLockMgr know that this column family exists so it can
// Let TransactionLockMgr know that this column family exists so it can
// allocate a LockMap for it.
// allocate a LockMap for it.
void TransactionDBImpl : : AddColumnFamily ( const ColumnFamilyHandle * handle ) {
void PessimisticTransactionDB : : AddColumnFamily (
const ColumnFamilyHandle * handle ) {
lock_mgr_ . AddColumnFamily ( handle - > GetID ( ) ) ;
lock_mgr_ . AddColumnFamily ( handle - > GetID ( ) ) ;
}
}
Status TransactionDBImpl : : CreateColumnFamily (
Status Pessimistic TransactionDB: : CreateColumnFamily (
const ColumnFamilyOptions & options , const std : : string & column_family_name ,
const ColumnFamilyOptions & options , const std : : string & column_family_name ,
ColumnFamilyHandle * * handle ) {
ColumnFamilyHandle * * handle ) {
InstrumentedMutexLock l ( & column_family_mutex_ ) ;
InstrumentedMutexLock l ( & column_family_mutex_ ) ;
@ -255,7 +289,8 @@ Status TransactionDBImpl::CreateColumnFamily(
// Let TransactionLockMgr know that it can deallocate the LockMap for this
// Let TransactionLockMgr know that it can deallocate the LockMap for this
// column family.
// column family.
Status TransactionDBImpl : : DropColumnFamily ( ColumnFamilyHandle * column_family ) {
Status PessimisticTransactionDB : : DropColumnFamily (
ColumnFamilyHandle * column_family ) {
InstrumentedMutexLock l ( & column_family_mutex_ ) ;
InstrumentedMutexLock l ( & column_family_mutex_ ) ;
Status s = db_ - > DropColumnFamily ( column_family ) ;
Status s = db_ - > DropColumnFamily ( column_family ) ;
@ -266,23 +301,24 @@ Status TransactionDBImpl::DropColumnFamily(ColumnFamilyHandle* column_family) {
return s ;
return s ;
}
}
Status TransactionDBImpl : : TryLock ( PessimisticTxn * txn , uint32_t cfh_id ,
Status PessimisticTransactionDB : : TryLock ( PessimisticTxn * txn , uint32_t cfh_id ,
const std : : string & key , bool exclusive ) {
const std : : string & key ,
bool exclusive ) {
return lock_mgr_ . TryLock ( txn , cfh_id , key , GetEnv ( ) , exclusive ) ;
return lock_mgr_ . TryLock ( txn , cfh_id , key , GetEnv ( ) , exclusive ) ;
}
}
void TransactionDBImpl : : UnLock ( PessimisticTxn * txn ,
void Pessimistic TransactionDB: : UnLock ( PessimisticTxn * txn ,
const TransactionKeyMap * keys ) {
const TransactionKeyMap * keys ) {
lock_mgr_ . UnLock ( txn , keys , GetEnv ( ) ) ;
lock_mgr_ . UnLock ( txn , keys , GetEnv ( ) ) ;
}
}
void TransactionDBImpl : : UnLock ( PessimisticTxn * txn , uint32_t cfh_id ,
void Pessimistic TransactionDB: : UnLock ( PessimisticTxn * txn , uint32_t cfh_id ,
const std : : string & key ) {
const std : : string & key ) {
lock_mgr_ . UnLock ( txn , cfh_id , key , GetEnv ( ) ) ;
lock_mgr_ . UnLock ( txn , cfh_id , key , GetEnv ( ) ) ;
}
}
// Used when wrapping DB write operations in a transaction
// Used when wrapping DB write operations in a transaction
Transaction * TransactionDBImpl : : BeginInternalTransaction (
Transaction * Pessimistic TransactionDB: : BeginInternalTransaction (
const WriteOptions & options ) {
const WriteOptions & options ) {
TransactionOptions txn_options ;
TransactionOptions txn_options ;
Transaction * txn = BeginTransaction ( options , txn_options , nullptr ) ;
Transaction * txn = BeginTransaction ( options , txn_options , nullptr ) ;
@ -301,9 +337,9 @@ Transaction* TransactionDBImpl::BeginInternalTransaction(
// sort its keys before locking them. This guarantees that TransactionDB write
// sort its keys before locking them. This guarantees that TransactionDB write
// methods cannot deadlock with eachother (but still could deadlock with a
// methods cannot deadlock with eachother (but still could deadlock with a
// Transaction).
// Transaction).
Status TransactionDBImpl : : Put ( const WriteOptions & options ,
Status Pessimistic TransactionDB: : Put ( const WriteOptions & options ,
ColumnFamilyHandle * column_family ,
ColumnFamilyHandle * column_family ,
const Slice & key , const Slice & val ) {
const Slice & key , const Slice & val ) {
Status s ;
Status s ;
Transaction * txn = BeginInternalTransaction ( options ) ;
Transaction * txn = BeginInternalTransaction ( options ) ;
@ -322,9 +358,9 @@ Status TransactionDBImpl::Put(const WriteOptions& options,
return s ;
return s ;
}
}
Status TransactionDBImpl : : Delete ( const WriteOptions & wopts ,
Status Pessimistic TransactionDB: : Delete ( const WriteOptions & wopts ,
ColumnFamilyHandle * column_family ,
ColumnFamilyHandle * column_family ,
const Slice & key ) {
const Slice & key ) {
Status s ;
Status s ;
Transaction * txn = BeginInternalTransaction ( wopts ) ;
Transaction * txn = BeginInternalTransaction ( wopts ) ;
@ -344,9 +380,9 @@ Status TransactionDBImpl::Delete(const WriteOptions& wopts,
return s ;
return s ;
}
}
Status TransactionDBImpl : : Merge ( const WriteOptions & options ,
Status Pessimistic TransactionDB: : Merge ( const WriteOptions & options ,
ColumnFamilyHandle * column_family ,
ColumnFamilyHandle * column_family ,
const Slice & key , const Slice & value ) {
const Slice & key , const Slice & value ) {
Status s ;
Status s ;
Transaction * txn = BeginInternalTransaction ( options ) ;
Transaction * txn = BeginInternalTransaction ( options ) ;
@ -366,7 +402,8 @@ Status TransactionDBImpl::Merge(const WriteOptions& options,
return s ;
return s ;
}
}
Status TransactionDBImpl : : Write ( const WriteOptions & opts , WriteBatch * updates ) {
Status PessimisticTransactionDB : : Write ( const WriteOptions & opts ,
WriteBatch * updates ) {
// Need to lock all keys in this batch to prevent write conflicts with
// Need to lock all keys in this batch to prevent write conflicts with
// concurrent transactions.
// concurrent transactions.
Transaction * txn = BeginInternalTransaction ( opts ) ;
Transaction * txn = BeginInternalTransaction ( opts ) ;
@ -385,19 +422,19 @@ Status TransactionDBImpl::Write(const WriteOptions& opts, WriteBatch* updates) {
return s ;
return s ;
}
}
void TransactionDBImpl : : InsertExpirableTransaction ( TransactionID tx_id ,
void Pessimistic TransactionDB: : InsertExpirableTransaction ( TransactionID tx_id ,
PessimisticTxn * tx ) {
PessimisticTxn * tx ) {
assert ( tx - > GetExpirationTime ( ) > 0 ) ;
assert ( tx - > GetExpirationTime ( ) > 0 ) ;
std : : lock_guard < std : : mutex > lock ( map_mutex_ ) ;
std : : lock_guard < std : : mutex > lock ( map_mutex_ ) ;
expirable_transactions_map_ . insert ( { tx_id , tx } ) ;
expirable_transactions_map_ . insert ( { tx_id , tx } ) ;
}
}
void TransactionDBImpl : : RemoveExpirableTransaction ( TransactionID tx_id ) {
void Pessimistic TransactionDB: : RemoveExpirableTransaction ( TransactionID tx_id ) {
std : : lock_guard < std : : mutex > lock ( map_mutex_ ) ;
std : : lock_guard < std : : mutex > lock ( map_mutex_ ) ;
expirable_transactions_map_ . erase ( tx_id ) ;
expirable_transactions_map_ . erase ( tx_id ) ;
}
}
bool TransactionDBImpl : : TryStealingExpiredTransactionLocks (
bool Pessimistic TransactionDB: : TryStealingExpiredTransactionLocks (
TransactionID tx_id ) {
TransactionID tx_id ) {
std : : lock_guard < std : : mutex > lock ( map_mutex_ ) ;
std : : lock_guard < std : : mutex > lock ( map_mutex_ ) ;
@ -409,7 +446,7 @@ bool TransactionDBImpl::TryStealingExpiredTransactionLocks(
return tx . TryStealingLocks ( ) ;
return tx . TryStealingLocks ( ) ;
}
}
void TransactionDBImpl : : ReinitializeTransaction (
void Pessimistic TransactionDB: : ReinitializeTransaction (
Transaction * txn , const WriteOptions & write_options ,
Transaction * txn , const WriteOptions & write_options ,
const TransactionOptions & txn_options ) {
const TransactionOptions & txn_options ) {
auto txn_impl = static_cast_with_check < PessimisticTxn , Transaction > ( txn ) ;
auto txn_impl = static_cast_with_check < PessimisticTxn , Transaction > ( txn ) ;
@ -417,7 +454,7 @@ void TransactionDBImpl::ReinitializeTransaction(
txn_impl - > Reinitialize ( this , write_options , txn_options ) ;
txn_impl - > Reinitialize ( this , write_options , txn_options ) ;
}
}
Transaction * TransactionDBImpl : : GetTransactionByName (
Transaction * Pessimistic TransactionDB: : GetTransactionByName (
const TransactionName & name ) {
const TransactionName & name ) {
std : : lock_guard < std : : mutex > lock ( name_map_mutex_ ) ;
std : : lock_guard < std : : mutex > lock ( name_map_mutex_ ) ;
auto it = transactions_ . find ( name ) ;
auto it = transactions_ . find ( name ) ;
@ -428,7 +465,7 @@ Transaction* TransactionDBImpl::GetTransactionByName(
}
}
}
}
void TransactionDBImpl : : GetAllPreparedTransactions (
void Pessimistic TransactionDB: : GetAllPreparedTransactions (
std : : vector < Transaction * > * transv ) {
std : : vector < Transaction * > * transv ) {
assert ( transv ) ;
assert ( transv ) ;
transv - > clear ( ) ;
transv - > clear ( ) ;
@ -440,11 +477,12 @@ void TransactionDBImpl::GetAllPreparedTransactions(
}
}
}
}
TransactionLockMgr : : LockStatusData TransactionDBImpl : : GetLockStatusData ( ) {
TransactionLockMgr : : LockStatusData
PessimisticTransactionDB : : GetLockStatusData ( ) {
return lock_mgr_ . GetLockStatusData ( ) ;
return lock_mgr_ . GetLockStatusData ( ) ;
}
}
void TransactionDBImpl : : RegisterTransaction ( Transaction * txn ) {
void Pessimistic TransactionDB: : RegisterTransaction ( Transaction * txn ) {
assert ( txn ) ;
assert ( txn ) ;
assert ( txn - > GetName ( ) . length ( ) > 0 ) ;
assert ( txn - > GetName ( ) . length ( ) > 0 ) ;
assert ( GetTransactionByName ( txn - > GetName ( ) ) = = nullptr ) ;
assert ( GetTransactionByName ( txn - > GetName ( ) ) = = nullptr ) ;
@ -453,7 +491,7 @@ void TransactionDBImpl::RegisterTransaction(Transaction* txn) {
transactions_ [ txn - > GetName ( ) ] = txn ;
transactions_ [ txn - > GetName ( ) ] = txn ;
}
}
void TransactionDBImpl : : UnregisterTransaction ( Transaction * txn ) {
void Pessimistic TransactionDB: : UnregisterTransaction ( Transaction * txn ) {
assert ( txn ) ;
assert ( txn ) ;
std : : lock_guard < std : : mutex > lock ( name_map_mutex_ ) ;
std : : lock_guard < std : : mutex > lock ( name_map_mutex_ ) ;
auto it = transactions_ . find ( txn - > GetName ( ) ) ;
auto it = transactions_ . find ( txn - > GetName ( ) ) ;