Commit 072d8f28 authored by sevenuz's avatar sevenuz
Browse files

client: capture the RPC engine once in bulk paths

A bulk handle is registered with one engine's Mercury instance, so
expose() and the RPC that carries the handle must use the same engine.
With the thread-local selection this already holds within one call, but
write, read, the dirents variants and the opendir fan-out now take
CTX->rpc_engine() and CTX->hosts() once at the top and use these
locals, which makes the invariant explicit.

Co-Authored-By: default avatarClaude Opus 5.5 <noreply@anthropic.com>
parent 24de08fc
Loading
Loading
Loading
Loading
+8 −5
Changes for src/client/gkfs_metadata.cpp: 8 added lines, 5 removed lines.
Original line number Diff line number Diff line
@@ -1220,8 +1220,12 @@ gkfs_opendir(const std::string& path) {
                }
            }
        } else {
            auto get_dirents_rpc = CTX->rpc_engine()->define(
                    gkfs::rpc::tag::get_dirents_extended);
            // one engine for the whole branch: the bulk handles exposed
            // below and the RPCs carrying them must belong to the same engine
            auto engine = CTX->rpc_engine();
            const auto& hosts = CTX->hosts();
            auto get_dirents_rpc =
                    engine->define(gkfs::rpc::tag::get_dirents_extended);
            std::string root_path = path;
            if(root_path.length() > 1 && root_path.back() != '/') {
                root_path += '/';
@@ -1232,7 +1236,7 @@ gkfs_opendir(const std::string& path) {
                std::vector<std::pair<void*, std::size_t>> segments = {
                        {buffers[i].data(), buffers[i].size()}};
                try {
                    exposed_buffers.emplace_back(CTX->rpc_engine()->expose(
                    exposed_buffers.emplace_back(engine->expose(
                            segments, thallium::bulk_mode::read_write));

                    gkfs::rpc::rpc_get_dirents_in_t in;
@@ -1240,8 +1244,7 @@ gkfs_opendir(const std::string& path) {
                    in.start_key = "";
                    in.bulk_handle = exposed_buffers.back();
                    waiters.push_back(
                            get_dirents_rpc.on(CTX->hosts().at(targets[i]))
                                    .async(in));
                            get_dirents_rpc.on(hosts.at(targets[i])).async(in));
                    waiter_buffer_ids.push_back(i);
                    waiter_servers.push_back(i);
                } catch(const std::exception& ex) {
+21 −12
Changes for src/client/rpc/forward_data.cpp: 21 added lines, 12 removed lines.
Original line number Diff line number Diff line
@@ -82,16 +82,19 @@ int
forward_data_helper(std::string rpc_tag,
                    const std::unordered_set<unsigned int>& targets,
                    PrepareFunc prepare_input, ProcessFunc process_output) {
    // the calling thread's engine and its endpoints, captured once
    auto engine = CTX->rpc_engine();
    const auto& hosts = CTX->hosts();

    std::vector<thallium::async_response> waiters;
    waiters.reserve(targets.size());

    auto rpc = CTX->rpc_engine()->define(rpc_tag);
    auto rpc = engine->define(rpc_tag);

    for(const auto& target : targets) {
        try {
            InputType in = prepare_input(target);
            waiters.push_back(rpc.on(CTX->hosts().at(target)).async(in));
            waiters.push_back(rpc.on(hosts.at(target)).async(in));
        } catch(const std::exception& ex) {
            LOG(ERROR, "Failed to send RPC to host {}: {}", target, ex.what());
        }
@@ -135,6 +138,10 @@ forward_data_helper(std::string rpc_tag,
pair<int, ssize_t>
forward_write(const string& path, const void* buf, const off64_t offset,
              const size_t write_size, const int8_t num_copies) {
    // one engine for the whole call: bulk handles exposed below and the
    // RPCs carrying them must belong to the same engine
    auto engine = CTX->rpc_engine();
    const auto& hosts = CTX->hosts();

    if(gkfs::config::proxy::fwd_io && CTX->use_proxy()) {
        LOG(WARNING,
@@ -204,8 +211,7 @@ forward_write(const string& path, const void* buf, const off64_t offset,
            std::make_pair(const_cast<void*>(buf), write_size)};
    thallium::bulk bulk_handle;
    try {
        bulk_handle = CTX->rpc_engine()->expose(segments,
                                                thallium::bulk_mode::read_only);
        bulk_handle = engine->expose(segments, thallium::bulk_mode::read_only);
    } catch(const std::exception& ex) {
        LOG(ERROR, "Failed to expose buffer: {}", ex.what());
        return make_pair(EBUSY, 0);
@@ -216,7 +222,7 @@ forward_write(const string& path, const void* buf, const off64_t offset,

    // Keep track of targets for error reporting

    auto write_rpc = CTX->rpc_engine()->define(gkfs::rpc::tag::write);
    auto write_rpc = engine->define(gkfs::rpc::tag::write);

    for(std::size_t i = 0; i < targets.size(); ++i) {
        auto target = targets[i];
@@ -247,7 +253,7 @@ forward_write(const string& path, const void* buf, const off64_t offset,
        in.path = path;
        in.offset = block_overrun(offset, gkfs::config::rpc::chunksize);
        in.host_id = target;
        in.host_size = CTX->hosts().size();
        in.host_size = hosts.size();
        in.wbitset = gkfs::rpc::compress_bitset(chnk_bitset);
        in.chunk_n = target_chnks[target].size();
        in.chunk_start = chnk_start;
@@ -256,7 +262,7 @@ forward_write(const string& path, const void* buf, const off64_t offset,
        in.bulk_handle = bulk_handle;

        try {
            waiters.push_back(write_rpc.on(CTX->hosts().at(target)).async(in));
            waiters.push_back(write_rpc.on(hosts.at(target)).async(in));
        } catch(const std::exception& ex) {
            LOG(ERROR, "Failed to send RPC to host {}: {}", target, ex.what());
            if(num_copies == 0) {
@@ -351,6 +357,10 @@ pair<int, ssize_t>
forward_read(const string& path, void* buf, const off64_t offset,
             const size_t read_size, const int8_t num_copies,
             std::set<int8_t>& failed) {
    // one engine for the whole call: bulk handles exposed below and the
    // RPCs carrying them must belong to the same engine
    auto engine = CTX->rpc_engine();
    const auto& hosts = CTX->hosts();

    if(gkfs::config::proxy::fwd_io && CTX->use_proxy()) {
        LOG(WARNING,
@@ -417,8 +427,7 @@ forward_read(const string& path, void* buf, const off64_t offset,
            std::make_pair(buf, read_size)};
    thallium::bulk bulk_handle;
    try {
        bulk_handle = CTX->rpc_engine()->expose(
                segments, thallium::bulk_mode::write_only);
        bulk_handle = engine->expose(segments, thallium::bulk_mode::write_only);
    } catch(const std::exception& ex) {
        LOG(ERROR, "Failed to expose buffer: {}", ex.what());
        return make_pair(EBUSY, 0);
@@ -429,7 +438,7 @@ forward_read(const string& path, void* buf, const off64_t offset,
    std::vector<uint64_t> waiter_targets; // track targets for error reporting
    waiter_targets.reserve(targets.size());

    auto read_rpc = CTX->rpc_engine()->define(gkfs::rpc::tag::read);
    auto read_rpc = engine->define(gkfs::rpc::tag::read);

    // Issue non-blocking RPC requests and wait for the result later
    for(std::size_t i = 0; i < targets.size(); ++i) {
@@ -462,7 +471,7 @@ forward_read(const string& path, void* buf, const off64_t offset,
        in.path = path;
        in.offset = block_overrun(offset, gkfs::config::rpc::chunksize);
        in.host_id = target;
        in.host_size = CTX->hosts().size();
        in.host_size = hosts.size();
        in.wbitset = gkfs::rpc::compress_bitset(chnk_bitset);
        in.chunk_n = target_chnks[target].size();
        in.chunk_start = chnk_start;
@@ -471,7 +480,7 @@ forward_read(const string& path, void* buf, const off64_t offset,
        in.bulk_handle = bulk_handle;

        try {
            waiters.push_back(read_rpc.on(CTX->hosts().at(target)).async(in));
            waiters.push_back(read_rpc.on(hosts.at(target)).async(in));
            waiter_targets.push_back(target);
        } catch(const std::exception& ex) {
            LOG(ERROR, "Failed to send RPC to host {}: {}", target, ex.what());
+22 −12
Changes for src/client/rpc/forward_metadata.cpp: 22 added lines, 12 removed lines.
Original line number Diff line number Diff line
@@ -349,6 +349,10 @@ decompress_and_parse_entries_standard(

pair<int, shared_ptr<gkfs::filemap::OpenDir>>
forward_get_dirents(const string& path) {
    // one engine for the whole call: bulk handles exposed below and the
    // RPCs carrying them must belong to the same engine
    auto engine = CTX->rpc_engine();
    const auto& hosts = CTX->hosts();
    LOG(DEBUG, "{}() enter for path '{}'", __func__, path);
    auto const targets = CTX->distributor()->locate_directory_metadata();

@@ -366,8 +370,8 @@ forward_get_dirents(const string& path) {
        try {
            std::vector<std::pair<void*, std::size_t>> segments = {
                    std::make_pair(buf_ptr, per_host_buff_size)};
            exposed_buffers.emplace_back(CTX->rpc_engine()->expose(
                    segments, thallium::bulk_mode::read_write));
            exposed_buffers.emplace_back(
                    engine->expose(segments, thallium::bulk_mode::read_write));
        } catch(const std::exception& e) {
            LOG(ERROR, "Failed to expose buffers: {}", e.what());
            return make_pair(EBUSY, nullptr);
@@ -383,11 +387,10 @@ forward_get_dirents(const string& path) {
        root_path += '/';
    }

    auto get_dirents_rpc =
            CTX->rpc_engine()->define(gkfs::rpc::tag::get_dirents);
    auto get_dirents_rpc = engine->define(gkfs::rpc::tag::get_dirents);

    for(std::size_t i = 0; i < targets.size(); ++i) {
        auto endp = CTX->hosts().at(targets[i]);
        auto endp = hosts.at(targets[i]);

        gkfs::rpc::rpc_get_dirents_in_t in;
        in.path = root_path;
@@ -679,6 +682,10 @@ pair<int, unique_ptr<vector<
                  tuple<const std::string, unsigned char, size_t, time_t>>>>
forward_get_dirents_single(const string& path, int server,
                           const std::string& start_key_arg, bool get_all) {
    // one engine for the whole call: bulk handles exposed below and the
    // RPCs carrying them must belong to the same engine
    auto engine = CTX->rpc_engine();
    const auto& hosts = CTX->hosts();
    auto const targets = CTX->distributor()->locate_directory_metadata();
    if((unsigned) server >= targets.size()) {
        LOG(ERROR, "{}() Invalid server index '{}' for targets size '{}'",
@@ -692,9 +699,8 @@ forward_get_dirents_single(const string& path, int server,
            vector<tuple<const std::string, unsigned char, size_t, time_t>>>();
    int err = 0;
    string start_key = start_key_arg;
    auto endp = CTX->hosts().at(targets[server]);
    auto get_dirents_rpc =
            CTX->rpc_engine()->define(gkfs::rpc::tag::get_dirents_extended);
    auto endp = hosts.at(targets[server]);
    auto get_dirents_rpc = engine->define(gkfs::rpc::tag::get_dirents_extended);

    // Ensure path ends with / for get_dirents prefix check
    std::string root_path = path;
@@ -721,7 +727,7 @@ forward_get_dirents_single(const string& path, int server,
                std::vector<std::pair<void*, std::size_t>> segments = {
                        std::make_pair(large_buffer.data(), buffer_size)};
                try {
                    exposed_buffer = CTX->rpc_engine()->expose(
                    exposed_buffer = engine->expose(
                            segments, thallium::bulk_mode::read_write);
                    expose_needed = false;
                } catch(const std::exception& e) {
@@ -799,8 +805,12 @@ forward_get_dirents_filtered(const std::string& path, int server,
                             const std::string& filter_name,
                             int64_t filter_size, int64_t filter_ctime,
                             bool count_only) {
    // one engine for the whole call: bulk handles exposed below and the
    // RPCs carrying them must belong to the same engine
    auto engine = CTX->rpc_engine();
    const auto& hosts = CTX->hosts();

    auto endp = CTX->hosts().at(server);
    auto endp = hosts.at(server);

    gkfs::rpc::rpc_get_dirents_filtered_in_t in;
    in.path = path;
@@ -831,12 +841,12 @@ forward_get_dirents_filtered(const std::string& path, int server,
        std::vector<std::pair<void*, std::size_t>> segments;
        segments.emplace_back(large_buffer.get(), buffer_size);
        tl::bulk exposed_buffer =
                CTX->rpc_engine()->expose(segments, tl::bulk_mode::write_only);
                engine->expose(segments, tl::bulk_mode::write_only);

        in.bulk_handle = exposed_buffer;

        auto get_dirents_rpc =
                CTX->rpc_engine()->define(gkfs::rpc::tag::get_dirents_filtered);
                engine->define(gkfs::rpc::tag::get_dirents_filtered);

        try {
            gkfs::rpc::rpc_get_dirents_filtered_out_t out =