Fix some bugs when testing opensds ansible
[stor4nfv.git] / src / 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 using 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
54 static void usage()
55 {
56   cout << "usage: ceph-mds -i <ID> [flags] [[--hot-standby][rank]]\n"
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
68 static 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
82 MDSDaemon *mds = NULL;
83
84
85 static void handle_mds_signal(int signum)
86 {
87   if (mds)
88     mds->handle_signal(signum);
89 }
90
91 #ifdef BUILDING_FOR_EMBEDDED
92 extern "C" int cephd_mds(int argc, const char **argv)
93 #else
94 int main(int argc, const char **argv)
95 #endif
96 {
97   ceph_pthread_setname(pthread_self(), "ceph-mds");
98
99   vector<const char*> args;
100   argv_to_vec(argc, argv, args);
101   env_to_vec(args);
102
103   auto cct = global_init(NULL, args,
104                          CEPH_ENTITY_TYPE_MDS, CODE_ENVIRONMENT_DAEMON,
105                          0, "mds_data");
106   ceph_heap_profiler_init();
107
108   std::string val, action;
109   for (std::vector<const char*>::iterator i = args.begin(); i != args.end(); ) {
110     if (ceph_argparse_double_dash(args, i)) {
111       break;
112     }
113     else if (ceph_argparse_flag(args, i, "--help", "-h", (char*)NULL)) {
114       // exit(1) will be called in the usage()
115       usage();
116     }
117     else if (ceph_argparse_witharg(args, i, &val, "--hot-standby", (char*)NULL)) {
118       int r = parse_rank("hot-standby", val);
119       dout(0) << "requesting standby_replay for mds." << r << dendl;
120       char rb[32];
121       snprintf(rb, sizeof(rb), "%d", r);
122       g_conf->set_val("mds_standby_for_rank", rb);
123       g_conf->set_val("mds_standby_replay", "true");
124       g_conf->apply_changes(NULL);
125     }
126     else {
127       derr << "Error: can't understand argument: " << *i << "\n" << dendl;
128       usage();
129     }
130   }
131
132   pick_addresses(g_ceph_context, CEPH_PICK_ADDRESS_PUBLIC);
133
134   // Normal startup
135   if (g_conf->name.has_default_id()) {
136     derr << "must specify '-i name' with the ceph-mds instance name" << dendl;
137     usage();
138   }
139
140   if (g_conf->name.get_id().empty() ||
141       (g_conf->name.get_id()[0] >= '0' && g_conf->name.get_id()[0] <= '9')) {
142     derr << "deprecation warning: MDS id '" << g_conf->name
143       << "' is invalid and will be forbidden in a future version.  "
144       "MDS names may not start with a numeric digit." << dendl;
145   }
146
147   uint64_t nonce = 0;
148   get_random_bytes((char*)&nonce, sizeof(nonce));
149
150   std::string public_msgr_type = g_conf->ms_public_type.empty() ? g_conf->get_val<std::string>("ms_type") : g_conf->ms_public_type;
151   Messenger *msgr = Messenger::create(g_ceph_context, public_msgr_type,
152                                       entity_name_t::MDS(-1), "mds",
153                                       nonce, Messenger::HAS_MANY_CONNECTIONS);
154   if (!msgr)
155     exit(1);
156   msgr->set_cluster_protocol(CEPH_MDS_PROTOCOL);
157
158   cout << "starting " << g_conf->name << " at " << msgr->get_myaddr()
159        << std::endl;
160   uint64_t required =
161     CEPH_FEATURE_OSDREPLYMUX;
162
163   msgr->set_default_policy(Messenger::Policy::lossy_client(required));
164   msgr->set_policy(entity_name_t::TYPE_MON,
165                    Messenger::Policy::lossy_client(CEPH_FEATURE_UID |
166                                                    CEPH_FEATURE_PGID64));
167   msgr->set_policy(entity_name_t::TYPE_MDS,
168                    Messenger::Policy::lossless_peer(CEPH_FEATURE_UID));
169   msgr->set_policy(entity_name_t::TYPE_CLIENT,
170                    Messenger::Policy::stateful_server(0));
171
172   int r = msgr->bind(g_conf->public_addr);
173   if (r < 0)
174     exit(1);
175
176   global_init_daemonize(g_ceph_context);
177   common_init_finish(g_ceph_context);
178
179   // get monmap
180   MonClient mc(g_ceph_context);
181   if (mc.build_initial_monmap() < 0)
182     return -1;
183   global_init_chdir(g_ceph_context);
184
185   msgr->start();
186
187   // start mds
188   mds = new MDSDaemon(g_conf->name.get_id().c_str(), msgr, &mc);
189
190   // in case we have to respawn...
191   mds->orig_argc = argc;
192   mds->orig_argv = argv;
193
194   r = mds->init();
195   if (r < 0) {
196     msgr->wait();
197     goto shutdown;
198   }
199
200   // set up signal handlers, now that we've daemonized/forked.
201   init_async_signal_handler();
202   register_async_signal_handler(SIGHUP, sighup_handler);
203   register_async_signal_handler_oneshot(SIGINT, handle_mds_signal);
204   register_async_signal_handler_oneshot(SIGTERM, handle_mds_signal);
205
206   if (g_conf->inject_early_sigterm)
207     kill(getpid(), SIGTERM);
208
209   msgr->wait();
210
211   unregister_async_signal_handler(SIGHUP, sighup_handler);
212   unregister_async_signal_handler(SIGINT, handle_mds_signal);
213   unregister_async_signal_handler(SIGTERM, handle_mds_signal);
214   shutdown_async_signal_handler();
215
216  shutdown:
217   // yuck: grab the mds lock, so we can be sure that whoever in *mds
218   // called shutdown finishes what they were doing.
219   mds->mds_lock.Lock();
220   mds->mds_lock.Unlock();
221
222   pidfile_remove();
223
224   // only delete if it was a clean shutdown (to aid memory leak
225   // detection, etc.).  don't bother if it was a suicide.
226   if (mds->is_clean_shutdown()) {
227     delete mds;
228     delete msgr;
229   }
230
231   // cd on exit, so that gmon.out (if any) goes into a separate directory for each node.
232   char s[20];
233   snprintf(s, sizeof(s), "gmon/%d", getpid());
234   if ((mkdir(s, 0755) == 0) && (chdir(s) == 0)) {
235     cerr << "ceph-mds: gmon.out should be in " << s << std::endl;
236   }
237
238   return 0;
239 }
240