2017-08-14 10:31:18 +00:00
|
|
|
// -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*-
|
|
|
|
// vim: ts=8 sw=2 smarttab
|
|
|
|
/*
|
|
|
|
* Ceph - scalable distributed file system
|
|
|
|
*
|
|
|
|
* Copyright (C) 2017 John Spray <john.spray@redhat.com>
|
|
|
|
*
|
|
|
|
* This is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU Lesser General Public
|
|
|
|
* License version 2.1, as published by the Free Software
|
|
|
|
* Foundation. See file COPYING.
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
#pragma once
|
|
|
|
|
|
|
|
// Python.h comes first because otherwise it clobbers ceph's assert
|
|
|
|
#include "Python.h"
|
|
|
|
|
2017-08-15 10:53:18 +00:00
|
|
|
#include <string>
|
|
|
|
#include <map>
|
|
|
|
#include <memory>
|
|
|
|
|
|
|
|
#include "common/LogClient.h"
|
|
|
|
|
|
|
|
#include "ActivePyModules.h"
|
2017-08-22 18:42:11 +00:00
|
|
|
#include "StandbyPyModules.h"
|
2017-08-14 10:31:18 +00:00
|
|
|
|
|
|
|
class PyModule
|
|
|
|
{
|
|
|
|
private:
|
|
|
|
const std::string module_name;
|
|
|
|
std::string get_site_packages();
|
2017-10-09 05:40:41 +00:00
|
|
|
int load_subclass_of(const char* class_name, PyObject** py_class);
|
2017-08-14 10:31:18 +00:00
|
|
|
public:
|
2017-10-16 14:51:34 +00:00
|
|
|
SafeThreadState pMyThreadState;
|
2017-08-14 10:31:18 +00:00
|
|
|
PyObject *pClass = nullptr;
|
2017-08-22 18:42:11 +00:00
|
|
|
PyObject *pStandbyClass = nullptr;
|
2017-08-14 10:31:18 +00:00
|
|
|
|
|
|
|
PyModule(const std::string &module_name_)
|
|
|
|
: module_name(module_name_)
|
|
|
|
{
|
|
|
|
}
|
2017-08-22 18:42:11 +00:00
|
|
|
|
2017-10-16 14:51:34 +00:00
|
|
|
~PyModule();
|
|
|
|
|
2017-08-14 10:31:18 +00:00
|
|
|
int load(PyThreadState *pMainThreadState);
|
2017-08-22 18:42:11 +00:00
|
|
|
|
|
|
|
std::string get_name() const {
|
|
|
|
return module_name;
|
|
|
|
}
|
2017-08-14 10:31:18 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* This class is responsible for setting up the python runtime environment
|
|
|
|
* and importing the python modules.
|
|
|
|
*
|
|
|
|
* It is *not* responsible for constructing instances of their BaseMgrModule
|
|
|
|
* subclasses.
|
|
|
|
*/
|
|
|
|
class PyModuleRegistry
|
|
|
|
{
|
|
|
|
private:
|
2017-08-22 18:42:11 +00:00
|
|
|
mutable Mutex lock{"PyModuleRegistry::lock"};
|
|
|
|
|
2017-08-14 10:31:18 +00:00
|
|
|
LogChannelRef clog;
|
|
|
|
|
|
|
|
std::map<std::string, std::unique_ptr<PyModule>> modules;
|
|
|
|
|
|
|
|
std::unique_ptr<ActivePyModules> active_modules;
|
2017-08-22 18:42:11 +00:00
|
|
|
std::unique_ptr<StandbyPyModules> standby_modules;
|
2017-08-14 10:31:18 +00:00
|
|
|
|
2017-10-16 14:51:34 +00:00
|
|
|
PyThreadState *pMainThreadState;
|
2017-08-14 10:31:18 +00:00
|
|
|
|
|
|
|
// We have our own copy of MgrMap, because we are constructed
|
|
|
|
// before ClusterState exists.
|
|
|
|
MgrMap mgr_map;
|
|
|
|
|
|
|
|
public:
|
|
|
|
static std::string config_prefix;
|
|
|
|
|
|
|
|
static void list_modules(std::set<std::string> *modules);
|
|
|
|
|
|
|
|
PyModuleRegistry(LogChannelRef clog_)
|
|
|
|
: clog(clog_)
|
|
|
|
{}
|
|
|
|
|
|
|
|
bool handle_mgr_map(const MgrMap &mgr_map_)
|
|
|
|
{
|
|
|
|
Mutex::Locker l(lock);
|
|
|
|
|
|
|
|
bool modules_changed = mgr_map_.modules != mgr_map.modules;
|
|
|
|
mgr_map = mgr_map_;
|
2017-08-22 18:42:11 +00:00
|
|
|
|
|
|
|
if (standby_modules != nullptr) {
|
|
|
|
standby_modules->handle_mgr_map(mgr_map_);
|
|
|
|
}
|
|
|
|
|
2017-08-14 10:31:18 +00:00
|
|
|
return modules_changed;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool is_initialized() const
|
|
|
|
{
|
|
|
|
return mgr_map.epoch > 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int init(const MgrMap &map);
|
|
|
|
|
|
|
|
void active_start(
|
|
|
|
PyModuleConfig &config_,
|
|
|
|
DaemonStateIndex &ds, ClusterState &cs, MonClient &mc,
|
|
|
|
LogChannelRef clog_, Objecter &objecter_, Client &client_,
|
|
|
|
Finisher &f);
|
2017-08-22 18:42:11 +00:00
|
|
|
void standby_start(
|
|
|
|
MonClient *monc);
|
|
|
|
|
|
|
|
bool is_standby_running() const
|
|
|
|
{
|
|
|
|
return standby_modules != nullptr;
|
|
|
|
}
|
2017-08-14 10:31:18 +00:00
|
|
|
|
2017-08-22 18:42:11 +00:00
|
|
|
void active_shutdown();
|
2017-08-14 10:31:18 +00:00
|
|
|
void shutdown();
|
|
|
|
|
|
|
|
template<typename Callback, typename...Args>
|
|
|
|
void with_active_modules(Callback&& cb, Args&&...args) const
|
|
|
|
{
|
|
|
|
Mutex::Locker l(lock);
|
|
|
|
assert(active_modules != nullptr);
|
|
|
|
|
|
|
|
std::forward<Callback>(cb)(*active_modules, std::forward<Args>(args)...);
|
|
|
|
}
|
|
|
|
|
|
|
|
// FIXME: breaking interface so that I don't have to go rewrite all
|
|
|
|
// the places that call into these (for now)
|
|
|
|
// >>>
|
|
|
|
void notify_all(const std::string ¬ify_type,
|
|
|
|
const std::string ¬ify_id)
|
|
|
|
{
|
|
|
|
if (active_modules) {
|
|
|
|
active_modules->notify_all(notify_type, notify_id);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void notify_all(const LogEntry &log_entry)
|
|
|
|
{
|
|
|
|
if (active_modules) {
|
|
|
|
active_modules->notify_all(log_entry);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
std::vector<MonCommand> get_commands() const
|
|
|
|
{
|
|
|
|
assert(active_modules);
|
|
|
|
return active_modules->get_commands();
|
|
|
|
}
|
|
|
|
std::vector<ModuleCommand> get_py_commands() const
|
|
|
|
{
|
|
|
|
assert(active_modules);
|
|
|
|
return active_modules->get_py_commands();
|
|
|
|
}
|
|
|
|
void get_health_checks(health_check_map_t *checks)
|
|
|
|
{
|
|
|
|
assert(active_modules);
|
|
|
|
active_modules->get_health_checks(checks);
|
|
|
|
}
|
|
|
|
std::map<std::string, std::string> get_services() const
|
|
|
|
{
|
|
|
|
assert(active_modules);
|
|
|
|
return active_modules->get_services();
|
|
|
|
}
|
|
|
|
// <<< (end of ActivePyModules cheeky call-throughs)
|
|
|
|
};
|