aboutsummaryrefslogtreecommitdiffstats
path: root/contrib/clickhouse/src/Coordination/RaftServerConfig.h
blob: 825f53ac0869f31d953e2377fe9dd4da33e9f6e5 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
#pragma once
#include <base/defines.h>
#include <base/types.h>
#include <fmt/core.h>
#error #include <libnuraft/srv_config.hxx>

namespace DB
{
// default- and copy-constructible version of nuraft::srv_config
struct RaftServerConfig
{
    int id;
    String endpoint;
    bool learner;
    int priority;

    constexpr RaftServerConfig() = default;
    constexpr RaftServerConfig(int id_, std::string_view endpoint_, bool learner_ = false, int priority_ = 1)
        : id(id_), endpoint(endpoint_), learner(learner_), priority(priority_)
    {
    }

    constexpr bool operator==(const RaftServerConfig &) const = default;
    explicit RaftServerConfig(const nuraft::srv_config & cfg) noexcept;
    explicit operator nuraft::srv_config() const noexcept;

    /// Parse server in format "server.id=host:port[;learner][;priority]"
    static std::optional<RaftServerConfig> parse(std::string_view server) noexcept;
};

using RaftServers = std::vector<RaftServerConfig>;
/// Parse comma-delimited servers. Check for duplicate endpoints and ids.
/// @returns {} on parsing or validation error.
RaftServers parseRaftServers(std::string_view servers);

struct AddRaftServer : RaftServerConfig
{
};

struct RemoveRaftServer
{
    int id;
};

struct UpdateRaftServerPriority
{
    int id;
    int priority;
};

using ClusterUpdateAction = std::variant<AddRaftServer, RemoveRaftServer, UpdateRaftServerPriority>;
using ClusterUpdateActions = std::vector<ClusterUpdateAction>;
}

template <>
struct fmt::formatter<DB::RaftServerConfig> : fmt::formatter<string_view>
{
    constexpr auto format(const DB::RaftServerConfig & server, format_context & ctx)
    {
        return fmt::format_to(
            ctx.out(), "server.{}={};{};{}", server.id, server.endpoint, server.learner ? "learner" : "participant", server.priority);
    }
};

template <>
struct fmt::formatter<DB::ClusterUpdateAction> : fmt::formatter<string_view>
{
    constexpr auto format(const DB::ClusterUpdateAction & action, format_context & ctx)
    {
        if (const auto * add = std::get_if<DB::AddRaftServer>(&action))
            return fmt::format_to(ctx.out(), "(Add server {})", add->id);
        if (const auto * remove = std::get_if<DB::RemoveRaftServer>(&action))
            return fmt::format_to(ctx.out(), "(Remove server {})", remove->id);
        if (const auto * update = std::get_if<DB::UpdateRaftServerPriority>(&action))
            return fmt::format_to(ctx.out(), "(Change server {} priority to {})", update->id, update->priority);
        UNREACHABLE();
    }
};