LCOV - code coverage report
Current view: top level - src/client - preload_util.cpp (source / functions) Hit Total Coverage
Test: coverage.info Lines: 143 205 69.8 %
Date: 2024-04-23 00:09:24 Functions: 9 9 100.0 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : /*
       2             :   Copyright 2018-2024, Barcelona Supercomputing Center (BSC), Spain
       3             :   Copyright 2015-2024, Johannes Gutenberg Universitaet Mainz, Germany
       4             : 
       5             :   This software was partially supported by the
       6             :   EC H2020 funded project NEXTGenIO (Project ID: 671951, www.nextgenio.eu).
       7             : 
       8             :   This software was partially supported by the
       9             :   ADA-FS project under the SPPEXA project funded by the DFG.
      10             : 
      11             :   This file is part of GekkoFS' POSIX interface.
      12             : 
      13             :   GekkoFS' POSIX interface is free software: you can redistribute it and/or
      14             :   modify it under the terms of the GNU Lesser General Public License as
      15             :   published by the Free Software Foundation, either version 3 of the License,
      16             :   or (at your option) any later version.
      17             : 
      18             :   GekkoFS' POSIX interface is distributed in the hope that it will be useful,
      19             :   but WITHOUT ANY WARRANTY; without even the implied warranty of
      20             :   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
      21             :   GNU Lesser General Public License for more details.
      22             : 
      23             :   You should have received a copy of the GNU Lesser General Public License
      24             :   along with GekkoFS' POSIX interface.  If not, see
      25             :   <https://www.gnu.org/licenses/>.
      26             : 
      27             :   SPDX-License-Identifier: LGPL-3.0-or-later
      28             : */
      29             : 
      30             : #include <client/preload_util.hpp>
      31             : #include <client/env.hpp>
      32             : #include <client/logging.hpp>
      33             : #include <client/rpc/forward_metadata.hpp>
      34             : 
      35             : #include <common/rpc/distributor.hpp>
      36             : #include <common/rpc/rpc_util.hpp>
      37             : #include <common/env_util.hpp>
      38             : #include <common/common_defs.hpp>
      39             : 
      40             : #include <hermes.hpp>
      41             : 
      42             : #include <fstream>
      43             : #include <sstream>
      44             : #include <regex>
      45             : #include <csignal>
      46             : #include <random>
      47             : 
      48             : extern "C" {
      49             : #include <sys/sysmacros.h>
      50             : }
      51             : 
      52             : using namespace std;
      53             : 
      54             : namespace {
      55             : 
      56             : /**
      57             :  * Looks up a host endpoint via Hermes
      58             :  * @param uri
      59             :  * @param max_retries
      60             :  * @return hermes endpoint, if successful
      61             :  * @throws std::runtime_error
      62             :  */
      63             : hermes::endpoint
      64         290 : lookup_endpoint(const std::string& uri, std::size_t max_retries = 3) {
      65             : 
      66         290 :     LOG(DEBUG, "Looking up address \"{}\"", uri);
      67             : 
      68         290 :     std::random_device rd; // obtain a random number from hardware
      69         290 :     std::size_t attempts = 0;
      70         580 :     std::string error_msg;
      71             : 
      72         290 :     do {
      73         290 :         try {
      74         580 :             return ld_network_service->lookup(uri);
      75           0 :         } catch(const exception& ex) {
      76           0 :             error_msg = ex.what();
      77             : 
      78           0 :             LOG(WARNING, "Failed to lookup address '{}'. Attempts [{}/{}]", uri,
      79           0 :                 attempts + 1, max_retries);
      80             : 
      81             :             // Wait a random amount of time and try again
      82           0 :             std::mt19937 g(rd()); // seed the random generator
      83           0 :             std::uniform_int_distribution<> distr(
      84           0 :                     50, 50 * (attempts + 2)); // define the range
      85           0 :             std::this_thread::sleep_for(std::chrono::milliseconds(distr(g)));
      86           0 :             continue;
      87             :         }
      88           0 :     } while(++attempts < max_retries);
      89             : 
      90           0 :     throw std::runtime_error(
      91           0 :             fmt::format("Endpoint for address '{}' could not be found ({})",
      92           0 :                         uri, error_msg));
      93             : }
      94             : 
      95             : /**
      96             :  * extracts protocol from a given URI generated by the RPC server of the daemon
      97             :  * @param uri
      98             :  * @throws std::runtime_error
      99             :  */
     100             : void
     101         269 : extract_protocol(const string& uri) {
     102         269 :     if(uri.rfind("://") == string::npos) {
     103             :         // invalid format. kill client
     104           0 :         throw runtime_error(fmt::format("Invalid format for URI: '{}'", uri));
     105             :     }
     106         269 :     string protocol{};
     107             : 
     108         269 :     if(uri.find(gkfs::rpc::protocol::ofi_sockets) != string::npos) {
     109         269 :         protocol = gkfs::rpc::protocol::ofi_sockets;
     110           0 :     } else if(uri.find(gkfs::rpc::protocol::ofi_psm2) != string::npos) {
     111           0 :         protocol = gkfs::rpc::protocol::ofi_psm2;
     112           0 :     } else if(uri.find(gkfs::rpc::protocol::ofi_verbs) != string::npos) {
     113           0 :         protocol = gkfs::rpc::protocol::ofi_verbs;
     114             :     }
     115             :     // check for shared memory protocol. Can be plain shared memory or real ofi
     116             :     // protocol + auto_sm
     117         269 :     if(uri.find(gkfs::rpc::protocol::na_sm) != string::npos) {
     118           0 :         if(protocol.empty())
     119           0 :             protocol = gkfs::rpc::protocol::na_sm;
     120             :         else
     121           0 :             CTX->auto_sm(true);
     122             :     }
     123         269 :     if(protocol.empty()) {
     124             :         // unsupported protocol. kill client
     125           0 :         throw runtime_error(fmt::format(
     126             :                 "Unsupported RPC protocol found in hosts file with URI: '{}'",
     127           0 :                 uri));
     128             :     }
     129         269 :     LOG(INFO,
     130             :         "RPC protocol '{}' extracted from hosts file. Using auto_sm is '{}'",
     131         269 :         protocol, CTX->auto_sm());
     132         269 :     CTX->rpc_protocol(protocol);
     133         269 : }
     134             : 
     135             : /**
     136             :  * Reads the daemon generator hosts file by a given path, returning hosts and
     137             :  * URI addresses
     138             :  * @param path to hosts file
     139             :  * @return vector<pair<hosts, URI>>
     140             :  * @throws std::runtime_error
     141             :  */
     142             : vector<pair<string, string>>
     143         269 : load_hostfile(const std::string& path) {
     144             : 
     145         269 :     LOG(DEBUG, "Loading hosts file: \"{}\"", path);
     146             : 
     147         538 :     ifstream lf(path);
     148         269 :     if(!lf) {
     149           0 :         throw runtime_error(fmt::format("Failed to open hosts file '{}': {}",
     150           0 :                                         path, strerror(errno)));
     151             :     }
     152         269 :     vector<pair<string, string>> hosts;
     153         269 :     const regex line_re("^(\\S+)\\s+(\\S+)$",
     154         269 :                         regex::ECMAScript | regex::optimize);
     155         538 :     string line;
     156         269 :     string host;
     157         269 :     string uri;
     158         538 :     std::smatch match;
     159         559 :     while(getline(lf, line)) {
     160         290 :         if(!regex_match(line, match, line_re)) {
     161             : 
     162           0 :             LOG(ERROR, "Unrecognized line format: [path: '{}', line: '{}']",
     163           0 :                 path, line);
     164             : 
     165           0 :             throw runtime_error(
     166           0 :                     fmt::format("unrecognized line format: '{}'", line));
     167             :         }
     168         290 :         host = match[1];
     169         290 :         uri = match[2];
     170         290 :         hosts.emplace_back(host, uri);
     171             :     }
     172         269 :     if(hosts.empty()) {
     173           0 :         throw runtime_error(
     174           0 :                 "Hosts file found but no suitable addresses could be extracted");
     175             :     }
     176         269 :     extract_protocol(hosts[0].second);
     177             :     // sort hosts so that data always hashes to the same place during restart
     178         269 :     std::sort(hosts.begin(), hosts.end());
     179             :     // remove rootdir suffix from host after sorting as no longer required
     180         559 :     for(auto& h : hosts) {
     181         290 :         auto idx = h.first.rfind("#");
     182         290 :         if(idx != string::npos)
     183           0 :             h.first.erase(idx, h.first.length());
     184             :     }
     185         538 :     return hosts;
     186             : }
     187             : 
     188             : } // namespace
     189             : 
     190             : namespace gkfs::utils {
     191             : 
     192             : 
     193             : /**
     194             :  * Retrieve metadata from daemon and return Metadata object
     195             :  * errno may be set
     196             :  * @param path
     197             :  * @param follow_links
     198             :  * @return Metadata
     199             :  */
     200             : optional<gkfs::metadata::Metadata>
     201        1389 : get_metadata(const string& path, bool follow_links) {
     202        2778 :     std::string attr;
     203        1389 :     auto err = gkfs::rpc::forward_stat(path, attr, 0);
     204             :     // TODO: retry on failure
     205             : 
     206        1389 :     if(err) {
     207          24 :         auto copy = 1;
     208          24 :         while(copy < CTX->get_replicas() + 1 && err) {
     209           0 :             LOG(ERROR, "Retrying Stat on replica {} {}", copy, follow_links);
     210           0 :             err = gkfs::rpc::forward_stat(path, attr, copy);
     211           0 :             copy++;
     212             :         }
     213          24 :         if(err) {
     214          24 :             errno = err;
     215          24 :             return {};
     216             :         }
     217             :     }
     218             : #ifdef HAS_SYMLINKS
     219        1365 :     if(follow_links) {
     220         150 :         gkfs::metadata::Metadata md{attr};
     221          75 :         while(md.is_link()) {
     222           0 :             err = gkfs::rpc::forward_stat(md.target_path(), attr, 0);
     223           0 :             if(err) {
     224           0 :                 errno = err;
     225           0 :                 return {};
     226             :             }
     227           0 :             md = gkfs::metadata::Metadata{attr};
     228             :         }
     229             :     }
     230             : #endif
     231        2730 :     return gkfs::metadata::Metadata{attr};
     232             : }
     233             : 
     234             : 
     235             : /**
     236             :  * Converts the Metadata object into a stat struct, which is needed by Linux
     237             :  * @param path
     238             :  * @param md
     239             :  * @param attr
     240             :  * @return
     241             :  */
     242             : int
     243          56 : metadata_to_stat(const std::string& path, const gkfs::metadata::Metadata& md,
     244             :                  struct stat& attr) {
     245             : 
     246             :     /* Populate default values */
     247          56 :     attr.st_dev = makedev(0, 0);
     248          56 :     attr.st_ino = std::hash<std::string>{}(path);
     249          56 :     attr.st_nlink = 1;
     250          56 :     attr.st_uid = CTX->fs_conf()->uid;
     251          56 :     attr.st_gid = CTX->fs_conf()->gid;
     252          56 :     attr.st_rdev = 0;
     253          56 :     attr.st_blksize = gkfs::config::rpc::chunksize;
     254          56 :     attr.st_blocks = 0;
     255             : 
     256          56 :     memset(&attr.st_atim, 0, sizeof(timespec));
     257          56 :     memset(&attr.st_mtim, 0, sizeof(timespec));
     258          56 :     memset(&attr.st_ctim, 0, sizeof(timespec));
     259             : 
     260          56 :     attr.st_mode = md.mode();
     261             : 
     262             : #ifdef HAS_SYMLINKS
     263          56 :     if(md.is_link())
     264           0 :         attr.st_size = md.target_path().size() + CTX->mountdir().size();
     265             :     else
     266             : #endif
     267          56 :         attr.st_size = md.size();
     268             : 
     269          56 :     if(CTX->fs_conf()->atime_state) {
     270          56 :         attr.st_atim.tv_sec = md.atime();
     271             :     }
     272          56 :     if(CTX->fs_conf()->mtime_state) {
     273          56 :         attr.st_mtim.tv_sec = md.mtime();
     274             :     }
     275          56 :     if(CTX->fs_conf()->ctime_state) {
     276          56 :         attr.st_ctim.tv_sec = md.ctime();
     277             :     }
     278          56 :     if(CTX->fs_conf()->link_cnt_state) {
     279          56 :         attr.st_nlink = md.link_count();
     280             :     }
     281          56 :     if(CTX->fs_conf()->blocks_state) {
     282          56 :         attr.st_blocks = md.blocks();
     283             :     } else {
     284           0 :         attr.st_blocks = md.size() / gkfs::config::syscall::stat::st_nblocksize;
     285             :     }
     286          56 :     return 0;
     287             : }
     288             : 
     289             : map<string, uint64_t>
     290          42 : load_forwarding_map_file(const std::string& lfpath) {
     291             : 
     292          42 :     LOG(DEBUG, "Loading forwarding map file file: \"{}\"", lfpath);
     293             : 
     294          84 :     ifstream lf(lfpath);
     295          42 :     if(!lf) {
     296           0 :         throw runtime_error(
     297           0 :                 fmt::format("Failed to open forwarding map file '{}': {}",
     298           0 :                             lfpath, strerror(errno)));
     299             :     }
     300          42 :     map<string, uint64_t> forwarding_map;
     301          42 :     const regex line_re("^(\\S+)\\s+(\\S+)$",
     302          42 :                         regex::ECMAScript | regex::optimize);
     303          84 :     string line;
     304          42 :     string host;
     305          42 :     uint64_t forwarder;
     306          84 :     std::smatch match;
     307          84 :     while(getline(lf, line)) {
     308          42 :         if(!regex_match(line, match, line_re)) {
     309             : 
     310           0 :             LOG(ERROR, "Unrecognized line format: [path: '{}', line: '{}']",
     311           0 :                 lfpath, line);
     312             : 
     313           0 :             throw runtime_error(
     314           0 :                     fmt::format("unrecognized line format: '{}'", line));
     315             :         }
     316          42 :         host = match[1];
     317          84 :         forwarder = std::stoi(match[2].str());
     318          42 :         forwarding_map[host] = forwarder;
     319             :     }
     320          84 :     return forwarding_map;
     321             : }
     322             : 
     323             : void
     324          42 : load_forwarding_map() {
     325          42 :     string forwarding_map_file;
     326             : 
     327          42 :     forwarding_map_file = gkfs::env::get_var(
     328          84 :             gkfs::env::FORWARDING_MAP_FILE, gkfs::config::forwarding_file_path);
     329             : 
     330          84 :     map<string, uint64_t> forwarding_map;
     331             : 
     332          84 :     while(forwarding_map.size() == 0) {
     333          42 :         try {
     334          84 :             forwarding_map = load_forwarding_map_file(forwarding_map_file);
     335           0 :         } catch(const exception& e) {
     336           0 :             auto emsg = fmt::format("Failed to load forwarding map file: {}",
     337           0 :                                     e.what());
     338           0 :             throw runtime_error(emsg);
     339             :         }
     340             :     }
     341             : 
     342             :     // if (forwarding_map.size() == 0) {
     343             :     //    throw runtime_error(fmt::format("Forwarding map file is empty: '{}'",
     344             :     //    forwarding_map_file));
     345             :     //}
     346             : 
     347          84 :     auto local_hostname = gkfs::rpc::get_my_hostname(true);
     348             : 
     349          42 :     if(forwarding_map.find(local_hostname) == forwarding_map.end()) {
     350           0 :         throw runtime_error(
     351           0 :                 fmt::format("Unable to determine the forwarder for host: '{}'",
     352           0 :                             local_hostname));
     353             :     }
     354          42 :     LOG(INFO, "Forwarding map loaded for '{}' as '{}'", local_hostname,
     355          42 :         forwarding_map[local_hostname]);
     356             : 
     357          42 :     CTX->fwd_host_id(forwarding_map[local_hostname]);
     358          42 : }
     359             : 
     360             : vector<pair<string, string>>
     361         269 : read_hosts_file() {
     362         269 :     string hostfile;
     363             : 
     364         269 :     hostfile = gkfs::env::get_var(gkfs::env::HOSTS_FILE,
     365         538 :                                   gkfs::config::hostfile_path);
     366             : 
     367         269 :     vector<pair<string, string>> hosts;
     368         269 :     try {
     369         538 :         hosts = load_hostfile(hostfile);
     370           0 :     } catch(const exception& e) {
     371           0 :         auto emsg = fmt::format("Failed to load hosts file: {}", e.what());
     372           0 :         throw runtime_error(emsg);
     373             :     }
     374             : 
     375         269 :     if(hosts.empty()) {
     376           0 :         throw runtime_error(fmt::format("Hostfile empty: '{}'", hostfile));
     377             :     }
     378             : 
     379         269 :     LOG(INFO, "Hosts pool size: {}", hosts.size());
     380         269 :     return hosts;
     381             : }
     382             : 
     383             : /**
     384             :  * Connects to daemons and lookup Mercury URI addresses via Hermes
     385             :  * @param hosts vector<pair<hostname, Mercury URI address>>
     386             :  * @throws std::runtime_error through lookup_endpoint()
     387             :  */
     388             : void
     389         269 : connect_to_hosts(const vector<pair<string, string>>& hosts) {
     390         269 :     auto local_hostname = gkfs::rpc::get_my_hostname(true);
     391         269 :     bool local_host_found = false;
     392             : 
     393         538 :     std::vector<hermes::endpoint> addrs;
     394         269 :     addrs.resize(hosts.size());
     395             : 
     396         538 :     vector<uint64_t> host_ids(hosts.size());
     397             :     // populate vector with [0, ..., host_size - 1]
     398         269 :     ::iota(::begin(host_ids), ::end(host_ids), 0);
     399             :     /*
     400             :      * Shuffle hosts to balance addr lookups to all hosts
     401             :      * Too many concurrent lookups send to same host
     402             :      * could overwhelm the server,
     403             :      * returning error when addr lookup
     404             :      */
     405         538 :     ::random_device rd; // obtain a random number from hardware
     406         269 :     ::mt19937 g(rd());  // seed the random generator
     407         269 :     ::shuffle(host_ids.begin(), host_ids.end(), g); // Shuffle hosts vector
     408             :     // lookup addresses and put abstract server addresses into rpc_addresses
     409             : 
     410         559 :     for(const auto& id : host_ids) {
     411         290 :         const auto& hostname = hosts.at(id).first;
     412         290 :         const auto& uri = hosts.at(id).second;
     413             : 
     414         290 :         addrs[id] = lookup_endpoint(uri);
     415             : 
     416         290 :         if(!local_host_found && hostname == local_hostname) {
     417         269 :             LOG(DEBUG, "Found local host: {}", hostname);
     418         269 :             CTX->local_host_id(id);
     419             :             local_host_found = true;
     420             :         }
     421             : 
     422         580 :         LOG(DEBUG, "Found peer: {}", addrs[id].to_string());
     423             :     }
     424             : 
     425         269 :     if(!local_host_found) {
     426           0 :         LOG(WARNING, "Failed to find local host. Using host '0' as local host");
     427           0 :         CTX->local_host_id(0);
     428             :     }
     429             : 
     430         269 :     CTX->hosts(addrs);
     431         269 : }
     432             : 
     433             : } // namespace gkfs::utils

Generated by: LCOV version 1.16