Commit 24de08fc authored by sevenuz's avatar sevenuz
Browse files

client: thread-local RPC engine selection

Each thread draws a ticket from an atomic round-robin counter on its
first RPC and keeps it in a thread_local; its engine is ticket % N.
rpc_engine() and hosts() resolve through this index, so all existing
call sites use the calling thread's engine and its endpoints. A
thread_local needs no cleanup when libfuse retires worker threads. With
one engine the counter is not touched and every thread gets engine 0.
The assignment is logged at DEBUG.

The round-robin logic lives in a small header-only EngineSelector so it
can be unit tested.

Co-Authored-By: default avatarClaude Opus 5.5 <noreply@anthropic.com>
parent 7c37192f
Loading
Loading
Loading
Loading
+3
Changes for include/client/preload_context.hpp: 3 added lines, 0 removed lines.
Original line number Diff line number Diff line
@@ -48,6 +48,7 @@
#include <mutex>
#include <unordered_set>
#include <config.hpp>
#include <client/rpc/engine_selector.hpp>

#include <bitset>

@@ -165,6 +166,8 @@ private:
    // (default 1), ipc_engine_ is the single proxy engine
    std::vector<std::shared_ptr<thallium::engine>> rpc_engines_;
    std::shared_ptr<thallium::engine> ipc_engine_;
    // assigns each thread one engine of rpc_engines_, round robin
    mutable gkfs::rpc::EngineSelector engine_selector_;


public:
+74
Changes for include/client/rpc/engine_selector.hpp: 74 added lines, 0 removed lines.
Original line number Diff line number Diff line
/*
  Copyright 2018-2025, Barcelona Supercomputing Center (BSC), Spain
  Copyright 2015-2025, Johannes Gutenberg Universitaet Mainz, Germany

  This software was partially supported by the
  EC H2020 funded project NEXTGenIO (Project ID: 671951, www.nextgenio.eu).

  This software was partially supported by the
  ADA-FS project under the SPPEXA project funded by the DFG.

  This software was partially supported by the
  the European Union’s Horizon 2020 JTI-EuroHPC research and
  innovation programme, by the project ADMIRE (Project ID: 956748,
  admire-eurohpc.eu)

  This project was partially promoted by the Ministry for Digital Transformation
  and the Civil Service, within the framework of the Recovery,
  Transformation and Resilience Plan - Funded by the European Union
  -NextGenerationEU.

  This file is part of GekkoFS' POSIX interface.

  GekkoFS' POSIX interface is free software: you can redistribute it and/or
  modify it under the terms of the GNU Lesser General Public License as
  published by the Free Software Foundation, either version 3 of the License,
  or (at your option) any later version.

  GekkoFS' POSIX interface is distributed in the hope that it will be useful,
  but WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  GNU Lesser General Public License for more details.

  You should have received a copy of the GNU Lesser General Public License
  along with GekkoFS' POSIX interface.  If not, see
  <https://www.gnu.org/licenses/>.

  SPDX-License-Identifier: LGPL-3.0-or-later
*/

#ifndef GEKKOFS_CLIENT_RPC_ENGINE_SELECTOR_HPP
#define GEKKOFS_CLIENT_RPC_ENGINE_SELECTOR_HPP

#include <atomic>
#include <cstddef>

namespace gkfs::rpc {

/**
 * Round-robin assignment of client threads to the engines of the RPC pool
 * (LIBGKFS_RPC_ENGINES). Each thread draws one ticket on its first RPC and
 * keeps it; its engine is ticket % number_of_engines. Tickets are drawn in
 * call order, so the first thread (the one that initializes the client)
 * gets engine 0. With one engine every thread gets engine 0.
 */
class EngineSelector {
    std::atomic<std::size_t> next_{0};

public:
    /// draw the next ticket
    std::size_t
    ticket() {
        return next_.fetch_add(1, std::memory_order_relaxed);
    }

    /// engine index of `ticket` in a pool of `n_engines`
    static constexpr std::size_t
    index(std::size_t ticket, std::size_t n_engines) {
        return n_engines <= 1 ? 0 : ticket % n_engines;
    }
};

} // namespace gkfs::rpc

#endif // GEKKOFS_CLIENT_RPC_ENGINE_SELECTOR_HPP
+14
Changes for src/client/preload_context.cpp: 14 added lines, 0 removed lines.
Original line number Diff line number Diff line
@@ -843,8 +843,22 @@ PreloadContext::rpc_engine_count() const {

std::size_t
PreloadContext::engine_index() const {
    const auto n = rpc_engines_.size();
    if(n <= 1) {
        return 0;
    }
    // One ticket per thread, drawn at its first RPC. thread_local instead of
    // a map keyed by thread id: FUSE worker threads come and go, and the
    // ticket dies with its thread without any cleanup.
    constexpr auto no_ticket = static_cast<std::size_t>(-1);
    thread_local std::size_t ticket = no_ticket;
    if(ticket == no_ticket) {
        ticket = engine_selector_.ticket();
        LOG(DEBUG, "{}() thread ticket {} uses RPC engine {}/{}", __func__,
            ticket, gkfs::rpc::EngineSelector::index(ticket, n), n);
    }
    return gkfs::rpc::EngineSelector::index(ticket, n);
}

std::shared_ptr<thallium::engine>
PreloadContext::ipc_engine() {