MySQL 8.0.37
Source Code Documentation
reference_caching.h
Go to the documentation of this file.
1/* Copyright (c) 2020, 2024, Oracle and/or its affiliates.
2
3This program is free software; you can redistribute it and/or modify
4it under the terms of the GNU General Public License, version 2.0,
5as published by the Free Software Foundation.
6
7This program is designed to work with certain software (including
8but not limited to OpenSSL) that is licensed under separate terms,
9as designated in a particular file or component or in included license
10documentation. The authors of MySQL hereby grant you an additional
11permission to link the program and your derivative works with the
12separately licensed software that they have either included with
13the program or referenced in the documentation.
14
15This program is distributed in the hope that it will be useful,
16but WITHOUT ANY WARRANTY; without even the implied warranty of
17MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18GNU General Public License, version 2.0, for more details.
19
20You should have received a copy of the GNU General Public License
21along with this program; if not, write to the Free Software
22Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */
23
24#ifndef REFERENCE_CACHING_H
25#define REFERENCE_CACHING_H
26
29
30/**
31 Handle for "producer channels".
32 A producer channel is the singleton used to emit events that are subject
33 to reference caching.
34 This is needed to hold a flag that will trigger forced invalidation of all
35 caches in a particular channel on their next access.
36 Multi threaded access is expected to be safe and lock less to this handle
37 once it's instantiated.
38*/
40
41/**
42 A handle of a "reference cache". This is created by all events producer
43 threads when they start and is maintained throughout their lifetimes.
44 Operations on that are not thread safe and are assumed to be done by a
45 single thread.
46*/
48
49/**
50 @ingroup group_components_services_inventory
51*/
52
53/**
54 A reference caching channel service.
55
56 The goal of this service is to ensure that event producers spend
57 a MINIMAL amount of time in the emitting code when there are no consumers of
58 the produced events.
59
60 Terminology
61 -----------
62
63 An event consumer is an implementation of a component service.
64 An event producer is a piece of code that wants to inform all currently
65 registered event consumers about an event.
66 A channel is a device that serves as a singleton of all reference caches
67 for all threads that are to produce events.
68 A reference cache is something each thread producing events must maintain
69 for its lifetime and use it to find the references to event consumers it
70 needs to call.
71
72 Typical lifetime of the event consumers
73 ---------------------------------------
74
75 At init time the event consumer will register implementations of any of
76 the services it's interested in receiving notifications for.
77
78 Then optionally it might force a channel invalidation to make sure all
79 existing event producers will start sending notifications to it immediately.
80 Or it can just sit and wait for the natural producer's reference cache
81 refresh cycles to kick in.
82
83 Now it is receiving notifications from all event producers as they come.
84
85 When it wishes to no longer receive notifications it needs to mark itself
86 as invisible for reference cache fill-ins. In this way all reference cache
87 flushes will not pick this implementation up even if it's still registered
88 in the registry.
89
90 Eventually all active references will be removed by the natural producers
91 flush cycles.
92 The consumer may choose to expedite this by triggering a channel
93 invalidation.
94
95 As with all service implementations, when all references to the services
96 are released it can unload.
97
98 Typical lifetime of the event producers
99 ---------------------------------------
100
101 An event producer will usually start at init time by creating channel(s).
102
103 Then, for each thread wishing to produce events, a reference cache must
104 be created and maintained until the thread will no longer be producing
105 events.
106
107 Now the thread can produce events using the reference cache.
108 This is done by calling the get method and then iterating over the
109 resulting set of references and calling each one in turn as one would
110 normally do for registry service references.
111 It is assumed that the references are owned by the cache and thus they
112 should not be released.
113
114 With some cyclical (e.g. at the end of each statement or something)
115 the event producing thread needs to flush the cache. This is to ensure
116 that references to event consumers are not held for very long and that
117 new event consumers are picked up. However flushing the cache is a relatively
118 expensive operation and thus a balance between the number of events produced
119 and the cache being flushed must be achieved.
120
121 General remarks
122 ---------------
123
124 Channels are something each event producer must have to produce events.
125 Channels are to be created by a single thread before the first event is ever
126 produced. And, once created they are to be kept until after the last event is
127 produced.
128
129 Channels serve as singletons for caches and you only need one channel instance
130 per event producer component.
131 There usually will be multiple caches (one per event producing thread) per
132 channel.
133
134 Creating and destroying a channel is a relatively "expensive" operation that
135 might involve some synchronization and should not be done frequently.
136
137 Channels exist to allow a non-related thread to trigger invalidation of all
138 currently active caches on that channel. This is necessary when for example
139 event consumers are registered and are about to be removed.
140
141 Invalidating a channel is a thread-safe operation that can be invoked without
142 synchronization at any time.
143
144 Each channel is associated with a specific set of service names.
145 @note It is a set of service names, not implementation names !
146
147 The names are stored and used in event caches to handle all implementations
148 of that particular service.
149*/
151/**
152 Creates a channel and returns a handle for it.
153
154 The handle created by this method must be destroyed by the invalidate
155 method otherwise there might be leaks.
156
157 The channel should be created before any events are to be produced on it.
158
159 @param service_names a list of service names that this channel will operate
160 on. Terminated by a NULL pointer
161 @param[out] out_channel placeholder for the newly created handle.
162 @retval false success
163 @retval true failure
164*/
165DECLARE_BOOL_METHOD(create, (const char *service_names[],
167
168/**
169 Destroys a channel handle
170
171 Should make sure no other thread is using the handle and no caches are
172 allocated on it.
173
174 @param channel the handle to destroy
175 @retval false success
176 @retval true failure
177*/
179
180/**
181 Invalidate a channel
182
183 This is thread safe to call without synchronization
184 and relatively fast.
185
186 Forces an asynchronous flush on all caches that are allocated on
187 that channel when they're next accessed.
188
189 @param channel the handle to destroy
190 @retval false success
191 @retval true failure
192*/
194
195/**
196 Validate a channel
197
198 This is thread safe to call without synchronization
199 and relatively fast.
200
201 This function is used to validate the channel. Which helps in
202 getting the cached service references on that channel when they're
203 next accessed.
204
205 @param channel the handle to destroy
206 @retval false success
207 @retval true failure
208*/
210
211/**
212 Fetches a reference caching channel by name.
213
214 Usually consumers wishing to force reference cache flush would
215 fetch the channel handle so they can then call invalidate on it.
216
217 This is a relatively expensive operation as it might involve some
218 synchronization.
219
220 @param service_name a service name that this channel will operate on.
221 @param[out] out_channel placeholder or NULL if not found.
222 @retval false success
223 @retval true failure
224*/
225DECLARE_BOOL_METHOD(fetch, (const char *service_name,
227
229
230/**
231 A service to maintain an "ignore list" for reference caches.
232
233 When a service implementation is on that ignore list it will never be
234 added to any reference caches when they're filled in even if the service
235 implementation is in the registry and implements the service
236 the reference caching channel is operating on.
237
238 This is just a list of "implementations", i.e. the part of the service
239 implementation name after the dot.
240 The channel already has the name of the service so a full implementation
241 name can be constructed if needed.
242*/
243BEGIN_SERVICE_DEFINITION(reference_caching_channel_ignore_list)
244/**
245 Adds an implementation name to the ignore list.
246
247 @param channel the channel to add the ignored implementation to.
248 @param implementation_name the second part of the service implementation
249 name (the part after the dot).
250 @retval false successfully added
251 @retval true error adding (e.g. the ignore is present etc.
252*/
254 const char *implementation_name));
255/**
256 Remove an implementation name to the ignore list.
257
258 @param channel the channel to remove the ignored implementation from.
259 @param implementation_name the second part of the service implementation
260 name (the part after the dot).
261 @retval false successfully removed
262 @retval true error removing or not present
263*/
265 const char *implementation_name));
266/**
267 Empty the ignore list.
268
269 @param channel the channel to remove the ignored implementation from.
270 @retval false successfully removed all ignores
271 @retval true error removing the ignores. State unknown.
272*/
274END_SERVICE_DEFINITION(reference_caching_channel_ignore_list)
275
276/**
277 Reference cache service.
278
279 See the reference_caching_channel service for details on how to
280 operate this.
281
282 Manages thread caches for event producer threads.
283*/
285/**
286 Create a reference cache.
287
288 Needs to be called before the first get() or flush() is called.
289 Each call to create must be paired with a call to destroy() or
290 there will be leaks.
291
292 @param channel the reference cache channel to operate on.
293 @param registry a handle to the registry so that no time is spent taking it
294 @param[out] handle of the newly allocated cache.
295 @retval false success
296 @retval true failure
297*/
299 SERVICE_TYPE(registry) * registry,
301/**
302 Destroy a reference cache.
303
304 Needs to be called to dispose of each cache allocated by create().
305 Needs to be called after all possible calls to get() and flush().
306 Once called the cache handle is invalid and should not be used anymore.
307
308 @param channel the reference cache channel to operate on.
309 @param[out] handle of the newly allocated cache.
310 @retval false success
311 @retval true failure
312*/
314
315/**
316 Gets a set of service references for an event producer to call.
317
318 This is the main event producer function that will be called when the
319 event producer needs to produce an event.
320
321 The cache must be a valid one. And the channel too.
322
323 If the cache is empty or invalidated (either via the channel or via a call
324 to flush) it will try to fill it by consulting the registry and acquiring
325 references to all implementations of the service the channel is created for.
326 Once that is done the cache will be marked as full. This a cache "miss":
327 a relatively expensive operation and care must be taken so it doesn't
328 happen very often.
329
330 If the cache is full (not flushed) this call will return all references
331 stored into the cache (might be zero too).
332 This is a very fast operation since the cache is single-thread-use and thus
333 no synchronization will be done (except for checking the channel's state
334 of course). This is a cache "hit" and should be the normal operation of the
335 cache.
336
337 @param cache the cache to use
338 @param service_index to get references to. Must be one of the services the
339 channel serves
340 @param[out] an array of my_h_service terminated with an empty service (0).
341 @retval true failure
342 @retval false success
343*/
344DECLARE_BOOL_METHOD(get, (reference_caching_cache cache, unsigned service_index,
345 const my_h_service **refs));
346/**
347 Flush a reference cache
348
349 This causes the reference cache supplied to be flushed. When in this state
350 the next call to get() will be a guaranteed cache miss and will fill in the
351 cache.
352
353 @param cache the cache to flush
354 @retval true failure
355 @retval false success
356*/
359
360#endif /* REFERENCE_CACHING_H */
struct my_h_service_imp * my_h_service
A handle type for acquired Service.
Definition: registry.h:33
static int destroy(mysql_cond_t *that, const char *, unsigned int)
Definition: mysql_cond_v1_native.cc:54
void get(PSI_field *, PSI_longlong *) noexcept
Definition: pfs_plugin_column_bigint_v1_all_empty.cc:32
static mysql_service_status_t flush(reference_caching_cache cache) noexcept
Definition: component.cc:122
static mysql_service_status_t remove(reference_caching_channel channel, const char *implementation_name) noexcept
Definition: component.cc:145
static mysql_service_status_t clear(reference_caching_channel channel) noexcept
Definition: component.cc:154
static mysql_service_status_t add(reference_caching_channel channel, const char *implementation_name) noexcept
Definition: component.cc:135
static mysql_service_status_t invalidate(reference_caching_channel channel) noexcept
Definition: component.cc:58
static mysql_service_status_t create(const char *service_names[], reference_caching_channel *out_channel) noexcept
Definition: component.cc:36
static mysql_service_status_t validate(reference_caching_channel channel) noexcept
Definition: component.cc:67
static mysql_service_status_t fetch(const char *service_name, reference_caching_channel *out_channel) noexcept
Definition: component.cc:77
struct reference_caching_cache_imp * reference_caching_cache
A handle of a "reference cache".
Definition: reference_caching.h:47
struct reference_caching_channel_imp * reference_caching_channel
Handle for "producer channels".
Definition: reference_caching.h:39
#define SERVICE_TYPE(name)
Generates the standard Service type name.
Definition: service.h:76
#define END_SERVICE_DEFINITION(name)
A macro to end the last Service definition started with the BEGIN_SERVICE_DEFINITION macro.
Definition: service.h:91
#define BEGIN_SERVICE_DEFINITION(name)
Declares a new Service.
Definition: service.h:86
#define DEFINE_SERVICE_HANDLE(name)
Defines an object type that is meant for carrying handles to the implementation-specific objects used...
Definition: service.h:129
#define DECLARE_BOOL_METHOD(name, args)
Declares a method that returns bool as a part of the Service definition.
Definition: service.h:112
Definition: task.h:427