Commit 443b9949 authored by sevenuz's avatar sevenuz
Browse files

client: RPC timeout/retry and optional preconnect to all daemons

Apply client-rpc-retry-preconnect.patch unchanged (the build used for the
MN5 HACC rerun): forward_call() retries transport failures with a
per-attempt timeout (LIBGKFS_RPC_TIMEOUT_MS, LIBGKFS_RPC_RETRIES) and
randomised backoff; LIBGKFS_PRECONNECT=1 contacts every daemon once at
start-up.

Co-Authored-By: default avatarClaude Opus 5.5 <noreply@anthropic.com>
parent 6e597ac2
Loading
Loading
Loading
Loading
+3
Changes for include/client/rpc/forward_data.hpp: 3 added lines, 0 removed lines.
Original line number Diff line number Diff line
@@ -66,6 +66,9 @@ forward_truncate(const std::string& path, size_t current_size, size_t new_size,
std::pair<int, ChunkStat>
forward_get_chunk_stat();

void
preconnect_hosts();

} // namespace gkfs::rpc

#endif // GEKKOFS_CLIENT_FORWARD_DATA_HPP
+67 −1
Changes for include/client/rpc/utils.hpp: 67 added lines, 1 removed line.
Original line number Diff line number Diff line
@@ -37,6 +37,11 @@
#include <cstdint>
#include <cstring>
#include <type_traits>
#include <algorithm>
#include <chrono>
#include <cstdlib>
#include <random>
#include <thread>

namespace gkfs::rpc {

@@ -169,16 +174,69 @@ decompress_and_parse_entries(const OutputOrErr& out,
 * @return Output The result of the RPC call. On failure, returns an object with
 * err = EBUSY.
 */
namespace detail {

/// Read a non-negative integer from the environment, `def` if unset/invalid.
inline long
env_long(const char* name, long def) {
    const char* v = std::getenv(name);
    if(v == nullptr || *v == '\0') {
        return def;
    }
    char* end = nullptr;
    long r = std::strtol(v, &end, 10);
    return (*end == '\0' && r >= 0) ? r : def;
}

/// Per-attempt RPC timeout in ms (LIBGKFS_RPC_TIMEOUT_MS, 0 = wait forever).
inline long
rpc_timeout_ms() {
    static const long v = env_long("LIBGKFS_RPC_TIMEOUT_MS", 30000);
    return v;
}

/// Number of retries after a failed attempt (LIBGKFS_RPC_RETRIES).
inline long
rpc_retries() {
    static const long v = env_long("LIBGKFS_RPC_RETRIES", 5);
    return v;
}

/// Randomised exponential backoff: sleep in [0, min(100 ms * 2^attempt, 5 s)].
/// Spreads out many clients that hit the same daemon at the same moment.
inline void
backoff(long attempt) {
    thread_local std::mt19937 gen{std::random_device{}()};
    const long cap = std::min<long>(100L << std::min<long>(attempt, 6), 5000);
    std::uniform_int_distribution<long> dist(0, cap);
    std::this_thread::sleep_for(std::chrono::milliseconds(dist(gen)));
}

} // namespace detail

template <typename Output, typename Input>
Output
forward_call(const std::shared_ptr<thallium::engine>& engine,
             const thallium::endpoint& endp, const std::string& tag,
             const Input& in, const char* func_name, const std::string& path) {
    // Transport failures (e.g. NA_TIMEOUT while many clients open their
    // first connection to the same daemon at once) are retried with a
    // per-attempt timeout and randomised backoff instead of failing the
    // operation with EBUSY right away. Caveat: if a timed-out request did
    // reach the daemon, a retry executes it twice (create -> EEXIST).
    const long timeout_ms = detail::rpc_timeout_ms();
    const long retries = detail::rpc_retries();
    for(long attempt = 0;; ++attempt) {
        try {
            LOG(DEBUG, "{}() Sending RPC for path '{}'...", func_name, path);

            auto rpc = engine->define(tag);
        Output out = rpc.on(endp)(in);
            auto data = (timeout_ms > 0)
                                ? rpc.on(endp).timed(
                                          std::chrono::milliseconds(timeout_ms),
                                          in)
                                : rpc.on(endp)(in);
            Output out = data;

            // Some output structs might not have .err, but most do.
            // We assume they do for this helper.
@@ -186,6 +244,13 @@ forward_call(const std::shared_ptr<thallium::engine>& engine,
            return out;

        } catch(const std::exception& ex) {
            if(attempt < retries) {
                LOG(WARNING,
                    "{}() rpc for path '{}' failed (attempt {}/{}): {}. Retrying.",
                    func_name, path, attempt + 1, retries + 1, ex.what());
                detail::backoff(attempt + 1);
                continue;
            }
            LOG(ERROR, "{}() getting rpc output for path '{}' failed: {}",
                func_name, path, ex.what());
            Output out;
@@ -193,6 +258,7 @@ forward_call(const std::shared_ptr<thallium::engine>& engine,
            return out;
        }
    }
}

} // namespace gkfs::rpc

+6
Changes for src/client/preload.cpp: 6 added lines, 0 removed lines.
Original line number Diff line number Diff line
@@ -45,6 +45,8 @@
#include <client/logging.hpp>

#include <client/preload_util.hpp>
#include <client/rpc/forward_data.hpp>
#include <client/rpc/utils.hpp>
#include <client/intercept.hpp>
#include <client/cache.hpp>

@@ -249,6 +251,10 @@ init_environment() {

    try {
        gkfs::utils::connect_to_hosts(hosts);
        if(gkfs::rpc::detail::env_long("LIBGKFS_PRECONNECT", 0) > 0 &&
           !CTX->use_proxy()) {
            gkfs::rpc::preconnect_hosts();
        }
        if(CTX->use_proxy()) {
            LOG(INFO, "Connecting to proxy...");
            gkfs::utils::lookup_proxy_addr();
+47
Changes for src/client/rpc/forward_data.cpp: 47 added lines, 0 removed lines.
Original line number Diff line number Diff line
@@ -39,6 +39,7 @@

#include <client/preload_util.hpp>
#include <client/rpc/forward_data.hpp>
#include <client/rpc/utils.hpp>
#include <client/logging.hpp>
#include <common/rpc/rpc_types_thallium.hpp>
#include <thallium.hpp>
@@ -48,6 +49,11 @@
#include <common/rpc/rpc_util.hpp>

#include <unordered_set>
#include <algorithm>
#include <chrono>
#include <numeric>
#include <random>
#include <thread>
#include <vector>
#include <map>
#include <iostream>
@@ -603,4 +609,45 @@ forward_get_chunk_stat() {
        return make_pair(0, ChunkStat{chunk_size, chunk_total, chunk_free});
}

/**
 * Open the connection to every daemon once, before the application starts.
 * Enabled with LIBGKFS_PRECONNECT=1. Each process first waits a random time in
 * [0, LIBGKFS_PRECONNECT_JITTER_MS] (default 2000) and then sends one cheap
 * RPC (chunk stat) to every daemon in random order, using forward_call()'s
 * timeout/retry. Without this, the first contact happens lazily at the first
 * I/O operation, which in MPI applications is often right after a barrier, so
 * hundreds of processes connect to the same daemon at the same moment.
 */
void
preconnect_hosts() {
    const auto n_hosts = CTX->hosts().size();
    const long jitter_ms =
            detail::env_long("LIBGKFS_PRECONNECT_JITTER_MS", 2000);
    std::mt19937 gen{std::random_device{}()};
    if(jitter_ms > 0) {
        std::uniform_int_distribution<long> dist(0, jitter_ms);
        std::this_thread::sleep_for(std::chrono::milliseconds(dist(gen)));
    }
    std::vector<std::size_t> order(n_hosts);
    std::iota(order.begin(), order.end(), 0);
    std::shuffle(order.begin(), order.end(), gen);
    std::size_t failed = 0;
    for(auto i : order) {
        gkfs::rpc::rpc_chunk_stat_in_t in;
        in.dummy = 0;
        auto out = forward_call<gkfs::rpc::rpc_chunk_stat_out_t>(
                CTX->rpc_engine(), CTX->hosts().at(i),
                gkfs::rpc::tag::get_chunk_stat, in, __func__, "");
        if(out.err != 0) {
            failed++;
        }
    }
    if(failed > 0) {
        LOG(WARNING, "{}() could not reach {}/{} daemons", __func__, failed,
            n_hosts);
    } else {
        LOG(INFO, "{}() connected to all {} daemons", __func__, n_hosts);
    }
}

} // namespace gkfs::rpc