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