]> git.proxmox.com Git - ceph.git/blame - ceph/src/ceph_mds.cc
update sources to 12.2.8
[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>
22using namespace std;
23
24#include "include/ceph_features.h"
25#include "include/compat.h"
26
27#include "common/config.h"
28#include "common/strtol.h"
29
30#include "mon/MonMap.h"
31#include "mds/MDSDaemon.h"
32
33#include "msg/Messenger.h"
34
35#include "common/Timer.h"
36#include "common/ceph_argparse.h"
37#include "common/pick_address.h"
38
39#include "global/global_init.h"
40#include "global/signal_handler.h"
41#include "global/pidfile.h"
42
43#include "mon/MonClient.h"
44
45#include "auth/KeyRing.h"
46
47#include "perfglue/heap_profiler.h"
48
49#include "include/assert.h"
50
51#define dout_context g_ceph_context
52#define dout_subsys ceph_subsys_mds
53
54static void usage()
55{
31f18b77 56 cout << "usage: ceph-mds -i <ID> [flags] [[--hot-standby][rank]]\n"
7c673cae
FG
57 << " -m monitorip:port\n"
58 << " connect to monitor at given address\n"
59 << " --debug_mds n\n"
60 << " debug MDS level (e.g. 10)\n"
61 << " --hot-standby rank\n"
62 << " start up as a hot standby for rank\n"
63 << std::endl;
64 generic_server_usage();
65}
66
67
68static int parse_rank(const char *opt_name, const std::string &val)
69{
70 std::string err;
71 int ret = strict_strtol(val.c_str(), 10, &err);
72 if (!err.empty()) {
73 derr << "error parsing " << opt_name << ": failed to parse rank. "
74 << "It must be an int." << "\n" << dendl;
75 usage();
76 }
77 return ret;
78}
79
80
81
82MDSDaemon *mds = NULL;
83
84
85static void handle_mds_signal(int signum)
86{
87 if (mds)
88 mds->handle_signal(signum);
89}
90
7c673cae 91int main(int argc, const char **argv)
7c673cae
FG
92{
93 ceph_pthread_setname(pthread_self(), "ceph-mds");
94
95 vector<const char*> args;
96 argv_to_vec(argc, argv, args);
97 env_to_vec(args);
98
99 auto cct = global_init(NULL, args,
100 CEPH_ENTITY_TYPE_MDS, CODE_ENVIRONMENT_DAEMON,
101 0, "mds_data");
102 ceph_heap_profiler_init();
103
104 std::string val, action;
105 for (std::vector<const char*>::iterator i = args.begin(); i != args.end(); ) {
106 if (ceph_argparse_double_dash(args, i)) {
107 break;
108 }
109 else if (ceph_argparse_flag(args, i, "--help", "-h", (char*)NULL)) {
110 // exit(1) will be called in the usage()
111 usage();
112 }
113 else if (ceph_argparse_witharg(args, i, &val, "--hot-standby", (char*)NULL)) {
114 int r = parse_rank("hot-standby", val);
115 dout(0) << "requesting standby_replay for mds." << r << dendl;
116 char rb[32];
117 snprintf(rb, sizeof(rb), "%d", r);
118 g_conf->set_val("mds_standby_for_rank", rb);
119 g_conf->set_val("mds_standby_replay", "true");
120 g_conf->apply_changes(NULL);
121 }
122 else {
123 derr << "Error: can't understand argument: " << *i << "\n" << dendl;
124 usage();
125 }
126 }
127
128 pick_addresses(g_ceph_context, CEPH_PICK_ADDRESS_PUBLIC);
129
130 // Normal startup
131 if (g_conf->name.has_default_id()) {
132 derr << "must specify '-i name' with the ceph-mds instance name" << dendl;
133 usage();
134 }
135
136 if (g_conf->name.get_id().empty() ||
137 (g_conf->name.get_id()[0] >= '0' && g_conf->name.get_id()[0] <= '9')) {
138 derr << "deprecation warning: MDS id '" << g_conf->name
139 << "' is invalid and will be forbidden in a future version. "
140 "MDS names may not start with a numeric digit." << dendl;
141 }
142
143 uint64_t nonce = 0;
144 get_random_bytes((char*)&nonce, sizeof(nonce));
145
146 std::string public_msgr_type = g_conf->ms_public_type.empty() ? g_conf->get_val<std::string>("ms_type") : g_conf->ms_public_type;
147 Messenger *msgr = Messenger::create(g_ceph_context, public_msgr_type,
148 entity_name_t::MDS(-1), "mds",
149 nonce, Messenger::HAS_MANY_CONNECTIONS);
150 if (!msgr)
151 exit(1);
152 msgr->set_cluster_protocol(CEPH_MDS_PROTOCOL);
153
154 cout << "starting " << g_conf->name << " at " << msgr->get_myaddr()
155 << std::endl;
156 uint64_t required =
157 CEPH_FEATURE_OSDREPLYMUX;
158
159 msgr->set_default_policy(Messenger::Policy::lossy_client(required));
160 msgr->set_policy(entity_name_t::TYPE_MON,
161 Messenger::Policy::lossy_client(CEPH_FEATURE_UID |
162 CEPH_FEATURE_PGID64));
163 msgr->set_policy(entity_name_t::TYPE_MDS,
164 Messenger::Policy::lossless_peer(CEPH_FEATURE_UID));
165 msgr->set_policy(entity_name_t::TYPE_CLIENT,
166 Messenger::Policy::stateful_server(0));
167
168 int r = msgr->bind(g_conf->public_addr);
169 if (r < 0)
170 exit(1);
171
172 global_init_daemonize(g_ceph_context);
173 common_init_finish(g_ceph_context);
174
175 // get monmap
176 MonClient mc(g_ceph_context);
177 if (mc.build_initial_monmap() < 0)
178 return -1;
179 global_init_chdir(g_ceph_context);
180
181 msgr->start();
182
183 // start mds
184 mds = new MDSDaemon(g_conf->name.get_id().c_str(), msgr, &mc);
185
186 // in case we have to respawn...
187 mds->orig_argc = argc;
188 mds->orig_argv = argv;
189
190 r = mds->init();
191 if (r < 0) {
192 msgr->wait();
193 goto shutdown;
194 }
195
196 // set up signal handlers, now that we've daemonized/forked.
197 init_async_signal_handler();
198 register_async_signal_handler(SIGHUP, sighup_handler);
199 register_async_signal_handler_oneshot(SIGINT, handle_mds_signal);
200 register_async_signal_handler_oneshot(SIGTERM, handle_mds_signal);
201
202 if (g_conf->inject_early_sigterm)
203 kill(getpid(), SIGTERM);
204
205 msgr->wait();
206
207 unregister_async_signal_handler(SIGHUP, sighup_handler);
208 unregister_async_signal_handler(SIGINT, handle_mds_signal);
209 unregister_async_signal_handler(SIGTERM, handle_mds_signal);
210 shutdown_async_signal_handler();
211
212 shutdown:
213 // yuck: grab the mds lock, so we can be sure that whoever in *mds
214 // called shutdown finishes what they were doing.
215 mds->mds_lock.Lock();
216 mds->mds_lock.Unlock();
217
218 pidfile_remove();
219
220 // only delete if it was a clean shutdown (to aid memory leak
221 // detection, etc.). don't bother if it was a suicide.
222 if (mds->is_clean_shutdown()) {
223 delete mds;
224 delete msgr;
225 }
226
227 // cd on exit, so that gmon.out (if any) goes into a separate directory for each node.
228 char s[20];
229 snprintf(s, sizeof(s), "gmon/%d", getpid());
230 if ((mkdir(s, 0755) == 0) && (chdir(s) == 0)) {
231 cerr << "ceph-mds: gmon.out should be in " << s << std::endl;
232 }
233
234 return 0;
235}
236