2011-11-21 17:19:25 +00:00
|
|
|
|
#pragma once
|
|
|
|
|
|
|
|
|
|
#include <Poco/URI.h>
|
|
|
|
|
#include <Poco/Net/HTTPRequest.h>
|
|
|
|
|
#include <Poco/Net/HTTPResponse.h>
|
|
|
|
|
#include <Poco/Net/HTTPClientSession.h>
|
2012-02-16 17:41:21 +00:00
|
|
|
|
#include <Poco/Net/NetException.h>
|
2011-11-21 17:19:25 +00:00
|
|
|
|
|
|
|
|
|
#include <DB/IO/WriteBuffer.h>
|
|
|
|
|
#include <DB/IO/WriteBufferFromOStream.h>
|
2014-06-05 16:44:12 +00:00
|
|
|
|
#include <DB/IO/HashingWriteBuffer.h>
|
2011-11-21 17:19:25 +00:00
|
|
|
|
|
2015-09-29 19:19:54 +00:00
|
|
|
|
#include <common/logger_useful.h>
|
2011-11-21 17:19:25 +00:00
|
|
|
|
|
2014-12-18 11:39:55 +00:00
|
|
|
|
#define DEFAULT_REMOTE_WRITE_BUFFER_CONNECTION_TIMEOUT 1
|
|
|
|
|
#define DEFAULT_REMOTE_WRITE_BUFFER_RECEIVE_TIMEOUT 1800
|
|
|
|
|
#define DEFAULT_REMOTE_WRITE_BUFFER_SEND_TIMEOUT 1800
|
2011-11-21 17:19:25 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
namespace DB
|
|
|
|
|
{
|
|
|
|
|
|
2016-01-12 02:21:15 +00:00
|
|
|
|
namespace ErrorCodes
|
|
|
|
|
{
|
|
|
|
|
extern const int CANNOT_WRITE_TO_OSTREAM;
|
|
|
|
|
extern const int RECEIVED_ERROR_FROM_REMOTE_IO_SERVER;
|
|
|
|
|
}
|
|
|
|
|
|
2011-11-21 17:19:25 +00:00
|
|
|
|
/** Позволяет писать файл на удалённый сервер.
|
|
|
|
|
*/
|
|
|
|
|
class RemoteWriteBuffer : public WriteBuffer
|
|
|
|
|
{
|
|
|
|
|
private:
|
|
|
|
|
std::string host;
|
|
|
|
|
int port;
|
|
|
|
|
std::string path;
|
2012-02-01 20:08:15 +00:00
|
|
|
|
std::string encoded_path;
|
|
|
|
|
std::string encoded_tmp_path;
|
2011-11-21 17:19:25 +00:00
|
|
|
|
std::string tmp_path;
|
|
|
|
|
std::string if_exists;
|
|
|
|
|
bool decompress;
|
2012-02-16 17:41:21 +00:00
|
|
|
|
unsigned connection_retries;
|
2011-11-21 17:19:25 +00:00
|
|
|
|
|
|
|
|
|
std::string uri_str;
|
|
|
|
|
|
|
|
|
|
Poco::Net::HTTPClientSession session;
|
|
|
|
|
std::ostream * ostr; /// этим владеет session
|
2016-05-28 12:22:22 +00:00
|
|
|
|
std::unique_ptr<WriteBuffer> impl;
|
2012-02-01 20:08:15 +00:00
|
|
|
|
|
|
|
|
|
/// Отправили все данные и переименовали файл
|
|
|
|
|
bool finalized;
|
2011-11-21 17:19:25 +00:00
|
|
|
|
public:
|
|
|
|
|
/** Если tmp_path не пустой, то записывает сначала временный файл, а затем переименовывает,
|
|
|
|
|
* удаляя существующие файлы, если есть.
|
|
|
|
|
* Иначе используется параметр if_exists.
|
|
|
|
|
*/
|
|
|
|
|
RemoteWriteBuffer(const std::string & host_, int port_, const std::string & path_,
|
2012-02-16 19:31:14 +00:00
|
|
|
|
const std::string & tmp_path_ = "", const std::string & if_exists_ = "remove",
|
2014-12-18 11:39:55 +00:00
|
|
|
|
bool decompress_ = false,
|
|
|
|
|
unsigned connection_retries_ = 3,
|
|
|
|
|
size_t buffer_size_ = DBMS_DEFAULT_BUFFER_SIZE,
|
|
|
|
|
const Poco::Timespan & connection_timeout = Poco::Timespan(DEFAULT_REMOTE_WRITE_BUFFER_CONNECTION_TIMEOUT, 0),
|
|
|
|
|
const Poco::Timespan & send_timeout = Poco::Timespan(DEFAULT_REMOTE_WRITE_BUFFER_SEND_TIMEOUT, 0),
|
|
|
|
|
const Poco::Timespan & receive_timeout = Poco::Timespan(DEFAULT_REMOTE_WRITE_BUFFER_RECEIVE_TIMEOUT, 0))
|
2014-04-08 07:58:53 +00:00
|
|
|
|
: WriteBuffer(nullptr, 0), host(host_), port(port_), path(path_),
|
2011-11-21 17:19:25 +00:00
|
|
|
|
tmp_path(tmp_path_), if_exists(if_exists_),
|
2014-06-17 11:41:54 +00:00
|
|
|
|
decompress(decompress_), connection_retries(connection_retries_), finalized(false)
|
2011-11-21 17:19:25 +00:00
|
|
|
|
{
|
|
|
|
|
Poco::URI::encode(path, "&#", encoded_path);
|
|
|
|
|
Poco::URI::encode(tmp_path, "&#", encoded_tmp_path);
|
2016-01-12 02:21:15 +00:00
|
|
|
|
|
2011-11-21 17:19:25 +00:00
|
|
|
|
std::stringstream uri;
|
2011-11-21 18:17:53 +00:00
|
|
|
|
uri << "http://" << host << ":" << port
|
2012-02-01 20:08:15 +00:00
|
|
|
|
<< "/?action=write"
|
|
|
|
|
<< "&path=" << (tmp_path.empty() ? encoded_path : encoded_tmp_path)
|
2011-11-21 17:19:25 +00:00
|
|
|
|
<< "&if_exists=" << if_exists
|
|
|
|
|
<< "&decompress=" << (decompress ? "true" : "false");
|
|
|
|
|
|
2012-11-08 09:46:01 +00:00
|
|
|
|
uri_str = Poco::URI(uri.str()).getPathAndQuery();
|
2011-11-21 17:19:25 +00:00
|
|
|
|
|
|
|
|
|
session.setHost(host);
|
|
|
|
|
session.setPort(port);
|
2012-11-08 09:46:01 +00:00
|
|
|
|
session.setKeepAlive(true);
|
2016-01-12 02:21:15 +00:00
|
|
|
|
|
2011-11-21 17:19:25 +00:00
|
|
|
|
/// устанавливаем таймаут
|
2014-12-18 11:39:55 +00:00
|
|
|
|
session.setTimeout(connection_timeout, send_timeout, receive_timeout);
|
2016-01-12 02:21:15 +00:00
|
|
|
|
|
2012-11-08 09:46:01 +00:00
|
|
|
|
Poco::Net::HTTPRequest request(Poco::Net::HTTPRequest::HTTP_POST, uri_str, Poco::Net::HTTPRequest::HTTP_1_1);
|
2016-01-12 02:21:15 +00:00
|
|
|
|
|
2012-11-08 09:46:01 +00:00
|
|
|
|
request.setChunkedTransferEncoding(true);
|
2016-01-12 02:21:15 +00:00
|
|
|
|
|
2012-02-16 17:41:21 +00:00
|
|
|
|
for (unsigned i = 0; i < connection_retries; ++i)
|
|
|
|
|
{
|
2012-11-08 09:46:01 +00:00
|
|
|
|
LOG_TRACE((&Logger::get("RemoteWriteBuffer")), "Sending write request to " << host << ":" << port << uri_str);
|
2012-02-16 17:41:21 +00:00
|
|
|
|
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
ostr = &session.sendRequest(request);
|
|
|
|
|
}
|
|
|
|
|
catch (const Poco::Net::NetException & e)
|
|
|
|
|
{
|
|
|
|
|
if (i + 1 == connection_retries)
|
|
|
|
|
throw;
|
|
|
|
|
|
2012-11-08 18:30:49 +00:00
|
|
|
|
LOG_WARNING((&Logger::get("RemoteWriteBuffer")), e.displayText() << ", URL: " << host << ":" << port << uri_str << ", try No " << i + 1 << ".");
|
2012-02-16 17:41:21 +00:00
|
|
|
|
session.reset();
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
catch (const Poco::TimeoutException & e)
|
|
|
|
|
{
|
|
|
|
|
if (i + 1 == connection_retries)
|
|
|
|
|
throw;
|
|
|
|
|
|
2012-11-08 09:46:01 +00:00
|
|
|
|
LOG_WARNING((&Logger::get("RemoteWriteBuffer")), "Connection timeout from " << host << ":" << port << uri_str << ", try No " << i + 1 << ".");
|
2012-02-16 17:41:21 +00:00
|
|
|
|
session.reset();
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
}
|
2011-11-21 17:19:25 +00:00
|
|
|
|
|
2016-05-28 12:22:22 +00:00
|
|
|
|
impl = std::make_unique<WriteBufferFromOStream>(*ostr, buffer_size_);
|
2014-06-05 16:44:12 +00:00
|
|
|
|
|
2011-11-21 18:17:53 +00:00
|
|
|
|
set(impl->buffer().begin(), impl->buffer().size());
|
2011-11-21 17:19:25 +00:00
|
|
|
|
}
|
|
|
|
|
|
2016-08-19 01:54:23 +00:00
|
|
|
|
void nextImpl() override
|
2011-11-21 17:19:25 +00:00
|
|
|
|
{
|
2012-03-23 18:54:55 +00:00
|
|
|
|
if (!offset() || finalized)
|
2011-11-21 17:19:25 +00:00
|
|
|
|
return;
|
2011-11-21 18:17:53 +00:00
|
|
|
|
|
2012-02-02 18:25:27 +00:00
|
|
|
|
/// Для корректной работы с AsynchronousWriteBuffer, который подменяет буферы.
|
|
|
|
|
impl->set(buffer().begin(), buffer().size());
|
2016-01-12 02:21:15 +00:00
|
|
|
|
|
2011-11-21 18:17:53 +00:00
|
|
|
|
impl->position() = pos;
|
2012-02-02 17:46:48 +00:00
|
|
|
|
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
impl->next();
|
|
|
|
|
}
|
2013-10-26 03:20:51 +00:00
|
|
|
|
catch (const Exception & e)
|
2012-02-02 17:46:48 +00:00
|
|
|
|
{
|
|
|
|
|
if (e.code() == ErrorCodes::CANNOT_WRITE_TO_OSTREAM)
|
|
|
|
|
checkStatus(); /// Меняем сообщение об ошибке на более ясное.
|
|
|
|
|
throw;
|
|
|
|
|
}
|
2011-11-21 17:19:25 +00:00
|
|
|
|
}
|
|
|
|
|
|
2012-02-01 20:08:15 +00:00
|
|
|
|
void finalize()
|
2011-11-21 17:19:25 +00:00
|
|
|
|
{
|
2012-02-01 20:08:15 +00:00
|
|
|
|
if (finalized)
|
|
|
|
|
return;
|
2016-01-12 02:21:15 +00:00
|
|
|
|
|
2012-02-01 20:08:15 +00:00
|
|
|
|
next();
|
2012-02-02 17:46:48 +00:00
|
|
|
|
checkStatus();
|
2011-12-12 00:38:01 +00:00
|
|
|
|
|
2012-02-01 20:08:15 +00:00
|
|
|
|
/// Переименовываем файл, если нужно.
|
|
|
|
|
if (!tmp_path.empty())
|
|
|
|
|
rename();
|
2011-11-21 17:19:25 +00:00
|
|
|
|
|
2012-02-01 20:08:15 +00:00
|
|
|
|
finalized = true;
|
|
|
|
|
}
|
|
|
|
|
|
2012-03-23 18:54:55 +00:00
|
|
|
|
void cancel()
|
|
|
|
|
{
|
|
|
|
|
finalized = true;
|
|
|
|
|
}
|
|
|
|
|
|
2012-02-02 23:14:08 +00:00
|
|
|
|
~RemoteWriteBuffer()
|
2012-02-01 20:08:15 +00:00
|
|
|
|
{
|
2013-11-18 17:17:45 +00:00
|
|
|
|
try
|
|
|
|
|
{
|
2012-02-01 20:08:15 +00:00
|
|
|
|
finalize();
|
2013-11-18 17:17:45 +00:00
|
|
|
|
}
|
|
|
|
|
catch (...)
|
|
|
|
|
{
|
2013-11-18 19:18:03 +00:00
|
|
|
|
tryLogCurrentException(__PRETTY_FUNCTION__);
|
2013-11-18 17:17:45 +00:00
|
|
|
|
}
|
2012-02-01 20:08:15 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private:
|
2012-02-02 17:46:48 +00:00
|
|
|
|
|
|
|
|
|
void checkStatus()
|
|
|
|
|
{
|
|
|
|
|
Poco::Net::HTTPResponse response;
|
|
|
|
|
std::istream & istr = session.receiveResponse(response);
|
|
|
|
|
Poco::Net::HTTPResponse::HTTPStatus status = response.getStatus();
|
|
|
|
|
|
2012-02-02 17:53:18 +00:00
|
|
|
|
std::stringstream message;
|
|
|
|
|
message << istr.rdbuf();
|
|
|
|
|
|
|
|
|
|
if (status != Poco::Net::HTTPResponse::HTTP_OK || message.str() != "Ok.\n")
|
2012-02-02 17:46:48 +00:00
|
|
|
|
{
|
|
|
|
|
std::stringstream error_message;
|
2012-02-02 17:53:18 +00:00
|
|
|
|
error_message << "Received error from remote server " << uri_str << ", body: " << message.str();
|
2012-02-02 17:46:48 +00:00
|
|
|
|
|
|
|
|
|
throw Exception(error_message.str(), ErrorCodes::RECEIVED_ERROR_FROM_REMOTE_IO_SERVER);
|
|
|
|
|
}
|
|
|
|
|
}
|
2016-01-12 02:21:15 +00:00
|
|
|
|
|
2012-02-01 20:08:15 +00:00
|
|
|
|
void rename()
|
|
|
|
|
{
|
|
|
|
|
std::stringstream uri;
|
|
|
|
|
uri << "http://" << host << ":" << port
|
|
|
|
|
<< "/?action=rename"
|
|
|
|
|
<< "&from=" << encoded_tmp_path
|
|
|
|
|
<< "&to=" << encoded_path;
|
|
|
|
|
|
2012-11-08 09:46:01 +00:00
|
|
|
|
uri_str = Poco::URI(uri.str()).getPathAndQuery();
|
2016-01-12 02:21:15 +00:00
|
|
|
|
|
2012-11-08 09:46:01 +00:00
|
|
|
|
Poco::Net::HTTPRequest request(Poco::Net::HTTPRequest::HTTP_GET, uri_str, Poco::Net::HTTPRequest::HTTP_1_1);
|
2016-01-12 02:21:15 +00:00
|
|
|
|
|
2012-02-16 17:41:21 +00:00
|
|
|
|
for (unsigned i = 0; i < connection_retries; ++i)
|
|
|
|
|
{
|
2012-11-08 09:46:01 +00:00
|
|
|
|
LOG_TRACE((&Logger::get("RemoteWriteBuffer")), "Sending rename request to " << host << ":" << port << uri_str);
|
2012-02-16 17:41:21 +00:00
|
|
|
|
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
session.sendRequest(request);
|
|
|
|
|
checkStatus();
|
|
|
|
|
}
|
|
|
|
|
catch (const Poco::Net::NetException & e)
|
|
|
|
|
{
|
|
|
|
|
if (i + 1 == connection_retries)
|
|
|
|
|
throw;
|
2016-01-12 02:21:15 +00:00
|
|
|
|
|
2012-11-08 18:30:49 +00:00
|
|
|
|
LOG_WARNING((&Logger::get("RemoteWriteBuffer")), e.what() << ", message: " << e.displayText()
|
2012-11-08 09:46:01 +00:00
|
|
|
|
<< ", URL: " << host << ":" << port << uri_str << ", try No " << i + 1 << ".");
|
2012-02-16 17:41:21 +00:00
|
|
|
|
session.reset();
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
catch (const Poco::TimeoutException & e)
|
|
|
|
|
{
|
|
|
|
|
if (i + 1 == connection_retries)
|
|
|
|
|
throw;
|
2016-01-12 02:21:15 +00:00
|
|
|
|
|
2012-11-08 09:46:01 +00:00
|
|
|
|
LOG_WARNING((&Logger::get("RemoteWriteBuffer")), "Connection timeout from " << host << ":" << port << uri_str << ", try No " << i + 1 << ".");
|
2012-02-16 17:41:21 +00:00
|
|
|
|
session.reset();
|
|
|
|
|
continue;
|
|
|
|
|
}
|
2012-05-22 16:47:52 +00:00
|
|
|
|
catch (const Exception & e)
|
2012-03-22 19:53:10 +00:00
|
|
|
|
{
|
|
|
|
|
/// Если в прошлую попытку от сервера не пришло ответа, но файл всё же был переименован.
|
2012-05-22 16:47:52 +00:00
|
|
|
|
if (i != 0 && e.code() == ErrorCodes::RECEIVED_ERROR_FROM_REMOTE_IO_SERVER
|
2014-04-08 07:47:51 +00:00
|
|
|
|
&& nullptr != strstr(e.displayText().data(), "File not found"))
|
2012-03-22 19:53:10 +00:00
|
|
|
|
{
|
|
|
|
|
LOG_TRACE((&Logger::get("RemoteWriteBuffer")), "File already renamed");
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
throw;
|
|
|
|
|
}
|
2012-02-16 17:41:21 +00:00
|
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
}
|
2011-11-21 17:19:25 +00:00
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
}
|