MySQL 8.4.2
Source Code Documentation
context.h
Go to the documentation of this file.
1/*
2 Copyright (c) 2019, 2024, 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
30#include <sql/log_event.h>
31#include <sql/rpl_gtid.h>
32#include <stddef.h>
33#include <sys/types.h>
34
35namespace binlog {
36namespace monitoring {
37
38enum log_type { BINARY = 1, RELAY, UKNOWN };
39
40/**
41 This class represents the compression stats collected for a given combination
42 of log type and compression type.
43 */
45 public:
46 /**
47 This tuple contains information about a transaction:
48 - transaction id : string
49 - compressed bytes : uint64
50 - uncompressed bytes : uint64
51 - timestamp : uint64
52 */
54 std::tuple<std::string, uint64_t, uint64_t, uint64_t>;
55
56 /**
57 A constant and static instance of the transaction compression stats.
58 */
60
61 protected:
62 /**
63 Enum stating whether FIRST or LAST transaction.
64 */
65 enum enum_trx_type { FIRST = 0, LAST };
66
67 /**
68 The log type.
69 */
71
72 /**
73 The compression type.
74 */
77
78 /**
79 Counter that tracks how many transactions have been observed.
80 */
81 std::atomic<uint64_t> m_counter_transactions{0};
82
83 /**
84 Sum of all compressed bytes for all transactions observed through this
85 object instance.
86 */
87 std::atomic<uint64_t> m_counter_compressed_bytes{0};
88
89 /**
90 Sum of all uncompressed bytes for all transactions observed through this
91 object instance.
92 */
93 std::atomic<uint64_t> m_counter_uncompressed_bytes{0};
94
95 /**
96 This tuple contains information about the first transaction.
97 */
98 std::atomic<Compression_stats_trx_row *> m_first_transaction_stats{nullptr};
99
100 /**
101 This tuple contains information about the last transaction.
102 */
103 std::atomic<Compression_stats_trx_row *> m_last_transaction_stats{nullptr};
104
105 protected:
106 /**
107 This member function shall claim memory used for tracking
108 transaction stats.
109
110 @param type Whether the FIRST or the LAST transaction.
111 */
113
114 /**
115 This member function shall destroy the object data structures.
116 Used by the object destroyer.
117 */
118 void destroy();
119
120 /**
121 This member function shall return the compression stats for the given
122 transaction.
123
124 @param type the transaction to get the status for (either FIRST or LAST).
125
126 @return the compression stats for the given transaction.
127 */
129
130 public:
132 /**
133 Initializes the compression stats for the given log type and
134 compression type. It initializes the counters and transaction
135 stats to 0.
136 */
138
139 /**
140 Copies the contents of the object referenced as a parameter.
141
142 @param rhs The object to copy.
143 */
145
146 /**
147 The destructor of this row.
148 */
149 virtual ~Compression_stats();
150
151 /**
152 Updates the existing stats with the ones passed as argument.
153
154 @param gtid the transaction identifier as a string.
155 @param transaction_timestamp The transaction timestamp as seconds since the
156 epoch.
157 @param comp_bytes The compressed bytes counter for the given transaction.
158 @param uncomp_bytes The uncompressed bytes counter for the given
159 transaction.
160 */
161 void add(std::string gtid, uint64_t transaction_timestamp,
162 uint64_t comp_bytes, uint64_t uncomp_bytes);
163
164 /**
165 This member function shall reset the counters to zero and
166 clear the transaction stats for both FIRST and LAST transactions.
167 */
168 void reset();
169
170 /**
171 Gets the log type that this object instance is tracking.
172 */
173 log_type get_log_type() const;
174
175 /**
176 Gets the compression type that this object instance is tracking.
177 */
179
180 /**
181 Gets the number of transactions counted.
182 @return number of transactions counted.
183 */
184 uint64_t get_counter_transactions() const;
185
186 /**
187 Gets the sum of compressed bytes accounted for by this object instance.
188 @return sum of compressed bytes for this object instance.
189 */
190 uint64_t get_counter_compressed_bytes() const;
191
192 /**
193 Gets the sum of uncompressed bytes accounted for by this object instance.
194 @return sum of uncompressed bytes for this object instance.
195 */
196 uint64_t get_counter_uncompressed_bytes() const;
197
198 /**
199 Gets the stats for the last transaction.
200 @return the stats for the last transaction.
201 */
203
204 /**
205 Gets the stats of the first transaction.
206 @return the stats for the first transaction.
207 */
209};
210
212 protected:
213 /**
214 The map that contains rows of stats in the probe. A stats row is a
215 combination of log type and compression type.
216 */
217 std::map<std::pair<log_type, mysql::binlog::event::compression::type>,
220
221 /**
222 Allocates this probe's internal structures.
223 */
224 void init();
225
226 /**
227 Claims this probe's internal resources.
228 */
229 void destroy();
230
231 public:
232 /**
233 Update this probe's stats.
234
235 @param log_type the type of the log that this invocation refers to.
236 @param comp_type the compression type for this invocation.
237 @param gtid the transaction identifier for this invocation.
238 @param transaction_timestamp the transaction commit timestamp in seconds
239 since the UNIX epoch.
240 @param comp_bytes the bytes compressed by this transaction.
241 @param uncomp_bytes the bytes uncompressed by this transaction.
242 @param tsid_map the Tsid_map to use to create a string representation from
243 the transaction identifier provided.
244 */
247 uint64_t transaction_timestamp, uint64_t comp_bytes,
248 uint64_t uncomp_bytes, Tsid_map *tsid_map = global_tsid_map);
249
250 /**
251 Gets the contents of the probe. The contents are a copy of the internal
252 stats and as such, the caller must free the resources in stats once they are
253 no longer needed.
254
255 @param stats the container to fill in with copies of the stats in the probe.
256 */
257 void get_stats(std::vector<Compression_stats *> &stats);
258
259 /**
260 Gets the number of stats in the probe. Each combination of log_type and
261 comp_type creates a row. Only those rows that have stats collected are
262 considered.
263
264 @return the number of combinations between log_type and comp_type that have
265 stats collected.
266 */
267 int number_stats_rows();
268
269 /**
270 Resets the stats of this probe to zero.
271 */
272 void reset();
273
274 /**
275 Constructor. The constructed object is reset after this returns.
276 */
278
279 /**
280 Destructor. Once the destructor returns the internal data structures have
281 been destroyed.
282 */
283 virtual ~Transaction_compression();
284};
285
286/**
287 The global context for binary/relay log monitoring.
288
289 @todo migrate the monitoring parts that are scattered all around
290 this this entry point.
291 */
292class Context {
293 protected:
295
296 public:
297 Context(const Context &rhs) = delete;
298 Context &operator=(const Context &rhs) = delete;
299
300 Context() = default;
301 virtual ~Context() = default;
302
304};
305
306} // namespace monitoring
307} // namespace binlog
308
309#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:44
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:87
std::atomic< uint64_t > m_counter_transactions
Counter that tracks how many transactions have been observed.
Definition: context.h:81
std::atomic< Compression_stats_trx_row * > m_last_transaction_stats
This tuple contains information about the last transaction.
Definition: context.h:103
std::atomic< Compression_stats_trx_row * > m_first_transaction_stats
This tuple contains information about the first transaction.
Definition: context.h:98
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:93
std::tuple< std::string, uint64_t, uint64_t, uint64_t > Compression_stats_trx_row
This tuple contains information about a transaction:
Definition: context.h:54
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:70
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:75
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:65
@ LAST
Definition: context.h:65
@ FIRST
Definition: context.h:65
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:292
Transaction_compression m_transaction_compression_ctx
Definition: context.h:294
Context & operator=(const Context &rhs)=delete
Transaction_compression & transaction_compression()
Definition: context.cc:320
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:309
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:219
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
void get_stats(std::vector< Compression_stats * > &stats)
Gets the contents of the probe.
Definition: context.cc:300
virtual ~Transaction_compression()
Destructor.
Definition: context.cc:239
void init()
Allocates this probe's internal structures.
Definition: context.cc:244
void destroy()
Claims this probe's internal resources.
Definition: context.cc:263
Binary log event definitions.
Tsid_map * global_tsid_map
Definition: mysqld.cc:1825
log_type
Definition: context.h:38
@ RELAY
Definition: context.h:38
@ BINARY
Definition: context.h:38
@ UKNOWN
Definition: context.h:38
Definition: pfs.cc:38
@ NONE
Definition: base.h:45
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