Debian Codegraph @

unittest/gunit/components/mysql_server/test_reference_cache.cc

raw symbols SCIP
1 /* Copyright (c) 2020, 2026, Oracle and/or its affiliates.
2
3 This program is free software; you can redistribute it and/or modify
4 it under the terms of the GNU General Public License, version 2.0,
5 as published by the Free Software Foundation.
6
7 This program is designed to work with certain software (including
8 but not limited to OpenSSL) that is licensed under separate terms,
9 as designated in a particular file or component or in included license
10 documentation. The authors of MySQL hereby grant you an additional
11 permission to link the program and your derivative works with the
12 separately licensed software that they have either included with
13 the program or referenced in the documentation.
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 #include "test_reference_cache.h"
25 #include <assert.h>
26 #include <limits.h>
27 #include <mysql/components/component_implementation.h>
28 #include <mysql/components/my_service.h>
29 #include <mysql/components/services/reference_caching.h>
30 #include <stdio.h>
31 #include <algorithm>
32 #include <atomic>
33 #include <chrono>
34 #include <thread>
35 #include <vector>
36
37 REQUIRES_SERVICE_PLACEHOLDER(reference_caching_channel);
38 REQUIRES_SERVICE_PLACEHOLDER(reference_caching_cache);
39 REQUIRES_SERVICE_PLACEHOLDER(reference_caching_channel_ignore_list);
40
41 BEGIN_COMPONENT_REQUIRES(test_reference_cache)
42 REQUIRES_SERVICE(reference_caching_channel)
43 , REQUIRES_SERVICE(reference_caching_cache),
44 REQUIRES_SERVICE(reference_caching_channel_ignore_list),
45 END_COMPONENT_REQUIRES();
46
47 reference_caching_channel channel = nullptr;
48
49 /**
50 A helper class to implement the storage into the call cache into a thread
51 local variable.
52 Make sure it's present for all threads calling the serice via the cache
53
54 foo_cache can be:
55 - nonexistent (the thread local variable is null)
56 - invalid (the thread local variable is set, but the cache creation failed)
57 - valid (the thread local variable is set and the cache creation succeeded)
58
59 */
60 class foo_cache {
61 reference_caching_cache cache_;
62
63 foo_cache() : cache_(nullptr) {
64 assert(channel != nullptr);
65 if (mysql_service_reference_caching_cache->create(
66 channel, mysql_service_registry, &cache_))
67 cache_ = nullptr;
68 }
69
70 /** the thread local variable keeper */
71 static foo_cache *current_cache() {
72 thread_local foo_cache *tl_cache_ptr = nullptr;
73 if (tl_cache_ptr == nullptr) tl_cache_ptr = new foo_cache();
74 return tl_cache_ptr;
75 }
76
77 public:
78 bool is_valid() { return cache_ != nullptr; }
79
80 /**
81 Call the service.
82 Fills in the cache in the process if empty
83 @return: number of consumers called
84 */
85 unsigned call(int arg) {
86 const my_h_service *refs = nullptr;
87 unsigned called = 0;
88 if (is_valid() && !mysql_service_reference_caching_cache->get(
89 cache_, arg /* service_name_index */, &refs)) {
90 for (const my_h_service *svc = refs; *svc; svc++) {
91 SERVICE_TYPE(mysql_test_foo) *f =
92 reinterpret_cast<SERVICE_TYPE(mysql_test_foo) *>(*svc);
93 if (f->emit(arg))
94 break;
95 else
96 called++;
97 }
98 }
99 return called; // the reference wasn't valid or the service call failed
100 }
101
102 /**
103 flush the cache.
104 @retval false success
105 @retval true failure
106 */
107 bool flush() {
108 return (!is_valid() ||
109 mysql_service_reference_caching_cache->flush(cache_));
110 }
111
112 /** helper method to get or create the thread local cache (if absent) */
113 static foo_cache *get_foo_cache() {
114 foo_cache *ptr = current_cache();
115 return ptr;
116 }
117
118 /** helper method to delete the thread local cache if present */
119 static void release_foo_cache() { delete current_cache(); }
120
121 ~foo_cache() {
122 if (is_valid()) mysql_service_reference_caching_cache->destroy(cache_);
123 }
124 };
125
126 static DEFINE_BOOL_METHOD(mysql_test_ref_cache_release_cache, ()) {
127 foo_cache::release_foo_cache();
128 return false;
129 }
130
131 static DEFINE_BOOL_METHOD(mysql_test_ref_cache_produce_event, (int arg)) {
132 int result = 0;
133 foo_cache *c = foo_cache::get_foo_cache();
134 if (c) {
135 return c->call(arg);
136 }
137 return result;
138 }
139
140 static DEFINE_BOOL_METHOD(mysql_test_ref_cache_flush, ()) {
141 int result = 0;
142 foo_cache *c = foo_cache::get_foo_cache();
143 if (c) {
144 if (c->flush()) result = 1;
145 }
146 return result;
147 }
148
149 // the kill switch for the benchamrk UDFs
150 std::atomic<bool> kill_switch;
151
152 /**
153 A benchmark UDF: spawns a number of threads and runs a number a test in them
154 Each benchmark does the following for each of its iterations:
155 1. takes the cache (this is to measure the effect of taking the cache)
156 2. if kill switch is on it exits the loop
157 3. if the cache is a valid reference it calls the listeners
158 4. if the cache is a valid reference flushes it on every 10th repetition
159 5. it sleeps for n_sleep if the cache is not a valid reference
160 */
161 static DEFINE_BOOL_METHOD(mysql_test_ref_cache_benchmark_run,
162 (int threads, int reps, int sleep, int flush)) {
163 long long n_threads = 100;
164 long long n_reps = 100000;
165 long long n_sleep = 500;
166 long long n_flush = 10;
167 kill_switch = false;
168
169 if (threads) n_threads = threads;
170
171 if (reps) n_reps = reps;
172
173 if (sleep) n_sleep = sleep;
174
175 if (flush) n_flush = flush;
176
177 std::vector<std::thread> thds;
178 for (long long i = 0; i < n_threads; i++)
179 thds.push_back(std::thread([n_reps, n_sleep, n_flush]() {
180 foo_cache *c = foo_cache::get_foo_cache();
181 for (long long arg = 0; n_reps == 0 || arg < n_reps; arg++) {
182 /* take again to measure the effect of fetching a populated cache */
183 c = foo_cache::get_foo_cache();
184
185 if (kill_switch.load(std::memory_order_relaxed)) break;
186
187 if (c) {
188 c->call(0 /* service_name_index */);
189 if (n_flush && arg % n_flush == 0) c->flush();
190 } else
191 std::this_thread::sleep_for(std::chrono::milliseconds(n_sleep));
192 }
193
194 foo_cache::release_foo_cache();
195 }));
196
197 std::for_each(thds.begin(), thds.end(), [](std::thread &t) { t.join(); });
198 return 0;
199 }
200
201 static DEFINE_BOOL_METHOD(mysql_test_ref_cache_benchmark_kill, ()) {
202 kill_switch.store(true, std::memory_order_relaxed);
203 return 0;
204 }
205
206 static mysql_service_status_t init() {
207 const char *service_names[] = {"mysql_test_foo", nullptr};
208 if (mysql_service_reference_caching_channel->create(service_names, &channel))
209 channel = nullptr;
210 return 0;
211 }
212
213 static mysql_service_status_t deinit() {
214 if (channel != nullptr) {
215 if (!mysql_service_reference_caching_channel->destroy(channel)) {
216 channel = nullptr;
217 }
218 }
219 return 0;
220 }
221
222 std::atomic<size_t> ctr;
223
224 static DEFINE_BOOL_METHOD(mysql_test_foo_emit, (int /*arg*/)) {
225 ctr++;
226 return false;
227 }
228
229 static DEFINE_BOOL_METHOD(mysql_test_ref_cache_consumer_counter_reset, ()) {
230 ctr = 0;
231 return false;
232 }
233
234 static DEFINE_BOOL_METHOD(mysql_test_ref_cache_consumer_counter_get, ()) {
235 return ctr.load();
236 }
237
238 BEGIN_SERVICE_IMPLEMENTATION(test_reference_cache, mysql_test_foo)
239 mysql_test_foo_emit END_SERVICE_IMPLEMENTATION();
240
241 BEGIN_SERVICE_IMPLEMENTATION(test_reference_cache, test_ref_cache_producer)
242 mysql_test_ref_cache_produce_event, mysql_test_ref_cache_flush,
243 mysql_test_ref_cache_release_cache, mysql_test_ref_cache_benchmark_run,
244 mysql_test_ref_cache_benchmark_kill END_SERVICE_IMPLEMENTATION();
245
246 BEGIN_SERVICE_IMPLEMENTATION(test_reference_cache, test_ref_cache_consumer)
247 mysql_test_ref_cache_consumer_counter_reset,
248 mysql_test_ref_cache_consumer_counter_get END_SERVICE_IMPLEMENTATION();
249
250 BEGIN_COMPONENT_PROVIDES(test_reference_cache)
251 PROVIDES_SERVICE(test_reference_cache, mysql_test_foo)
252 , PROVIDES_SERVICE(test_reference_cache, test_ref_cache_producer),
253 PROVIDES_SERVICE(test_reference_cache, test_ref_cache_consumer),
254 END_COMPONENT_PROVIDES();
255
256 BEGIN_COMPONENT_METADATA(test_reference_cache)
257 METADATA("mysql.author", "Oracle Corporation")
258 , METADATA("mysql.license", "GPL"), METADATA("test_property", "1"),
259 END_COMPONENT_METADATA();
260
261 DECLARE_COMPONENT(test_reference_cache, "mysql:test_reference_cache")
262 init, deinit END_DECLARE_COMPONENT();
263
264 DECLARE_LIBRARY_COMPONENTS &COMPONENT_REF(test_reference_cache)
265 END_DECLARE_LIBRARY_COMPONENTS