File: gcs_xcom_xcom_cache-t.cc

package info (click to toggle)
mysql-8.0 8.0.43-3
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 1,273,924 kB
  • sloc: cpp: 4,684,605; ansic: 412,450; pascal: 108,398; java: 83,641; perl: 30,221; cs: 27,067; sql: 26,594; sh: 24,181; python: 21,816; yacc: 17,169; php: 11,522; xml: 7,388; javascript: 7,076; makefile: 2,194; lex: 1,075; awk: 670; asm: 520; objc: 183; ruby: 97; lisp: 86
file content (403 lines) | stat: -rw-r--r-- 12,881 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
/* Copyright (c) 2016, 2025, Oracle and/or its affiliates.

   This program is free software; you can redistribute it and/or modify
   it under the terms of the GNU General Public License, version 2.0,
   as published by the Free Software Foundation.

   This program is designed to work with certain software (including
   but not limited to OpenSSL) that is licensed under separate terms,
   as designated in a particular file or component or in included license
   documentation.  The authors of MySQL hereby grant you an additional
   permission to link the program and your derivative works with the
   separately licensed software that they have either included with
   the program or referenced in the documentation.

   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License, version 2.0, for more details.

   You should have received a copy of the GNU General Public License
   along with this program; if not, write to the Free Software
   Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301  USA */

#include <string>
#include <vector>

#include "gcs_base_test.h"

#include "app_data.h"
#include "synode_no.h"
#include "xcom_base.h"
#include "xcom_cache.h"
#include "xcom_cfg.h"

namespace gcs_xcom_xcom_unittest {

void setup_cache(uint64_t increment) {
  set_length_increment(increment);
  set_size_decrement(increment);
  init_cache();
}

void cleanup_cache() { deinit_cache(); }

/**
 * Mocks the XCom maintenance task.
 */
void *cache_task(void *ptr) {
  bool *run = (bool *)ptr;
  while (*run) {
    do_cache_maintenance();
    std::this_thread::sleep_for(std::chrono::milliseconds(100));
  }
  return nullptr;
}

class GcsXComXComCache : public GcsBaseTest {
 public:
  const std::string *m_addr;
  char m_payload[512];
  u_int m_payload_size;
  node_address *m_na;
  site_def *m_sd;
  app_data m_a;
  synode_no m_synode;
  My_xp_thread_impl *m_thread;
  bool m_run;

 protected:
  GcsXComXComCache()
      : m_addr(nullptr),
        m_payload_size(0),
        m_na(nullptr),
        m_sd(nullptr),
        m_thread(nullptr),
        m_run(false) {}
  ~GcsXComXComCache() override = default;

  void SetUp() override {
    m_synode = {1, 1, 0};
    m_addr = new std::string("127.0.0.1:12345");
    char const *names[]{m_addr->c_str()};
    m_na = new_node_address(1, names);
    m_sd = new_site_def();
    init_site_def(1, m_na, m_sd);
    push_site_def(m_sd);
    m_payload_size = 512 - sizeof(pax_msg) - sizeof(app_data);
    init_app_msg(&m_a, m_payload, m_payload_size);
    init_cfg_app_xcom();
  }

  void TearDown() override {
    m_run = false;
    if (m_thread) {
      m_thread->join(nullptr);
      delete m_thread;
    }
    push_site_def(nullptr);
    free_site_defs();
    delete_node_address(1, m_na);
    cleanup_cache();
    deinit_cfg_app_xcom();
    delete m_addr;
  }

  virtual void cache_msg(synode_no synode) {
    pax_machine *pm = nullptr;
    pm = get_cache(synode);
    ASSERT_TRUE(pm != nullptr);
    ASSERT_TRUE(synode_eq(pm->synode, synode));
    unchecked_replace_pax_msg(&pm->proposer.msg, pax_msg_new(synode, m_sd));
    pm->proposer.msg->a = clone_app_data(&m_a);
    add_cache_size(pm);
  }

  virtual void cache_bulk(size_t num_msg) {
    while (m_synode.msgno <= num_msg) {
      cache_msg(m_synode);
      m_synode.msgno++;
    }
  }

  virtual size_t msg_size() {
    return sizeof(pax_msg) + sizeof(app_data) + m_payload_size;
  }

  virtual void basic_test_generic(size_t increment, size_t target_occupation) {
    setup_cache(increment);
    ASSERT_EQ(get_xcom_cache_length(), increment);
    ASSERT_EQ(get_xcom_cache_occupation(), 0);
    ASSERT_EQ(get_xcom_cache_size(), 0);

    cache_bulk(target_occupation);

    ASSERT_EQ(get_xcom_cache_occupation(), target_occupation);
    ASSERT_EQ(get_xcom_cache_size(), target_occupation * (msg_size()));
    size_t expected_length =
        (floor((double)target_occupation / (double)increment) + 1) * increment;
    ASSERT_EQ(get_xcom_cache_length(), expected_length);
  }
};

/**
 * Basic test to verify that cache grows as needed.
 */
TEST_F(GcsXComXComCache, XComCacheTestDefaults) {
  basic_test_generic(50000, 500000);
  ASSERT_EQ(get_xcom_cache_length(), 550000);
}

/**
 * Checks the boundaries of increment: cache only grows in 50k increments and
 * the increment is triggered when the last empty slot is occupied. In the
 * previous test the cache reached the limit when it hit the 500k slots and was
 * incremented by 50k. In this test the cache will not be incremented because
 * it still has one slots left.
 */
TEST_F(GcsXComXComCache, XComCacheTestIncrementBelow) {
  basic_test_generic(50000, 499999);
  ASSERT_EQ(get_xcom_cache_length(), 500000);
  deinit_cache();
  ASSERT_EQ(get_xcom_cache_length(), 0);
  ASSERT_EQ(get_xcom_cache_occupation(), 0);
  ASSERT_EQ(get_xcom_cache_size(), 0);
}

TEST_F(GcsXComXComCache, XComCacheTestIncrementAbove) {
  basic_test_generic(50000, 500001);
  ASSERT_EQ(get_xcom_cache_length(), 550000);
  deinit_cache();
  ASSERT_EQ(get_xcom_cache_length(), 0);
  ASSERT_EQ(get_xcom_cache_occupation(), 0);
  ASSERT_EQ(get_xcom_cache_size(), 0);
}

/**
 * Stress test with a large cache.
 */
TEST_F(GcsXComXComCache, XComCacheTestDefaultsLargeCache) {
  basic_test_generic(50000, 3000000);
  deinit_cache();
  ASSERT_EQ(get_xcom_cache_length(), 0);
  ASSERT_EQ(get_xcom_cache_occupation(), 0);
  ASSERT_EQ(get_xcom_cache_size(), 0);
}

/**
 * Test to exert some extra stress on the system by having a large number of
 * small hash tables.
 */
TEST_F(GcsXComXComCache, XComCacheTestLargeCacheSmallConfig) {
  basic_test_generic(100, 3000000);
  deinit_cache();
  ASSERT_EQ(get_xcom_cache_length(), 0);
  ASSERT_EQ(get_xcom_cache_occupation(), 0);
  ASSERT_EQ(get_xcom_cache_size(), 0);
}

/**
 * Same as above, but with even smaller hash tables.
 */
TEST_F(GcsXComXComCache, XComCacheTestLargeCacheSmallConfig2) {
  basic_test_generic(10, 3000000);
  deinit_cache();
  ASSERT_EQ(get_xcom_cache_length(), 0);
  ASSERT_EQ(get_xcom_cache_occupation(), 0);
  ASSERT_EQ(get_xcom_cache_size(), 0);
}

/**
 * Iterates the cache starting with the oldest message. Simulates the recovery
 * of an unreachable node that just got back into the group.
 */
TEST_F(GcsXComXComCache, XComCacheTestIterateForward) {
  basic_test_generic(50000, 3000000);
  synode_no synode = {1, 1, 0};
  while (synode.msgno < m_synode.msgno) {
    pax_machine *pm = nullptr;
    pm = get_cache(synode);
    ASSERT_TRUE(pm != nullptr);
    ASSERT_TRUE(synode_eq(pm->synode, synode));
    synode.msgno++;
  }
  deinit_cache();
  ASSERT_EQ(get_xcom_cache_length(), 0);
  ASSERT_EQ(get_xcom_cache_occupation(), 0);
  ASSERT_EQ(get_xcom_cache_size(), 0);
}

/**
 * For performance comparison with the test above, just does 5M accesses to
 * the most recent message.
 */
TEST_F(GcsXComXComCache, XComCacheTestAccessRecent) {
  basic_test_generic(50000, 3000000);
  u_int iterations = 3000000;
  while (iterations > 0) {
    pax_machine *pm = nullptr;
    pm = get_cache(m_synode);
    ASSERT_TRUE(pm != nullptr);
    ASSERT_TRUE(synode_eq(pm->synode, m_synode));
    iterations--;
  }
  deinit_cache();
  ASSERT_EQ(get_xcom_cache_length(), 0);
  ASSERT_EQ(get_xcom_cache_occupation(), 0);
  ASSERT_EQ(get_xcom_cache_size(), 0);
}

/**
 * Tests the length decrease process by gradually changing configuration
 * parameters. The management itself is made manually in the test by explicitly
 * calling the management functions.
 */
TEST_F(GcsXComXComCache, XComCacheTestLengthDecrease) {
  size_t target_occupation = 450000;  // 450K
  basic_test_generic(50000, target_occupation);
  ASSERT_EQ(get_xcom_cache_occupation(), 450000);
  ASSERT_EQ(get_xcom_cache_length(), 500000);
  /*
    Cache refuses to decrease before it reaches 500K slots
  */
  ASSERT_EQ(check_decrease(), CACHE_TOO_SMALL);

  target_occupation = 3000000;  // 3M
  cache_bulk(target_occupation);
  ASSERT_EQ(get_xcom_cache_occupation(), 3000000);
  ASSERT_EQ(get_xcom_cache_length(), 3050000);
  /*
    Fails because there are no empty hash tables in the stack
  */
  ASSERT_EQ(check_decrease(), CACHE_HASH_NOTEMPTY);

  size_t count = 0;
  while (above_cache_limit()) {
    count += shrink_cache();
  }
  /**
    The size of the cache decreasead from 1536000000 to below 1000000000.
    With a message size of 512, shrink_cache() had to remove 3046875 messages:
      -> 1536000000 - 1000000000 = 536000000
      -> 536000000 / 512 = 1046875
   */
  ASSERT_EQ(count, 1046875);
  ASSERT_EQ(get_xcom_cache_occupation(), 1953125);  // init occupation - count
  ASSERT_EQ(get_xcom_cache_length(), 3050000);      // Length did not change
  ASSERT_EQ(check_decrease(), CACHE_SHRINK_OK);
  ASSERT_EQ(get_xcom_cache_length(), 3000000);
  while (!check_decrease()) {
  }
  /*
    Fails because occupation >= cache_length * min_target_occupation
    (1953125 / 0.7) == 2790178. Since the cache decreases in 50k slots,
    decrease will stop once cache_length hits the lowest 50k decrement
    that is lower than 3892564, which is 2750000
  */
  ASSERT_EQ(check_decrease(), CACHE_HIGH_OCCUPATION);
  ASSERT_EQ(get_xcom_cache_occupation(), 1953125);
  ASSERT_EQ(get_xcom_cache_length(), 2750000);

  set_min_target_occupation(1.0);  // Force previous test to pass
  while (!check_decrease()) {
  }
  /*
   * Now it fails because
   * (cache_length - BUCKETS) * min_occupation <= occupation):
   * 1953125 / 0.9 = 2170139 ; 2170139 + 50000 = 2220139
   *
   */
  ASSERT_EQ(check_decrease(), CACHE_RESULT_LOW);
  ASSERT_EQ(get_xcom_cache_length(), 2200000);
  set_min_length_threshold(1);  // Force previous test to pass
  ASSERT_EQ(check_decrease(), CACHE_SHRINK_OK);
  set_max_cache_size(2000000000);
  /*
    Verify that decrease fails because cache_size is still far from the limit.
  */
  ASSERT_EQ(check_decrease(), CACHE_INCREASING);
  // Reset vars
  set_min_target_occupation(MIN_TARGET_OCCUPATION);
  set_min_length_threshold(MIN_LENGTH_THRESHOLD);
  deinit_cache();
  ASSERT_EQ(get_xcom_cache_length(), 0);
  ASSERT_EQ(get_xcom_cache_occupation(), 0);
  ASSERT_EQ(get_xcom_cache_size(), 0);
}

/**
 * Similar to the previous test, but instead of calling the management functions
 * directly, sets up a background task mocking the management task of the XCom
 * cache.
 */
TEST_F(GcsXComXComCache, XComCacheTestLengthDecreaseWithTask) {
  size_t target_occupation = 3000000;
  basic_test_generic(50000, target_occupation);
  ASSERT_EQ(get_xcom_cache_size(), target_occupation * (msg_size()));
  ASSERT_EQ(get_xcom_cache_occupation(), 3000000);
  ASSERT_EQ(get_xcom_cache_length(), 3050000);
  m_thread = new My_xp_thread_impl();
  m_run = true;
  m_thread->create(PSI_NOT_INSTRUMENTED, nullptr, cache_task, (void *)&m_run);
  /*
    Wait for the task to decrease the size
  */
  while (above_cache_limit()) {
    sleep(1);
  }

  ASSERT_EQ(get_xcom_cache_occupation(), 1953125);
  /*
    After XCom decrease the size, it should start decreasing the length.
    Decrease stops when (occupation >= cache_length * min_target_occupation)
    (1953125 / 0.7) == 2790178. Since the cache decreases in 50k slots,
    decrease will stop once cache_length hits the lowest 50k decrement
    that is lower than 3892564, which is 2750000
  */
  while (get_xcom_cache_length() != 2750000) {
    sleep(1);
  }

  ASSERT_EQ(get_xcom_cache_occupation(), 1953125);
  ASSERT_EQ(get_xcom_cache_length(), 2750000);  // Redundant, but let's do it!
  ASSERT_EQ(check_decrease(), CACHE_HIGH_OCCUPATION);

  set_min_target_occupation(1.0);  // Force previous test to pass
  /*
    Now it fails because
    (cache_length - BUCKETS) * min_occupation <= occupation):
    1953125 / 0.9 = 2170139 ; 2170139 + 50000 = 2220139
  */
  while (get_xcom_cache_length() != 2200000) {
    sleep(1);
  }

  ASSERT_EQ(get_xcom_cache_length(), 2200000);
  ASSERT_EQ(check_decrease(), CACHE_RESULT_LOW);

  /*
   * Increase the max cache size before "removing" the min length threshold
   */
  set_max_cache_size(2000000000);

  set_min_length_threshold(1);  // Force previous test to pass
  // Check that decrease still has no effect, but for a different reason
  ASSERT_EQ(check_decrease(), CACHE_INCREASING);
  // Restore max cache size
  set_max_cache_size(1000000000);

  while (get_xcom_cache_length() != 2050000) {
    sleep(1);
  }

  // The decrease stops once the length gets to 2050000...
  ASSERT_EQ(get_xcom_cache_length(), 2050000);
  // ...because we have no more empty hash tables at the end of the stack
  ASSERT_EQ(check_decrease(), CACHE_HASH_NOTEMPTY);
  deinit_cache();
  ASSERT_EQ(get_xcom_cache_length(), 0);
  ASSERT_EQ(get_xcom_cache_occupation(), 0);
  ASSERT_EQ(get_xcom_cache_size(), 0);
}

}  // namespace gcs_xcom_xcom_unittest