File: rpl_commit_stage_manager.cc

package info (click to toggle)
mysql-8.0 8.0.44-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 1,272,892 kB
  • sloc: cpp: 4,685,345; ansic: 412,712; pascal: 108,395; java: 83,641; perl: 30,221; cs: 27,067; sql: 26,594; python: 21,816; sh: 17,285; yacc: 17,169; php: 11,522; xml: 7,388; javascript: 7,083; makefile: 1,793; lex: 1,075; awk: 670; asm: 520; objc: 183; ruby: 97; lisp: 86
file content (577 lines) | stat: -rw-r--r-- 21,280 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
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
/* Copyright (c) 2019, 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 <algorithm>

#include "mutex_lock.h"            // MUTEX_LOCK
#include "mysql/psi/mysql_cond.h"  // mysql_cond_timedwait
#include "sql/binlog.h"
#include "sql/binlog/group_commit/bgc_ticket_manager.h"  // Bgc_ticket_manager
#include "sql/debug_sync.h"                              // DEBUG_SYNC
#include "sql/mysqld.h"       //PSI_stage_info stage_wait_on_commit_ticket
#include "sql/raii/sentry.h"  // raii::Sentry<>
#include "sql/rpl_commit_stage_manager.h"
#include "sql/rpl_replica_commit_order_manager.h"  // Commit_order_manager
#include "sql/rpl_rli_pdb.h"                       // Slave_worker

class Slave_worker;
class Commit_order_manager;

#define YESNO(X) ((X) ? "yes" : "no")

bool Commit_stage_manager::Mutex_queue::append(THD *first) {
  DBUG_TRACE;
  DBUG_PRINT("enter", ("first: 0x%llx", (ulonglong)first));
  DBUG_PRINT("info",
             ("m_first: 0x%llx, &m_first: 0x%llx, m_last: 0x%llx",
              (ulonglong)m_first, (ulonglong)&m_first, (ulonglong)m_last));
  int32 count = 1;
  bool empty = (m_first == nullptr);

  *m_last = first;
  DBUG_PRINT("info",
             ("m_first: 0x%llx, &m_first: 0x%llx, m_last: 0x%llx",
              (ulonglong)m_first, (ulonglong)&m_first, (ulonglong)m_last));
  /*
    Go to the last THD instance of the list. We expect lists to be
    moderately short. If they are not, we need to track the end of
    the queue as well.
  */

  while (first->next_to_commit) {
    count++;
    first = first->next_to_commit;
  }
  m_size += count;

  m_last = &first->next_to_commit;
  DBUG_PRINT("info",
             ("m_first: 0x%llx, &m_first: 0x%llx, m_last: 0x%llx",
              (ulonglong)m_first, (ulonglong)&m_first, (ulonglong)m_last));
  assert(m_first || m_last == &m_first);
  DBUG_PRINT("return", ("empty: %s", YESNO(empty)));
  return empty;
}

std::pair<bool, THD *> Commit_stage_manager::Mutex_queue::pop_front() {
  DBUG_TRACE;
  lock();
  THD *result = m_first;
  bool more = true;
  /*
    We do not set next_to_commit to nullptr here since this is only used
    in the flush stage. We will have to call fetch_queue last here,
    and will then "cut" the linked list by setting the end of that
    queue to nullptr.
  */
  if (result) m_first = result->next_to_commit;
  if (m_first == nullptr) {
    more = false;
    m_last = &m_first;
  }
  assert(m_size.load() > 0);
  --m_size;
  assert(m_first || m_last == &m_first);
  unlock();
  DBUG_PRINT("return",
             ("result: 0x%llx, more: %s", (ulonglong)result, YESNO(more)));
  return std::make_pair(more, result);
}

void Commit_stage_manager::init(PSI_mutex_key key_LOCK_flush_queue,
                                PSI_mutex_key key_LOCK_sync_queue,
                                PSI_mutex_key key_LOCK_commit_queue,
                                PSI_mutex_key key_LOCK_after_commit_queue,
                                PSI_mutex_key key_LOCK_done,
                                PSI_mutex_key key_LOCK_wait_for_group_turn,
                                PSI_cond_key key_COND_done,
                                PSI_cond_key key_COND_flush_queue,
                                PSI_cond_key key_COND_wait_for_group_turn) {
  if (m_is_initialized) return;
  m_is_initialized = true;

  mysql_mutex_init(key_LOCK_done, &m_lock_done, MY_MUTEX_INIT_FAST);
  mysql_cond_init(key_COND_done, &m_stage_cond_binlog);
  mysql_cond_init(key_COND_done, &m_stage_cond_commit_order);
  mysql_cond_init(key_COND_flush_queue, &m_stage_cond_leader);
  mysql_cond_init(key_COND_wait_for_group_turn,
                  &this->m_cond_wait_for_ticket_turn);
#ifndef NDEBUG
  leader_thd = nullptr;

  /**
    reuse key_COND_done 'cos a new PSI object would be wasteful in !NDEBUG
  */
  mysql_cond_init(key_COND_done, &m_cond_preempt);
#endif

  /**
    Initialize mutex for flush, sync, commit and after commit stage queue. The
    binlog flush stage and commit order flush stage share same mutex.
  */
  mysql_mutex_init(key_LOCK_flush_queue, &m_queue_lock[BINLOG_FLUSH_STAGE],
                   MY_MUTEX_INIT_FAST);
  mysql_mutex_init(key_LOCK_sync_queue, &m_queue_lock[SYNC_STAGE],
                   MY_MUTEX_INIT_FAST);
  mysql_mutex_init(key_LOCK_commit_queue, &m_queue_lock[COMMIT_STAGE],
                   MY_MUTEX_INIT_FAST);
  mysql_mutex_init(key_LOCK_after_commit_queue,
                   &m_queue_lock[AFTER_COMMIT_STAGE], MY_MUTEX_INIT_FAST);
  mysql_mutex_init(key_LOCK_wait_for_group_turn,
                   &this->m_lock_wait_for_ticket_turn, MY_MUTEX_INIT_FAST);

  m_queue[BINLOG_FLUSH_STAGE].init(&m_queue_lock[BINLOG_FLUSH_STAGE]);
  m_queue[SYNC_STAGE].init(&m_queue_lock[SYNC_STAGE]);
  m_queue[COMMIT_STAGE].init(&m_queue_lock[COMMIT_STAGE]);
  m_queue[AFTER_COMMIT_STAGE].init(&m_queue_lock[AFTER_COMMIT_STAGE]);
  m_queue[COMMIT_ORDER_FLUSH_STAGE].init(&m_queue_lock[BINLOG_FLUSH_STAGE]);
}

void Commit_stage_manager::deinit() {
  if (!m_is_initialized) return;
  m_is_initialized = false;

  mysql_mutex_destroy(&m_queue_lock[BINLOG_FLUSH_STAGE]);
  mysql_mutex_destroy(&m_queue_lock[SYNC_STAGE]);
  mysql_mutex_destroy(&m_queue_lock[COMMIT_STAGE]);
  mysql_mutex_destroy(&m_queue_lock[AFTER_COMMIT_STAGE]);

  mysql_cond_destroy(&m_stage_cond_binlog);
  mysql_cond_destroy(&m_stage_cond_commit_order);
  mysql_cond_destroy(&m_stage_cond_leader);
  mysql_mutex_destroy(&m_lock_done);
  mysql_cond_destroy(&this->m_cond_wait_for_ticket_turn);
  mysql_mutex_destroy(&this->m_lock_wait_for_ticket_turn);
}

bool Commit_stage_manager::is_ticket_on_its_turn_and_back_ticket_incremented(
    THD *thd) const {
  if (!thd->is_applier_thread()) {
    assert(false);
    return false;
  }

  auto &ticket_manager = binlog::Bgc_ticket_manager::instance();
  auto &ticket_ctx = thd->rpl_thd_ctx.binlog_group_commit_ctx();
  binlog::BgcTicket ticket(ticket_ctx.get_session_ticket());

  if (ticket == ticket_manager.get_front_ticket() &&
      ticket < ticket_manager.get_back_ticket()) {
    return true;
  }

  return false;
}

void Commit_stage_manager::wait_for_ticket_turn(THD *thd,
                                                bool update_ticket_manager) {
  auto &ticket_ctx = thd->rpl_thd_ctx.binlog_group_commit_ctx();
  if (ticket_ctx.has_waited()) return;

  auto &ticket_manager = binlog::Bgc_ticket_manager::instance();
  binlog::BgcTicket ticket(ticket_ctx.get_session_ticket());

  CONDITIONAL_SYNC_POINT_FOR_TIMESTAMP("before_wait_on_ticket");
  // Check, first, if the session ticket is already being processed, to avoid
  // acquiring the mutex
  if (ticket != ticket_manager.get_front_ticket() &&
      ticket > ticket_manager.get_coalesced_ticket() && !thd->killed) {
    CONDITIONAL_SYNC_POINT_FOR_TIMESTAMP("inside_wait_on_ticket");
    PSI_stage_info old_stage;
    {
      MUTEX_LOCK(guard, &this->m_lock_wait_for_ticket_turn);
      thd->ENTER_COND(&this->m_cond_wait_for_ticket_turn,
                      &this->m_lock_wait_for_ticket_turn,
                      &stage_wait_on_commit_ticket, &old_stage);
      struct timespec abstime;
      while (ticket != ticket_manager.get_front_ticket() &&
             ticket > ticket_manager.get_coalesced_ticket() && !thd->killed) {
        // in rare cases View Changes cause ticket changes with no broadcast
        set_timespec(&abstime, 1);
        mysql_cond_timedwait(&this->m_cond_wait_for_ticket_turn,
                             &this->m_lock_wait_for_ticket_turn, &abstime);
      }
    }
    thd->EXIT_COND(&old_stage);
  }

#ifndef NDEBUG
  if (Binlog_group_commit_ctx::manual_ticket_setting()->load()) {
    assert(ticket >= ticket_manager.get_coalesced_ticket());
  }
#endif

  if (update_ticket_manager) {
    this->update_session_ticket_state(thd);
  }
}

bool Commit_stage_manager::append_to(StageID stage, THD *thd) {
  this->wait_for_ticket_turn(thd, false);
  raii::Sentry<> _ticket_guard{
      [thd, this]() -> void { this->update_session_ticket_state(thd); }};

  lock_queue(stage);
  return m_queue[stage].append(thd);
}

bool Commit_stage_manager::enroll_for(StageID stage, THD *thd,
                                      mysql_mutex_t *stage_mutex,
                                      mysql_mutex_t *enter_mutex) {
  DBUG_TRACE;

  // If the queue was empty: we're the leader for this batch
  DBUG_PRINT("debug",
             ("Enqueue 0x%llx to queue for stage %d", (ulonglong)thd, stage));

  thd->rpl_thd_ctx.binlog_group_commit_ctx().assign_ticket();
  bool leader = this->append_to(stage, thd);

  /*
   if its FLUSH stage queue (BINLOG_FLUSH_STAGE or COMMIT_ORDER_FLUSH_STAGE)
   and not empty then this thread should not become leader as other queue
   already has leader. The leader acquires enter_mutex.
  */
  if (leader) {
    if (stage == COMMIT_ORDER_FLUSH_STAGE) {
      leader = m_queue[BINLOG_FLUSH_STAGE].is_empty();
    } else if (stage == BINLOG_FLUSH_STAGE &&
               !m_queue[COMMIT_ORDER_FLUSH_STAGE].is_empty()) {
      /*
        The current thread is the first one in the binlog queue, but there is
        already a leader for the commit order queue. Then we need to change
        leader, so the commit order leader changes to follower and the current
        threads becomes leader.

        The reason we need to change leader is that the commit order leader
        cannot be leader for binlog threads, since commit order threads have to
        leave the commit group before the binlog threads are done.

        The process to change leader is as follows:
        1. The first thread to enter the flush stage is a commit order thread.
           It becomes commit order leader.
        2. The commit order leader tries to acquire the stage mutex. This may
           take some time, since the mutex is held by the leader for the
           previous commit group.
        3. Meanwhile, a binlog thread enters the flush stage. It reaches this
           point, and waits for signal from the commit order leader.
        4. The commit order leader gets the stage mutex. Then it checks if any
           binlog thread entered the flush stage, finds that one did, and
           decides to change leader.
        5. The commit order leader signals the binlog leader, becomes follower,
           and waits for the commit to complete (just like other followers do).
        6. The binlog leader wakes up by the signal that the commit order leader
           sent in step 5, and performs the group commit.
      */
      CONDITIONAL_SYNC_POINT_FOR_TIMESTAMP("before_binlog_leader_wait");
      while (thd->tx_commit_pending)
        mysql_cond_wait(&m_stage_cond_leader,
                        &m_queue_lock[BINLOG_FLUSH_STAGE]);
    }
  }

  unlock_queue(stage);

  /* Notify next transaction in commit order that it can enter the queue. */
  if (stage == BINLOG_FLUSH_STAGE) {
    Commit_order_manager::finish_one(thd);
    CONDITIONAL_SYNC_POINT_FOR_TIMESTAMP("after_binlog_leader_wait");
  } else if (stage == COMMIT_ORDER_FLUSH_STAGE) {
    Commit_order_manager::finish_one(thd);
  }

  /*
    The stage mutex can be nullptr if we are enrolling for the first
    stage.
  */
  if (stage_mutex) mysql_mutex_unlock(stage_mutex);

#ifndef NDEBUG
  DBUG_PRINT("info", ("This is a leader thread: %d (0=n 1=y)", leader));

  DEBUG_SYNC(thd, "after_enrolling_for_stage");

  switch (stage) {
    case BINLOG_FLUSH_STAGE:
      DEBUG_SYNC(thd, "bgc_after_enrolling_for_flush_stage");
      CONDITIONAL_SYNC_POINT_FOR_TIMESTAMP(
          "bgc_after_enrolling_for_flush_stage");
      break;
    case SYNC_STAGE:
      DEBUG_SYNC(thd, "bgc_after_enrolling_for_sync_stage");
      CONDITIONAL_SYNC_POINT_FOR_TIMESTAMP(
          "bgc_after_enrolling_for_sync_stage");
      break;
    case COMMIT_STAGE:
      DEBUG_SYNC(thd, "bgc_after_enrolling_for_commit_stage");
      CONDITIONAL_SYNC_POINT_FOR_TIMESTAMP("after_writing_to_tc_log");
      break;
    case AFTER_COMMIT_STAGE:
      DEBUG_SYNC(thd, "bgc_after_enrolling_for_after_commit_stage");
      CONDITIONAL_SYNC_POINT_FOR_TIMESTAMP(
          "bgc_after_enrolling_for_after_commit_stage");
      break;
    case COMMIT_ORDER_FLUSH_STAGE:
      break;
    default:
      // not reached
      assert(0);
  }

  DBUG_EXECUTE_IF("assert_leader", assert(leader););
  DBUG_EXECUTE_IF("assert_follower", assert(!leader););
#endif

  /*
    If the queue was not empty, we're a follower and wait for the
    leader to process the queue. If we were holding a mutex, we have
    to release it before going to sleep.
  */
  if (!leader) {
    CONDITIONAL_SYNC_POINT_FOR_TIMESTAMP("before_follower_wait");
    mysql_mutex_lock(&m_lock_done);
#ifndef NDEBUG
    /*
      Leader can be awaiting all-clear to preempt follower's execution.
      With setting the status the follower ensures it won't execute anything
      including thread-specific code.
    */
    thd->get_transaction()->m_flags.ready_preempt = true;
    if (leader_await_preempt_status) mysql_cond_signal(&m_cond_preempt);
#endif
    while (thd->tx_commit_pending) {
      if (stage == COMMIT_ORDER_FLUSH_STAGE) {
        mysql_cond_wait(&m_stage_cond_commit_order, &m_lock_done);
      } else {
        mysql_cond_wait(&m_stage_cond_binlog, &m_lock_done);
      }
    }

    mysql_mutex_unlock(&m_lock_done);
    return false;
  }

#ifndef NDEBUG
  if (stage == Commit_stage_manager::SYNC_STAGE)
    DEBUG_SYNC(thd, "bgc_between_flush_and_sync");
#endif

  if (leader && enter_mutex != nullptr) {
    mysql_mutex_lock(enter_mutex);
  }

  if (stage == COMMIT_ORDER_FLUSH_STAGE) {
    CONDITIONAL_SYNC_POINT_FOR_TIMESTAMP(
        "after_commit_order_thread_becomes_leader");
    lock_queue(stage);

    if (!m_queue[BINLOG_FLUSH_STAGE].is_empty()) {
      mysql_mutex_unlock(enter_mutex);

      THD *binlog_leader = m_queue[BINLOG_FLUSH_STAGE].get_leader();
      binlog_leader->tx_commit_pending = false;

      mysql_cond_signal(&m_stage_cond_leader);
      unlock_queue(stage);

      mysql_mutex_lock(&m_lock_done);
      /* wait for signal from binlog leader */
      CONDITIONAL_SYNC_POINT_FOR_TIMESTAMP(
          "before_commit_order_leader_waits_for_binlog_leader");
      while (thd->tx_commit_pending)
        mysql_cond_wait(&m_stage_cond_commit_order, &m_lock_done);
      mysql_mutex_unlock(&m_lock_done);

      leader = false;
      return leader;
    }
  }

  return leader;
}

THD *Commit_stage_manager::Mutex_queue::fetch_and_empty_acquire_lock() {
  lock();
  THD *ret = fetch_and_empty();
  unlock();
  return ret;
}

THD *Commit_stage_manager::Mutex_queue::fetch_and_empty_skip_acquire_lock() {
  assert_owner();
  return fetch_and_empty();
}

THD *Commit_stage_manager::Mutex_queue::fetch_and_empty() {
  DBUG_TRACE;
  DBUG_PRINT("enter",
             ("m_first: 0x%llx, &m_first: 0x%llx, m_last: 0x%llx",
              (ulonglong)m_first, (ulonglong)&m_first, (ulonglong)m_last));
  THD *result = m_first;
  m_first = nullptr;
  m_last = &m_first;
  DBUG_PRINT("info",
             ("m_first: 0x%llx, &m_first: 0x%llx, m_last: 0x%llx",
              (ulonglong)m_first, (ulonglong)&m_first, (ulonglong)m_last));
  DBUG_PRINT("info", ("fetched queue of %d transactions", m_size.load()));
  DBUG_PRINT("return", ("result: 0x%llx", (ulonglong)result));
  assert(m_size.load() >= 0);
  m_size.store(0);
  return result;
}

void Commit_stage_manager::wait_count_or_timeout(ulong count, long usec,
                                                 StageID stage) {
  long to_wait = DBUG_EVALUATE_IF("bgc_set_infinite_delay", LONG_MAX, usec);
  /*
    For testing purposes while waiting for inifinity
    to arrive, we keep checking the queue size at regular,
    small intervals. Otherwise, waiting 0.1 * infinite
    is too long.
   */
  long delta = DBUG_EVALUATE_IF("bgc_set_infinite_delay", 100000,
                                std::max<long>(1, (to_wait * 0.1)));

  while (
      to_wait > 0 &&
      (count == 0 || static_cast<ulong>(m_queue[stage].get_size()) < count)) {
#ifndef NDEBUG
    if (current_thd) DEBUG_SYNC(current_thd, "bgc_wait_count_or_timeout");
#endif
    my_sleep(delta);
    to_wait -= delta;
  }
}

THD *Commit_stage_manager::fetch_queue_acquire_lock(StageID stage) {
  DBUG_PRINT("debug", ("Fetching queue for stage %d", stage));
  return m_queue[stage].fetch_and_empty_acquire_lock();
}

THD *Commit_stage_manager::fetch_queue_skip_acquire_lock(StageID stage) {
  DBUG_PRINT("debug", ("Fetching queue for stage %d", stage));
  return m_queue[stage].fetch_and_empty_skip_acquire_lock();
}

void Commit_stage_manager::process_final_stage_for_ordered_commit_group(
    THD *first) {
  if (first != nullptr) {
    gtid_state->update_commit_group(first);
    signal_done(first, Commit_stage_manager::COMMIT_ORDER_FLUSH_STAGE);
  }
}

void Commit_stage_manager::signal_done(THD *queue, StageID stage) {
  mysql_mutex_lock(&m_lock_done);

  for (THD *thd = queue; thd; thd = thd->next_to_commit) {
    thd->tx_commit_pending = false;
    thd->rpl_thd_ctx.binlog_group_commit_ctx().reset();
  }

  /* if thread belong to commit order wake only commit order queue threads */
  if (stage == COMMIT_ORDER_FLUSH_STAGE)
    mysql_cond_broadcast(&m_stage_cond_commit_order);
  else
    mysql_cond_broadcast(&m_stage_cond_binlog);

  mysql_mutex_unlock(&m_lock_done);
}

void Commit_stage_manager::signal_end_of_ticket(bool force) {
  auto &ticket_manager = binlog::Bgc_ticket_manager::instance();
  // Check, first, if there are any tickets other than the active, to avoid
  // taking the mutex.
  if (force ||
      ticket_manager.get_front_ticket() != ticket_manager.get_back_ticket()) {
    auto [previous_front, current_front] = ticket_manager.pop_front_ticket();
    // if pop was successful - front changed, notify waiting threads
    if (force || previous_front != current_front) {
      MUTEX_LOCK(guard, &this->m_lock_wait_for_ticket_turn);
      mysql_cond_broadcast(&this->m_cond_wait_for_ticket_turn);
    }
  }
}

void Commit_stage_manager::update_session_ticket_state(THD *thd) {
  auto &ticket_ctx = thd->rpl_thd_ctx.binlog_group_commit_ctx();
  if (ticket_ctx.has_waited()) return;
  if (ticket_ctx.get_session_ticket() >
      binlog::Bgc_ticket_manager::instance().get_coalesced_ticket())
    this->update_ticket_manager(1, ticket_ctx.get_session_ticket());
  ticket_ctx.mark_as_already_waited();
}

void Commit_stage_manager::update_ticket_manager(
    std::uint64_t sessions_count, const binlog::BgcTicket &session_ticket) {
  auto &ticket_manager = binlog::Bgc_ticket_manager::instance();
  ticket_manager.add_processed_sessions_to_front_ticket(sessions_count,
                                                        session_ticket);

  DBUG_EXECUTE_IF("rpl_end_of_ticket_blocked", {
    const char act[] =
        "now signal signal.end_of_ticket_waiting wait_for "
        "signal.end_of_ticket_continue";
    assert(!debug_sync_set_action(current_thd, STRING_WITH_LEN(act)));
  });

  this->signal_end_of_ticket();
}

void Commit_stage_manager::finish_session_ticket(THD *thd) {
  auto &ticket_ctx = thd->rpl_thd_ctx.binlog_group_commit_ctx();
  if (ticket_ctx.get_session_ticket().is_set()) {
    this->wait_for_ticket_turn(thd);
    ticket_ctx.reset();
  }
}

void Commit_stage_manager::disable_manual_session_tickets() {
  if (!Binlog_group_commit_ctx::manual_ticket_setting()->load()) return;
  Binlog_group_commit_ctx::manual_ticket_setting()->store(false);
  binlog::Bgc_ticket_manager::instance().coalesce();
  Commit_stage_manager::get_instance().signal_end_of_ticket(true);
}

void Commit_stage_manager::enable_manual_session_tickets() {
  Binlog_group_commit_ctx::manual_ticket_setting()->store(true);
}

#ifndef NDEBUG
void Commit_stage_manager::clear_preempt_status(THD *head) {
  assert(head);

  mysql_mutex_lock(&m_lock_done);
  while (!head->get_transaction()->m_flags.ready_preempt) {
    leader_await_preempt_status = true;
    mysql_cond_wait(&m_cond_preempt, &m_lock_done);
  }
  leader_await_preempt_status = false;
  mysql_mutex_unlock(&m_lock_done);
}
#endif

Commit_stage_manager &Commit_stage_manager::get_instance() {
  static Commit_stage_manager shared_instance;
  return shared_instance;
}