CAPIO-CL 2.0.1
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 <atomic>
5#include <cstddef>
6#include <cstdint>
7#include <filesystem>
8#include <mutex>
9#include <optional>
10#include <set>
11#include <string>
12#include <thread>
13#include <unordered_map>
14#include <vector>
15
16#include "configuration.h"
17
20
22inline constexpr std::size_t HOSTNAME_BUFFER_SIZE = 1024;
23
25inline constexpr std::size_t PATH_BUFFER_SIZE = 4096;
26
28static const std::string NO_HOME_NODE = "<NONE>";
29
33class MonitorException final : public std::exception {
34 std::string message{};
35
36 public:
41 explicit MonitorException(const std::string &msg);
42
47 [[nodiscard]] const char *what() const noexcept override { return message.c_str(); }
48};
49
62 protected:
66 mutable std::mutex committed_lock;
67
71 mutable std::mutex home_node_lock;
72
76 mutable std::vector<std::string> _committed_files;
77
83 mutable std::unordered_map<std::string, std::string> _home_nodes;
84
88 mutable char _hostname[HOSTNAME_BUFFER_SIZE] = {0};
89
90 public:
94 virtual ~MonitorInterface() = default;
95
102 virtual bool isCommitted(const std::filesystem::path &path) const;
103
109 virtual void setCommitted(const std::filesystem::path &path) const;
110
115 virtual std::optional<bool> increaseCloseCount(const std::filesystem::path &path,
116 long threshold) const;
117
122 virtual void setHomeNode(const std::filesystem::path &path) const;
123
129 virtual std::string getHomeNode(const std::filesystem::path &path) const;
130};
131
142class MulticastMonitor final : public MonitorInterface {
143
144 static constexpr int MESSAGE_SIZE = 2 + (2 * PATH_BUFFER_SIZE);
145
149 std::thread commit_thread, home_node_thread;
150
154 std::string MULTICAST_COMMIT_ADDR;
155
156 std::string MULTICAST_HOME_NODE_ADDR;
157
159 std::atomic<bool> terminate = false;
160
162 mutable std::unordered_map<std::string, std::unordered_map<std::string, std::uint64_t>>
163 close_counts;
164 mutable std::mutex close_count_lock;
165 std::string close_count_origin;
166
168 static constexpr int MULTICAST_THREAD_POLL_INTERVAL = 250;
169
173 int MULTICAST_COMMIT_PORT{};
174
175 int MULTICAST_HOME_NODE_PORT{};
176
178 int MULTICAST_DELAY_MILLIS{};
179
183 typedef enum { SET = '!', GET = '?', COUNT_SET = 'C', COUNT_GET = 'Q' } MESSAGE_COMMANDS;
184
193 static void _send_message(const std::string &ip_addr, int ip_port, const std::string &payload,
194 MESSAGE_COMMANDS action);
195
209 static void
210 commit_listener(std::vector<std::string> &committed_files, std::mutex &lock,
211 std::unordered_map<std::string, std::unordered_map<std::string, std::uint64_t>>
212 &close_counts,
213 std::mutex &close_count_lock, const std::string &ip_addr, int ip_port,
214 const std::atomic<bool> *terminate);
215
229 static void home_node_listener(std::unordered_map<std::string, std::string> &home_nodes,
230 std::mutex &lock, const std::string &ip_addr, int ip_port,
231 const std::atomic<bool> *terminate);
232
233 public:
240
245
246 bool isCommitted(const std::filesystem::path &path) const override;
247 void setCommitted(const std::filesystem::path &path) const override;
248 std::optional<bool> increaseCloseCount(const std::filesystem::path &path,
249 long threshold) const override;
250 void setHomeNode(const std::filesystem::path &path) const override;
251 std::string getHomeNode(const std::filesystem::path &path) const override;
252};
253
262
263 typedef enum { COMMIT, HOME_NODE } CAPIO_CL_COMMIT_TOKEN_TYPES;
264 std::filesystem::path metadata_root;
265
273 static std::filesystem::path
274 compute_capiocl_token_name(const std::filesystem::path &path,
275 CAPIO_CL_COMMIT_TOKEN_TYPES type = COMMIT);
276
284 static void generate_commit_token(const std::filesystem::path &path);
285
294 static void generate_home_node_token(const std::filesystem::path &path,
295 const std::string &home_node);
296
297 public:
302
308
312 ~FileSystemMonitor() override = default;
313
314 bool isCommitted(const std::filesystem::path &path) const override;
315 void setCommitted(const std::filesystem::path &path) const override;
316 std::optional<bool> increaseCloseCount(const std::filesystem::path &path,
317 long threshold) const override;
318 void setHomeNode(const std::filesystem::path &path) const override;
319 std::string getHomeNode(const std::filesystem::path &path) const override;
320};
321
326class Monitor {
327 friend class Engine;
328 friend class MonitorInterface;
329
330 std::vector<const MonitorInterface *> interfaces;
331
332 public:
341 [[nodiscard]] bool isCommitted(const std::filesystem::path &path) const;
342
349 void setCommitted(std::filesystem::path path) const;
350
352 [[nodiscard]] bool increaseCloseCount(const std::filesystem::path &path, long threshold) const;
353
358 void registerMonitorBackend(const MonitorInterface *interface);
359
365 void setHomeNode(const std::filesystem::path &path) const;
366
372 [[nodiscard]] std::set<std::string> getHomeNode(const std::filesystem::path &path) const;
373
374 ~Monitor();
375};
376} // namespace capiocl::monitor
377
378#endif // CAPIO_CL_MONITOR_H
Load configuration and store it from a CAPIO-CL TOML configuration file.
Definition configuration.h:41
bool isCommitted(const std::filesystem::path &path) const override
Check whether the given file has been committed.
std::string getHomeNode(const std::filesystem::path &path) const override
FileSystemMonitor()
Construct a filesystem-based commit monitor.
std::optional< bool > increaseCloseCount(const std::filesystem::path &path, long threshold) const override
Atomically record a close and commit when threshold is reached.
FileSystemMonitor(const configuration::CapioClConfiguration &config)
Construct a filesystem-based monitor using runtime configuration.
~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.
MonitorException(const std::string &msg)
Construct a new CAPIO-CL Exception.
Definition Monitor.cpp:5
const char * what() const noexcept override
Definition monitor.h:47
Abstract interface for monitoring the commit state of files in CAPIO-CL.
Definition monitor.h:61
virtual bool isCommitted(const std::filesystem::path &path) const
Check whether the given file has been committed.
char _hostname[HOSTNAME_BUFFER_SIZE]
hostname of the current instance
Definition monitor.h:88
std::vector< std::string > _committed_files
List of committed file paths stored as strings.
Definition monitor.h:76
virtual void setCommitted(const std::filesystem::path &path) const
Mark the given file as committed.
virtual void setHomeNode(const std::filesystem::path &path) const
virtual std::string getHomeNode(const std::filesystem::path &path) const
std::mutex committed_lock
Mutex protecting access to the committed file list.
Definition monitor.h:66
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:83
std::mutex home_node_lock
Mutex protecting access to the home nodes list.
Definition monitor.h:71
virtual std::optional< bool > increaseCloseCount(const std::filesystem::path &path, long threshold) const
Atomically record a close and commit when threshold is reached.
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:326
bool increaseCloseCount(const std::filesystem::path &path, long threshold) const
Record a close independently in every counter-capable backend.
Definition Monitor.cpp:27
void setHomeNode(const std::filesystem::path &path) const
Definition Monitor.cpp:58
bool isCommitted(const std::filesystem::path &path) const
Definition Monitor.cpp:11
std::set< std::string > getHomeNode(const std::filesystem::path &path) const
Definition Monitor.cpp:66
void setCommitted(std::filesystem::path path) const
Definition Monitor.cpp:19
void registerMonitorBackend(const MonitorInterface *interface)
Definition Monitor.cpp:52
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
std::optional< bool > increaseCloseCount(const std::filesystem::path &path, long threshold) const override
Atomically record a close and commit when threshold is reached.
~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.
std::string getHomeNode(const std::filesystem::path &path) const override
Namespace containing the CAPIO-CL Monitor components.
Definition monitor.h:19
constexpr std::size_t PATH_BUFFER_SIZE
Maximum path storage used in multicast monitor messages.
Definition monitor.h:25
constexpr std::size_t HOSTNAME_BUFFER_SIZE
Buffer size used to store a null-terminated host name.
Definition monitor.h:22