MySQL 8.3.0
Source Code Documentation
process_state_component.h
Go to the documentation of this file.
1/*
2 Copyright (c) 2022, 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_PROCESS_STATE_COMPONENT_INCLUDED
26#define MYSQL_HARNESS_PROCESS_STATE_COMPONENT_INCLUDED
27
28#include <functional>
29#include <string>
30#include <vector>
31
32#include "harness_export.h"
34
35namespace mysql_harness {
36// set when the Router receives a signal to shut down or some fatal error
37// condition occurred
38class HARNESS_EXPORT ShutdownPending {
39 public:
40 /**
41 * Reason for shutdown.
42 */
43 enum class Reason { NONE, REQUESTED, FATAL_ERROR };
44
45 [[nodiscard]] std::string message() const { return message_; }
46 void message(const std::string &msg) { message_ = msg; }
47
48 [[nodiscard]] Reason reason() const { return reason_; }
49 void reason(Reason r) { reason_ = r; }
50
51 private:
53 std::string message_;
54};
55
56std::string HARNESS_EXPORT to_string(const ShutdownPending::Reason &reason);
57
58/**
59 * manages the state of the process.
60 *
61 * allows to shutdown the current process in a
62 *
63 * - thread safe
64 * - waitable
65 *
66 * way.
67 *
68 * To be used
69 *
70 * - by the signal-handler to mark the process for shutdown,
71 * - and by the Loader to wait for a shutdown to happen.
72 */
73class HARNESS_EXPORT ProcessStateComponent {
74 public:
75 static ProcessStateComponent &get_instance();
76
77 using key_type = std::string;
78
79 // disable copy, as we are a single-instance
81 void operator=(ProcessStateComponent const &) = delete;
82
83 // no move either
86
87 void clear();
88
89 /**
90 * request application shutdown.
91 *
92 * @param reason reason for the shutdown
93 * @param msg human readable reason of the shutdown
94 * @throws std::system_error same as std::unique_lock::lock does
95 */
96 void request_application_shutdown(
98 const std::string &msg = {});
99
100 using on_shutdown_request_callback = std::function<void(
101 ShutdownPending::Reason reason, const std::string &msg)>;
102 /**
103 * register a callback that will get exectued whenever the component gets the
104 * shutdown request
105 *
106 * @param callback callback to execute when the shutdown is requested
107 */
110 on_shutdown_request_callbacks_.push_back(callback);
111 }
112
113 /**
114 * pending shutdown state.
115 *
116 * - synchronized
117 * - waitable
118 */
120 return shutdown_pending_;
121 }
122
123 private:
125
126 WaitableMonitor<ShutdownPending> shutdown_pending_{{}};
127
128 std::vector<on_shutdown_request_callback> on_shutdown_request_callbacks_;
129};
130
131} // namespace mysql_harness
132
133#endif
Monitor can be waited for.
Definition: monitor.h:61
manages the state of the process.
Definition: process_state_component.h:73
void register_on_shutdown_request_callback(on_shutdown_request_callback callback)
register a callback that will get exectued whenever the component gets the shutdown request
Definition: process_state_component.h:108
std::vector< on_shutdown_request_callback > on_shutdown_request_callbacks_
Definition: process_state_component.h:128
void operator=(ProcessStateComponent &&)=delete
WaitableMonitor< ShutdownPending > & shutdown_pending()
pending shutdown state.
Definition: process_state_component.h:119
ProcessStateComponent(ProcessStateComponent const &)=delete
std::function< void(ShutdownPending::Reason reason, const std::string &msg)> on_shutdown_request_callback
Definition: process_state_component.h:101
ProcessStateComponent(ProcessStateComponent &&)=delete
void operator=(ProcessStateComponent const &)=delete
std::string key_type
Definition: process_state_component.h:77
Definition: process_state_component.h:38
void message(const std::string &msg)
Definition: process_state_component.h:46
Reason
Reason for shutdown.
Definition: process_state_component.h:43
std::string message_
Definition: process_state_component.h:53
std::string message() const
Definition: process_state_component.h:45
void reason(Reason r)
Definition: process_state_component.h:49
Reason reason() const
Definition: process_state_component.h:48
@ NONE
Definition: base.h:44
Definition: common.h:41
std::string HARNESS_EXPORT to_string(const ShutdownPending::Reason &reason)
Definition: process_state_component.cc:59
const mysql_service_registry_t * r
Definition: pfs_example_plugin_employee.cc:85