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