1
0
Fork 0
mirror of https://git.rwth-aachen.de/acs/public/villas/node/ synced 2025-03-16 00:00:02 +01:00
VILLASnode/lib/super_node.cpp

550 lines
12 KiB
C++
Raw Normal View History

/** The super node object holding the state of the application.
*
* @author Steffen Vogel <stvogel@eonerc.rwth-aachen.de>
2018-08-20 18:39:04 +02:00
* @copyright 2017-2018, Institute for Automation of Complex Power Systems, EONERC
2017-04-27 12:56:43 +02:00
* @license GNU General Public License (version 3)
*
* VILLASnode
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* any later version.
*
2017-04-27 12:56:43 +02:00
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
2017-04-27 12:56:43 +02:00
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*********************************************************************************/
#include <stdlib.h>
#include <string.h>
#include <libgen.h>
#include <unistd.h>
2018-10-20 14:20:06 +02:00
#include <villas/super_node.hpp>
2017-12-09 02:19:28 +08:00
#include <villas/node.h>
#include <villas/path.h>
#include <villas/utils.h>
#include <villas/list.h>
#include <villas/hook.h>
#include <villas/advio.h>
#include <villas/plugin.h>
#include <villas/memory.h>
#include <villas/config_helper.h>
2018-10-20 14:20:06 +02:00
#include <villas/log.hpp>
#include <villas/exceptions.hpp>
#include <villas/kernel/rt.hpp>
2018-10-20 14:20:06 +02:00
using namespace villas;
2018-07-03 21:51:48 +02:00
using namespace villas::node;
2018-10-20 14:20:06 +02:00
Logger SuperNode::logger = logging.get("super_node");
2018-07-03 21:51:48 +02:00
SuperNode::SuperNode() :
state(STATE_INITIALIZED),
priority(0),
affinity(0),
hugepages(DEFAULT_NR_HUGEPAGES),
2018-10-20 14:20:06 +02:00
stats(0),
api(this),
web(&api),
json(nullptr)
{
2018-07-03 21:51:48 +02:00
list_init(&nodes);
list_init(&paths);
list_init(&plugins);
2018-08-17 12:41:10 +02:00
2018-10-20 14:20:06 +02:00
char hname[128];
gethostname(hname, 128);
name = hname;
}
2018-10-20 14:20:06 +02:00
int SuperNode::parseUri(const std::string &u)
{
json_error_t err;
2017-09-02 14:20:38 +02:00
2018-10-20 14:20:06 +02:00
FILE *f;
AFILE *af;
2018-10-20 14:20:06 +02:00
/* Via stdin */
if (u == "-") {
logger->info("Reading configuration from standard input");
2018-10-20 14:20:06 +02:00
af = nullptr;
f = stdin;
}
else {
logger->info("Reading configuration from URI: {}", u);
2018-10-20 14:20:06 +02:00
af = afopen(u.c_str(), "r");
if (!af)
throw RuntimeError("Failed to open configuration from: {}", u);
2018-10-20 14:20:06 +02:00
f = af->file;
}
2018-10-20 14:20:06 +02:00
/* Parse config */
json = json_loadf(f, 0, &err);
if (json == nullptr) {
2018-06-25 06:00:34 +02:00
#ifdef LIBCONFIG_FOUND
2018-10-20 14:20:06 +02:00
int ret;
2018-10-20 14:20:06 +02:00
config_t cfg;
config_setting_t *json_root = nullptr;
2018-10-20 14:20:06 +02:00
logger->warn("Failed to parse JSON configuration. Re-trying with old libconfig format.");
logger->warn(" Please consider migrating to the new format using the 'conf2json' command.");
2018-10-20 14:20:06 +02:00
config_init(&cfg);
config_set_auto_convert(&cfg, 1);
2018-10-20 14:20:06 +02:00
/* Setup libconfig include path.
* This is only supported for local files */
if (access(u.c_str(), F_OK) != -1) {
char *cpy = strdup(u.c_str());
2018-10-20 14:20:06 +02:00
config_set_include_dir(&cfg, dirname(cpy));
2017-08-10 17:57:00 +02:00
2018-10-20 14:20:06 +02:00
free(cpy);
}
2018-10-20 14:20:06 +02:00
if (af)
arewind(af);
else
rewind(f);
ret = config_read(&cfg, f);
if (ret != CONFIG_TRUE) {
logger->warn("conf: {} in {}:{}", config_error_text(&cfg), u.c_str(), config_error_line(&cfg));
logger->warn("json: {} in {}:{}:{}", err.text, err.source, err.line, err.column);
logger->error("Failed to parse configuration");
killme(SIGABRT);
}
2018-10-20 14:20:06 +02:00
json_root = config_root_setting(&cfg);
2018-10-20 14:20:06 +02:00
json = config_to_json(json_root);
if (json == nullptr)
throw RuntimeError("Failed to convert JSON to configuration file");
2018-10-20 14:20:06 +02:00
config_destroy(&cfg);
#else
throw JsonError(err, "Failed to parse configuration file");
2018-06-25 06:00:34 +02:00
#endif /* LIBCONFIG_FOUND */
}
2018-10-20 14:20:06 +02:00
/* Close configuration file */
if (af)
afclose(af);
else if (f != stdin)
fclose(f);
2017-09-02 14:20:38 +02:00
2018-10-20 14:20:06 +02:00
uri = u;
2018-10-20 14:20:06 +02:00
return parseJson(json);
return 0;
}
2018-07-03 21:51:48 +02:00
int SuperNode::parseJson(json_t *j)
{
int ret;
2018-08-27 11:21:57 +02:00
const char *nme = nullptr;
2018-07-03 21:51:48 +02:00
assert(state != STATE_STARTED);
2018-08-27 11:21:57 +02:00
json_t *json_nodes = nullptr;
json_t *json_paths = nullptr;
json_t *json_plugins = nullptr;
json_t *json_logging = nullptr;
json_t *json_web = nullptr;
json_error_t err;
2018-07-03 21:51:48 +02:00
ret = json_unpack_ex(j, &err, 0, "{ s?: o, s?: o, s?: o, s?: o, s?: o, s?: i, s?: i, s?: i, s?: F, s?: s }",
2017-10-16 08:08:35 +02:00
"http", &json_web,
"logging", &json_logging,
"plugins", &json_plugins,
"nodes", &json_nodes,
"paths", &json_paths,
2018-07-03 21:51:48 +02:00
"hugepages", &hugepages,
"affinity", &affinity,
"priority", &priority,
"stats", &stats,
"name", &nme
);
if (ret)
throw JsonError(err, "Failed to parse global configuration");
2018-10-20 14:20:06 +02:00
if (nme)
name = nme;
#ifdef WITH_WEB
2017-10-16 08:08:35 +02:00
if (json_web)
2018-10-20 14:20:06 +02:00
web.parse(json_web);
#endif /* WITH_WEB */
2017-10-16 08:08:35 +02:00
if (json_logging)
2018-10-20 14:20:06 +02:00
logging.parse(json_logging);
/* Parse plugins */
2017-10-16 08:08:35 +02:00
if (json_plugins) {
if (!json_is_array(json_plugins))
throw ConfigError(json_plugins, "node-config-plugins", "Setting 'plugins' must be a list of strings");
2018-08-07 18:40:32 +02:00
size_t i;
2017-10-16 08:08:35 +02:00
json_t *json_plugin;
2018-08-07 18:40:32 +02:00
json_array_foreach(json_plugins, i, json_plugin) {
2018-10-20 14:20:06 +02:00
auto *p = (plugin *) alloc(sizeof(plugin));
ret = plugin_init(p);
2017-03-13 23:51:38 -03:00
if (ret)
throw RuntimeError("Failed to initialize plugin");
2017-10-16 08:08:35 +02:00
ret = plugin_parse(p, json_plugin);
if (ret)
throw RuntimeError("Failed to parse plugin");
2018-07-03 21:51:48 +02:00
list_push(&plugins, p);
}
}
/* Parse nodes */
2017-10-16 08:08:35 +02:00
if (json_nodes) {
if (!json_is_object(json_nodes))
throw ConfigError(json_nodes, "node-config-nodes", "Setting 'nodes' must be a group with node name => group mappings.");
const char *name;
2017-10-16 08:08:35 +02:00
json_t *json_node;
json_object_foreach(json_nodes, name, json_node) {
struct node_type *nt;
const char *type;
2017-10-16 08:08:35 +02:00
ret = json_unpack_ex(json_node, &err, 0, "{ s: s }", "type", &type);
if (ret)
throw JsonError(err, "Failed to parse node");
2017-03-13 23:51:38 -03:00
nt = node_type_lookup(type);
if (!nt)
throw RuntimeError("Invalid node type: {}", type);
2017-03-13 23:51:38 -03:00
2018-10-20 14:20:06 +02:00
auto *n = (struct node *) alloc(sizeof(struct node));
ret = node_init(n, nt);
if (ret)
throw RuntimeError("Failed to initialize node");
2017-10-16 08:08:35 +02:00
ret = node_parse(n, json_node, name);
if (ret)
throw RuntimeError("Failed to parse node");
2018-07-03 21:51:48 +02:00
list_push(&nodes, n);
}
}
/* Parse paths */
2017-10-16 08:08:35 +02:00
if (json_paths) {
if (!json_is_array(json_paths))
2018-10-20 14:20:06 +02:00
logger->warn("Setting 'paths' must be a list of objects");
2018-08-07 18:40:32 +02:00
size_t i;
2017-10-16 08:08:35 +02:00
json_t *json_path;
2018-08-07 18:40:32 +02:00
json_array_foreach(json_paths, i, json_path) {
2018-10-20 14:20:06 +02:00
path *p = (path *) alloc(sizeof(path));
ret = path_init(p);
if (ret)
throw RuntimeError("Failed to initialize path");
2018-07-03 21:51:48 +02:00
ret = path_parse(p, json_path, &nodes);
if (ret)
throw RuntimeError("Failed to parse path");
2018-07-03 21:51:48 +02:00
list_push(&paths, p);
if (p->reverse) {
2018-10-20 14:20:06 +02:00
path *r = (path *) alloc(sizeof(path));
ret = path_init(r);
if (ret)
throw RuntimeError("Failed to init path");
ret = path_reverse(p, r);
if (ret)
throw RuntimeError("Failed to reverse path {}", path_name(p));
2018-07-03 21:51:48 +02:00
list_push(&paths, r);
}
}
}
2018-07-03 21:51:48 +02:00
json = j;
state = STATE_PARSED;
return 0;
}
2018-07-03 21:51:48 +02:00
int SuperNode::check()
{
int ret;
2018-10-20 14:20:06 +02:00
assert(state == STATE_INITIALIZED || state == STATE_PARSED || state == STATE_CHECKED);
2018-07-03 21:51:48 +02:00
for (size_t i = 0; i < list_length(&nodes); i++) {
2018-10-20 14:20:06 +02:00
auto *n = (struct node *) list_at(&nodes, i);
ret = node_check(n);
if (ret)
throw RuntimeError("Invalid configuration for node {}", node_name(n));
}
2018-07-03 21:51:48 +02:00
for (size_t i = 0; i < list_length(&paths); i++) {
2018-10-20 14:20:06 +02:00
auto *p = (struct path *) list_at(&paths, i);
ret = path_check(p);
if (ret)
throw RuntimeError("Invalid configuration for path {}", path_name(p));
}
2018-07-03 21:51:48 +02:00
state = STATE_CHECKED;
return 0;
}
2018-07-03 21:51:48 +02:00
int SuperNode::start()
{
int ret;
2018-07-03 21:51:48 +02:00
assert(state == STATE_CHECKED);
2018-07-03 21:51:48 +02:00
memory_init(hugepages);
2018-10-20 14:20:06 +02:00
kernel::rt::init(priority, affinity);
#ifdef WITH_API
2018-10-20 14:20:06 +02:00
api.start();
#endif
2018-10-20 14:20:06 +02:00
#ifdef WITH_WEB
2018-10-20 14:20:06 +02:00
web.start();
#endif
2018-10-20 14:20:06 +02:00
logger->info("Starting node-types");
2018-07-03 21:51:48 +02:00
for (size_t i = 0; i < list_length(&nodes); i++) {
2018-10-20 14:20:06 +02:00
auto *n = (struct node *) list_at(&nodes, i);
2018-12-02 02:56:52 +01:00
ret = node_type_start(n->_vt, reinterpret_cast<super_node *>(this));
if (ret)
throw RuntimeError("Failed to start node-type: {}", node_type_name(n->_vt));
}
2018-10-20 14:20:06 +02:00
logger->info("Starting nodes");
2018-07-03 21:51:48 +02:00
for (size_t i = 0; i < list_length(&nodes); i++) {
2018-10-20 14:20:06 +02:00
auto *n = (struct node *) list_at(&nodes, i);
2018-08-20 18:31:27 +02:00
ret = node_init2(n);
if (ret)
throw RuntimeError("Failed to prepare node: {}", node_name(n));
2018-08-20 18:31:27 +02:00
2018-07-03 21:51:48 +02:00
int refs = list_count(&paths, (cmp_cb_t) path_uses_node, n);
if (refs > 0) {
ret = node_start(n);
if (ret)
throw RuntimeError("Failed to start node: {}", node_name(n));
2017-09-16 14:59:48 +02:00
}
else
2018-10-20 14:20:06 +02:00
logger->warn("No path is using the node {}. Skipping...", node_name(n));
}
2018-10-20 14:20:06 +02:00
logger->info("Starting paths");
2018-07-03 21:51:48 +02:00
for (size_t i = 0; i < list_length(&paths); i++) {
2018-10-20 14:20:06 +02:00
auto *p = (struct path *) list_at(&paths, i);
if (p->enabled) {
ret = path_init2(p);
if (ret)
throw RuntimeError("Failed to prepare path: {}", path_name(p));
ret = path_start(p);
if (ret)
throw RuntimeError("Failed to start path: {}", path_name(p));
}
else
2018-10-20 14:20:06 +02:00
logger->warn("Path {} is disabled. Skipping...", path_name(p));
}
2018-10-20 14:20:06 +02:00
#ifdef WITH_HOOKS
if (stats > 0) {
stats_print_header(STATS_FORMAT_HUMAN);
ret = task_init(&task, 1.0 / stats, CLOCK_REALTIME);
if (ret)
throw RuntimeError("Failed to create stats timer");
2018-10-20 14:20:06 +02:00
}
#endif /* WITH_HOOKS */
2018-07-03 21:51:48 +02:00
state = STATE_STARTED;
return 0;
}
2018-07-03 21:51:48 +02:00
int SuperNode::stop()
{
int ret;
2018-10-20 14:20:06 +02:00
#ifdef WITH_HOOKS
if (stats > 0) {
2018-07-03 21:51:48 +02:00
stats_print_footer(STATS_FORMAT_HUMAN);
2018-10-20 14:20:06 +02:00
ret = task_destroy(&task);
if (ret)
throw RuntimeError("Failed to stop stats timer");
2018-10-20 14:20:06 +02:00
}
#endif /* WITH_HOOKS */
logger->info("Stopping paths");
2018-07-03 21:51:48 +02:00
for (size_t i = 0; i < list_length(&paths); i++) {
2018-10-20 14:20:06 +02:00
auto *p = (struct path *) list_at(&paths, i);
ret = path_stop(p);
if (ret)
throw RuntimeError("Failed to stop path: {}", path_name(p));
}
2018-10-20 14:20:06 +02:00
logger->info("Stopping nodes");
2018-07-03 21:51:48 +02:00
for (size_t i = 0; i < list_length(&nodes); i++) {
2018-10-20 14:20:06 +02:00
auto *n = (struct node *) list_at(&nodes, i);
ret = node_stop(n);
if (ret)
throw RuntimeError("Failed to stop node: {}", node_name(n));
}
2018-10-20 14:20:06 +02:00
logger->info("Stopping node-types");
for (size_t i = 0; i < list_length(&plugins); i++) {
2018-10-20 14:20:06 +02:00
auto *p = (struct plugin *) list_at(&plugins, i);
if (p->type == PLUGIN_TYPE_NODE) {
ret = node_type_stop(&p->node);
if (ret)
throw RuntimeError("Failed to stop node-type: {}", node_type_name(&p->node));
}
}
#ifdef WITH_API
2018-10-20 14:20:06 +02:00
api.stop();
#endif
#ifdef WITH_WEB
2018-10-20 14:20:06 +02:00
web.stop();
#endif
2018-07-03 21:51:48 +02:00
state = STATE_STOPPED;
return 0;
}
2018-07-03 21:51:48 +02:00
void SuperNode::run()
{
2018-07-03 21:51:48 +02:00
#ifdef WITH_HOOKS
2018-10-20 14:20:06 +02:00
task_wait(&task);
periodic();
#else
pause();
2018-07-03 21:51:48 +02:00
#endif /* WITH_HOOKS */
}
SuperNode::~SuperNode()
{
2018-10-20 14:20:06 +02:00
assert(state != STATE_STARTED);
2018-07-03 21:51:48 +02:00
list_destroy(&plugins, (dtor_cb_t) plugin_destroy, false);
list_destroy(&paths, (dtor_cb_t) path_destroy, true);
list_destroy(&nodes, (dtor_cb_t) node_destroy, true);
if (json)
json_decref(json);
2017-04-06 12:12:56 +02:00
}
2018-07-03 21:51:48 +02:00
int SuperNode::periodic()
{
#ifdef WITH_HOOKS
int ret;
2018-07-03 21:51:48 +02:00
for (size_t i = 0; i < list_length(&paths); i++) {
2018-10-20 14:20:06 +02:00
auto *p = (struct path *) list_at(&paths, i);
if (p->state != STATE_STARTED)
continue;
for (size_t j = 0; j < list_length(&p->hooks); j++) {
2018-10-20 14:20:06 +02:00
hook *h = (struct hook *) list_at(&p->hooks, j);
ret = hook_periodic(h);
if (ret)
return ret;
}
}
2018-07-03 21:51:48 +02:00
for (size_t i = 0; i < list_length(&nodes); i++) {
2018-10-20 14:20:06 +02:00
auto *n = (struct node *) list_at(&nodes, i);
if (n->state != STATE_STARTED)
continue;
for (size_t j = 0; j < list_length(&n->in.hooks); j++) {
2018-10-20 14:20:06 +02:00
auto *h = (struct hook *) list_at(&n->in.hooks, j);
ret = hook_periodic(h);
if (ret)
return ret;
}
for (size_t j = 0; j < list_length(&n->out.hooks); j++) {
2018-10-20 14:20:06 +02:00
auto *h = (struct hook *) list_at(&n->out.hooks, j);
ret = hook_periodic(h);
if (ret)
return ret;
}
}
#endif
return 0;
}
2018-12-02 02:56:52 +01:00
/* C-compatability */
extern "C" {
struct list * super_node_get_nodes(struct super_node *sn)
{
SuperNode *ssn = reinterpret_cast<SuperNode *>(sn);
return ssn->getNodes();
}
struct lws_context * super_node_get_web_context(struct super_node *sn)
{
SuperNode *ssn = reinterpret_cast<SuperNode *>(sn);
return ssn->getWeb()->getContext();
}
struct lws_vhost * super_node_get_web_vhost(struct super_node *sn)
{
SuperNode *ssn = reinterpret_cast<SuperNode *>(sn);
return ssn->getWeb()->getVHost();
}
enum state super_node_get_web_state(struct super_node *sn)
{
SuperNode *ssn = reinterpret_cast<SuperNode *>(sn);
return ssn->getWeb()->getState();
}
}