summaryrefslogtreecommitdiffstats
path: root/src/mgr/ClusterState.h
blob: 7939cd8eb8f0f7911b9f0d6c0aa218646bd67437 (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
// -*- 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) 2014 John Spray <john.spray@inktank.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 CLUSTER_STATE_H_
#define CLUSTER_STATE_H_

#include "mds/FSMap.h"
#include "mon/MgrMap.h"
#include "common/ceph_mutex.h"

#include "osdc/Objecter.h"
#include "mon/MonClient.h"
#include "mon/PGMap.h"
#include "mgr/ServiceMap.h"

class MMgrDigest;
class MMonMgrReport;
class MPGStats;


/**
 * Cluster-scope state (things like cluster maps) as opposed
 * to daemon-level state (things like perf counters and smart)
 */
class ClusterState
{
protected:
  MonClient *monc;
  Objecter *objecter;
  FSMap fsmap;
  ServiceMap servicemap;
  mutable ceph::mutex lock = ceph::make_mutex("ClusterState");

  MgrMap mgr_map;

  std::map<int64_t,unsigned> existing_pools; ///< pools that exist, and pg_num, as of PGMap epoch
  PGMap pg_map;
  PGMap::Incremental pending_inc;

  bufferlist health_json;
  bufferlist mon_status_json;

  class ClusterSocketHook *asok_hook;

public:

  void load_digest(MMgrDigest *m);
  void ingest_pgstats(ceph::ref_t<MPGStats> stats);

  void update_delta_stats();

  ClusterState(MonClient *monc_, Objecter *objecter_, const MgrMap& mgrmap);

  void set_objecter(Objecter *objecter_);
  void set_fsmap(FSMap const &new_fsmap);
  void set_mgr_map(MgrMap const &new_mgrmap);
  void set_service_map(ServiceMap const &new_service_map);

  void notify_osdmap(const OSDMap &osd_map);

  bool have_fsmap() const {
    std::lock_guard l(lock);
    return fsmap.get_epoch() > 0;
  }

  template<typename Callback, typename...Args>
  auto with_servicemap(Callback&& cb, Args&&...args) const
  {
    std::lock_guard l(lock);
    return std::forward<Callback>(cb)(servicemap, std::forward<Args>(args)...);
  }

  template<typename Callback, typename...Args>
  auto with_fsmap(Callback&& cb, Args&&...args) const
  {
    std::lock_guard l(lock);
    return std::forward<Callback>(cb)(fsmap, std::forward<Args>(args)...);
  }

  template<typename Callback, typename...Args>
  auto with_mgrmap(Callback&& cb, Args&&...args) const
  {
    std::lock_guard l(lock);
    return std::forward<Callback>(cb)(mgr_map, std::forward<Args>(args)...);
  }

  template<typename Callback, typename...Args>
  auto with_pgmap(Callback&& cb, Args&&...args) const ->
    decltype(cb(pg_map, std::forward<Args>(args)...))
  {
    std::lock_guard l(lock);
    return std::forward<Callback>(cb)(pg_map, std::forward<Args>(args)...);
  }

  template<typename Callback, typename...Args>
  auto with_mutable_pgmap(Callback&& cb, Args&&...args) ->
    decltype(cb(pg_map, std::forward<Args>(args)...))
  {
    std::lock_guard l(lock);
    return std::forward<Callback>(cb)(pg_map, std::forward<Args>(args)...);
  }

  template<typename... Args>
  auto with_monmap(Args &&... args) const
  {
    std::lock_guard l(lock);
    ceph_assert(monc != nullptr);
    return monc->with_monmap(std::forward<Args>(args)...);
  }

  template<typename... Args>
  auto with_osdmap(Args &&... args) const ->
    decltype(objecter->with_osdmap(std::forward<Args>(args)...))
  {
    ceph_assert(objecter != nullptr);
    return objecter->with_osdmap(std::forward<Args>(args)...);
  }

  // call cb(osdmap, pg_map, ...args) with the appropriate locks
  template <typename Callback, typename ...Args>
  auto with_osdmap_and_pgmap(Callback&& cb, Args&& ...args) const {
    ceph_assert(objecter != nullptr);
    std::lock_guard l(lock);
    return objecter->with_osdmap(
      std::forward<Callback>(cb),
      pg_map,
      std::forward<Args>(args)...);
  }

  template<typename Callback, typename...Args>
  auto with_health(Callback&& cb, Args&&...args) const
  {
    std::lock_guard l(lock);
    return std::forward<Callback>(cb)(health_json, std::forward<Args>(args)...);
  }

  template<typename Callback, typename...Args>
  auto with_mon_status(Callback&& cb, Args&&...args) const
  {
    std::lock_guard l(lock);
    return std::forward<Callback>(cb)(mon_status_json, std::forward<Args>(args)...);
  }

  void final_init();
  void shutdown();
  bool asok_command(std::string_view admin_command,
		    const cmdmap_t& cmdmap,
		    Formatter *f,
		    std::ostream& ss);
};

#endif