MySQL 8.0.39
Source Code Documentation
bgc_ticket.h
Go to the documentation of this file.
1/* Copyright (c) 2022, 2024, Oracle and/or its affiliates.
2
3 This program is free software; you can redistribute it and/or modify
4 it under the terms of the GNU General Public License, version 2.0,
5 as published by the Free Software Foundation.
6
7 This program is designed to work with certain software (including
8 but not limited to OpenSSL) that is licensed under separate terms,
9 as designated in a particular file or component or in included license
10 documentation. The authors of MySQL hereby grant you an additional
11 permission to link the program and your derivative works with the
12 separately licensed software that they have either included with
13 the program or referenced in the documentation.
14
15 This program is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 GNU General Public License, version 2.0, for more details.
19
20 You should have received a copy of the GNU General Public License
21 along with this program; if not, write to the Free Software
22 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */
23
24#ifndef BINLOG_BCG_TICKET_H
25#define BINLOG_BCG_TICKET_H
26
27#include <cstdint>
28#include <functional>
29#include <limits>
30#include <memory>
31#include <ostream>
32
34
35namespace binlog {
36
37class AtomicBgcTicket;
38class BgcTicketGuard;
39
40/// @brief Represents the Binlog Group Commit Ticket - BGC Ticket.
41/// @details Context: BGC ticket is granted to sessions with the same View,
42/// which may be changed with the View Change Event (server connecting to /
43/// disconnecting from group). When View Change Event is generated, it is
44/// granted a separate ticket value (value+1). All sessions that should be
45/// executed after View Change Event are assigned with a ticket value + 2.
46/// Implementation: Ticket is composed of 1 synchronization bit, used for
47/// critical section implementation and 63 bits that represent a ticket value.
48/// This class contains operation common for BGC tickets, such as: value
49/// extraction, changing synchronization bit between 0 and 1, ticket comparison,
50/// formatting, incrementation, internal constants. After reachning the maximum
51/// value for a ticket, ticket value is wrapped up to the first valid ticket
52/// value.
53/// @see Bgc_ticket_manager
54class BgcTicket {
55 public:
56 using ValueType = std::uint64_t;
57
58 /// @brief Default value of ticket, which means it is not being used.
59 static constexpr BgcTicket::ValueType kTicketUnset = 0;
60
61 /// @brief Constructor
62 /// @param[in] ticket Pattern to copy from
63 explicit BgcTicket(const ValueType &ticket = first_ticket_value);
64
65 friend class AtomicBgcTicket;
66
67 /// @brief "Less than" operator
68 /// @param[in] lhs Left hand side BgcTicket argument (const ref)
69 /// @param[in] rhs Right hand side BgcTicket argument (const ref)
70 friend bool operator<(const BgcTicket &lhs, const BgcTicket &rhs);
71 /// @brief "Greater than" operator
72 /// @param[in] lhs Left hand side BgcTicket argument (const ref)
73 /// @param[in] rhs Right hand side BgcTicket argument (const ref)
74 /// @retval true lhs > rhs
75 /// @retval false rhs <= lhs
76 friend bool operator>(const BgcTicket &lhs, const BgcTicket &rhs);
77
78 /// @brief Comparison operator (>=)
79 /// @param[in] lhs Left hand side BgcTicket argument (const ref)
80 /// @param[in] rhs Right hand side BgcTicket argument (const ref)
81 /// @retval true lhs >= rhs
82 /// @retval false !(rhs >= lhs)
83 friend bool operator>=(const BgcTicket &lhs, const BgcTicket &rhs);
84
85 /// @brief Comparison operator
86 /// @details Comparison without (!) first bit, meaning that if some thread
87 /// locked on lhs or rhs, tickets can still be equal
88 /// @param[in] lhs Left hand side BgcTicket argument (const ref)
89 /// @param[in] rhs Right hand side BgcTicket argument (const ref)
90 /// @retval true lhs == rhs
91 /// @retval false rhs != lhs
92 friend bool operator==(const BgcTicket &lhs, const BgcTicket &rhs);
93
94 /// @brief Comparison operator
95 /// @details Comparison without (!) first bit
96 /// @param[in] lhs Left hand side BgcTicket argument (const ref)
97 /// @param[in] rhs Right hand side BgcTicket argument (const ref)
98 /// @retval true lhs != rhs
99 /// @retval false rhs == lhs
100 friend bool operator!=(const BgcTicket &lhs, const BgcTicket &rhs);
101
102 /// @brief Obtain ticket value (1st bit cleared out)
103 /// @return Ticket value
104 ValueType get_value() const;
105
106 /// @brief Obtain ticket, may be in use or not (cannot be easily compared)
107 /// @details Cannot be easily compared, use comparison operator from BgcTicket
108 /// instead
109 /// @returns Internal value of the ticket (value+sync bit)
110 const ValueType &get() const { return m_ticket; }
111
112 /// @brief sets synchronization bit to 1, only 1 thread can operate on this
113 /// ticket
114 void set_in_use();
115 /// @brief sets synchronization bit to 0, other thread may "lock" this ticket
116 /// for writing
117 void set_used();
118 /// @brief checks whether this ticket is "locked" for writing
119 /// @retval true Ticket is in use
120 /// @retval false Ticket is not in use
121 bool is_in_use() const;
122
123 /// @brief Returns incremented ticket, this remain unchanged
124 /// @returns Incremented ticket
125 BgcTicket next() const;
126
127 /// @brief Increments this ticket value
128 void set_next();
129
130 /// @brief Checks whether the ticket value differs from kTicketUnset
131 /// @retval true Ticket value is set
132 /// @retval false Ticket value is unset
133 bool is_set() const;
134
135 /// @brief Stream operator impl for BgcTicket class
136 /// @param[in] os Reference to stream obj
137 /// @param[in] arg Constant reference to BgcTicket object
138 /// @returns Reference to changed stream obj
139 friend std::ostream &operator<<(std::ostream &os, const BgcTicket &arg);
140
141 public:
142 /// 64-bit mask with all 0s except for the most significant bit, to extract
143 /// synchronization bit
144 static constexpr ValueType set_bit = 1ULL << 63;
145 /// 64-bit mask with all 0s except for the most significant bit, to clear out
146 /// synchronization bit - get ticket value
147 static constexpr ValueType clear_bit = set_bit - 1;
148 /// Maximum allowed value for a ticket, after which will wrap around to
149 /// first_ticket_value
150 static constexpr ValueType max_ticket_value =
151 std::numeric_limits<ValueType>::max() & clear_bit;
152 /// Minimum allowed value for a ticket
153 static constexpr ValueType first_ticket_value = 1;
154
155 protected:
156 ValueType m_ticket; ///< 1 bit for synchronization, 63 bits - ticket value
157};
158
159} // namespace binlog
160
161#endif // BINLOG_BCG_TICKET_H
Implements atomic ops on BgcTicket object.
Definition: atomic_bgc_ticket.h:43
Represents the Binlog Group Commit Ticket - BGC Ticket.
Definition: bgc_ticket.h:54
friend bool operator<(const BgcTicket &lhs, const BgcTicket &rhs)
"Less than" operator
Definition: bgc_ticket.cc:57
static constexpr ValueType first_ticket_value
Minimum allowed value for a ticket.
Definition: bgc_ticket.h:153
static constexpr ValueType max_ticket_value
Maximum allowed value for a ticket, after which will wrap around to first_ticket_value.
Definition: bgc_ticket.h:150
BgcTicket(const ValueType &ticket=first_ticket_value)
Constructor.
Definition: bgc_ticket.cc:29
void set_next()
Increments this ticket value.
Definition: bgc_ticket.cc:55
friend bool operator!=(const BgcTicket &lhs, const BgcTicket &rhs)
Comparison operator.
Definition: bgc_ticket.cc:73
void set_used()
sets synchronization bit to 0, other thread may "lock" this ticket for writing
Definition: bgc_ticket.cc:39
const ValueType & get() const
Obtain ticket, may be in use or not (cannot be easily compared)
Definition: bgc_ticket.h:110
std::uint64_t ValueType
Definition: bgc_ticket.h:56
bool is_in_use() const
checks whether this ticket is "locked" for writing
Definition: bgc_ticket.cc:40
friend bool operator>=(const BgcTicket &lhs, const BgcTicket &rhs)
Comparison operator (>=)
Definition: bgc_ticket.cc:64
ValueType get_value() const
Obtain ticket value (1st bit cleared out)
Definition: bgc_ticket.cc:35
friend bool operator==(const BgcTicket &lhs, const BgcTicket &rhs)
Comparison operator.
Definition: bgc_ticket.cc:68
ValueType m_ticket
1 bit for synchronization, 63 bits - ticket value
Definition: bgc_ticket.h:156
friend bool operator>(const BgcTicket &lhs, const BgcTicket &rhs)
"Greater than" operator
Definition: bgc_ticket.cc:62
static constexpr ValueType set_bit
64-bit mask with all 0s except for the most significant bit, to extract synchronization bit
Definition: bgc_ticket.h:144
friend std::ostream & operator<<(std::ostream &os, const BgcTicket &arg)
Stream operator impl for BgcTicket class.
Definition: bgc_ticket.cc:50
BgcTicket next() const
Returns incremented ticket, this remain unchanged.
Definition: bgc_ticket.cc:42
bool is_set() const
Checks whether the ticket value differs from kTicketUnset.
Definition: bgc_ticket.cc:31
static constexpr ValueType clear_bit
64-bit mask with all 0s except for the most significant bit, to clear out synchronization bit - get t...
Definition: bgc_ticket.h:147
void set_in_use()
sets synchronization bit to 1, only 1 thread can operate on this ticket
Definition: bgc_ticket.cc:38
static constexpr BgcTicket::ValueType kTicketUnset
Default value of ticket, which means it is not being used.
Definition: bgc_ticket.h:59
Definition: decompressing_event_object_istream.cc:31