userver
C++ Async Framework
Toggle main menu visibility
Loading...
Searching...
No Matches
cluster.hpp
Go to the documentation of this file.
1
#
pragma
once
2
3
/// @file userver/storages/mysql/cluster.hpp
4
/// @copybrief storages::mysql::Cluster
5
6
#
include
<
memory
>
7
#
include
<
optional
>
8
9
#
include
<
userver
/
clients
/
dns
/
resolver_fwd
.
hpp
>
10
#
include
<
userver
/
components
/
component_fwd
.
hpp
>
11
#
include
<
userver
/
engine
/
deadline
.
hpp
>
12
#
include
<
userver
/
testsuite
/
mysql_control
.
hpp
>
13
#
include
<
userver
/
utils
/
optional_ref
.
hpp
>
14
#
include
<
userver
/
utils
/
statistics
/
writer
.
hpp
>
15
16
#
include
<
userver
/
storages
/
mysql
/
cluster_host_type
.
hpp
>
17
#
include
<
userver
/
storages
/
mysql
/
command_result_set
.
hpp
>
18
#
include
<
userver
/
storages
/
mysql
/
cursor_result_set
.
hpp
>
19
#
include
<
userver
/
storages
/
mysql
/
impl
/
bind_helper
.
hpp
>
20
#
include
<
userver
/
storages
/
mysql
/
options
.
hpp
>
21
#
include
<
userver
/
storages
/
mysql
/
query
.
hpp
>
22
#
include
<
userver
/
storages
/
mysql
/
statement_result_set
.
hpp
>
23
#
include
<
userver
/
storages
/
mysql
/
transaction
.
hpp
>
24
25
USERVER_NAMESPACE_BEGIN
26
27
namespace
storages::
mysql
{
28
29
namespace
settings {
30
struct
MysqlSettings;
31
}
32
33
namespace
infra::topology {
34
class
TopologyBase;
35
}
36
37
/// @ingroup userver_clients
38
///
39
/// @brief Client interface for a cluster of MySQL servers.
40
/// Usually retrieved from components::MySQL
41
class
Cluster
final
{
42
public
:
43
/// @brief Cluster constructor
44
///
45
/// @param testsuite_mysql_control testsuite overrides, see
46
/// @ref testsuite::MysqlControl; no overrides are applied if absent
47
Cluster
(
48
clients::
dns
::
Resolver
& resolver,
49
const
settings::MysqlSettings& settings,
50
const
components
::ComponentConfig& config,
51
utils
::
OptionalRef
<
const
testsuite::
MysqlControl
> testsuite_mysql_control = std::nullopt
52
);
53
/// @brief Cluster destructor
54
~
Cluster
();
55
56
/// @brief Executes a statement on a host of host_type with default deadline.
57
/// Fills placeholders of the statement with args..., `Args` are expected to
58
/// be of supported types.
59
/// See @ref scripts/docs/en/userver/mysql/supported_types.md for better
60
/// understanding of `Args` requirements.
61
///
62
/// UINVARIANTs on params count mismatch doesn't validate types.
63
template
<
typename
... Args>
64
StatementResultSet
Execute
(
ClusterHostType
host_type,
const
Query& query,
const
Args&... args)
const
;
65
66
/// @brief Executes a statement on a host of host_type with provided
67
/// CommandControl.
68
/// Fills placeholders of the statement with args..., `Args` are expected to
69
/// be of supported types.
70
/// See @ref scripts/docs/en/userver/mysql/supported_types.md for better understanding of `Args`
71
/// requirements.
72
///
73
/// UINVARIANTs on params count mismatch doesn't validate types.
74
///
75
/// @snippet mysql/tests/cluster.cpp uMySQL usage sample - Cluster Execute
76
template
<
typename
... Args>
77
StatementResultSet
Execute
(
78
OptionalCommandControl
command_control,
79
ClusterHostType
host_type,
80
const
Query& query,
81
const
Args&... args
82
)
const
;
83
84
/// @brief Executes a statement on a host of host_type with default deadline
85
///
86
/// Basically an alias for Execute(host_type, query, AsArgs<T>(row)),
87
/// where AsArgs is an imaginary function which passes fields of T as
88
/// variadic params. Handy for one-liner inserts.
89
/// See @ref scripts/docs/en/userver/mysql/supported_types.md for better
90
/// understanding of `T` requirements.
91
///
92
/// UINVARIANTs on params count mismatch, doesn't validate types.
93
template
<
typename
T>
94
StatementResultSet
ExecuteDecompose
(
ClusterHostType
host_type,
const
Query& query,
const
T& row)
const
;
95
96
/// @brief Executes a statement on a host of host_type with provided
97
/// CommandControl.
98
///
99
/// Basically an alias for Execute(command_control, host_type, query,
100
/// AsArgs<T>(row)), where AsArgs is an imaginary function which passes
101
/// fields of T as variadic params. Handy for one-liner inserts.
102
/// See @ref scripts/docs/en/userver/mysql/supported_types.md for better understanding of `T` requirements.
103
///
104
/// UINVARIANTs on params count mismatch, doesn't validate types.
105
///
106
/// @snippet mysql/tests/cluster.cpp uMySQL usage sample - Cluster ExecuteDecompose
107
template
<
typename
T>
108
StatementResultSet
ExecuteDecompose
(
109
OptionalCommandControl
command_control,
110
ClusterHostType
host_type,
111
const
Query& query,
112
const
T& row
113
)
const
;
114
115
/// @brief Executes a statement on a host of host_type with default deadline.
116
/// Fills placeholders of the statements with Container::value_type in a
117
/// bulk-manner.
118
/// Container is expected to be a std::Container, Container::value_type is
119
/// expected to be an aggregate of supported types.
120
/// See @ref scripts/docs/en/userver/mysql/supported_types.md for better
121
/// understanding of `Container::value_type` requirements.
122
///
123
/// @note Requires MariaDB 10.2.6+ as a server
124
///
125
/// UINVARIANTs on params count mismatch, doesn't validate types.
126
/// UINVARIANTs on empty params container.
127
template
<
typename
Container>
128
StatementResultSet
ExecuteBulk
(
ClusterHostType
host_type,
const
Query& query,
const
Container& params)
const
;
129
130
/// @brief Executes a statement on a host of host_type with provided
131
/// CommandControl.
132
/// Fills placeholders of the statements with
133
/// Container::value_type in a bulk-manner.
134
/// Container is expected to be a std::Container, Container::value_type is
135
/// expected to be an aggregate of supported types.
136
/// See @ref scripts/docs/en/userver/mysql/supported_types.md for better understanding of
137
/// `Container::value_type` requirements.
138
///
139
/// @note Requires MariaDB 10.2.6+ as a server
140
///
141
/// UINVARIANTs on params count mismatch, doesn't validate types.
142
/// UINVARIANTs on empty params container.
143
/// @snippet mysql/tests/cluster.cpp uMySQL usage sample - Cluster ExecuteBulk
144
template
<
typename
Container>
145
StatementResultSet
ExecuteBulk
(
146
OptionalCommandControl
command_control,
147
ClusterHostType
host_type,
148
const
Query& query,
149
const
Container& params
150
)
const
;
151
152
// TODO : don't require Container to be const, so Convert can move
153
// clang-format off
154
/// @brief Executes a statement on a host of host_type with default deadline,
155
/// on the flight remapping from `Container::value_type` to `MapTo`.
156
/// `Container` is expected to be a std::Container of whatever type pleases
157
/// you, `MapTo` is expected to be an aggregate of supported types.
158
/// See @ref scripts/docs/en/userver/mysql/supported_types.md for better understanding of `MapTo` requirements.
159
/// You are expected to provide a converter function
160
/// `MapTo Convert(const Container::value_type&, storages::mysql::convert::To<MapTo>)`
161
/// in namespace of `MapTo` or storages::mysql::convert.
162
///
163
/// @note Requires MariaDB 10.2.6+ as a server
164
///
165
/// UINVARIANTs on params count mismatch, doesn't validate types.
166
/// UINVARIANTs on empty params container.
167
template
<
typename
MapTo,
typename
Container>
168
StatementResultSet
ExecuteBulkMapped
(
ClusterHostType
host_type,
169
const
Query& query,
170
const
Container& params)
const
;
171
// clang-format on
172
173
// TODO : don't require Container to be const, so Convert can move
174
/// @brief Executes a statement on a host of host_type with provided
175
/// CommandControl, on the flight remapping from `Container::value_type`
176
/// to `MapTo`.
177
/// `Container` is expected to be a std::Container of whatever type pleases
178
/// you, `MapTo` is expected to be an aggregate of supported types.
179
/// See @ref scripts/docs/en/userver/mysql/supported_types.md for better understanding of `MapTo` requirements.
180
/// You are expected to provide a converter function
181
/// `MapTo Convert(const Container::value_type&, storages::mysql::convert::To<MapTo>)`
182
/// in namespace of `MapTo` or storages::mysql::convert.
183
///
184
/// @note Requires MariaDB 10.2.6+ as a server
185
///
186
/// UINVARIANTs on params count mismatch, doesn't validate types.
187
/// UINVARIANTs on empty params container.
188
///
189
/// @snippet mysql/tests/cluster.cpp uMySQL usage sample - Cluster ExecuteBulkMapped
190
template
<
typename
MapTo,
typename
Container>
191
StatementResultSet
ExecuteBulkMapped
(
192
OptionalCommandControl
command_control,
193
ClusterHostType
host_type,
194
const
Query& query,
195
const
Container& params
196
)
const
;
197
198
/// @brief Begin a transaction with default deadline.
199
///
200
/// @note The deadline is transaction-wide, not just for Begin query itself.
201
///
202
/// @param host_type Host type on which to execute transaction.
203
Transaction
Begin
(
ClusterHostType
host_type)
const
;
204
205
/// @brief Being a transaction with specified CommandControl.
206
///
207
/// @note The deadline is transaction-wide, not just for Begin query itself.
208
///
209
/// @param command_control Optional request QOS overrides.
210
/// @param host_type Host type on which to execute transaction.
211
Transaction
Begin
(
OptionalCommandControl
command_control,
ClusterHostType
host_type)
const
;
212
213
/// @brief Executes a command on host of type host_type over plan-text
214
/// protocol, with default deadline.
215
///
216
/// This method is intended to be used for statements that cannot be prepared
217
/// or as an escape hatch from typed parsing if you really need to, but such
218
/// use is neither recommended nor optimized for.
219
CommandResultSet
ExecuteCommand
(
ClusterHostType
host_type,
const
Query& command)
const
;
220
221
/// @brief Executes a command on host of type host_type over plan-text
222
/// protocol, with provided CommandControl.
223
///
224
/// This method is intended to be used for statements that cannot be prepared
225
/// or as an escape hatch from typed parsing if you really need to, but such
226
/// use is neither recommended nor optimized for.
227
///
228
/// @snippet mysql/tests/cluster.cpp uMySQL usage sample - Cluster ExecuteCommand
229
CommandResultSet
ExecuteCommand
(
230
OptionalCommandControl
command_control,
231
ClusterHostType
host_type,
232
const
Query& command
233
)
const
;
234
235
/// @brief Executes a statement with default deadline on a host of host_type,
236
/// filling statements placeholders with `args...`, and returns a read-only
237
/// cursor which fetches `batch_count` rows in each next fetch request.
238
/// See @ref scripts/docs/en/userver/mysql/supported_types.md for better
239
/// understanding of `Args` requirements.
240
///
241
/// @note Deadline is processing-wide, not just for initial cursor creation.
242
///
243
/// UINVARIANTs on params count mismatch, doesn't validate types.
244
template
<
typename
T,
typename
... Args>
245
CursorResultSet<T>
GetCursor
(
246
ClusterHostType
host_type,
247
std::size_t batch_size,
248
const
Query& query,
249
const
Args&... args
250
)
const
;
251
252
/// @brief Executes a statement with provided CommandControl on
253
/// a host of host_type, filling statements placeholders with `args...`, and
254
/// returns a read-only cursor which fetches `batch_count` rows in each next
255
/// fetch request.
256
/// See @ref scripts/docs/en/userver/mysql/supported_types.md for better understanding of `Args`
257
/// requirements.
258
///
259
/// @note Deadline is processing-wide, not just for initial cursor creation.
260
///
261
/// UINVARIANTs on params count mismatch, doesn't validate types.
262
///
263
/// @snippet mysql/tests/cluster.cpp uMySQL usage sample - Cluster GetCursor
264
template
<
typename
T,
typename
... Args>
265
CursorResultSet<T>
GetCursor
(
266
OptionalCommandControl
command_control,
267
ClusterHostType
host_type,
268
std::size_t batch_size,
269
const
Query& query,
270
const
Args&... args
271
)
const
;
272
273
/// Write cluster statistics
274
void
WriteStatistics
(
utils
::
statistics
::Writer& writer)
const
;
275
276
private
:
277
static
CommandControl GetDefaultCommandControl();
278
279
StatementResultSet DoExecute(
280
OptionalCommandControl
command_control,
281
ClusterHostType
host_type,
282
const
Query& query,
283
impl::io::ParamsBinderBase& params,
284
std::optional<std::size_t> batch_size
285
)
const
;
286
287
std::unique_ptr<infra::topology::TopologyBase> topology_;
288
};
289
290
template
<
typename
... Args>
291
StatementResultSet Cluster::
Execute
(
ClusterHostType
host_type,
const
Query& query,
const
Args&... args)
const
{
292
return
Execute(std::nullopt, host_type, query, args...);
293
}
294
295
template
<
typename
... Args>
296
StatementResultSet Cluster::
Execute
(
297
OptionalCommandControl
command_control,
298
ClusterHostType
host_type,
299
const
Query& query,
300
const
Args&... args
301
)
const
{
302
auto
params_binder = impl::BindHelper::BindParams(args...);
303
304
return
DoExecute(command_control, host_type, query, params_binder, std::nullopt);
305
}
306
307
template
<
typename
T>
308
StatementResultSet Cluster::
ExecuteDecompose
(
ClusterHostType
host_type,
const
Query& query,
const
T& row)
const
{
309
return
ExecuteDecompose(std::nullopt, host_type, query, row);
310
}
311
312
template
<
typename
T>
313
StatementResultSet Cluster::
ExecuteDecompose
(
314
OptionalCommandControl
command_control,
315
ClusterHostType
host_type,
316
const
Query& query,
317
const
T& row
318
)
const
{
319
auto
params_binder = impl::BindHelper::BindRowAsParams(row);
320
321
return
DoExecute(command_control, host_type, query, params_binder, std::nullopt);
322
}
323
324
template
<
typename
Container>
325
StatementResultSet Cluster::
ExecuteBulk
(
ClusterHostType
host_type,
const
Query& query,
const
Container& params)
const
{
326
return
ExecuteBulk(std::nullopt, host_type, query, params);
327
}
328
329
template
<
typename
Container>
330
StatementResultSet Cluster::
ExecuteBulk
(
331
OptionalCommandControl
command_control,
332
ClusterHostType
host_type,
333
const
Query& query,
334
const
Container& params
335
)
const
{
336
UINVARIANT
(!params.empty(),
"Empty params in bulk execution"
);
337
338
auto
params_binder = impl::BindHelper::BindContainerAsParams(params);
339
340
return
DoExecute(command_control, host_type, query, params_binder, std::nullopt);
341
}
342
343
template
<
typename
MapTo,
typename
Container>
344
StatementResultSet Cluster::
ExecuteBulkMapped
(
ClusterHostType
host_type,
const
Query& query,
const
Container& params)
345
const
{
346
return
ExecuteBulkMapped<MapTo>(std::nullopt, host_type, query, params);
347
}
348
349
template
<
typename
MapTo,
typename
Container>
350
StatementResultSet Cluster::
ExecuteBulkMapped
(
351
OptionalCommandControl
command_control,
352
ClusterHostType
host_type,
353
const
Query& query,
354
const
Container& params
355
)
const
{
356
UINVARIANT
(!params.empty(),
"Empty params in bulk execution"
);
357
358
auto
params_binder = impl::BindHelper::BindContainerAsParamsMapped<MapTo>(params);
359
360
return
DoExecute(command_control, host_type, query, params_binder, std::nullopt);
361
}
362
363
template
<
typename
T,
typename
... Args>
364
CursorResultSet<T> Cluster::
GetCursor
(
365
ClusterHostType
host_type,
366
std::size_t batch_size,
367
const
Query& query,
368
const
Args&... args
369
)
const
{
370
return
GetCursor<T>(std::nullopt, host_type, batch_size, query, args...);
371
}
372
373
template
<
typename
T,
typename
... Args>
374
CursorResultSet<T> Cluster::
GetCursor
(
375
OptionalCommandControl
command_control,
376
ClusterHostType
host_type,
377
std::size_t batch_size,
378
const
Query& query,
379
const
Args&... args
380
)
const
{
381
auto
params_binder = impl::BindHelper::BindParams(args...);
382
383
return
CursorResultSet<T>{DoExecute(command_control, host_type, query, params_binder, batch_size)};
384
}
385
386
}
// namespace storages::mysql
387
388
USERVER_NAMESPACE_END
userver
storages
mysql
cluster.hpp
Generated on
for userver by
Doxygen
1.17.0