]> git.proxmox.com Git - ceph.git/blob - ceph/src/mgr/StandbyPyModules.h
b434c917c7aec15b984f211b451a4ea26b832113
[ceph.git] / ceph / src / mgr / StandbyPyModules.h
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
16 #include <string>
17 #include <map>
18
19 #include <Python.h>
20
21 #include "common/Thread.h"
22 #include "common/ceph_mutex.h"
23
24 #include "mgr/Gil.h"
25 #include "mon/MonClient.h"
26 #include "mon/MgrMap.h"
27 #include "mgr/PyModuleRunner.h"
28
29 class Finisher;
30
31 /**
32 * State that is read by all modules running in standby mode
33 */
34 class StandbyPyModuleState
35 {
36 mutable ceph::mutex lock = ceph::make_mutex("StandbyPyModuleState::lock");
37
38 MgrMap mgr_map;
39 PyModuleConfig &module_config;
40 MonClient &monc;
41
42 public:
43
44
45 StandbyPyModuleState(PyModuleConfig &module_config_, MonClient &monc_)
46 : module_config(module_config_), monc(monc_)
47 {}
48
49 void set_mgr_map(const MgrMap &mgr_map_)
50 {
51 std::lock_guard l(lock);
52
53 mgr_map = mgr_map_;
54 }
55
56 // MonClient does all its own locking so we're happy to hand out
57 // references.
58 MonClient &get_monc() {return monc;};
59
60 template<typename Callback, typename...Args>
61 void with_mgr_map(Callback&& cb, Args&&...args) const
62 {
63 std::lock_guard l(lock);
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 ->
69 decltype(cb(module_config, std::forward<Args>(args)...)) {
70 std::lock_guard l(lock);
71
72 return std::forward<Callback>(cb)(module_config, std::forward<Args>(args)...);
73 }
74 };
75
76
77 class StandbyPyModule : public PyModuleRunner
78 {
79 StandbyPyModuleState &state;
80
81 public:
82
83 StandbyPyModule(
84 StandbyPyModuleState &state_,
85 const PyModuleRef &py_module_,
86 LogChannelRef clog_)
87 :
88 PyModuleRunner(py_module_, clog_),
89 state(state_)
90 {
91 }
92
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
97 int load();
98 };
99
100 class StandbyPyModules
101 {
102 private:
103 mutable ceph::mutex lock = ceph::make_mutex("StandbyPyModules::lock");
104 std::map<std::string, std::unique_ptr<StandbyPyModule>> modules;
105
106 StandbyPyModuleState state;
107
108 LogChannelRef clog;
109
110 Finisher &finisher;
111
112 public:
113
114 StandbyPyModules(
115 const MgrMap &mgr_map_,
116 PyModuleConfig &module_config,
117 LogChannelRef clog_,
118 MonClient &monc,
119 Finisher &f);
120
121 void start_one(PyModuleRef py_module);
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 };