1 // -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*-
2 // vim: ts=8 sw=2 smarttab
4 * Ceph - scalable distributed file system
6 * Copyright (C) 2004-2006 Sage Weil <sage@newdream.net>
8 * This is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License version 2.1, as published by the Free Software
11 * Foundation. See file COPYING.
15 #include <sys/types.h>
22 #include "common/config.h"
23 #include "include/ceph_features.h"
25 #include "mon/MonMap.h"
26 #include "mon/Monitor.h"
27 #include "mon/MonitorDBStore.h"
28 #include "mon/MonClient.h"
30 #include "msg/Messenger.h"
32 #include "include/CompatSet.h"
34 #include "common/ceph_argparse.h"
35 #include "common/pick_address.h"
36 #include "common/Throttle.h"
37 #include "common/Timer.h"
38 #include "common/errno.h"
39 #include "common/Preforker.h"
41 #include "global/global_init.h"
42 #include "global/signal_handler.h"
44 #include "perfglue/heap_profiler.h"
46 #include "include/ceph_assert.h"
48 #define dout_subsys ceph_subsys_mon
54 using std::ostringstream
;
58 using ceph::bufferlist
;
61 using ceph::JSONFormatter
;
66 void handle_mon_signal(int signum
)
69 mon
->handle_signal(signum
);
73 int obtain_monmap(MonitorDBStore
&store
, bufferlist
&bl
)
75 dout(10) << __func__
<< dendl
;
77 * the monmap may be in one of three places:
78 * 'mon_sync:temp_newer_monmap' - stashed newer map for bootstrap
79 * 'monmap:<latest_version_no>' - the monmap we'd really like to have
80 * 'mon_sync:latest_monmap' - last monmap backed up for the last sync
81 * 'mkfs:monmap' - a monmap resulting from mkfs
84 if (store
.exists("monmap", "last_committed")) {
85 version_t latest_ver
= store
.get("monmap", "last_committed");
86 if (store
.exists("monmap", latest_ver
)) {
87 int err
= store
.get("monmap", latest_ver
, bl
);
88 ceph_assert(err
== 0);
89 ceph_assert(bl
.length() > 0);
90 dout(10) << __func__
<< " read last committed monmap ver "
91 << latest_ver
<< dendl
;
93 // see if there is stashed newer map (see bootstrap())
94 if (store
.exists("mon_sync", "temp_newer_monmap")) {
96 int err
= store
.get("mon_sync", "temp_newer_monmap", bl2
);
97 ceph_assert(err
== 0);
98 ceph_assert(bl2
.length() > 0);
101 if (b
.get_epoch() > latest_ver
) {
102 dout(10) << __func__
<< " using stashed monmap " << b
.get_epoch()
103 << " instead" << dendl
;
106 dout(10) << __func__
<< " ignoring stashed monmap " << b
.get_epoch()
114 if (store
.exists("mon_sync", "in_sync")
115 || store
.exists("mon_sync", "force_sync")) {
116 dout(10) << __func__
<< " detected aborted sync" << dendl
;
117 if (store
.exists("mon_sync", "latest_monmap")) {
118 int err
= store
.get("mon_sync", "latest_monmap", bl
);
119 ceph_assert(err
== 0);
120 ceph_assert(bl
.length() > 0);
121 dout(10) << __func__
<< " read backup monmap" << dendl
;
126 if (store
.exists("mon_sync", "temp_newer_monmap")) {
127 dout(10) << __func__
<< " found temp_newer_monmap" << dendl
;
128 int err
= store
.get("mon_sync", "temp_newer_monmap", bl
);
129 ceph_assert(err
== 0);
130 ceph_assert(bl
.length() > 0);
134 if (store
.exists("mkfs", "monmap")) {
135 dout(10) << __func__
<< " found mkfs monmap" << dendl
;
136 int err
= store
.get("mkfs", "monmap", bl
);
137 ceph_assert(err
== 0);
138 ceph_assert(bl
.length() > 0);
142 derr
<< __func__
<< " unable to find a monmap" << dendl
;
146 int check_mon_data_exists()
148 string mon_data
= g_conf()->mon_data
;
150 if (::stat(mon_data
.c_str(), &buf
)) {
151 if (errno
!= ENOENT
) {
152 derr
<< "stat(" << mon_data
<< ") " << cpp_strerror(errno
) << dendl
;
159 /** Check whether **mon data** is empty.
161 * Being empty means mkfs has not been run and there's no monitor setup
162 * at **g_conf()->mon_data**.
164 * If the directory g_conf()->mon_data is not empty we will return -ENOTEMPTY.
165 * Otherwise we will return 0. Any other negative returns will represent
166 * a failure to be handled by the caller.
168 * @return **0** on success, -ENOTEMPTY if not empty or **-errno** otherwise.
170 int check_mon_data_empty()
172 string mon_data
= g_conf()->mon_data
;
174 DIR *dir
= ::opendir(mon_data
.c_str());
176 derr
<< "opendir(" << mon_data
<< ") " << cpp_strerror(errno
) << dendl
;
180 struct dirent
*de
= nullptr;
182 while ((de
= ::readdir(dir
))) {
183 if (string(".") != de
->d_name
&&
184 string("..") != de
->d_name
&&
185 string("kv_backend") != de
->d_name
) {
191 derr
<< "readdir(" << mon_data
<< ") " << cpp_strerror(errno
) << dendl
;
202 cout
<< "usage: ceph-mon -i <ID> [flags]\n"
203 << " --debug_mon n\n"
204 << " debug monitor level (e.g. 10)\n"
206 << " build fresh monitor fs\n"
208 << " force a sync from another mon by wiping local data (BE CAREFUL)\n"
209 << " --yes-i-really-mean-it\n"
210 << " mandatory safeguard for --force-sync\n"
212 << " compact the monitor store\n"
213 << " --osdmap <filename>\n"
214 << " only used when --mkfs is provided: load the osdmap from <filename>\n"
215 << " --inject-monmap <filename>\n"
216 << " write the <filename> monmap to the local monitor store and exit\n"
217 << " --extract-monmap <filename>\n"
218 << " extract the monmap from the local monitor store and exit\n"
219 << " --mon-data <directory>\n"
220 << " where the mon store and keyring are located\n"
221 << " --set-crush-location <bucket>=<foo>"
222 << " sets monitor's crush bucket location (only for stretch mode)"
224 generic_server_usage();
227 entity_addrvec_t
make_mon_addrs(entity_addr_t a
)
229 entity_addrvec_t addrs
;
230 if (a
.get_port() == 0) {
231 a
.set_type(entity_addr_t::TYPE_MSGR2
);
232 a
.set_port(CEPH_MON_PORT_IANA
);
233 addrs
.v
.push_back(a
);
234 a
.set_type(entity_addr_t::TYPE_LEGACY
);
235 a
.set_port(CEPH_MON_PORT_LEGACY
);
236 addrs
.v
.push_back(a
);
237 } else if (a
.get_port() == CEPH_MON_PORT_LEGACY
) {
238 a
.set_type(entity_addr_t::TYPE_LEGACY
);
239 addrs
.v
.push_back(a
);
240 } else if (a
.get_type() == entity_addr_t::TYPE_ANY
) {
241 a
.set_type(entity_addr_t::TYPE_MSGR2
);
242 addrs
.v
.push_back(a
);
244 addrs
.v
.push_back(a
);
249 int main(int argc
, const char **argv
)
251 // reset our process name, in case we did a respawn, so that it's not
253 ceph_pthread_setname(pthread_self(), "ceph-mon");
258 bool compact
= false;
259 bool force_sync
= false;
260 bool yes_really
= false;
261 std::string osdmapfn
, inject_monmap
, extract_monmap
, crush_loc
;
263 auto args
= argv_to_vec(argc
, argv
);
265 cerr
<< argv
[0] << ": -h or --help for usage" << std::endl
;
268 if (ceph_argparse_need_usage(args
)) {
273 // We need to specify some default values that may be overridden by the
274 // user, that are specific to the monitor. The options we are overriding
275 // are also used on the OSD (or in any other component that uses leveldb),
276 // so changing the global defaults is not an option.
277 // This is not the prettiest way of doing this, especially since it has us
278 // having a different place defining default values, but it's not horribly
279 // wrong enough to prevent us from doing it :)
281 // NOTE: user-defined options will take precedence over ours.
283 // leveldb_write_buffer_size = 32*1024*1024 = 33554432 // 32MB
284 // leveldb_cache_size = 512*1024*1204 = 536870912 // 512MB
285 // leveldb_block_size = 64*1024 = 65536 // 64KB
286 // leveldb_compression = false
288 map
<string
,string
> defaults
= {
289 { "leveldb_write_buffer_size", "33554432" },
290 { "leveldb_cache_size", "536870912" },
291 { "leveldb_block_size", "65536" },
292 { "leveldb_compression", "false"},
293 { "leveldb_log", "" },
294 { "keyring", "$mon_data/keyring" },
299 vector
<const char*> args_copy
= args
;
301 for (std::vector
<const char*>::iterator i
= args_copy
.begin();
302 i
!= args_copy
.end(); ) {
303 if (ceph_argparse_double_dash(args_copy
, i
)) {
305 } else if (ceph_argparse_flag(args_copy
, i
, "--mkfs", (char*)NULL
)) {
306 flags
|= CINIT_FLAG_NO_DAEMON_ACTIONS
;
307 } else if (ceph_argparse_witharg(args_copy
, i
, &val
, "--inject_monmap", (char*)NULL
)) {
308 flags
|= CINIT_FLAG_NO_DAEMON_ACTIONS
;
309 } else if (ceph_argparse_witharg(args_copy
, i
, &val
, "--extract-monmap", (char*)NULL
)) {
310 flags
|= CINIT_FLAG_NO_DAEMON_ACTIONS
;
317 // don't try to get config from mon cluster during startup
318 flags
|= CINIT_FLAG_NO_MON_CONFIG
;
320 auto cct
= global_init(&defaults
, args
,
321 CEPH_ENTITY_TYPE_MON
, CODE_ENVIRONMENT_DAEMON
,
323 ceph_heap_profiler_init();
326 for (std::vector
<const char*>::iterator i
= args
.begin(); i
!= args
.end(); ) {
327 if (ceph_argparse_double_dash(args
, i
)) {
329 } else if (ceph_argparse_flag(args
, i
, "--mkfs", (char*)NULL
)) {
331 } else if (ceph_argparse_flag(args
, i
, "--compact", (char*)NULL
)) {
333 } else if (ceph_argparse_flag(args
, i
, "--force-sync", (char*)NULL
)) {
335 } else if (ceph_argparse_flag(args
, i
, "--yes-i-really-mean-it", (char*)NULL
)) {
337 } else if (ceph_argparse_witharg(args
, i
, &val
, "--osdmap", (char*)NULL
)) {
339 } else if (ceph_argparse_witharg(args
, i
, &val
, "--inject_monmap", (char*)NULL
)) {
341 } else if (ceph_argparse_witharg(args
, i
, &val
, "--extract-monmap", (char*)NULL
)) {
342 extract_monmap
= val
;
343 } else if (ceph_argparse_witharg(args
, i
, &val
, "--set-crush-location", (char*)NULL
)) {
350 cerr
<< "too many arguments: " << args
<< std::endl
;
354 if (force_sync
&& !yes_really
) {
355 cerr
<< "are you SURE you want to force a sync? this will erase local data and may\n"
356 << "break your mon cluster. pass --yes-i-really-mean-it if you do." << std::endl
;
360 if (g_conf()->mon_data
.empty()) {
361 cerr
<< "must specify '--mon-data=foo' data path" << std::endl
;
365 if (g_conf()->name
.get_id().empty()) {
366 cerr
<< "must specify id (--id <id> or --name mon.<id>)" << std::endl
;
373 int err
= check_mon_data_exists();
374 if (err
== -ENOENT
) {
375 if (::mkdir(g_conf()->mon_data
.c_str(), 0755)) {
376 derr
<< "mkdir(" << g_conf()->mon_data
<< ") : "
377 << cpp_strerror(errno
) << dendl
;
380 } else if (err
< 0) {
381 derr
<< "error opening '" << g_conf()->mon_data
<< "': "
382 << cpp_strerror(-err
) << dendl
;
386 err
= check_mon_data_empty();
387 if (err
== -ENOTEMPTY
) {
388 // Mon may exist. Let the user know and exit gracefully.
389 derr
<< "'" << g_conf()->mon_data
<< "' already exists and is not empty"
390 << ": monitor may already exist" << dendl
;
392 } else if (err
< 0) {
393 derr
<< "error checking if '" << g_conf()->mon_data
<< "' is empty: "
394 << cpp_strerror(-err
) << dendl
;
398 // resolve public_network -> public_addr
399 pick_addresses(g_ceph_context
, CEPH_PICK_ADDRESS_PUBLIC
);
401 dout(10) << "public_network " << g_conf()->public_network
<< dendl
;
402 dout(10) << "public_addr " << g_conf()->public_addr
<< dendl
;
403 dout(10) << "public_addrv " << g_conf()->public_addrv
<< dendl
;
405 common_init_finish(g_ceph_context
);
407 bufferlist monmapbl
, osdmapbl
;
411 // load or generate monmap
412 const auto monmap_fn
= g_conf().get_val
<string
>("monmap");
413 if (monmap_fn
.length()) {
414 int err
= monmapbl
.read_file(monmap_fn
.c_str(), &error
);
416 derr
<< argv
[0] << ": error reading " << monmap_fn
<< ": " << error
<< dendl
;
420 monmap
.decode(monmapbl
);
422 // always mark seed/mkfs monmap as epoch 0
424 } catch (const ceph::buffer::error
& e
) {
425 derr
<< argv
[0] << ": error decoding monmap " << monmap_fn
<< ": " << e
.what() << dendl
;
429 dout(1) << "imported monmap:\n";
430 monmap
.print(*_dout
);
435 int err
= monmap
.build_initial(g_ceph_context
, true, oss
);
437 derr
<< oss
.str() << dendl
;
439 derr
<< argv
[0] << ": warning: no initial monitors; must use admin socket to feed hints" << dendl
;
442 dout(1) << "initial generated monmap:\n";
443 monmap
.print(*_dout
);
446 // am i part of the initial quorum?
447 if (monmap
.contains(g_conf()->name
.get_id())) {
448 // hmm, make sure the ip listed exists on the current host?
450 } else if (!g_conf()->public_addrv
.empty()) {
451 entity_addrvec_t av
= g_conf()->public_addrv
;
453 if (monmap
.contains(av
, &name
)) {
454 monmap
.rename(name
, g_conf()->name
.get_id());
455 dout(0) << argv
[0] << ": renaming mon." << name
<< " " << av
456 << " to mon." << g_conf()->name
.get_id() << dendl
;
458 } else if (!g_conf()->public_addr
.is_blank_ip()) {
459 entity_addrvec_t av
= make_mon_addrs(g_conf()->public_addr
);
461 if (monmap
.contains(av
, &name
)) {
462 monmap
.rename(name
, g_conf()->name
.get_id());
463 dout(0) << argv
[0] << ": renaming mon." << name
<< " " << av
464 << " to mon." << g_conf()->name
.get_id() << dendl
;
467 // is a local address listed without a name? if so, name myself.
468 list
<entity_addr_t
> ls
;
469 monmap
.list_addrs(ls
);
470 dout(0) << " monmap addrs are " << ls
<< ", checking if any are local"
474 if (have_local_addr(g_ceph_context
, ls
, &local
)) {
475 dout(0) << " have local addr " << local
<< dendl
;
477 local
.set_type(entity_addr_t::TYPE_MSGR2
);
478 if (!monmap
.get_addr_name(local
, name
)) {
479 local
.set_type(entity_addr_t::TYPE_LEGACY
);
480 if (!monmap
.get_addr_name(local
, name
)) {
481 dout(0) << "no local addresses appear in bootstrap monmap"
485 if (name
.compare(0, 7, "noname-") == 0) {
486 dout(0) << argv
[0] << ": mon." << name
<< " " << local
487 << " is local, renaming to mon." << g_conf()->name
.get_id()
489 monmap
.rename(name
, g_conf()->name
.get_id());
490 } else if (name
.size()) {
491 dout(0) << argv
[0] << ": mon." << name
<< " " << local
492 << " is local, but not 'noname-' + something; "
493 << "not assuming it's me" << dendl
;
496 dout(0) << " no local addrs match monmap" << dendl
;
501 const auto fsid
= g_conf().get_val
<uuid_d
>("fsid");
502 if (!fsid
.is_zero()) {
504 dout(0) << argv
[0] << ": set fsid to " << fsid
<< dendl
;
507 if (monmap
.fsid
.is_zero()) {
508 derr
<< argv
[0] << ": generated monmap has no fsid; use '--fsid <uuid>'" << dendl
;
512 //monmap.print(cout);
515 if (osdmapfn
.length()) {
516 err
= osdmapbl
.read_file(osdmapfn
.c_str(), &error
);
518 derr
<< argv
[0] << ": error reading " << osdmapfn
<< ": "
525 MonitorDBStore
store(g_conf()->mon_data
);
527 int r
= store
.create_and_open(oss
);
529 derr
<< oss
.str() << dendl
;
531 derr
<< argv
[0] << ": error opening mon data directory at '"
532 << g_conf()->mon_data
<< "': " << cpp_strerror(r
) << dendl
;
537 Monitor
mon(g_ceph_context
, g_conf()->name
.get_id(), &store
, 0, 0, &monmap
);
538 r
= mon
.mkfs(osdmapbl
);
540 derr
<< argv
[0] << ": error creating monfs: " << cpp_strerror(r
) << dendl
;
544 dout(0) << argv
[0] << ": created monfs at " << g_conf()->mon_data
545 << " for " << g_conf()->name
<< dendl
;
549 err
= check_mon_data_exists();
550 if (err
< 0 && err
== -ENOENT
) {
551 derr
<< "monitor data directory at '" << g_conf()->mon_data
<< "'"
552 << " does not exist: have you run 'mkfs'?" << dendl
;
554 } else if (err
< 0) {
555 derr
<< "error accessing monitor data directory at '"
556 << g_conf()->mon_data
<< "': " << cpp_strerror(-err
) << dendl
;
560 err
= check_mon_data_empty();
562 derr
<< "monitor data directory at '" << g_conf()->mon_data
563 << "' is empty: have you run 'mkfs'?" << dendl
;
565 } else if (err
< 0 && err
!= -ENOTEMPTY
) {
566 // we don't want an empty data dir by now
567 derr
<< "error accessing '" << g_conf()->mon_data
<< "': "
568 << cpp_strerror(-err
) << dendl
;
573 // check fs stats. don't start if it's critically close to full.
574 ceph_data_stats_t stats
;
575 int err
= get_fs_stats(stats
, g_conf()->mon_data
.c_str());
577 derr
<< "error checking monitor data's fs stats: " << cpp_strerror(err
)
581 if (stats
.avail_percent
<= g_conf()->mon_data_avail_crit
) {
582 derr
<< "error: monitor data filesystem reached concerning levels of"
583 << " available storage space (available: "
584 << stats
.avail_percent
<< "% " << byte_u_t(stats
.byte_avail
)
585 << ")\nyou may adjust 'mon data avail crit' to a lower value"
586 << " to make this go away (default: " << g_conf()->mon_data_avail_crit
592 // we fork early to prevent leveldb's environment static state from
595 if (!(flags
& CINIT_FLAG_NO_DAEMON_ACTIONS
)) {
596 if (global_init_prefork(g_ceph_context
) >= 0) {
598 err
= prefork
.prefork(err_msg
);
600 derr
<< err_msg
<< dendl
;
603 if (prefork
.is_parent()) {
604 err
= prefork
.parent_wait(err_msg
);
606 derr
<< err_msg
<< dendl
;
610 global_init_postfork_start(g_ceph_context
);
612 common_init_finish(g_ceph_context
);
613 global_init_chdir(g_ceph_context
);
614 if (global_init_preload_erasure_code(g_ceph_context
) < 0)
618 // set up signal handlers, now that we've daemonized/forked.
619 init_async_signal_handler();
621 MonitorDBStore
*store
= new MonitorDBStore(g_conf()->mon_data
);
623 // make sure we aren't upgrading too fast
626 int r
= store
->read_meta("min_mon_release", &val
);
627 if (r
>= 0 && val
.size()) {
628 ceph_release_t from_release
= ceph_release_from_name(val
);
630 if (!can_upgrade_from(from_release
, "min_mon_release", err
)) {
631 derr
<< err
.str() << dendl
;
639 err
= store
->open(oss
);
641 derr
<< oss
.str() << dendl
;
643 derr
<< "error opening mon data directory at '"
644 << g_conf()->mon_data
<< "': " << cpp_strerror(err
) << dendl
;
650 err
= store
->get(Monitor::MONITOR_NAME
, "magic", magicbl
);
651 if (err
|| !magicbl
.length()) {
652 derr
<< "unable to read magic from mon data" << dendl
;
655 string
magic(magicbl
.c_str(), magicbl
.length()-1); // ignore trailing \n
656 if (strcmp(magic
.c_str(), CEPH_MON_ONDISK_MAGIC
)) {
657 derr
<< "mon fs magic '" << magic
<< "' != current '" << CEPH_MON_ONDISK_MAGIC
<< "'" << dendl
;
661 err
= Monitor::check_features(store
);
663 derr
<< "error checking features: " << cpp_strerror(err
) << dendl
;
667 // inject new monmap?
668 if (!inject_monmap
.empty()) {
671 int r
= bl
.read_file(inject_monmap
.c_str(), &error
);
673 derr
<< "unable to read monmap from " << inject_monmap
<< ": "
679 version_t v
= store
->get("monmap", "last_committed");
680 dout(0) << "last committed monmap epoch is " << v
<< ", injected map will be " << (v
+1)
687 if (tmp
.get_epoch() != v
) {
688 dout(0) << "changing monmap epoch from " << tmp
.get_epoch()
689 << " to " << v
<< dendl
;
693 tmp
.encode(mapbl
, CEPH_FEATURES_ALL
);
696 encode(mapbl
, final
);
698 auto t(std::make_shared
<MonitorDBStore::Transaction
>());
700 t
->put("monmap", v
, mapbl
);
701 t
->put("monmap", "latest", final
);
702 t
->put("monmap", "last_committed", v
);
703 store
->apply_transaction(t
);
705 dout(0) << "done." << dendl
;
712 // note that even if we don't find a viable monmap, we should go ahead
713 // and try to build it up in the next if-else block.
715 int err
= obtain_monmap(*store
, mapbl
);
718 monmap
.decode(mapbl
);
719 } catch (const ceph::buffer::error
& e
) {
720 derr
<< "can't decode monmap: " << e
.what() << dendl
;
723 derr
<< "unable to obtain a monmap: " << cpp_strerror(err
) << dendl
;
726 dout(10) << __func__
<< " monmap:\n";
727 JSONFormatter
jf(true);
728 jf
.dump_object("monmap", monmap
);
732 if (!extract_monmap
.empty()) {
733 int r
= mapbl
.write_file(extract_monmap
.c_str());
736 derr
<< "error writing monmap to " << extract_monmap
<< ": " << cpp_strerror(r
) << dendl
;
739 derr
<< "wrote monmap to " << extract_monmap
<< dendl
;
744 // this is what i will bind to
745 entity_addrvec_t ipaddrs
;
747 if (monmap
.contains(g_conf()->name
.get_id())) {
748 ipaddrs
= monmap
.get_addrs(g_conf()->name
.get_id());
750 // print helpful warning if the conf file doesn't match
751 std::vector
<std::string
> my_sections
= g_conf().get_my_sections();
752 std::string mon_addr_str
;
753 if (g_conf().get_val_from_conf_file(my_sections
, "mon addr",
754 mon_addr_str
, true) == 0) {
755 entity_addr_t conf_addr
;
756 if (conf_addr
.parse(mon_addr_str
)) {
757 entity_addrvec_t conf_addrs
= make_mon_addrs(conf_addr
);
758 if (ipaddrs
!= conf_addrs
) {
759 derr
<< "WARNING: 'mon addr' config option " << conf_addrs
760 << " does not match monmap file" << std::endl
761 << " continuing with monmap configuration" << dendl
;
764 derr
<< "WARNING: invalid 'mon addr' config option" << std::endl
765 << " continuing with monmap configuration" << dendl
;
768 dout(0) << g_conf()->name
<< " does not exist in monmap, will attempt to join an existing cluster" << dendl
;
770 pick_addresses(g_ceph_context
, CEPH_PICK_ADDRESS_PUBLIC
);
771 if (!g_conf()->public_addrv
.empty()) {
772 ipaddrs
= g_conf()->public_addrv
;
773 dout(0) << "using public_addrv " << ipaddrs
<< dendl
;
774 } else if (!g_conf()->public_addr
.is_blank_ip()) {
775 ipaddrs
= make_mon_addrs(g_conf()->public_addr
);
776 dout(0) << "using public_addr " << g_conf()->public_addr
<< " -> "
781 int err
= tmpmap
.build_initial(g_ceph_context
, true, oss
);
783 derr
<< oss
.str() << dendl
;
785 derr
<< argv
[0] << ": error generating initial monmap: "
786 << cpp_strerror(err
) << dendl
;
789 if (tmpmap
.contains(g_conf()->name
.get_id())) {
790 ipaddrs
= tmpmap
.get_addrs(g_conf()->name
.get_id());
792 derr
<< "no public_addr or public_network specified, and "
793 << g_conf()->name
<< " not present in monmap or ceph.conf" << dendl
;
800 int rank
= monmap
.get_rank(g_conf()->name
.get_id());
801 std::string public_msgr_type
= g_conf()->ms_public_type
.empty() ? g_conf().get_val
<std::string
>("ms_type") : g_conf()->ms_public_type
;
802 Messenger
*msgr
= Messenger::create(g_ceph_context
, public_msgr_type
,
803 entity_name_t::MON(rank
), "mon", 0);
806 msgr
->set_cluster_protocol(CEPH_MON_PROTOCOL
);
807 msgr
->set_default_send_priority(CEPH_MSG_PRIO_HIGH
);
809 msgr
->set_default_policy(Messenger::Policy::stateless_server(0));
810 msgr
->set_policy(entity_name_t::TYPE_MON
,
811 Messenger::Policy::lossless_peer_reuse(
812 CEPH_FEATURE_SERVER_LUMINOUS
));
813 msgr
->set_policy(entity_name_t::TYPE_OSD
,
814 Messenger::Policy::stateless_server(
815 CEPH_FEATURE_SERVER_LUMINOUS
));
816 msgr
->set_policy(entity_name_t::TYPE_CLIENT
,
817 Messenger::Policy::stateless_server(0));
818 msgr
->set_policy(entity_name_t::TYPE_MDS
,
819 Messenger::Policy::stateless_server(0));
821 // throttle client traffic
822 Throttle
*client_throttler
= new Throttle(g_ceph_context
, "mon_client_bytes",
823 g_conf()->mon_client_bytes
);
824 msgr
->set_policy_throttlers(entity_name_t::TYPE_CLIENT
,
825 client_throttler
, NULL
);
827 // throttle daemon traffic
828 // NOTE: actual usage on the leader may multiply by the number of
829 // monitors if they forward large update messages from daemons.
830 Throttle
*daemon_throttler
= new Throttle(g_ceph_context
, "mon_daemon_bytes",
831 g_conf()->mon_daemon_bytes
);
832 msgr
->set_policy_throttlers(entity_name_t::TYPE_OSD
, daemon_throttler
,
834 msgr
->set_policy_throttlers(entity_name_t::TYPE_MDS
, daemon_throttler
,
837 entity_addrvec_t bind_addrs
= ipaddrs
;
838 entity_addrvec_t public_addrs
= ipaddrs
;
840 // check if the public_bind_addr option is set
841 if (!g_conf()->public_bind_addr
.is_blank_ip()) {
842 bind_addrs
= make_mon_addrs(g_conf()->public_bind_addr
);
845 dout(0) << "starting " << g_conf()->name
<< " rank " << rank
846 << " at public addrs " << public_addrs
847 << " at bind addrs " << bind_addrs
848 << " mon_data " << g_conf()->mon_data
849 << " fsid " << monmap
.get_fsid()
852 Messenger
*mgr_msgr
= Messenger::create(g_ceph_context
, public_msgr_type
,
853 entity_name_t::MON(rank
), "mon-mgrc",
854 Messenger::get_pid_nonce());
856 derr
<< "unable to create mgr_msgr" << dendl
;
860 mon
= new Monitor(g_ceph_context
, g_conf()->name
.get_id(), store
,
861 msgr
, mgr_msgr
, &monmap
);
863 mon
->orig_argc
= argc
;
864 mon
->orig_argv
= argv
;
867 derr
<< "flagging a forced sync ..." << dendl
;
869 JSONFormatter
jf(true);
870 mon
->sync_force(&jf
);
876 err
= mon
->preinit();
878 derr
<< "failed to initialize" << dendl
;
882 if (compact
|| g_conf()->mon_compact_on_start
) {
883 derr
<< "compacting monitor store ..." << dendl
;
884 mon
->store
->compact();
885 derr
<< "done compacting" << dendl
;
889 err
= msgr
->bindv(bind_addrs
);
891 derr
<< "unable to bind monitor to " << bind_addrs
<< dendl
;
895 // if the public and bind addr are different set the msgr addr
896 // to the public one, now that the bind is complete.
897 if (public_addrs
!= bind_addrs
) {
898 msgr
->set_addrs(public_addrs
);
901 if (g_conf()->daemonize
) {
902 global_init_postfork_finish(g_ceph_context
);
909 mon
->set_mon_crush_location(crush_loc
);
912 register_async_signal_handler_oneshot(SIGINT
, handle_mon_signal
);
913 register_async_signal_handler_oneshot(SIGTERM
, handle_mon_signal
);
914 register_async_signal_handler(SIGHUP
, handle_mon_signal
);
916 if (g_conf()->inject_early_sigterm
)
917 kill(getpid(), SIGTERM
);
924 unregister_async_signal_handler(SIGHUP
, handle_mon_signal
);
925 unregister_async_signal_handler(SIGINT
, handle_mon_signal
);
926 unregister_async_signal_handler(SIGTERM
, handle_mon_signal
);
927 shutdown_async_signal_handler();
933 delete client_throttler
;
934 delete daemon_throttler
;
936 // cd on exit, so that gmon.out (if any) goes into a separate directory for each node.
938 snprintf(s
, sizeof(s
), "gmon/%d", getpid());
939 if ((mkdir(s
, 0755) == 0) && (chdir(s
) == 0)) {
940 dout(0) << "ceph-mon: gmon.out should be in " << s
<< dendl
;
943 prefork
.signal_exit(0);