-
Notifications
You must be signed in to change notification settings - Fork 22
Expand file tree
/
Copy pathsuper_node.cpp
More file actions
537 lines (416 loc) · 12.1 KB
/
Copy pathsuper_node.cpp
File metadata and controls
537 lines (416 loc) · 12.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
/* The super node object holding the state of the application.
*
* Author: Steffen Vogel <post@steffenvogel.de>
* SPDX-FileCopyrightText: 2014-2023 Institute for Automation of Complex Power Systems, RWTH Aachen University
* SPDX-License-Identifier: Apache-2.0
*/
#include <cstdlib>
#include <cstring>
#include <nlohmann/json.hpp>
#include <villas/config_helper.hpp>
#include <villas/hook_list.hpp>
#include <villas/kernel/if.hpp>
#include <villas/kernel/rt.hpp>
#include <villas/log.hpp>
#include <villas/node.hpp>
#include <villas/node/exceptions.hpp>
#include <villas/node/memory.hpp>
#include <villas/path.hpp>
#include <villas/super_node.hpp>
#include <villas/timing.hpp>
#include <villas/uuid.hpp>
#include "villas/json.hpp"
#ifdef WITH_NETEM
#include <villas/kernel/nl.hpp>
#endif
using namespace villas;
using namespace villas::node;
SuperNode::SuperNode()
: state(State::INITIALIZED), idleStop(false),
#ifdef WITH_API
api(this),
#endif
#ifdef WITH_WEB
#ifdef WITH_API
web(&api),
#else
web(),
#endif
#endif
seed(0), priority(0), affinity(0), hugepages(DEFAULT_NR_HUGEPAGES),
statsRate(1.0), task(), started(time_now()) {
int ret;
char hname[128];
ret = gethostname(hname, sizeof(hname));
if (ret)
throw SystemError("Failed to determine hostname");
// Default UUID is derived from hostname
uuid::generateFromString(uuid, hname);
#ifdef WITH_NETEM
kernel::nl::init(); // Fill link cache
#endif // WITH_NETEM
logger = Log::get("super_node");
}
void SuperNode::parse(fs::path const &path) {
configPath = path;
load_config_file(path,
{
.allow_libconfig = true,
.allow_environment = true,
.allow_include = true,
})
.get_to(configRoot);
parse(configRoot.get());
}
void SuperNode::parse(json_t *root) {
int ret;
assert(state == State::PARSED || state == State::INITIALIZED ||
state == State::CHECKED);
const char *uuid_str = nullptr;
json_t *json_nodes = nullptr;
json_t *json_paths = nullptr;
json_t *json_logging = nullptr;
json_t *json_http = nullptr;
json_error_t err;
int stop = -1;
ret = json_unpack_ex(root, &err, 0,
"{ s?: F, s?: o, s?: o, s?: o, s?: o, s?: i, s?: i, s?: "
"i, s?: b, s?: s, s?: i }",
"stats", &statsRate, "http", &json_http, "logging",
&json_logging, "nodes", &json_nodes, "paths",
&json_paths, "hugepages", &hugepages, "affinity",
&affinity, "priority", &priority, "idle_stop", &stop,
"uuid", &uuid_str, "seed", &seed);
if (ret)
throw ConfigError(root, err, "node-config",
"Unpacking top-level config failed");
if (stop >= 0) {
idleStop = stop > 0;
}
if (uuid_str) {
ret = uuid_parse(uuid_str, uuid);
if (ret)
throw ConfigError(root, "node-config-uuid", "Failed to parse UUID: {}",
uuid_str);
}
#ifdef WITH_WEB
if (json_http)
web.parse(json_http);
#endif // WITH_WEB
if (json_logging)
Log::getInstance().parse(json_logging);
// Parse nodes
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 *node_name;
json_t *json_node;
json_object_foreach (json_nodes, node_name, json_node) {
uuid_t node_uuid;
const char *node_type;
const char *node_uuid_str = nullptr;
ret = Node::isValidName(node_name);
if (!ret)
throw RuntimeError("Invalid name for node: {}", node_name);
ret = json_unpack_ex(json_node, &err, 0, "{ s: s, s?: s }", "type",
&node_type, "uuid", &node_uuid_str);
if (ret)
throw ConfigError(root, err, "node-config-node-type",
"Failed to parse type of node '{}'", node_name);
if (node_uuid_str) {
ret = uuid_parse(uuid_str, uuid);
if (ret)
throw ConfigError(json_node, "node-config-node-uuid",
"Failed to parse UUID: {}", uuid_str);
} else
// Generate UUID from node name and super-node UUID
uuid::generateFromString(node_uuid, node_name, uuid::toString(uuid));
auto *n = NodeFactory::make(node_type, node_uuid, node_name);
if (!n)
throw MemoryAllocationError();
n->configPath = getConfigPath();
ret = n->parse(json_node);
if (ret) {
auto config_id = fmt::format("node-config-node-{}", node_type);
throw ConfigError(json_node, config_id,
"Failed to parse configuration of node '{}'",
node_name);
}
nodes.push_back(n);
}
}
// Parse paths
if (json_paths) {
if (!json_is_array(json_paths))
logger->warn("Setting 'paths' must be a list of objects");
size_t i;
json_t *json_path;
json_array_foreach (json_paths, i, json_path) {
parse:
auto *p = new Path();
if (!p)
throw MemoryAllocationError();
p->parse(json_path, nodes, uuid);
paths.push_back(p);
if (p->isReversed()) {
// Only simple paths can be reversed
ret = p->isSimple();
if (!ret)
throw RuntimeError("Complex paths can not be reversed!");
// Parse a second time with in/out reversed
json_path = json_copy(json_path);
json_t *json_in = json_object_get(json_path, "in");
json_t *json_out = json_object_get(json_path, "out");
if (json_equal(json_in, json_out))
throw RuntimeError(
"Can not reverse path with identical in/out nodes!");
json_object_set(json_path, "reverse", json_false());
json_object_set(json_path, "in", json_out);
json_object_set(json_path, "out", json_in);
goto parse;
}
}
}
state = State::PARSED;
}
void SuperNode::check() {
int ret;
assert(state == State::INITIALIZED || state == State::PARSED ||
state == State::CHECKED);
for (auto *n : nodes) {
ret = n->check();
if (ret)
throw RuntimeError("Invalid configuration for node {}", n->getName());
}
for (auto *p : paths)
p->check();
state = State::CHECKED;
}
void SuperNode::prepareNodeTypes() {
int ret;
for (auto *n : nodes) {
auto *nf = n->getFactory();
if (nf->getState() == State::STARTED)
continue;
ret = nf->start(this);
if (ret)
throw RuntimeError("Failed to start node-type: {}",
n->getFactory()->getName());
}
}
void SuperNode::startInterfaces() {
#ifdef WITH_NETEM
int ret;
for (auto *i : interfaces) {
ret = i->start();
if (ret)
throw RuntimeError("Failed to start network interface: {}", i->getName());
}
#endif // WITH_NETEM
}
void SuperNode::startNodes() {
int ret;
for (auto *n : nodes) {
if (!n->isEnabled())
continue;
ret = n->start();
if (ret)
throw RuntimeError("Failed to start node: {}", n->getName());
}
}
void SuperNode::startPaths() {
for (auto *p : paths) {
if (!p->isEnabled())
continue;
p->start();
}
}
void SuperNode::prepareNodes() {
int ret;
for (auto *n : nodes) {
if (!n->isEnabled())
continue;
ret = n->prepare();
if (ret)
throw RuntimeError("Failed to prepare node: {}", n->getName());
}
}
void SuperNode::preparePaths() {
for (auto *p : paths) {
if (!p->isEnabled())
continue;
p->prepare(nodes);
}
}
void SuperNode::prepare() {
int ret;
assert(state == State::CHECKED);
ret = memory::init(hugepages);
if (ret)
throw RuntimeError("Failed to initialize memory system");
kernel::rt::init(priority, affinity);
prepareNodeTypes();
prepareNodes();
preparePaths();
for (auto *n : nodes) {
if (n->sources.size() == 0 && n->destinations.size() == 0) {
logger->info("Node {} is not used by any path. Disabling...",
n->getName());
n->setEnabled(false);
}
}
state = State::PREPARED;
}
void SuperNode::start() {
assert(state == State::PREPARED);
srand(seed);
#ifdef WITH_API
api.start();
#endif
#ifdef WITH_WEB
web.start();
#endif
startInterfaces();
startNodes();
startPaths();
if (statsRate > 0) // A rate <0 will disable the periodic stats
task.setRate(statsRate);
Stats::printHeader(Stats::Format::HUMAN);
state = State::STARTED;
}
void SuperNode::stopPaths() {
for (auto *p : paths) {
if (p->getState() == State::STARTED || p->getState() == State::PAUSED)
p->stop();
}
}
void SuperNode::stopNodes() {
int ret;
for (auto *n : nodes) {
if (n->getState() == State::STARTED || n->getState() == State::PAUSED ||
n->getState() == State::STOPPING) {
ret = n->stop();
if (ret)
throw RuntimeError("Failed to stop node: {}", n->getName());
}
}
}
void SuperNode::stopNodeTypes() {
int ret;
for (auto *n : nodes) {
auto *nf = n->getFactory();
if (nf->getState() != State::STARTED)
continue;
ret = nf->stop();
if (ret)
throw RuntimeError("Failed to stop node-type: {}",
n->getFactory()->getName());
}
}
void SuperNode::stopInterfaces() {
#ifdef WITH_NETEM
int ret;
for (auto *i : interfaces) {
ret = i->stop();
if (ret)
throw RuntimeError("Failed to stop interface: {}", i->getName());
}
#endif // WITH_NETEM
}
void SuperNode::stop() {
stopNodes();
stopPaths();
stopNodeTypes();
stopInterfaces();
#ifdef WITH_API
api.stop();
#endif
#ifdef WITH_WEB
web.stop();
#endif
state = State::STOPPED;
}
void SuperNode::run() {
int ret;
while (state == State::STARTED) {
task.wait();
ret = periodic();
if (ret)
state = State::STOPPING;
}
}
SuperNode::~SuperNode() {
assert(state == State::INITIALIZED || state == State::PARSED ||
state == State::CHECKED || state == State::STOPPED ||
state == State::PREPARED);
for (auto *p : paths)
delete p;
for (auto *n : nodes)
delete n;
}
int SuperNode::periodic() {
int started = 0;
for (auto *p : paths) {
if (p->getState() == State::STARTED) {
started++;
#ifdef WITH_HOOKS
p->hooks.periodic();
#endif // WITH_HOOKS
}
}
for (auto *n : nodes) {
if (n->getState() == State::STARTED) {
#ifdef WITH_HOOKS
n->in.hooks.periodic();
n->out.hooks.periodic();
#endif // WITH_HOOKS
}
}
if (idleStop && state == State::STARTED && started == 0) {
logger->info("No more active paths. Stopping super-node");
return -1;
}
return 0;
}
#ifdef WITH_GRAPHVIZ
static void set_attr(void *ptr, const std::string &key,
const std::string &value, bool html = false) {
Agraph_t *g = agraphof(ptr);
char *d = (char *)"";
char *k = (char *)key.c_str();
char *v = (char *)value.c_str();
char *vd = html ? agstrdup_html(g, v) : agstrdup(g, v);
agsafeset(ptr, k, vd, d);
}
graph_t *SuperNode::getGraph() {
Agraph_t *g;
Agnode_t *m;
g = agopen((char *)"g", Agdirected, 0);
std::unordered_map<Node *, Agnode_t *> nodeMap;
for (auto *n : nodes) {
nodeMap[n] = agnode(g, (char *)n->getNameShort().c_str(), 1);
set_attr(nodeMap[n], "shape", "ellipse");
set_attr(nodeMap[n], "tooltip",
fmt::format("type={}, uuid={}", n->getFactory()->getName(),
uuid::toString(n->getUuid()).c_str()));
// set_attr(nodeMap[n], "fixedsize", "true");
// set_attr(nodeMap[n], "width", "0.15");
// set_attr(nodeMap[n], "height", "0.15");
}
unsigned i = 0;
for (auto *p : paths) {
auto name = fmt::format("path_{}", i++);
m = agnode(g, (char *)name.c_str(), 1);
set_attr(m, "shape", "box");
set_attr(m, "tooltip",
fmt::format("uuid={}", uuid::toString(p->getUuid()).c_str()));
for (auto ps : p->sources)
agedge(g, nodeMap[ps->getNode()], m, nullptr, 1);
for (auto pd : p->destinations)
agedge(g, m, nodeMap[pd->getNode()], nullptr, 1);
}
return g;
}
#endif // WITH_GRAPHVIZ