YugabyteDB (2.13.0.0-b42, bfc6a6643e7399ac8a0e81d06a3ee6d6571b33ab)

Coverage Report

Created: 2022-03-09 17:30

/Users/deen/code/yugabyte-db/src/yb/rocksdb/utilities/transactions/transaction_impl.h
Line
Count
Source (jump to first uncovered line)
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
#ifndef ROCKSDB_UTILITIES_TRANSACTIONS_TRANSACTION_IMPL_H
21
#define ROCKSDB_UTILITIES_TRANSACTIONS_TRANSACTION_IMPL_H
22
23
#pragma once
24
25
#ifndef ROCKSDB_LITE
26
27
#include <atomic>
28
#include <stack>
29
#include <string>
30
#include <unordered_map>
31
#include <vector>
32
33
#include "yb/rocksdb/db/write_callback.h"
34
#include "yb/rocksdb/db.h"
35
#include "yb/util/slice.h"
36
#include "yb/rocksdb/snapshot.h"
37
#include "yb/rocksdb/status.h"
38
#include "yb/rocksdb/types.h"
39
#include "yb/rocksdb/utilities/transaction.h"
40
#include "yb/rocksdb/utilities/transaction_db.h"
41
#include "yb/rocksdb/utilities/write_batch_with_index.h"
42
#include "yb/rocksdb/utilities/transactions/transaction_base.h"
43
#include "yb/rocksdb/utilities/transactions/transaction_util.h"
44
45
namespace rocksdb {
46
47
using TransactionID = uint64_t;
48
49
class TransactionDBImpl;
50
51
class TransactionImpl : public TransactionBaseImpl {
52
 public:
53
  TransactionImpl(TransactionDB* db, const WriteOptions& write_options,
54
                  const TransactionOptions& txn_options);
55
56
  virtual ~TransactionImpl();
57
58
  void Reinitialize(TransactionDB* txn_db, const WriteOptions& write_options,
59
                    const TransactionOptions& txn_options);
60
61
  Status Commit() override;
62
63
  Status CommitBatch(WriteBatch* batch);
64
65
  void Rollback() override;
66
67
  Status RollbackToSavePoint() override;
68
69
  // Generate a new unique transaction identifier
70
  static TransactionID GenTxnID();
71
72
9.73k
  TransactionID GetTxnID() const { return txn_id_; }
73
74
  // Returns the time (in microseconds according to Env->GetMicros())
75
  // that this transaction will be expired.  Returns 0 if this transaction does
76
  // not expire.
77
3.34k
  uint64_t GetExpirationTime() const { return expiration_time_; }
78
79
  // returns true if this transaction has an expiration_time and has expired.
80
  bool IsExpired() const;
81
82
  // Returns the number of microseconds a transaction can wait on acquiring a
83
  // lock or -1 if there is no timeout.
84
3.33k
  int64_t GetLockTimeout() const { return lock_timeout_; }
85
3.09k
  void SetLockTimeout(int64_t timeout) override {
86
3.09k
    lock_timeout_ = timeout * 1000;
87
3.09k
  }
88
89
  // Returns true if locks were stolen successfully, false otherwise.
90
  bool TryStealingLocks();
91
92
 protected:
93
  Status TryLock(ColumnFamilyHandle* column_family, const Slice& key,
94
                 bool read_only, bool untracked = false) override;
95
96
 private:
97
  enum ExecutionStatus { STARTED, COMMITTING, LOCKS_STOLEN };
98
99
  TransactionDBImpl* txn_db_impl_;
100
101
  // Used to create unique ids for transactions.
102
  static std::atomic<TransactionID> txn_id_counter_;
103
104
  // Unique ID for this transaction
105
  TransactionID txn_id_;
106
107
  // If non-zero, this transaction should not be committed after this time (in
108
  // microseconds according to Env->NowMicros())
109
  uint64_t expiration_time_;
110
111
  // Timeout in microseconds when locking a key or -1 if there is no timeout.
112
  int64_t lock_timeout_;
113
114
  // Execution status of the transaction.
115
  std::atomic<ExecutionStatus> exec_status_;
116
117
  void Clear() override;
118
119
  void Initialize(const TransactionOptions& txn_options);
120
121
  Status ValidateSnapshot(ColumnFamilyHandle* column_family, const Slice& key,
122
                          SequenceNumber prev_seqno, SequenceNumber* new_seqno);
123
124
  Status LockBatch(WriteBatch* batch, TransactionKeyMap* keys_to_unlock);
125
126
  Status DoCommit(WriteBatch* batch);
127
128
  void RollbackLastN(size_t num);
129
130
  void UnlockGetForUpdate(ColumnFamilyHandle* column_family,
131
                          const Slice& key) override;
132
133
  // No copying allowed
134
  TransactionImpl(const TransactionImpl&);
135
  void operator=(const TransactionImpl&);
136
};
137
138
// Used at commit time to check whether transaction is committing before its
139
// expiration time.
140
class TransactionCallback : public WriteCallback {
141
 public:
142
0
  explicit TransactionCallback(TransactionImpl* txn) : txn_(txn) {}
143
144
0
  Status Callback(DB* db) override {
145
0
    if (txn_->IsExpired()) {
146
0
      return STATUS(Expired, "");
147
0
    } else {
148
0
      return Status::OK();
149
0
    }
150
0
  }
151
152
0
  bool AllowWriteBatching() override { return true; }
153
154
 private:
155
  TransactionImpl* txn_;
156
};
157
158
}  // namespace rocksdb
159
160
#endif  // ROCKSDB_LITE
161
162
#endif // ROCKSDB_UTILITIES_TRANSACTIONS_TRANSACTION_IMPL_H