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. 12 : 13 : GekkoFS is free software: you can redistribute it and/or modify 14 : it under the terms of the GNU General Public License as published by 15 : the Free Software Foundation, either version 3 of the License, or 16 : (at your option) any later version. 17 : 18 : GekkoFS 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 General Public License for more details. 22 : 23 : You should have received a copy of the GNU General Public License 24 : along with GekkoFS. If not, see <https://www.gnu.org/licenses/>. 25 : 26 : SPDX-License-Identifier: GPL-3.0-or-later 27 : */ 28 : 29 : #include <daemon/util.hpp> 30 : #include <daemon/daemon.hpp> 31 : 32 : #include <common/rpc/rpc_util.hpp> 33 : 34 : using namespace std; 35 : 36 : namespace gkfs::utils { 37 : 38 : /** 39 : * @internal 40 : * Appends a single line to an existing shared hosts file with the RPC 41 : * connection information of this daemon. If it doesn't exist, it is created. 42 : * The line includes the hostname (and rootdir_suffix if applicable) and the RPC 43 : * server's listening address. 44 : * 45 : * NOTE, the shared file system must support strong consistency semantics to 46 : * ensure each daemon can write its information to the file even if the write 47 : * access is simultaneous. 48 : * @endinternal 49 : */ 50 : void 51 33 : populate_hosts_file() { 52 33 : const auto& hosts_file = GKFS_DATA->hosts_file(); 53 33 : GKFS_DATA->spdlogger()->debug("{}() Populating hosts file: '{}'", __func__, 54 33 : hosts_file); 55 33 : ofstream lfstream(hosts_file, ios::out | ios::app); 56 33 : if(!lfstream) { 57 0 : throw runtime_error(fmt::format("Failed to open hosts file '{}': {}", 58 0 : hosts_file, strerror(errno))); 59 : } 60 : // if rootdir_suffix is used, append it to hostname 61 33 : auto hostname = 62 33 : GKFS_DATA->rootdir_suffix().empty() 63 : ? gkfs::rpc::get_my_hostname(true) 64 0 : : fmt::format("{}#{}", gkfs::rpc::get_my_hostname(true), 65 66 : GKFS_DATA->rootdir_suffix()); 66 66 : lfstream << fmt::format("{} {}", hostname, RPC_DATA->self_addr_str()) 67 33 : << std::endl; 68 33 : if(!lfstream) { 69 0 : throw runtime_error( 70 0 : fmt::format("Failed to write on hosts file '{}': {}", 71 0 : hosts_file, strerror(errno))); 72 : } 73 33 : lfstream.close(); 74 33 : } 75 : 76 : /** 77 : * @internal 78 : * This function removes the entire hosts file even if just one daemon is 79 : * shutdown. This makes sense because the data distribution calculation would be 80 : * misaligned if the entry of the current daemon was only removed. 81 : * @endinternal 82 : */ 83 : void 84 33 : destroy_hosts_file() { 85 33 : std::remove(GKFS_DATA->hosts_file().c_str()); 86 33 : } 87 : 88 : } // namespace gkfs::utils