aboutsummaryrefslogtreecommitdiffstats
path: root/contrib/clickhouse/src/Storages/System/StorageSystemZooKeeperConnection.cpp
blob: 33268d583582a9a3ac16a2e950f2af53dd86787f (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
#include <Interpreters/Context.h>
#include <DataTypes/DataTypeArray.h>
#include <DataTypes/DataTypeEnum.h>
#include <DataTypes/DataTypeString.h>
#include <DataTypes/DataTypesNumber.h>
#include <DataTypes/DataTypeDateTime.h>
#include <Common/ZooKeeper/ZooKeeper.h>
#include <Coordination/KeeperFeatureFlags.h>
#include <Storages/System/StorageSystemZooKeeperConnection.h>

namespace DB
{

NamesAndTypesList StorageSystemZooKeeperConnection::getNamesAndTypes()
{
    DataTypeEnum16::Values feature_flags_enum_values;
    feature_flags_enum_values.reserve(magic_enum::enum_count<KeeperFeatureFlag>());
    for (const auto & [feature_flag, feature_flag_string] : magic_enum::enum_entries<KeeperFeatureFlag>())
        feature_flags_enum_values.push_back(std::pair{std::string{feature_flag_string}, static_cast<Int16>(feature_flag)});

    auto feature_flags_enum = std::make_shared<DataTypeEnum16>(std::move(feature_flags_enum_values));

    return {
        {"name", std::make_shared<DataTypeString>()},
        {"host", std::make_shared<DataTypeString>()},
        {"port", std::make_shared<DataTypeUInt16>()},
        {"index", std::make_shared<DataTypeUInt8>()},
        {"connected_time", std::make_shared<DataTypeDateTime>()},
        {"session_uptime_elapsed_seconds", std::make_shared<DataTypeUInt64>()},
        {"is_expired", std::make_shared<DataTypeUInt8>()},
        {"keeper_api_version", std::make_shared<DataTypeUInt8>()},
        {"client_id", std::make_shared<DataTypeInt64>()},
        {"enabled_feature_flags", std::make_shared<DataTypeArray>(std::move(feature_flags_enum))}
    };
}

void StorageSystemZooKeeperConnection::fillData(MutableColumns & res_columns, ContextPtr context,
    const SelectQueryInfo &) const
{
    res_columns[0]->insert("default");
    res_columns[1]->insert(context->getZooKeeper()->getConnectedZooKeeperHost());
    res_columns[2]->insert(context->getZooKeeper()->getConnectedZooKeeperPort());
    res_columns[3]->insert(context->getZooKeeper()->getConnectedZooKeeperIndex());
    res_columns[4]->insert(context->getZooKeeper()->getConnectedTime());
    res_columns[5]->insert(context->getZooKeeperSessionUptime());
    res_columns[6]->insert(context->getZooKeeper()->expired());
    res_columns[7]->insert(0);
    res_columns[8]->insert(context->getZooKeeper()->getClientID());

    const auto add_enabled_feature_flags = [&](const auto & zookeeper)
    {
        Array enabled_feature_flags;
        const auto * feature_flags = zookeeper->getKeeperFeatureFlags();
        if (feature_flags)
        {
            for (const auto & feature_flag : magic_enum::enum_values<KeeperFeatureFlag>())
            {
                if (feature_flags->isEnabled(feature_flag))
                {
                    enabled_feature_flags.push_back(feature_flag);
                }
            }
        }
        res_columns[9]->insert(std::move(enabled_feature_flags));
    };

    add_enabled_feature_flags(context->getZooKeeper());

    for (const auto & elem : context->getAuxiliaryZooKeepers())
    {
        res_columns[0]->insert(elem.first);
        res_columns[1]->insert(elem.second->getConnectedZooKeeperHost());
        res_columns[2]->insert(elem.second->getConnectedZooKeeperPort());
        res_columns[3]->insert(elem.second->getConnectedZooKeeperIndex());
        res_columns[4]->insert(elem.second->getConnectedTime());
        res_columns[5]->insert(elem.second->getSessionUptime());
        res_columns[6]->insert(elem.second->expired());
        res_columns[7]->insert(0);
        res_columns[8]->insert(elem.second->getClientID());
        add_enabled_feature_flags(elem.second);
    }

}

}