forked from mariadb-corporation/MaxScale
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmainworker.hh
157 lines (132 loc) · 4.09 KB
/
mainworker.hh
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
/*
* Copyright (c) 2018 MariaDB Corporation Ab
* Copyright (c) 2023 MariaDB plc, Finnish Branch
*
* Use of this software is governed by the Business Source License included
* in the LICENSE.TXT file and at www.mariadb.com/bsl11.
*
* Change Date: 2025-09-12
*
* On the date above, in accordance with the Business Source License, use
* of this software will be governed by version 2 or later of the General
* Public License.
*/
#pragma once
#include <maxscale/ccdefs.hh>
#include <set>
#include <unordered_set>
#include <maxbase/stopwatch.hh>
#include <maxbase/watchedworker.hh>
#include <maxscale/indexedstorage.hh>
namespace maxscale
{
class MainWorker : public mxb::WatchedWorker
{
MainWorker(const MainWorker&) = delete;
MainWorker& operator=(const MainWorker&) = delete;
public:
/**
* Construct the main worker.
*
* @param pNotifier The watchdog notifier.
*
* @note There can be exactly one instance of @c MainWorker.
*/
MainWorker(mxb::WatchdogNotifier* pNotifier);
~MainWorker();
/**
* Does the main worker exist. It is only at startup and shutdown that this
* function may return false. When MaxScale is running normally, it will
* always return true.
*
* @return True, if the main worker has been created, false otherwise.
*/
static bool created();
/**
* Returns the main worker.
*
* @return The main worker.
*/
static MainWorker* get();
static int64_t ticks();
/**
* @return True, if the calling thread is the main worker.
*/
static bool is_main_worker();
/**
* @return The indexed storage of this worker.
*/
IndexedStorage& storage()
{
return m_storage;
}
const IndexedStorage& storage() const
{
return m_storage;
}
/**
* Starts the rebalancing.
*
* @note Must *only* be called from the main worker thread.
*/
void update_rebalancing();
enum BalancingApproach
{
BALANCE_UNCONDITIONALLY,
BALANCE_ACCORDING_TO_PERIOD
};
/**
* Balance worker load.
*
* @param approach Unconditionally or according to 'rebalance_period'.
* @param threshold The rebalance threshold. If -1, then the value of
* 'rebalance_threshold' will be used.
*
* @return True, if balancing actually was performed.
*/
bool balance_workers(BalancingApproach approach, int threshold = -1);
/**
* Starts the shutdown process
*/
static void start_shutdown();
const char* name() const override
{
return "MainWorker";
}
/**
* Call a function in a signal-safe manner
*
* This function can be safely called from a signal handler since it only writes the address of the
* callback function into an internal pipe that is added to epoll. This makes it possible to move the
* execution away from the signal handler where it is very hard to do pretty much anything in a safe
* manner.
*
* @param func The function that will be called by the MainWorker
*
* @return True if the execution of the function was queued successfully
*/
bool execute_signal_safe(void (* func)(void));
private:
bool pre_run() override;
void post_run() override;
struct SignalHandler final : public mxb::Pollable
{
uint32_t handle_poll_events(Worker* worker, uint32_t events, Context context) override;
int poll_fd() const override;
};
static bool inc_ticks(Callable::Action action);
bool balance_workers_dc();
void order_balancing_dc();
void read_signal_from_pipe();
void check_dependencies_dc();
// Waits until all RoutingWorkers have stopped and then stops the MainWorker
bool wait_for_shutdown();
Worker::Callable m_callable;
IndexedStorage m_storage;
mxb::Worker::DCId m_rebalancing_dc {0};
mxb::TimePoint m_last_rebalancing;
std::set<std::string> m_tunables; // Tunable parameters
int m_signal_pipe[2] {-1, -1};
SignalHandler m_signal_handler;
};
}