CAPIO-CL 1.2.0
CAPIO-CL: Cross Application Programmable I/O - Coordination Language
Loading...
Searching...
No Matches
monitor.h
1#ifndef CAPIO_CL_MONITOR_H
2#define CAPIO_CL_MONITOR_H
3
4#include <filesystem>
5#include <mutex>
6#include <set>
7#include <string>
8#include <thread>
9#include <unordered_map>
10#include <vector>
11
12#include "configuration.h"
13
14#ifndef PATH_MAX
15#define PATH_MAX 4096
16#endif
17
18#ifndef HOST_NAME_MAX
19#define HOST_NAME_MAX 1024
20#endif
21
24
26static const std::string NO_HOME_NODE = "<NONE>";
27
31class MonitorException final : public std::exception {
32 std::string message{};
33
34 public:
39 explicit MonitorException(const std::string &msg);
40
45 [[nodiscard]] const char *what() const noexcept override { return message.c_str(); }
46};
47
60 protected:
64 mutable std::mutex committed_lock;
65
69 mutable std::mutex home_node_lock;
70
74 mutable std::vector<std::string> _committed_files;
75
81 mutable std::unordered_map<std::string, std::string> _home_nodes;
82
86 mutable char _hostname[HOST_NAME_MAX] = {0};
87
88 public:
92 virtual ~MonitorInterface() = default;
93
100 virtual bool isCommitted(const std::filesystem::path &path) const;
101
107 virtual void setCommitted(const std::filesystem::path &path) const;
108
113 virtual void setHomeNode(const std::filesystem::path &path) const;
114
120 virtual const std::string &getHomeNode(const std::filesystem::path &path) const;
121};
122
133class MulticastMonitor final : public MonitorInterface {
134
135 static constexpr int MESSAGE_SIZE = (2 + PATH_MAX + PATH_MAX);
136
140 std::thread commit_thread, home_node_thread;
141
145 std::string MULTICAST_COMMIT_ADDR;
146
147 std::string MULTICAST_HOME_NODE_ADDR;
148
152 int MULTICAST_COMMIT_PORT{};
153
154 int MULTICAST_HOME_NODE_PORT{};
155
157 int MULTICAST_DELAY_MILLIS{};
158
162 typedef enum { SET = '!', GET = '?' } MESSAGE_COMMANDS;
163
172 static void _send_message(const std::string &ip_addr, int ip_port, const std::string &path,
173 MESSAGE_COMMANDS action);
174
187 [[noreturn]] static void commit_listener(std::vector<std::string> &committed_files,
188 std::mutex &lock, const std::string &ip_addr,
189 int ip_port);
190
203 [[noreturn]] static void
204 home_node_listener(std::unordered_map<std::string, std::string> &home_nodes, std::mutex &lock,
205 const std::string &ip_addr, int ip_port);
206
207 public:
214
219
220 bool isCommitted(const std::filesystem::path &path) const override;
221 void setCommitted(const std::filesystem::path &path) const override;
222 void setHomeNode(const std::filesystem::path &path) const override;
223 const std::string &getHomeNode(const std::filesystem::path &path) const override;
224};
225
234
235 typedef enum { COMMIT, HOME_NODE } CAPIO_CL_COMMIT_TOKEN_TYPES;
236
244 static std::filesystem::path
245 compute_capiocl_token_name(const std::filesystem::path &path,
246 CAPIO_CL_COMMIT_TOKEN_TYPES type = COMMIT);
247
255 static void generate_commit_token(const std::filesystem::path &path);
256
265 static void generate_home_node_token(const std::filesystem::path &path,
266 const std::string &home_node);
267
268 public:
273
277 ~FileSystemMonitor() override = default;
278
279 bool isCommitted(const std::filesystem::path &path) const override;
280 void setCommitted(const std::filesystem::path &path) const override;
281 void setHomeNode(const std::filesystem::path &path) const override;
282 const std::string &getHomeNode(const std::filesystem::path &path) const override;
283};
284
289class Monitor {
290 friend class Engine;
291 friend class MonitorInterface;
292
293 std::vector<const MonitorInterface *> interfaces;
294
295 public:
304 [[nodiscard]] bool isCommitted(const std::filesystem::path &path) const;
305
312 void setCommitted(std::filesystem::path path) const;
313
318 void registerMonitorBackend(const MonitorInterface *interface);
319
325 void setHomeNode(const std::filesystem::path &path) const;
326
332 [[nodiscard]] std::set<std::string> getHomeNode(const std::filesystem::path &path) const;
333
334 ~Monitor();
335};
336} // namespace capiocl::monitor
337
338#endif // CAPIO_CL_MONITOR_H
Load configuration and store it from a CAPIO-CL TOML configuration file.
Definition configuration.h:31
bool isCommitted(const std::filesystem::path &path) const override
Check whether the given file has been committed.
FileSystemMonitor()
Construct a filesystem-based commit monitor.
~FileSystemMonitor() override=default
Destructor for FileSystemMonitor.
void setHomeNode(const std::filesystem::path &path) const override
void setCommitted(const std::filesystem::path &path) const override
Mark the given file as committed.
const std::string & getHomeNode(const std::filesystem::path &path) const override
MonitorException(const std::string &msg)
Construct a new CAPIO-CL Exception.
Definition Monitor.cpp:5
const char * what() const noexcept override
Definition monitor.h:45
Abstract interface for monitoring the commit state of files in CAPIO-CL.
Definition monitor.h:59
virtual bool isCommitted(const std::filesystem::path &path) const
Check whether the given file has been committed.
std::vector< std::string > _committed_files
List of committed file paths stored as strings.
Definition monitor.h:74
virtual void setCommitted(const std::filesystem::path &path) const
Mark the given file as committed.
virtual void setHomeNode(const std::filesystem::path &path) const
std::mutex committed_lock
Mutex protecting access to the committed file list.
Definition monitor.h:64
std::unordered_map< std::string, std::string > _home_nodes
Lookup table to get home node staring from path. Key = file path; Value = hostname of home node.
Definition monitor.h:81
virtual const std::string & getHomeNode(const std::filesystem::path &path) const
std::mutex home_node_lock
Mutex protecting access to the home nodes list.
Definition monitor.h:69
char _hostname[HOST_NAME_MAX]
hostname of the current instance
Definition monitor.h:86
virtual ~MonitorInterface()=default
Virtual destructor for safe polymorphic deletion.
Class to monitor runtime dependent information on CAPIO-CL related paths, such as commitment status a...
Definition monitor.h:289
void setHomeNode(const std::filesystem::path &path) const
Definition Monitor.cpp:22
bool isCommitted(const std::filesystem::path &path) const
Definition Monitor.cpp:9
std::set< std::string > getHomeNode(const std::filesystem::path &path) const
Definition Monitor.cpp:28
void setCommitted(std::filesystem::path path) const
Definition Monitor.cpp:14
void registerMonitorBackend(const MonitorInterface *interface)
Definition Monitor.cpp:19
const std::string & getHomeNode(const std::filesystem::path &path) const override
MulticastMonitor(const capiocl::configuration::CapioClConfiguration &config)
Construct a multicast-based monitor.
void setCommitted(const std::filesystem::path &path) const override
Mark the given file as committed.
void setHomeNode(const std::filesystem::path &path) const override
~MulticastMonitor() override
Destructor; stops listener thread and cleans resources.
bool isCommitted(const std::filesystem::path &path) const override
Check whether the given file has been committed.
Namespace containing the CAPIO-CL Monitor components.
Definition monitor.h:23