2 // -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*-
3 // vim: ts=8 sw=2 smarttab
5 * Ceph - scalable distributed file system
7 * Copyright (C) 2016 John Spray <john.spray@redhat.com>
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.
18 #include <boost/variant.hpp>
20 #include "msg/Connection.h"
21 #include "msg/Dispatcher.h"
22 #include "mon/MgrMap.h"
23 #include "mgr/DaemonHealthMetric.h"
25 #include "messages/MMgrReport.h"
26 #include "mgr/MetricTypes.h"
28 #include "common/perf_counters.h"
29 #include "common/Timer.h"
30 #include "common/CommandTable.h"
43 // Which performance counters have we already transmitted schema for?
44 std::set
<std::string
> declared
;
46 // Our connection to the mgr
50 class MgrCommand
: public CommandOp
56 explicit MgrCommand(ceph_tid_t t
) : CommandOp(t
) {}
57 MgrCommand() : CommandOp() {}
60 class MgrClient
: public Dispatcher
68 std::unique_ptr
<MgrSessionState
> session
;
70 ceph::mutex lock
= ceph::make_mutex("MgrClient::lock");
71 ceph::condition_variable shutdown_cond
;
73 uint32_t stats_period
= 0;
74 uint32_t stats_threshold
= 0;
77 CommandTable
<MgrCommand
> command_table
;
79 using clock_t = ceph::mono_clock
;
80 clock_t::time_point last_connect_attempt
;
82 uint64_t last_config_bl_version
= 0;
84 Context
*report_callback
= nullptr;
85 Context
*connect_retry_callback
= nullptr;
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
;
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
;
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;
113 MgrClient(CephContext
*cct_
, Messenger
*msgr_
, MonMap
*monmap
);
115 void set_messenger(Messenger
*msgr_
) { msgr
= msgr_
; }
120 void set_mgr_optional(bool optional_
) {mgr_optional
= optional_
;}
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
;
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(
132 ceph::buffer::list
& data
,
133 const std::string
& rs
,
136 void set_perf_metric_query_cb(
137 std::function
<void(const ConfigPayload
&)> cb_set
,
138 std::function
<MetricPayload()> cb_get
)
140 std::lock_guard
l(lock
);
141 set_perf_queries_cb
= cb_set
;
142 get_perf_report_cb
= cb_get
;
146 void set_pgstats_cb(std::function
<MPGStats
*()>&& cb_
)
148 std::lock_guard
l(lock
);
149 pgstats_cb
= std::move(cb_
);
153 const std::vector
<std::string
>& cmd
, const ceph::buffer::list
& inbl
,
154 ceph::buffer::list
*outbl
, std::string
*outs
,
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
,
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
);
176 bool is_initialized() const { return initialized
; }
179 void handle_config_payload(const OSDConfigPayload
&payload
) {
180 if (set_perf_queries_cb
) {
181 set_perf_queries_cb(payload
);
185 void handle_config_payload(const MDSConfigPayload
&payload
) {
186 if (set_perf_queries_cb
) {
187 set_perf_queries_cb(payload
);
191 void handle_config_payload(const UnknownConfigPayload
&payload
) {
195 struct HandlePayloadVisitor
: public boost::static_visitor
<void> {
198 HandlePayloadVisitor(MgrClient
*mgrc
)
202 template <typename ConfigPayload
>
203 inline void operator()(const ConfigPayload
&payload
) const {
204 mgrc
->handle_config_payload(payload
);
209 void _send_pgstats();
212 bool initialized
= false;