MySQL 8.4.9
Source Code Documentation
context.h
Go to the documentation of this file.
1/*
2 Copyright (c) 2019, 2026, Oracle and/or its affiliates.
3
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License, version 2.0,
6 as published by the Free Software Foundation.
7
8 This program is designed to work with certain software (including
9 but not limited to OpenSSL) that is licensed under separate terms,
10 as designated in a particular file or component or in included license
11 documentation. The authors of MySQL hereby grant you an additional
12 permission to link the program and your derivative works with the
13 separately licensed software that they have either included with
14 the program or referenced in the documentation.
15
16 This program is distributed in the hope that it will be useful,
17 but WITHOUT ANY WARRANTY; without even the implied warranty of
18 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 GNU General Public License, version 2.0, for more details.
20
21 You should have received a copy of the GNU General Public License
22 along with this program; if not, write to the Free Software
23 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */
24
25#ifndef BINLOG_MONITORING_CONTEXT_H
26#define BINLOG_MONITORING_CONTEXT_H
27
28#include <map>
29#include <memory>
30
31#include <sql/log_event.h>
32#include <sql/rpl_gtid.h>
33#include <stddef.h>
34#include <sys/types.h>
35
36namespace binlog {
37namespace monitoring {
38
39enum log_type { BINARY = 1, RELAY, UKNOWN };
40
41/**
42 This class represents the compression stats collected for a given combination
43 of log type and compression type.
44 */
46 public:
47 /**
48 This tuple contains information about a transaction:
49 - transaction id : string
50 - compressed bytes : uint64
51 - uncompressed bytes : uint64
52 - timestamp : uint64
53 */
55 std::tuple<std::string, uint64_t, uint64_t, uint64_t>;
56
57 /**
58 A constant and static instance of the transaction compression stats.
59 */
61
62 protected:
63 /**
64 Enum stating whether FIRST or LAST transaction.
65 */
66 enum enum_trx_type { FIRST = 0, LAST };
67
68 /**
69 The log type.
70 */
72
73 /**
74 The compression type.
75 */
78
79 /**
80 Counter that tracks how many transactions have been observed.
81 */
82 std::atomic<uint64_t> m_counter_transactions{0};
83
84 /**
85 Sum of all compressed bytes for all transactions observed through this
86 object instance.
87 */
88 std::atomic<uint64_t> m_counter_compressed_bytes{0};
89
90 /**
91 Sum of all uncompressed bytes for all transactions observed through this
92 object instance.
93 */
94 std::atomic<uint64_t> m_counter_uncompressed_bytes{0};
95
96 /**
97 This tuple contains information about the first transaction.
98 */
99 std::atomic<Compression_stats_trx_row *> m_first_transaction_stats{nullptr};
100
101 /**
102 This tuple contains information about the last transaction.
103 */
104 std::atomic<Compression_stats_trx_row *> m_last_transaction_stats{nullptr};
105
106 protected:
107 /**
108 This member function shall claim memory used for tracking
109 transaction stats.
110
111 @param type Whether the FIRST or the LAST transaction.
112 */
114
115 /**
116 This member function shall destroy the object data structures.
117 Used by the object destroyer.
118 */
119 void destroy();
120
121 /**
122 This member function shall return the compression stats for the given
123 transaction.
124
125 @param type the transaction to get the status for (either FIRST or LAST).
126
127 @return the compression stats for the given transaction.
128 */
130
131 public:
133 /**
134 Initializes the compression stats for the given log type and
135 compression type. It initializes the counters and transaction
136 stats to 0.
137 */
139
140 /**
141 Copies the contents of the object referenced as a parameter.
142
143 @param rhs The object to copy.
144 */
146
147 /**
148 The destructor of this row.
149 */
150 virtual ~Compression_stats();
151
152 /**
153 Updates the existing stats with the ones passed as argument.
154
155 @param gtid the transaction identifier as a string.
156 @param transaction_timestamp The transaction timestamp as seconds since the
157 epoch.
158 @param comp_bytes The compressed bytes counter for the given transaction.
159 @param uncomp_bytes The uncompressed bytes counter for the given
160 transaction.
161 */
162 void add(std::string gtid, uint64_t transaction_timestamp,
163 uint64_t comp_bytes, uint64_t uncomp_bytes);
164
165 /**
166 This member function shall reset the counters to zero and
167 clear the transaction stats for both FIRST and LAST transactions.
168 */
169 void reset();
170
171 /**
172 Gets the log type that this object instance is tracking.
173 */
174 log_type get_log_type() const;
175
176 /**
177 Gets the compression type that this object instance is tracking.
178 */
180
181 /**
182 Gets the number of transactions counted.
183 @return number of transactions counted.
184 */
185 uint64_t get_counter_transactions() const;
186
187 /**
188 Gets the sum of compressed bytes accounted for by this object instance.
189 @return sum of compressed bytes for this object instance.
190 */
191 uint64_t get_counter_compressed_bytes() const;
192
193 /**
194 Gets the sum of uncompressed bytes accounted for by this object instance.
195 @return sum of uncompressed bytes for this object instance.
196 */
197 uint64_t get_counter_uncompressed_bytes() const;
198
199 /**
200 Gets the stats for the last transaction.
201 @return the stats for the last transaction.
202 */
204
205 /**
206 Gets the stats of the first transaction.
207 @return the stats for the first transaction.
208 */
210};
211
213 protected:
214 /**
215 The map that contains rows of stats in the probe. A stats row is a
216 combination of log type and compression type.
217 */
218 std::map<std::pair<log_type, mysql::binlog::event::compression::type>,
221
222 /**
223 Allocates this probe's internal structures.
224 */
225 void init();
226
227 /**
228 Claims this probe's internal resources.
229 */
230 void destroy();
231
232 public:
233 /**
234 Update this probe's stats.
235
236 @param log_type the type of the log that this invocation refers to.
237 @param comp_type the compression type for this invocation.
238 @param gtid the transaction identifier for this invocation.
239 @param transaction_timestamp the transaction commit timestamp in seconds
240 since the UNIX epoch.
241 @param comp_bytes the bytes compressed by this transaction.
242 @param uncomp_bytes the bytes uncompressed by this transaction.
243 @param tsid_map the Tsid_map to use to create a string representation from
244 the transaction identifier provided.
245 */
248 uint64_t transaction_timestamp, uint64_t comp_bytes,
249 uint64_t uncomp_bytes, Tsid_map *tsid_map = global_tsid_map);
250
251 /**
252 Gets the contents of the probe. The contents are a copy of the internal
253 stats and as such, the caller must free the resources in stats once they are
254 no longer needed.
255
256 @param stats the container to fill in with copies of the stats in the probe.
257 */
258 void get_stats(std::vector<std::unique_ptr<Compression_stats>> &stats);
259
260 /**
261 Gets the number of stats in the probe. Each combination of log_type and
262 comp_type creates a row. Only those rows that have stats collected are
263 considered.
264
265 @return the number of combinations between log_type and comp_type that have
266 stats collected.
267 */
268 int number_stats_rows();
269
270 /**
271 Resets the stats of this probe to zero.
272 */
273 void reset();
274
275 /**
276 Constructor. The constructed object is reset after this returns.
277 */
279
280 /**
281 Destructor. Once the destructor returns the internal data structures have
282 been destroyed.
283 */
284 virtual ~Transaction_compression();
285};
286
287/**
288 The global context for binary/relay log monitoring.
289
290 @todo migrate the monitoring parts that are scattered all around
291 this this entry point.
292 */
293class Context {
294 protected:
296
297 public:
298 Context(const Context &rhs) = delete;
299 Context &operator=(const Context &rhs) = delete;
300
301 Context() = default;
302 virtual ~Context() = default;
303
305};
306
307} // namespace monitoring
308} // namespace binlog
309
310#endif
Represents a bidirectional map between TSID and SIDNO.
Definition: rpl_gtid.h:749
This class represents the compression stats collected for a given combination of log type and compres...
Definition: context.h:45
void reset()
This member function shall reset the counters to zero and clear the transaction stats for both FIRST ...
Definition: context.cc:185
void add(std::string gtid, uint64_t transaction_timestamp, uint64_t comp_bytes, uint64_t uncomp_bytes)
Updates the existing stats with the ones passed as argument.
Definition: context.cc:122
std::atomic< uint64_t > m_counter_compressed_bytes
Sum of all compressed bytes for all transactions observed through this object instance.
Definition: context.h:88
std::atomic< uint64_t > m_counter_transactions
Counter that tracks how many transactions have been observed.
Definition: context.h:82
std::atomic< Compression_stats_trx_row * > m_last_transaction_stats
This tuple contains information about the last transaction.
Definition: context.h:104
std::atomic< Compression_stats_trx_row * > m_first_transaction_stats
This tuple contains information about the first transaction.
Definition: context.h:99
uint64_t get_counter_compressed_bytes() const
Gets the sum of compressed bytes accounted for by this object instance.
Definition: context.cc:77
void destroy()
This member function shall destroy the object data structures.
Definition: context.cc:227
std::atomic< uint64_t > m_counter_uncompressed_bytes
Sum of all uncompressed bytes for all transactions observed through this object instance.
Definition: context.h:94
std::tuple< std::string, uint64_t, uint64_t, uint64_t > Compression_stats_trx_row
This tuple contains information about a transaction:
Definition: context.h:55
void destroy_transaction_stats(enum_trx_type type)
This member function shall claim memory used for tracking transaction stats.
Definition: context.cc:168
mysql::binlog::event::compression::type get_type() const
Gets the compression type that this object instance is tracking.
Definition: context.cc:69
uint64_t get_counter_transactions() const
Gets the number of transactions counted.
Definition: context.cc:73
log_type m_log_type
The log type.
Definition: context.h:71
Compression_stats_trx_row get_transaction_stats(enum_trx_type type)
This member function shall return the compression stats for the given transaction.
Definition: context.cc:86
mysql::binlog::event::compression::type m_type
The compression type.
Definition: context.h:76
uint64_t get_counter_uncompressed_bytes() const
Gets the sum of uncompressed bytes accounted for by this object instance.
Definition: context.cc:81
Compression_stats_trx_row get_first_transaction_stats()
Gets the stats of the first transaction.
Definition: context.cc:116
enum_trx_type
Enum stating whether FIRST or LAST transaction.
Definition: context.h:66
@ LAST
Definition: context.h:66
@ FIRST
Definition: context.h:66
log_type get_log_type() const
Gets the log type that this object instance is tracking.
Definition: context.cc:67
static const Compression_stats_trx_row & ZERO_TRX_ROW()
A constant and static instance of the transaction compression stats.
Definition: context.cc:36
Compression_stats_trx_row get_last_transaction_stats()
Gets the stats for the last transaction.
Definition: context.cc:111
virtual ~Compression_stats()
The destructor of this row.
Definition: context.cc:63
The global context for binary/relay log monitoring.
Definition: context.h:293
Transaction_compression m_transaction_compression_ctx
Definition: context.h:295
Context & operator=(const Context &rhs)=delete
Transaction_compression & transaction_compression()
Definition: context.cc:321
virtual ~Context()=default
Context(const Context &rhs)=delete
Transaction_compression()
Constructor.
Definition: context.cc:234
void reset()
Resets the stats of this probe to zero.
Definition: context.cc:270
int number_stats_rows()
Gets the number of stats in the probe.
Definition: context.cc:310
std::map< std::pair< log_type, mysql::binlog::event::compression::type >, Compression_stats * > m_stats
The map that contains rows of stats in the probe.
Definition: context.h:220
void update(log_type log_type, mysql::binlog::event::compression::type comp_type, Gtid &gtid, uint64_t transaction_timestamp, uint64_t comp_bytes, uint64_t uncomp_bytes, Tsid_map *tsid_map=global_tsid_map)
Update this probe's stats.
Definition: context.cc:275
virtual ~Transaction_compression()
Destructor.
Definition: context.cc:239
void init()
Allocates this probe's internal structures.
Definition: context.cc:244
void get_stats(std::vector< std::unique_ptr< Compression_stats > > &stats)
Gets the contents of the probe.
Definition: context.cc:300
void destroy()
Claims this probe's internal resources.
Definition: context.cc:263
Binary log event definitions.
Tsid_map * global_tsid_map
Definition: mysqld.cc:1832
log_type
Definition: context.h:39
@ RELAY
Definition: context.h:39
@ BINARY
Definition: context.h:39
@ UKNOWN
Definition: context.h:39
Definition: pfs.cc:38
@ NONE
Definition: base.h:45
std::vector< T, ut::allocator< T > > vector
Specialization of vector which uses allocator.
Definition: ut0new.h:2879
required string type
Definition: replication_group_member_actions.proto:34
TODO: Move this structure to mysql/binlog/event/control_events.h when we start using C++11.
Definition: rpl_gtid.h:1100
Definition: mysqlslap.cc:240