CUBRID Engine  latest
mvcc_table.hpp
Go to the documentation of this file.
1 /*
2  * Copyright 2008 Search Solution Corporation
3  * Copyright 2016 CUBRID Corporation
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  *
17  */
18 
19 //
20 // MVCC table - transaction information required for multi-version concurrency control system
21 //
22 
23 #ifndef _MVCC_TABLE_H_
24 #define _MVCC_TABLE_H_
25 
26 #if !defined (SERVER_MODE) && !defined (SA_MODE)
27 #error Wrong Module
28 #endif
29 
30 #include "mvcc_active_tran.hpp"
31 #include "storage_common.h"
32 
33 #include <atomic>
34 #include <mutex>
35 
36 // forward declarations
37 struct log_tdes;
38 struct mvcc_info;
39 
41 {
42  using version_type = unsigned int;
43 
45  {
49  };
50 
52 
53  MVCCID m_last_completed_mvccid; // just for info
54  event_type m_event_type; // just for info
55  std::atomic<version_type> m_version;
56 
59 
60  void initialize ();
61  void finalize ();
62 };
63 
64 class mvcctable
65 {
66  public:
67  using lowest_active_mvccid_type = std::atomic<MVCCID>;
68 
69  mvcctable ();
70  ~mvcctable ();
71 
72  void initialize ();
73  void finalize ();
74 
75  void alloc_transaction_lowest_active ();
76  void reset_transaction_lowest_active (int tran_index);
77 
78  // mvcc_snapshot/mvcc_info functions
79  void build_mvcc_info (log_tdes &tdes);
80  void complete_mvcc (int tran_index, MVCCID mvccid, bool committed);
81  void complete_sub_mvcc (MVCCID mvccid);
82  MVCCID get_new_mvccid ();
83  void get_two_new_mvccid (MVCCID &first, MVCCID &second);
84 
85  bool is_active (MVCCID mvccid) const;
86 
87  void reset_start_mvccid (); // not thread safe
88 
89  MVCCID get_global_oldest_visible () const;
90  MVCCID update_global_oldest_visible ();
91  void lock_global_oldest_visible ();
92  void unlock_global_oldest_visible ();
93  bool is_global_oldest_visible_locked () const;
94 
95  private:
96 
97  static const size_t HISTORY_MAX_SIZE = 2048; // must be a power of 2
98  static const size_t HISTORY_INDEX_MASK = HISTORY_MAX_SIZE - 1;
99 
100  /* lowest active MVCCIDs - array of size NUM_TOTAL_TRAN_INDICES */
103  /* lowest active MVCCID */
105 
106  /* current transaction status */
108  /* transaction status history - array of size TRANS_STATUS_HISTORY_MAX_SIZE */
109  /* the position in transaction status history array */
110  std::atomic<size_t> m_trans_status_history_position;
112 
113  /* protect against getting new MVCCIDs concurrently */
114  std::mutex m_new_mvccid_lock; // theoretically, it may be replaced with atomic operations
115  /* protect against current transaction status modifications */
117 
118  std::atomic<MVCCID> m_oldest_visible;
119  std::atomic<size_t> m_ov_lock_count;
120 
121  mvcc_trans_status &next_trans_status_start (mvcc_trans_status::version_type &next_version, size_t &next_index);
122  void next_tran_status_finish (mvcc_trans_status &next_trans_status, size_t next_index);
123  void advance_oldest_active (MVCCID next_oldest_active);
124  MVCCID compute_oldest_visible_mvccid () const;
125 };
126 
127 #endif // !_MVCC_TABLE_H_
MVCCID m_last_completed_mvccid
Definition: mvcc_table.hpp:53
std::atomic< size_t > m_trans_status_history_position
Definition: mvcc_table.hpp:110
static API_MUTEX mutex
Definition: api_util.c:72
std::atomic< version_type > m_version
Definition: mvcc_table.hpp:55
std::atomic< MVCCID > m_oldest_visible
Definition: mvcc_table.hpp:118
size_t m_transaction_lowest_visible_mvccids_size
Definition: mvcc_table.hpp:102
std::mutex m_new_mvccid_lock
Definition: mvcc_table.hpp:114
UINT64 MVCCID
std::atomic< MVCCID > lowest_active_mvccid_type
Definition: mvcc_table.hpp:67
mvcc_trans_status * m_trans_status_history
Definition: mvcc_table.hpp:111
lowest_active_mvccid_type m_current_status_lowest_active_mvccid
Definition: mvcc_table.hpp:104
event_type m_event_type
Definition: mvcc_table.hpp:54
unsigned int version_type
Definition: mvcc_table.hpp:42
mvcc_trans_status m_current_trans_status
Definition: mvcc_table.hpp:107
lowest_active_mvccid_type * m_transaction_lowest_visible_mvccids
Definition: mvcc_table.hpp:101
std::mutex m_active_trans_mutex
Definition: mvcc_table.hpp:116
mvcc_active_tran m_active_mvccs
Definition: mvcc_table.hpp:51
std::atomic< size_t > m_ov_lock_count
Definition: mvcc_table.hpp:119