2021-03-29 08:24:56 +00:00
|
|
|
#include <Server/KeeperTCPHandler.h>
|
2021-02-01 13:18:17 +00:00
|
|
|
|
|
|
|
#if USE_NURAFT
|
|
|
|
|
2020-11-11 13:07:06 +00:00
|
|
|
#include <Common/ZooKeeper/ZooKeeperIO.h>
|
2020-10-30 14:16:47 +00:00
|
|
|
#include <Core/Types.h>
|
|
|
|
#include <IO/WriteBufferFromPocoSocket.h>
|
|
|
|
#include <IO/ReadBufferFromPocoSocket.h>
|
|
|
|
#include <Poco/Net/NetException.h>
|
|
|
|
#include <Common/CurrentThread.h>
|
|
|
|
#include <Common/Stopwatch.h>
|
|
|
|
#include <Common/NetException.h>
|
|
|
|
#include <Common/setThreadName.h>
|
2021-10-02 07:13:14 +00:00
|
|
|
#include <base/logger_useful.h>
|
2020-11-03 14:49:30 +00:00
|
|
|
#include <chrono>
|
2020-11-25 08:18:15 +00:00
|
|
|
#include <Common/PipeFDs.h>
|
2020-11-25 13:19:09 +00:00
|
|
|
#include <Poco/Util/AbstractConfiguration.h>
|
2020-12-18 09:13:50 +00:00
|
|
|
#include <IO/ReadBufferFromFileDescriptor.h>
|
2021-01-20 12:11:26 +00:00
|
|
|
#include <queue>
|
|
|
|
#include <mutex>
|
2020-11-25 13:19:09 +00:00
|
|
|
|
2020-11-25 12:36:46 +00:00
|
|
|
#ifdef POCO_HAVE_FD_EPOLL
|
|
|
|
#include <sys/epoll.h>
|
|
|
|
#else
|
|
|
|
#include <poll.h>
|
|
|
|
#endif
|
2020-11-24 14:02:55 +00:00
|
|
|
|
2021-02-01 13:18:17 +00:00
|
|
|
|
2020-10-30 14:16:47 +00:00
|
|
|
namespace DB
|
|
|
|
{
|
|
|
|
|
2021-02-01 13:18:17 +00:00
|
|
|
|
2020-10-30 14:16:47 +00:00
|
|
|
namespace ErrorCodes
|
|
|
|
{
|
2020-11-24 14:02:55 +00:00
|
|
|
extern const int SYSTEM_ERROR;
|
2021-01-22 10:14:53 +00:00
|
|
|
extern const int LOGICAL_ERROR;
|
2020-11-25 13:19:09 +00:00
|
|
|
extern const int UNEXPECTED_PACKET_FROM_CLIENT;
|
2021-01-25 12:29:12 +00:00
|
|
|
extern const int TIMEOUT_EXCEEDED;
|
2020-11-24 14:02:55 +00:00
|
|
|
}
|
|
|
|
|
2020-12-18 09:13:50 +00:00
|
|
|
struct PollResult
|
|
|
|
{
|
2021-02-24 11:54:14 +00:00
|
|
|
size_t responses_count{0};
|
2021-01-22 10:02:48 +00:00
|
|
|
bool has_requests{false};
|
|
|
|
bool error{false};
|
2020-12-18 09:13:50 +00:00
|
|
|
};
|
2020-11-26 08:59:23 +00:00
|
|
|
|
2020-11-24 14:02:55 +00:00
|
|
|
struct SocketInterruptablePollWrapper
|
|
|
|
{
|
|
|
|
int sockfd;
|
2020-11-25 08:18:15 +00:00
|
|
|
PipeFDs pipe;
|
2020-12-18 09:13:50 +00:00
|
|
|
ReadBufferFromFileDescriptor response_in;
|
2020-11-25 08:56:40 +00:00
|
|
|
|
2020-11-25 20:42:23 +00:00
|
|
|
#if defined(POCO_HAVE_FD_EPOLL)
|
|
|
|
int epollfd;
|
|
|
|
epoll_event socket_event{};
|
|
|
|
epoll_event pipe_event{};
|
|
|
|
#endif
|
|
|
|
|
2020-11-24 14:02:55 +00:00
|
|
|
using InterruptCallback = std::function<void()>;
|
|
|
|
|
2020-11-25 08:18:15 +00:00
|
|
|
explicit SocketInterruptablePollWrapper(const Poco::Net::StreamSocket & poco_socket_)
|
2020-11-24 14:02:55 +00:00
|
|
|
: sockfd(poco_socket_.impl()->sockfd())
|
2020-12-18 09:13:50 +00:00
|
|
|
, response_in(pipe.fds_rw[0])
|
2020-11-24 14:02:55 +00:00
|
|
|
{
|
2020-11-27 10:12:11 +00:00
|
|
|
pipe.setNonBlockingReadWrite();
|
2020-11-25 12:36:46 +00:00
|
|
|
|
|
|
|
#if defined(POCO_HAVE_FD_EPOLL)
|
2020-11-25 20:42:23 +00:00
|
|
|
epollfd = epoll_create(2);
|
2020-11-24 14:02:55 +00:00
|
|
|
if (epollfd < 0)
|
|
|
|
throwFromErrno("Cannot epoll_create", ErrorCodes::SYSTEM_ERROR);
|
|
|
|
|
2021-02-19 12:56:51 +00:00
|
|
|
socket_event.events = EPOLLIN | EPOLLERR | EPOLLPRI;
|
2020-11-24 14:02:55 +00:00
|
|
|
socket_event.data.fd = sockfd;
|
|
|
|
if (epoll_ctl(epollfd, EPOLL_CTL_ADD, sockfd, &socket_event) < 0)
|
|
|
|
{
|
|
|
|
::close(epollfd);
|
|
|
|
throwFromErrno("Cannot insert socket into epoll queue", ErrorCodes::SYSTEM_ERROR);
|
|
|
|
}
|
2021-02-19 12:56:51 +00:00
|
|
|
pipe_event.events = EPOLLIN | EPOLLERR | EPOLLPRI;
|
2020-11-25 08:18:15 +00:00
|
|
|
pipe_event.data.fd = pipe.fds_rw[0];
|
|
|
|
if (epoll_ctl(epollfd, EPOLL_CTL_ADD, pipe.fds_rw[0], &pipe_event) < 0)
|
2020-11-24 14:02:55 +00:00
|
|
|
{
|
|
|
|
::close(epollfd);
|
|
|
|
throwFromErrno("Cannot insert socket into epoll queue", ErrorCodes::SYSTEM_ERROR);
|
|
|
|
}
|
2020-11-25 20:42:23 +00:00
|
|
|
#endif
|
|
|
|
}
|
2020-11-24 14:02:55 +00:00
|
|
|
|
2020-11-26 08:59:23 +00:00
|
|
|
int getResponseFD() const
|
2020-11-25 20:42:23 +00:00
|
|
|
{
|
|
|
|
return pipe.fds_rw[1];
|
|
|
|
}
|
|
|
|
|
2021-02-19 11:06:17 +00:00
|
|
|
PollResult poll(Poco::Timespan remaining_time, const std::shared_ptr<ReadBufferFromPocoSocket> & in)
|
2020-11-25 20:42:23 +00:00
|
|
|
{
|
2021-02-24 11:54:14 +00:00
|
|
|
|
|
|
|
bool socket_ready = false;
|
|
|
|
bool fd_ready = false;
|
2021-02-19 11:06:17 +00:00
|
|
|
|
|
|
|
if (in->available() != 0)
|
2021-02-24 11:54:14 +00:00
|
|
|
socket_ready = true;
|
2021-02-19 11:06:17 +00:00
|
|
|
|
2021-02-24 11:54:14 +00:00
|
|
|
if (response_in.available() != 0)
|
|
|
|
fd_ready = true;
|
2021-02-19 11:06:17 +00:00
|
|
|
|
2021-02-24 11:54:14 +00:00
|
|
|
int rc = 0;
|
|
|
|
if (!fd_ready)
|
2020-11-24 14:02:55 +00:00
|
|
|
{
|
2021-02-24 11:54:14 +00:00
|
|
|
#if defined(POCO_HAVE_FD_EPOLL)
|
|
|
|
epoll_event evout[2];
|
|
|
|
evout[0].data.fd = evout[1].data.fd = -1;
|
|
|
|
do
|
2020-11-24 14:02:55 +00:00
|
|
|
{
|
2021-02-24 11:54:14 +00:00
|
|
|
Poco::Timestamp start;
|
|
|
|
rc = epoll_wait(epollfd, evout, 2, remaining_time.totalMilliseconds());
|
|
|
|
if (rc < 0 && errno == EINTR)
|
|
|
|
{
|
|
|
|
Poco::Timestamp end;
|
|
|
|
Poco::Timespan waited = end - start;
|
|
|
|
if (waited < remaining_time)
|
|
|
|
remaining_time -= waited;
|
|
|
|
else
|
|
|
|
remaining_time = 0;
|
|
|
|
}
|
2020-11-24 14:02:55 +00:00
|
|
|
}
|
2021-02-24 11:54:14 +00:00
|
|
|
while (rc < 0 && errno == EINTR);
|
2020-11-24 14:02:55 +00:00
|
|
|
|
2021-02-24 11:54:14 +00:00
|
|
|
for (int i = 0; i < rc; ++i)
|
|
|
|
{
|
|
|
|
if (evout[i].data.fd == sockfd)
|
|
|
|
socket_ready = true;
|
|
|
|
if (evout[i].data.fd == pipe.fds_rw[0])
|
|
|
|
fd_ready = true;
|
|
|
|
}
|
2020-11-25 12:36:46 +00:00
|
|
|
#else
|
2021-02-24 11:54:14 +00:00
|
|
|
pollfd poll_buf[2];
|
|
|
|
poll_buf[0].fd = sockfd;
|
|
|
|
poll_buf[0].events = POLLIN;
|
|
|
|
poll_buf[1].fd = pipe.fds_rw[0];
|
|
|
|
poll_buf[1].events = POLLIN;
|
|
|
|
|
|
|
|
do
|
2020-11-25 12:36:46 +00:00
|
|
|
{
|
2021-02-24 11:54:14 +00:00
|
|
|
Poco::Timestamp start;
|
|
|
|
rc = ::poll(poll_buf, 2, remaining_time.totalMilliseconds());
|
|
|
|
if (rc < 0 && errno == POCO_EINTR)
|
|
|
|
{
|
|
|
|
Poco::Timestamp end;
|
|
|
|
Poco::Timespan waited = end - start;
|
|
|
|
if (waited < remaining_time)
|
|
|
|
remaining_time -= waited;
|
|
|
|
else
|
|
|
|
remaining_time = 0;
|
|
|
|
}
|
2020-11-25 12:36:46 +00:00
|
|
|
}
|
2021-02-24 11:54:14 +00:00
|
|
|
while (rc < 0 && errno == POCO_EINTR);
|
2021-02-19 12:56:51 +00:00
|
|
|
|
2021-02-24 11:54:14 +00:00
|
|
|
if (rc >= 1 && poll_buf[0].revents & POLLIN)
|
|
|
|
socket_ready = true;
|
2021-03-09 09:42:27 +00:00
|
|
|
if (rc >= 1 && poll_buf[1].revents & POLLIN)
|
2021-02-24 11:54:14 +00:00
|
|
|
fd_ready = true;
|
2020-11-25 12:36:46 +00:00
|
|
|
#endif
|
2020-11-26 08:59:23 +00:00
|
|
|
}
|
2021-02-24 11:54:14 +00:00
|
|
|
|
|
|
|
PollResult result{};
|
|
|
|
result.has_requests = socket_ready;
|
|
|
|
if (fd_ready)
|
2020-11-26 08:59:23 +00:00
|
|
|
{
|
2021-02-24 11:54:14 +00:00
|
|
|
UInt8 dummy;
|
|
|
|
readIntBinary(dummy, response_in);
|
|
|
|
result.responses_count = 1;
|
|
|
|
auto available = response_in.available();
|
|
|
|
response_in.ignore(available);
|
|
|
|
result.responses_count += available;
|
2020-11-26 08:59:23 +00:00
|
|
|
}
|
2021-02-24 11:54:14 +00:00
|
|
|
|
|
|
|
if (rc < 0)
|
|
|
|
result.error = true;
|
|
|
|
|
2020-11-26 08:59:23 +00:00
|
|
|
return result;
|
2020-11-24 14:02:55 +00:00
|
|
|
}
|
2020-11-25 20:42:23 +00:00
|
|
|
|
|
|
|
#if defined(POCO_HAVE_FD_EPOLL)
|
|
|
|
~SocketInterruptablePollWrapper()
|
|
|
|
{
|
|
|
|
::close(epollfd);
|
|
|
|
}
|
|
|
|
#endif
|
2020-11-24 14:02:55 +00:00
|
|
|
};
|
|
|
|
|
2021-03-29 08:24:56 +00:00
|
|
|
KeeperTCPHandler::KeeperTCPHandler(IServer & server_, const Poco::Net::StreamSocket & socket_)
|
2020-11-24 14:02:55 +00:00
|
|
|
: Poco::Net::TCPServerConnection(socket_)
|
|
|
|
, server(server_)
|
2021-09-02 09:20:11 +00:00
|
|
|
, log(&Poco::Logger::get("KeeperTCPHandler"))
|
2021-04-10 23:33:54 +00:00
|
|
|
, global_context(Context::createCopy(server.context()))
|
2021-08-24 12:30:31 +00:00
|
|
|
, keeper_dispatcher(global_context->getKeeperDispatcher())
|
2021-07-29 18:24:32 +00:00
|
|
|
, operation_timeout(0, global_context->getConfigRef().getUInt("keeper_server.operation_timeout_ms", Coordination::DEFAULT_OPERATION_TIMEOUT_MS) * 1000)
|
|
|
|
, session_timeout(0, global_context->getConfigRef().getUInt("keeper_server.session_timeout_ms", Coordination::DEFAULT_SESSION_TIMEOUT_MS) * 1000)
|
2020-11-24 14:02:55 +00:00
|
|
|
, poll_wrapper(std::make_unique<SocketInterruptablePollWrapper>(socket_))
|
2021-10-08 08:48:08 +00:00
|
|
|
, responses(std::make_unique<ThreadSafeResponseQueue>(std::numeric_limits<size_t>::max()))
|
2020-11-24 14:02:55 +00:00
|
|
|
{
|
2020-10-30 14:16:47 +00:00
|
|
|
}
|
|
|
|
|
2021-03-29 08:24:56 +00:00
|
|
|
void KeeperTCPHandler::sendHandshake(bool has_leader)
|
2020-10-30 14:16:47 +00:00
|
|
|
{
|
2020-11-11 13:07:06 +00:00
|
|
|
Coordination::write(Coordination::SERVER_HANDSHAKE_LENGTH, *out);
|
2021-02-01 07:51:10 +00:00
|
|
|
if (has_leader)
|
|
|
|
Coordination::write(Coordination::ZOOKEEPER_PROTOCOL_VERSION, *out);
|
|
|
|
else /// Specially ignore connections if we are not leader, client will throw exception
|
|
|
|
Coordination::write(42, *out);
|
|
|
|
|
2021-02-03 20:32:15 +00:00
|
|
|
Coordination::write(static_cast<int32_t>(session_timeout.totalMilliseconds()), *out);
|
2020-11-19 16:06:19 +00:00
|
|
|
Coordination::write(session_id, *out);
|
2020-11-11 13:07:06 +00:00
|
|
|
std::array<char, Coordination::PASSWORD_LENGTH> passwd{};
|
|
|
|
Coordination::write(passwd, *out);
|
2020-10-30 14:16:47 +00:00
|
|
|
out->next();
|
|
|
|
}
|
|
|
|
|
2021-03-29 08:24:56 +00:00
|
|
|
void KeeperTCPHandler::run()
|
2020-10-30 14:16:47 +00:00
|
|
|
{
|
|
|
|
runImpl();
|
|
|
|
}
|
|
|
|
|
2021-03-29 08:24:56 +00:00
|
|
|
Poco::Timespan KeeperTCPHandler::receiveHandshake()
|
2020-10-30 14:16:47 +00:00
|
|
|
{
|
|
|
|
int32_t handshake_length;
|
|
|
|
int32_t protocol_version;
|
|
|
|
int64_t last_zxid_seen;
|
2021-02-03 20:32:15 +00:00
|
|
|
int32_t timeout_ms;
|
2020-10-30 14:16:47 +00:00
|
|
|
int64_t previous_session_id = 0; /// We don't support session restore. So previous session_id is always zero.
|
2020-11-11 13:07:06 +00:00
|
|
|
std::array<char, Coordination::PASSWORD_LENGTH> passwd {};
|
|
|
|
Coordination::read(handshake_length, *in);
|
2020-12-08 13:28:39 +00:00
|
|
|
if (handshake_length != Coordination::CLIENT_HANDSHAKE_LENGTH && handshake_length != Coordination::CLIENT_HANDSHAKE_LENGTH_WITH_READONLY)
|
2020-11-25 13:19:09 +00:00
|
|
|
throw Exception("Unexpected handshake length received: " + toString(handshake_length), ErrorCodes::UNEXPECTED_PACKET_FROM_CLIENT);
|
2020-10-30 14:16:47 +00:00
|
|
|
|
2020-11-11 13:07:06 +00:00
|
|
|
Coordination::read(protocol_version, *in);
|
2020-10-30 14:16:47 +00:00
|
|
|
|
2020-11-11 13:07:06 +00:00
|
|
|
if (protocol_version != Coordination::ZOOKEEPER_PROTOCOL_VERSION)
|
2020-11-25 13:19:09 +00:00
|
|
|
throw Exception("Unexpected protocol version: " + toString(protocol_version), ErrorCodes::UNEXPECTED_PACKET_FROM_CLIENT);
|
2020-10-30 14:16:47 +00:00
|
|
|
|
2020-11-11 13:07:06 +00:00
|
|
|
Coordination::read(last_zxid_seen, *in);
|
2021-02-03 20:32:15 +00:00
|
|
|
Coordination::read(timeout_ms, *in);
|
2020-10-30 14:16:47 +00:00
|
|
|
|
2021-03-10 16:12:32 +00:00
|
|
|
/// TODO Stop ignoring this value
|
|
|
|
Coordination::read(previous_session_id, *in);
|
2020-11-11 13:07:06 +00:00
|
|
|
Coordination::read(passwd, *in);
|
2020-12-08 13:28:39 +00:00
|
|
|
|
|
|
|
int8_t readonly;
|
|
|
|
if (handshake_length == Coordination::CLIENT_HANDSHAKE_LENGTH_WITH_READONLY)
|
|
|
|
Coordination::read(readonly, *in);
|
2021-02-03 20:32:15 +00:00
|
|
|
|
|
|
|
return Poco::Timespan(0, timeout_ms * 1000);
|
2020-10-30 14:16:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2021-03-29 08:24:56 +00:00
|
|
|
void KeeperTCPHandler::runImpl()
|
2020-10-30 14:16:47 +00:00
|
|
|
{
|
|
|
|
setThreadName("TstKprHandler");
|
|
|
|
ThreadStatus thread_status;
|
2021-04-10 23:33:54 +00:00
|
|
|
auto global_receive_timeout = global_context->getSettingsRef().receive_timeout;
|
|
|
|
auto global_send_timeout = global_context->getSettingsRef().send_timeout;
|
2020-10-30 14:16:47 +00:00
|
|
|
|
|
|
|
socket().setReceiveTimeout(global_receive_timeout);
|
|
|
|
socket().setSendTimeout(global_send_timeout);
|
|
|
|
socket().setNoDelay(true);
|
|
|
|
|
|
|
|
in = std::make_shared<ReadBufferFromPocoSocket>(socket());
|
|
|
|
out = std::make_shared<WriteBufferFromPocoSocket>(socket());
|
|
|
|
|
|
|
|
if (in->eof())
|
|
|
|
{
|
|
|
|
LOG_WARNING(log, "Client has not sent any data.");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
try
|
|
|
|
{
|
2021-02-03 20:32:15 +00:00
|
|
|
auto client_timeout = receiveHandshake();
|
|
|
|
if (client_timeout != 0)
|
|
|
|
session_timeout = std::min(client_timeout, session_timeout);
|
2020-10-30 14:16:47 +00:00
|
|
|
}
|
|
|
|
catch (const Exception & e) /// Typical for an incorrect username, password, or address.
|
|
|
|
{
|
2020-11-11 13:07:06 +00:00
|
|
|
LOG_WARNING(log, "Cannot receive handshake {}", e.displayText());
|
2020-11-11 09:05:45 +00:00
|
|
|
return;
|
2020-10-30 14:16:47 +00:00
|
|
|
}
|
|
|
|
|
2021-10-14 10:21:41 +00:00
|
|
|
if (keeper_dispatcher->checkInit() && keeper_dispatcher->hasLeader())
|
2021-01-27 17:54:25 +00:00
|
|
|
{
|
2021-02-01 07:51:10 +00:00
|
|
|
try
|
|
|
|
{
|
2021-02-04 19:29:46 +00:00
|
|
|
LOG_INFO(log, "Requesting session ID for the new client");
|
2021-05-22 07:46:12 +00:00
|
|
|
session_id = keeper_dispatcher->getSessionID(session_timeout.totalMilliseconds());
|
2021-02-04 19:29:46 +00:00
|
|
|
LOG_INFO(log, "Received session ID {}", session_id);
|
2021-02-01 07:51:10 +00:00
|
|
|
}
|
|
|
|
catch (const Exception & e)
|
|
|
|
{
|
|
|
|
LOG_WARNING(log, "Cannot receive session id {}", e.displayText());
|
2021-02-01 11:27:26 +00:00
|
|
|
sendHandshake(false);
|
2021-02-01 07:51:10 +00:00
|
|
|
return;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
sendHandshake(true);
|
2021-01-27 17:54:25 +00:00
|
|
|
}
|
2021-02-01 07:51:10 +00:00
|
|
|
else
|
2021-01-27 17:54:25 +00:00
|
|
|
{
|
2021-10-30 12:32:29 +00:00
|
|
|
String reason;
|
2021-10-30 13:02:05 +00:00
|
|
|
if (!keeper_dispatcher->checkInit() && !keeper_dispatcher->hasLeader())
|
2021-10-30 12:32:29 +00:00
|
|
|
reason = "server is not initialized yet and no alive leader exists";
|
|
|
|
else if (!keeper_dispatcher->checkInit())
|
|
|
|
reason = "server is not initialized yet";
|
|
|
|
else
|
|
|
|
reason = "no alive leader exists";
|
|
|
|
|
2021-10-14 10:21:41 +00:00
|
|
|
LOG_WARNING(log, "Ignoring user request, because {}", reason);
|
2021-02-01 07:51:10 +00:00
|
|
|
sendHandshake(false);
|
2021-01-27 17:54:25 +00:00
|
|
|
return;
|
|
|
|
}
|
2021-01-19 14:22:28 +00:00
|
|
|
|
|
|
|
auto response_fd = poll_wrapper->getResponseFD();
|
|
|
|
auto response_callback = [this, response_fd] (const Coordination::ZooKeeperResponsePtr & response)
|
|
|
|
{
|
2021-10-12 15:41:38 +00:00
|
|
|
if (!responses->push(response))
|
|
|
|
throw Exception(ErrorCodes::SYSTEM_ERROR,
|
|
|
|
"Could not push response with xid {} and zxid {}",
|
|
|
|
response->xid,
|
|
|
|
response->zxid);
|
|
|
|
|
2021-01-19 14:22:28 +00:00
|
|
|
UInt8 single_byte = 1;
|
|
|
|
[[maybe_unused]] int result = write(response_fd, &single_byte, sizeof(single_byte));
|
|
|
|
};
|
2021-05-22 07:46:12 +00:00
|
|
|
keeper_dispatcher->registerSession(session_id, response_callback);
|
2021-01-19 14:22:28 +00:00
|
|
|
|
2020-11-19 16:06:19 +00:00
|
|
|
session_stopwatch.start();
|
2020-12-08 14:28:51 +00:00
|
|
|
bool close_received = false;
|
2020-12-09 08:19:52 +00:00
|
|
|
try
|
2020-10-30 14:16:47 +00:00
|
|
|
{
|
2020-12-09 08:19:52 +00:00
|
|
|
while (true)
|
2020-11-10 13:43:10 +00:00
|
|
|
{
|
2020-12-09 08:19:52 +00:00
|
|
|
using namespace std::chrono_literals;
|
|
|
|
|
2021-02-19 11:06:17 +00:00
|
|
|
PollResult result = poll_wrapper->poll(session_timeout, in);
|
2021-01-19 14:45:45 +00:00
|
|
|
if (result.has_requests && !close_received)
|
2020-11-10 13:43:10 +00:00
|
|
|
{
|
2021-02-19 11:06:17 +00:00
|
|
|
auto [received_op, received_xid] = receiveRequest();
|
2020-12-09 08:19:52 +00:00
|
|
|
|
2021-02-19 11:06:17 +00:00
|
|
|
if (received_op == Coordination::OpNum::Close)
|
|
|
|
{
|
|
|
|
LOG_DEBUG(log, "Received close event with xid {} for session id #{}", received_xid, session_id);
|
|
|
|
close_xid = received_xid;
|
|
|
|
close_received = true;
|
|
|
|
}
|
|
|
|
else if (received_op == Coordination::OpNum::Heartbeat)
|
|
|
|
{
|
|
|
|
LOG_TRACE(log, "Received heartbeat for session #{}", session_id);
|
2020-11-26 12:26:41 +00:00
|
|
|
}
|
2021-03-06 14:14:38 +00:00
|
|
|
|
|
|
|
/// Each request restarts session stopwatch
|
|
|
|
session_stopwatch.restart();
|
2020-11-27 10:12:11 +00:00
|
|
|
}
|
2020-11-26 08:59:23 +00:00
|
|
|
|
2021-01-22 10:48:40 +00:00
|
|
|
/// Process exact amount of responses from pipe
|
2021-01-22 10:02:48 +00:00
|
|
|
/// otherwise state of responses queue and signaling pipe
|
|
|
|
/// became inconsistent and race condition is possible.
|
2021-02-24 11:54:14 +00:00
|
|
|
while (result.responses_count != 0)
|
2020-11-24 14:02:55 +00:00
|
|
|
{
|
2021-01-19 14:22:28 +00:00
|
|
|
Coordination::ZooKeeperResponsePtr response;
|
2021-02-19 12:56:51 +00:00
|
|
|
|
2021-01-22 10:02:48 +00:00
|
|
|
if (!responses->tryPop(response))
|
2021-02-19 11:06:17 +00:00
|
|
|
throw Exception(ErrorCodes::LOGICAL_ERROR, "We must have ready response, but queue is empty. It's a bug.");
|
2021-01-19 14:22:28 +00:00
|
|
|
|
2021-01-22 10:02:48 +00:00
|
|
|
if (response->xid == close_xid)
|
|
|
|
{
|
|
|
|
LOG_DEBUG(log, "Session #{} successfully closed", session_id);
|
|
|
|
return;
|
2020-11-24 14:02:55 +00:00
|
|
|
}
|
2021-02-24 11:54:14 +00:00
|
|
|
|
2021-02-04 12:07:41 +00:00
|
|
|
response->write(*out);
|
|
|
|
if (response->error == Coordination::Error::ZSESSIONEXPIRED)
|
|
|
|
{
|
|
|
|
LOG_DEBUG(log, "Session #{} expired because server shutting down or quorum is not alive", session_id);
|
2021-05-22 07:46:12 +00:00
|
|
|
keeper_dispatcher->finishSession(session_id);
|
2021-02-04 12:07:41 +00:00
|
|
|
return;
|
|
|
|
}
|
2021-02-24 11:54:14 +00:00
|
|
|
|
|
|
|
result.responses_count--;
|
2020-11-24 14:02:55 +00:00
|
|
|
}
|
2020-11-19 16:06:19 +00:00
|
|
|
|
2020-12-18 09:13:50 +00:00
|
|
|
if (result.error)
|
2020-12-09 08:19:52 +00:00
|
|
|
throw Exception("Exception happened while reading from socket", ErrorCodes::SYSTEM_ERROR);
|
|
|
|
|
|
|
|
if (session_stopwatch.elapsedMicroseconds() > static_cast<UInt64>(session_timeout.totalMicroseconds()))
|
2020-12-08 14:28:51 +00:00
|
|
|
{
|
2020-12-09 08:19:52 +00:00
|
|
|
LOG_DEBUG(log, "Session #{} expired", session_id);
|
2021-05-22 07:46:12 +00:00
|
|
|
keeper_dispatcher->finishSession(session_id);
|
2020-12-09 08:19:52 +00:00
|
|
|
break;
|
|
|
|
}
|
2020-11-10 13:43:10 +00:00
|
|
|
}
|
2020-10-30 14:16:47 +00:00
|
|
|
}
|
2020-12-09 08:19:52 +00:00
|
|
|
catch (const Exception & ex)
|
|
|
|
{
|
|
|
|
LOG_INFO(log, "Got exception processing session #{}: {}", session_id, getExceptionMessage(ex, true));
|
2021-05-22 07:46:12 +00:00
|
|
|
keeper_dispatcher->finishSession(session_id);
|
2020-12-09 08:19:52 +00:00
|
|
|
}
|
2020-10-30 14:16:47 +00:00
|
|
|
}
|
|
|
|
|
2021-03-29 08:24:56 +00:00
|
|
|
std::pair<Coordination::OpNum, Coordination::XID> KeeperTCPHandler::receiveRequest()
|
2020-10-30 14:16:47 +00:00
|
|
|
{
|
|
|
|
int32_t length;
|
2020-11-11 13:07:06 +00:00
|
|
|
Coordination::read(length, *in);
|
2020-10-30 14:16:47 +00:00
|
|
|
int32_t xid;
|
2020-11-11 13:07:06 +00:00
|
|
|
Coordination::read(xid, *in);
|
2020-10-30 14:16:47 +00:00
|
|
|
|
2020-11-11 13:07:06 +00:00
|
|
|
Coordination::OpNum opnum;
|
|
|
|
Coordination::read(opnum, *in);
|
2020-11-10 13:43:10 +00:00
|
|
|
|
2020-11-04 18:54:55 +00:00
|
|
|
Coordination::ZooKeeperRequestPtr request = Coordination::ZooKeeperRequestFactory::instance().get(opnum);
|
|
|
|
request->xid = xid;
|
|
|
|
request->readImpl(*in);
|
2020-11-03 14:49:30 +00:00
|
|
|
|
2021-05-22 07:46:12 +00:00
|
|
|
if (!keeper_dispatcher->putRequest(request, session_id))
|
2021-01-26 08:18:00 +00:00
|
|
|
throw Exception(ErrorCodes::TIMEOUT_EXCEEDED, "Session {} already disconnected", session_id);
|
2021-01-19 14:22:28 +00:00
|
|
|
return std::make_pair(opnum, xid);
|
2020-10-30 14:16:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
2021-02-01 13:18:17 +00:00
|
|
|
|
|
|
|
#endif
|