YugabyteDB (2.13.0.0-b42, bfc6a6643e7399ac8a0e81d06a3ee6d6571b33ab)

Coverage Report

Created: 2022-03-09 17:30

/Users/deen/code/yugabyte-db/src/yb/yql/pggate/pg_txn_manager.h
Line
Count
Source
1
// Copyright (c) YugaByte, Inc.
2
//
3
// Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
4
// in compliance with the License.  You may obtain a copy of the License at
5
//
6
// http://www.apache.org/licenses/LICENSE-2.0
7
//
8
// Unless required by applicable law or agreed to in writing, software distributed under the License
9
// is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
10
// or implied.  See the License for the specific language governing permissions and limitations
11
// under the License.
12
//
13
14
// No include guards here because this file is expected to be included multiple times.
15
16
#ifndef YB_YQL_PGGATE_PG_TXN_MANAGER_H_
17
#define YB_YQL_PGGATE_PG_TXN_MANAGER_H_
18
19
#include <mutex>
20
21
#include "yb/client/client_fwd.h"
22
#include "yb/client/transaction.h"
23
#include "yb/common/clock.h"
24
#include "yb/common/transaction.h"
25
#include "yb/gutil/ref_counted.h"
26
27
#include "yb/tserver/pg_client.pb.h"
28
#include "yb/tserver/tserver_fwd.h"
29
#include "yb/tserver/tserver_util_fwd.h"
30
31
#include "yb/util/enums.h"
32
33
#include "yb/yql/pggate/pg_gate_fwd.h"
34
#include "yb/yql/pggate/pg_callbacks.h"
35
36
namespace yb {
37
namespace pggate {
38
39
// These should match XACT_READ_UNCOMMITED, XACT_READ_COMMITED, XACT_REPEATABLE_READ,
40
// XACT_SERIALIZABLE from xact.h. Please do not change this enum.
41
YB_DEFINE_ENUM(
42
  PgIsolationLevel,
43
  ((READ_UNCOMMITED, 0))
44
  ((READ_COMMITTED, 1))
45
  ((REPEATABLE_READ, 2))
46
  ((SERIALIZABLE, 3))
47
);
48
49
std::shared_ptr<yb::client::YBSession> BuildSession(
50
    yb::client::YBClient* client,
51
    const scoped_refptr<ClockBase>& clock = nullptr);
52
53
class PgTxnManager : public RefCountedThreadSafe<PgTxnManager> {
54
 public:
55
  PgTxnManager(PgClient* pg_client,
56
               scoped_refptr<ClockBase> clock,
57
               const tserver::TServerSharedObject* tserver_shared_object,
58
               PgCallbacks pg_callbacks);
59
60
  virtual ~PgTxnManager();
61
62
  CHECKED_STATUS BeginTransaction();
63
  CHECKED_STATUS CalculateIsolation(
64
      bool read_only_op, TxnPriorityRequirement txn_priority_requirement);
65
  CHECKED_STATUS RecreateTransaction();
66
  CHECKED_STATUS RestartTransaction();
67
  CHECKED_STATUS ResetTransactionReadPoint();
68
  CHECKED_STATUS RestartReadPoint();
69
  CHECKED_STATUS CommitTransaction();
70
  CHECKED_STATUS AbortTransaction();
71
  CHECKED_STATUS SetPgIsolationLevel(int isolation);
72
  PgIsolationLevel GetPgIsolationLevel();
73
  CHECKED_STATUS SetReadOnly(bool read_only);
74
  CHECKED_STATUS EnableFollowerReads(bool enable_follower_reads, int32_t staleness);
75
  CHECKED_STATUS SetDeferrable(bool deferrable);
76
  CHECKED_STATUS EnterSeparateDdlTxnMode();
77
  CHECKED_STATUS ExitSeparateDdlTxnMode(Commit commit);
78
79
599k
  bool IsDdlMode() const { return ddl_mode_; }
80
3.33M
  bool IsTxnInProgress() const { return txn_in_progress_; }
81
48.8k
  IsolationLevel GetIsolationLevel() const { return isolation_level_; }
82
562k
  bool ShouldUseFollowerReads() const { return read_time_for_follower_reads_.is_valid(); }
83
84
  void SetupPerformOptions(tserver::PgPerformOptionsPB* options);
85
86
 private:
87
  YB_STRONGLY_TYPED_BOOL(NeedsHigherPriorityTxn);
88
  YB_STRONGLY_TYPED_BOOL(SavePriority);
89
90
  void ResetTxnAndSession();
91
  void StartNewSession();
92
  Status UpdateReadTimeForFollowerReadsIfRequired();
93
  Status RecreateTransaction(SavePriority save_priority);
94
95
  static uint64_t NewPriority(TxnPriorityRequirement txn_priority_requirement);
96
97
  std::string TxnStateDebugStr() const;
98
99
  CHECKED_STATUS FinishTransaction(Commit commit);
100
101
  // ----------------------------------------------------------------------------------------------
102
103
  PgClient* client_;
104
  scoped_refptr<ClockBase> clock_;
105
  const tserver::TServerSharedObject* const tserver_shared_object_;
106
107
  bool txn_in_progress_ = false;
108
  IsolationLevel isolation_level_ = IsolationLevel::NON_TRANSACTIONAL;
109
  uint64_t txn_serial_no_ = 0;
110
  bool need_restart_ = false;
111
  bool need_defer_read_point_ = false;
112
  tserver::ReadTimeManipulation read_time_manipulation_ = tserver::ReadTimeManipulation::NONE;
113
114
  // Postgres transaction characteristics.
115
  PgIsolationLevel pg_isolation_level_ = PgIsolationLevel::REPEATABLE_READ;
116
  bool read_only_ = false;
117
  bool enable_follower_reads_ = false;
118
  uint64_t follower_read_staleness_ms_ = 0;
119
  HybridTime read_time_for_follower_reads_;
120
  bool deferrable_ = false;
121
122
  bool ddl_mode_ = false;
123
124
  // On a transaction conflict error we want to recreate the transaction with the same priority as
125
  // the last transaction. This avoids the case where the current transaction gets a higher priority
126
  // and cancels the other transaction.
127
  uint64_t priority_ = 0;
128
  SavePriority use_saved_priority_ = SavePriority::kFalse;
129
130
  std::unique_ptr<tserver::TabletServerServiceProxy> tablet_server_proxy_;
131
132
  PgCallbacks pg_callbacks_;
133
134
  DISALLOW_COPY_AND_ASSIGN(PgTxnManager);
135
};
136
137
}  // namespace pggate
138
}  // namespace yb
139
#endif // YB_YQL_PGGATE_PG_TXN_MANAGER_H_