oeAware-manager/0003-the-client-adapts-to-the-new-interface-and-refactor-.patch
fly_1997 d080eabfc6 refactor instance interfaces and add signal function
(cherry picked from commit 0509a1c1085175fc5f7061da747eed724d59563e)
2024-06-05 11:23:19 +08:00

1081 lines
40 KiB
Diff

From 2f00d1f6c4a0dd199cdfb13e9a541c5f87f5cf4e Mon Sep 17 00:00:00 2001
From: fly_1997 <flylove7@outlook.com>
Date: Tue, 28 May 2024 11:09:13 +0800
Subject: [PATCH 2/4] the client adapts to the new interface and refactor
memory store
---
src/client/arg_parse.cpp | 18 +-----
src/client/arg_parse.h | 4 --
src/client/client.cpp | 1 -
src/client/cmd_handler.cpp | 7 +--
src/client/cmd_handler.h | 4 --
src/common/message_protocol.cpp | 1 -
src/common/message_protocol.h | 4 +-
src/plugin_mgr/config.h | 7 +--
src/plugin_mgr/dep_handler.cpp | 84 +++++++++++++------------
src/plugin_mgr/dep_handler.h | 56 ++++++++---------
src/plugin_mgr/instance_run_handler.cpp | 8 +--
src/plugin_mgr/instance_run_handler.h | 11 +---
src/plugin_mgr/interface.h | 33 ++++++++--
src/plugin_mgr/logger.cpp | 1 -
src/plugin_mgr/main.cpp | 1 -
src/plugin_mgr/memory_store.h | 39 +++++++-----
src/plugin_mgr/message_manager.h | 3 +-
src/plugin_mgr/plugin.cpp | 2 +-
src/plugin_mgr/plugin.h | 2 +-
src/plugin_mgr/plugin_manager.cpp | 75 ++++++++--------------
src/plugin_mgr/plugin_manager.h | 29 +++------
21 files changed, 174 insertions(+), 216 deletions(-)
diff --git a/src/client/arg_parse.cpp b/src/client/arg_parse.cpp
index 947c4c4..75e9671 100644
--- a/src/client/arg_parse.cpp
+++ b/src/client/arg_parse.cpp
@@ -13,11 +13,10 @@
#include <iostream>
#include <getopt.h>
-const std::string ArgParse::OPT_STRING = "Qqd:t:l:r:e:i:";
+const std::string ArgParse::OPT_STRING = "Qqd:l:r:e:i:";
const struct option ArgParse::long_options[] = {
{"help", no_argument, NULL, 'h'},
{"load", required_argument, NULL, 'l'},
- {"type", required_argument, NULL, 't'},
{"remove", required_argument, NULL, 'r'},
{"query", required_argument, NULL, 'q'},
{"query-dep", required_argument, NULL, 'Q'},
@@ -34,10 +33,6 @@ void ArgParse::arg_error(const std::string &msg) {
exit(EXIT_FAILURE);
}
-void ArgParse::set_type(char *_type) {
- type = _type;
-}
-
void ArgParse::set_arg(char *_arg) {
arg = std::string(_arg);
}
@@ -46,10 +41,6 @@ void ArgParse::print_help() {
std::cout << "usage: oeawarectl [options]...\n"
" options\n"
" -l|--load [plugin] load plugin and need plugin type.\n"
- " -t|--type [plugin_type] assign plugin type. there are three types:\n"
- " collector: collection plugin.\n"
- " scenario: awareness plugin.\n"
- " tune: tune plugin.\n"
" -r|--remove [plugin] remove plugin from system.\n"
" -e|--enable [instance] enable the plugin instance.\n"
" -d|--disable [instance] disable the plugin instance.\n"
@@ -70,7 +61,6 @@ void ArgParse::init_opts() {
opts.insert('e');
opts.insert('d');
opts.insert('i');
- opts.insert('t');
}
int ArgParse::init(int argc, char *argv[]) {
@@ -82,9 +72,6 @@ int ArgParse::init(int argc, char *argv[]) {
while((opt = getopt_long(argc, argv, OPT_STRING.c_str(), long_options, nullptr)) != -1) {
std::string full_opt;
switch (opt) {
- case 't':
- set_type(optarg);
- break;
case 'h':
help = true;
break;
@@ -118,9 +105,6 @@ int ArgParse::init(int argc, char *argv[]) {
}
}
- if (cmd == 'l' && type.empty()) {
- arg_error("missing arguments.");
- }
if (help) {
print_help();
exit(EXIT_SUCCESS);
diff --git a/src/client/arg_parse.h b/src/client/arg_parse.h
index 6a0a25b..e3e55cd 100644
--- a/src/client/arg_parse.h
+++ b/src/client/arg_parse.h
@@ -22,15 +22,11 @@ public:
static void init_opts();
static void set_type(char* _type);
static void set_arg(char* _arg);
- static std::string get_type() {
- return type;
- }
static std::string get_arg() {
return arg;
}
private:
static std::string arg;
- static std::string type;
static std::unordered_set<char> opts;
static const std::string OPT_STRING;
static const int MAX_OPT_SIZE = 20;
diff --git a/src/client/client.cpp b/src/client/client.cpp
index afaa189..6318d5d 100644
--- a/src/client/client.cpp
+++ b/src/client/client.cpp
@@ -12,7 +12,6 @@
#include "client.h"
std::string ArgParse::arg;
-std::string ArgParse::type;
std::unordered_set<char> ArgParse::opts;
void Client::cmd_groups_init() {
diff --git a/src/client/cmd_handler.cpp b/src/client/cmd_handler.cpp
index 6f3f760..009a9bc 100644
--- a/src/client/cmd_handler.cpp
+++ b/src/client/cmd_handler.cpp
@@ -23,10 +23,7 @@ void LoadHandler::check(const std::string &type) {
void LoadHandler::handler(Msg &msg) {
std::string arg = ArgParse::get_arg();
- std::string type = ArgParse::get_type();
- check(type);
msg.add_payload(arg);
- msg.add_payload(type);
msg.set_opt(Opt::LOAD);
}
@@ -109,10 +106,10 @@ void write_to_file(const std::string &file_name, const std::string &text) {
void QueryTopHandler::handler(Msg &msg) {
std::string arg = ArgParse::get_arg();
if (arg.empty()) {
- msg.set_opt(Opt::QUERY_ALL_TOP);
+ msg.set_opt(Opt::QUERY_ALL_DEPS);
} else {
msg.add_payload(arg);
- msg.set_opt(Opt::QUERY_TOP);
+ msg.set_opt(Opt::QUERY_DEP);
}
}
diff --git a/src/client/cmd_handler.h b/src/client/cmd_handler.h
index 2e32098..53178fd 100644
--- a/src/client/cmd_handler.h
+++ b/src/client/cmd_handler.h
@@ -13,9 +13,6 @@
#define CLIENT_CMD_HANDLER_H
#include "message_protocol.h"
#include "arg_parse.h"
-#include <unordered_set>
-#include <string>
-#include <stdio.h>
class CmdHandler {
public:
@@ -27,7 +24,6 @@ public:
class LoadHandler : public CmdHandler {
public:
- // LoadHandler(const ArgParse &arg_parse) { }
void handler(Msg &msg) override;
void res_handler(Msg &msg) override;
private:
diff --git a/src/common/message_protocol.cpp b/src/common/message_protocol.cpp
index a813a6c..9cf9415 100644
--- a/src/common/message_protocol.cpp
+++ b/src/common/message_protocol.cpp
@@ -10,7 +10,6 @@
* See the Mulan PSL v2 for more details.
******************************************************************************/
#include "message_protocol.h"
-#include <iostream>
template <typename T>
inline ssize_t handle_error(T fn) {
diff --git a/src/common/message_protocol.h b/src/common/message_protocol.h
index b2c3c0d..d659f59 100644
--- a/src/common/message_protocol.h
+++ b/src/common/message_protocol.h
@@ -34,8 +34,8 @@ enum class Opt {
REMOVE,
QUERY,
QUERY_ALL,
- QUERY_TOP,
- QUERY_ALL_TOP,
+ QUERY_DEP,
+ QUERY_ALL_DEPS,
LIST,
DOWNLOAD,
RESPONSE_OK,
diff --git a/src/plugin_mgr/config.h b/src/plugin_mgr/config.h
index 3844f71..9d097ab 100644
--- a/src/plugin_mgr/config.h
+++ b/src/plugin_mgr/config.h
@@ -13,7 +13,6 @@
#define PLUGIN_MGR_CONFIG_H
#include "plugin.h"
-#include <string>
#include <unordered_map>
#include <yaml-cpp/yaml.h>
#include <sys/stat.h>
@@ -52,7 +51,7 @@ namespace std {
class EnableItem {
public:
- EnableItem(const std::string &name) : name(name), enabled(false) { }
+ explicit EnableItem(const std::string &name) : name(name), enabled(false) { }
void set_enabled(bool enabled) {
this->enabled = enabled;
}
@@ -86,9 +85,6 @@ public:
int get_log_level() const {
return this->log_level;
}
- int get_schedule_cycle() const {
- return this->schedule_cycle;
- }
std::string get_log_type() const {
return this->log_type;
}
@@ -115,7 +111,6 @@ public:
}
private:
int log_level;
- int schedule_cycle;
std::string log_path;
std::string log_type;
std::unordered_map<std::string, PluginInfo> plugin_list;
diff --git a/src/plugin_mgr/dep_handler.cpp b/src/plugin_mgr/dep_handler.cpp
index c6d0985..1006175 100644
--- a/src/plugin_mgr/dep_handler.cpp
+++ b/src/plugin_mgr/dep_handler.cpp
@@ -11,39 +11,52 @@
******************************************************************************/
#include "dep_handler.h"
#include <queue>
-#include <unordered_set>
-#include <stdio.h>
+bool DepHandler::is_instance_exist(const std::string &name) {
+ return nodes.count(name);
+}
+
+void DepHandler::add_instance(std::shared_ptr<Instance> instance) {
+ add_node(instance);
+}
+
+void DepHandler::delete_instance(const std::string &name) {
+ del_node(name);
+}
+
void DepHandler::add_arc_node(std::shared_ptr<Node> node, const std::vector<std::string> &dep_nodes) {
std::shared_ptr<ArcNode> arc_head = node->head;
node->cnt = dep_nodes.size();
int real_cnt = 0;
- bool state = true;
for (auto name : dep_nodes) {
std::shared_ptr<ArcNode> tmp = std::make_shared<ArcNode>();
tmp->arc_name = name;
- tmp->node_name = node->name;
+ tmp->node_name = node->instance->get_name();
tmp->next = arc_head->next;
arc_head->next = tmp;
if (nodes.count(name)) {
- arc_nodes[name][tmp] = true;
+ tmp->is_exist = true;
+ arc_nodes[name].insert(tmp);
real_cnt++;
} else {
- arc_nodes[name][tmp] = false;
- state = false;
+ tmp->is_exist = false;
+ arc_nodes[name].insert(tmp);
}
}
- node->state = state;
+ if (real_cnt == node->cnt) {
+ node->instance->set_state(true);
+ }
node->real_cnt = real_cnt;
}
-
-void DepHandler::add_node(const std::string &name, const std::vector<std::string> &dep_nodes) {
- std::shared_ptr<Node> cur_node = add_new_node(name);
+void DepHandler::add_node(std::shared_ptr<Instance> instance) {
+ std::string name = instance->get_name();
+ std::shared_ptr<Node> cur_node = add_new_node(instance);
this->nodes[name] = cur_node;
+ std::vector<std::string> dep_nodes = instance->get_deps();
add_arc_node(cur_node, dep_nodes);
- change_arc_nodes(name, true);
+ update_instance_state(name);
}
void DepHandler::del_node(const std::string &name) {
@@ -56,16 +69,14 @@ std::shared_ptr<Node> DepHandler::get_node(const std::string &name) {
return this->nodes[name];
}
-std::shared_ptr<Node> DepHandler::add_new_node(std::string name) {
- std::shared_ptr<Node> cur_node = std::make_shared<Node>(name);
+std::shared_ptr<Node> DepHandler::add_new_node(std::shared_ptr<Instance> instance) {
+ std::shared_ptr<Node> cur_node = std::make_shared<Node>();
+ cur_node->instance = instance;
cur_node->head = std::make_shared<ArcNode>();
- tail->next = cur_node;
- tail = cur_node;
return cur_node;
}
void DepHandler::del_node_and_arc_nodes(std::shared_ptr<Node> node) {
- std::shared_ptr<Node> next = node->next;
std::shared_ptr<ArcNode> arc = node->head;
while(arc) {
std::shared_ptr<ArcNode> tmp = arc->next;
@@ -80,34 +91,28 @@ void DepHandler::del_node_and_arc_nodes(std::shared_ptr<Node> node) {
}
}
-void DepHandler::change_arc_nodes(std::string name, bool state) {
- if (!nodes[name]->state || !arc_nodes.count(name)) return;
- std::unordered_map<std::shared_ptr<ArcNode>, bool> &mp = arc_nodes[name];
- for (auto &vec : mp) {
- vec.second = state;
- if (nodes.count(vec.first->node_name)) {
- std::shared_ptr<Node> tmp = nodes[vec.first->node_name];
- if (state) {
- tmp->real_cnt++;
- if (tmp->real_cnt == tmp->cnt) {
- tmp->state = true;
- }
- } else {
- tmp->real_cnt--;
- tmp->state = false;
+void DepHandler::update_instance_state(const std::string &name) {
+ if (!nodes[name]->instance->get_state() || !arc_nodes.count(name)) return;
+ std::unordered_set<std::shared_ptr<ArcNode>> &arcs = arc_nodes[name];
+ for (auto &arc_node : arcs) {
+ if (nodes.count(arc_node->node_name)) {
+ auto tmp = nodes[arc_node->node_name];
+ tmp->real_cnt++;
+ if (tmp->real_cnt == tmp->cnt) {
+ tmp->instance->set_state(true);
}
- change_arc_nodes(vec.first->node_name, state);
+ update_instance_state(tmp->instance->get_name());
}
}
}
-void DepHandler::query_all_top(std::vector<std::vector<std::string>> &query) {
+void DepHandler::query_all_dependencies(std::vector<std::vector<std::string>> &query) {
for (auto &p : nodes) {
query_node_top(p.first, query);
}
}
-void DepHandler::query_node_top(std::string name, std::vector<std::vector<std::string>> &query) {
+void DepHandler::query_node_top(const std::string &name, std::vector<std::vector<std::string>> &query) {
std::shared_ptr<ArcNode> p = nodes[name]->head;
if (p->next == nullptr) {
query.emplace_back(std::vector<std::string>{name});
@@ -119,7 +124,7 @@ void DepHandler::query_node_top(std::string name, std::vector<std::vector<std::s
}
}
-void DepHandler::query_node(const std::string &name, std::vector<std::vector<std::string>> &query) {
+void DepHandler::query_node_dependency(const std::string &name, std::vector<std::vector<std::string>> &query) {
if (!nodes.count(name)) return;
std::queue<std::string> q;
std::unordered_set<std::string> vis;
@@ -128,9 +133,10 @@ void DepHandler::query_node(const std::string &name, std::vector<std::vector<std
while (!q.empty()) {
auto node = nodes[q.front()];
q.pop();
- query.emplace_back(std::vector<std::string>{node->name});
+ std::string node_name = node->instance->get_name();
+ query.emplace_back(std::vector<std::string>{node_name});
for (auto cur = node->head->next; cur != nullptr; cur = cur->next) {
- query.emplace_back(std::vector<std::string>{node->name, cur->arc_name});
+ query.emplace_back(std::vector<std::string>{node_name, cur->arc_name});
if (!vis.count(cur->arc_name) && nodes.count(cur->arc_name)) {
vis.insert(cur->arc_name);
q.push(cur->arc_name);
@@ -148,7 +154,7 @@ std::vector<std::string> DepHandler::get_pre_dependencies(const std::string &nam
while (!q.empty()) {
auto &node = q.front();
q.pop();
- res.emplace_back(node->name);
+ res.emplace_back(node->instance->get_name());
for (auto arc_node = node->head->next; arc_node != nullptr; arc_node = arc_node->next) {
if (!vis.count(arc_node->arc_name)) {
vis.insert(arc_node->arc_name);
diff --git a/src/plugin_mgr/dep_handler.h b/src/plugin_mgr/dep_handler.h
index 40b8748..a7a8ef6 100644
--- a/src/plugin_mgr/dep_handler.h
+++ b/src/plugin_mgr/dep_handler.h
@@ -12,68 +12,62 @@
#ifndef PLUGIN_MGR_DEP_HANDLER_H
#define PLUGIN_MGR_DEP_HANDLER_H
+#include "plugin.h"
#include <unordered_map>
-#include <string>
-#include <vector>
-#include <cstdint>
-#include <memory>
+#include <unordered_set>
struct ArcNode {
std::shared_ptr<ArcNode> next;
std::string arc_name;
std::string node_name;
- ArcNode() : next(nullptr) {}
+ bool is_exist;
+ ArcNode() : next(nullptr), is_exist(false) { }
};
// a instance node
struct Node {
std::shared_ptr<Node> next;
std::shared_ptr<ArcNode> head;
- std::string name;
+ std::shared_ptr<Instance> instance;
int cnt;
int real_cnt;
- bool state; // dependency closed-loop
- Node() : next(nullptr), head(nullptr), cnt(0), real_cnt(0), state(true) {}
- Node(const std::string &name): next(nullptr), head(nullptr), name(name), cnt(0), real_cnt(0), state(true) {}
+ Node(): next(nullptr), head(nullptr), cnt(0), real_cnt(0) { }
};
class DepHandler {
public:
DepHandler() {
this->head = std::make_shared<Node>();
- this->tail = head;
}
std::shared_ptr<Node> get_node(const std::string &name);
- bool get_node_state(std::string name) {
- return this->nodes[name]->state;
+ bool get_node_state(const std::string &name) {
+ return this->nodes[name]->instance->get_state();
}
- void add_node(const std::string &name, const std::vector<std::string> &dep_nodes = {});
- void del_node(const std::string &name);
- std::vector<std::string> get_pre_dependencies(const std::string &name);
- // query instance dependency
- void query_node(const std::string &name, std::vector<std::vector<std::string>> &query);
- // query all instance dependencies
- void query_all_top(std::vector<std::vector<std::string>> &query);
+ void add_instance(std::shared_ptr<Instance> instance);
+ void delete_instance(const std::string &name);
+ bool is_instance_exist(const std::string &name);
+ std::shared_ptr<Instance> get_instance(const std::string &name) const {
+ return nodes.at(name)->instance;
+ }
+ void query_node_dependency(const std::string &name, std::vector<std::vector<std::string>> &query);
+ void query_all_dependencies(std::vector<std::vector<std::string>> &query);
+ /* check whether the instance has dependencies */
bool have_dep(const std::string &name) {
return arc_nodes.count(name);
}
- bool is_empty() const {
- return nodes.empty();
- }
- size_t get_node_nums() const {
- return nodes.size();
- }
+ std::vector<std::string> get_pre_dependencies(const std::string &name);
private:
- void query_node_top(std::string name, std::vector<std::vector<std::string>> &query);
+ void add_node(std::shared_ptr<Instance> instance);
+ void del_node(const std::string &name);
+ void query_node_top(const std::string &name, std::vector<std::vector<std::string>> &query);
void add_arc_node(std::shared_ptr<Node> node, const std::vector<std::string> &dep_nodes);
- void change_arc_nodes(std::string name, bool state);
+ void update_instance_state(const std::string &name);
void del_node_and_arc_nodes(std::shared_ptr<Node> node);
- std::shared_ptr<Node> add_new_node(std::string name);
-
- std::unordered_map<std::string, std::unordered_map<std::shared_ptr<ArcNode>, bool>> arc_nodes;
+ std::shared_ptr<Node> add_new_node(std::shared_ptr<Instance> instance);
+ /* indegree edges */
+ std::unordered_map<std::string, std::unordered_set<std::shared_ptr<ArcNode>>> arc_nodes;
std::unordered_map<std::string, std::shared_ptr<Node>> nodes;
std::shared_ptr<Node> head;
- std::shared_ptr<Node> tail;
};
#endif // !PLUGIN_MGR_DEP_HANDLER_H
diff --git a/src/plugin_mgr/instance_run_handler.cpp b/src/plugin_mgr/instance_run_handler.cpp
index 9eed762..c60207b 100644
--- a/src/plugin_mgr/instance_run_handler.cpp
+++ b/src/plugin_mgr/instance_run_handler.cpp
@@ -13,7 +13,7 @@
#include <thread>
#include <unistd.h>
-static const void* get_ring_buf(std::shared_ptr<Instance> instance) {
+static const DataRingBuf* get_ring_buf(std::shared_ptr<Instance> instance) {
if (instance == nullptr) {
return nullptr;
}
@@ -21,14 +21,14 @@ static const void* get_ring_buf(std::shared_ptr<Instance> instance) {
}
void InstanceRunHandler::run_instance(std::shared_ptr<Instance> instance) {
- std::vector<const void*> input_data;
+ std::vector<const DataRingBuf*> input_data;
std::vector<std::string> deps = instance->get_deps();
for (size_t i = 0; i < deps.size(); ++i) {
std::shared_ptr<Instance> ins = memory_store.get_instance(deps[i]);
input_data.emplace_back(get_ring_buf(ins));
}
Param param;
- param.args = input_data.data();
+ param.ring_bufs = input_data.data();
param.len = input_data.size();
instance->get_interface()->run(&param);
}
@@ -73,7 +73,7 @@ void InstanceRunHandler::schedule(uint64_t time) {
break;
}
run_instance(schedule_instance.instance);
- schedule_instance.time += schedule_instance.instance->get_interface()->get_cycle();
+ schedule_instance.time += schedule_instance.instance->get_interface()->get_period();
schedule_queue.push(schedule_instance);
}
}
diff --git a/src/plugin_mgr/instance_run_handler.h b/src/plugin_mgr/instance_run_handler.h
index 4172e99..fc45874 100644
--- a/src/plugin_mgr/instance_run_handler.h
+++ b/src/plugin_mgr/instance_run_handler.h
@@ -16,19 +16,14 @@
#include "plugin.h"
#include "logger.h"
#include "memory_store.h"
-#include <set>
-#include <string>
-#include <vector>
-#include <unordered_map>
#include <queue>
-#include <memory>
enum class RunType {
ENABLED,
DISABLED,
};
-// Message for communication between plugin manager and instance scheduling
+/* Message for communication between plugin manager and instance scheduling */
class InstanceRunMessage {
public:
InstanceRunMessage() {}
@@ -53,10 +48,10 @@ public:
uint64_t time;
};
-// A handler to schedule plugin instance
+/* A handler to schedule instances. */
class InstanceRunHandler {
public:
- InstanceRunHandler(MemoryStore &memory_store) : memory_store(memory_store), cycle(DEFAULT_CYCLE_SIZE) {}
+ explicit InstanceRunHandler(MemoryStore &memory_store) : memory_store(memory_store), cycle(DEFAULT_CYCLE_SIZE) {}
void run();
void schedule(uint64_t time);
void handle_instance(uint64_t time);
diff --git a/src/plugin_mgr/interface.h b/src/plugin_mgr/interface.h
index 2106698..6495b92 100644
--- a/src/plugin_mgr/interface.h
+++ b/src/plugin_mgr/interface.h
@@ -11,6 +11,8 @@
******************************************************************************/
#ifndef PLUGIN_MGR_INTERFACE_H
#define PLUGIN_MGR_INTERFACE_H
+#include <stdbool.h>
+#include <stdint.h>
enum PluginType {
COLLECTOR,
@@ -18,8 +20,24 @@ enum PluginType {
TUNE,
};
+struct DataBuf {
+ int len;
+ void *data;
+};
+
+struct DataRingBuf {
+ /* instance name */
+ const char *instance_name;
+ /* buf write index, initial value is -1 */
+ int index;
+ /* instance run times */
+ uint64_t count;
+ struct DataBuf *buf;
+ int buf_len;
+};
+
struct Param {
- void *args;
+ const struct DataRingBuf **ring_bufs;
int len;
};
@@ -28,12 +46,17 @@ struct Interface {
const char* (*get_name)();
const char* (*get_description)();
const char* (*get_dep)();
- PluginType (*get_type)();
- int (*get_cycle)();
+ enum PluginType (*get_type)();
+ int (*get_period)();
bool (*enable)();
void (*disable)();
- const void* (*get_ring_buf)();
- void (*run)(const Param*);
+ const struct DataRingBuf* (*get_ring_buf)();
+ void (*run)(const struct Param*);
};
+/* Obtains the instances from the plugin.
+ * The return value is the number of instances.
+ */
+int get_instance(struct Interface **interface);
+
#endif // !PLUGIN_MGR_INTERFACE_H
diff --git a/src/plugin_mgr/logger.cpp b/src/plugin_mgr/logger.cpp
index 318eafb..7a924c2 100644
--- a/src/plugin_mgr/logger.cpp
+++ b/src/plugin_mgr/logger.cpp
@@ -11,7 +11,6 @@
******************************************************************************/
#include "logger.h"
#include <sys/stat.h>
-#include <unistd.h>
Logger::Logger() {
logger = log4cplus::Logger::getInstance("oeAware");
diff --git a/src/plugin_mgr/main.cpp b/src/plugin_mgr/main.cpp
index 4e6acdc..698ba62 100644
--- a/src/plugin_mgr/main.cpp
+++ b/src/plugin_mgr/main.cpp
@@ -53,7 +53,6 @@ int main(int argc, char **argv) {
INFO("[PluginManager] Start plugin manager!");
PluginManager plugin_manager(config, handler_msg, res_msg);
plugin_manager.init();
- plugin_manager.pre_load();
plugin_manager.run();
return 0;
}
\ No newline at end of file
diff --git a/src/plugin_mgr/memory_store.h b/src/plugin_mgr/memory_store.h
index ac3ff98..999795a 100644
--- a/src/plugin_mgr/memory_store.h
+++ b/src/plugin_mgr/memory_store.h
@@ -11,37 +11,36 @@
******************************************************************************/
#ifndef PLUGIN_MGR_MEMORY_STORE_H
#define PLUGIN_MGR_MEMORY_STORE_H
-#include "plugin.h"
#include "logger.h"
+#include "dep_handler.h"
#include <unordered_map>
-#include <memory>
-//OeAware memory storage, which is used to store plugins and instances in the memory.
+/* OeAware memory storage, which is used to store plugins and instances in the memory. */
class MemoryStore {
public:
void add_plugin(const std::string &name, std::shared_ptr<Plugin> plugin) {
this->plugins.insert(std::make_pair(name, plugin));
}
- void add_instance(const std::string &name, std::shared_ptr<Instance> instance) {
- this->instances.insert(std::make_pair(name, instance));
+ void add_instance(std::shared_ptr<Instance> instance) {
+ dep_handler.add_instance(instance);
}
std::shared_ptr<Plugin> get_plugin(const std::string &name) const {
return this->plugins.at(name);
}
std::shared_ptr<Instance> get_instance(const std::string &name) const {
- return this->instances.at(name);
+ return dep_handler.get_instance(name);
}
void delete_plugin(const std::string &name) {
this->plugins.erase(name);
}
void delete_instance(const std::string &name) {
- this->instances.erase(name);
+ dep_handler.delete_instance(name);
}
bool is_plugin_exist(const std::string &name) const {
return this->plugins.count(name);
}
- bool is_instance_exist(const std::string &name) const {
- return this->instances.count(name);
+ bool is_instance_exist(const std::string &name) {
+ return dep_handler.is_instance_exist(name);
}
std::vector<std::shared_ptr<Plugin>> get_all_plugins() {
std::vector<std::shared_ptr<Plugin>> res;
@@ -50,16 +49,24 @@ public:
}
return res;
}
- std::vector<std::shared_ptr<Instance>> get_all_instances() {
- std::vector<std::shared_ptr<Instance>> res;
- for (auto &p : instances) {
- res.emplace_back(p.second);
- }
- return res;
+ void query_node_dependency(const std::string &name, std::vector<std::vector<std::string>> &query) {
+ return dep_handler.query_node_dependency(name, query);
+ }
+ void query_all_dependencies(std::vector<std::vector<std::string>> &query) {
+ return dep_handler.query_all_dependencies(query);
+ }
+ bool have_dep(const std::string &name) {
+ return dep_handler.have_dep(name);
+ }
+ std::vector<std::string> get_pre_dependencies(const std::string &name) {
+ return dep_handler.get_pre_dependencies(name);
}
private:
+ /* instance are stored in the form of DAG.
+ * DepHandler stores instances and manages dependencies.
+ */
+ DepHandler dep_handler;
std::unordered_map<std::string, std::shared_ptr<Plugin>> plugins;
- std::unordered_map<std::string, std::shared_ptr<Instance>> instances;
};
#endif // !PLUGIN_MGR_MEMORY_STORE_H
diff --git a/src/plugin_mgr/message_manager.h b/src/plugin_mgr/message_manager.h
index 00ea4c7..95bbd1a 100644
--- a/src/plugin_mgr/message_manager.h
+++ b/src/plugin_mgr/message_manager.h
@@ -29,7 +29,7 @@ enum class MessageType {
class Message {
public:
Message() : type(MessageType::EXTERNAL) {}
- Message(Opt opt) : opt(opt) {}
+ explicit Message(Opt opt) : opt(opt) {}
Message(Opt opt, MessageType type) : opt(opt), type(type) {}
Message(Opt opt, const std::vector<std::string> &payload) : opt(opt), payload(payload) {}
Opt get_opt() {
@@ -79,6 +79,7 @@ public:
MessageManager(SafeQueue<Message> *handler_msg, SafeQueue<Message> *res_msg) {
this->handler_msg = handler_msg;
this->res_msg = res_msg;
+ this->tcp_socket = nullptr;
}
void init(){
this->tcp_socket = new TcpSocket();
diff --git a/src/plugin_mgr/plugin.cpp b/src/plugin_mgr/plugin.cpp
index bdd8226..4096fef 100644
--- a/src/plugin_mgr/plugin.cpp
+++ b/src/plugin_mgr/plugin.cpp
@@ -33,7 +33,7 @@ std::string Instance::get_info() const {
std::vector<std::string> Instance::get_deps() {
std::vector<std::string> vec;
- if (get_interface()->get_dep == nullptr) {
+ if (get_interface()->get_dep == nullptr || get_interface()->get_dep() == nullptr) {
return vec;
}
std::string deps = get_interface()->get_dep();
diff --git a/src/plugin_mgr/plugin.h b/src/plugin_mgr/plugin.h
index ac0e0b5..f6b5029 100644
--- a/src/plugin_mgr/plugin.h
+++ b/src/plugin_mgr/plugin.h
@@ -68,7 +68,7 @@ private:
class Plugin {
public:
- Plugin(const std::string &name) : name(name), handler(nullptr) { }
+ explicit Plugin(const std::string &name) : name(name), handler(nullptr) { }
~Plugin() {
if (handler != nullptr) {
dlclose(handler);
diff --git a/src/plugin_mgr/plugin_manager.cpp b/src/plugin_mgr/plugin_manager.cpp
index 1503fc1..7900ecf 100644
--- a/src/plugin_mgr/plugin_manager.cpp
+++ b/src/plugin_mgr/plugin_manager.cpp
@@ -11,21 +11,13 @@
******************************************************************************/
#include "plugin_manager.h"
#include "default_path.h"
-#include "utils.h"
-#include <iostream>
-#include <unordered_set>
#include <dirent.h>
-#include <sys/stat.h>
-const std::string PluginManager::COLLECTOR_TEXT = "collector";
-const std::string PluginManager::SCENARIO_TEXT = "scenario";
-const std::string PluginManager::TUNE_TEXT = "tune";
const static int ST_MODE_MASK = 0777;
void PluginManager::init() {
- plugin_types[COLLECTOR_TEXT] = PluginType::COLLECTOR;
- plugin_types[SCENARIO_TEXT] = PluginType::SCENARIO;
- plugin_types[TUNE_TEXT] = PluginType::TUNE;
+ instance_run_handler.reset(new InstanceRunHandler(memory_store));
+ pre_load();
}
ErrorCode PluginManager::remove(const std::string &name) {
@@ -40,19 +32,18 @@ ErrorCode PluginManager::remove(const std::string &name) {
if (instance->get_enabled()) {
return ErrorCode::REMOVE_INSTANCE_IS_RUNNING;
}
- if (dep_handler.have_dep(iname)) {
+ if (memory_store.have_dep(iname)) {
return ErrorCode::REMOVE_INSTANCE_HAVE_DEP;
}
instance_names.emplace_back(iname);
}
for(auto &iname : instance_names) {
memory_store.delete_instance(iname);
- dep_handler.del_node(iname);
}
memory_store.delete_plugin(name);
- update_instance_state();
return ErrorCode::OK;
}
+
ErrorCode PluginManager::query_all_plugins(std::string &res) {
std::vector<std::shared_ptr<Plugin>> all_plugins = memory_store.get_all_plugins();
for (auto &p : all_plugins) {
@@ -99,10 +90,8 @@ void PluginManager::save_instance(std::shared_ptr<Plugin> plugin, Interface *int
instance->set_name(name);
instance->set_plugin_name(plugin->get_name());
instance->set_enabled(false);
- dep_handler.add_node(name, instance->get_deps());
- instance->set_state(dep_handler.get_node_state(name));
DEBUG("[PluginManager] Instance: " << name.c_str());
- memory_store.add_instance(name, instance);
+ memory_store.add_instance(instance);
plugin->add_instance(instance);
}
}
@@ -116,21 +105,9 @@ bool PluginManager::load_instance(std::shared_ptr<Plugin> plugin) {
return false;
}
save_instance(plugin, interface_list, len);
- update_instance_state();
return true;
}
-void PluginManager::update_instance_state() {
- std::vector<std::shared_ptr<Instance>> all_instances = memory_store.get_all_instances();
- for (auto &instance : all_instances) {
- if (dep_handler.get_node_state(instance->get_name())) {
- instance->set_state(true);
- } else {
- instance->set_state(false);
- }
- }
-}
-
ErrorCode PluginManager::load_plugin(const std::string &name) {
std::string plugin_path = get_path() + "/" + name;
if (!file_exist(plugin_path)) {
@@ -160,6 +137,8 @@ ErrorCode PluginManager::load_plugin(const std::string &name) {
std::string generate_dot(MemoryStore &memory_store, const std::vector<std::vector<std::string>> &query) {
std::string res;
res += "digraph G {\n";
+ res += " rankdir = TB\n";
+ res += " ranksep = 1\n";
std::unordered_map<std::string, std::unordered_set<std::string>> sub_graph;
for (auto &vec : query) {
std::shared_ptr<Instance> instance = memory_store.get_instance(vec[0]);
@@ -171,45 +150,45 @@ std::string generate_dot(MemoryStore &memory_store, const std::vector<std::vecto
instance = memory_store.get_instance(vec[1]);
sub_graph[instance->get_plugin_name()].insert(vec[1]);
} else {
- res += vec[1] + "[label=\"(missing)\\n" + vec[1] + "\", fontcolor=red];\n";
+ res += " " + vec[1] + "[label=\"(missing)\\n" + vec[1] + "\", fontcolor=red];\n";
}
- res += vec[0] + "->" + vec[1] + ";\n";
+ res += " " + vec[0] + "->" + vec[1] + ";\n";
}
int id = 0;
for (auto &p : sub_graph) {
- res += "subgraph cluster_" + std::to_string(id) + " {\n";
- res += "node [style=filled];\n";
- res += "label = \"" + p.first + "\";\n";
+ res += " subgraph cluster_" + std::to_string(id) + " {\n";
+ res += " node [style=filled];\n";
+ res += " label = \"" + p.first + "\";\n";
for (auto &i_name : p.second) {
- res += i_name + ";\n";
+ res += " " + i_name + ";\n";
}
- res += "}\n";
+ res += " }\n";
id++;
}
res += "}";
return res;
}
-ErrorCode PluginManager::query_top(const std::string &name, std::string &res) {
+ErrorCode PluginManager::query_dependency(const std::string &name, std::string &res) {
if (!memory_store.is_instance_exist(name)) {
return ErrorCode::QUERY_DEP_NOT_EXIST;
}
DEBUG("[PluginManager] query top : " << name);
std::vector<std::vector<std::string>> query;
- dep_handler.query_node(name, query);
+ memory_store.query_node_dependency(name, query);
res = generate_dot(memory_store, query);
return ErrorCode::OK;
}
-ErrorCode PluginManager::query_all_tops(std::string &res) {
+ErrorCode PluginManager::query_all_dependencies(std::string &res) {
std::vector<std::vector<std::string>> query;
- dep_handler.query_all_top(query);
+ memory_store.query_all_dependencies(query);
DEBUG("[PluginManager] query size:" << query.size());
res = generate_dot(memory_store, query);
return ErrorCode::OK;
}
-ErrorCode PluginManager::instance_enabled(std::string name) {
+ErrorCode PluginManager::instance_enabled(const std::string &name) {
if (!memory_store.is_instance_exist(name)) {
return ErrorCode::ENABLE_INSTANCE_NOT_LOAD;
}
@@ -220,7 +199,7 @@ ErrorCode PluginManager::instance_enabled(std::string name) {
if (instance->get_enabled()) {
return ErrorCode::ENABLE_INSTANCE_ALREADY_ENABLED;
}
- std::vector<std::string> pre_dependencies = dep_handler.get_pre_dependencies(name);
+ std::vector<std::string> pre_dependencies = memory_store.get_pre_dependencies(name);
std::vector<std::shared_ptr<Instance>> new_enabled;
bool enabled = true;
for (int i = pre_dependencies.size() - 1; i >= 0; --i) {
@@ -252,7 +231,7 @@ ErrorCode PluginManager::instance_enabled(std::string name) {
}
}
-ErrorCode PluginManager::instance_disabled(std::string name) {
+ErrorCode PluginManager::instance_disabled(const std::string &name) {
if (!memory_store.is_instance_exist(name)) {
return ErrorCode::DISABLE_INSTANCE_NOT_LOAD;
}
@@ -364,7 +343,7 @@ void PluginManager::pre_load() {
pre_enable();
}
-const void* PluginManager::get_data_buffer(std::string name) {
+const void* PluginManager::get_data_buffer(const std::string &name) {
std::shared_ptr<Instance> instance = memory_store.get_instance(name);
return instance->get_interface()->get_ring_buf();
}
@@ -375,7 +354,7 @@ std::string PluginManager::instance_dep_check(const std::string &name) {
for (size_t i = 0; i < plugin->get_instance_len(); ++i) {
std::string instance_name = plugin->get_instance(i)->get_name();
std::vector<std::vector<std::string>> query;
- dep_handler.query_node(instance_name, query);
+ memory_store.query_node_dependency(instance_name, query);
std::vector<std::string> lack;
for (auto &item : query) {
if (item.size() < 2) continue;
@@ -478,10 +457,10 @@ int PluginManager::run() {
}
break;
}
- case Opt::QUERY_TOP: {
+ case Opt::QUERY_DEP: {
std::string res_text;
std::string name = msg.get_payload(0);
- ErrorCode ret_code = query_top(name , res_text);
+ ErrorCode ret_code = query_dependency(name , res_text);
if (ret_code == ErrorCode::OK) {
INFO("[PluginManager] query " << name << " instance dependencies.");
res.set_opt(Opt::RESPONSE_OK);
@@ -494,9 +473,9 @@ int PluginManager::run() {
}
break;
}
- case Opt::QUERY_ALL_TOP: {
+ case Opt::QUERY_ALL_DEPS: {
std::string res_text;
- ErrorCode ret_code = query_all_tops(res_text);
+ ErrorCode ret_code = query_all_dependencies(res_text);
if (ret_code == ErrorCode::OK) {
INFO("[PluginManager] query all instances dependencies.");
res.set_opt(Opt::RESPONSE_OK);
diff --git a/src/plugin_mgr/plugin_manager.h b/src/plugin_mgr/plugin_manager.h
index 9339f0d..18d3f35 100644
--- a/src/plugin_mgr/plugin_manager.h
+++ b/src/plugin_mgr/plugin_manager.h
@@ -15,35 +15,28 @@
#include "instance_run_handler.h"
#include "config.h"
#include "memory_store.h"
-#include "dep_handler.h"
#include "message_manager.h"
#include "error_code.h"
-#include <vector>
-#include <queue>
-#include <unordered_map>
-#include <dlfcn.h>
class PluginManager {
public:
PluginManager(Config &config, SafeQueue<Message> &handler_msg, SafeQueue<Message> &res_msg) :
- config(config), handler_msg(handler_msg), res_msg(res_msg) {
- instance_run_handler.reset(new InstanceRunHandler(memory_store));
- }
+ config(config), handler_msg(handler_msg), res_msg(res_msg) { }
int run();
- void pre_load();
- void pre_enable();
void init();
- const void* get_data_buffer(std::string name);
+ const void* get_data_buffer(const std::string &name);
private:
+ void pre_load();
+ void pre_enable();
void pre_load_plugin();
ErrorCode load_plugin(const std::string &path);
ErrorCode remove(const std::string &name);
ErrorCode query_all_plugins(std::string &res);
ErrorCode query_plugin(const std::string &name, std::string &res);
- ErrorCode query_top(const std::string &name, std::string &res);
- ErrorCode query_all_tops(std::string &res);
- ErrorCode instance_enabled(std::string name);
- ErrorCode instance_disabled(std::string name);
+ ErrorCode query_dependency(const std::string &name, std::string &res);
+ ErrorCode query_all_dependencies(std::string &res);
+ ErrorCode instance_enabled(const std::string &name);
+ ErrorCode instance_disabled(const std::string &name);
ErrorCode add_list(std::string &res);
ErrorCode download(const std::string &name, std::string &res);
std::string instance_dep_check(const std::string &name);
@@ -61,13 +54,9 @@ private:
SafeQueue<Message> &handler_msg;
SafeQueue<Message> &res_msg;
MemoryStore memory_store;
- DepHandler dep_handler;
- std::unordered_map<std::string, PluginType> plugin_types;
- static const std::string COLLECTOR_TEXT;
- static const std::string SCENARIO_TEXT;
- static const std::string TUNE_TEXT;
};
bool check_permission(std::string path, int mode);
bool file_exist(const std::string &file_name);
+
#endif
--
2.33.0