summaryrefslogtreecommitdiffstats
path: root/src/mon/MgrMap.h
blob: 5342fc51fc3deda4e013fa88672e182e86064914 (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
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
// -*- 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) 2016 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.
 */

#ifndef MGR_MAP_H_
#define MGR_MAP_H_

#include <sstream>
#include <set>

#include "msg/msg_types.h"
#include "include/encoding.h"
#include "include/utime.h"
#include "common/Formatter.h"
#include "common/ceph_releases.h"
#include "common/version.h"
#include "common/options.h"
#include "common/Clock.h"


class MgrMap
{
public:
  struct ModuleOption {
    std::string name;
    uint8_t type = Option::TYPE_STR;         // Option::type_t TYPE_*
    uint8_t level = Option::LEVEL_ADVANCED;  // Option::level_t LEVEL_*
    uint32_t flags = 0; // Option::flag_t FLAG_*
    std::string default_value;
    std::string min, max;
    std::set<std::string> enum_allowed;
    std::string desc, long_desc;
    std::set<std::string> tags;
    std::set<std::string> see_also;

    void encode(ceph::buffer::list& bl) const {
      ENCODE_START(1, 1, bl);
      encode(name, bl);
      encode(type, bl);
      encode(level, bl);
      encode(flags, bl);
      encode(default_value, bl);
      encode(min, bl);
      encode(max, bl);
      encode(enum_allowed, bl);
      encode(desc, bl);
      encode(long_desc, bl);
      encode(tags, bl);
      encode(see_also, bl);
      ENCODE_FINISH(bl);
    }
    void decode(ceph::buffer::list::const_iterator& p) {
      DECODE_START(1, p);
      decode(name, p);
      decode(type, p);
      decode(level, p);
      decode(flags, p);
      decode(default_value, p);
      decode(min, p);
      decode(max, p);
      decode(enum_allowed, p);
      decode(desc, p);
      decode(long_desc, p);
      decode(tags, p);
      decode(see_also, p);
      DECODE_FINISH(p);
    }
    void dump(ceph::Formatter *f) const {
      f->dump_string("name", name);
      f->dump_string("type", Option::type_to_str(
		       static_cast<Option::type_t>(type)));
      f->dump_string("level", Option::level_to_str(
		       static_cast<Option::level_t>(level)));
      f->dump_unsigned("flags", flags);
      f->dump_string("default_value", default_value);
      f->dump_string("min", min);
      f->dump_string("max", max);
      f->open_array_section("enum_allowed");
      for (auto& i : enum_allowed) {
	f->dump_string("value", i);
      }
      f->close_section();
      f->dump_string("desc", desc);
      f->dump_string("long_desc", long_desc);
      f->open_array_section("tags");
      for (auto& i : tags) {
	f->dump_string("tag", i);
      }
      f->close_section();
      f->open_array_section("see_also");
      for (auto& i : see_also) {
	f->dump_string("option", i);
      }
      f->close_section();
    }
  };

  class ModuleInfo
  {
    public:
    std::string name;
    bool can_run = true;
    std::string error_string;
    std::map<std::string,ModuleOption> module_options;

    // We do not include the module's `failed` field in the beacon,
    // because it is exposed via health checks.
    void encode(ceph::buffer::list &bl) const {
      ENCODE_START(2, 1, bl);
      encode(name, bl);
      encode(can_run, bl);
      encode(error_string, bl);
      encode(module_options, bl);
      ENCODE_FINISH(bl);
    }

    void decode(ceph::buffer::list::const_iterator &bl) {
      DECODE_START(1, bl);
      decode(name, bl);
      decode(can_run, bl);
      decode(error_string, bl);
      if (struct_v >= 2) {
	decode(module_options, bl);
      }
      DECODE_FINISH(bl);
    }

    bool operator==(const ModuleInfo &rhs) const
    {
      return (name == rhs.name) && (can_run == rhs.can_run);
    }

    void dump(ceph::Formatter *f) const {
      f->open_object_section("module");
      f->dump_string("name", name);
      f->dump_bool("can_run", can_run);
      f->dump_string("error_string", error_string);
      f->open_object_section("module_options");
      for (auto& i : module_options) {
	f->dump_object(i.first.c_str(), i.second);
      }
      f->close_section();
      f->close_section();
    }
  };

  class StandbyInfo
  {
  public:
    uint64_t gid = 0;
    std::string name;
    std::vector<ModuleInfo> available_modules;
    uint64_t mgr_features = 0;

    StandbyInfo(uint64_t gid_, const std::string &name_,
                const std::vector<ModuleInfo>& am,
		uint64_t feat)
      : gid(gid_), name(name_), available_modules(am),
	mgr_features(feat)
    {}

    StandbyInfo() {}

    void encode(ceph::buffer::list& bl) const
    {
      ENCODE_START(4, 1, bl);
      encode(gid, bl);
      encode(name, bl);
      std::set<std::string> old_available_modules;
      for (const auto &i : available_modules) {
        old_available_modules.insert(i.name);
      }
      encode(old_available_modules, bl);  // version 2
      encode(available_modules, bl);  // version 3
      encode(mgr_features, bl); // v4
      ENCODE_FINISH(bl);
    }

    void decode(ceph::buffer::list::const_iterator& p)
    {
      DECODE_START(4, p);
      decode(gid, p);
      decode(name, p);
      if (struct_v >= 2) {
        std::set<std::string> old_available_modules;
        decode(old_available_modules, p);
        if (struct_v < 3) {
          for (const auto &name : old_available_modules) {
            MgrMap::ModuleInfo info;
            info.name = name;
            available_modules.push_back(std::move(info));
          }
        }
      }
      if (struct_v >= 3) {
        decode(available_modules, p);
      }
      if (struct_v >= 4) {
	decode(mgr_features, p);
      }
      DECODE_FINISH(p);
    }

    bool have_module(const std::string &module_name) const
    {
      auto it = std::find_if(available_modules.begin(),
          available_modules.end(),
          [module_name](const ModuleInfo &m) -> bool {
            return m.name == module_name;
          });

      return it != available_modules.end();
    }
  };

  epoch_t epoch = 0;
  epoch_t last_failure_osd_epoch = 0;

  /// global_id of the ceph-mgr instance selected as a leader
  uint64_t active_gid = 0;
  /// server address reported by the leader once it is active
  entity_addrvec_t active_addrs;
  /// whether the nominated leader is active (i.e. has initialized its server)
  bool available = false;
  /// the name (foo in mgr.<foo>) of the active daemon
  std::string active_name;
  /// when the active mgr became active, or we lost the active mgr
  utime_t active_change;
  /// features
  uint64_t active_mgr_features = 0;

  std::vector<entity_addrvec_t> clients; // for blocklist

  std::map<uint64_t, StandbyInfo> standbys;

  // Modules which are enabled
  std::set<std::string> modules;

  // Modules which should always be enabled. A manager daemon will enable
  // modules from the union of this set and the `modules` set above, latest
  // active version.
  std::map<uint32_t, std::set<std::string>> always_on_modules;

  // Modules which are reported to exist
  std::vector<ModuleInfo> available_modules;

  // Map of module name to URI, indicating services exposed by
  // running modules on the active mgr daemon.
  std::map<std::string, std::string> services;

  epoch_t get_epoch() const { return epoch; }
  epoch_t get_last_failure_osd_epoch() const { return last_failure_osd_epoch; }
  const entity_addrvec_t& get_active_addrs() const { return active_addrs; }
  uint64_t get_active_gid() const { return active_gid; }
  bool get_available() const { return available; }
  const std::string &get_active_name() const { return active_name; }
  const utime_t& get_active_change() const { return active_change; }
  int get_num_standby() const { return standbys.size(); }

  bool all_support_module(const std::string& module) {
    if (!have_module(module)) {
      return false;
    }
    for (auto& p : standbys) {
      if (!p.second.have_module(module)) {
	return false;
      }
    }
    return true;
  }

  bool have_module(const std::string &module_name) const
  {
    for (const auto &i : available_modules) {
      if (i.name == module_name) {
        return true;
      }
    }

    return false;
  }

  const ModuleInfo *get_module_info(const std::string &module_name) const {
    for (const auto &i : available_modules) {
      if (i.name == module_name) {
        return &i;
      }
    }
    return nullptr;
  }

  bool can_run_module(const std::string &module_name, std::string *error) const
  {
    for (const auto &i : available_modules) {
      if (i.name == module_name) {
        *error = i.error_string;
        return i.can_run;
      }
    }

    std::ostringstream oss;
    oss << "Module '" << module_name << "' does not exist";
    throw std::logic_error(oss.str());
  }

  bool module_enabled(const std::string& module_name) const
  {
    return modules.find(module_name) != modules.end();
  }

  bool any_supports_module(const std::string& module) const {
    if (have_module(module)) {
      return true;
    }
    for (auto& p : standbys) {
      if (p.second.have_module(module)) {
        return true;
      }
    }
    return false;
  }

  bool have_name(const std::string& name) const {
    if (active_name == name) {
      return true;
    }
    for (auto& p : standbys) {
      if (p.second.name == name) {
	return true;
      }
    }
    return false;
  }

  std::set<std::string> get_all_names() const {
    std::set<std::string> ls;
    if (active_name.size()) {
      ls.insert(active_name);
    }
    for (auto& p : standbys) {
      ls.insert(p.second.name);
    }
    return ls;
  }

  std::set<std::string> get_always_on_modules() const {
    unsigned rnum = to_integer<uint32_t>(ceph_release());
    auto it = always_on_modules.find(rnum);
    if (it == always_on_modules.end()) {
      // ok, try the most recent release
      if (always_on_modules.empty()) {
	return {}; // ugh
      }
      --it;
      if (it->first < rnum) {
	return it->second;
      }
      return {};      // wth
    }
    return it->second;
  }

  void encode(ceph::buffer::list& bl, uint64_t features) const
  {
    if (!HAVE_FEATURE(features, SERVER_NAUTILUS)) {
      ENCODE_START(5, 1, bl);
      encode(epoch, bl);
      encode(active_addrs.legacy_addr(), bl, features);
      encode(active_gid, bl);
      encode(available, bl);
      encode(active_name, bl);
      encode(standbys, bl);
      encode(modules, bl);

      // Pre-version 4 std::string std::list of available modules
      // (replaced by direct encode of ModuleInfo below)
      std::set<std::string> old_available_modules;
      for (const auto &i : available_modules) {
	old_available_modules.insert(i.name);
      }
      encode(old_available_modules, bl);

      encode(services, bl);
      encode(available_modules, bl);
      ENCODE_FINISH(bl);
      return;
    }
    ENCODE_START(11, 6, bl);
    encode(epoch, bl);
    encode(active_addrs, bl, features);
    encode(active_gid, bl);
    encode(available, bl);
    encode(active_name, bl);
    encode(standbys, bl);
    encode(modules, bl);
    encode(services, bl);
    encode(available_modules, bl);
    encode(active_change, bl);
    encode(always_on_modules, bl);
    encode(active_mgr_features, bl);
    encode(last_failure_osd_epoch, bl);
    encode(clients, bl, features);
    ENCODE_FINISH(bl);
    return;
  }

  void decode(ceph::buffer::list::const_iterator& p)
  {
    DECODE_START(11, p);
    decode(epoch, p);
    decode(active_addrs, p);
    decode(active_gid, p);
    decode(available, p);
    decode(active_name, p);
    decode(standbys, p);
    if (struct_v >= 2) {
      decode(modules, p);

      if (struct_v < 6) {
	// Reconstitute ModuleInfos from names
	std::set<std::string> module_name_list;
	decode(module_name_list, p);
	// Only need to unpack this field if we won't have the full
	// MgrMap::ModuleInfo structures added in v4
	if (struct_v < 4) {
	  for (const auto &i : module_name_list) {
	    MgrMap::ModuleInfo info;
	    info.name = i;
	    available_modules.push_back(std::move(info));
	  }
	}
      }
    }
    if (struct_v >= 3) {
      decode(services, p);
    }
    if (struct_v >= 4) {
      decode(available_modules, p);
    }
    if (struct_v >= 7) {
      decode(active_change, p);
    } else {
      active_change = {};
    }
    if (struct_v >= 8) {
      decode(always_on_modules, p);
    }
    if (struct_v >= 9) {
      decode(active_mgr_features, p);
    }
    if (struct_v >= 10) {
      decode(last_failure_osd_epoch, p);
    }
    if (struct_v >= 11) {
      decode(clients, p);
    }
    DECODE_FINISH(p);
  }

  void dump(ceph::Formatter *f) const {
    f->dump_int("epoch", epoch);
    f->dump_int("active_gid", get_active_gid());
    f->dump_string("active_name", get_active_name());
    f->dump_object("active_addrs", active_addrs);
    f->dump_stream("active_addr") << active_addrs.get_legacy_str();
    f->dump_stream("active_change") << active_change;
    f->dump_unsigned("active_mgr_features", active_mgr_features);
    f->dump_bool("available", available);
    f->open_array_section("standbys");
    for (const auto &i : standbys) {
      f->open_object_section("standby");
      f->dump_int("gid", i.second.gid);
      f->dump_string("name", i.second.name);
      f->dump_unsigned("mgr_features", i.second.mgr_features);
      f->open_array_section("available_modules");
      for (const auto& j : i.second.available_modules) {
        j.dump(f);
      }
      f->close_section();
      f->close_section();
    }
    f->close_section();
    f->open_array_section("modules");
    for (auto& i : modules) {
      f->dump_string("module", i);
    }
    f->close_section();
    f->open_array_section("available_modules");
    for (const auto& j : available_modules) {
      j.dump(f);
    }
    f->close_section();

    f->open_object_section("services");
    for (const auto &i : services) {
      f->dump_string(i.first.c_str(), i.second);
    }
    f->close_section();

    f->open_object_section("always_on_modules");
    for (auto& v : always_on_modules) {
      f->open_array_section(ceph_release_name(v.first));
      for (auto& m : v.second) {
        f->dump_string("module", m);
      }
      f->close_section();
    }
    f->dump_int("last_failure_osd_epoch", last_failure_osd_epoch);
    f->open_array_section("active_clients");
    for (const auto &c : clients) {
      f->dump_object("client", c);
    }
    f->close_section();
    f->close_section();
  }

  static void generate_test_instances(std::list<MgrMap*> &l) {
    l.push_back(new MgrMap);
  }

  void print_summary(ceph::Formatter *f, std::ostream *ss) const
  {
    // One or the other, not both
    ceph_assert((ss != nullptr) != (f != nullptr));
    if (f) {
      f->dump_bool("available", available);
      f->dump_int("num_standbys", standbys.size());
      f->open_array_section("modules");
      for (auto& i : modules) {
	f->dump_string("module", i);
      }
      f->close_section();
      f->open_object_section("services");
      for (const auto &i : services) {
	f->dump_string(i.first.c_str(), i.second);
      }
      f->close_section();
    } else {
      utime_t now = ceph_clock_now();
      if (get_active_gid() != 0) {
	*ss << get_active_name();
        if (!available) {
          // If the daemon hasn't gone active yet, indicate that.
          *ss << "(active, starting";
        } else {
          *ss << "(active";
        }
	if (active_change) {
	  *ss << ", since " << utimespan_str(now - active_change);
	}
	*ss << ")";
      } else {
	*ss << "no daemons active";
	if (active_change) {
	  *ss << " (since " << utimespan_str(now - active_change) << ")";
	}
      }
      if (standbys.size()) {
	*ss << ", standbys: ";
	bool first = true;
	for (const auto &i : standbys) {
	  if (!first) {
	    *ss << ", ";
	  }
	  *ss << i.second.name;
	  first = false;
	}
      }
    }
  }

  friend std::ostream& operator<<(std::ostream& out, const MgrMap& m) {
    std::ostringstream ss;
    m.print_summary(nullptr, &ss);
    return out << ss.str();
  }

  friend std::ostream& operator<<(std::ostream& out, const std::vector<ModuleInfo>& mi) {
    for (const auto &i : mi) {
      out << i.name << " ";
    }
    return out;
  }
};

WRITE_CLASS_ENCODER_FEATURES(MgrMap)
WRITE_CLASS_ENCODER(MgrMap::StandbyInfo)
WRITE_CLASS_ENCODER(MgrMap::ModuleInfo);
WRITE_CLASS_ENCODER(MgrMap::ModuleOption);

#endif