/Users/deen/code/yugabyte-db/src/yb/rocksdb/utilities/transactions/transaction_db_impl.h
Line | Count | Source |
1 | | // Copyright (c) 2011-present, Facebook, Inc. All rights reserved. |
2 | | // This source code is licensed under the BSD-style license found in the |
3 | | // LICENSE file in the root directory of this source tree. An additional grant |
4 | | // of patent rights can be found in the PATENTS file in the same directory. |
5 | | // |
6 | | // The following only applies to changes made to this file as part of YugaByte development. |
7 | | // |
8 | | // Portions Copyright (c) YugaByte, Inc. |
9 | | // |
10 | | // Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except |
11 | | // in compliance with the License. You may obtain a copy of the License at |
12 | | // |
13 | | // http://www.apache.org/licenses/LICENSE-2.0 |
14 | | // |
15 | | // Unless required by applicable law or agreed to in writing, software distributed under the License |
16 | | // is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express |
17 | | // or implied. See the License for the specific language governing permissions and limitations |
18 | | // under the License. |
19 | | // |
20 | | |
21 | | #pragma once |
22 | | #ifndef ROCKSDB_LITE |
23 | | |
24 | | #include <mutex> |
25 | | #include <string> |
26 | | #include <unordered_map> |
27 | | |
28 | | #include "yb/rocksdb/db.h" |
29 | | #include "yb/rocksdb/options.h" |
30 | | #include "yb/rocksdb/utilities/transaction_db.h" |
31 | | #include "yb/rocksdb/utilities/transactions/transaction_impl.h" |
32 | | #include "yb/rocksdb/utilities/transactions/transaction_lock_mgr.h" |
33 | | |
34 | | namespace rocksdb { |
35 | | |
36 | | class TransactionDBImpl : public TransactionDB { |
37 | | public: |
38 | | explicit TransactionDBImpl(DB* db, |
39 | | const TransactionDBOptions& txn_db_options); |
40 | | |
41 | 47 | ~TransactionDBImpl() {} |
42 | | |
43 | | Transaction* BeginTransaction(const WriteOptions& write_options, |
44 | | const TransactionOptions& txn_options, |
45 | | Transaction* old_txn) override; |
46 | | |
47 | | using StackableDB::Put; |
48 | | virtual Status Put(const WriteOptions& options, |
49 | | ColumnFamilyHandle* column_family, const Slice& key, |
50 | | const Slice& val) override; |
51 | | |
52 | | using StackableDB::Delete; |
53 | | virtual Status Delete(const WriteOptions& wopts, |
54 | | ColumnFamilyHandle* column_family, |
55 | | const Slice& key) override; |
56 | | |
57 | | using StackableDB::Merge; |
58 | | virtual Status Merge(const WriteOptions& options, |
59 | | ColumnFamilyHandle* column_family, const Slice& key, |
60 | | const Slice& value) override; |
61 | | |
62 | | using StackableDB::Write; |
63 | | virtual Status Write(const WriteOptions& opts, WriteBatch* updates) override; |
64 | | |
65 | | using StackableDB::CreateColumnFamily; |
66 | | virtual Status CreateColumnFamily(const ColumnFamilyOptions& options, |
67 | | const std::string& column_family_name, |
68 | | ColumnFamilyHandle** handle) override; |
69 | | |
70 | | using StackableDB::DropColumnFamily; |
71 | | virtual Status DropColumnFamily(ColumnFamilyHandle* column_family) override; |
72 | | |
73 | | Status TryLock(TransactionImpl* txn, uint32_t cfh_id, const std::string& key); |
74 | | |
75 | | void UnLock(TransactionImpl* txn, const TransactionKeyMap* keys); |
76 | | void UnLock(TransactionImpl* txn, uint32_t cfh_id, const std::string& key); |
77 | | |
78 | | void AddColumnFamily(const ColumnFamilyHandle* handle); |
79 | | |
80 | | static TransactionDBOptions ValidateTxnDBOptions( |
81 | | const TransactionDBOptions& txn_db_options); |
82 | | |
83 | 3.17k | const TransactionDBOptions& GetTxnDBOptions() const { |
84 | 3.17k | return txn_db_options_; |
85 | 3.17k | } |
86 | | |
87 | | void InsertExpirableTransaction(TransactionID tx_id, TransactionImpl* tx); |
88 | | void RemoveExpirableTransaction(TransactionID tx_id); |
89 | | |
90 | | // If transaction is no longer available, locks can be stolen |
91 | | // If transaction is available, try stealing locks directly from transaction |
92 | | // It is the caller's responsibility to ensure that the referred transaction |
93 | | // is expirable (GetExpirationTime() > 0) and that it is expired. |
94 | | bool TryStealingExpiredTransactionLocks(TransactionID tx_id); |
95 | | |
96 | | private: |
97 | | void ReinitializeTransaction( |
98 | | Transaction* txn, const WriteOptions& write_options, |
99 | | const TransactionOptions& txn_options = TransactionOptions()); |
100 | | |
101 | | const TransactionDBOptions txn_db_options_; |
102 | | TransactionLockMgr lock_mgr_; |
103 | | |
104 | | // Must be held when adding/dropping column families. |
105 | | InstrumentedMutex column_family_mutex_; |
106 | | Transaction* BeginInternalTransaction(const WriteOptions& options); |
107 | | Status WriteHelper(WriteBatch* updates, TransactionImpl* txn_impl); |
108 | | |
109 | | // Used to ensure that no locks are stolen from an expirable transaction |
110 | | // that has started a commit. Only transactions with an expiration time |
111 | | // should be in this map. |
112 | | std::mutex map_mutex_; |
113 | | std::unordered_map<TransactionID, TransactionImpl*> |
114 | | expirable_transactions_map_; |
115 | | }; |
116 | | |
117 | | } // namespace rocksdb |
118 | | #endif // ROCKSDB_LITE |