aboutsummaryrefslogtreecommitdiffstats
path: root/contrib/clickhouse/src/Backups/BackupSettings.cpp
blob: 53edd004a919deff9073bdcd95d57a579fa0be55 (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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
#include <Backups/BackupInfo.h>
#include <Backups/BackupSettings.h>
#include <Core/SettingsFields.h>
#include <Parsers/ASTBackupQuery.h>
#include <Parsers/ASTFunction.h>
#include <Parsers/ASTSetQuery.h>
#include <Parsers/ASTLiteral.h>
#include <IO/ReadHelpers.h>
#include <Backups/SettingsFieldOptionalUUID.h>

namespace DB
{
namespace ErrorCodes
{
    extern const int CANNOT_PARSE_BACKUP_SETTINGS;
    extern const int WRONG_BACKUP_SETTINGS;
}

/// List of backup settings except base_backup_name and cluster_host_ids.
#define LIST_OF_BACKUP_SETTINGS(M) \
    M(String, id) \
    M(String, compression_method) \
    M(String, password) \
    M(String, s3_storage_class) \
    M(Bool, structure_only) \
    M(Bool, async) \
    M(Bool, decrypt_files_from_encrypted_disks) \
    M(Bool, deduplicate_files) \
    M(Bool, allow_s3_native_copy) \
    M(Bool, use_same_s3_credentials_for_base_backup) \
    M(Bool, read_from_filesystem_cache) \
    M(UInt64, shard_num) \
    M(UInt64, replica_num) \
    M(Bool, internal) \
    M(String, host_id) \
    M(OptionalUUID, backup_uuid)
    /// M(Int64, compression_level)

BackupSettings BackupSettings::fromBackupQuery(const ASTBackupQuery & query)
{
    BackupSettings res;

    if (query.settings)
    {
        const auto & settings = query.settings->as<const ASTSetQuery &>().changes;
        for (const auto & setting : settings)
        {
            if (setting.name == "compression_level")
                res.compression_level = static_cast<int>(SettingFieldInt64{setting.value}.value);
            else
#define GET_SETTINGS_FROM_BACKUP_QUERY_HELPER(TYPE, NAME) \
            if (setting.name == #NAME) \
                res.NAME = SettingField##TYPE{setting.value}.value; \
            else

            LIST_OF_BACKUP_SETTINGS(GET_SETTINGS_FROM_BACKUP_QUERY_HELPER)
            throw Exception(ErrorCodes::CANNOT_PARSE_BACKUP_SETTINGS, "Unknown setting {}", setting.name);
        }
    }

    if (query.base_backup_name)
        res.base_backup_info = BackupInfo::fromAST(*query.base_backup_name);

    if (query.cluster_host_ids)
        res.cluster_host_ids = Util::clusterHostIDsFromAST(*query.cluster_host_ids);

    return res;
}

void BackupSettings::copySettingsToQuery(ASTBackupQuery & query) const
{
    auto query_settings = std::make_shared<ASTSetQuery>();
    query_settings->is_standalone = false;

    static const BackupSettings default_settings;
    bool all_settings_are_default = true;

#define SET_SETTINGS_IN_BACKUP_QUERY_HELPER(TYPE, NAME) \
    if ((NAME) != default_settings.NAME) \
    { \
        query_settings->changes.emplace_back(#NAME, static_cast<Field>(SettingField##TYPE{NAME})); \
        all_settings_are_default = false; \
    }

    LIST_OF_BACKUP_SETTINGS(SET_SETTINGS_IN_BACKUP_QUERY_HELPER)

    if (all_settings_are_default)
        query_settings = nullptr;

    query.settings = query_settings;

    auto base_backup_name = base_backup_info ? base_backup_info->toAST() : nullptr;
    if (base_backup_name)
        query.setOrReplace(query.base_backup_name, base_backup_name);
    else
        query.reset(query.base_backup_name);

    query.cluster_host_ids = !cluster_host_ids.empty() ? Util::clusterHostIDsToAST(cluster_host_ids) : nullptr;
}

std::vector<Strings> BackupSettings::Util::clusterHostIDsFromAST(const IAST & ast)
{
    std::vector<Strings> res;

    const auto * array_of_shards = typeid_cast<const ASTFunction *>(&ast);
    if (!array_of_shards || (array_of_shards->name != "array"))
        throw Exception(
            ErrorCodes::CANNOT_PARSE_BACKUP_SETTINGS,
            "Setting cluster_host_ids has wrong format, must be array of arrays of string literals");

    if (array_of_shards->arguments)
    {
        const ASTs shards = array_of_shards->arguments->children;
        res.resize(shards.size());

        for (size_t i = 0; i != shards.size(); ++i)
        {
            const auto * array_of_replicas = typeid_cast<const ASTLiteral *>(shards[i].get());
            if (!array_of_replicas || (array_of_replicas->value.getType() != Field::Types::Array))
                throw Exception(
                    ErrorCodes::CANNOT_PARSE_BACKUP_SETTINGS,
                    "Setting cluster_host_ids has wrong format, must be array of arrays of string literals");
            const auto & replicas = array_of_replicas->value.get<const Array &>();
            res[i].resize(replicas.size());
            for (size_t j = 0; j != replicas.size(); ++j)
            {
                const auto & replica = replicas[j];
                if (replica.getType() != Field::Types::String)
                    throw Exception(
                        ErrorCodes::CANNOT_PARSE_BACKUP_SETTINGS,
                        "Setting cluster_host_ids has wrong format, must be array of arrays of string literals");
                res[i][j] = replica.get<const String &>();
            }
        }
    }

    return res;
}

ASTPtr BackupSettings::Util::clusterHostIDsToAST(const std::vector<Strings> & cluster_host_ids)
{
    if (cluster_host_ids.empty())
        return nullptr;

    auto res = std::make_shared<ASTFunction>();
    res->name = "array";
    auto res_replicas = std::make_shared<ASTExpressionList>();
    res->arguments = res_replicas;
    res->children.push_back(res_replicas);
    res_replicas->children.resize(cluster_host_ids.size());

    for (size_t i = 0; i != cluster_host_ids.size(); ++i)
    {
        const auto & shard = cluster_host_ids[i];

        Array res_shard;
        res_shard.resize(shard.size());
        for (size_t j = 0; j != shard.size(); ++j)
            res_shard[j] = Field{shard[j]};

        res_replicas->children[i] = std::make_shared<ASTLiteral>(Field{std::move(res_shard)});
    }

    return res;
}

std::pair<size_t, size_t> BackupSettings::Util::findShardNumAndReplicaNum(const std::vector<Strings> & cluster_host_ids, const String & host_id)
{
    for (size_t i = 0; i != cluster_host_ids.size(); ++i)
    {
        for (size_t j = 0; j != cluster_host_ids[i].size(); ++j)
            if (cluster_host_ids[i][j] == host_id)
                return {i + 1, j + 1};
    }
    throw Exception(ErrorCodes::WRONG_BACKUP_SETTINGS,
                    "Cannot determine shard number or replica number, the current host {} is not found "
                    "in the cluster's hosts", host_id);
}

Strings BackupSettings::Util::filterHostIDs(const std::vector<Strings> & cluster_host_ids, size_t only_shard_num, size_t only_replica_num)
{
    Strings collected_host_ids;

    auto collect_replicas = [&](size_t shard_index)
    {
        const auto & shard = cluster_host_ids[shard_index - 1];
        if (only_replica_num)
        {
            if (only_replica_num <= shard.size())
                collected_host_ids.push_back(shard[only_replica_num - 1]);
        }
        else
        {
            for (size_t replica_index = 1; replica_index <= shard.size(); ++replica_index)
                collected_host_ids.push_back(shard[replica_index - 1]);
        }
    };

    if (only_shard_num)
    {
        if (only_shard_num <= cluster_host_ids.size())
            collect_replicas(only_shard_num);
    }
    else
    {
        for (size_t shard_index = 1; shard_index <= cluster_host_ids.size(); ++shard_index)
            collect_replicas(shard_index);
    }

    return collected_host_ids;
}

}