ClickHouse/src/Server/ReplicasStatusHandler.cpp

119 lines
3.9 KiB
C++
Raw Normal View History

#include <Server/ReplicasStatusHandler.h>
2016-01-17 13:34:36 +00:00
#include <Databases/IDatabase.h>
#include <IO/HTTPCommon.h>
#include <Interpreters/Context.h>
#include <Server/HTTP/HTMLForm.h>
#include <Server/HTTPHandlerFactory.h>
#include <Server/HTTPHandlerRequestFilter.h>
#include <Server/IServer.h>
#include <Storages/StorageReplicatedMergeTree.h>
2017-07-13 20:58:19 +00:00
#include <Common/typeid_cast.h>
2016-01-17 13:34:36 +00:00
#include <Poco/Net/HTTPRequestHandlerFactory.h>
2016-01-17 13:34:36 +00:00
#include <Poco/Net/HTTPServerRequest.h>
#include <Poco/Net/HTTPServerResponse.h>
namespace DB
{
ReplicasStatusHandler::ReplicasStatusHandler(IServer & server) : WithContext(server.context())
2016-01-17 13:34:36 +00:00
{
}
void ReplicasStatusHandler::handleRequest(HTTPServerRequest & request, HTTPServerResponse & response)
2016-01-17 13:34:36 +00:00
{
try
{
2021-06-16 14:33:14 +00:00
HTMLForm params(getContext()->getSettingsRef(), request);
/// Even if lag is small, output detailed information about the lag.
bool verbose = params.get("verbose", "") == "1";
const MergeTreeSettings & settings = getContext()->getReplicatedMergeTreeSettings();
bool ok = true;
2020-11-10 18:22:26 +00:00
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())
2019-10-01 12:44:17 +00:00
continue;
for (auto iterator = db.second->getTablesIterator(getContext()); iterator->isValid(); iterator->next())
{
2020-05-18 08:08:55 +00:00
const auto & table = iterator->table();
2020-06-02 02:06:16 +00:00
if (!table)
continue;
2017-11-04 16:46:14 +00:00
StorageReplicatedMergeTree * table_replicated = dynamic_cast<StorageReplicatedMergeTree *>(table.get());
if (!table_replicated)
continue;
time_t absolute_delay = 0;
time_t relative_delay = 0;
table_replicated->getReplicaDelays(absolute_delay, relative_delay);
if ((settings.min_absolute_delay_to_close && absolute_delay >= static_cast<time_t>(settings.min_absolute_delay_to_close))
|| (settings.min_relative_delay_to_close && relative_delay >= static_cast<time_t>(settings.min_relative_delay_to_close)))
ok = false;
message << backQuoteIfNeed(db.first) << "." << backQuoteIfNeed(iterator->name())
<< ":\tAbsolute delay: " << absolute_delay << ". Relative delay: " << relative_delay << ".\n";
}
}
const auto & config = getContext()->getConfigRef();
setResponseDefaultHeaders(response, config.getUInt("keep_alive_timeout", 10));
2020-08-17 07:46:38 +00:00
if (!ok)
{
response.setStatusAndReason(Poco::Net::HTTPResponse::HTTP_SERVICE_UNAVAILABLE);
verbose = true;
}
if (verbose)
*response.send() << message.str();
2020-08-17 07:46:38 +00:00
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())
{
2019-04-24 19:38:11 +00:00
/// We have not sent anything yet and we don't even know if we need to compress response.
*response.send() << getCurrentExceptionMessage(false) << std::endl;
}
}
catch (...)
{
2020-05-30 21:57:37 +00:00
LOG_ERROR((&Poco::Logger::get("ReplicasStatusHandler")), "Cannot send exception to client");
}
}
2016-01-17 13:34:36 +00:00
}
HTTPRequestHandlerFactoryPtr createReplicasStatusHandlerFactory(IServer & server, const std::string & config_prefix)
{
auto factory = std::make_shared<HandlingRuleHTTPHandlerFactory<ReplicasStatusHandler>>(server);
factory->addFiltersFromConfig(server.config(), config_prefix);
return factory;
}
2016-01-17 13:34:36 +00:00
}