1#ifndef CAPIO_CL_MONITOR_H
2#define CAPIO_CL_MONITOR_H
13#include <unordered_map>
16#include "configuration.h"
28static const std::string NO_HOME_NODE =
"<NONE>";
34 std::string message{};
47 [[nodiscard]]
const char *
what() const noexcept
override {
return message.c_str(); }
83 mutable std::unordered_map<std::string, std::string>
_home_nodes;
102 virtual bool isCommitted(
const std::filesystem::path &path)
const;
116 long threshold)
const;
122 virtual void setHomeNode(
const std::filesystem::path &path)
const;
129 virtual std::string
getHomeNode(
const std::filesystem::path &path)
const;
149 std::thread commit_thread, home_node_thread;
154 std::string MULTICAST_COMMIT_ADDR;
156 std::string MULTICAST_HOME_NODE_ADDR;
159 std::atomic<bool> terminate =
false;
162 mutable std::unordered_map<std::string, std::unordered_map<std::string, std::uint64_t>>
164 mutable std::mutex close_count_lock;
165 std::string close_count_origin;
168 static constexpr int MULTICAST_THREAD_POLL_INTERVAL = 250;
173 int MULTICAST_COMMIT_PORT{};
175 int MULTICAST_HOME_NODE_PORT{};
178 int MULTICAST_DELAY_MILLIS{};
183 typedef enum { SET =
'!', GET =
'?', COUNT_SET =
'C', COUNT_GET =
'Q' } MESSAGE_COMMANDS;
193 static void _send_message(
const std::string &ip_addr,
int ip_port,
const std::string &payload,
194 MESSAGE_COMMANDS action);
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>>
213 std::mutex &close_count_lock,
const std::string &ip_addr,
int ip_port,
214 const std::atomic<bool> *terminate);
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);
246 bool isCommitted(
const std::filesystem::path &path)
const override;
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;
263 typedef enum { COMMIT, HOME_NODE } CAPIO_CL_COMMIT_TOKEN_TYPES;
264 std::filesystem::path metadata_root;
273 static std::filesystem::path
274 compute_capiocl_token_name(
const std::filesystem::path &path,
275 CAPIO_CL_COMMIT_TOKEN_TYPES type = COMMIT);
284 static void generate_commit_token(
const std::filesystem::path &path);
294 static void generate_home_node_token(
const std::filesystem::path &path,
295 const std::string &home_node);
314 bool isCommitted(
const std::filesystem::path &path)
const override;
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;
328 friend class MonitorInterface;
330 std::vector<const MonitorInterface *> interfaces;
341 [[nodiscard]]
bool isCommitted(
const std::filesystem::path &path)
const;
352 [[nodiscard]]
bool increaseCloseCount(
const std::filesystem::path &path,
long threshold)
const;
365 void setHomeNode(
const std::filesystem::path &path)
const;
372 [[nodiscard]] std::set<std::string>
getHomeNode(
const std::filesystem::path &path)
const;
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