1 // -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*-
2 // vim: ts=8 sw=2 smarttab
4 * Ceph - scalable distributed file system
6 * Copyright (C) 2016 John Spray <john.spray@redhat.com>
8 * This is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License version 2.1, as published by the Free Software
11 * Foundation. See file COPYING.
14 #ifndef DAEMON_STATE_H_
15 #define DAEMON_STATE_H_
21 #include <boost/circular_buffer.hpp>
23 #include "include/str_map.h"
25 #include "msg/msg_types.h"
27 // For PerfCounterType
28 #include "messages/MMgrReport.h"
29 #include "DaemonKey.h"
35 // An instance of a performance counter type, within
36 // a particular daemon.
37 class PerfCounterInstance
44 DataPoint(utime_t t_
, uint64_t v_
)
55 AvgDataPoint(utime_t t_
, uint64_t s_
, uint64_t c_
)
60 boost::circular_buffer
<DataPoint
> buffer
;
61 boost::circular_buffer
<AvgDataPoint
> avg_buffer
;
63 uint64_t get_current() const;
66 const boost::circular_buffer
<DataPoint
> & get_data() const
70 const DataPoint
& get_latest_data() const
74 const boost::circular_buffer
<AvgDataPoint
> & get_data_avg() const
78 const AvgDataPoint
& get_latest_data_avg() const
80 return avg_buffer
.back();
82 void push(utime_t t
, uint64_t const &v
);
83 void push_avg(utime_t t
, uint64_t const &s
, uint64_t const &c
);
85 PerfCounterInstance(enum perfcounter_type_d type
)
87 if (type
& PERFCOUNTER_LONGRUNAVG
)
88 avg_buffer
= boost::circular_buffer
<AvgDataPoint
>(20);
90 buffer
= boost::circular_buffer
<DataPoint
>(20);
95 typedef std::map
<std::string
, PerfCounterType
> PerfCounterTypes
;
97 // Performance counters for one daemon
98 class DaemonPerfCounters
101 // The record of perf stat types, shared between daemons
102 PerfCounterTypes
&types
;
104 explicit DaemonPerfCounters(PerfCounterTypes
&types_
)
108 std::map
<std::string
, PerfCounterInstance
> instances
;
110 void update(const MMgrReport
& report
);
118 // The state that we store about one daemon
122 ceph::mutex lock
= ceph::make_mutex("DaemonState::lock");
126 // The hostname where daemon was last seen running (extracted
127 // from the metadata)
128 std::string hostname
;
130 // The metadata (hostname, version, etc) sent from the daemon
131 std::map
<std::string
, std::string
> metadata
;
133 /// device ids -> devname, derived from metadata[device_ids]
134 std::map
<std::string
,std::string
> devices
;
136 /// device ids -> by-path, derived from metadata[device_ids]
137 std::map
<std::string
,std::string
> devices_bypath
;
139 // TODO: this can be generalized to other daemons
140 std::vector
<DaemonHealthMetric
> daemon_health_metrics
;
143 bool service_daemon
= false;
144 utime_t service_status_stamp
;
145 std::map
<std::string
, std::string
> service_status
;
146 utime_t last_service_beacon
;
149 std::map
<std::string
,std::map
<int32_t,std::string
>> config
;
151 // mon config values we failed to set
152 std::map
<std::string
,std::string
> ignored_mon_config
;
154 // compiled-in config defaults (rarely used, so we leave them encoded!)
155 bufferlist config_defaults_bl
;
156 std::map
<std::string
,std::string
> config_defaults
;
158 // The perf counters received in MMgrReport messages
159 DaemonPerfCounters perf_counters
;
161 explicit DaemonState(PerfCounterTypes
&types_
)
162 : perf_counters(types_
)
165 void set_metadata(const std::map
<std::string
,std::string
>& m
);
166 const std::map
<std::string
,std::string
>& _get_config_defaults();
169 typedef std::shared_ptr
<DaemonState
> DaemonStatePtr
;
170 typedef std::map
<DaemonKey
, DaemonStatePtr
> DaemonStateCollection
;
173 struct DeviceState
: public RefCountedObject
176 /// (server,devname,path)
177 std::set
<std::tuple
<std::string
,std::string
,std::string
>> attachments
;
178 std::set
<DaemonKey
> daemons
;
180 std::map
<std::string
,std::string
> metadata
; ///< persistent metadata
182 std::pair
<utime_t
,utime_t
> life_expectancy
; ///< when device failure is expected
183 utime_t life_expectancy_stamp
; ///< when life expectency was recorded
184 float wear_level
= -1; ///< SSD wear level (negative if unknown)
186 void set_metadata(std::map
<std::string
,std::string
>&& m
);
188 void set_life_expectancy(utime_t from
, utime_t to
, utime_t now
);
189 void rm_life_expectancy();
191 void set_wear_level(float wear
);
193 std::string
get_life_expectancy_str(utime_t now
) const;
195 /// true of we can be safely forgotten/removed from memory
197 return daemons
.empty() && metadata
.empty();
200 void dump(Formatter
*f
) const;
201 void print(std::ostream
& out
) const;
204 FRIEND_MAKE_REF(DeviceState
);
205 DeviceState(const std::string
& n
) : devid(n
) {}
209 * Fuse the collection of per-daemon metadata from Ceph into
210 * a view that can be queried by service type, ID or also
211 * by server (aka fqdn).
213 class DaemonStateIndex
216 mutable ceph::shared_mutex lock
=
217 ceph::make_shared_mutex("DaemonStateIndex", true, true, true);
219 std::map
<std::string
, DaemonStateCollection
> by_server
;
220 DaemonStateCollection all
;
221 std::set
<DaemonKey
> updating
;
223 std::map
<std::string
,ceph::ref_t
<DeviceState
>> devices
;
225 void _erase(const DaemonKey
& dmk
);
227 ceph::ref_t
<DeviceState
> _get_or_create_device(const std::string
& dev
) {
228 auto em
= devices
.try_emplace(dev
, nullptr);
229 auto& d
= em
.first
->second
;
231 d
= ceph::make_ref
<DeviceState
>(dev
);
235 void _erase_device(const ceph::ref_t
<DeviceState
>& d
) {
236 devices
.erase(d
->devid
);
240 DaemonStateIndex() {}
242 // FIXME: shouldn't really be public, maybe construct DaemonState
243 // objects internally to avoid this.
244 PerfCounterTypes types
;
246 void insert(DaemonStatePtr dm
);
247 void _insert(DaemonStatePtr dm
);
248 bool exists(const DaemonKey
&key
) const;
249 DaemonStatePtr
get(const DaemonKey
&key
);
250 void rm(const DaemonKey
&key
);
251 void _rm(const DaemonKey
&key
);
253 // Note that these return by value rather than reference to avoid
254 // callers needing to stay in lock while using result. Callers must
255 // still take the individual DaemonState::lock on each entry though.
256 DaemonStateCollection
get_by_server(const std::string
&hostname
) const;
257 DaemonStateCollection
get_by_service(const std::string
&svc_name
) const;
258 DaemonStateCollection
get_all() const {return all
;}
260 template<typename Callback
, typename
...Args
>
261 auto with_daemons_by_server(Callback
&& cb
, Args
&&... args
) const ->
262 decltype(cb(by_server
, std::forward
<Args
>(args
)...)) {
263 std::shared_lock l
{lock
};
265 return std::forward
<Callback
>(cb
)(by_server
, std::forward
<Args
>(args
)...);
268 template<typename Callback
, typename
...Args
>
269 bool with_device(const std::string
& dev
,
270 Callback
&& cb
, Args
&&... args
) const {
271 std::shared_lock l
{lock
};
272 auto p
= devices
.find(dev
);
273 if (p
== devices
.end()) {
276 std::forward
<Callback
>(cb
)(*p
->second
, std::forward
<Args
>(args
)...);
280 template<typename Callback
, typename
...Args
>
281 bool with_device_write(const std::string
& dev
,
282 Callback
&& cb
, Args
&&... args
) {
283 std::unique_lock l
{lock
};
284 auto p
= devices
.find(dev
);
285 if (p
== devices
.end()) {
288 std::forward
<Callback
>(cb
)(*p
->second
, std::forward
<Args
>(args
)...);
289 if (p
->second
->empty()) {
290 _erase_device(p
->second
);
295 template<typename Callback
, typename
...Args
>
296 void with_device_create(const std::string
& dev
,
297 Callback
&& cb
, Args
&&... args
) {
298 std::unique_lock l
{lock
};
299 auto d
= _get_or_create_device(dev
);
300 std::forward
<Callback
>(cb
)(*d
, std::forward
<Args
>(args
)...);
303 template<typename Callback
, typename
...Args
>
304 void with_devices(Callback
&& cb
, Args
&&... args
) const {
305 std::shared_lock l
{lock
};
306 for (auto& i
: devices
) {
307 std::forward
<Callback
>(cb
)(*i
.second
, std::forward
<Args
>(args
)...);
311 template<typename CallbackInitial
, typename Callback
, typename
...Args
>
312 void with_devices2(CallbackInitial
&& cbi
, // with lock taken
313 Callback
&& cb
, // for each device
314 Args
&&... args
) const {
315 std::shared_lock l
{lock
};
317 for (auto& i
: devices
) {
318 std::forward
<Callback
>(cb
)(*i
.second
, std::forward
<Args
>(args
)...);
322 void list_devids_by_server(const std::string
& server
,
323 std::set
<std::string
> *ls
) {
324 auto m
= get_by_server(server
);
326 std::lock_guard
l(i
.second
->lock
);
327 for (auto& j
: i
.second
->devices
) {
333 void notify_updating(const DaemonKey
&k
) {
334 std::unique_lock l
{lock
};
337 void clear_updating(const DaemonKey
&k
) {
338 std::unique_lock l
{lock
};
341 bool is_updating(const DaemonKey
&k
) {
342 std::shared_lock l
{lock
};
343 return updating
.count(k
) > 0;
346 void update_metadata(DaemonStatePtr state
,
347 const std::map
<std::string
,std::string
>& meta
) {
348 // remove and re-insert in case the device metadata changed
349 std::unique_lock l
{lock
};
352 std::lock_guard l2
{state
->lock
};
353 state
->set_metadata(meta
);
359 * Remove state for all daemons of this type whose names are
360 * not present in `names_exist`. Use this function when you have
361 * a cluster map and want to ensure that anything absent in the map
362 * is also absent in this class.
364 void cull(const std::string
& svc_name
,
365 const std::set
<std::string
>& names_exist
);
366 void cull_services(const std::set
<std::string
>& types_exist
);