]>
Commit | Line | Data |
---|---|---|
7c673cae | 1 | // Copyright (c) 2011-present, Facebook, Inc. All rights reserved. |
11fdf7f2 TL |
2 | // This source code is licensed under both the GPLv2 (found in the |
3 | // COPYING file in the root directory) and Apache 2.0 License | |
4 | // (found in the LICENSE.Apache file in the root directory). | |
7c673cae FG |
5 | |
6 | #ifndef ROCKSDB_LITE | |
7 | ||
11fdf7f2 | 8 | #include "utilities/transactions/optimistic_transaction.h" |
7c673cae | 9 | |
7c673cae | 10 | #include <string> |
7c673cae FG |
11 | |
12 | #include "db/column_family.h" | |
13 | #include "db/db_impl.h" | |
14 | #include "rocksdb/comparator.h" | |
15 | #include "rocksdb/db.h" | |
16 | #include "rocksdb/status.h" | |
17 | #include "rocksdb/utilities/optimistic_transaction_db.h" | |
11fdf7f2 | 18 | #include "util/cast_util.h" |
7c673cae FG |
19 | #include "util/string_util.h" |
20 | #include "utilities/transactions/transaction_util.h" | |
21 | ||
22 | namespace rocksdb { | |
23 | ||
24 | struct WriteOptions; | |
25 | ||
11fdf7f2 | 26 | OptimisticTransaction::OptimisticTransaction( |
7c673cae FG |
27 | OptimisticTransactionDB* txn_db, const WriteOptions& write_options, |
28 | const OptimisticTransactionOptions& txn_options) | |
29 | : TransactionBaseImpl(txn_db->GetBaseDB(), write_options), txn_db_(txn_db) { | |
30 | Initialize(txn_options); | |
31 | } | |
32 | ||
11fdf7f2 | 33 | void OptimisticTransaction::Initialize( |
7c673cae FG |
34 | const OptimisticTransactionOptions& txn_options) { |
35 | if (txn_options.set_snapshot) { | |
36 | SetSnapshot(); | |
37 | } | |
38 | } | |
39 | ||
11fdf7f2 | 40 | void OptimisticTransaction::Reinitialize( |
7c673cae FG |
41 | OptimisticTransactionDB* txn_db, const WriteOptions& write_options, |
42 | const OptimisticTransactionOptions& txn_options) { | |
43 | TransactionBaseImpl::Reinitialize(txn_db->GetBaseDB(), write_options); | |
44 | Initialize(txn_options); | |
45 | } | |
46 | ||
11fdf7f2 | 47 | OptimisticTransaction::~OptimisticTransaction() {} |
7c673cae | 48 | |
11fdf7f2 | 49 | void OptimisticTransaction::Clear() { TransactionBaseImpl::Clear(); } |
7c673cae | 50 | |
11fdf7f2 | 51 | Status OptimisticTransaction::Prepare() { |
7c673cae FG |
52 | return Status::InvalidArgument( |
53 | "Two phase commit not supported for optimistic transactions."); | |
54 | } | |
55 | ||
11fdf7f2 | 56 | Status OptimisticTransaction::Commit() { |
7c673cae FG |
57 | // Set up callback which will call CheckTransactionForConflicts() to |
58 | // check whether this transaction is safe to be committed. | |
59 | OptimisticTransactionCallback callback(this); | |
60 | ||
11fdf7f2 | 61 | DBImpl* db_impl = static_cast_with_check<DBImpl, DB>(db_->GetRootDB()); |
7c673cae FG |
62 | |
63 | Status s = db_impl->WriteWithCallback( | |
64 | write_options_, GetWriteBatch()->GetWriteBatch(), &callback); | |
65 | ||
66 | if (s.ok()) { | |
67 | Clear(); | |
68 | } | |
69 | ||
70 | return s; | |
71 | } | |
72 | ||
11fdf7f2 | 73 | Status OptimisticTransaction::Rollback() { |
7c673cae FG |
74 | Clear(); |
75 | return Status::OK(); | |
76 | } | |
77 | ||
78 | // Record this key so that we can check it for conflicts at commit time. | |
79 | // | |
80 | // 'exclusive' is unused for OptimisticTransaction. | |
11fdf7f2 TL |
81 | Status OptimisticTransaction::TryLock(ColumnFamilyHandle* column_family, |
82 | const Slice& key, bool read_only, | |
494da23a TL |
83 | bool exclusive, const bool do_validate, |
84 | const bool assume_tracked) { | |
85 | assert(!assume_tracked); // not supported | |
86 | (void)assume_tracked; | |
87 | if (!do_validate) { | |
7c673cae FG |
88 | return Status::OK(); |
89 | } | |
90 | uint32_t cfh_id = GetColumnFamilyID(column_family); | |
91 | ||
92 | SetSnapshotIfNeeded(); | |
93 | ||
94 | SequenceNumber seq; | |
95 | if (snapshot_) { | |
96 | seq = snapshot_->GetSequenceNumber(); | |
97 | } else { | |
98 | seq = db_->GetLatestSequenceNumber(); | |
99 | } | |
100 | ||
101 | std::string key_str = key.ToString(); | |
102 | ||
103 | TrackKey(cfh_id, key_str, seq, read_only, exclusive); | |
104 | ||
105 | // Always return OK. Confilct checking will happen at commit time. | |
106 | return Status::OK(); | |
107 | } | |
108 | ||
109 | // Returns OK if it is safe to commit this transaction. Returns Status::Busy | |
110 | // if there are read or write conflicts that would prevent us from committing OR | |
111 | // if we can not determine whether there would be any such conflicts. | |
112 | // | |
113 | // Should only be called on writer thread in order to avoid any race conditions | |
114 | // in detecting write conflicts. | |
11fdf7f2 | 115 | Status OptimisticTransaction::CheckTransactionForConflicts(DB* db) { |
7c673cae FG |
116 | Status result; |
117 | ||
11fdf7f2 | 118 | auto db_impl = static_cast_with_check<DBImpl, DB>(db); |
7c673cae FG |
119 | |
120 | // Since we are on the write thread and do not want to block other writers, | |
121 | // we will do a cache-only conflict check. This can result in TryAgain | |
122 | // getting returned if there is not sufficient memtable history to check | |
123 | // for conflicts. | |
124 | return TransactionUtil::CheckKeysForConflicts(db_impl, GetTrackedKeys(), | |
125 | true /* cache_only */); | |
126 | } | |
127 | ||
11fdf7f2 | 128 | Status OptimisticTransaction::SetName(const TransactionName& /* unused */) { |
7c673cae FG |
129 | return Status::InvalidArgument("Optimistic transactions cannot be named."); |
130 | } | |
131 | ||
132 | } // namespace rocksdb | |
133 | ||
134 | #endif // ROCKSDB_LITE |