]>
Commit | Line | Data |
---|---|---|
3efd9988 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 | #pragma once | |
15 | ||
11fdf7f2 | 16 | #include "PythonCompat.h" |
3efd9988 FG |
17 | |
18 | #include <string> | |
19 | #include <map> | |
20 | ||
21 | #include "common/Thread.h" | |
22 | #include "common/Mutex.h" | |
23 | ||
24 | #include "mgr/Gil.h" | |
25 | #include "mon/MonClient.h" | |
26 | #include "mon/MgrMap.h" | |
27 | #include "mgr/PyModuleRunner.h" | |
28 | ||
11fdf7f2 | 29 | class Finisher; |
3efd9988 FG |
30 | |
31 | /** | |
32 | * State that is read by all modules running in standby mode | |
33 | */ | |
34 | class StandbyPyModuleState | |
35 | { | |
36 | mutable Mutex lock{"StandbyPyModuleState::lock"}; | |
37 | ||
38 | MgrMap mgr_map; | |
11fdf7f2 TL |
39 | PyModuleConfig &module_config; |
40 | MonClient &monc; | |
3efd9988 FG |
41 | |
42 | public: | |
43 | ||
11fdf7f2 TL |
44 | |
45 | StandbyPyModuleState(PyModuleConfig &module_config_, MonClient &monc_) | |
46 | : module_config(module_config_), monc(monc_) | |
47 | {} | |
3efd9988 FG |
48 | |
49 | void set_mgr_map(const MgrMap &mgr_map_) | |
50 | { | |
11fdf7f2 | 51 | std::lock_guard l(lock); |
3efd9988 FG |
52 | |
53 | mgr_map = mgr_map_; | |
54 | } | |
55 | ||
11fdf7f2 TL |
56 | // MonClient does all its own locking so we're happy to hand out |
57 | // references. | |
58 | MonClient &get_monc() {return monc;}; | |
3efd9988 FG |
59 | |
60 | template<typename Callback, typename...Args> | |
61 | void with_mgr_map(Callback&& cb, Args&&...args) const | |
62 | { | |
11fdf7f2 | 63 | std::lock_guard l(lock); |
3efd9988 FG |
64 | std::forward<Callback>(cb)(mgr_map, std::forward<Args>(args)...); |
65 | } | |
66 | ||
67 | template<typename Callback, typename...Args> | |
68 | auto with_config(Callback&& cb, Args&&... args) const -> | |
11fdf7f2 TL |
69 | decltype(cb(module_config, std::forward<Args>(args)...)) { |
70 | std::lock_guard l(lock); | |
3efd9988 | 71 | |
11fdf7f2 | 72 | return std::forward<Callback>(cb)(module_config, std::forward<Args>(args)...); |
3efd9988 FG |
73 | } |
74 | }; | |
75 | ||
76 | ||
77 | class StandbyPyModule : public PyModuleRunner | |
78 | { | |
79 | StandbyPyModuleState &state; | |
80 | ||
81 | public: | |
82 | ||
83 | StandbyPyModule( | |
84 | StandbyPyModuleState &state_, | |
11fdf7f2 | 85 | const PyModuleRef &py_module_, |
b32b8144 | 86 | LogChannelRef clog_) |
3efd9988 | 87 | : |
11fdf7f2 | 88 | PyModuleRunner(py_module_, clog_), |
3efd9988 FG |
89 | state(state_) |
90 | { | |
91 | } | |
92 | ||
93 | bool get_config(const std::string &key, std::string *value) const; | |
11fdf7f2 | 94 | bool get_store(const std::string &key, std::string *value) const; |
3efd9988 FG |
95 | std::string get_active_uri() const; |
96 | ||
97 | int load(); | |
98 | }; | |
99 | ||
100 | class StandbyPyModules | |
101 | { | |
102 | private: | |
103 | mutable Mutex lock{"StandbyPyModules::lock"}; | |
104 | std::map<std::string, std::unique_ptr<StandbyPyModule>> modules; | |
105 | ||
3efd9988 FG |
106 | StandbyPyModuleState state; |
107 | ||
b32b8144 FG |
108 | LogChannelRef clog; |
109 | ||
11fdf7f2 TL |
110 | Finisher &finisher; |
111 | ||
3efd9988 FG |
112 | public: |
113 | ||
114 | StandbyPyModules( | |
b32b8144 | 115 | const MgrMap &mgr_map_, |
11fdf7f2 TL |
116 | PyModuleConfig &module_config, |
117 | LogChannelRef clog_, | |
118 | MonClient &monc, | |
119 | Finisher &f); | |
3efd9988 | 120 | |
11fdf7f2 | 121 | void start_one(PyModuleRef py_module); |
3efd9988 FG |
122 | |
123 | void shutdown(); | |
124 | ||
125 | void handle_mgr_map(const MgrMap &mgr_map) | |
126 | { | |
127 | state.set_mgr_map(mgr_map); | |
128 | } | |
129 | ||
130 | }; |