]>
Commit | Line | Data |
---|---|---|
7c673cae FG |
1 | // -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*- |
2 | // vim: ts=8 sw=2 smarttab | |
3 | /* | |
4 | * Ceph - scalable distributed file system | |
5 | * | |
6 | * Copyright (C) 2016 John Spray <john.spray@redhat.com> | |
7 | * | |
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. | |
12 | */ | |
13 | ||
14 | #ifndef DAEMON_STATE_H_ | |
15 | #define DAEMON_STATE_H_ | |
16 | ||
17 | #include <map> | |
18 | #include <string> | |
19 | #include <memory> | |
20 | #include <set> | |
21 | #include <boost/circular_buffer.hpp> | |
22 | ||
23 | #include "common/Mutex.h" | |
24 | ||
25 | #include "msg/msg_types.h" | |
26 | ||
27 | // For PerfCounterType | |
28 | #include "messages/MMgrReport.h" | |
29 | ||
30 | ||
31 | // Unique reference to a daemon within a cluster | |
224ce89b | 32 | typedef std::pair<std::string, std::string> DaemonKey; |
7c673cae FG |
33 | |
34 | // An instance of a performance counter type, within | |
35 | // a particular daemon. | |
36 | class PerfCounterInstance | |
37 | { | |
38 | class DataPoint | |
39 | { | |
40 | public: | |
41 | utime_t t; | |
42 | uint64_t v; | |
43 | DataPoint(utime_t t_, uint64_t v_) | |
44 | : t(t_), v(v_) | |
45 | {} | |
46 | }; | |
47 | ||
48 | boost::circular_buffer<DataPoint> buffer; | |
49 | uint64_t get_current() const; | |
50 | ||
51 | public: | |
52 | const boost::circular_buffer<DataPoint> & get_data() const | |
53 | { | |
54 | return buffer; | |
55 | } | |
56 | void push(utime_t t, uint64_t const &v); | |
57 | PerfCounterInstance() | |
58 | : buffer(20) {} | |
59 | }; | |
60 | ||
61 | ||
62 | typedef std::map<std::string, PerfCounterType> PerfCounterTypes; | |
63 | ||
64 | // Performance counters for one daemon | |
65 | class DaemonPerfCounters | |
66 | { | |
67 | public: | |
68 | // The record of perf stat types, shared between daemons | |
69 | PerfCounterTypes &types; | |
70 | ||
71 | DaemonPerfCounters(PerfCounterTypes &types_) | |
72 | : types(types_) | |
73 | {} | |
74 | ||
75 | std::map<std::string, PerfCounterInstance> instances; | |
76 | ||
77 | // FIXME: this state is really local to DaemonServer, it's part | |
78 | // of the protocol rather than being part of what other classes | |
79 | // mgiht want to read. Maybe have a separate session object | |
80 | // inside DaemonServer instead of stashing session-ish state here? | |
81 | std::set<std::string> declared_types; | |
82 | ||
83 | void update(MMgrReport *report); | |
84 | ||
85 | void clear() | |
86 | { | |
87 | instances.clear(); | |
88 | declared_types.clear(); | |
89 | } | |
90 | }; | |
91 | ||
92 | // The state that we store about one daemon | |
93 | class DaemonState | |
94 | { | |
95 | public: | |
c07f9fc5 FG |
96 | Mutex lock = {"DaemonState::lock"}; |
97 | ||
7c673cae FG |
98 | DaemonKey key; |
99 | ||
100 | // The hostname where daemon was last seen running (extracted | |
101 | // from the metadata) | |
102 | std::string hostname; | |
103 | ||
104 | // The metadata (hostname, version, etc) sent from the daemon | |
105 | std::map<std::string, std::string> metadata; | |
106 | ||
224ce89b WB |
107 | // Ephemeral state |
108 | bool service_daemon = false; | |
109 | utime_t service_status_stamp; | |
110 | std::map<std::string, std::string> service_status; | |
111 | utime_t last_service_beacon; | |
112 | ||
7c673cae FG |
113 | // The perf counters received in MMgrReport messages |
114 | DaemonPerfCounters perf_counters; | |
115 | ||
116 | DaemonState(PerfCounterTypes &types_) | |
117 | : perf_counters(types_) | |
118 | { | |
119 | } | |
120 | }; | |
121 | ||
122 | typedef std::shared_ptr<DaemonState> DaemonStatePtr; | |
123 | typedef std::map<DaemonKey, DaemonStatePtr> DaemonStateCollection; | |
124 | ||
125 | ||
126 | ||
127 | ||
128 | /** | |
129 | * Fuse the collection of per-daemon metadata from Ceph into | |
130 | * a view that can be queried by service type, ID or also | |
131 | * by server (aka fqdn). | |
132 | */ | |
133 | class DaemonStateIndex | |
134 | { | |
135 | private: | |
136 | std::map<std::string, DaemonStateCollection> by_server; | |
137 | DaemonStateCollection all; | |
138 | ||
139 | std::set<DaemonKey> updating; | |
140 | ||
141 | mutable Mutex lock; | |
142 | ||
143 | public: | |
144 | ||
145 | DaemonStateIndex() : lock("DaemonState") {} | |
146 | ||
147 | // FIXME: shouldn't really be public, maybe construct DaemonState | |
148 | // objects internally to avoid this. | |
149 | PerfCounterTypes types; | |
150 | ||
151 | void insert(DaemonStatePtr dm); | |
31f18b77 | 152 | void _erase(const DaemonKey& dmk); |
7c673cae FG |
153 | |
154 | bool exists(const DaemonKey &key) const; | |
155 | DaemonStatePtr get(const DaemonKey &key); | |
156 | DaemonStateCollection get_by_server(const std::string &hostname) const; | |
224ce89b | 157 | DaemonStateCollection get_by_service(const std::string &svc_name) const; |
7c673cae FG |
158 | |
159 | const DaemonStateCollection &get_all() const {return all;} | |
160 | const std::map<std::string, DaemonStateCollection> &get_all_servers() const | |
161 | { | |
162 | return by_server; | |
163 | } | |
164 | ||
165 | void notify_updating(const DaemonKey &k) { updating.insert(k); } | |
166 | void clear_updating(const DaemonKey &k) { updating.erase(k); } | |
167 | bool is_updating(const DaemonKey &k) { return updating.count(k) > 0; } | |
168 | ||
169 | /** | |
170 | * Remove state for all daemons of this type whose names are | |
171 | * not present in `names_exist`. Use this function when you have | |
172 | * a cluster map and want to ensure that anything absent in the map | |
173 | * is also absent in this class. | |
174 | */ | |
224ce89b WB |
175 | void cull(const std::string& svc_name, |
176 | const std::set<std::string>& names_exist); | |
7c673cae FG |
177 | }; |
178 | ||
179 | #endif | |
180 |