File: rpl_commit_order_queue-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 (408 lines) | stat: -rw-r--r-- 13,850 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
404
405
406
407
408
/* Copyright (c) 2020, 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 <array>
#include <chrono>
#include <thread>
#include <vector>

#include "sql/changestreams/apply/commit_order_queue.h"
#include "sql/memory/aligned_atomic.h"

#include <gmock/gmock.h>
#include <gtest/gtest.h>

namespace rpl {
namespace unittests {

class Rpl_commit_order_queue_test : public ::testing::Test {
 protected:
  Rpl_commit_order_queue_test() = default;
  virtual void SetUp() {}
  virtual void TearDown() {}

  std::atomic<bool> m_go{false};
  std::atomic<size_t> m_count{0};
};

TEST_F(Rpl_commit_order_queue_test, Simulate_mts) {
  constexpr cs::apply::Commit_order_queue::value_type total_workers{32};
  constexpr size_t total_transactions{25000};
  cs::apply::Commit_order_queue scheduled{total_workers};
  cs::apply::Commit_order_queue free{total_workers};
  std::array<std::atomic<bool>, total_workers> context;
  std::atomic<cs::apply::Commit_order_queue::value_type> transactions{
      total_transactions};

  for (auto n_wrk = 0; n_wrk != total_workers; ++n_wrk) {
    context[n_wrk].store(true);
    free.push(n_wrk);
  }

  std::vector<std::thread> threads;
  for (auto n_wrk = 0; n_wrk != total_workers; ++n_wrk) {
    threads.emplace_back(
        [&](cs::apply::Commit_order_queue::value_type worker_id) -> void {
          assert(worker_id != cs::apply::Commit_order_queue::NO_WORKER);

          for (; transactions > 0;) {
            if (scheduled[worker_id].m_stage ==
                cs::apply::Commit_order_queue::enum_worker_stage::FINISHED) {
              scheduled[worker_id].m_stage =
                  cs::apply::Commit_order_queue::enum_worker_stage::REGISTERED;

              for (; context[worker_id].exchange(
                       true);) {  // Wait for coordinator to
                                  // schedule the worker in
                std::this_thread::yield();
              }
              if (transactions <= 0) break;
            }

            // Worker would apply the transaction here

            // Worker enters the wait on the commit order
            scheduled[worker_id].m_stage = cs::apply::Commit_order_queue::
                enum_worker_stage::FINISHED_APPLYING;

            if (worker_id == scheduled.front()) {  // Is the head of the queue
              scheduled[worker_id].m_stage =
                  cs::apply::Commit_order_queue::enum_worker_stage::WAITED;

              auto this_worker{cs::apply::Commit_order_queue::NO_WORKER};
              auto next_seq_nr{0};
              std::tie(this_worker, next_seq_nr) =
                  scheduled.pop();  // Pops the head of the
                                    // queue and gets the
                                    // the next sequence
                                    // number
              EXPECT_EQ(worker_id, this_worker);
              EXPECT_NE(next_seq_nr, 0);  // NO_SEQUENCE_NR
              EXPECT_NE(next_seq_nr, 1);  // SEQUENCE_NR_FROZEN

              auto next_worker =
                  scheduled.front();  // Gets the next worker and checks if it
                                      // needs to release it
              if (next_worker != cs::apply::Commit_order_queue::NO_WORKER &&
                  (scheduled[next_worker].m_stage ==
                       cs::apply::Commit_order_queue::enum_worker_stage::
                           FINISHED_APPLYING ||
                   scheduled[next_worker].m_stage ==
                       cs::apply::Commit_order_queue::enum_worker_stage::
                           REQUESTED_GRANT) &&
                  scheduled[next_worker].freeze_commit_sequence_nr(
                      next_seq_nr)) {
                context[next_worker].store(false);  // Releases the next worker
                scheduled[next_worker].unfreeze_commit_sequence_nr(next_seq_nr);
              }

              --transactions;  // One less transaction in the workload

              scheduled[worker_id].m_stage =
                  cs::apply::Commit_order_queue::enum_worker_stage::FINISHED;
              context[worker_id].store(true);
              free.push(worker_id);  // Finishes the work and pushes itself
                                     // into the free worker queue

            } else {
              scheduled[worker_id].m_stage =
                  cs::apply::Commit_order_queue::enum_worker_stage::
                      REQUESTED_GRANT;  // Starts the commit order wait

              for (; context[worker_id].exchange(true);) {  // Wait for previous
                                                            // worker to release
                std::this_thread::yield();
              }
            }
          }
          for (cs::apply::Commit_order_queue::value_type sib = 0;
               sib != total_workers; ++sib) {  // Work is finished
            context[sib].store(false);         // Release all waiting workers
          }
        },
        total_workers - n_wrk - 1);
  }

  std::thread coordinator{[&]() -> void {
    for (size_t n_trx = 0; n_trx != total_transactions; ++n_trx) {
      auto w{cs::apply::Commit_order_queue::NO_WORKER};
      for (std::tie(w, std::ignore) = free.pop();
           w == cs::apply::Commit_order_queue::NO_WORKER;
           std::tie(w, std::ignore) =
               free.pop())  // Get a free worker to schedule
        std::this_thread::yield();
      scheduled.push(w);        // Schedule the worker
      context[w].store(false);  // Signal the worker
    }
  }};

  coordinator.join();
  for (auto n_wrk = 0; n_wrk != total_workers; ++n_wrk) {
    threads[n_wrk].join();
  }
}

TEST_F(Rpl_commit_order_queue_test, Pushing_while_poping_test) {
  constexpr cs::apply::Commit_order_queue::value_type total_workers{32};
  cs::apply::Commit_order_queue q{total_workers};
  cs::apply::Commit_order_queue f{total_workers};

  std::vector<std::thread> threads;
  for (auto idx = 0; idx != total_workers; ++idx) {
    threads.emplace_back(
        [&](size_t) -> void {
          for (; true;) {
            auto v{cs::apply::Commit_order_queue::NO_WORKER};
            std::tie(v, std::ignore) = q.pop();
            if (v != cs::apply::Commit_order_queue::NO_WORKER) {
              ++m_count;
              f.push(v);
              break;
            }
            std::this_thread::yield();
          }
        },
        idx);
  }

  std::thread producer{[&]() -> void {
    for (cs::apply::Commit_order_queue::value_type idx = 0;
         idx != total_workers; ++idx) {
      q.push(total_workers - idx - 1);
    }
  }};

  producer.join();
  for (auto idx = 0; idx != total_workers; ++idx) {
    threads[idx].join();
  }

  EXPECT_EQ(q.is_empty(), true);
  EXPECT_EQ(m_count.load(), total_workers);

  EXPECT_EQ(q.to_string(), "EOF");
  std::ostringstream oss;
  for (int k = total_workers - 1; k != -1; --k) {
    oss << k << ", " << std::flush;
  }
  oss << "EOF" << std::flush;
  EXPECT_EQ(f.to_string().length(), oss.str().length());

  std::map<cs::apply::Commit_order_queue::value_type,
           cs::apply::Commit_order_queue::value_type>
      dup;
  for (auto v : f) {
    if (v == nullptr) continue;
    bool inserted{false};
    std::tie(std::ignore, inserted) =
        dup.insert(std::make_pair(v->m_worker_id, v->m_worker_id));
    EXPECT_EQ(inserted, true);
  }
  EXPECT_EQ(dup.size(), total_workers);

  EXPECT_EQ(f.is_empty(), false);
  f.clear();
  EXPECT_EQ(f.is_empty(), true);
}

TEST_F(Rpl_commit_order_queue_test, Pushing_then_poping_test) {
  constexpr cs::apply::Commit_order_queue::value_type total_workers{32};
  cs::apply::Commit_order_queue q{total_workers};
  cs::apply::Commit_order_queue f{total_workers};
  m_go.store(false);

  std::vector<std::thread> threads;
  for (auto idx = 0; idx != total_workers; ++idx) {
    threads.emplace_back(
        [&](size_t) -> void {
          for (; true;) {
            if (m_go.load()) break;
            std::this_thread::yield();
          }

          for (; true;) {
            auto v{cs::apply::Commit_order_queue::NO_WORKER};
            std::tie(v, std::ignore) = q.pop();
            if (v != cs::apply::Commit_order_queue::NO_WORKER) {
              ++m_count;
              f.push(v);
              break;
            }
            std::this_thread::yield();
          }
        },
        idx);
  }

  std::thread producer{[&]() -> void {
    for (cs::apply::Commit_order_queue::value_type idx = 0;
         idx != total_workers; ++idx) {
      q.push(total_workers - idx - 1);
    }
    m_go.store(true);
  }};

  producer.join();
  for (auto idx = 0; idx != total_workers; ++idx) {
    threads[idx].join();
  }

  EXPECT_EQ(q.is_empty(), true);
  EXPECT_EQ(
      q.get_state(),
      cs::apply::Commit_order_queue::queue_type::enum_queue_state::SUCCESS);
  EXPECT_EQ(m_count.load(), total_workers);

  EXPECT_EQ(q.to_string(), "EOF");
  std::ostringstream oss;
  for (int k = total_workers - 1; k != -1; --k) {
    oss << k << ", " << std::flush;
  }
  oss << "EOF" << std::flush;
  EXPECT_EQ(f.to_string().length(), oss.str().length());

  auto it = f.begin();
  auto it1{it};
  auto it2 = it1++;
  auto it3 = std::move(it2);
  auto it4{std::move(it3)};
  it2 = it4;
  it3 = std::move(it1);
  EXPECT_NE(*it4, nullptr);
  EXPECT_EQ(it4->m_worker_id, it2->m_worker_id);

  std::map<cs::apply::Commit_order_queue::value_type,
           cs::apply::Commit_order_queue::value_type>
      dup;
  for (auto v : f) {
    if (v == nullptr) continue;
    bool inserted{false};
    std::tie(std::ignore, inserted) =
        dup.insert(std::make_pair(v->m_worker_id, v->m_worker_id));
    EXPECT_EQ(inserted, true);
  }
  EXPECT_EQ(dup.size(), total_workers);

  EXPECT_EQ(f.is_empty(), false);
  f.clear();
  EXPECT_EQ(f.is_empty(), true);
}

TEST_F(Rpl_commit_order_queue_test, Remove) {
  constexpr cs::apply::Commit_order_queue::value_type total_workers{4};
  cs::apply::Commit_order_queue queue{total_workers};
  auto this_worker{cs::apply::Commit_order_queue::NO_WORKER};
  auto next_seq_nr{0};

  queue.push(0);
  queue.push(1);
  queue.push(2);
  queue.push(3);
  /*
    +----------------------+----+----+----+----+
    | worker               |  0 |  1 |  2 |  3 |
    | sequence number      |  2 |  3 |  4 |  5 |
    | next sequence number |  3 |  4 |  5 |  6 |
    +----------------------+----+----+----+----+
  */
  EXPECT_EQ(queue.to_string(), "0, 1, 2, 3, EOF");

  std::tie(this_worker, next_seq_nr) = queue.remove(0);
  /*
    +----------------------+----+----+----+
    | worker               |  1 |  2 |  3 |
    | sequence number      |  3 |  4 |  5 |
    | next sequence number |  4 |  5 |  6 |
    +----------------------+----+----+----+
  */
  EXPECT_EQ(this_worker, 0);
  EXPECT_EQ(next_seq_nr, 3);
  EXPECT_EQ(queue.to_string(), "1, 2, 3, EOF");

  std::tie(this_worker, next_seq_nr) = queue.remove(3);
  /*
    +----------------------+----+----+
    | worker               |  1 |  2 |
    | sequence number      |  3 |  4 |
    | next sequence number |  4 |  6 |
    +----------------------+----+----+
  */
  EXPECT_EQ(this_worker, 3);
  EXPECT_EQ(next_seq_nr, 0);  // NO_SEQUENCE_NR
  EXPECT_EQ(queue.to_string(), "1, 2, EOF");

  queue.push(0);
  /*
    +----------------------+----+----+----+
    | worker               |  1 |  2 |  0 |
    | sequence number      |  3 |  4 |  6 |
    | next sequence number |  4 |  6 |  7 |
    +----------------------+----+----+----+
  */
  EXPECT_EQ(queue.to_string(), "1, 2, 0, EOF");

  std::tie(this_worker, next_seq_nr) = queue.remove(2);
  /*
    +----------------------+----+----+
    | worker               |  1 |  0 |
    | sequence number      |  3 |  6 |
    | next sequence number |  6 |  7 |
    +----------------------+----+----+
  */
  EXPECT_EQ(this_worker, 2);
  EXPECT_EQ(next_seq_nr, 0);  // NO_SEQUENCE_NR
  EXPECT_EQ(queue.to_string(), "1, 0, EOF");

  std::tie(this_worker, next_seq_nr) = queue.pop();
  /*
    +----------------------+----+
    | worker               |  0 |
    | sequence number      |  6 |
    | next sequence number |  7 |
    +----------------------+----+
  */
  EXPECT_EQ(this_worker, 1);
  EXPECT_EQ(next_seq_nr, 6);
  EXPECT_EQ(queue.to_string(), "0, EOF");

  std::tie(this_worker, next_seq_nr) = queue.pop();
  /*
    +----------------------+
    | worker               |
    | sequence number      |
    | next sequence number |
    +----------------------+
  */
  EXPECT_EQ(this_worker, 0);
  EXPECT_EQ(next_seq_nr, 7);
  EXPECT_EQ(queue.to_string(), "EOF");

  std::tie(this_worker, next_seq_nr) = queue.remove(50);
  EXPECT_EQ(this_worker, -1);  // NO_WORKER
  EXPECT_EQ(next_seq_nr, 0);   // NO_SEQUENCE_NR
  EXPECT_EQ(queue.to_string(), "EOF");
}

}  // namespace unittests
}  // namespace rpl