ClickHouse源码阅读笔记(一)之主要流程

ClickHouse源码阅读笔记(一)之主要流程

入口main函数在dbms/programs/main.cpp

int main(int argc_, char ** argv_)
{
...

/// Print a basic help if nothing was matched
MainFunc main_func = printHelp;//这里根据启动时传入的参数来确定后面执行哪个func,对于server来说,对应的函数为mainEntryClickHouseServer

for (auto & application : clickhouse_applications)
{
if (isClickhouseApp(application.first, argv))
{
main_func = application.second;
break;
}
}

return main_func(static_cast<int>(argv.size()), argv.data());//对于server,这里调用mainEntryClickHouseServer后,转到dbms/programs/server/server.cpp
}

在dbms/programs/server/server.cpp中,提供三类接口,按照源码的描述,说明如下:

/** Server provides three interfaces:
* 1. HTTP - simple interface for any applications.

适用于任何应用程序的HTTP接口。
* 2. TCP - interface for native clickhouse-client and for server to server internal communications.

用于本地client和server之间通信的TCP接口。
* More rich and efficient, but less compatible

丰富高效,但兼容性不好
* - data is transferred by columns;

数据按列传输
* - data is transferred compressed;

数据压缩后传输
* Allows to get more information in response.

允许在响应消息中获取更多信息
* 3. Interserver HTTP - for replication.

用于复制的内部HTTP。
*/

dbms/programs/server/server.cpp中的main函数会解析参数配置,初始化server,启动服务监听端口。

int mainEntryClickHouseServer(int argc, char ** argv)
{
DB::Server app;
try
{
return app.run(argc, argv);//这里调用run。
}
catch (...)
{
std::cerr << DB::getCurrentExceptionMessage(true) << "\n";
auto code = DB::getCurrentExceptionCode();
return code ? code : 1;
}
}

clickhouse使用poco这个网络库来处理网络请求,每个client连接的处理逻辑在dbms/programs/server//TCPHandler.cpp的run()方法中。

void TCPHandler::run()
{
try
{
runImpl();//这里调用 runImpl函数。

LOG_INFO(log, "Done processing connection.");
}
catch (Poco::Exception & e)
{
/// Timeout - not an error.
if (!strcmp(e.what(), "Timeout"))
{
LOG_DEBUG(log, "Poco::Exception. Code: " << ErrorCodes::POCO_EXCEPTION << ", e.code() = " << e.code()
<< ", e.displayText() = " << e.displayText() << ", e.what() = " << e.what());
}
else
throw;
}
}

在TCPHandler::runImpl()函数中,除去握手,初始化上下文,异常处理等代码,主要逻辑如下:

void TCPHandler::runImpl()
{

receivePacket();//接收请求

executeQuery(state.query, *query_context, false, state.stage, may_have_embedded_data);//处理请求

/// Does the request require receive data from client?
if (state.need_receive_data_for_insert)
processInsertQuery(connection_settings);//负责将结果返回给客户端
else if (state.need_receive_data_for_input)
{
/// It is special case for input(), all works for reading data from client will be done in callbacks.
/// state.io.in is NullAndDoCopyBlockInputStream so read it once.
state.io.in->read();
state.io.onFinish();
}
else if (state.io.pipeline.initialized())
processOrdinaryQueryWithProcessors(query_context->getSettingsRef().max_threads);//负责将结果返回给客户端
else
processOrdinaryQuery();//负责将结果返回给客户端

}

接下来,我们继续看executeQuery处理请求的逻辑,在dbms/src/Interpreters/executeQuery.cpp中,主要逻辑如下:

BlockIO executeQuery(
const String & query,
Context & context,
bool internal,
QueryProcessingStage::Enum stage,
bool may_have_embedded_data,
bool allow_processors)
{

std::tie(ast, streams) = executeQueryImpl(query.data(), query.data() + query.size(), context,
internal, stage, !may_have_embedded_data, nullptr, allow_processors);//这里调用executeQueryImpl

}

接下来再看executeQueryImpl的主要处理逻辑:

static std::tuple<ASTPtr, BlockIO> executeQueryImpl(
const char * begin,
const char * end,
Context & context,
bool internal,
QueryProcessingStage::Enum stage,
bool has_query_tail,
ReadBuffer * istr,
bool allow_processors)
{

ast = parseQuery(parser, begin, end, "", max_query_size, settings.max_parser_depth);//解析查询语句

if (use_processors)//使用pipeline
pipeline = interpreter->executeWithProcessors();
else//不使用pipiline
res = interpreter->execute();//根据interpreter的类型来调用对应类型的execute函数执行

}

下一篇文章会介绍interpreter,未完待续。。。

猜你喜欢

转载自www.cnblogs.com/snake-fly/p/12689092.html