]> git.proxmox.com Git - ceph.git/blob - ceph/src/ceph_mds.cc
import 15.2.0 Octopus source
[ceph.git] / ceph / src / ceph_mds.cc
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>
22
23 #include "include/ceph_features.h"
24 #include "include/compat.h"
25 #include "include/random.h"
26
27 #include "common/config.h"
28 #include "common/strtol.h"
29 #include "common/numa.h"
30
31 #include "mon/MonMap.h"
32 #include "mds/MDSDaemon.h"
33
34 #include "msg/Messenger.h"
35
36 #include "common/Timer.h"
37 #include "common/ceph_argparse.h"
38 #include "common/pick_address.h"
39 #include "common/Preforker.h"
40
41 #include "global/global_init.h"
42 #include "global/signal_handler.h"
43 #include "global/pidfile.h"
44
45 #include "mon/MonClient.h"
46
47 #include "auth/KeyRing.h"
48
49 #include "perfglue/heap_profiler.h"
50
51 #include "include/ceph_assert.h"
52
53 #define dout_context g_ceph_context
54 #define dout_subsys ceph_subsys_mds
55
56 static void usage()
57 {
58 cout << "usage: ceph-mds -i <ID> [flags]\n"
59 << " -m monitorip:port\n"
60 << " connect to monitor at given address\n"
61 << " --debug_mds n\n"
62 << " debug MDS level (e.g. 10)\n"
63 << std::endl;
64 generic_server_usage();
65 }
66
67
68 MDSDaemon *mds = NULL;
69
70
71 static void handle_mds_signal(int signum)
72 {
73 if (mds)
74 mds->handle_signal(signum);
75 }
76
77 int main(int argc, const char **argv)
78 {
79 ceph_pthread_setname(pthread_self(), "ceph-mds");
80
81 vector<const char*> args;
82 argv_to_vec(argc, argv, args);
83 if (args.empty()) {
84 cerr << argv[0] << ": -h or --help for usage" << std::endl;
85 exit(1);
86 }
87 if (ceph_argparse_need_usage(args)) {
88 usage();
89 exit(0);
90 }
91
92 auto cct = global_init(NULL, args,
93 CEPH_ENTITY_TYPE_MDS, CODE_ENVIRONMENT_DAEMON,
94 0, "mds_data");
95 ceph_heap_profiler_init();
96
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 }
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 }
121 else if (ceph_argparse_witharg(args, i, &val, "--hot-standby", (char*)NULL)) {
122 dout(0) << "--hot-standby is obsolete and has no effect" << dendl;
123 }
124 else {
125 derr << "Error: can't understand argument: " << *i << "\n" << dendl;
126 exit(1);
127 }
128 }
129
130 Preforker forker;
131
132 entity_addrvec_t addrs;
133 pick_addresses(g_ceph_context, CEPH_PICK_ADDRESS_PUBLIC, &addrs);
134
135 // Normal startup
136 if (g_conf()->name.has_default_id()) {
137 derr << "must specify '-i name' with the ceph-mds instance name" << dendl;
138 exit(1);
139 }
140
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. "
144 "MDS names may not start with a numeric digit." << dendl;
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);
162 }
163 common_init_finish(g_ceph_context);
164 global_init_chdir(g_ceph_context);
165
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;
167 Messenger *msgr = Messenger::create(g_ceph_context, public_msgr_type,
168 entity_name_t::MDS(-1), "mds",
169 Messenger::get_random_nonce(),
170 Messenger::HAS_MANY_CONNECTIONS);
171 if (!msgr)
172 forker.exit(1);
173 msgr->set_cluster_protocol(CEPH_MDS_PROTOCOL);
174
175 cout << "starting " << g_conf()->name << " at " << msgr->get_myaddrs()
176 << std::endl;
177 uint64_t required =
178 CEPH_FEATURE_OSDREPLYMUX;
179
180 msgr->set_default_policy(Messenger::Policy::lossy_client(required));
181 msgr->set_policy(entity_name_t::TYPE_MON,
182 Messenger::Policy::lossy_client(CEPH_FEATURE_UID |
183 CEPH_FEATURE_PGID64));
184 msgr->set_policy(entity_name_t::TYPE_MDS,
185 Messenger::Policy::lossless_peer(CEPH_FEATURE_UID));
186 msgr->set_policy(entity_name_t::TYPE_CLIENT,
187 Messenger::Policy::stateful_server(0));
188
189 int r = msgr->bindv(addrs);
190 if (r < 0)
191 forker.exit(1);
192
193 // set up signal handlers, now that we've daemonized/forked.
194 init_async_signal_handler();
195 register_async_signal_handler(SIGHUP, sighup_handler);
196
197 // get monmap
198 MonClient mc(g_ceph_context);
199 if (mc.build_initial_monmap() < 0)
200 forker.exit(1);
201 global_init_chdir(g_ceph_context);
202
203 msgr->start();
204
205 // start mds
206 mds = new MDSDaemon(g_conf()->name.get_id().c_str(), msgr, &mc);
207
208 // in case we have to respawn...
209 mds->orig_argc = argc;
210 mds->orig_argv = argv;
211
212 if (g_conf()->daemonize) {
213 global_init_postfork_finish(g_ceph_context);
214 forker.daemonize();
215 }
216
217 r = mds->init();
218 if (r < 0) {
219 msgr->wait();
220 goto shutdown;
221 }
222
223 register_async_signal_handler_oneshot(SIGINT, handle_mds_signal);
224 register_async_signal_handler_oneshot(SIGTERM, handle_mds_signal);
225
226 if (g_conf()->inject_early_sigterm)
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:
237 // yuck: grab the mds lock, so we can be sure that whoever in *mds
238 // called shutdown finishes what they were doing.
239 mds->mds_lock.lock();
240 mds->mds_lock.unlock();
241
242 pidfile_remove();
243
244 // only delete if it was a clean shutdown (to aid memory leak
245 // detection, etc.). don't bother if it was a suicide.
246 if (mds->is_clean_shutdown()) {
247 delete mds;
248 delete msgr;
249 }
250
251 // cd on exit, so that gmon.out (if any) goes into a separate directory for each node.
252 char s[20];
253 snprintf(s, sizeof(s), "gmon/%d", getpid());
254 if ((mkdir(s, 0755) == 0) && (chdir(s) == 0)) {
255 cerr << "ceph-mds: gmon.out should be in " << s << std::endl;
256 }
257
258 return 0;
259 }
260