]> git.proxmox.com Git - ceph.git/blob - ceph/src/mgr/MgrClient.h
import ceph quincy 17.2.4
[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 bool need_metadata_update = true;
98 std::string service_name, daemon_name;
99 std::map<std::string,std::string> daemon_metadata;
100 std::map<std::string,std::string> daemon_status;
101 std::map<std::string,std::string> task_status;
102 std::vector<DaemonHealthMetric> daemon_health_metrics;
103
104 void reconnect();
105 void _send_open();
106 void _send_update();
107
108 // In pre-luminous clusters, the ceph-mgr service is absent or optional,
109 // so we must not block in start_command waiting for it.
110 bool mgr_optional = false;
111
112 public:
113 MgrClient(CephContext *cct_, Messenger *msgr_, MonMap *monmap);
114
115 void set_messenger(Messenger *msgr_) { msgr = msgr_; }
116
117 void init();
118 void shutdown();
119
120 void set_mgr_optional(bool optional_) {mgr_optional = optional_;}
121
122 bool ms_dispatch2(const ceph::ref_t<Message>& m) override;
123 bool ms_handle_reset(Connection *con) override;
124 void ms_handle_remote_reset(Connection *con) override {}
125 bool ms_handle_refused(Connection *con) override;
126
127 bool handle_mgr_map(ceph::ref_t<MMgrMap> m);
128 bool handle_mgr_configure(ceph::ref_t<MMgrConfigure> m);
129 bool handle_mgr_close(ceph::ref_t<MMgrClose> m);
130 bool handle_command_reply(
131 uint64_t tid,
132 ceph::buffer::list& data,
133 const std::string& rs,
134 int r);
135
136 void set_perf_metric_query_cb(
137 std::function<void(const ConfigPayload &)> cb_set,
138 std::function<MetricPayload()> cb_get)
139 {
140 std::lock_guard l(lock);
141 set_perf_queries_cb = cb_set;
142 get_perf_report_cb = cb_get;
143 }
144
145 void send_pgstats();
146 void set_pgstats_cb(std::function<MPGStats*()>&& cb_)
147 {
148 std::lock_guard l(lock);
149 pgstats_cb = std::move(cb_);
150 }
151
152 int start_command(
153 const std::vector<std::string>& cmd, const ceph::buffer::list& inbl,
154 ceph::buffer::list *outbl, std::string *outs,
155 Context *onfinish);
156 int start_tell_command(
157 const std::string& name,
158 const std::vector<std::string>& cmd, const ceph::buffer::list& inbl,
159 ceph::buffer::list *outbl, std::string *outs,
160 Context *onfinish);
161
162 int update_daemon_metadata(
163 const std::string& service,
164 const std::string& name,
165 const std::map<std::string,std::string>& metadata);
166 int service_daemon_register(
167 const std::string& service,
168 const std::string& name,
169 const std::map<std::string,std::string>& metadata);
170 int service_daemon_update_status(
171 std::map<std::string,std::string>&& status);
172 int service_daemon_update_task_status(
173 std::map<std::string,std::string> &&task_status);
174 void update_daemon_health(std::vector<DaemonHealthMetric>&& metrics);
175
176 bool is_initialized() const { return initialized; }
177
178 private:
179 void handle_config_payload(const OSDConfigPayload &payload) {
180 if (set_perf_queries_cb) {
181 set_perf_queries_cb(payload);
182 }
183 }
184
185 void handle_config_payload(const MDSConfigPayload &payload) {
186 if (set_perf_queries_cb) {
187 set_perf_queries_cb(payload);
188 }
189 }
190
191 void handle_config_payload(const UnknownConfigPayload &payload) {
192 ceph_abort();
193 }
194
195 struct HandlePayloadVisitor : public boost::static_visitor<void> {
196 MgrClient *mgrc;
197
198 HandlePayloadVisitor(MgrClient *mgrc)
199 : mgrc(mgrc) {
200 }
201
202 template <typename ConfigPayload>
203 inline void operator()(const ConfigPayload &payload) const {
204 mgrc->handle_config_payload(payload);
205 }
206 };
207
208 void _send_stats();
209 void _send_pgstats();
210 void _send_report();
211
212 bool initialized = false;
213 };
214
215 #endif