Commit 7c37192f authored by sevenuz's avatar sevenuz
Browse files

client: create N RPC engines and look up endpoints per engine

init_environment() reads LIBGKFS_RPC_ENGINES (default 1, clamped to
[1, 64]) and creates that many Thallium engines with the same Margo
config, each with its own progress thread. All RPC tags are
pre-registered on every engine while still single-threaded.
connect_to_hosts() looks up every daemon once per engine (each engine in
its own shuffled host order) and stores the endpoints per engine;
preconnect contacts every daemon from every engine. The IPC engine for
the proxy stays single.

Start-up is measured and logged at INFO: engine creation and tag
registration, the N x hosts lookups, and the preconnect phase.

Co-Authored-By: default avatarClaude Opus 5.5 <noreply@anthropic.com>
parent 3bf5fa3e
Loading
Loading
Loading
Loading
+58 −27
Changes for src/client/preload.cpp: 58 added lines, 27 removed lines.
Original line number Diff line number Diff line
@@ -63,6 +63,8 @@
#include <dlfcn.h>
#include <limits.h>

#include <algorithm>
#include <chrono>
#include <ctime>
#include <cstdlib>
#include <fstream>
@@ -176,6 +178,42 @@ log_prog_name() {

} // namespace

namespace {

/**
 * Pre-register every RPC tag on one engine while still single-threaded, so
 * that concurrent calls to define() from FUSE worker threads never race
 * inside HG_Core_register() and corrupt Mercury's RPC callback table.
 */
void
preregister_rpcs(thallium::engine& engine) {
    for(const auto* tag : {gkfs::rpc::tag::fs_config,
                           gkfs::rpc::tag::create,
                           gkfs::rpc::tag::create_write_inline,
                           gkfs::rpc::tag::stat,
                           gkfs::rpc::tag::remove_metadata,
                           gkfs::rpc::tag::remove_data,
                           gkfs::rpc::tag::decr_size,
                           gkfs::rpc::tag::update_metadentry,
                           gkfs::rpc::tag::get_metadentry_size,
                           gkfs::rpc::tag::update_metadentry_size,
                           gkfs::rpc::tag::get_dirents,
                           gkfs::rpc::tag::get_dirents_extended,
                           gkfs::rpc::tag::get_dirents_filtered,
                           gkfs::rpc::tag::mk_symlink,
                           gkfs::rpc::tag::rename,
                           gkfs::rpc::tag::write,
                           gkfs::rpc::tag::read,
                           gkfs::rpc::tag::truncate,
                           gkfs::rpc::tag::get_chunk_stat,
                           gkfs::rpc::tag::write_data_inline,
                           gkfs::rpc::tag::read_data_inline}) {
        engine.define(tag);
    }
}

} // namespace

namespace gkfs::preload {

/**
@@ -200,6 +238,11 @@ init_environment() {
    // initialize Thallium interface
    LOG(INFO, "Initializing RPC subsystem...");

    // LIBGKFS_RPC_ENGINES: size of the RPC engine pool (default 1)
    const auto n_engines = static_cast<std::size_t>(
            std::clamp(gkfs::env::get_var(gkfs::env::RPC_ENGINES, 1), 1, 64));
    const auto t_start = std::chrono::steady_clock::now();

    try {
        auto margo_config = R"(
            {
@@ -207,35 +250,12 @@ init_environment() {
                "rpc_thread_count" : 0
            }
        )";
        CTX->rpc_engine(nullptr);
        for(std::size_t e = 0; e < n_engines; ++e) {
            auto rpc_engine = std::make_shared<thallium::engine>(
                    CTX->rpc_protocol(), THALLIUM_CLIENT_MODE, margo_config);
        CTX->rpc_engine(rpc_engine);

        // Pre-register every RPC tag while still single-threaded so that
        // concurrent calls to define() from FUSE worker threads never race
        // inside HG_Core_register() and corrupt Mercury's RPC callback table.
        for(const auto* tag : {gkfs::rpc::tag::fs_config,
                               gkfs::rpc::tag::create,
                               gkfs::rpc::tag::create_write_inline,
                               gkfs::rpc::tag::stat,
                               gkfs::rpc::tag::remove_metadata,
                               gkfs::rpc::tag::remove_data,
                               gkfs::rpc::tag::decr_size,
                               gkfs::rpc::tag::update_metadentry,
                               gkfs::rpc::tag::get_metadentry_size,
                               gkfs::rpc::tag::update_metadentry_size,
                               gkfs::rpc::tag::get_dirents,
                               gkfs::rpc::tag::get_dirents_extended,
                               gkfs::rpc::tag::get_dirents_filtered,
                               gkfs::rpc::tag::mk_symlink,
                               gkfs::rpc::tag::rename,
                               gkfs::rpc::tag::write,
                               gkfs::rpc::tag::read,
                               gkfs::rpc::tag::truncate,
                               gkfs::rpc::tag::get_chunk_stat,
                               gkfs::rpc::tag::write_data_inline,
                               gkfs::rpc::tag::read_data_inline}) {
            rpc_engine->define(tag);
            preregister_rpcs(*rpc_engine);
            CTX->add_rpc_engine(rpc_engine);
        }

        if(CTX->use_proxy()) {
@@ -249,8 +269,11 @@ init_environment() {
        exit_error_msg(EXIT_FAILURE, "Unable to initialize RPC/IPC subsystem");
    }

    const auto t_engines = std::chrono::steady_clock::now();
    auto t_lookup = t_engines;
    try {
        gkfs::utils::connect_to_hosts(hosts);
        t_lookup = std::chrono::steady_clock::now();
        if(gkfs::rpc::detail::env_long("LIBGKFS_PRECONNECT", 0) > 0 &&
           !CTX->use_proxy()) {
            gkfs::rpc::preconnect_hosts();
@@ -263,6 +286,14 @@ init_environment() {
        exit_error_msg(EXIT_FAILURE,
                       "Failed to connect to hosts: "s + e.what());
    }
    {
        using ms = std::chrono::duration<double, std::milli>;
        const auto t_end = std::chrono::steady_clock::now();
        LOG(INFO,
            "RPC start-up: {} engine(s) x {} host(s): engines+tags {:.1f} ms, lookups {:.1f} ms, preconnect {:.1f} ms",
            n_engines, hosts.size(), ms(t_engines - t_start).count(),
            ms(t_lookup - t_engines).count(), ms(t_end - t_lookup).count());
    }

    CTX->protect_files_generator(
            gkfs::env::get_var(gkfs::env::PROTECT_FILES_GENERATOR, 0));
+14 −8
Changes for src/client/preload_util.cpp: 14 added lines, 8 removed lines.
Original line number Diff line number Diff line
@@ -79,7 +79,7 @@ namespace {
 */
thallium::endpoint
lookup_endpoint(const std::string& uri, bool use_proxy = false,
                std::size_t max_retries = 3) {
                std::size_t max_retries = 3, std::size_t engine_idx = 0) {

    LOG(DEBUG, "Looking up address \"{}\"", uri);

@@ -95,7 +95,7 @@ lookup_endpoint(const std::string& uri, bool use_proxy = false,
            if(use_proxy) {
                return CTX->ipc_engine()->lookup(uri);
            } else {
                return CTX->rpc_engine()->lookup(uri);
                return CTX->rpc_engines().at(engine_idx)->lookup(uri);
            }
        } catch(const std::exception& e) {
            error_msg = e.what();
@@ -509,13 +509,19 @@ connect_to_hosts(const vector<pair<string, string>>& hosts) {
    ::random_device rd; // obtain a random number from hardware
    ::mt19937 g(rd());  // seed the random generator
    ::shuffle(host_ids.begin(), host_ids.end(), g); // Shuffle hosts vector
    // lookup addresses and put abstract server addresses into rpc_addresses

    // lookup addresses and put abstract server addresses into rpc_addresses.
    // Every RPC engine needs its own endpoints: one lookup per engine and
    // host, each engine in its own shuffled host order.
    CTX->clear_hosts();
    for(std::size_t e = 0; e < CTX->rpc_engine_count(); ++e) {
        if(e > 0) {
            ::shuffle(host_ids.begin(), host_ids.end(), g);
        }
        for(const auto& id : host_ids) {
            const auto& hostname = hosts.at(id).first;
            const auto& uri = hosts.at(id).second;

        addrs[id] = lookup_endpoint(uri);
            addrs[id] = lookup_endpoint(uri, false, 3, e);

            if(!local_host_found && hostname == local_hostname) {
                LOG(DEBUG, "Found local host: {}", hostname);
@@ -523,15 +529,15 @@ connect_to_hosts(const vector<pair<string, string>>& hosts) {
                local_host_found = true;
            }

        LOG(DEBUG, "Found peer: {}", hostname);
            LOG(DEBUG, "Found peer: {} (engine {})", hostname, e);
        }
        CTX->hosts(e, addrs);
    }

    if(!local_host_found) {
        LOG(WARNING, "Failed to find local host. Using host '0' as local host");
        CTX->local_host_id(0);
    }

    CTX->hosts(addrs);
}

/**
+10 −5
Changes for src/client/rpc/forward_data.cpp: 10 added lines, 5 removed lines.
Original line number Diff line number Diff line
@@ -620,7 +620,8 @@ forward_get_chunk_stat() {
 */
void
preconnect_hosts() {
    const auto n_hosts = CTX->hosts().size();
    const auto n_hosts = CTX->hosts(0).size();
    const auto n_engines = CTX->rpc_engine_count();
    const long jitter_ms =
            detail::env_long("LIBGKFS_PRECONNECT_JITTER_MS", 2000);
    std::mt19937 gen{std::random_device{}()};
@@ -631,22 +632,26 @@ preconnect_hosts() {
    std::vector<std::size_t> order(n_hosts);
    std::iota(order.begin(), order.end(), 0);
    std::shuffle(order.begin(), order.end(), gen);
    // every engine opens its own connection to every daemon
    std::size_t failed = 0;
    for(std::size_t e = 0; e < n_engines; ++e) {
        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),
                    CTX->rpc_engines().at(e), CTX->hosts(e).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);
        LOG(WARNING, "{}() could not reach {}/{} daemon connections", __func__,
            failed, n_hosts * n_engines);
    } else {
        LOG(INFO, "{}() connected to all {} daemons", __func__, n_hosts);
        LOG(INFO, "{}() connected to all {} daemons with {} engine(s)",
            __func__, n_hosts, n_engines);
    }
}