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 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
;
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;
111 MgrClient(CephContext
*cct_
, Messenger
*msgr_
, MonMap
*monmap
);
113 void set_messenger(Messenger
*msgr_
) { msgr
= msgr_
; }
118 void set_mgr_optional(bool optional_
) {mgr_optional
= optional_
;}
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
;
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(
130 ceph::buffer::list
& data
,
131 const std::string
& rs
,
134 void set_perf_metric_query_cb(
135 std::function
<void(const ConfigPayload
&)> cb_set
,
136 std::function
<MetricPayload()> cb_get
)
138 std::lock_guard
l(lock
);
139 set_perf_queries_cb
= cb_set
;
140 get_perf_report_cb
= cb_get
;
144 void set_pgstats_cb(std::function
<MPGStats
*()>&& cb_
)
146 std::lock_guard
l(lock
);
147 pgstats_cb
= std::move(cb_
);
151 const std::vector
<std::string
>& cmd
, const ceph::buffer::list
& inbl
,
152 ceph::buffer::list
*outbl
, std::string
*outs
,
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
,
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
);
170 bool is_initialized() const { return initialized
; }
173 void handle_config_payload(const OSDConfigPayload
&payload
) {
174 if (set_perf_queries_cb
) {
175 set_perf_queries_cb(payload
);
179 void handle_config_payload(const MDSConfigPayload
&payload
) {
180 if (set_perf_queries_cb
) {
181 set_perf_queries_cb(payload
);
185 void handle_config_payload(const UnknownConfigPayload
&payload
) {
189 struct HandlePayloadVisitor
: public boost::static_visitor
<void> {
192 HandlePayloadVisitor(MgrClient
*mgrc
)
196 template <typename ConfigPayload
>
197 inline void operator()(const ConfigPayload
&payload
) const {
198 mgrc
->handle_config_payload(payload
);
203 void _send_pgstats();
206 bool initialized
= false;