MySQL 8.0.39
Source Code Documentation
log_reopen.h
Go to the documentation of this file.
1/*
2 Copyright (c) 2015, 2024, Oracle and/or its affiliates.
3
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License, version 2.0,
6 as published by the Free Software Foundation.
7
8 This program is designed to work with certain software (including
9 but not limited to OpenSSL) that is licensed under separate terms,
10 as designated in a particular file or component or in included license
11 documentation. The authors of MySQL hereby grant you an additional
12 permission to link the program and your derivative works with the
13 separately licensed software that they have either included with
14 the program or referenced in the documentation.
15
16 This program is distributed in the hope that it will be useful,
17 but WITHOUT ANY WARRANTY; without even the implied warranty of
18 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 GNU General Public License for more details.
20
21 You should have received a copy of the GNU General Public License
22 along with this program; if not, write to the Free Software
23 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
24*/
25
26#ifndef MYSQL_HARNESS_LOG_REOPEN_INCLUDED
27#define MYSQL_HARNESS_LOG_REOPEN_INCLUDED
28
29#include <functional>
30#include <string>
31#include <thread>
32
33#include "harness_export.h"
35
36namespace mysql_harness {
37
38class HARNESS_EXPORT LogReopen {
39 public:
40 using reopen_callback = std::function<void(const std::string &)>;
41
43 public:
44 /**
45 * request reopen
46 *
47 * @note Empty dst will cause reopen only, and the old content will not be
48 * moved to dst.
49 * @note This method uses mutex::try_lock() to avoid blocking the interrupt
50 * handler if a signal is received during an already ongoing concurrent
51 * reopen. The consequence is that reopen requests are ignored if rotation
52 * is already in progress.
53 *
54 * @param dst filename to use for old log file during reopen
55 * @throws std::system_error same as std::unique_lock::lock does
56 */
57 void request_reopen(const std::string &dst = "");
58
59 /* Log reopen state triplet */
60 enum class State { NONE, REQUESTED, ACTIVE, SHUTDOWN };
61
62 /* Check log reopen completed */
63 bool is_completed() const { return state_ == State::NONE; }
64
65 /* Check log reopen requested */
66 bool is_requested() const { return state_ == State::REQUESTED; }
67
68 /* Check log reopen active */
69 bool is_active() const { return state_ == State::ACTIVE; }
70
71 bool is_shutdown() const { return state_ == State::SHUTDOWN; }
72
73 /* Retrieve error from the last reopen */
74 std::string errmsg() const { return errmsg_; }
75 void errmsg(const std::string &errmsg) { errmsg_ = errmsg; }
76
77 void destination(const std::string &dst) { dst_ = dst; }
78 std::string destination() const { return dst_; }
79
80 void state(State st) { state_ = st; }
81 State state() const { return state_; }
82
83 private:
84 /* The log reopen thread state */
86
87 /* The last error message from the log reopen thread */
88 std::string errmsg_;
89
90 /* The destination filename to use for the old logfile during reopen */
91 std::string dst_;
92 }; // class LogReopenThread
93
94 LogReopen() { reopen_thr_ = std::thread{&LogReopen::main_loop, this}; }
95
96 /**
97 * destruct the thread.
98 *
99 * Same as std::thread it may call std::terminate in case the thread isn't
100 * joined yet, but joinable.
101 *
102 * In case join() fails as best-effort, a log-message is attempted to be
103 * written.
104 */
105 ~LogReopen();
106
107 /**
108 * notify a "log_reopen" is requested with optional filename for old logfile.
109 *
110 * @param dst rename old logfile to filename before reopen
111 * @throws std::system_error same as std::unique_lock::lock does
112 */
113 void request_reopen(const std::string &dst = "");
114
115 /**
116 * check reopen completed
117 */
118 bool completed() const;
119
120 /**
121 * get last log reopen error
122 */
123 std::string get_last_error() const;
124
125 /**
126 * Setter for the log reopen thread completion callback function.
127 *
128 * @param cb Function to call at completion.
129 */
130 void set_complete_callback(reopen_callback cb);
131
132 private:
133 static void main_loop(LogReopen *self); // thread.
134
135 friend void main_loop(LogReopen *self);
136
137 /**
138 * stop the log_reopen_thread_function.
139 */
140 void stop();
141
142 /**
143 * join the log_reopen thread.
144 *
145 * @throws std::system_error same as std::thread::join
146 */
147 void join();
148
149 /* The thread handle */
150 std::thread reopen_thr_;
151
152 Monitor<reopen_callback> complete_callback_{{}};
153
155};
156
157} // namespace mysql_harness
158
159#endif
Monitor pattern.
Definition: monitor.h:39
Monitor can be waited for.
Definition: monitor.h:62
Definition: log_reopen.h:42
State
Definition: log_reopen.h:60
bool is_completed() const
Definition: log_reopen.h:63
std::string errmsg_
Definition: log_reopen.h:88
bool is_active() const
Definition: log_reopen.h:69
void state(State st)
Definition: log_reopen.h:80
State state() const
Definition: log_reopen.h:81
void destination(const std::string &dst)
Definition: log_reopen.h:77
bool is_shutdown() const
Definition: log_reopen.h:71
std::string dst_
Definition: log_reopen.h:91
void errmsg(const std::string &errmsg)
Definition: log_reopen.h:75
std::string errmsg() const
Definition: log_reopen.h:74
bool is_requested() const
Definition: log_reopen.h:66
std::string destination() const
Definition: log_reopen.h:78
void request_reopen(const std::string &dst="")
request reopen
Definition: log_reopen.h:38
LogReopen()
Definition: log_reopen.h:94
std::thread reopen_thr_
Definition: log_reopen.h:150
std::function< void(const std::string &)> reopen_callback
Definition: log_reopen.h:40
static void main_loop(LogReopen *self)
Definition: log_reopen.cc:113
friend void main_loop(LogReopen *self)
Definition: common.h:42
std::string join(Container cont, const std::string &delim)
join elements of an container into a string separated by a delimiter.
Definition: string.h:151
@ SHUTDOWN
Definition: sql_yacc.h:551