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/optimistic_transaction_impl.cc
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
21
#ifndef ROCKSDB_LITE
22
23
#include "yb/rocksdb/utilities/transactions/optimistic_transaction_impl.h"
24
25
#include <algorithm>
26
#include <string>
27
#include <vector>
28
29
#include "yb/rocksdb/comparator.h"
30
#include "yb/rocksdb/db.h"
31
#include "yb/rocksdb/db/db_impl.h"
32
#include "yb/rocksdb/status.h"
33
#include "yb/rocksdb/utilities/optimistic_transaction_db.h"
34
#include "yb/rocksdb/utilities/transactions/transaction_util.h"
35
36
namespace rocksdb {
37
38
struct WriteOptions;
39
40
OptimisticTransactionImpl::OptimisticTransactionImpl(
41
    OptimisticTransactionDB* txn_db, const WriteOptions& write_options,
42
    const OptimisticTransactionOptions& txn_options)
43
54
    : TransactionBaseImpl(txn_db->GetBaseDB(), write_options), txn_db_(txn_db) {
44
54
  Initialize(txn_options);
45
54
}
46
47
void OptimisticTransactionImpl::Initialize(
48
60
    const OptimisticTransactionOptions& txn_options) {
49
60
  if (txn_options.set_snapshot) {
50
16
    SetSnapshot();
51
16
  }
52
60
}
53
54
void OptimisticTransactionImpl::Reinitialize(
55
    OptimisticTransactionDB* txn_db, const WriteOptions& write_options,
56
6
    const OptimisticTransactionOptions& txn_options) {
57
6
  TransactionBaseImpl::Reinitialize(txn_db->GetBaseDB(), write_options);
58
6
  Initialize(txn_options);
59
6
}
60
61
54
OptimisticTransactionImpl::~OptimisticTransactionImpl() {
62
54
}
63
64
46
void OptimisticTransactionImpl::Clear() {
65
46
  TransactionBaseImpl::Clear();
66
46
}
67
68
55
Status OptimisticTransactionImpl::Commit() {
69
  // Set up callback which will call CheckTransactionForConflicts() to
70
  // check whether this transaction is safe to be committed.
71
55
  OptimisticTransactionCallback callback(this);
72
73
55
  DBImpl* db_impl = dynamic_cast<DBImpl*>(db_->GetRootDB());
74
55
  if (db_impl == nullptr) {
75
    // This should only happen if we support creating transactions from
76
    // a StackableDB and someone overrides GetRootDB().
77
0
    return STATUS(InvalidArgument,
78
0
        "DB::GetRootDB() returned an unexpected DB class");
79
0
  }
80
81
55
  Status s = db_impl->WriteWithCallback(
82
55
      write_options_, GetWriteBatch()->GetWriteBatch(), &callback);
83
84
55
  if (s.ok()) {
85
36
    Clear();
86
36
  }
87
88
55
  return s;
89
55
}
90
91
4
void OptimisticTransactionImpl::Rollback() { Clear(); }
92
93
// Record this key so that we can check it for conflicts at commit time.
94
Status OptimisticTransactionImpl::TryLock(ColumnFamilyHandle* column_family,
95
                                          const Slice& key, bool read_only,
96
128
                                          bool untracked) {
97
128
  if (untracked) {
98
5
    return Status::OK();
99
5
  }
100
123
  uint32_t cfh_id = GetColumnFamilyID(column_family);
101
102
123
  SetSnapshotIfNeeded();
103
104
123
  SequenceNumber seq;
105
123
  if (snapshot_) {
106
60
    seq = snapshot_->GetSequenceNumber();
107
63
  } else {
108
63
    seq = db_->GetLatestSequenceNumber();
109
63
  }
110
111
123
  std::string key_str = key.ToString();
112
113
123
  TrackKey(cfh_id, key_str, seq, read_only);
114
115
  // Always return OK. Confilct checking will happen at commit time.
116
123
  return Status::OK();
117
123
}
118
119
// Returns OK if it is safe to commit this transaction.  Returns Status::Busy
120
// if there are read or write conflicts that would prevent us from committing OR
121
// if we can not determine whether there would be any such conflicts.
122
//
123
// Should only be called on writer thread in order to avoid any race conditions
124
// in detecting write conflicts.
125
55
Status OptimisticTransactionImpl::CheckTransactionForConflicts(DB* db) {
126
55
  Status result;
127
128
55
  assert(dynamic_cast<DBImpl*>(db) != nullptr);
129
55
  auto db_impl = reinterpret_cast<DBImpl*>(db);
130
131
  // Since we are on the write thread and do not want to block other writers,
132
  // we will do a cache-only conflict check.  This can result in TryAgain
133
  // getting returned if there is not sufficient memtable history to check
134
  // for conflicts.
135
55
  return TransactionUtil::CheckKeysForConflicts(db_impl, GetTrackedKeys(),
136
55
                                                true /* cache_only */);
137
55
}
138
139
}  // namespace rocksdb
140
141
#endif  // ROCKSDB_LITE