]> git.proxmox.com Git - ceph.git/blame - ceph/src/ceph_mds.cc
import ceph 16.2.6
[ceph.git] / ceph / src / ceph_mds.cc
CommitLineData
7c673cae
FG
1// -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*-
2// vim: ts=8 sw=2 smarttab
3/*
4 * Ceph - scalable distributed file system
5 *
6 * Copyright (C) 2004-2006 Sage Weil <sage@newdream.net>
7 *
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.
12 *
13 */
14
15#include <sys/types.h>
16#include <sys/stat.h>
17#include <fcntl.h>
18#include <pthread.h>
19
20#include <iostream>
21#include <string>
7c673cae 22
f67539c2 23#include "common/async/context_pool.h"
7c673cae
FG
24#include "include/ceph_features.h"
25#include "include/compat.h"
11fdf7f2 26#include "include/random.h"
7c673cae
FG
27
28#include "common/config.h"
29#include "common/strtol.h"
9f95a23c 30#include "common/numa.h"
7c673cae
FG
31
32#include "mon/MonMap.h"
33#include "mds/MDSDaemon.h"
34
35#include "msg/Messenger.h"
36
37#include "common/Timer.h"
38#include "common/ceph_argparse.h"
39#include "common/pick_address.h"
11fdf7f2 40#include "common/Preforker.h"
7c673cae
FG
41
42#include "global/global_init.h"
43#include "global/signal_handler.h"
44#include "global/pidfile.h"
45
46#include "mon/MonClient.h"
47
48#include "auth/KeyRing.h"
49
50#include "perfglue/heap_profiler.h"
51
11fdf7f2 52#include "include/ceph_assert.h"
7c673cae
FG
53
54#define dout_context g_ceph_context
55#define dout_subsys ceph_subsys_mds
56
57static void usage()
58{
11fdf7f2 59 cout << "usage: ceph-mds -i <ID> [flags]\n"
7c673cae
FG
60 << " -m monitorip:port\n"
61 << " connect to monitor at given address\n"
62 << " --debug_mds n\n"
63 << " debug MDS level (e.g. 10)\n"
7c673cae
FG
64 << std::endl;
65 generic_server_usage();
66}
67
68
7c673cae
FG
69MDSDaemon *mds = NULL;
70
71
72static void handle_mds_signal(int signum)
73{
74 if (mds)
75 mds->handle_signal(signum);
76}
77
7c673cae 78int main(int argc, const char **argv)
7c673cae
FG
79{
80 ceph_pthread_setname(pthread_self(), "ceph-mds");
81
82 vector<const char*> args;
83 argv_to_vec(argc, argv, args);
11fdf7f2
TL
84 if (args.empty()) {
85 cerr << argv[0] << ": -h or --help for usage" << std::endl;
86 exit(1);
87 }
88 if (ceph_argparse_need_usage(args)) {
89 usage();
90 exit(0);
91 }
7c673cae
FG
92
93 auto cct = global_init(NULL, args,
f67539c2 94 CEPH_ENTITY_TYPE_MDS, CODE_ENVIRONMENT_DAEMON, 0);
7c673cae
FG
95 ceph_heap_profiler_init();
96
9f95a23c
TL
97 int numa_node = g_conf().get_val<int64_t>("mds_numa_node");
98 size_t numa_cpu_set_size = 0;
99 cpu_set_t numa_cpu_set;
100 if (numa_node >= 0) {
101 int r = get_numa_node_cpu_set(numa_node, &numa_cpu_set_size, &numa_cpu_set);
102 if (r < 0) {
103 dout(1) << __func__ << " unable to determine mds numa node " << numa_node
104 << " CPUs" << dendl;
105 numa_node = -1;
106 } else {
107 r = set_cpu_affinity_all_threads(numa_cpu_set_size, &numa_cpu_set);
108 if (r < 0) {
109 derr << __func__ << " failed to set numa affinity: " << cpp_strerror(r)
110 << dendl;
111 }
112 }
113 } else {
114 dout(1) << __func__ << " not setting numa affinity" << dendl;
115 }
7c673cae
FG
116 std::string val, action;
117 for (std::vector<const char*>::iterator i = args.begin(); i != args.end(); ) {
118 if (ceph_argparse_double_dash(args, i)) {
119 break;
120 }
7c673cae 121 else if (ceph_argparse_witharg(args, i, &val, "--hot-standby", (char*)NULL)) {
11fdf7f2 122 dout(0) << "--hot-standby is obsolete and has no effect" << dendl;
7c673cae
FG
123 }
124 else {
125 derr << "Error: can't understand argument: " << *i << "\n" << dendl;
11fdf7f2 126 exit(1);
7c673cae
FG
127 }
128 }
129
11fdf7f2
TL
130 Preforker forker;
131
132 entity_addrvec_t addrs;
133 pick_addresses(g_ceph_context, CEPH_PICK_ADDRESS_PUBLIC, &addrs);
7c673cae
FG
134
135 // Normal startup
11fdf7f2 136 if (g_conf()->name.has_default_id()) {
7c673cae 137 derr << "must specify '-i name' with the ceph-mds instance name" << dendl;
11fdf7f2 138 exit(1);
7c673cae
FG
139 }
140
11fdf7f2
TL
141 if (g_conf()->name.get_id().empty() ||
142 (g_conf()->name.get_id()[0] >= '0' && g_conf()->name.get_id()[0] <= '9')) {
143 derr << "MDS id '" << g_conf()->name << "' is invalid. "
7c673cae 144 "MDS names may not start with a numeric digit." << dendl;
11fdf7f2
TL
145 exit(1);
146 }
147
148 if (global_init_prefork(g_ceph_context) >= 0) {
149 std::string err;
150 int r = forker.prefork(err);
151 if (r < 0) {
152 cerr << err << std::endl;
153 return r;
154 }
155 if (forker.is_parent()) {
156 if (forker.parent_wait(err) != 0) {
157 return -ENXIO;
158 }
159 return 0;
160 }
161 global_init_postfork_start(g_ceph_context);
7c673cae 162 }
11fdf7f2
TL
163 common_init_finish(g_ceph_context);
164 global_init_chdir(g_ceph_context);
7c673cae 165
11fdf7f2 166 std::string public_msgr_type = g_conf()->ms_public_type.empty() ? g_conf().get_val<std::string>("ms_type") : g_conf()->ms_public_type;
7c673cae
FG
167 Messenger *msgr = Messenger::create(g_ceph_context, public_msgr_type,
168 entity_name_t::MDS(-1), "mds",
f67539c2 169 Messenger::get_random_nonce());
7c673cae 170 if (!msgr)
11fdf7f2 171 forker.exit(1);
7c673cae
FG
172 msgr->set_cluster_protocol(CEPH_MDS_PROTOCOL);
173
11fdf7f2 174 cout << "starting " << g_conf()->name << " at " << msgr->get_myaddrs()
7c673cae
FG
175 << std::endl;
176 uint64_t required =
177 CEPH_FEATURE_OSDREPLYMUX;
178
179 msgr->set_default_policy(Messenger::Policy::lossy_client(required));
180 msgr->set_policy(entity_name_t::TYPE_MON,
181 Messenger::Policy::lossy_client(CEPH_FEATURE_UID |
182 CEPH_FEATURE_PGID64));
183 msgr->set_policy(entity_name_t::TYPE_MDS,
184 Messenger::Policy::lossless_peer(CEPH_FEATURE_UID));
185 msgr->set_policy(entity_name_t::TYPE_CLIENT,
186 Messenger::Policy::stateful_server(0));
187
11fdf7f2 188 int r = msgr->bindv(addrs);
7c673cae 189 if (r < 0)
11fdf7f2 190 forker.exit(1);
7c673cae 191
11fdf7f2
TL
192 // set up signal handlers, now that we've daemonized/forked.
193 init_async_signal_handler();
194 register_async_signal_handler(SIGHUP, sighup_handler);
195
7c673cae 196 // get monmap
f67539c2
TL
197 ceph::async::io_context_pool ctxpool(2);
198 MonClient mc(g_ceph_context, ctxpool);
7c673cae 199 if (mc.build_initial_monmap() < 0)
11fdf7f2 200 forker.exit(1);
7c673cae
FG
201 global_init_chdir(g_ceph_context);
202
203 msgr->start();
204
205 // start mds
f67539c2 206 mds = new MDSDaemon(g_conf()->name.get_id().c_str(), msgr, &mc, ctxpool);
7c673cae
FG
207
208 // in case we have to respawn...
209 mds->orig_argc = argc;
210 mds->orig_argv = argv;
211
11fdf7f2
TL
212 if (g_conf()->daemonize) {
213 global_init_postfork_finish(g_ceph_context);
214 forker.daemonize();
215 }
216
7c673cae
FG
217 r = mds->init();
218 if (r < 0) {
219 msgr->wait();
220 goto shutdown;
221 }
222
7c673cae
FG
223 register_async_signal_handler_oneshot(SIGINT, handle_mds_signal);
224 register_async_signal_handler_oneshot(SIGTERM, handle_mds_signal);
225
11fdf7f2 226 if (g_conf()->inject_early_sigterm)
7c673cae
FG
227 kill(getpid(), SIGTERM);
228
229 msgr->wait();
230
231 unregister_async_signal_handler(SIGHUP, sighup_handler);
232 unregister_async_signal_handler(SIGINT, handle_mds_signal);
233 unregister_async_signal_handler(SIGTERM, handle_mds_signal);
234 shutdown_async_signal_handler();
235
236 shutdown:
f67539c2 237 ctxpool.stop();
7c673cae
FG
238 // yuck: grab the mds lock, so we can be sure that whoever in *mds
239 // called shutdown finishes what they were doing.
9f95a23c
TL
240 mds->mds_lock.lock();
241 mds->mds_lock.unlock();
7c673cae
FG
242
243 pidfile_remove();
244
245 // only delete if it was a clean shutdown (to aid memory leak
246 // detection, etc.). don't bother if it was a suicide.
247 if (mds->is_clean_shutdown()) {
248 delete mds;
249 delete msgr;
250 }
251
252 // cd on exit, so that gmon.out (if any) goes into a separate directory for each node.
253 char s[20];
254 snprintf(s, sizeof(s), "gmon/%d", getpid());
255 if ((mkdir(s, 0755) == 0) && (chdir(s) == 0)) {
256 cerr << "ceph-mds: gmon.out should be in " << s << std::endl;
257 }
258
259 return 0;
260}