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.
21 #include "common/Thread.h"
22 #include "common/Mutex.h"
25 #include "mon/MonClient.h"
26 #include "mon/MgrMap.h"
27 #include "mgr/PyModuleRunner.h"
29 typedef std::map
<std::string
, std::string
> PyModuleConfig
;
32 * State that is read by all modules running in standby mode
34 class StandbyPyModuleState
36 mutable Mutex lock
{"StandbyPyModuleState::lock"};
39 PyModuleConfig config_cache
;
41 mutable Cond config_loaded
;
45 bool is_config_loaded
= false;
47 void set_mgr_map(const MgrMap
&mgr_map_
)
49 Mutex::Locker
l(lock
);
54 void loaded_config(const PyModuleConfig
&config_
)
56 Mutex::Locker
l(lock
);
58 config_cache
= config_
;
59 is_config_loaded
= true;
60 config_loaded
.Signal();
63 template<typename Callback
, typename
...Args
>
64 void with_mgr_map(Callback
&& cb
, Args
&&...args
) const
66 Mutex::Locker
l(lock
);
67 std::forward
<Callback
>(cb
)(mgr_map
, std::forward
<Args
>(args
)...);
70 template<typename Callback
, typename
...Args
>
71 auto with_config(Callback
&& cb
, Args
&&... args
) const ->
72 decltype(cb(config_cache
, std::forward
<Args
>(args
)...)) {
73 Mutex::Locker
l(lock
);
75 if (!is_config_loaded
) {
76 config_loaded
.Wait(lock
);
79 return std::forward
<Callback
>(cb
)(config_cache
, std::forward
<Args
>(args
)...);
84 class StandbyPyModule
: public PyModuleRunner
86 StandbyPyModuleState
&state
;
91 StandbyPyModuleState
&state_
,
92 const std::string
&module_name_
,
94 const SafeThreadState
&pMyThreadState_
,
97 PyModuleRunner(module_name_
, pClass_
, pMyThreadState_
, clog_
),
102 bool get_config(const std::string
&key
, std::string
*value
) const;
103 std::string
get_active_uri() const;
108 class StandbyPyModules
111 mutable Mutex lock
{"StandbyPyModules::lock"};
112 std::map
<std::string
, std::unique_ptr
<StandbyPyModule
>> modules
;
116 StandbyPyModuleState state
;
119 class LoadConfigThread
: public Thread
123 StandbyPyModuleState
*state
;
125 LoadConfigThread(MonClient
*monc_
, StandbyPyModuleState
*state_
)
126 : monc(monc_
), state(state_
)
128 void *entry() override
;
131 LoadConfigThread load_config_thread
;
139 const MgrMap
&mgr_map_
,
140 LogChannelRef clog_
);
142 int start_one(std::string
const &module_name
,
144 const SafeThreadState
&pMyThreadState
);
148 void handle_mgr_map(const MgrMap
&mgr_map
)
150 state
.set_mgr_map(mgr_map
);