File: autorejoin.h

package info (click to toggle)
mysql-8.0 8.0.45-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 1,273,048 kB
  • sloc: cpp: 4,685,434; ansic: 412,712; pascal: 108,396; 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 (171 lines) | stat: -rw-r--r-- 5,810 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
/* Copyright (c) 2018, 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 */

#ifndef GR_AUTOREJOIN_INCLUDED
#define GR_AUTOREJOIN_INCLUDED

#include "plugin/group_replication/include/member_info.h"
#include "plugin/group_replication/include/plugin_utils.h"

/**
  Represents and encapsulates the logic responsible for handling the auto-rejoin
  process within Group Replication. The auto-rejoin process kicks in one of two
  possible situations: either the member was expelled from the group or the
  member lost contact to a majority of the group. The auto-rejoin feature must
  also be explicitly enabled by setting the group_replication_autorejoin_tries
  sysvar to a value greater than 0.

  This thread will do a busy-wait loop for group_replication_autorejoin_tries
  number of attempts, waiting 5 minutes between each attempt (this wait is
  achieved via a timed wait on a conditional variable).

  We execute the auto-rejoin process in its own thead because the join operation
  of the GCS layer is asynchronous. We cannot actually block while waiting for a
  confirmation if the server managed to join the group or not. As such, we wait
  on a callback invoked by an entity that is registered as a GCS event listener.

  @sa Plugin_gcs_events_handler
*/
class Autorejoin_thread {
 public:
  /**
    Deleted copy ctor.
  */
  Autorejoin_thread(const Autorejoin_thread &) = delete;

  /**
    Deleted move ctor.
  */
  Autorejoin_thread(const Autorejoin_thread &&) = delete;

  /**
    Deleted assignment operator.
  */
  Autorejoin_thread &operator=(const Autorejoin_thread &) = delete;

  /**
    Deleted move operator.
  */
  Autorejoin_thread &operator=(const Autorejoin_thread &&) = delete;

  /**
    Initializes the synchronization primitives of the thread.
  */
  Autorejoin_thread();

  /**
    The dtor for the thread will destroy the mutex and cond_var.
  */
  ~Autorejoin_thread();

  /**
    Initializes the auto-rejoin module with a clean slate, i.e. it
    resets any state/flags that are checked in start_autorejoin().

    @sa start_autorejoin
  */
  void init();

  /**
    Aborts the thread's main loop, effectively killing the thread.

    @return a flag indicating whether or not the auto-rejoin procedure was
    ongoing at the time the abort was requested.
    @retval true the auto-rejoin was ongoing
    @retval false the auto-rejoin wasn't running
  */
  bool abort_rejoin();

  /**
    Starts the process of auto-rejoin, launches the thread that will call
    attempt_rejoin() until it succeeds or until it reaches a given
    amount of maximum retries, waiting on a conditional variable each
    iteration with a given timeout.

    An auto-rejoin can only start if it isn't already running or if the
    auto-rejoin module is not in the process of being terminated.

    @param[in] attempts the number of attempts we will try to rejoin.
    @param[in] timeout the time to wait between each retry.
    @return whether or not we managed to launch the auto-rejoin thread.
      @retval 0 the thread launched successfully
      @retval != 0 the thread couldn't be launched
    @sa mysql_thread_create
  */
  int start_autorejoin(uint attempts, ulonglong timeout);

  /**
    Returns a flag indicating whether or not the auto-rejoin process is ongoing
    on this thread.

    @return the state of the rejoin process.
    @retval true if the auto-rejoin is ongoing
    @retval false otherwise
  */
  bool is_autorejoin_ongoing();

 private:
  /**
    The thread callback passed onto mysql_thread_create.

    @param[in] arg a pointer to an Autorejoin_thread instance.

    @return Does not return.
  */
  static void *launch_thread(void *arg);

  /**
    The thread handle, i.e. setups and tearsdown the infrastructure for this
    mysql_thread.
  */
  [[noreturn]] void autorejoin_thread_handle();

  /**
    Handles the busy-wait retry loop.
  */
  void execute_rejoin_process();

  /** the THD handle. */
  THD *m_thd;
  /** the state of the thread. */
  thread_state m_autorejoin_thd_state;
  /** the thread handle. */
  my_thread_handle m_handle;
  /** the mutex for controlling access to the thread itself. */
  mysql_mutex_t m_run_lock;
  /** the cond_var used to signal the thread. */
  mysql_cond_t m_run_cond;
  /** flag to indicate whether or not the thread is to be aborted. */
  std::atomic<bool> m_abort;
  /**
    flag that indicates that the auto-rejoin module is in the process of
    being terminated.
  */
  bool m_being_terminated;
  /** the number of attempts for the rejoin. */
  ulong m_attempts;
  /** the time to wait in seconds until the next rejoin attempt. */
  ulonglong m_rejoin_timeout;
};

#endif /* GR_AUTREJOIN_INCLUDED */