summaryrefslogtreecommitdiffstats
path: root/src/tools/cephfs/TableTool.cc
blob: dcd35a6241b7e5bcda4da685e2e229679881d2ad (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
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
// -*- 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) 2015 John Spray <john.spray@redhat.com>
 *
 * 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 "common/ceph_argparse.h"
#include "common/errno.h"

#include "mds/SessionMap.h"
#include "mds/InoTable.h"
#include "mds/SnapServer.h"

#include "TableTool.h"


#define dout_context g_ceph_context
#define dout_subsys ceph_subsys_mds
#undef dout_prefix
#define dout_prefix *_dout << __func__ << ": "

using namespace std;

void TableTool::usage()
{
  std::cout << "Usage: \n"
    << "  cephfs-table-tool <all|[mds rank]> <reset|show> <session|snap|inode>"
    << "  cephfs-table-tool <all|[mds rank]> <take_inos> <max_ino>"
    << std::endl;

  generic_client_usage();
}


/**
 * For a function that takes an MDS role as an argument and
 * returns an error code, execute it on the roles specified
 * by `role_selector`.
 */
int TableTool::apply_role_fn(std::function<int(mds_role_t, Formatter *)> fptr, Formatter *f)
{
  ceph_assert(f != NULL);

  int r = 0;

  f->open_object_section("ranks");

  for (auto role : role_selector.get_roles()) {
    std::ostringstream rank_str;
    rank_str << role.rank;
    f->open_object_section(rank_str.str().c_str());

    f->open_object_section("data");
    int rank_r = fptr(role, f);
    f->close_section();
    r = r ? r : rank_r;

    f->dump_int("result", rank_r);
    f->close_section();

    
  }

  f->close_section();

  return r;
}


/**
 * This class wraps an MDS table class (SessionMap, SnapServer, InoTable)
 * with offline load/store code such that we can do offline dumps and resets
 * on those tables.
 */
template <typename A>
class TableHandler
{
protected:
  // The RADOS object ID for the table
  std::string object_name;

  // The role in question (may be NONE)
  mds_role_t role;

  // Whether this is an MDSTable subclass (i.e. has leading version field to decode)
  bool mds_table;

public:
  TableHandler(mds_role_t r, std::string const &name, bool mds_table_)
    : role(r), mds_table(mds_table_)
  {
    // Compose object name of the table we will dump
    std::ostringstream oss;
    oss << "mds";
    if (!role.is_none()) {
      oss << role.rank;
    }
    oss << "_" << name;
    object_name = oss.str();
  }

  int load_and_dump(librados::IoCtx *io, Formatter *f)
  {
    ceph_assert(io != NULL);
    ceph_assert(f != NULL);

    // Attempt read
    bufferlist table_bl;
    int read_r = io->read(object_name, table_bl, 0, 0);
    if (read_r >= 0) {
      auto q = table_bl.cbegin();
      try {
        if (mds_table) {
          version_t version;
          decode(version, q);
          f->dump_int("version", version);
        }
        A table_inst;
        table_inst.set_rank(role.rank);
        table_inst.decode(q);
        table_inst.dump(f);

        return 0;
      } catch (buffer::error &e) {
        derr << "table " << object_name << " is corrupt" << dendl;
        return -EIO;
      }
    } else {
      derr << "error reading table object " << object_name
        << ": " << cpp_strerror(read_r) << dendl;
      return read_r;
    }
  }

  int reset(librados::IoCtx *io)
  {
    A table_inst;
    // Compose new (blank) table
    table_inst.set_rank(role.rank);
    table_inst.reset_state();
    // Write the table out
    return write(table_inst, io);
  }

protected:

  int write(const A &table_inst, librados::IoCtx *io)
  {
    bufferlist new_bl;
    if (mds_table) {
      version_t version = 1;
      encode(version, new_bl);
    }
    table_inst.encode_state(new_bl);

    // Write out new table
    int r = io->write_full(object_name, new_bl);
    if (r != 0) {
      derr << "error writing table object " << object_name
        << ": " << cpp_strerror(r) << dendl;
      return r;
    }

    return r;
  }
};

template <typename A>
class TableHandlerOmap
{
private:
  // The RADOS object ID for the table
  std::string object_name;

  // The role (rank may be NONE)
  mds_role_t role;

  // Whether this is an MDSTable subclass (i.e. has leading version field to decode)
  bool mds_table;

public:
  TableHandlerOmap(mds_role_t r, std::string const &name, bool mds_table_)
    : role(r), mds_table(mds_table_)
  {
    // Compose object name of the table we will dump
    std::ostringstream oss;
    oss << "mds";
    if (!role.is_none()) {
      oss << role.rank;
    }
    oss << "_" << name;
    object_name = oss.str();
  }

  int load_and_dump(librados::IoCtx *io, Formatter *f)
  {
    ceph_assert(io != NULL);
    ceph_assert(f != NULL);

    // Read in the header
    bufferlist header_bl;
    int r = io->omap_get_header(object_name, &header_bl);
    if (r != 0) {
      derr << "error reading header on '" << object_name << "': "
           << cpp_strerror(r) << dendl;
      return r;
    }

    // Decode the header
    A table_inst;
    table_inst.set_rank(role.rank);
    try {
      table_inst.decode_header(header_bl);
    } catch (buffer::error &e) {
      derr << "table " << object_name << " is corrupt" << dendl;
      return -EIO;
    }

    // Read and decode OMAP values in chunks
    std::string last_key = "";
    while(true) {
      std::map<std::string, bufferlist> values;
      int r = io->omap_get_vals(object_name, last_key,
          g_conf()->mds_sessionmap_keys_per_op, &values);

      if (r != 0) {
        derr << "error reading values: " << cpp_strerror(r) << dendl;
        return r;
      }

      if (values.empty()) {
        break;
      }

      try {
        table_inst.decode_values(values);
      } catch (buffer::error &e) {
        derr << "table " << object_name << " is corrupt" << dendl;
        return -EIO;
      }
      last_key = values.rbegin()->first;
    }

    table_inst.dump(f);

    return 0;
  }

  int reset(librados::IoCtx *io)
  {
    A table_inst;
    table_inst.set_rank(role.rank);
    table_inst.reset_state();
    bufferlist header_bl;
    table_inst.encode_header(&header_bl);

    // Compose a transaction to clear and write header
    librados::ObjectWriteOperation op;
    op.omap_clear();
    op.set_op_flags2(LIBRADOS_OP_FLAG_FAILOK);
    op.omap_set_header(header_bl);
    
    return io->operate(object_name, &op);
  }
};

class InoTableHandler : public TableHandler<InoTable>
{
  public:
  explicit InoTableHandler(mds_role_t r)
    : TableHandler(r, "inotable", true)
  {}

  int take_inos(librados::IoCtx *io, inodeno_t max, Formatter *f)
  {
    InoTable inst;
    inst.set_rank(role.rank);
    inst.reset_state();

    int r = 0;
    if (inst.force_consume_to(max)) {
      r = write(inst, io);
    }

    f->dump_int("version", inst.get_version());
    inst.dump(f);

    return r;
  }
};


int TableTool::main(std::vector<const char*> &argv)
{
  int r;

  dout(10) << __func__ << dendl;

  // RADOS init
  // ==========
  r = rados.init_with_context(g_ceph_context);
  if (r < 0) {
    derr << "RADOS unavailable, cannot scan filesystem journal" << dendl;
    return r;
  }

  dout(4) << "connecting to RADOS..." << dendl;
  r = rados.connect();
  if (r < 0) {
    derr << "couldn't connect to cluster: " << cpp_strerror(r) << dendl;
    return r;
  }

  // Require at least 3 args <rank> <mode> <arg> [args...]
  if (argv.size() < 3) {
    cerr << "missing required 3 arguments" << std::endl;
    return -EINVAL;
  }

  const std::string role_str = std::string(argv[0]);
  const std::string mode = std::string(argv[1]);
  const std::string table = std::string(argv[2]);

  r = role_selector.parse(*fsmap, role_str);
  if (r < 0) {
    derr << "Bad rank selection: " << role_str << "'" << dendl;
    return r;
  }

  auto fs =  fsmap->get_filesystem(role_selector.get_ns());
  ceph_assert(fs != nullptr);
  int64_t const pool_id = fs->mds_map.get_metadata_pool();
  dout(4) << "resolving pool " << pool_id << dendl;
  std::string pool_name;
  r = rados.pool_reverse_lookup(pool_id, &pool_name);
  if (r < 0) {
    derr << "Pool " << pool_id << " identified in MDS map not found in RADOS!"
         << dendl;
    return r;
  }

  dout(4) << "creating IoCtx.." << dendl;
  r = rados.ioctx_create(pool_name.c_str(), io);
  if (r != 0) {
    return r;
  }

  JSONFormatter jf(true);
  if (mode == "reset") {
    const std::string table = std::string(argv[2]);
    if (table == "session") {
      r = apply_role_fn([this](mds_role_t rank, Formatter *f) -> int {
            return TableHandlerOmap<SessionMapStore>(rank, "sessionmap", false).reset(&io);
      }, &jf);
    } else if (table == "inode") {
      r = apply_role_fn([this](mds_role_t rank, Formatter *f) -> int {
            return TableHandler<InoTable>(rank, "inotable", true).reset(&io);
      }, &jf);
    } else if (table == "snap") {
      r = TableHandler<SnapServer>(mds_role_t(), "snaptable", true).reset(&io);
      jf.open_object_section("reset_snap_status");
      jf.dump_int("result", r);
      jf.close_section();
    } else {
      cerr << "Invalid table '" << table << "'" << std::endl;
      return -EINVAL;
    }
  } else if (mode == "show") {
    const std::string table = std::string(argv[2]);
    if (table == "session") {
      r = apply_role_fn([this](mds_role_t rank, Formatter *f) -> int {
        return TableHandlerOmap<SessionMapStore>(rank, "sessionmap", false).load_and_dump(&io, f);
      }, &jf);
    } else if (table == "inode") {
      r = apply_role_fn([this](mds_role_t rank, Formatter *f) -> int {
        return TableHandler<InoTable>(rank, "inotable", true).load_and_dump(&io, f);;
      }, &jf);
    } else if (table == "snap") {
      jf.open_object_section("show_snap_table");
      {
        r = TableHandler<SnapServer>(
            mds_role_t(), "snaptable", true).load_and_dump(&io, &jf);
        jf.dump_int("result", r);
      }
      jf.close_section();
    } else {
      cerr << "Invalid table '" << table << "'" << std::endl;
      return -EINVAL;
    }
  } else if (mode == "take_inos") {
    const std::string ino_str = std::string(argv[2]);
    std::string ino_err;
    inodeno_t ino = strict_strtoll(ino_str.c_str(), 10, &ino_err);
    if (!ino_err.empty()) {
      derr << "Bad ino '" << ino_str << "'" << dendl;
      return -EINVAL;
    }
    r = apply_role_fn([this, ino](mds_role_t rank, Formatter *f) -> int {
      return InoTableHandler(rank).take_inos(&io, ino, f);
    }, &jf);
  } else {
    cerr << "Invalid mode '" << mode << "'" << std::endl;
    return -EINVAL;
  }

  // Subcommand should have written to formatter, flush it
  jf.flush(std::cout);
  std::cout << std::endl;
  return r;
}