2016-01-17 13:34:36 +00:00
|
|
|
#include "ReplicasStatusHandler.h"
|
|
|
|
|
2017-04-01 09:19:00 +00:00
|
|
|
#include <Interpreters/Context.h>
|
|
|
|
#include <Storages/StorageReplicatedMergeTree.h>
|
|
|
|
#include <Common/HTMLForm.h>
|
2017-07-13 20:58:19 +00:00
|
|
|
#include <Common/typeid_cast.h>
|
2017-04-01 09:19:00 +00:00
|
|
|
#include <Databases/IDatabase.h>
|
|
|
|
#include <IO/HTTPCommon.h>
|
2016-01-17 13:34:36 +00:00
|
|
|
|
|
|
|
#include <Poco/Net/HTTPServerRequest.h>
|
|
|
|
#include <Poco/Net/HTTPServerResponse.h>
|
|
|
|
|
|
|
|
|
|
|
|
namespace DB
|
|
|
|
{
|
|
|
|
|
|
|
|
|
2019-11-24 15:43:53 +00:00
|
|
|
ReplicasStatusHandler::ReplicasStatusHandler(IServer & server)
|
|
|
|
: context(server.context())
|
2016-01-17 13:34:36 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void ReplicasStatusHandler::handleRequest(Poco::Net::HTTPServerRequest & request, Poco::Net::HTTPServerResponse & response)
|
|
|
|
{
|
2017-04-01 07:20:54 +00:00
|
|
|
try
|
|
|
|
{
|
|
|
|
HTMLForm params(request);
|
|
|
|
|
|
|
|
/// Even if lag is small, output detailed information about the lag.
|
|
|
|
bool verbose = params.get("verbose", "") == "1";
|
|
|
|
|
|
|
|
const MergeTreeSettings & settings = context.getMergeTreeSettings();
|
|
|
|
|
|
|
|
bool ok = true;
|
|
|
|
std::stringstream message;
|
|
|
|
|
2020-02-10 13:10:17 +00:00
|
|
|
auto databases = DatabaseCatalog::instance().getDatabases();
|
2017-04-01 07:20:54 +00:00
|
|
|
|
|
|
|
/// Iterate through all the replicated tables.
|
|
|
|
for (const auto & db : databases)
|
|
|
|
{
|
2019-10-01 12:44:17 +00:00
|
|
|
/// Lazy database can not contain replicated tables
|
|
|
|
if (db.second->getEngineName() == "Lazy")
|
|
|
|
continue;
|
|
|
|
|
2020-04-01 22:41:29 +00:00
|
|
|
for (auto iterator = db.second->getTablesIterator(); iterator->isValid(); iterator->next())
|
2017-04-01 07:20:54 +00:00
|
|
|
{
|
|
|
|
auto & table = iterator->table();
|
2017-11-04 16:46:14 +00:00
|
|
|
StorageReplicatedMergeTree * table_replicated = dynamic_cast<StorageReplicatedMergeTree *>(table.get());
|
2017-04-01 07:20:54 +00:00
|
|
|
|
|
|
|
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";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-09-08 11:57:43 +00:00
|
|
|
const auto & config = context.getConfigRef();
|
2017-09-08 16:41:35 +00:00
|
|
|
setResponseDefaultHeaders(response, config.getUInt("keep_alive_timeout", 10));
|
2017-04-01 07:20:54 +00:00
|
|
|
|
|
|
|
if (ok && !verbose)
|
|
|
|
{
|
|
|
|
const char * data = "Ok.\n";
|
|
|
|
response.sendBuffer(data, strlen(data));
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2020-02-14 14:20:27 +00:00
|
|
|
response.setStatusAndReason(Poco::Net::HTTPResponse::HTTP_SERVICE_UNAVAILABLE);
|
2017-04-01 07:20:54 +00:00
|
|
|
response.send() << message.rdbuf();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
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.
|
2017-04-01 07:20:54 +00:00
|
|
|
response.send() << getCurrentExceptionMessage(false) << std::endl;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
catch (...)
|
|
|
|
{
|
|
|
|
LOG_ERROR((&Logger::get("ReplicasStatusHandler")), "Cannot send exception to client");
|
|
|
|
}
|
|
|
|
}
|
2016-01-17 13:34:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
}
|