2014-03-21 13:42:14 +00:00
|
|
|
#pragma once
|
|
|
|
|
2017-04-01 09:19:00 +00:00
|
|
|
#include <IO/ReadBuffer.h>
|
|
|
|
#include <IO/WriteBuffer.h>
|
|
|
|
#include <IO/ReadBufferFromString.h>
|
|
|
|
#include <IO/ReadHelpers.h>
|
|
|
|
#include <IO/WriteBufferFromString.h>
|
|
|
|
#include <IO/WriteHelpers.h>
|
2017-10-06 16:53:55 +00:00
|
|
|
#include <Common/ActionBlocker.h>
|
2023-01-12 15:51:04 +00:00
|
|
|
#include <Common/SharedMutex.h>
|
2021-10-02 07:13:14 +00:00
|
|
|
#include <base/types.h>
|
2021-02-19 12:51:26 +00:00
|
|
|
|
|
|
|
#include <atomic>
|
|
|
|
#include <map>
|
|
|
|
#include <utility>
|
2017-04-06 13:03:23 +00:00
|
|
|
|
2020-10-08 15:45:10 +00:00
|
|
|
namespace zkutil
|
|
|
|
{
|
|
|
|
class ZooKeeper;
|
|
|
|
using ZooKeeperPtr = std::shared_ptr<ZooKeeper>;
|
|
|
|
}
|
|
|
|
|
2014-03-21 13:42:14 +00:00
|
|
|
namespace DB
|
|
|
|
{
|
|
|
|
|
2016-01-11 21:46:36 +00:00
|
|
|
namespace ErrorCodes
|
|
|
|
{
|
|
|
|
extern const int DUPLICATE_INTERSERVER_IO_ENDPOINT;
|
|
|
|
extern const int NO_SUCH_INTERSERVER_IO_ENDPOINT;
|
|
|
|
}
|
|
|
|
|
2021-02-19 12:51:26 +00:00
|
|
|
class HTMLForm;
|
|
|
|
class HTTPServerResponse;
|
|
|
|
|
2017-06-02 21:37:28 +00:00
|
|
|
/** Query processor from other servers.
|
2014-03-21 13:42:14 +00:00
|
|
|
*/
|
|
|
|
class InterserverIOEndpoint
|
|
|
|
{
|
|
|
|
public:
|
2016-01-28 01:00:27 +00:00
|
|
|
virtual std::string getId(const std::string & path) const = 0;
|
2021-02-19 12:51:26 +00:00
|
|
|
virtual void processQuery(const HTMLForm & params, ReadBuffer & body, WriteBuffer & out, HTTPServerResponse & response) = 0;
|
2020-04-10 21:29:54 +00:00
|
|
|
virtual ~InterserverIOEndpoint() = default;
|
2015-12-24 21:28:18 +00:00
|
|
|
|
2017-10-06 16:53:55 +00:00
|
|
|
/// You need to stop the data transfer if blocker is activated.
|
|
|
|
ActionBlocker blocker;
|
2023-01-12 15:51:04 +00:00
|
|
|
SharedMutex rwlock;
|
2014-03-21 13:42:14 +00:00
|
|
|
};
|
|
|
|
|
2016-05-28 10:15:36 +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);
|
2018-05-25 15:38:57 +00:00
|
|
|
bool inserted = endpoint_map.try_emplace(name, std::move(endpoint)).second;
|
|
|
|
if (!inserted)
|
2023-01-23 21:13:58 +00:00
|
|
|
throw Exception(ErrorCodes::DUPLICATE_INTERSERVER_IO_ENDPOINT, "Duplicate interserver IO endpoint: {}", name);
|
2014-03-21 13:42:14 +00:00
|
|
|
}
|
2017-04-01 07:20:54 +00:00
|
|
|
|
2020-04-07 12:02:07 +00:00
|
|
|
bool removeEndpointIfExists(const String & name)
|
2014-03-21 13:42:14 +00:00
|
|
|
{
|
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);
|
2014-03-21 13:42:14 +00:00
|
|
|
}
|
2017-04-01 07:20:54 +00:00
|
|
|
|
2022-05-03 08:20:50 +00:00
|
|
|
InterserverIOEndpointPtr getEndpoint(const String & name) const
|
2018-05-25 15:38:57 +00:00
|
|
|
try
|
2014-03-21 13:42:14 +00:00
|
|
|
{
|
2019-01-02 06:44:36 +00:00
|
|
|
std::lock_guard lock(mutex);
|
2018-05-25 15:38:57 +00:00
|
|
|
return endpoint_map.at(name);
|
|
|
|
}
|
|
|
|
catch (...)
|
|
|
|
{
|
2023-01-23 21:13:58 +00:00
|
|
|
throw Exception(ErrorCodes::NO_SUCH_INTERSERVER_IO_ENDPOINT, "No interserver IO endpoint named {}", name);
|
2014-03-21 13:42:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
2016-05-28 10:35:44 +00:00
|
|
|
using EndpointMap = std::map<String, InterserverIOEndpointPtr>;
|
2014-03-21 13:42:14 +00:00
|
|
|
|
|
|
|
EndpointMap endpoint_map;
|
2022-05-03 08:20:50 +00:00
|
|
|
mutable std::mutex mutex;
|
2014-03-21 13:42:14 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
}
|