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

modularize address lookup

introduced `get_uri_from_hostname` function
parent 8349002d
Loading
Loading
Loading
Loading
+19 −16
Original line number Diff line number Diff line
@@ -184,6 +184,20 @@ hg_addr_t get_local_addr() {
    return margo_addr_lookup_retry(daemon_addr);
}

/*
 * Get the URI of a given hostname.
 *
 * This URI is to be used for margo lookup function.
 */
std::string get_uri_from_hostname(const std::string& hostname) {
    auto host = hostname + HOSTNAME_SUFFIX;
    // get the ip address from /etc/hosts which is mapped to the sys_hostfile map
    if (CTX->fs_conf()->sys_hostfile.count(host) == 1) {
        host = CTX->fs_conf()->sys_hostfile.at(host);
    }
    return fmt::format("{}://{}:{}", RPC_PROTOCOL, host, CTX->fs_conf()->rpc_port);
}

bool lookup_all_hosts() {
    rpc_addresses = std::make_unique<std::unordered_map<uint64_t, hg_addr_t>>();
    vector<uint64_t> hosts(CTX->fs_conf()->host_size);
@@ -197,26 +211,15 @@ bool lookup_all_hosts() {
    ::mt19937 g(rd()); // seed the random generator
    ::shuffle(hosts.begin(), hosts.end(), g); // Shuffle hosts vector
    // lookup addresses and put abstract server addresses into rpc_addresses
    hg_addr_t remote_addr = HG_ADDR_NULL;
    std::string remote_uri;
    for (auto& host : hosts) {
        auto hostname = CTX->fs_conf()->hosts.at(host) + HOSTNAME_SUFFIX;
        // get the ip address from /etc/hosts which is mapped to the sys_hostfile map
        if (CTX->fs_conf()->sys_hostfile.count(hostname) == 1) {
            auto remote_ip = CTX->fs_conf()->sys_hostfile.at(hostname);
            remote_uri = RPC_PROTOCOL + "://"s + remote_ip + ":"s + CTX->fs_conf()->rpc_port;
        } else {
            // fallback hostname to use for lookup
            remote_uri = RPC_PROTOCOL + "://"s + hostname + ":"s + CTX->fs_conf()->rpc_port;
        }

        remote_addr = margo_addr_lookup_retry(remote_uri);
        auto hostname = CTX->fs_conf()->hosts.at(host);
        auto uri = get_uri_from_hostname(hostname);
        auto remote_addr = margo_addr_lookup_retry(uri);
        if (remote_addr == HG_ADDR_NULL) {
            CTX->log()->error("{}() Failed to lookup address {}", __func__, remote_uri);
            CTX->log()->error("{}() Failed to lookup address {}", __func__, uri);
            return false;
        }
        CTX->log()->trace("generated remote_addr {} for hostname {} with rpc_port {}",
                remote_uri, hostname, CTX->fs_conf()->rpc_port);
        CTX->log()->trace("{}() Successful address lookup for '{}'", __func__, uri);
        rpc_addresses->insert(make_pair(host, remote_addr));
    }
    return true;