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>
|
2017-04-01 09:19:00 +00:00
|
|
|
#include <Core/Types.h>
|
2014-03-21 13:42:14 +00:00
|
|
|
#include <map>
|
2015-12-24 21:28:18 +00:00
|
|
|
#include <atomic>
|
2017-10-06 16:53:55 +00:00
|
|
|
#include <utility>
|
2014-03-21 13:42:14 +00:00
|
|
|
#include <Poco/Net/HTMLForm.h>
|
|
|
|
|
2017-04-06 13:03:23 +00:00
|
|
|
namespace Poco { namespace Net { class HTTPServerResponse; } }
|
|
|
|
|
2014-03-21 13:42:14 +00:00
|
|
|
namespace DB
|
|
|
|
{
|
|
|
|
|
2016-01-11 21:46:36 +00:00
|
|
|
namespace ErrorCodes
|
|
|
|
{
|
2017-04-01 07:20:54 +00:00
|
|
|
extern const int DUPLICATE_INTERSERVER_IO_ENDPOINT;
|
|
|
|
extern const int NO_SUCH_INTERSERVER_IO_ENDPOINT;
|
2016-01-11 21:46:36 +00:00
|
|
|
}
|
|
|
|
|
2017-06-02 21:37:28 +00:00
|
|
|
/** Location of the service.
|
2016-01-28 01:00:27 +00:00
|
|
|
*/
|
|
|
|
struct InterserverIOEndpointLocation
|
|
|
|
{
|
|
|
|
public:
|
2017-04-01 07:20:54 +00:00
|
|
|
InterserverIOEndpointLocation(const std::string & name_, const std::string & host_, UInt16 port_)
|
|
|
|
: name(name_), host(host_), port(port_)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2017-06-02 21:37:28 +00:00
|
|
|
/// Creates a location based on its serialized representation.
|
2017-04-01 07:20:54 +00:00
|
|
|
InterserverIOEndpointLocation(const std::string & serialized_location)
|
|
|
|
{
|
|
|
|
ReadBufferFromString buf(serialized_location);
|
|
|
|
readBinary(name, buf);
|
|
|
|
readBinary(host, buf);
|
|
|
|
readBinary(port, buf);
|
|
|
|
assertEOF(buf);
|
|
|
|
}
|
|
|
|
|
2017-06-02 21:37:28 +00:00
|
|
|
/// Serializes the location.
|
2017-04-01 07:20:54 +00:00
|
|
|
std::string toString() const
|
|
|
|
{
|
2017-07-31 21:39:24 +00:00
|
|
|
WriteBufferFromOwnString buf;
|
2017-04-01 07:20:54 +00:00
|
|
|
writeBinary(name, buf);
|
|
|
|
writeBinary(host, buf);
|
|
|
|
writeBinary(port, buf);
|
2017-07-31 21:39:24 +00:00
|
|
|
return buf.str();
|
2017-04-01 07:20:54 +00:00
|
|
|
}
|
2016-01-28 01:00:27 +00:00
|
|
|
|
|
|
|
public:
|
2017-04-01 07:20:54 +00:00
|
|
|
std::string name;
|
|
|
|
std::string host;
|
|
|
|
UInt16 port;
|
2016-01-28 01:00:27 +00:00
|
|
|
};
|
2016-01-11 21:46:36 +00:00
|
|
|
|
2017-06-02 21:37:28 +00:00
|
|
|
/** Query processor from other servers.
|
2014-03-21 13:42:14 +00:00
|
|
|
*/
|
|
|
|
class InterserverIOEndpoint
|
|
|
|
{
|
|
|
|
public:
|
2017-04-01 07:20:54 +00:00
|
|
|
virtual std::string getId(const std::string & path) const = 0;
|
2017-04-06 13:03:23 +00:00
|
|
|
virtual void processQuery(const Poco::Net::HTMLForm & params, ReadBuffer & body, WriteBuffer & out, Poco::Net::HTTPServerResponse & response) = 0;
|
2017-04-01 07:20:54 +00:00
|
|
|
virtual ~InterserverIOEndpoint() {}
|
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;
|
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:
|
2017-04-01 07:20:54 +00:00
|
|
|
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)
|
2017-04-01 07:20:54 +00:00
|
|
|
throw Exception("Duplicate interserver IO endpoint: " + name, ErrorCodes::DUPLICATE_INTERSERVER_IO_ENDPOINT);
|
|
|
|
}
|
|
|
|
|
|
|
|
void removeEndpoint(const String & name)
|
|
|
|
{
|
2019-01-02 06:44:36 +00:00
|
|
|
std::lock_guard lock(mutex);
|
2018-05-25 15:38:57 +00:00
|
|
|
if (!endpoint_map.erase(name))
|
2017-04-01 07:20:54 +00:00
|
|
|
throw Exception("No interserver IO endpoint named " + name, ErrorCodes::NO_SUCH_INTERSERVER_IO_ENDPOINT);
|
|
|
|
}
|
|
|
|
|
|
|
|
InterserverIOEndpointPtr getEndpoint(const String & name)
|
2018-05-25 15:38:57 +00:00
|
|
|
try
|
2017-04-01 07:20:54 +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 (...)
|
|
|
|
{
|
|
|
|
throw Exception("No interserver IO endpoint named " + name, ErrorCodes::NO_SUCH_INTERSERVER_IO_ENDPOINT);
|
2017-04-01 07:20:54 +00:00
|
|
|
}
|
2014-03-21 13:42:14 +00:00
|
|
|
|
|
|
|
private:
|
2017-04-01 07:20:54 +00:00
|
|
|
using EndpointMap = std::map<String, InterserverIOEndpointPtr>;
|
2014-03-21 13:42:14 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
EndpointMap endpoint_map;
|
|
|
|
std::mutex mutex;
|
2014-03-21 13:42:14 +00:00
|
|
|
};
|
|
|
|
|
2017-06-02 21:37:28 +00:00
|
|
|
/// In the constructor calls `addEndpoint`, in the destructor - `removeEndpoint`.
|
2014-03-22 14:44:44 +00:00
|
|
|
class InterserverIOEndpointHolder
|
|
|
|
{
|
|
|
|
public:
|
2017-04-01 07:20:54 +00:00
|
|
|
InterserverIOEndpointHolder(const String & name_, InterserverIOEndpointPtr endpoint_, InterserverIOHandler & handler_)
|
2017-10-06 16:53:55 +00:00
|
|
|
: name(name_), endpoint(std::move(endpoint_)), handler(handler_)
|
2017-04-01 07:20:54 +00:00
|
|
|
{
|
|
|
|
handler.addEndpoint(name, endpoint);
|
|
|
|
}
|
|
|
|
|
|
|
|
InterserverIOEndpointPtr getEndpoint()
|
|
|
|
{
|
|
|
|
return endpoint;
|
|
|
|
}
|
|
|
|
|
|
|
|
~InterserverIOEndpointHolder()
|
2018-05-25 15:38:57 +00:00
|
|
|
try
|
|
|
|
{
|
|
|
|
handler.removeEndpoint(name);
|
|
|
|
/// After destroying the object, `endpoint` can still live, since its ownership is acquired during the processing of the request,
|
|
|
|
/// see InterserverIOHTTPHandler.cpp
|
|
|
|
}
|
|
|
|
catch (...)
|
2017-04-01 07:20:54 +00:00
|
|
|
{
|
2018-05-25 15:38:57 +00:00
|
|
|
tryLogCurrentException("~InterserverIOEndpointHolder");
|
2017-04-01 07:20:54 +00:00
|
|
|
}
|
|
|
|
|
2017-10-06 16:53:55 +00:00
|
|
|
ActionBlocker & getBlocker() { return endpoint->blocker; }
|
2015-12-24 21:28:18 +00:00
|
|
|
|
2014-03-22 14:44:44 +00:00
|
|
|
private:
|
2017-04-01 07:20:54 +00:00
|
|
|
String name;
|
|
|
|
InterserverIOEndpointPtr endpoint;
|
|
|
|
InterserverIOHandler & handler;
|
2014-03-22 14:44:44 +00:00
|
|
|
};
|
|
|
|
|
2016-05-28 10:15:36 +00:00
|
|
|
using InterserverIOEndpointHolderPtr = std::shared_ptr<InterserverIOEndpointHolder>;
|
2014-03-22 14:44:44 +00:00
|
|
|
|
2014-03-21 13:42:14 +00:00
|
|
|
}
|