forked from ClickHouse/ClickHouse
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathReplicasStatusHandler.cpp
More file actions
157 lines (126 loc) · 5.74 KB
/
ReplicasStatusHandler.cpp
File metadata and controls
157 lines (126 loc) · 5.74 KB
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
#include <Server/ReplicasStatusHandler.h>
#include <Common/quoteString.h>
#include <Core/ServerSettings.h>
#include <Databases/IDatabase.h>
#include <IO/HTTPCommon.h>
#include <Interpreters/Context.h>
#include <Interpreters/DatabaseCatalog.h>
#include <Server/HTTP/HTMLForm.h>
#include <Server/HTTPHandlerFactory.h>
#include <Server/HTTPHandlerRequestFilter.h>
#include <Server/HTTPResponseHeaderWriter.h>
#include <Server/IServer.h>
#include <Storages/MergeTree/MergeTreeSettings.h>
#include <Storages/StorageReplicatedMergeTree.h>
#include <Common/typeid_cast.h>
#include <Poco/Net/HTTPRequestHandlerFactory.h>
#include <Poco/Net/HTTPServerRequest.h>
#include <Poco/Net/HTTPServerResponse.h>
namespace DB
{
namespace MergeTreeSetting
{
extern const MergeTreeSettingsUInt64 min_absolute_delay_to_close;
extern const MergeTreeSettingsUInt64 min_relative_delay_to_close;
}
ReplicasStatusHandler::ReplicasStatusHandler(IServer & server) : WithContext(server.context())
{
}
void ReplicasStatusHandler::handleRequest(HTTPServerRequest & request, HTTPServerResponse & response, const ProfileEvents::Event & /*write_event*/)
{
try
{
applyHTTPResponseHeaders(response, http_response_headers_override);
HTMLForm params(getContext()->getSettingsRef(), request);
const auto & config = getContext()->getConfigRef();
const MergeTreeSettings & settings = getContext()->getReplicatedMergeTreeSettings();
/// Even if lag is small, output detailed information about the lag.
bool verbose = false;
bool enable_verbose = config.getBool("enable_verbose_replicas_status", true);
if (params.get("verbose", "") == "1" && enable_verbose)
verbose = true;
bool ok = true;
WriteBufferFromOwnString message;
auto databases = DatabaseCatalog::instance().getDatabases();
/// Iterate through all the replicated tables.
for (const auto & db : databases)
{
/// Check if database can contain replicated tables
if (!db.second->canContainMergeTreeTables())
continue;
// Note that in case `async_load_databases = true` we do not want replica status handler to be hanging
// and waiting (in getTablesIterator() call) for every table to be load, so we just skip not-yet-loaded tables.
// If they have some lag it will be reflected as soon as they are load.
for (auto iterator = db.second->getTablesIterator(getContext(), {}, true); iterator->isValid(); iterator->next())
{
const auto & table = iterator->table();
if (!table)
continue;
StorageReplicatedMergeTree * table_replicated = dynamic_cast<StorageReplicatedMergeTree *>(table.get());
if (!table_replicated)
continue;
time_t absolute_delay = 0;
time_t relative_delay = 0;
if (!table_replicated->isTableReadOnly())
{
table_replicated->getReplicaDelays(absolute_delay, relative_delay);
if ((settings[MergeTreeSetting::min_absolute_delay_to_close] && absolute_delay >= static_cast<time_t>(settings[MergeTreeSetting::min_absolute_delay_to_close]))
|| (settings[MergeTreeSetting::min_relative_delay_to_close] && relative_delay >= static_cast<time_t>(settings[MergeTreeSetting::min_relative_delay_to_close])))
ok = false;
message << backQuoteIfNeed(db.first) << "." << backQuoteIfNeed(iterator->name())
<< ":\tAbsolute delay: " << absolute_delay << ". Relative delay: " << relative_delay << ".\n";
}
else
{
message << backQuoteIfNeed(db.first) << "." << backQuoteIfNeed(iterator->name())
<< ":\tis readonly. \n";
}
}
}
setResponseDefaultHeaders(response);
if (!ok)
{
response.setStatusAndReason(Poco::Net::HTTPResponse::HTTP_SERVICE_UNAVAILABLE);
if (enable_verbose)
verbose = true;
}
if (verbose)
*response.send() << message.str();
else
{
const char * data = "Ok.\n";
response.sendBuffer(data, strlen(data));
}
}
catch (...)
{
tryLogCurrentException("ReplicasStatusHandler");
try
{
response.setStatusAndReason(Poco::Net::HTTPResponse::HTTP_INTERNAL_SERVER_ERROR);
if (!response.sent())
{
/// We have not sent anything yet and we don't even know if we need to compress response.
*response.send() << getCurrentExceptionMessage(false) << '\n';
}
}
catch (...)
{
LOG_ERROR((getLogger("ReplicasStatusHandler")), "Cannot send exception to client");
}
}
}
HTTPRequestHandlerFactoryPtr createReplicasStatusHandlerFactory(IServer & server,
const Poco::Util::AbstractConfiguration & config,
const std::string & config_prefix,
std::unordered_map<String, String> & common_headers)
{
std::unordered_map<String, String> http_response_headers_override
= parseHTTPResponseHeadersWithCommons(config, config_prefix, "text/plain; charset=UTF-8", common_headers);
auto creator = [&server, http_response_headers_override]() -> std::unique_ptr<ReplicasStatusHandler>
{ return std::make_unique<ReplicasStatusHandler>(server, http_response_headers_override); };
auto factory = std::make_shared<HandlingRuleHTTPHandlerFactory<ReplicasStatusHandler>>(std::move(creator));
factory->addFiltersFromConfig(config, config_prefix);
return factory;
}
}