2012-03-09 03:06:09 +00:00
|
|
|
|
#include <Poco/Net/HTTPServerRequest.h>
|
|
|
|
|
|
|
|
|
|
#include <Yandex/ApplicationServerExt.h>
|
|
|
|
|
|
|
|
|
|
#include <DB/Interpreters/loadMetadata.h>
|
|
|
|
|
#include <DB/Storages/StorageSystemNumbers.h>
|
2012-05-08 11:19:00 +00:00
|
|
|
|
#include <DB/Storages/StorageSystemTables.h>
|
2012-06-18 07:49:19 +00:00
|
|
|
|
#include <DB/Storages/StorageSystemDatabases.h>
|
2013-09-03 20:21:28 +00:00
|
|
|
|
#include <DB/Storages/StorageSystemProcesses.h>
|
2014-01-03 08:20:13 +00:00
|
|
|
|
#include <DB/Storages/StorageSystemEvents.h>
|
2012-03-09 03:06:09 +00:00
|
|
|
|
#include <DB/Storages/StorageSystemOne.h>
|
|
|
|
|
|
|
|
|
|
#include "Server.h"
|
2012-03-09 15:46:52 +00:00
|
|
|
|
#include "HTTPHandler.h"
|
2012-12-14 11:21:07 +00:00
|
|
|
|
#include "OLAPHTTPHandler.h"
|
2012-03-09 15:46:52 +00:00
|
|
|
|
#include "TCPHandler.h"
|
2012-03-09 03:06:09 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
namespace DB
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
/// Отвечает "Ok.\n", если получен любой GET запрос. Используется для проверки живости.
|
|
|
|
|
class PingRequestHandler : public Poco::Net::HTTPRequestHandler
|
|
|
|
|
{
|
|
|
|
|
public:
|
|
|
|
|
PingRequestHandler()
|
|
|
|
|
{
|
|
|
|
|
LOG_TRACE((&Logger::get("PingRequestHandler")), "Ping request.");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void handleRequest(Poco::Net::HTTPServerRequest & request, Poco::Net::HTTPServerResponse & response)
|
|
|
|
|
{
|
|
|
|
|
response.send() << "Ok." << std::endl;
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
2012-12-14 11:21:07 +00:00
|
|
|
|
template<typename HandlerType>
|
|
|
|
|
class HTTPRequestHandlerFactory : public Poco::Net::HTTPRequestHandlerFactory
|
2012-03-09 03:06:09 +00:00
|
|
|
|
{
|
2012-12-14 11:21:07 +00:00
|
|
|
|
private:
|
|
|
|
|
Server & server;
|
|
|
|
|
Logger * log;
|
|
|
|
|
std::string name;
|
|
|
|
|
|
|
|
|
|
public:
|
|
|
|
|
HTTPRequestHandlerFactory(Server & server_, const std::string & name_)
|
|
|
|
|
: server(server_), log(&Logger::get(name_)), name(name_) {}
|
|
|
|
|
|
|
|
|
|
Poco::Net::HTTPRequestHandler * createRequestHandler(const Poco::Net::HTTPServerRequest & request)
|
|
|
|
|
{
|
|
|
|
|
LOG_TRACE(log, "HTTP Request for " << name << ". "
|
|
|
|
|
<< "Method: " << request.getMethod()
|
|
|
|
|
<< ", Address: " << request.clientAddress().toString()
|
|
|
|
|
<< ", User-Agent: " << (request.has("User-Agent") ? request.get("User-Agent") : "none"));
|
|
|
|
|
|
|
|
|
|
if (request.getURI().find('?') != std::string::npos || request.getMethod() == Poco::Net::HTTPRequest::HTTP_POST)
|
|
|
|
|
return new HandlerType(server);
|
|
|
|
|
else if (request.getMethod() == Poco::Net::HTTPRequest::HTTP_GET)
|
|
|
|
|
return new PingRequestHandler();
|
|
|
|
|
else
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
};
|
2012-03-09 03:06:09 +00:00
|
|
|
|
|
|
|
|
|
|
2012-12-14 11:21:07 +00:00
|
|
|
|
class TCPConnectionFactory : public Poco::Net::TCPServerConnectionFactory
|
2012-03-09 15:46:52 +00:00
|
|
|
|
{
|
2012-12-14 11:21:07 +00:00
|
|
|
|
private:
|
|
|
|
|
Server & server;
|
|
|
|
|
Logger * log;
|
|
|
|
|
|
|
|
|
|
public:
|
|
|
|
|
TCPConnectionFactory(Server & server_) : server(server_), log(&Logger::get("TCPConnectionFactory")) {}
|
2012-03-09 15:46:52 +00:00
|
|
|
|
|
2012-12-14 11:21:07 +00:00
|
|
|
|
Poco::Net::TCPServerConnection * createConnection(const Poco::Net::StreamSocket & socket)
|
|
|
|
|
{
|
2013-01-28 18:20:20 +00:00
|
|
|
|
LOG_TRACE(log, "TCP Request. " << "Address: " << socket.peerAddress().toString());
|
2012-12-14 11:21:07 +00:00
|
|
|
|
|
|
|
|
|
return new TCPHandler(server, socket);
|
|
|
|
|
}
|
|
|
|
|
};
|
2012-03-09 15:46:52 +00:00
|
|
|
|
|
|
|
|
|
|
2012-03-09 03:06:09 +00:00
|
|
|
|
int Server::main(const std::vector<std::string> & args)
|
|
|
|
|
{
|
2012-06-24 23:35:13 +00:00
|
|
|
|
Logger * log = &logger();
|
|
|
|
|
|
2012-03-09 03:06:09 +00:00
|
|
|
|
/// Заранее инициализируем DateLUT, чтобы первая инициализация потом не влияла на измеряемую скорость выполнения.
|
2012-06-24 23:25:07 +00:00
|
|
|
|
LOG_DEBUG(log, "Initializing DateLUT.");
|
2013-08-11 03:40:14 +00:00
|
|
|
|
DateLUTSingleton::instance();
|
2012-06-24 23:25:07 +00:00
|
|
|
|
LOG_TRACE(log, "Initialized DateLUT.");
|
2012-03-09 03:56:12 +00:00
|
|
|
|
|
2013-09-14 05:14:22 +00:00
|
|
|
|
global_context = new Context;
|
|
|
|
|
|
2012-03-09 03:56:12 +00:00
|
|
|
|
/** Контекст содержит всё, что влияет на обработку запроса:
|
|
|
|
|
* настройки, набор функций, типов данных, агрегатных функций, баз данных...
|
|
|
|
|
*/
|
2013-09-14 05:14:22 +00:00
|
|
|
|
global_context->setGlobalContext(*global_context);
|
|
|
|
|
global_context->setPath(config.getString("path"));
|
2012-03-09 03:06:09 +00:00
|
|
|
|
|
2013-08-10 07:46:45 +00:00
|
|
|
|
/// Загружаем пользователей.
|
2013-09-14 05:14:22 +00:00
|
|
|
|
global_context->initUsersFromConfig();
|
2013-08-10 07:46:45 +00:00
|
|
|
|
|
2013-08-12 00:36:18 +00:00
|
|
|
|
/// Загружаем квоты.
|
2013-09-14 05:14:22 +00:00
|
|
|
|
global_context->initQuotasFromConfig();
|
2013-08-12 00:36:18 +00:00
|
|
|
|
|
2013-09-03 20:21:28 +00:00
|
|
|
|
/// Максимальное количество одновременно выполняющихся запросов.
|
2013-09-14 05:14:22 +00:00
|
|
|
|
global_context->getProcessList().setMaxSize(config.getInt("max_concurrent_queries", 0));
|
2013-09-03 20:21:28 +00:00
|
|
|
|
|
2013-09-08 05:53:10 +00:00
|
|
|
|
/// Размер кэша разжатых блоков. Если нулевой - кэш отключён.
|
|
|
|
|
size_t uncompressed_cache_size = config.getInt("uncompressed_cache_size", 0);
|
|
|
|
|
if (uncompressed_cache_size)
|
2013-09-14 05:14:22 +00:00
|
|
|
|
global_context->setUncompressedCache(uncompressed_cache_size);
|
2013-09-08 05:53:10 +00:00
|
|
|
|
|
2012-10-22 20:08:28 +00:00
|
|
|
|
/// Загружаем настройки.
|
2013-09-14 05:14:22 +00:00
|
|
|
|
Settings & settings = global_context->getSettingsRef();
|
2013-02-17 19:54:32 +00:00
|
|
|
|
settings.setProfile(config.getString("default_profile", "default"));
|
2012-10-22 20:08:28 +00:00
|
|
|
|
|
|
|
|
|
LOG_INFO(log, "Loading metadata.");
|
2013-09-14 05:14:22 +00:00
|
|
|
|
loadMetadata(*global_context);
|
2012-10-22 20:08:28 +00:00
|
|
|
|
LOG_DEBUG(log, "Loaded metadata.");
|
|
|
|
|
|
|
|
|
|
/// Создаём системные таблицы.
|
2013-09-14 05:14:22 +00:00
|
|
|
|
global_context->addDatabase("system");
|
2012-03-09 03:06:09 +00:00
|
|
|
|
|
2013-09-14 05:14:22 +00:00
|
|
|
|
global_context->addTable("system", "one", StorageSystemOne::create("one"));
|
|
|
|
|
global_context->addTable("system", "numbers", StorageSystemNumbers::create("numbers"));
|
|
|
|
|
global_context->addTable("system", "tables", StorageSystemTables::create("tables", *global_context));
|
2014-01-03 08:20:13 +00:00
|
|
|
|
global_context->addTable("system", "databases", StorageSystemDatabases::create("databases", *global_context));
|
|
|
|
|
global_context->addTable("system", "processes", StorageSystemProcesses::create("processes", *global_context));
|
|
|
|
|
global_context->addTable("system", "events", StorageSystemEvents::create("events"));
|
2012-10-22 20:08:28 +00:00
|
|
|
|
|
2013-09-14 05:14:22 +00:00
|
|
|
|
global_context->setCurrentDatabase(config.getString("default_database", "default"));
|
2012-10-22 20:08:28 +00:00
|
|
|
|
|
2013-09-14 05:14:22 +00:00
|
|
|
|
{
|
|
|
|
|
bool use_olap_server = config.getBool("use_olap_http_server", false);
|
|
|
|
|
Poco::Timespan keep_alive_timeout(config.getInt("keep_alive_timeout", 10), 0);
|
|
|
|
|
|
|
|
|
|
Poco::ThreadPool server_pool(3, config.getInt("max_connections", 1024));
|
|
|
|
|
Poco::Net::HTTPServerParams * http_params = new Poco::Net::HTTPServerParams;
|
|
|
|
|
http_params->setTimeout(settings.receive_timeout);
|
|
|
|
|
http_params->setKeepAliveTimeout(keep_alive_timeout);
|
|
|
|
|
|
|
|
|
|
/// HTTP
|
|
|
|
|
Poco::Net::ServerSocket http_socket(Poco::Net::SocketAddress("[::]:" + config.getString("http_port")));
|
|
|
|
|
http_socket.setReceiveTimeout(settings.receive_timeout);
|
|
|
|
|
http_socket.setSendTimeout(settings.send_timeout);
|
|
|
|
|
Poco::Net::HTTPServer http_server(
|
|
|
|
|
new HTTPRequestHandlerFactory<HTTPHandler>(*this, "HTTPHandler-factory"),
|
|
|
|
|
server_pool,
|
|
|
|
|
http_socket,
|
|
|
|
|
http_params);
|
|
|
|
|
|
|
|
|
|
/// TCP
|
|
|
|
|
Poco::Net::ServerSocket tcp_socket(Poco::Net::SocketAddress("[::]:" + config.getString("tcp_port")));
|
|
|
|
|
tcp_socket.setReceiveTimeout(settings.receive_timeout);
|
|
|
|
|
tcp_socket.setSendTimeout(settings.send_timeout);
|
|
|
|
|
Poco::Net::TCPServer tcp_server(
|
|
|
|
|
new TCPConnectionFactory(*this),
|
|
|
|
|
server_pool,
|
|
|
|
|
tcp_socket,
|
|
|
|
|
new Poco::Net::TCPServerParams);
|
|
|
|
|
|
|
|
|
|
/// OLAP HTTP
|
|
|
|
|
Poco::SharedPtr<Poco::Net::HTTPServer> olap_http_server;
|
|
|
|
|
if (use_olap_server)
|
|
|
|
|
{
|
|
|
|
|
olap_parser = new OLAP::QueryParser();
|
|
|
|
|
olap_converter = new OLAP::QueryConverter(config);
|
|
|
|
|
|
|
|
|
|
Poco::Net::HTTPServerParams * olap_http_params = new Poco::Net::HTTPServerParams;
|
|
|
|
|
olap_http_params->setTimeout(settings.receive_timeout);
|
|
|
|
|
olap_http_params->setKeepAliveTimeout(keep_alive_timeout);
|
|
|
|
|
|
|
|
|
|
Poco::Net::ServerSocket olap_http_socket(Poco::Net::SocketAddress("[::]:" + config.getString("olap_http_port")));
|
|
|
|
|
olap_http_socket.setReceiveTimeout(settings.receive_timeout);
|
|
|
|
|
olap_http_socket.setSendTimeout(settings.send_timeout);
|
|
|
|
|
olap_http_server = new Poco::Net::HTTPServer(
|
|
|
|
|
new HTTPRequestHandlerFactory<OLAPHTTPHandler>(*this, "OLAPHTTPHandler-factory"),
|
|
|
|
|
server_pool,
|
|
|
|
|
olap_http_socket,
|
|
|
|
|
olap_http_params);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
http_server.start();
|
|
|
|
|
tcp_server.start();
|
|
|
|
|
if (use_olap_server)
|
|
|
|
|
olap_http_server->start();
|
|
|
|
|
|
|
|
|
|
LOG_INFO(log, "Ready for connections.");
|
2012-12-14 11:21:07 +00:00
|
|
|
|
|
2013-09-14 05:14:22 +00:00
|
|
|
|
waitForTerminationRequest();
|
2012-12-14 11:21:07 +00:00
|
|
|
|
|
2013-09-14 05:14:22 +00:00
|
|
|
|
LOG_DEBUG(log, "Received termination signal. Waiting for current connections to close.");
|
|
|
|
|
is_cancelled = true;
|
2012-12-27 17:54:16 +00:00
|
|
|
|
|
2013-09-14 05:14:22 +00:00
|
|
|
|
http_server.stop();
|
|
|
|
|
tcp_server.stop();
|
|
|
|
|
if (use_olap_server)
|
|
|
|
|
olap_http_server->stop();
|
2012-12-14 11:21:07 +00:00
|
|
|
|
}
|
2012-03-09 15:46:52 +00:00
|
|
|
|
|
2013-09-14 05:14:22 +00:00
|
|
|
|
LOG_DEBUG(log, "Closed all connections.");
|
2012-03-09 15:46:52 +00:00
|
|
|
|
|
2013-09-14 05:14:22 +00:00
|
|
|
|
/** Явно уничтожаем контекст - это удобнее, чем в деструкторе Server-а, так как ещё доступен логгер.
|
|
|
|
|
* В этот момент никто больше не должен владеть shared-частью контекста.
|
|
|
|
|
*/
|
|
|
|
|
global_context = NULL;
|
2012-03-09 15:46:52 +00:00
|
|
|
|
|
2013-09-14 05:14:22 +00:00
|
|
|
|
LOG_DEBUG(log, "Destroyed global context.");
|
2012-03-09 15:46:52 +00:00
|
|
|
|
|
2012-03-09 03:06:09 +00:00
|
|
|
|
return Application::EXIT_OK;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
YANDEX_APP_SERVER_MAIN(DB::Server);
|