ClickHouse/dbms/src/Server/Server.cpp

157 lines
5.8 KiB
C++
Raw Normal View History

2012-03-09 03:06:09 +00:00
#include <Poco/Net/HTTPServerRequest.h>
#include <Yandex/ApplicationServerExt.h>
#include <DB/Functions/FunctionsLibrary.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>
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"
#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;
}
};
Poco::Net::HTTPRequestHandler * HTTPRequestHandlerFactory::createRequestHandler(
const Poco::Net::HTTPServerRequest & request)
{
LOG_TRACE(log, "HTTP Request. "
<< "Method: " << request.getMethod()
<< ", Address: " << request.clientAddress().toString()
<< ", User-Agent: " << (request.has("User-Agent") ? request.get("User-Agent") : "none"));
2012-11-19 04:16:00 +00:00
if (request.getURI().find('?') != std::string::npos || request.getMethod() == Poco::Net::HTTPRequest::HTTP_POST)
2012-03-09 15:46:52 +00:00
return new HTTPHandler(server);
2012-03-09 03:06:09 +00:00
else if (request.getMethod() == Poco::Net::HTTPRequest::HTTP_GET)
return new PingRequestHandler();
else
return 0;
}
2012-03-09 15:46:52 +00:00
Poco::Net::TCPServerConnection * TCPConnectionFactory::createConnection(const Poco::Net::StreamSocket & socket)
{
LOG_TRACE(log, "TCP Request. " << "Address: " << socket.address().toString());
return new TCPHandler(server, socket);
}
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.");
2012-03-09 03:06:09 +00:00
Yandex::DateLUTSingleton::instance();
2012-06-24 23:25:07 +00:00
LOG_TRACE(log, "Initialized DateLUT.");
2012-03-09 03:56:12 +00:00
/** Контекст содержит всё, что влияет на обработку запроса:
* настройки, набор функций, типов данных, агрегатных функций, баз данных...
*/
2012-08-02 17:33:31 +00:00
global_context.setGlobalContext(global_context);
global_context.setPath(config.getString("path"));
2012-03-09 03:06:09 +00:00
2012-10-22 20:08:28 +00:00
/// Загружаем настройки.
2012-08-02 17:33:31 +00:00
Settings settings;
2012-10-22 20:08:28 +00:00
2012-08-02 17:33:31 +00:00
settings.asynchronous = config.getBool("asynchronous", settings.asynchronous);
settings.max_block_size = config.getInt("max_block_size", settings.max_block_size);
settings.max_query_size = config.getInt("max_query_size", settings.max_query_size);
settings.max_threads = config.getInt("max_threads", settings.max_threads);
settings.interactive_delay = config.getInt("interactive_delay", settings.interactive_delay);
settings.connect_timeout = Poco::Timespan(config.getInt("connect_timeout", DBMS_DEFAULT_CONNECT_TIMEOUT_SEC), 0);
settings.connect_timeout_with_failover_ms
= Poco::Timespan(config.getInt("connect_timeout_with_failover_ms", DBMS_DEFAULT_CONNECT_TIMEOUT_WITH_FAILOVER_MS) * 1000);
2012-08-02 17:33:31 +00:00
settings.receive_timeout = Poco::Timespan(config.getInt("receive_timeout", DBMS_DEFAULT_RECEIVE_TIMEOUT_SEC), 0);
settings.send_timeout = Poco::Timespan(config.getInt("send_timeout", DBMS_DEFAULT_SEND_TIMEOUT_SEC), 0);
2012-08-16 18:02:15 +00:00
settings.poll_interval = config.getInt("poll_interval", settings.poll_interval);
settings.max_distributed_connections = config.getInt("max_distributed_connections", settings.max_distributed_connections);
2012-10-22 20:01:37 +00:00
settings.distributed_connections_pool_size =
config.getInt("distributed_connections_pool_size", settings.distributed_connections_pool_size);
settings.connections_with_failover_max_tries =
config.getInt("connections_with_failover_max_tries", settings.connections_with_failover_max_tries);
2012-08-02 17:33:31 +00:00
global_context.setSettings(settings);
2012-10-22 20:08:28 +00:00
LOG_INFO(log, "Loading metadata.");
loadMetadata(global_context);
LOG_DEBUG(log, "Loaded metadata.");
/// Создаём системные таблицы.
global_context.addDatabase("system");
2012-03-09 03:06:09 +00:00
2012-10-22 20:08:28 +00:00
global_context.addTable("system", "one", new StorageSystemOne("one"));
global_context.addTable("system", "numbers", new StorageSystemNumbers("numbers"));
global_context.addTable("system", "tables", new StorageSystemTables("tables", global_context));
global_context.addTable("system", "databases", new StorageSystemDatabases("databases", global_context));
global_context.setCurrentDatabase(config.getString("default_database", "default"));
2012-03-09 15:46:52 +00:00
Poco::Net::ServerSocket http_socket(Poco::Net::SocketAddress("[::]:" + config.getString("http_port")));
Poco::Net::ServerSocket tcp_socket(Poco::Net::SocketAddress("[::]:" + config.getString("tcp_port")));
2012-03-09 03:06:09 +00:00
2012-08-02 17:33:31 +00:00
http_socket.setReceiveTimeout(settings.receive_timeout);
http_socket.setSendTimeout(settings.send_timeout);
tcp_socket.setReceiveTimeout(settings.receive_timeout);
tcp_socket.setSendTimeout(settings.send_timeout);
2012-07-26 19:42:20 +00:00
2012-05-09 15:50:42 +00:00
Poco::ThreadPool server_pool(2, config.getInt("max_connections", 128));
2012-03-09 03:06:09 +00:00
2012-07-26 20:16:57 +00:00
Poco::Net::HTTPServerParams * http_params = new Poco::Net::HTTPServerParams;
2012-08-02 17:33:31 +00:00
http_params->setTimeout(settings.receive_timeout);
2012-07-26 20:16:57 +00:00
2012-03-09 15:46:52 +00:00
Poco::Net::HTTPServer http_server(
2012-03-09 03:06:09 +00:00
new HTTPRequestHandlerFactory(*this),
server_pool,
2012-03-09 15:46:52 +00:00
http_socket,
2012-07-26 20:16:57 +00:00
http_params);
2012-03-09 03:06:09 +00:00
2012-03-09 15:46:52 +00:00
Poco::Net::TCPServer tcp_server(
new TCPConnectionFactory(*this),
server_pool,
tcp_socket,
new Poco::Net::TCPServerParams);
http_server.start();
tcp_server.start();
2012-06-24 23:25:07 +00:00
LOG_INFO(log, "Ready for connections.");
2012-03-09 03:06:09 +00:00
waitForTerminationRequest();
2012-08-16 18:36:40 +00:00
LOG_DEBUG(log, "Received termination signal.");
is_cancelled = true;
2012-03-09 15:46:52 +00:00
http_server.stop();
tcp_server.stop();
2012-03-09 03:06:09 +00:00
return Application::EXIT_OK;
}
}
YANDEX_APP_SERVER_MAIN(DB::Server);