ClickHouse/src/DataStreams/RemoteQueryExecutorReadContext.h

82 lines
1.9 KiB
C++
Raw Normal View History

2020-12-04 11:00:32 +00:00
#pragma once
2020-12-14 16:16:08 +00:00
#if defined(OS_LINUX)
#include <mutex>
#include <atomic>
2020-12-10 09:30:43 +00:00
#include <Common/Fiber.h>
#include <Common/FiberStack.h>
#include <Common/TimerDescriptor.h>
2021-01-19 19:21:06 +00:00
#include <Common/Epoll.h>
#include <Client/Connection.h>
2021-01-19 19:21:06 +00:00
#include <Client/IConnections.h>
#include <Poco/Timespan.h>
2020-12-04 11:00:32 +00:00
namespace Poco::Net
2020-12-04 11:00:32 +00:00
{
class Socket;
}
2020-12-04 11:00:32 +00:00
namespace DB
2020-12-04 11:00:32 +00:00
{
class MultiplexedConnections;
2020-12-04 11:00:32 +00:00
class RemoteQueryExecutorReadContext
{
public:
std::atomic_bool is_read_in_progress = false;
2020-12-04 11:00:32 +00:00
Packet packet;
std::exception_ptr exception;
2020-12-15 10:08:13 +00:00
FiberStack stack;
2020-12-04 11:00:32 +00:00
boost::context::fiber fiber;
2020-12-22 08:55:21 +00:00
/// This mutex for fiber is needed because fiber could be destroyed in cancel method from another thread.
2020-12-14 16:16:08 +00:00
std::mutex fiber_lock;
2020-12-04 11:00:32 +00:00
Poco::Timespan receive_timeout;
2021-01-19 19:21:06 +00:00
IConnections & connections;
Poco::Net::Socket * last_used_socket = nullptr;
2020-12-04 11:00:32 +00:00
2020-12-22 08:55:21 +00:00
/// Here we have three descriptors we are going to wait:
2021-01-19 19:21:06 +00:00
/// * connection_fd is a descriptor of connection. It may be changed in case of reading from several replicas.
2020-12-22 08:55:21 +00:00
/// * 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.
2021-01-19 19:21:06 +00:00
/// We put those descriptors into our own epoll which is used by external executor.
2020-12-04 11:00:32 +00:00
TimerDescriptor timer{CLOCK_MONOTONIC, 0};
bool is_timer_alarmed = false;
2021-01-19 19:21:06 +00:00
int connection_fd = -1;
int pipe_fd[2] = { -1, -1 };
2020-12-04 11:00:32 +00:00
2021-01-19 19:21:06 +00:00
Epoll epoll;
std::string connection_fd_description;
explicit RemoteQueryExecutorReadContext(IConnections & connections_);
~RemoteQueryExecutorReadContext();
2020-12-04 11:00:32 +00:00
bool checkTimeout(bool blocking = false);
bool checkTimeoutImpl(bool blocking);
2020-12-04 11:00:32 +00:00
2021-01-19 19:21:06 +00:00
void setConnectionFD(int fd, const Poco::Timespan & timeout = 0, const std::string & fd_description = "");
void setTimer() const;
2020-12-10 12:20:18 +00:00
bool resumeRoutine();
void cancel();
2020-12-04 11:00:32 +00:00
};
2020-12-14 16:16:08 +00:00
}
2020-12-14 16:16:08 +00:00
#else
2020-12-14 16:16:08 +00:00
namespace DB
{
class RemoteQueryExecutorReadContext
{
2020-12-15 10:08:13 +00:00
public:
void cancel() {}
2020-12-14 16:16:08 +00:00
};
2020-12-04 11:00:32 +00:00
}
2020-12-14 16:16:08 +00:00
#endif