| 1 | /* Copyright (c) 2016, 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 <string> |
| 25 | #include <vector> |
| 26 | |
| 27 | #include "gcs_base_test.h" |
| 28 | |
| 29 | #include "app_data.h" |
| 30 | #include "synode_no.h" |
| 31 | #include "xcom_base.h" |
| 32 | #include "xcom_cache.h" |
| 33 | #include "xcom_cfg.h" |
| 34 | |
| 35 | namespace gcs_xcom_xcom_unittest { |
| 36 | |
| 37 | void setup_cache(uint64_t increment) { |
| 38 | set_length_increment(increment); |
| 39 | set_size_decrement(increment); |
| 40 | init_cache(); |
| 41 | } |
| 42 | |
| 43 | void cleanup_cache() { deinit_cache(); } |
| 44 | |
| 45 | /** |
| 46 | * Mocks the XCom maintenance task. |
| 47 | */ |
| 48 | void *cache_task(void *ptr) { |
| 49 | bool *run = (bool *)ptr; |
| 50 | while (*run) { |
| 51 | do_cache_maintenance(); |
| 52 | std::this_thread::sleep_for(std::chrono::milliseconds(100)); |
| 53 | } |
| 54 | return nullptr; |
| 55 | } |
| 56 | |
| 57 | class GcsXComXComCache : public GcsBaseTest { |
| 58 | public: |
| 59 | const std::string *m_addr; |
| 60 | char m_payload[512]; |
| 61 | u_int m_payload_size; |
| 62 | node_address *m_na; |
| 63 | site_def *m_sd; |
| 64 | app_data m_a; |
| 65 | synode_no m_synode; |
| 66 | My_xp_thread_impl *m_thread; |
| 67 | bool m_run; |
| 68 | |
| 69 | protected: |
| 70 | GcsXComXComCache() |
| 71 | : m_addr(nullptr), |
| 72 | m_payload_size(0), |
| 73 | m_na(nullptr), |
| 74 | m_sd(nullptr), |
| 75 | m_thread(nullptr), |
| 76 | m_run(false) {} |
| 77 | ~GcsXComXComCache() override = default; |
| 78 | |
| 79 | void SetUp() override { |
| 80 | m_synode = {1, 1, 0}; |
| 81 | m_addr = new std::string("127.0.0.1:12345"); |
| 82 | char const *names[]{m_addr->c_str()}; |
| 83 | m_na = new_node_address(1, names); |
| 84 | m_sd = new_site_def(); |
| 85 | init_site_def(1, m_na, m_sd); |
| 86 | push_site_def(m_sd); |
| 87 | m_payload_size = 512 - sizeof(pax_msg) - sizeof(app_data); |
| 88 | init_app_msg(&m_a, m_payload, m_payload_size); |
| 89 | init_cfg_app_xcom(); |
| 90 | } |
| 91 | |
| 92 | void TearDown() override { |
| 93 | m_run = false; |
| 94 | if (m_thread) { |
| 95 | m_thread->join(nullptr); |
| 96 | delete m_thread; |
| 97 | } |
| 98 | push_site_def(nullptr); |
| 99 | free_site_defs(); |
| 100 | delete_node_address(1, m_na); |
| 101 | cleanup_cache(); |
| 102 | deinit_cfg_app_xcom(); |
| 103 | delete m_addr; |
| 104 | } |
| 105 | |
| 106 | virtual void cache_msg(synode_no synode) { |
| 107 | pax_machine *pm = nullptr; |
| 108 | pm = get_cache(synode); |
| 109 | ASSERT_TRUE(pm != nullptr); |
| 110 | ASSERT_TRUE(synode_eq(pm->synode, synode)); |
| 111 | unchecked_replace_pax_msg(&pm->proposer.msg, pax_msg_new(synode, m_sd)); |
| 112 | pm->proposer.msg->a = clone_app_data(&m_a); |
| 113 | add_cache_size(pm); |
| 114 | } |
| 115 | |
| 116 | virtual void cache_bulk(size_t num_msg) { |
| 117 | while (m_synode.msgno <= num_msg) { |
| 118 | cache_msg(m_synode); |
| 119 | m_synode.msgno++; |
| 120 | } |
| 121 | } |
| 122 | |
| 123 | virtual size_t msg_size() { |
| 124 | return sizeof(pax_msg) + sizeof(app_data) + m_payload_size; |
| 125 | } |
| 126 | |
| 127 | virtual void basic_test_generic(size_t increment, size_t target_occupation) { |
| 128 | setup_cache(increment); |
| 129 | ASSERT_EQ(get_xcom_cache_length(), increment); |
| 130 | ASSERT_EQ(get_xcom_cache_occupation(), 0); |
| 131 | ASSERT_EQ(get_xcom_cache_size(), 0); |
| 132 | |
| 133 | cache_bulk(target_occupation); |
| 134 | |
| 135 | ASSERT_EQ(get_xcom_cache_occupation(), target_occupation); |
| 136 | ASSERT_EQ(get_xcom_cache_size(), target_occupation * (msg_size())); |
| 137 | size_t expected_length = |
| 138 | (floor((double)target_occupation / (double)increment) + 1) * increment; |
| 139 | ASSERT_EQ(get_xcom_cache_length(), expected_length); |
| 140 | } |
| 141 | }; |
| 142 | |
| 143 | /** |
| 144 | * Basic test to verify that cache grows as needed. |
| 145 | */ |
| 146 | TEST_F(GcsXComXComCache, XComCacheTestDefaults) { |
| 147 | basic_test_generic(50000, 500000); |
| 148 | ASSERT_EQ(get_xcom_cache_length(), 550000); |
| 149 | } |
| 150 | |
| 151 | /** |
| 152 | * Checks the boundaries of increment: cache only grows in 50k increments and |
| 153 | * the increment is triggered when the last empty slot is occupied. In the |
| 154 | * previous test the cache reached the limit when it hit the 500k slots and was |
| 155 | * incremented by 50k. In this test the cache will not be incremented because |
| 156 | * it still has one slots left. |
| 157 | */ |
| 158 | TEST_F(GcsXComXComCache, XComCacheTestIncrementBelow) { |
| 159 | basic_test_generic(50000, 499999); |
| 160 | ASSERT_EQ(get_xcom_cache_length(), 500000); |
| 161 | deinit_cache(); |
| 162 | ASSERT_EQ(get_xcom_cache_length(), 0); |
| 163 | ASSERT_EQ(get_xcom_cache_occupation(), 0); |
| 164 | ASSERT_EQ(get_xcom_cache_size(), 0); |
| 165 | } |
| 166 | |
| 167 | TEST_F(GcsXComXComCache, XComCacheTestIncrementAbove) { |
| 168 | basic_test_generic(50000, 500001); |
| 169 | ASSERT_EQ(get_xcom_cache_length(), 550000); |
| 170 | deinit_cache(); |
| 171 | ASSERT_EQ(get_xcom_cache_length(), 0); |
| 172 | ASSERT_EQ(get_xcom_cache_occupation(), 0); |
| 173 | ASSERT_EQ(get_xcom_cache_size(), 0); |
| 174 | } |
| 175 | |
| 176 | /** |
| 177 | * Stress test with a large cache. |
| 178 | */ |
| 179 | TEST_F(GcsXComXComCache, XComCacheTestDefaultsLargeCache) { |
| 180 | basic_test_generic(50000, 3000000); |
| 181 | deinit_cache(); |
| 182 | ASSERT_EQ(get_xcom_cache_length(), 0); |
| 183 | ASSERT_EQ(get_xcom_cache_occupation(), 0); |
| 184 | ASSERT_EQ(get_xcom_cache_size(), 0); |
| 185 | } |
| 186 | |
| 187 | /** |
| 188 | * Test to exert some extra stress on the system by having a large number of |
| 189 | * small hash tables. |
| 190 | */ |
| 191 | TEST_F(GcsXComXComCache, XComCacheTestLargeCacheSmallConfig) { |
| 192 | basic_test_generic(100, 3000000); |
| 193 | deinit_cache(); |
| 194 | ASSERT_EQ(get_xcom_cache_length(), 0); |
| 195 | ASSERT_EQ(get_xcom_cache_occupation(), 0); |
| 196 | ASSERT_EQ(get_xcom_cache_size(), 0); |
| 197 | } |
| 198 | |
| 199 | /** |
| 200 | * Same as above, but with even smaller hash tables. |
| 201 | */ |
| 202 | TEST_F(GcsXComXComCache, XComCacheTestLargeCacheSmallConfig2) { |
| 203 | basic_test_generic(10, 3000000); |
| 204 | deinit_cache(); |
| 205 | ASSERT_EQ(get_xcom_cache_length(), 0); |
| 206 | ASSERT_EQ(get_xcom_cache_occupation(), 0); |
| 207 | ASSERT_EQ(get_xcom_cache_size(), 0); |
| 208 | } |
| 209 | |
| 210 | /** |
| 211 | * Iterates the cache starting with the oldest message. Simulates the recovery |
| 212 | * of an unreachable node that just got back into the group. |
| 213 | */ |
| 214 | TEST_F(GcsXComXComCache, XComCacheTestIterateForward) { |
| 215 | basic_test_generic(50000, 3000000); |
| 216 | synode_no synode = {1, 1, 0}; |
| 217 | while (synode.msgno < m_synode.msgno) { |
| 218 | pax_machine *pm = nullptr; |
| 219 | pm = get_cache(synode); |
| 220 | ASSERT_TRUE(pm != nullptr); |
| 221 | ASSERT_TRUE(synode_eq(pm->synode, synode)); |
| 222 | synode.msgno++; |
| 223 | } |
| 224 | deinit_cache(); |
| 225 | ASSERT_EQ(get_xcom_cache_length(), 0); |
| 226 | ASSERT_EQ(get_xcom_cache_occupation(), 0); |
| 227 | ASSERT_EQ(get_xcom_cache_size(), 0); |
| 228 | } |
| 229 | |
| 230 | /** |
| 231 | * For performance comparison with the test above, just does 5M accesses to |
| 232 | * the most recent message. |
| 233 | */ |
| 234 | TEST_F(GcsXComXComCache, XComCacheTestAccessRecent) { |
| 235 | basic_test_generic(50000, 3000000); |
| 236 | u_int iterations = 3000000; |
| 237 | while (iterations > 0) { |
| 238 | pax_machine *pm = nullptr; |
| 239 | pm = get_cache(m_synode); |
| 240 | ASSERT_TRUE(pm != nullptr); |
| 241 | ASSERT_TRUE(synode_eq(pm->synode, m_synode)); |
| 242 | iterations--; |
| 243 | } |
| 244 | deinit_cache(); |
| 245 | ASSERT_EQ(get_xcom_cache_length(), 0); |
| 246 | ASSERT_EQ(get_xcom_cache_occupation(), 0); |
| 247 | ASSERT_EQ(get_xcom_cache_size(), 0); |
| 248 | } |
| 249 | |
| 250 | /** |
| 251 | * Tests the length decrease process by gradually changing configuration |
| 252 | * parameters. The management itself is made manually in the test by explicitly |
| 253 | * calling the management functions. |
| 254 | */ |
| 255 | TEST_F(GcsXComXComCache, XComCacheTestLengthDecrease) { |
| 256 | size_t target_occupation = 450000; // 450K |
| 257 | basic_test_generic(50000, target_occupation); |
| 258 | ASSERT_EQ(get_xcom_cache_occupation(), 450000); |
| 259 | ASSERT_EQ(get_xcom_cache_length(), 500000); |
| 260 | /* |
| 261 | Cache refuses to decrease before it reaches 500K slots |
| 262 | */ |
| 263 | ASSERT_EQ(check_decrease(), CACHE_TOO_SMALL); |
| 264 | |
| 265 | target_occupation = 3000000; // 3M |
| 266 | cache_bulk(target_occupation); |
| 267 | ASSERT_EQ(get_xcom_cache_occupation(), 3000000); |
| 268 | ASSERT_EQ(get_xcom_cache_length(), 3050000); |
| 269 | /* |
| 270 | Fails because there are no empty hash tables in the stack |
| 271 | */ |
| 272 | ASSERT_EQ(check_decrease(), CACHE_HASH_NOTEMPTY); |
| 273 | |
| 274 | size_t count = 0; |
| 275 | while (above_cache_limit()) { |
| 276 | count += shrink_cache(); |
| 277 | } |
| 278 | /** |
| 279 | The size of the cache decreasead from 1536000000 to below 1000000000. |
| 280 | With a message size of 512, shrink_cache() had to remove 3046875 messages: |
| 281 | -> 1536000000 - 1000000000 = 536000000 |
| 282 | -> 536000000 / 512 = 1046875 |
| 283 | */ |
| 284 | ASSERT_EQ(count, 1046875); |
| 285 | ASSERT_EQ(get_xcom_cache_occupation(), 1953125); // init occupation - count |
| 286 | ASSERT_EQ(get_xcom_cache_length(), 3050000); // Length did not change |
| 287 | ASSERT_EQ(check_decrease(), CACHE_SHRINK_OK); |
| 288 | ASSERT_EQ(get_xcom_cache_length(), 3000000); |
| 289 | while (!check_decrease()) { |
| 290 | } |
| 291 | /* |
| 292 | Fails because occupation >= cache_length * min_target_occupation |
| 293 | (1953125 / 0.7) == 2790178. Since the cache decreases in 50k slots, |
| 294 | decrease will stop once cache_length hits the lowest 50k decrement |
| 295 | that is lower than 3892564, which is 2750000 |
| 296 | */ |
| 297 | ASSERT_EQ(check_decrease(), CACHE_HIGH_OCCUPATION); |
| 298 | ASSERT_EQ(get_xcom_cache_occupation(), 1953125); |
| 299 | ASSERT_EQ(get_xcom_cache_length(), 2750000); |
| 300 | |
| 301 | set_min_target_occupation(1.0); // Force previous test to pass |
| 302 | while (!check_decrease()) { |
| 303 | } |
| 304 | /* |
| 305 | * Now it fails because |
| 306 | * (cache_length - BUCKETS) * min_occupation <= occupation): |
| 307 | * 1953125 / 0.9 = 2170139 ; 2170139 + 50000 = 2220139 |
| 308 | * |
| 309 | */ |
| 310 | ASSERT_EQ(check_decrease(), CACHE_RESULT_LOW); |
| 311 | ASSERT_EQ(get_xcom_cache_length(), 2200000); |
| 312 | set_min_length_threshold(1); // Force previous test to pass |
| 313 | ASSERT_EQ(check_decrease(), CACHE_SHRINK_OK); |
| 314 | set_max_cache_size(2000000000); |
| 315 | /* |
| 316 | Verify that decrease fails because cache_size is still far from the limit. |
| 317 | */ |
| 318 | ASSERT_EQ(check_decrease(), CACHE_INCREASING); |
| 319 | // Reset vars |
| 320 | set_min_target_occupation(MIN_TARGET_OCCUPATION); |
| 321 | set_min_length_threshold(MIN_LENGTH_THRESHOLD); |
| 322 | deinit_cache(); |
| 323 | ASSERT_EQ(get_xcom_cache_length(), 0); |
| 324 | ASSERT_EQ(get_xcom_cache_occupation(), 0); |
| 325 | ASSERT_EQ(get_xcom_cache_size(), 0); |
| 326 | } |
| 327 | |
| 328 | /** |
| 329 | * Similar to the previous test, but instead of calling the management functions |
| 330 | * directly, sets up a background task mocking the management task of the XCom |
| 331 | * cache. |
| 332 | */ |
| 333 | TEST_F(GcsXComXComCache, XComCacheTestLengthDecreaseWithTask) { |
| 334 | size_t target_occupation = 3000000; |
| 335 | basic_test_generic(50000, target_occupation); |
| 336 | ASSERT_EQ(get_xcom_cache_size(), target_occupation * (msg_size())); |
| 337 | ASSERT_EQ(get_xcom_cache_occupation(), 3000000); |
| 338 | ASSERT_EQ(get_xcom_cache_length(), 3050000); |
| 339 | m_thread = new My_xp_thread_impl(); |
| 340 | m_run = true; |
| 341 | m_thread->create(PSI_NOT_INSTRUMENTED, nullptr, cache_task, (void *)&m_run); |
| 342 | /* |
| 343 | Wait for the task to decrease the size |
| 344 | */ |
| 345 | while (above_cache_limit()) { |
| 346 | sleep(1); |
| 347 | } |
| 348 | |
| 349 | ASSERT_EQ(get_xcom_cache_occupation(), 1953125); |
| 350 | /* |
| 351 | After XCom decrease the size, it should start decreasing the length. |
| 352 | Decrease stops when (occupation >= cache_length * min_target_occupation) |
| 353 | (1953125 / 0.7) == 2790178. Since the cache decreases in 50k slots, |
| 354 | decrease will stop once cache_length hits the lowest 50k decrement |
| 355 | that is lower than 3892564, which is 2750000 |
| 356 | */ |
| 357 | while (get_xcom_cache_length() != 2750000) { |
| 358 | sleep(1); |
| 359 | } |
| 360 | |
| 361 | ASSERT_EQ(get_xcom_cache_occupation(), 1953125); |
| 362 | ASSERT_EQ(get_xcom_cache_length(), 2750000); // Redundant, but let's do it! |
| 363 | ASSERT_EQ(check_decrease(), CACHE_HIGH_OCCUPATION); |
| 364 | |
| 365 | set_min_target_occupation(1.0); // Force previous test to pass |
| 366 | /* |
| 367 | Now it fails because |
| 368 | (cache_length - BUCKETS) * min_occupation <= occupation): |
| 369 | 1953125 / 0.9 = 2170139 ; 2170139 + 50000 = 2220139 |
| 370 | */ |
| 371 | while (get_xcom_cache_length() != 2200000) { |
| 372 | sleep(1); |
| 373 | } |
| 374 | |
| 375 | ASSERT_EQ(get_xcom_cache_length(), 2200000); |
| 376 | ASSERT_EQ(check_decrease(), CACHE_RESULT_LOW); |
| 377 | |
| 378 | /* |
| 379 | * Increase the max cache size before "removing" the min length threshold |
| 380 | */ |
| 381 | set_max_cache_size(2000000000); |
| 382 | |
| 383 | set_min_length_threshold(1); // Force previous test to pass |
| 384 | // Check that decrease still has no effect, but for a different reason |
| 385 | ASSERT_EQ(check_decrease(), CACHE_INCREASING); |
| 386 | // Restore max cache size |
| 387 | set_max_cache_size(1000000000); |
| 388 | |
| 389 | while (get_xcom_cache_length() != 2050000) { |
| 390 | sleep(1); |
| 391 | } |
| 392 | |
| 393 | // The decrease stops once the length gets to 2050000... |
| 394 | ASSERT_EQ(get_xcom_cache_length(), 2050000); |
| 395 | // ...because we have no more empty hash tables at the end of the stack |
| 396 | ASSERT_EQ(check_decrease(), CACHE_HASH_NOTEMPTY); |
| 397 | deinit_cache(); |
| 398 | ASSERT_EQ(get_xcom_cache_length(), 0); |
| 399 | ASSERT_EQ(get_xcom_cache_occupation(), 0); |
| 400 | ASSERT_EQ(get_xcom_cache_size(), 0); |
| 401 | } |
| 402 | |
| 403 | } // namespace gcs_xcom_xcom_unittest |