MySQL 26.10.0
Source Code Documentation
health_monitor_timer.h
Go to the documentation of this file.
1/* Copyright (c) 2021, 2026, Oracle and/or its affiliates.
2
3This program is free software; you can redistribute it and/or modify
4it under the terms of the GNU General Public License, version 2.0,
5as published by the Free Software Foundation.
6
7This program is designed to work with certain software (including
8but not limited to OpenSSL) that is licensed under separate terms,
9as designated in a particular file or component or in included license
10documentation. The authors of MySQL hereby grant you an additional
11permission to link the program and your derivative works with the
12separately licensed software that they have either included with
13the program or referenced in the documentation.
14
15This program is distributed in the hope that it will be useful,
16but WITHOUT ANY WARRANTY; without even the implied warranty of
17MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18GNU General Public License, version 2.0, for more details.
19
20You should have received a copy of the GNU General Public License
21along with this program; if not, write to the Free Software
22Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */
23
24#pragma once
25
26#include <string>
27
28namespace hm {
29
30// clang-format off
31
32static const int k_default_timer_interval{10};
33static const int k_default_timer_timeout{60};
34static const int k_default_report_interval{30};
35
36/**
37 Server mode timer used for fallback mode, recovery mode, etc.
38 - Time-based interval, driven by the collection cycle, i.e. one tick per cycle
39 - Timer interval (tick) is the base monitoring interval of the collector (10s)
40 - Reporting interval is the interval of issuing status messages, and should
41 be a multiple of the timer interval
42
43 NOTE: This timer assumes a constant Collector base interval.
44 If the base interval is dynamic, then the Mode_Timer must be modified
45 to use the current Collector base interval for the tick interval.
46*/
48 public:
49 Mode_Timer() = default;
50
51 Mode_Timer(std::string &name, const int interval)
52 : m_name{name},
54
55 void start();
56 void stop() { m_running = false; }
57 void cancel();
58 void reset();
59 void set_timeout(int timeout);
60 void adjust_timeout();
61 void tick();
62 bool report_status() const;
63 std::string format_status() const;
64
65 bool canceled() const { return m_canceled; }
67 bool expired() const { return m_expired; }
68 int interval() const { return m_interval; }
70 int report_interval() const { return m_report_interval; }
72 int remaining() const;
73 bool running() const { return m_running; }
74 bool started() const { return m_started; }
75 int timeout() const { return m_timeout; }
76 std::string name() const { return m_name; }
77 void set_name(const std::string name) { m_name = name; }
78 std::string running_msg() const { return m_running_msg; }
79 void set_running_msg(const std::string &msg) { m_running_msg = msg; }
80
81 private:
82 std::string m_name;
83 int m_timer{0}; // time counter
84 int m_interval{k_default_timer_interval}; // timer interval, usually the collector base interval
85 int m_report_interval{k_default_report_interval}; // interval for reporting status, multiple of interval
86 int m_timeout{0}; // length of the timer in seconds
87 bool m_running{false}; // timer is started, advancing and not expired
88 bool m_started{false}; // timer initialized, may or may not be running or expired
89 bool m_expired{false}; // timer counter > timeout
90 bool m_canceled{false}; // state and counter reset, timeout and interval preserved
91 std::string m_running_msg; // timer-specific running message
92};
93
94/**
95 Retry timer with backoff.
96 - Used for retrying operations e.g. setting SRO, terminating connections, etc.
97 - Driven by a sleep wait interval until max retries reached
98 - Unrelated to the Collector base interval
99*/
101 public:
104 bool wait();
105 void reset();
106 int max_retries() const { return m_max_retries; }
108 int retry_count() const { return m_retry_count; }
109 int remaining() const { return m_max_retries - m_retry_count; }
110 int next_wait() const { return m_next_wait_ms; }
111 std::string format_status() const;
112
113 private:
116 int m_wait_ms{0};
118};
119
120/**
121 Cycle-driven timer.
122 - Interval = time between ticks
123 - Timeout = total time before expiration
124*/
126 public:
127 Cycle_Timer() = default;
128
132
133 void tick();
134 void tick(int interval);
135 void reset();
136 void set_name(const std::string &name) { m_name = name; }
137 void set_interval(int interval);
138 void set_timeout(int timeout);
139 void adjust_timeout();
140 int interval() const { return m_interval; }
141 int timeout() const { return m_timeout; }
142 bool expired() const { return m_expired; }
143 int remaining() const;
144 std::string format_status() const;
145
146 private:
147 std::string m_name{"Cycle_Timer"};
148 int m_timer{0}; // time counter
149 int m_interval{k_default_timer_interval}; // timer interval, usually the collector base interval
150 int m_timeout{k_default_timer_timeout}; // length of the timer in seconds
151 bool m_expired{false}; // timer counter > timeout
152};
153
154// clang-format on
155
156} // namespace hm
Cycle-driven timer.
Definition: health_monitor_timer.h:125
void set_interval(int interval)
Cycle Timer - Set interval.
Definition: health_monitor_timer.cc:207
std::string format_status() const
Cycle Timer - Return formatted string with timer settings.
Definition: health_monitor_timer.cc:239
void reset()
Cycle Timer - Reset.
Definition: health_monitor_timer.cc:199
void set_timeout(int timeout)
Cycle Timer - Set timeout.
Definition: health_monitor_timer.cc:215
void set_name(const std::string &name)
Definition: health_monitor_timer.h:136
int interval() const
Definition: health_monitor_timer.h:140
Cycle_Timer(int interval, int timeout)
Definition: health_monitor_timer.h:129
void tick()
Cycle Timer - Advance the timer using the default interval.
Definition: health_monitor_timer.cc:179
bool expired() const
Definition: health_monitor_timer.h:142
int remaining() const
Cycle Timer - Return seconds remaining or 0.
Definition: health_monitor_timer.cc:232
int m_timer
Definition: health_monitor_timer.h:148
int timeout() const
Definition: health_monitor_timer.h:141
Cycle_Timer()=default
int m_interval
Definition: health_monitor_timer.h:149
bool m_expired
Definition: health_monitor_timer.h:151
std::string m_name
Definition: health_monitor_timer.h:147
void adjust_timeout()
Cycle Timer - Round up timeout to the next interval.
Definition: health_monitor_timer.cc:224
int m_timeout
Definition: health_monitor_timer.h:150
Server mode timer used for fallback mode, recovery mode, etc.
Definition: health_monitor_timer.h:47
bool m_canceled
Definition: health_monitor_timer.h:90
int m_timeout
Definition: health_monitor_timer.h:86
void set_name(const std::string name)
Definition: health_monitor_timer.h:77
int m_report_interval
Definition: health_monitor_timer.h:85
void stop()
Definition: health_monitor_timer.h:56
int timeout() const
Definition: health_monitor_timer.h:75
bool m_started
Definition: health_monitor_timer.h:88
bool canceled() const
Definition: health_monitor_timer.h:65
void tick()
Mode Timer - Tick.
Definition: health_monitor_timer.cc:93
void set_report_interval(int interval)
Definition: health_monitor_timer.h:71
Mode_Timer()=default
bool report_status() const
Mode Timer - Report status.
Definition: health_monitor_timer.cc:104
std::string running_msg() const
Definition: health_monitor_timer.h:78
void cancel()
Mode Timer - Cancel.
Definition: health_monitor_timer.cc:53
bool running() const
Definition: health_monitor_timer.h:73
bool started() const
Definition: health_monitor_timer.h:74
int m_interval
Definition: health_monitor_timer.h:84
std::string m_name
Definition: health_monitor_timer.h:82
bool expired() const
Definition: health_monitor_timer.h:67
void set_timeout(int timeout)
Mode Timer - Set timeout.
Definition: health_monitor_timer.cc:74
std::string m_running_msg
Definition: health_monitor_timer.h:91
void set_interval(int interval)
Definition: health_monitor_timer.h:69
int report_interval() const
Definition: health_monitor_timer.h:70
bool m_running
Definition: health_monitor_timer.h:87
void start()
Mode Timer - Start.
Definition: health_monitor_timer.cc:40
void reset()
Mode Timer - Reset.
Definition: health_monitor_timer.cc:62
void set_canceled(bool canceled)
Definition: health_monitor_timer.h:66
int interval() const
Definition: health_monitor_timer.h:68
std::string format_status() const
Mode Timer - Format status.
Definition: health_monitor_timer.cc:112
void adjust_timeout()
Mode Timer - Adjust timeout.
Definition: health_monitor_timer.cc:83
void set_running_msg(const std::string &msg)
Definition: health_monitor_timer.h:79
int m_timer
Definition: health_monitor_timer.h:83
int remaining() const
Mode Timer - Remaining time.
Definition: health_monitor_timer.cc:132
std::string name() const
Definition: health_monitor_timer.h:76
Mode_Timer(std::string &name, const int interval)
Definition: health_monitor_timer.h:51
bool m_expired
Definition: health_monitor_timer.h:89
Retry timer with backoff.
Definition: health_monitor_timer.h:100
bool wait()
Retry Timer - Wait.
Definition: health_monitor_timer.cc:141
int m_retry_count
Definition: health_monitor_timer.h:115
int m_next_wait_ms
Definition: health_monitor_timer.h:117
int m_max_retries
Definition: health_monitor_timer.h:114
void reset()
Definition: health_monitor_timer.cc:156
int retry_count() const
Definition: health_monitor_timer.h:108
int m_wait_ms
Definition: health_monitor_timer.h:116
int remaining() const
Definition: health_monitor_timer.h:109
Retry_Timer(int max_retries)
Definition: health_monitor_timer.h:102
int max_retries() const
Definition: health_monitor_timer.h:106
void set_max_retries(int max_retries)
Definition: health_monitor_timer.h:107
std::string format_status() const
Retry Timer - Print status.
Definition: health_monitor_timer.cc:166
int next_wait() const
Definition: health_monitor_timer.h:110
Definition: component_command.h:29
static const int k_default_timer_interval
Definition: health_monitor_timer.h:32
static const int k_default_report_interval
Definition: health_monitor_timer.h:34
static const int k_default_timer_timeout
Definition: health_monitor_timer.h:33
case opt name
Definition: sslopt-case.h:29