ClickHouse源码阅读(0000 1011) —— ClickHouse Client端如何接收Server端发送回来的数据

本文深入解析ClickHouse客户端与服务器的交互过程,重点介绍processOrdinaryQuery()方法,包括SQL发送、外部表发送及结果接收流程。详细分析了receiveResult()方法内部的双重循环机制,阐述了数据包接收、处理及取消查询的实现细节。

这篇文章中提到:client端对于非INSERT语句,processOrdinaryQuery()方法的代码如下:

        /// Process the query that doesn't require transferring data blocks to the server.
        void processOrdinaryQuery()
        {
            //客户端sendQuery()后, SQL及其他信息封装成数据包发送到服务端, //数据包被发送到对应的端口, 由服务端*Handler进行具体的处理, run() -> runImpl() -> receivePacket()
            connection->sendQuery(query, query_id, QueryProcessingStage::Complete, &context.getSettingsRef(), nullptr,
                                  true);
            sendExternalTables();
            receiveResult();//客户端接收服务端的处理结果, 也是以数据包的形式发送过来的
        }

主要包含三个步骤:1)将SQL从Client端发送到Server端;2)sendExternalTables();3)Client端等待Server端发送回来的结果。

receiveResult()方法如下:

        /// Receives and processes packets coming from server. Also checks if query execution should be cancelled.
        //接收处理服务器Server端发送的数据包, 同时检查SQL执行是否被取消
        void receiveResult()
        {
            InterruptListener interrupt_listener;
            bool cancelled = false;

            // TODO: get the poll_interval from commandline.
            const auto receive_timeout = connection->getTimeouts().receive_timeout;
            constexpr size_t default_poll_interval = 1000000; /// in microseconds
            constexpr size_t min_poll_interval = 5000; /// in microseconds
            const size_t poll_interval
                    = std::max(min_poll_interval,
                               std::min<size_t>(receive_timeout.totalMicroseconds(), default_poll_interval));

            while (true)
            {
                Stopwatch receive_watch(CLOCK_MONOTONIC_COARSE);

                while (true)
                {
                    /// Has the Ctrl+C been pressed and thus the query should be cancelled?
                    /// If this is the case, inform the server about it
                    /// and receive
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值