-
Notifications
You must be signed in to change notification settings - Fork 7
/
MgrStandby.cc
384 lines (327 loc) · 9.98 KB
/
MgrStandby.cc
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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
// -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*-
// vim: ts=8 sw=2 smarttab
/*
* Ceph - scalable distributed file system
*
* Copyright (C) 2016 John Spray <john.spray@redhat.com>
*
* This is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License version 2.1, as published by the Free Software
* Foundation. See file COPYING.
*/
#include <Python.h>
#include "common/errno.h"
#include "common/signal.h"
#include "include/compat.h"
#include "include/stringify.h"
#include "global/global_context.h"
#include "global/signal_handler.h"
#include "mgr/MgrContext.h"
#include "messages/MMgrBeacon.h"
#include "messages/MMgrMap.h"
#include "Mgr.h"
#include "MgrStandby.h"
#define dout_context g_ceph_context
#define dout_subsys ceph_subsys_mgr
#undef dout_prefix
#define dout_prefix *_dout << "mgr " << __func__ << " "
MgrStandby::MgrStandby(int argc, const char **argv) :
Dispatcher(g_ceph_context),
monc{g_ceph_context},
client_messenger(Messenger::create_client_messenger(g_ceph_context, "mgr")),
objecter{g_ceph_context, client_messenger.get(), &monc, NULL, 0, 0},
client{client_messenger.get(), &monc, &objecter},
log_client(g_ceph_context, client_messenger.get(), &monc.monmap, LogClient::NO_FLAGS),
clog(log_client.create_channel(CLOG_CHANNEL_CLUSTER)),
audit_clog(log_client.create_channel(CLOG_CHANNEL_AUDIT)),
lock("MgrStandby::lock"),
timer(g_ceph_context, lock),
active_mgr(nullptr),
orig_argc(argc),
orig_argv(argv)
{
}
MgrStandby::~MgrStandby() = default;
const char** MgrStandby::get_tracked_conf_keys() const
{
static const char* KEYS[] = {
// clog & admin clog
"clog_to_monitors",
"clog_to_syslog",
"clog_to_syslog_facility",
"clog_to_syslog_level",
"osd_objectstore_fuse",
"clog_to_graylog",
"clog_to_graylog_host",
"clog_to_graylog_port",
"host",
"fsid",
NULL
};
return KEYS;
}
void MgrStandby::handle_conf_change(
const struct md_config_t *conf,
const std::set <std::string> &changed)
{
if (changed.count("clog_to_monitors") ||
changed.count("clog_to_syslog") ||
changed.count("clog_to_syslog_level") ||
changed.count("clog_to_syslog_facility") ||
changed.count("clog_to_graylog") ||
changed.count("clog_to_graylog_host") ||
changed.count("clog_to_graylog_port") ||
changed.count("host") ||
changed.count("fsid")) {
_update_log_config();
}
}
int MgrStandby::init()
{
Mutex::Locker l(lock);
// Initialize Messenger
client_messenger->add_dispatcher_tail(this);
client_messenger->add_dispatcher_head(&objecter);
client_messenger->add_dispatcher_tail(&client);
client_messenger->start();
// Initialize MonClient
if (monc.build_initial_monmap() < 0) {
client_messenger->shutdown();
client_messenger->wait();
return -1;
}
monc.sub_want("mgrmap", 0, 0);
monc.set_want_keys(CEPH_ENTITY_TYPE_MON|CEPH_ENTITY_TYPE_OSD
|CEPH_ENTITY_TYPE_MDS|CEPH_ENTITY_TYPE_MGR);
monc.set_messenger(client_messenger.get());
int r = monc.init();
if (r < 0) {
monc.shutdown();
client_messenger->shutdown();
client_messenger->wait();
return r;
}
r = monc.authenticate();
if (r < 0) {
derr << "Authentication failed, did you specify a mgr ID with a valid keyring?" << dendl;
monc.shutdown();
client_messenger->shutdown();
client_messenger->wait();
return r;
}
client_t whoami = monc.get_global_id();
client_messenger->set_myname(entity_name_t::CLIENT(whoami.v));
monc.set_log_client(&log_client);
_update_log_config();
objecter.set_client_incarnation(0);
objecter.init();
objecter.start();
client.init();
timer.init();
tick();
dout(4) << "Complete." << dendl;
return 0;
}
void MgrStandby::send_beacon()
{
assert(lock.is_locked_by_me());
dout(1) << state_str() << dendl;
dout(10) << "sending beacon as gid " << monc.get_global_id() << dendl;
bool available = active_mgr != nullptr && active_mgr->is_initialized();
auto addr = available ? active_mgr->get_server_addr() : entity_addr_t();
MMgrBeacon *m = new MMgrBeacon(monc.get_fsid(),
monc.get_global_id(),
g_conf->name.get_id(),
addr,
available);
monc.send_mon_message(m);
}
void MgrStandby::tick()
{
dout(0) << __func__ << dendl;
send_beacon();
if (active_mgr) {
active_mgr->tick();
}
timer.add_event_after(g_conf->mgr_tick_period, new FunctionContext(
[this](int r){
tick();
}
));
}
void MgrStandby::handle_signal(int signum)
{
Mutex::Locker l(lock);
assert(signum == SIGINT || signum == SIGTERM);
derr << "*** Got signal " << sig_str(signum) << " ***" << dendl;
shutdown();
}
void MgrStandby::shutdown()
{
// Expect already to be locked as we're called from signal handler
assert(lock.is_locked_by_me());
// stop sending beacon first, i use monc to talk with monitors
timer.shutdown();
// client uses monc and objecter
client.shutdown();
// stop monc, so mon won't be able to instruct me to shutdown/activate after
// the active_mgr is stopped
monc.shutdown();
if (active_mgr) {
active_mgr->shutdown();
}
// objecter is used by monc and active_mgr
objecter.shutdown();
// client_messenger is used by all of them, so stop it in the end
client_messenger->shutdown();
}
void MgrStandby::respawn()
{
char *new_argv[orig_argc+1];
dout(1) << " e: '" << orig_argv[0] << "'" << dendl;
for (int i=0; i<orig_argc; i++) {
new_argv[i] = (char *)orig_argv[i];
dout(1) << " " << i << ": '" << orig_argv[i] << "'" << dendl;
}
new_argv[orig_argc] = NULL;
/* Determine the path to our executable, test if Linux /proc/self/exe exists.
* This allows us to exec the same executable even if it has since been
* unlinked.
*/
char exe_path[PATH_MAX] = "";
if (readlink(PROCPREFIX "/proc/self/exe", exe_path, PATH_MAX-1) == -1) {
/* Print CWD for the user's interest */
char buf[PATH_MAX];
char *cwd = getcwd(buf, sizeof(buf));
assert(cwd);
dout(1) << " cwd " << cwd << dendl;
/* Fall back to a best-effort: just running in our CWD */
strncpy(exe_path, orig_argv[0], PATH_MAX-1);
} else {
dout(1) << "respawning with exe " << exe_path << dendl;
strcpy(exe_path, PROCPREFIX "/proc/self/exe");
}
dout(1) << " exe_path " << exe_path << dendl;
unblock_all_signals(NULL);
execv(exe_path, new_argv);
derr << "respawn execv " << orig_argv[0]
<< " failed with " << cpp_strerror(errno) << dendl;
ceph_abort();
}
void MgrStandby::_update_log_config()
{
map<string,string> log_to_monitors;
map<string,string> log_to_syslog;
map<string,string> log_channel;
map<string,string> log_prio;
map<string,string> log_to_graylog;
map<string,string> log_to_graylog_host;
map<string,string> log_to_graylog_port;
uuid_d fsid;
string host;
if (parse_log_client_options(cct, log_to_monitors, log_to_syslog,
log_channel, log_prio, log_to_graylog,
log_to_graylog_host, log_to_graylog_port,
fsid, host) == 0) {
clog->update_config(log_to_monitors, log_to_syslog,
log_channel, log_prio, log_to_graylog,
log_to_graylog_host, log_to_graylog_port,
fsid, host);
audit_clog->update_config(log_to_monitors, log_to_syslog,
log_channel, log_prio, log_to_graylog,
log_to_graylog_host, log_to_graylog_port,
fsid, host);
}
}
void MgrStandby::handle_mgr_map(MMgrMap* mmap)
{
auto map = mmap->get_map();
dout(4) << "received map epoch " << map.get_epoch() << dendl;
const bool active_in_map = map.active_gid == monc.get_global_id();
dout(4) << "active in map: " << active_in_map
<< " active is " << map.active_gid << dendl;
if (active_in_map) {
if (!active_mgr) {
dout(1) << "Activating!" << dendl;
active_mgr.reset(new Mgr(&monc, client_messenger.get(), &objecter,
&client, clog, audit_clog));
active_mgr->background_init();
dout(1) << "I am now active" << dendl;
} else {
dout(10) << "I was already active" << dendl;
}
} else {
if (active_mgr != nullptr) {
derr << "I was active but no longer am" << dendl;
respawn();
}
}
mmap->put();
}
bool MgrStandby::ms_dispatch(Message *m)
{
Mutex::Locker l(lock);
dout(4) << state_str() << " " << *m << dendl;
switch (m->get_type()) {
case MSG_MGR_MAP:
handle_mgr_map(static_cast<MMgrMap*>(m));
break;
default:
if (active_mgr) {
auto am = active_mgr;
lock.Unlock();
am->ms_dispatch(m);
lock.Lock();
} else {
return false;
}
}
return true;
}
bool MgrStandby::ms_get_authorizer(int dest_type, AuthAuthorizer **authorizer,
bool force_new)
{
if (dest_type == CEPH_ENTITY_TYPE_MON)
return true;
if (force_new) {
if (monc.wait_auth_rotating(10) < 0)
return false;
}
*authorizer = monc.build_authorizer(dest_type);
return *authorizer != NULL;
}
bool MgrStandby::ms_handle_refused(Connection *con)
{
// do nothing for now
return false;
}
// A reference for use by the signal handler
static MgrStandby *signal_mgr = nullptr;
static void handle_mgr_signal(int signum)
{
if (signal_mgr) {
signal_mgr->handle_signal(signum);
}
}
int MgrStandby::main(vector<const char *> args)
{
// Enable signal handlers
signal_mgr = this;
init_async_signal_handler();
register_async_signal_handler(SIGHUP, sighup_handler);
register_async_signal_handler_oneshot(SIGINT, handle_mgr_signal);
register_async_signal_handler_oneshot(SIGTERM, handle_mgr_signal);
client_messenger->wait();
// Disable signal handlers
unregister_async_signal_handler(SIGHUP, sighup_handler);
unregister_async_signal_handler(SIGINT, handle_mgr_signal);
unregister_async_signal_handler(SIGTERM, handle_mgr_signal);
shutdown_async_signal_handler();
signal_mgr = nullptr;
return 0;
}
std::string MgrStandby::state_str()
{
return active_mgr == nullptr ? "standby" : "active";
}