/Users/deen/code/yugabyte-db/src/yb/rocksdb/utilities/transaction_db.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 <string> |
25 | | #include <vector> |
26 | | |
27 | | #include "yb/rocksdb/comparator.h" |
28 | | #include "yb/rocksdb/db.h" |
29 | | #include "yb/rocksdb/utilities/stackable_db.h" |
30 | | #include "yb/rocksdb/utilities/transaction.h" |
31 | | |
32 | | // Database with Transaction support. |
33 | | // |
34 | | // See transaction.h and examples/transaction_example.cc |
35 | | |
36 | | namespace rocksdb { |
37 | | |
38 | | class TransactionDBMutexFactory; |
39 | | |
40 | | struct TransactionDBOptions { |
41 | | // Specifies the maximum number of keys that can be locked at the same time |
42 | | // per column family. |
43 | | // If the number of locked keys is greater than max_num_locks, transaction |
44 | | // writes (or GetForUpdate) will return an error. |
45 | | // If this value is not positive, no limit will be enforced. |
46 | | int64_t max_num_locks = -1; |
47 | | |
48 | | // Increasing this value will increase the concurrency by dividing the lock |
49 | | // table (per column family) into more sub-tables, each with their own |
50 | | // separate |
51 | | // mutex. |
52 | | size_t num_stripes = 16; |
53 | | |
54 | | // If positive, specifies the default wait timeout in milliseconds when |
55 | | // a transaction attempts to lock a key if not specified by |
56 | | // TransactionOptions::lock_timeout. |
57 | | // |
58 | | // If 0, no waiting is done if a lock cannot instantly be acquired. |
59 | | // If negative, there is no timeout. Not using a timeout is not recommended |
60 | | // as it can lead to deadlocks. Currently, there is no deadlock-detection to |
61 | | // recover |
62 | | // from a deadlock. |
63 | | int64_t transaction_lock_timeout = 1000; // 1 second |
64 | | |
65 | | // If positive, specifies the wait timeout in milliseconds when writing a key |
66 | | // OUTSIDE of a transaction (ie by calling DB::Put(),Merge(),Delete(),Write() |
67 | | // directly). |
68 | | // If 0, no waiting is done if a lock cannot instantly be acquired. |
69 | | // If negative, there is no timeout and will block indefinitely when acquiring |
70 | | // a lock. |
71 | | // |
72 | | // Not using a a timeout can lead to deadlocks. Currently, there |
73 | | // is no deadlock-detection to recover from a deadlock. While DB writes |
74 | | // cannot deadlock with other DB writes, they can deadlock with a transaction. |
75 | | // A negative timeout should only be used if all transactions have an small |
76 | | // expiration set. |
77 | | int64_t default_lock_timeout = 1000; // 1 second |
78 | | |
79 | | // If set, the TransactionDB will use this implemenation of a mutex and |
80 | | // condition variable for all transaction locking instead of the default |
81 | | // mutex/condvar implementation. |
82 | | std::shared_ptr<TransactionDBMutexFactory> custom_mutex_factory; |
83 | | }; |
84 | | |
85 | | struct TransactionOptions { |
86 | | // Setting set_snapshot=true is the same as calling |
87 | | // Transaction::SetSnapshot(). |
88 | | bool set_snapshot = false; |
89 | | |
90 | | |
91 | | // TODO(agiardullo): TransactionDB does not yet support comparators that allow |
92 | | // two non-equal keys to be equivalent. Ie, cmp->Compare(a,b) should only |
93 | | // return 0 if |
94 | | // a.compare(b) returns 0. |
95 | | |
96 | | |
97 | | // If positive, specifies the wait timeout in milliseconds when |
98 | | // a transaction attempts to lock a key. |
99 | | // |
100 | | // If 0, no waiting is done if a lock cannot instantly be acquired. |
101 | | // If negative, TransactionDBOptions::transaction_lock_timeout will be used. |
102 | | int64_t lock_timeout = -1; |
103 | | |
104 | | // Expiration duration in milliseconds. If non-negative, transactions that |
105 | | // last longer than this many milliseconds will fail to commit. If not set, |
106 | | // a forgotten transaction that is never committed, rolled back, or deleted |
107 | | // will never relinquish any locks it holds. This could prevent keys from |
108 | | // being |
109 | | // written by other writers. |
110 | | int64_t expiration = -1; |
111 | | }; |
112 | | |
113 | | class TransactionDB : public StackableDB { |
114 | | public: |
115 | | // Open a TransactionDB similar to DB::Open(). |
116 | | static Status Open(const Options& options, |
117 | | const TransactionDBOptions& txn_db_options, |
118 | | const std::string& dbname, TransactionDB** dbptr); |
119 | | |
120 | | static Status Open(const DBOptions& db_options, |
121 | | const TransactionDBOptions& txn_db_options, |
122 | | const std::string& dbname, |
123 | | const std::vector<ColumnFamilyDescriptor>& column_families, |
124 | | std::vector<ColumnFamilyHandle*>* handles, |
125 | | TransactionDB** dbptr); |
126 | | |
127 | 47 | virtual ~TransactionDB() {} |
128 | | |
129 | | // Starts a new Transaction. |
130 | | // |
131 | | // Caller is responsible for deleting the returned transaction when no |
132 | | // longer needed. |
133 | | // |
134 | | // If old_txn is not null, BeginTransaction will reuse this Transaction |
135 | | // handle instead of allocating a new one. This is an optimization to avoid |
136 | | // extra allocations when repeatedly creating transactions. |
137 | | virtual Transaction* BeginTransaction( |
138 | | const WriteOptions& write_options, |
139 | | const TransactionOptions& txn_options = TransactionOptions(), |
140 | | Transaction* old_txn = nullptr) = 0; |
141 | | |
142 | | protected: |
143 | | // To Create an TransactionDB, call Open() |
144 | 47 | explicit TransactionDB(DB* db) : StackableDB(db) {} |
145 | | |
146 | | private: |
147 | | // No copying allowed |
148 | | TransactionDB(const TransactionDB&); |
149 | | void operator=(const TransactionDB&); |
150 | | }; |
151 | | |
152 | | } // namespace rocksdb |
153 | | |
154 | | #endif // ROCKSDB_LITE |