Verified Commit 2389ad69 authored by Alberto Miranda's avatar Alberto Miranda ♨️
Browse files

logging: Refactor RPC formatting

parent 4f2d492f
Loading
Loading
Loading
Loading
+2 −2
Original line number Diff line number Diff line
@@ -25,7 +25,7 @@
add_library(_rpc_client STATIC)
target_sources(
  _rpc_client
  INTERFACE endpoint.hpp client.hpp request.hpp serialization.hpp
  INTERFACE endpoint.hpp client.hpp request.hpp serialization.hpp utilities.hpp
  PRIVATE endpoint.cpp client.cpp
)

@@ -35,7 +35,7 @@ set_property(TARGET _rpc_client PROPERTY POSITION_INDEPENDENT_CODE ON)
add_library(_rpc_server STATIC)
target_sources(
  _rpc_server
  INTERFACE endpoint.hpp server.hpp request.hpp serialization.hpp
  INTERFACE endpoint.hpp server.hpp request.hpp serialization.hpp utilities.hpp
  PRIVATE server.cpp endpoint.cpp
)

+135 −0
Original line number Diff line number Diff line
/******************************************************************************
 * Copyright 2021-2023, Barcelona Supercomputing Center (BSC), Spain
 *
 * This software was partially supported by the EuroHPC-funded project ADMIRE
 *   (Project ID: 956748, https://www.admire-eurohpc.eu).
 *
 * This file is part of scord.
 *
 * scord is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * scord 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 General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with scord.  If not, see <https://www.gnu.org/licenses/>.
 *
 * SPDX-License-Identifier: GPL-3.0-or-later
 *****************************************************************************/


#ifndef NETWORK_UTILITIES_HPP
#define NETWORK_UTILITIES_HPP

#include <cstdint>
#include <optional>
#include <string>
#include <string_view>
#include <atomic>
#include <fmt/format.h>

namespace network {

class rpc_info {
private:
    static std::uint64_t
    new_id() {
        static std::atomic_uint64_t s_current_id;
        return s_current_id++;
    }

public:
    rpc_info(std::uint64_t id, std::string name, std::string address)
        : m_id(id), m_children(0), m_name(std::move(name)),
          m_address(std::move(address)) {}

    rpc_info(std::uint64_t id, std::uint64_t pid, std::string name,
             std::string address)
        : m_id(id), m_pid(pid), m_children(0), m_name(std::move(name)),
          m_address(std::move(address)) {}

    template <typename... Args>
    static rpc_info
    create(Args&&... args) {
        return {new_id(), std::forward<Args>(args)...};
    }

    template <typename... Args>
    rpc_info
    add_child(std::string address) const {
        return {m_children, m_id, m_name, std::move(address)};
    }

    constexpr std::uint64_t
    id() const {
        return m_id;
    }

    constexpr std::optional<std::uint64_t>
    pid() const {
        return m_pid;
    }

    const std::string&
    name() const {
        return m_name;
    }

    const std::string&
    address() const {
        return m_address;
    }

private:
    std::uint64_t m_id;
    std::optional<std::uint64_t> m_pid;
    std::uint64_t m_children;
    std::string m_name;
    std::string m_address;
};

} // namespace network

template <>
struct fmt::formatter<network::rpc_info> {

    // RPC direction format:
    //   '<': from self to target (outbound)
    //   '>': from target to self (inbound)
    bool m_outbound = true;

    // Parses format specifications in the form:
    constexpr auto
    parse(format_parse_context& ctx) {
        auto it = ctx.begin(), end = ctx.end();

        if(it != end && (*it == '<' || *it == '>')) {
            m_outbound = *it++ == '<';
        }

        if(it != end && *it != '}') {
            ctx.on_error("invalid format");
        }

        return it;
    }

    template <typename FormatContext>
    constexpr auto
    format(const network::rpc_info& rpc, FormatContext& ctx) const {
        format_to(ctx.out(), "{}{} id: {} name: {} ", m_outbound ? "<=" : "=>",
                  rpc.pid() ? fmt::format(" pid: {}", *rpc.pid()) : "",
                  rpc.id(), std::quoted(rpc.name()));
        return m_outbound ? format_to(ctx.out(), "to: {}",
                                      std::quoted(rpc.address()))
                          : format_to(ctx.out(), "from: {}",
                                      std::quoted(rpc.address()));
    }
};

#endif // NETWORK_UTILITIES_HPP
+82 −163

File changed.

Preview size limit exceeded, changes collapsed.

+16 −34
Original line number Diff line number Diff line
@@ -24,6 +24,7 @@

#include <net/request.hpp>
#include <net/serialization.hpp>
#include <net/utilities.hpp>
#include "rpc_server.hpp"

// required for ::waitpid()
@@ -32,14 +33,6 @@

using namespace std::literals;

struct remote_procedure {
    static std::uint64_t
    new_id() {
        static std::atomic_uint64_t current_id;
        return current_id++;
    }
};

namespace scord_ctl {

rpc_server::rpc_server(std::string name, std::string address, bool daemonize,
@@ -56,24 +49,22 @@ rpc_server::rpc_server(std::string name, std::string address, bool daemonize,
#undef EXPAND
}

#define RPC_NAME() ("ADM_"s + __FUNCTION__)

void
rpc_server::ping(const network::request& req) {

    using network::generic_response;
    using network::get_address;
    using network::rpc_info;

    const auto rpc_name = "ADM_"s + __FUNCTION__;
    const auto rpc_id = remote_procedure::new_id();
    const auto rpc = rpc_info::create(RPC_NAME(), get_address(req));

    LOGGER_INFO("rpc id: {} name: {} from: {} => "
                "body: {{}}",
                rpc_id, std::quoted(rpc_name), std::quoted(get_address(req)));
    LOGGER_INFO("rpc {:>} body: {{}}", rpc);

    const auto resp = generic_response{rpc_id, scord::error_code::success};
    const auto resp = generic_response{rpc.id(), scord::error_code::success};

    LOGGER_INFO("rpc id: {} name: {} to: {} <= "
                "body: {{retval: {}}}",
                rpc_id, std::quoted(rpc_name), std::quoted(get_address(req)),
    LOGGER_INFO("rpc {:<} body: {{retval: {}}}", rpc,
                scord::error_code::success);

    req.respond(resp);
@@ -88,15 +79,12 @@ rpc_server::deploy_adhoc_storage(

    using network::generic_response;
    using network::get_address;
    using network::rpc_info;

    const auto rpc_name = "ADM_"s + __FUNCTION__;
    const auto rpc_id = remote_procedure::new_id();
    const auto rpc = rpc_info::create(RPC_NAME(), get_address(req));

    LOGGER_INFO("rpc id: {} name: {} from: {} => "
                "body: {{type: {}, ctx: {}, resources: {}}}",
                rpc_id, std::quoted(__FUNCTION__),
                std::quoted(get_address(req)), adhoc_type, adhoc_ctx,
                adhoc_resources);
    LOGGER_INFO("rpc {:>} body: {{type: {}, ctx: {}, resources: {}}}", rpc,
                adhoc_type, adhoc_ctx, adhoc_resources);

    auto ec = scord::error_code::success;

@@ -133,10 +121,7 @@ rpc_server::deploy_adhoc_storage(
            }
            case -1: {
                ec = scord::error_code::other;
                LOGGER_ERROR("rpc id: {} name: {} to: {} <= "
                             "body: {{retval: {}}}",
                             rpc_id, std::quoted(rpc_name),
                             std::quoted(get_address(req)), ec);
                LOGGER_ERROR("rpc {:<} body: {{retval: {}}}", rpc, ec);
                break;
            }
            default: {
@@ -145,7 +130,7 @@ rpc_server::deploy_adhoc_storage(
                if(retwait == -1) {
                    LOGGER_ERROR(
                            "rpc id: {} error_msg: \"Error waitpid code: {}\"",
                            rpc_id, retwait);
                            rpc.id(), retwait);
                    ec = scord::error_code::other;
                } else {
                    if(WEXITSTATUS(wstatus) != 0) {
@@ -159,12 +144,9 @@ rpc_server::deploy_adhoc_storage(
        }
    }

    const auto resp = generic_response{rpc_id, ec};
    const auto resp = generic_response{rpc.id(), ec};

    LOGGER_INFO("rpc id: {} name: {} to: {} <= "
                "body: {{retval: {}}}",
                rpc_id, std::quoted(rpc_name), std::quoted(get_address(req)),
                ec);
    LOGGER_INFO("rpc {:<} body: {{retval: {}}}", rpc, ec);

    req.respond(resp);
}
+104 −177

File changed.

Preview size limit exceeded, changes collapsed.