1#include <Common/DNSResolver.h>
2#include <DataTypes/DataTypeString.h>
3#include <DataTypes/DataTypesNumber.h>
4#include <Interpreters/Cluster.h>
5#include <Interpreters/Context.h>
6#include <Storages/System/StorageSystemClusters.h>
7
8namespace DB
9{
10
11NamesAndTypesList StorageSystemClusters::getNamesAndTypes()
12{
13 return
14 {
15 {"cluster", std::make_shared<DataTypeString>()},
16 {"shard_num", std::make_shared<DataTypeUInt32>()},
17 {"shard_weight", std::make_shared<DataTypeUInt32>()},
18 {"replica_num", std::make_shared<DataTypeUInt32>()},
19 {"host_name", std::make_shared<DataTypeString>()},
20 {"host_address", std::make_shared<DataTypeString>()},
21 {"port", std::make_shared<DataTypeUInt16>()},
22 {"is_local", std::make_shared<DataTypeUInt8>()},
23 {"user", std::make_shared<DataTypeString>()},
24 {"default_database", std::make_shared<DataTypeString>()},
25 {"errors_count", std::make_shared<DataTypeUInt32>()},
26 {"estimated_recovery_time", std::make_shared<DataTypeUInt32>()}
27 };
28}
29
30void StorageSystemClusters::fillData(MutableColumns & res_columns, const Context & context, const SelectQueryInfo &) const
31{
32 for (const auto & name_and_cluster : context.getClusters().getContainer())
33 {
34 const String & cluster_name = name_and_cluster.first;
35 const ClusterPtr & cluster = name_and_cluster.second;
36 const auto & shards_info = cluster->getShardsInfo();
37 const auto & addresses_with_failover = cluster->getShardsAddresses();
38
39 for (size_t shard_index = 0; shard_index < shards_info.size(); ++shard_index)
40 {
41 const auto & shard_info = shards_info[shard_index];
42 const auto & shard_addresses = addresses_with_failover[shard_index];
43 const auto pool_status = shard_info.pool->getStatus();
44
45 for (size_t replica_index = 0; replica_index < shard_addresses.size(); ++replica_index)
46 {
47 size_t i = 0;
48 const auto & address = shard_addresses[replica_index];
49
50 res_columns[i++]->insert(cluster_name);
51 res_columns[i++]->insert(shard_info.shard_num);
52 res_columns[i++]->insert(shard_info.weight);
53 res_columns[i++]->insert(replica_index + 1);
54 res_columns[i++]->insert(address.host_name);
55 auto resolved = address.getResolvedAddress();
56 res_columns[i++]->insert(resolved ? resolved->host().toString() : String());
57 res_columns[i++]->insert(address.port);
58 res_columns[i++]->insert(address.is_local);
59 res_columns[i++]->insert(address.user);
60 res_columns[i++]->insert(address.default_database);
61 res_columns[i++]->insert(pool_status[replica_index].error_count);
62 res_columns[i++]->insert(pool_status[replica_index].estimated_recovery_time.count());
63 }
64 }
65 }
66}
67}
68