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_)
96 PyModuleRunner(module_name_, pClass_, pMyThreadState_),
101 bool get_config(const std::string &key, std::string *value) const;
102 std::string get_active_uri() const;
107 class StandbyPyModules
110 mutable Mutex lock{"StandbyPyModules::lock"};
111 std::map<std::string, std::unique_ptr<StandbyPyModule>> modules;
115 StandbyPyModuleState state;
118 class LoadConfigThread : public Thread
122 StandbyPyModuleState *state;
124 LoadConfigThread(MonClient *monc_, StandbyPyModuleState *state_)
125 : monc(monc_), state(state_)
127 void *entry() override;
130 LoadConfigThread load_config_thread;
136 const MgrMap &mgr_map_);
138 int start_one(std::string const &module_name,
140 const SafeThreadState &pMyThreadState);
144 void handle_mgr_map(const MgrMap &mgr_map)
146 state.set_mgr_map(mgr_map);