aboutsummaryrefslogtreecommitdiffstats
path: root/contrib/clickhouse/src/QueryPipeline/RemoteQueryExecutorReadContext.h
blob: adfc0c5eacfcfeccff93488d232c5c2707dad546 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
#pragma once

#if defined(OS_LINUX)

#include <mutex>
#include <atomic>
#include <Common/Fiber.h>
#include <Common/FiberStack.h>
#include <Common/TimerDescriptor.h>
#include <Common/Epoll.h>
#include <Common/AsyncTaskExecutor.h>
#include <Client/Connection.h>
#include <Client/IConnections.h>
#include <Poco/Timespan.h>

namespace Poco::Net
{
class Socket;
}

namespace DB
{

class MultiplexedConnections;
class RemoteQueryExecutor;

class RemoteQueryExecutorReadContext : public AsyncTaskExecutor
{
public:
    explicit RemoteQueryExecutorReadContext(RemoteQueryExecutor & executor_, bool suspend_when_query_sent_ = false);

    ~RemoteQueryExecutorReadContext() override;

    bool isInProgress() const { return is_in_progress.load(std::memory_order_relaxed); }

    bool isCancelled() const { return AsyncTaskExecutor::isCancelled() || is_pipe_alarmed; }

    bool isQuerySent() const { return is_query_sent;  }

    int getFileDescriptor() const { return epoll.getFileDescriptor(); }

    Packet getPacket() { return std::move(packet); }

private:
    bool checkTimeout(bool blocking = false);

    bool checkBeforeTaskResume() override;
    void afterTaskResume() override {}

    void processAsyncEvent(int fd, Poco::Timespan socket_timeout, AsyncEventTimeoutType type, const std::string & description, uint32_t events) override;
    void clearAsyncEvent() override;

    void cancelBefore() override;

    struct Task : public AsyncTask
    {
        Task(RemoteQueryExecutorReadContext & read_context_) : read_context(read_context_) {}

        RemoteQueryExecutorReadContext & read_context;

        void run(AsyncCallback async_callback, SuspendCallback suspend_callback) override;
    };

    std::atomic_bool is_in_progress = false;
    Packet packet;

    RemoteQueryExecutor & executor;

    /// Here we have three descriptors we are going to wait:
    /// * connection_fd is a descriptor of connection. It may be changed in case of reading from several replicas.
    /// * timer is a timerfd descriptor to manually check socket timeout
    /// * pipe_fd is a pipe we use to cancel query and socket polling by executor.
    /// We put those descriptors into our own epoll which is used by external executor.
    TimerDescriptor timer{CLOCK_MONOTONIC, 0};
    Poco::Timespan timeout;
    AsyncEventTimeoutType timeout_type;
    std::atomic_bool is_timer_alarmed = false;
    int connection_fd = -1;
    int pipe_fd[2] = { -1, -1 };
    std::atomic_bool is_pipe_alarmed = false;

    Epoll epoll;

    std::string connection_fd_description;
    bool suspend_when_query_sent = false;
    bool is_query_sent = false;
};

}

#else

namespace DB
{
class RemoteQueryExecutorReadContext
{
public:
    void cancel() {}
    void setTimer() {}
};

}
#endif