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/ceph_mutex.h"
25 #include "mon/MonClient.h"
26 #include "mon/MgrMap.h"
27 #include "mgr/PyModuleRunner.h"
32 * State that is read by all modules running in standby mode
34 class StandbyPyModuleState
36 mutable ceph::mutex lock
= ceph::make_mutex("StandbyPyModuleState::lock");
39 PyModuleConfig
&module_config
;
45 StandbyPyModuleState(PyModuleConfig
&module_config_
, MonClient
&monc_
)
46 : module_config(module_config_
), monc(monc_
)
49 void set_mgr_map(const MgrMap
&mgr_map_
)
51 std::lock_guard
l(lock
);
56 // MonClient does all its own locking so we're happy to hand out
58 MonClient
&get_monc() {return monc
;};
60 template<typename Callback
, typename
...Args
>
61 void with_mgr_map(Callback
&& cb
, Args
&&...args
) const
63 std::lock_guard
l(lock
);
64 std::forward
<Callback
>(cb
)(mgr_map
, std::forward
<Args
>(args
)...);
67 template<typename Callback
, typename
...Args
>
68 auto with_config(Callback
&& cb
, Args
&&... args
) const ->
69 decltype(cb(module_config
, std::forward
<Args
>(args
)...)) {
70 std::lock_guard
l(lock
);
72 return std::forward
<Callback
>(cb
)(module_config
, std::forward
<Args
>(args
)...);
77 class StandbyPyModule
: public PyModuleRunner
79 StandbyPyModuleState
&state
;
84 StandbyPyModuleState
&state_
,
85 const PyModuleRef
&py_module_
,
88 PyModuleRunner(py_module_
, clog_
),
93 bool get_config(const std::string
&key
, std::string
*value
) const;
94 bool get_store(const std::string
&key
, std::string
*value
) const;
95 std::string
get_active_uri() const;
96 entity_addrvec_t
get_myaddrs() const {
97 return state
.get_monc().get_myaddrs();
103 class StandbyPyModules
106 mutable ceph::mutex lock
= ceph::make_mutex("StandbyPyModules::lock");
107 std::map
<std::string
, std::unique_ptr
<StandbyPyModule
>> modules
;
109 StandbyPyModuleState state
;
118 const MgrMap
&mgr_map_
,
119 PyModuleConfig
&module_config
,
124 void start_one(PyModuleRef py_module
);
128 void handle_mgr_map(const MgrMap
&mgr_map
)
130 state
.set_mgr_map(mgr_map
);