summaryrefslogtreecommitdiffstats
path: root/src/mgr/MgrClient.h
blob: 1668d8da0fbc29fc64894e21b0fa5aefc58ca27b (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
// -*- 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_CLIENT_H_
#define MGR_CLIENT_H_

#include <boost/variant.hpp>

#include "msg/Connection.h"
#include "msg/Dispatcher.h"
#include "mon/MgrMap.h"
#include "mgr/DaemonHealthMetric.h"

#include "messages/MMgrReport.h"
#include "mgr/MetricTypes.h"

#include "common/perf_counters.h"
#include "common/Timer.h"
#include "common/CommandTable.h"

class MMgrMap;
class MMgrConfigure;
class MMgrClose;
class Messenger;
class MCommandReply;
class MPGStats;
class MonMap;

class MgrSessionState
{
  public:
  // Which performance counters have we already transmitted schema for?
  std::set<std::string> declared;

  // Our connection to the mgr
  ConnectionRef con;
};

class MgrCommand : public CommandOp
{
  public:
  std::string name;
  bool tell = false;

  explicit MgrCommand(ceph_tid_t t) : CommandOp(t) {}
  MgrCommand() : CommandOp() {}
};

class MgrClient : public Dispatcher
{
protected:
  CephContext *cct;
  MgrMap map;
  Messenger *msgr;
  MonMap *monmap;

  std::unique_ptr<MgrSessionState> session;

  ceph::mutex lock = ceph::make_mutex("MgrClient::lock");
  ceph::condition_variable shutdown_cond;

  uint32_t stats_period = 0;
  uint32_t stats_threshold = 0;
  SafeTimer timer;

  CommandTable<MgrCommand> command_table;

  using clock_t = ceph::real_clock;
  clock_t::time_point last_connect_attempt;

  uint64_t last_config_bl_version = 0;

  Context *report_callback = nullptr;
  Context *connect_retry_callback = nullptr;

  // If provided, use this to compose an MPGStats to send with
  // our reports (hook for use by OSD)
  std::function<MPGStats*()> pgstats_cb;
  std::function<void(const ConfigPayload &)> set_perf_queries_cb;
  std::function<MetricPayload()> get_perf_report_cb;

  // for service registration and beacon
  bool service_daemon = false;
  bool daemon_dirty_status = false;
  bool task_dirty_status = false;
  bool need_metadata_update = true;
  std::string service_name, daemon_name;
  std::map<std::string,std::string> daemon_metadata;
  std::map<std::string,std::string> daemon_status;
  std::map<std::string,std::string> task_status;
  std::vector<DaemonHealthMetric> daemon_health_metrics;

  void reconnect();
  void _send_open();
  void _send_update();

  // In pre-luminous clusters, the ceph-mgr service is absent or optional,
  // so we must not block in start_command waiting for it.
  bool mgr_optional = false;

public:
  MgrClient(CephContext *cct_, Messenger *msgr_, MonMap *monmap);

  void set_messenger(Messenger *msgr_) { msgr = msgr_; }

  void init();
  void shutdown();

  void set_mgr_optional(bool optional_) {mgr_optional = optional_;}

  bool ms_dispatch2(const ceph::ref_t<Message>& m) override;
  bool ms_handle_reset(Connection *con) override;
  void ms_handle_remote_reset(Connection *con) override {}
  bool ms_handle_refused(Connection *con) override;

  bool handle_mgr_map(ceph::ref_t<MMgrMap> m);
  bool handle_mgr_configure(ceph::ref_t<MMgrConfigure> m);
  bool handle_mgr_close(ceph::ref_t<MMgrClose> m);
  bool handle_command_reply(
    uint64_t tid,
    ceph::buffer::list& data,
    const std::string& rs,
    int r);

  void set_perf_metric_query_cb(
    std::function<void(const ConfigPayload &)> cb_set,
    std::function<MetricPayload()> cb_get)
  {
      std::lock_guard l(lock);
      set_perf_queries_cb = cb_set;
      get_perf_report_cb = cb_get;
  }

  void send_pgstats();
  void set_pgstats_cb(std::function<MPGStats*()>&& cb_)
  {
    std::lock_guard l(lock);
    pgstats_cb = std::move(cb_);
  }

  int start_command(
    const std::vector<std::string>& cmd, const ceph::buffer::list& inbl,
    ceph::buffer::list *outbl, std::string *outs,
    Context *onfinish);
  int start_tell_command(
    const std::string& name,
    const std::vector<std::string>& cmd, const ceph::buffer::list& inbl,
    ceph::buffer::list *outbl, std::string *outs,
    Context *onfinish);

  int update_daemon_metadata(
    const std::string& service,
    const std::string& name,
    const std::map<std::string,std::string>& metadata);
  int service_daemon_register(
    const std::string& service,
    const std::string& name,
    const std::map<std::string,std::string>& metadata);
  int service_daemon_update_status(
    std::map<std::string,std::string>&& status);
  int service_daemon_update_task_status(
    std::map<std::string,std::string> &&task_status);
  void update_daemon_health(std::vector<DaemonHealthMetric>&& metrics);

  bool is_initialized() const { return initialized; }

private:
  void handle_config_payload(const OSDConfigPayload &payload) {
    if (set_perf_queries_cb) {
      set_perf_queries_cb(payload);
    }
  }

  void handle_config_payload(const MDSConfigPayload &payload) {
    if (set_perf_queries_cb) {
      set_perf_queries_cb(payload);
    }
  }

  void handle_config_payload(const UnknownConfigPayload &payload) {
    ceph_abort();
  }

  struct HandlePayloadVisitor : public boost::static_visitor<void> {
    MgrClient *mgrc;

    HandlePayloadVisitor(MgrClient *mgrc)
      : mgrc(mgrc) {
    }

    template <typename ConfigPayload>
    inline void operator()(const ConfigPayload &payload) const {
      mgrc->handle_config_payload(payload);
    }
  };

  void _send_stats();
  void _send_pgstats();
  void _send_report();

  bool initialized = false;
};

#endif