]> git.proxmox.com Git - ceph.git/blob - ceph/src/mgr/MgrClient.h
import quincy beta 17.1.0
[ceph.git] / ceph / src / mgr / MgrClient.h
1
2 // -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*-
3 // vim: ts=8 sw=2 smarttab
4 /*
5 * Ceph - scalable distributed file system
6 *
7 * Copyright (C) 2016 John Spray <john.spray@redhat.com>
8 *
9 * This is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU Lesser General Public
11 * License version 2.1, as published by the Free Software
12 * Foundation. See file COPYING.
13 */
14
15 #ifndef MGR_CLIENT_H_
16 #define MGR_CLIENT_H_
17
18 #include <boost/variant.hpp>
19
20 #include "msg/Connection.h"
21 #include "msg/Dispatcher.h"
22 #include "mon/MgrMap.h"
23 #include "mgr/DaemonHealthMetric.h"
24
25 #include "messages/MMgrReport.h"
26 #include "mgr/MetricTypes.h"
27
28 #include "common/perf_counters.h"
29 #include "common/Timer.h"
30 #include "common/CommandTable.h"
31
32 class MMgrMap;
33 class MMgrConfigure;
34 class MMgrClose;
35 class Messenger;
36 class MCommandReply;
37 class MPGStats;
38 class MonMap;
39
40 class MgrSessionState
41 {
42 public:
43 // Which performance counters have we already transmitted schema for?
44 std::set<std::string> declared;
45
46 // Our connection to the mgr
47 ConnectionRef con;
48 };
49
50 class MgrCommand : public CommandOp
51 {
52 public:
53 std::string name;
54 bool tell = false;
55
56 explicit MgrCommand(ceph_tid_t t) : CommandOp(t) {}
57 MgrCommand() : CommandOp() {}
58 };
59
60 class MgrClient : public Dispatcher
61 {
62 protected:
63 CephContext *cct;
64 MgrMap map;
65 Messenger *msgr;
66 MonMap *monmap;
67
68 std::unique_ptr<MgrSessionState> session;
69
70 ceph::mutex lock = ceph::make_mutex("MgrClient::lock");
71 ceph::condition_variable shutdown_cond;
72
73 uint32_t stats_period = 0;
74 uint32_t stats_threshold = 0;
75 SafeTimer timer;
76
77 CommandTable<MgrCommand> command_table;
78
79 using clock_t = ceph::mono_clock;
80 clock_t::time_point last_connect_attempt;
81
82 uint64_t last_config_bl_version = 0;
83
84 Context *report_callback = nullptr;
85 Context *connect_retry_callback = nullptr;
86
87 // If provided, use this to compose an MPGStats to send with
88 // our reports (hook for use by OSD)
89 std::function<MPGStats*()> pgstats_cb;
90 std::function<void(const ConfigPayload &)> set_perf_queries_cb;
91 std::function<MetricPayload()> get_perf_report_cb;
92
93 // for service registration and beacon
94 bool service_daemon = false;
95 bool daemon_dirty_status = false;
96 bool task_dirty_status = false;
97 std::string service_name, daemon_name;
98 std::map<std::string,std::string> daemon_metadata;
99 std::map<std::string,std::string> daemon_status;
100 std::map<std::string,std::string> task_status;
101 std::vector<DaemonHealthMetric> daemon_health_metrics;
102
103 void reconnect();
104 void _send_open();
105
106 // In pre-luminous clusters, the ceph-mgr service is absent or optional,
107 // so we must not block in start_command waiting for it.
108 bool mgr_optional = false;
109
110 public:
111 MgrClient(CephContext *cct_, Messenger *msgr_, MonMap *monmap);
112
113 void set_messenger(Messenger *msgr_) { msgr = msgr_; }
114
115 void init();
116 void shutdown();
117
118 void set_mgr_optional(bool optional_) {mgr_optional = optional_;}
119
120 bool ms_dispatch2(const ceph::ref_t<Message>& m) override;
121 bool ms_handle_reset(Connection *con) override;
122 void ms_handle_remote_reset(Connection *con) override {}
123 bool ms_handle_refused(Connection *con) override;
124
125 bool handle_mgr_map(ceph::ref_t<MMgrMap> m);
126 bool handle_mgr_configure(ceph::ref_t<MMgrConfigure> m);
127 bool handle_mgr_close(ceph::ref_t<MMgrClose> m);
128 bool handle_command_reply(
129 uint64_t tid,
130 ceph::buffer::list& data,
131 const std::string& rs,
132 int r);
133
134 void set_perf_metric_query_cb(
135 std::function<void(const ConfigPayload &)> cb_set,
136 std::function<MetricPayload()> cb_get)
137 {
138 std::lock_guard l(lock);
139 set_perf_queries_cb = cb_set;
140 get_perf_report_cb = cb_get;
141 }
142
143 void send_pgstats();
144 void set_pgstats_cb(std::function<MPGStats*()>&& cb_)
145 {
146 std::lock_guard l(lock);
147 pgstats_cb = std::move(cb_);
148 }
149
150 int start_command(
151 const std::vector<std::string>& cmd, const ceph::buffer::list& inbl,
152 ceph::buffer::list *outbl, std::string *outs,
153 Context *onfinish);
154 int start_tell_command(
155 const std::string& name,
156 const std::vector<std::string>& cmd, const ceph::buffer::list& inbl,
157 ceph::buffer::list *outbl, std::string *outs,
158 Context *onfinish);
159
160 int service_daemon_register(
161 const std::string& service,
162 const std::string& name,
163 const std::map<std::string,std::string>& metadata);
164 int service_daemon_update_status(
165 std::map<std::string,std::string>&& status);
166 int service_daemon_update_task_status(
167 std::map<std::string,std::string> &&task_status);
168 void update_daemon_health(std::vector<DaemonHealthMetric>&& metrics);
169
170 bool is_initialized() const { return initialized; }
171
172 private:
173 void handle_config_payload(const OSDConfigPayload &payload) {
174 if (set_perf_queries_cb) {
175 set_perf_queries_cb(payload);
176 }
177 }
178
179 void handle_config_payload(const MDSConfigPayload &payload) {
180 if (set_perf_queries_cb) {
181 set_perf_queries_cb(payload);
182 }
183 }
184
185 void handle_config_payload(const UnknownConfigPayload &payload) {
186 ceph_abort();
187 }
188
189 struct HandlePayloadVisitor : public boost::static_visitor<void> {
190 MgrClient *mgrc;
191
192 HandlePayloadVisitor(MgrClient *mgrc)
193 : mgrc(mgrc) {
194 }
195
196 template <typename ConfigPayload>
197 inline void operator()(const ConfigPayload &payload) const {
198 mgrc->handle_config_payload(payload);
199 }
200 };
201
202 void _send_stats();
203 void _send_pgstats();
204 void _send_report();
205
206 bool initialized = false;
207 };
208
209 #endif