ClickHouse/dbms/Interpreters/InterserverIOHandler.h

84 lines
2.2 KiB
C++
Raw Normal View History

2014-03-21 13:42:14 +00:00
#pragma once
#include <IO/ReadBuffer.h>
#include <IO/WriteBuffer.h>
#include <IO/ReadBufferFromString.h>
#include <IO/ReadHelpers.h>
#include <IO/WriteBufferFromString.h>
#include <IO/WriteHelpers.h>
#include <Common/ActionBlocker.h>
#include <Core/Types.h>
2014-03-21 13:42:14 +00:00
#include <map>
#include <atomic>
#include <utility>
2020-01-14 14:27:48 +00:00
#include <shared_mutex>
2014-03-21 13:42:14 +00:00
#include <Poco/Net/HTMLForm.h>
namespace Poco { namespace Net { class HTTPServerResponse; } }
2014-03-21 13:42:14 +00:00
namespace DB
{
namespace ErrorCodes
{
extern const int DUPLICATE_INTERSERVER_IO_ENDPOINT;
extern const int NO_SUCH_INTERSERVER_IO_ENDPOINT;
}
2017-06-02 21:37:28 +00:00
/** Query processor from other servers.
2014-03-21 13:42:14 +00:00
*/
class InterserverIOEndpoint
{
public:
virtual std::string getId(const std::string & path) const = 0;
virtual void processQuery(const Poco::Net::HTMLForm & params, ReadBuffer & body, WriteBuffer & out, Poco::Net::HTTPServerResponse & response) = 0;
virtual ~InterserverIOEndpoint() {}
/// You need to stop the data transfer if blocker is activated.
ActionBlocker blocker;
2020-01-14 14:27:48 +00:00
std::shared_mutex rwlock;
2014-03-21 13:42:14 +00:00
};
using InterserverIOEndpointPtr = std::shared_ptr<InterserverIOEndpoint>;
2014-03-21 13:42:14 +00:00
2017-06-02 21:37:28 +00:00
/** Here you can register a service that processes requests from other servers.
* Used to transfer chunks in ReplicatedMergeTree.
2014-03-21 13:42:14 +00:00
*/
class InterserverIOHandler
{
public:
void addEndpoint(const String & name, InterserverIOEndpointPtr endpoint)
{
2019-01-02 06:44:36 +00:00
std::lock_guard lock(mutex);
bool inserted = endpoint_map.try_emplace(name, std::move(endpoint)).second;
if (!inserted)
throw Exception("Duplicate interserver IO endpoint: " + name, ErrorCodes::DUPLICATE_INTERSERVER_IO_ENDPOINT);
}
2020-01-14 14:27:48 +00:00
bool removeEndpointIfExists(const String & name)
{
2019-01-02 06:44:36 +00:00
std::lock_guard lock(mutex);
2020-01-14 14:27:48 +00:00
return endpoint_map.erase(name);
}
InterserverIOEndpointPtr getEndpoint(const String & name)
try
{
2019-01-02 06:44:36 +00:00
std::lock_guard lock(mutex);
return endpoint_map.at(name);
}
catch (...)
{
throw Exception("No interserver IO endpoint named " + name, ErrorCodes::NO_SUCH_INTERSERVER_IO_ENDPOINT);
}
2014-03-21 13:42:14 +00:00
private:
using EndpointMap = std::map<String, InterserverIOEndpointPtr>;
2014-03-21 13:42:14 +00:00
EndpointMap endpoint_map;
std::mutex mutex;
2014-03-21 13:42:14 +00:00
};
}