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