Unverified Commit a0f3d6ca authored by Tommaso Tocci's avatar Tommaso Tocci
Browse files

Better lookup-file loading routine

 - Use regular expression to parse lines
 - More consistent error handling
 - More comprehensive error messages
parent 4ba3c5e7
Loading
Loading
Loading
Loading
+1 −1
Original line number Diff line number Diff line
@@ -40,7 +40,7 @@ struct FsConfig {

    // rpc infos
    std::map<uint64_t, std::string> hosts;
    std::unordered_map<std::string, std::string> endpoints;
    std::map<std::string, std::string> endpoints;
    uint64_t host_id; // my host number
    size_t host_size;
    std::string hostname_suffix;
+2 −2
Original line number Diff line number Diff line
@@ -20,7 +20,7 @@
// third party libs
#include <string>
#include <iostream>
#include <unordered_map>
#include <map>

extern "C" {
#include <margo.h>
@@ -67,7 +67,7 @@ int metadata_to_stat(const std::string& path, const Metadata& md, struct stat& a

int get_daemon_pid();

std::unordered_map<std::string, std::string> load_lookup_file(const std::string& lfpath);
std::map<std::string, std::string> load_lookup_file(const std::string& lfpath);

hg_addr_t get_local_addr();

+22 −17
Original line number Diff line number Diff line
@@ -18,6 +18,7 @@

#include <fstream>
#include <sstream>
#include <regex>
#include <csignal>
#include <random>
#include <sys/sysmacros.h>
@@ -132,28 +133,32 @@ int get_daemon_pid() {
    return adafs_daemon_pid;
}

unordered_map<string, string> load_lookup_file(const std::string& lfpath) {
    CTX->log()->debug("{}() Loading lookup file: '{}'",
                      __func__, lfpath);
map<string, string> load_lookup_file(const std::string& lfpath) {
    CTX->log()->debug("{}() Loading lookup file: '{}'", __func__, lfpath);
    ifstream lf(lfpath);
    lf.exceptions(ifstream::badbit);

    unordered_map<string, string> endpoints_map;
    if (!lf) {
        throw runtime_error(fmt::format("Failed to open lookup file '{}': {}",
                            lfpath, strerror(errno)));
    }
    map<string, string> hostmap;
    const regex line_re("^(\\S+)\\s+(\\S+)$",
                        regex::ECMAScript | regex::optimize);
    string line;
    string hostname;
    string endpoint;
    string::size_type delim_pos;
    string host;
    string uri;
    std::smatch match;
    while (getline(lf, line)) {
        delim_pos = line.find(" ", delim_pos = 0);
        if(delim_pos == string::npos) {
            throw runtime_error(fmt::format("Failed to parse line in lookup file: '{}'", line));
        if (!regex_match(line, match, line_re)) {
            spdlog::error("{}() Unrecognized line format: [path: '{}', line: '{}']",
                          __func__, lfpath, line);
            throw runtime_error(
                    fmt::format("unrecognized line format: '{}'", line));
        }
        hostname = line.substr(0, delim_pos);
        endpoint = line.substr(delim_pos + 1);
        CTX->log()->trace("{}() endpoint loaded: '{}' '{}'", __func__, hostname, endpoint);
        endpoints_map.insert(make_pair(hostname, endpoint));
        host = match[1];
        uri = match[2];
        hostmap.emplace(host, uri);
    }
    return endpoints_map;
    return hostmap;
}

hg_addr_t margo_addr_lookup_retry(const std::string& uri) {