summaryrefslogtreecommitdiff
path: root/src/ceph_mds.cc
blob: 36aa58bd57919a4613429d991e67251437e81694 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
// -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*- 
// vim: ts=8 sw=2 smarttab
/*
 * Ceph - scalable distributed file system
 *
 * Copyright (C) 2004-2006 Sage Weil <sage@newdream.net>
 *
 * This is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License version 2.1, as published by the Free Software 
 * Foundation.  See file COPYING.
 * 
 */

#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>

#include <iostream>
#include <string>
using namespace std;

#include "include/ceph_features.h"

#include "common/config.h"
#include "common/strtol.h"

#include "mon/MonMap.h"
#include "mds/MDS.h"
#include "mds/Dumper.h"
#include "mds/Resetter.h"

#include "msg/Messenger.h"

#include "common/Timer.h"
#include "common/ceph_argparse.h"
#include "common/pick_address.h"

#include "global/global_init.h"
#include "global/signal_handler.h"
#include "global/pidfile.h"

#include "mon/MonClient.h"

#include "auth/KeyRing.h"

#include "include/assert.h"

#define dout_subsys ceph_subsys_mds

void usage()
{
  derr << "usage: ceph-mds -i name [flags] [[--journal_check rank]|[--hot-standby][rank]]\n"
       << "  -m monitorip:port\n"
       << "        connect to monitor at given address\n"
       << "  --debug_mds n\n"
       << "        debug MDS level (e.g. 10)\n"
       << "  --dump-journal rank filename\n"
       << "        dump the MDS journal for rank.\n"
       << "  --journal-check rank\n"
       << "        replay the journal for rank, then exit\n"
       << "  --hot-standby rank\n"
       << "        stat up as a hot standby for rank\n"
       << "  --reset-journal rank\n"
       << "        discard the MDS journal for rank, and replace it with a single\n"
       << "        event that updates/resets inotable and sessionmap on replay.\n"
       << dendl;
  generic_server_usage();
}

static int do_cmds_special_action(const std::string &action,
				  const std::string &dump_file, int rank)
{
  common_init_finish(g_ceph_context);
  Messenger *messenger = Messenger::create(g_ceph_context,
					   entity_name_t::CLIENT(), "mds",
					   getpid());
  int r = messenger->bind(g_conf->public_addr);
  if (r < 0)
    return r;
  MonClient mc(g_ceph_context);
  if (mc.build_initial_monmap() < 0)
    return -1;

  if (action == "dump-journal") {
    dout(0) << "dumping journal for mds." << rank << " to " << dump_file << dendl;
    Dumper *journal_dumper = new Dumper(messenger, &mc);
    journal_dumper->init(rank);
    journal_dumper->dump(dump_file.c_str());
    mc.shutdown();
  }
  else if (action == "undump-journal") {
    dout(0) << "undumping journal for mds." << rank << " from " << dump_file << dendl;
    Dumper *journal_dumper = new Dumper(messenger, &mc);
    journal_dumper->init(rank);
    journal_dumper->undump(dump_file.c_str());
    mc.shutdown();
  }
  else if (action == "reset-journal") {
    dout(0) << "resetting journal" << dendl;
    Resetter *jr = new Resetter(messenger, &mc);
    jr->init(rank);
    jr->reset();
    mc.shutdown();
  }
  else {
    assert(0);
  }
  return 0;
}

static void set_special_action(std::string &dest, const std::string &act)
{
  if (!dest.empty()) {
    derr << "Parse error! Can't specify more than one action. You "
	 << "specified both " << act << " and " << dest << "\n" << dendl;
    usage();
    exit(1);
  }
  dest = act;
}

static int parse_rank(const char *opt_name, const std::string &val)
{
  std::string err;
  int ret = strict_strtol(val.c_str(), 10, &err);
  if (!err.empty()) {
    derr << "error parsing " << opt_name << ": failed to parse rank. "
	 << "It must be an int." << "\n" << dendl;
    usage();
  }
  return ret;
}



MDS *mds = NULL;


static void handle_mds_signal(int signum)
{
  if (mds)
    mds->handle_signal(signum);
}

int main(int argc, const char **argv) 
{
  vector<const char*> args;
  argv_to_vec(argc, argv, args);
  env_to_vec(args);

  global_init(NULL, args, CEPH_ENTITY_TYPE_MDS, CODE_ENVIRONMENT_DAEMON, 0);

  // mds specific args
  int shadow = 0;
  int rank = -1;
  std::string dump_file;

  std::string val, action;
  for (std::vector<const char*>::iterator i = args.begin(); i != args.end(); ) {
    if (ceph_argparse_double_dash(args, i)) {
      break;
    }
    else if (ceph_argparse_witharg(args, i, &val, "--dump-journal", (char*)NULL)) {
      set_special_action(action, "dump-journal");
      rank = parse_rank("dump-journal", val);
      if (i == args.end()) {
	derr << "error parsing --dump-journal: you must give a second "
	     << "dump-journal argument: the filename to dump the journal to. "
	     << "\n" << dendl;
	usage();
      }
      dump_file = *i++;
    }
    else if (ceph_argparse_witharg(args, i, &val, "--undump-journal", (char*)NULL)) {
      set_special_action(action, "undump-journal");
      rank = parse_rank("undump-journal", val);
      if (i == args.end()) {
	derr << "error parsing --undump-journal: you must give a second "
	     << "undump-journal argument: the filename to undump the journal from. "
	     << "\n" << dendl;
	usage();
      }
      dump_file = *i++;
    }
    else if (ceph_argparse_witharg(args, i, &val, "--reset-journal", (char*)NULL)) {
      set_special_action(action, "reset-journal");
      rank = parse_rank("reset-journal", val);
    }
    else if (ceph_argparse_witharg(args, i, &val, "--journal-check", (char*)NULL)) {
      int r = parse_rank("journal-check", val);
      if (shadow) {
        dout(0) << "Error: can only select one standby state" << dendl;
        return -1;
      }
      dout(0) << "requesting oneshot_replay for mds." << r << dendl;
      shadow = MDSMap::STATE_ONESHOT_REPLAY;
      char rb[32];
      snprintf(rb, sizeof(rb), "%d", r);
      g_conf->set_val("mds_standby_for_rank", rb);
      g_conf->apply_changes(NULL);
    }
    else if (ceph_argparse_witharg(args, i, &val, "--hot-standby", (char*)NULL)) {
      int r = parse_rank("hot-standby", val);
      if (shadow) {
        dout(0) << "Error: can only select one standby state" << dendl;
        return -1;
      }
      dout(0) << "requesting standby_replay for mds." << r << dendl;
      shadow = MDSMap::STATE_STANDBY_REPLAY;
      char rb[32];
      snprintf(rb, sizeof(rb), "%d", r);
      g_conf->set_val("mds_standby_for_rank", rb);
      g_conf->apply_changes(NULL);
    }
    else {
      derr << "Error: can't understand argument: " << *i << "\n" << dendl;
      usage();
    }
  }

  pick_addresses(g_ceph_context);

  // Check for special actions
  if (!action.empty()) {
    return do_cmds_special_action(action, dump_file, rank);
  }

  // Normal startup
  if (g_conf->name.has_default_id()) {
    derr << "must specify '-i name' with the ceph-mds instance name" << dendl;
    usage();
  }

  Messenger *messenger = Messenger::create(g_ceph_context,
					   entity_name_t::MDS(-1), "mds",
					   getpid());
  messenger->set_cluster_protocol(CEPH_MDS_PROTOCOL);

  cout << "starting " << g_conf->name << " at " << messenger->get_myaddr()
       << std::endl;
  uint64_t supported =
    CEPH_FEATURE_UID |
    CEPH_FEATURE_NOSRCADDR |
    CEPH_FEATURE_DIRLAYOUTHASH |
    CEPH_FEATURE_PGID64 |
    CEPH_FEATURE_MSG_AUTH;
  uint64_t required =
    CEPH_FEATURE_OSDREPLYMUX;
  messenger->set_default_policy(Messenger::Policy::lossy_client(supported, required));
  messenger->set_policy(entity_name_t::TYPE_MON,
			Messenger::Policy::lossy_client(supported,
							CEPH_FEATURE_UID |
							CEPH_FEATURE_PGID64));
  messenger->set_policy(entity_name_t::TYPE_MDS,
			Messenger::Policy::lossless_peer(supported,
							 CEPH_FEATURE_UID));
  messenger->set_policy(entity_name_t::TYPE_CLIENT,
			Messenger::Policy::stateful_server(supported, 0));

  int r = messenger->bind(g_conf->public_addr);
  if (r < 0)
    exit(1);

  if (shadow != MDSMap::STATE_ONESHOT_REPLAY)
    global_init_daemonize(g_ceph_context, 0);
  common_init_finish(g_ceph_context);

  // get monmap
  MonClient mc(g_ceph_context);
  if (mc.build_initial_monmap() < 0)
    return -1;
  global_init_chdir(g_ceph_context);

  messenger->start();

  // set up signal handlers, now that we've daemonized/forked.
  init_async_signal_handler();
  register_async_signal_handler(SIGHUP, sighup_handler);
  register_async_signal_handler_oneshot(SIGINT, handle_mds_signal);
  register_async_signal_handler_oneshot(SIGTERM, handle_mds_signal);

  // start mds
  mds = new MDS(g_conf->name.get_id().c_str(), messenger, &mc);

  // in case we have to respawn...
  mds->orig_argc = argc;
  mds->orig_argv = argv;

  if (shadow)
    r = mds->init(shadow);
  else
    r = mds->init();

  if (r >= 0) {
    messenger->wait();
  }

  unregister_async_signal_handler(SIGHUP, sighup_handler);
  unregister_async_signal_handler(SIGINT, handle_mds_signal);
  unregister_async_signal_handler(SIGTERM, handle_mds_signal);

  // yuck: grab the mds lock, so we can be sure that whoever in *mds
  // called shutdown finishes what they were doing.
  mds->mds_lock.Lock();
  mds->mds_lock.Unlock();

  pidfile_remove();

  // only delete if it was a clean shutdown (to aid memory leak
  // detection, etc.).  don't bother if it was a suicide.
  if (mds->is_stopped())
    delete mds;

  // cd on exit, so that gmon.out (if any) goes into a separate directory for each node.
  char s[20];
  snprintf(s, sizeof(s), "gmon/%d", getpid());
  if ((mkdir(s, 0755) == 0) && (chdir(s) == 0)) {
    dout(0) << "ceph-mds: gmon.out should be in " << s << dendl;
  }

  generic_dout(0) << "stopped." << dendl;
  return 0;
}