2016-06-07 08:23:15 +00:00
|
|
|
#include <Poco/Net/NetException.h>
|
|
|
|
|
2022-02-20 15:59:17 +00:00
|
|
|
#include <base/scope_guard.h>
|
|
|
|
|
2017-04-01 09:19:00 +00:00
|
|
|
#include <IO/ReadBufferFromPocoSocket.h>
|
2019-02-10 17:40:52 +00:00
|
|
|
#include <Common/Exception.h>
|
2017-04-01 09:19:00 +00:00
|
|
|
#include <Common/NetException.h>
|
2018-05-28 19:53:03 +00:00
|
|
|
#include <Common/Stopwatch.h>
|
2021-07-03 21:00:50 +00:00
|
|
|
#include <Common/ProfileEvents.h>
|
|
|
|
#include <Common/CurrentMetrics.h>
|
2018-05-28 19:53:03 +00:00
|
|
|
|
|
|
|
|
|
|
|
namespace ProfileEvents
|
|
|
|
{
|
|
|
|
extern const Event NetworkReceiveElapsedMicroseconds;
|
2021-07-03 21:00:50 +00:00
|
|
|
extern const Event NetworkReceiveBytes;
|
|
|
|
}
|
|
|
|
|
|
|
|
namespace CurrentMetrics
|
|
|
|
{
|
|
|
|
extern const Metric NetworkReceive;
|
2018-05-28 19:53:03 +00:00
|
|
|
}
|
2016-06-07 08:23:15 +00:00
|
|
|
|
|
|
|
|
|
|
|
namespace DB
|
|
|
|
{
|
|
|
|
namespace ErrorCodes
|
|
|
|
{
|
|
|
|
extern const int NETWORK_ERROR;
|
|
|
|
extern const int SOCKET_TIMEOUT;
|
|
|
|
extern const int CANNOT_READ_FROM_SOCKET;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
bool ReadBufferFromPocoSocket::nextImpl()
|
|
|
|
{
|
|
|
|
ssize_t bytes_read = 0;
|
2018-05-31 15:54:08 +00:00
|
|
|
Stopwatch watch;
|
2017-04-01 07:20:54 +00:00
|
|
|
|
2022-02-17 15:45:15 +00:00
|
|
|
SCOPE_EXIT({
|
|
|
|
// / NOTE: it is quite inaccurate on high loads since the thread could be replaced by another one
|
|
|
|
ProfileEvents::increment(ProfileEvents::NetworkReceiveElapsedMicroseconds, watch.elapsedMicroseconds());
|
|
|
|
ProfileEvents::increment(ProfileEvents::NetworkReceiveBytes, bytes_read);
|
|
|
|
});
|
|
|
|
|
2017-03-25 20:12:56 +00:00
|
|
|
/// Add more details to exceptions.
|
2016-06-07 08:23:15 +00:00
|
|
|
try
|
|
|
|
{
|
2021-07-03 21:00:50 +00:00
|
|
|
CurrentMetrics::Increment metric_increment(CurrentMetrics::NetworkReceive);
|
|
|
|
|
2021-03-03 13:10:15 +00:00
|
|
|
/// If async_callback is specified, and read will block, run async_callback and try again later.
|
2020-12-02 17:02:14 +00:00
|
|
|
/// It is expected that file descriptor may be polled externally.
|
|
|
|
/// Note that receive timeout is not checked here. External code should check it while polling.
|
2021-03-03 13:10:15 +00:00
|
|
|
while (async_callback && !socket.poll(0, Poco::Net::Socket::SELECT_READ))
|
2021-02-06 00:54:27 +00:00
|
|
|
async_callback(socket.impl()->sockfd(), socket.getReceiveTimeout(), socket_description);
|
2021-03-03 13:10:15 +00:00
|
|
|
|
|
|
|
bytes_read = socket.impl()->receiveBytes(internal_buffer.begin(), internal_buffer.size());
|
2016-06-07 08:23:15 +00:00
|
|
|
}
|
|
|
|
catch (const Poco::Net::NetException & e)
|
|
|
|
{
|
2018-11-22 18:09:17 +00:00
|
|
|
throw NetException(e.displayText() + ", while reading from socket (" + peer_address.toString() + ")", ErrorCodes::NETWORK_ERROR);
|
2016-06-07 08:23:15 +00:00
|
|
|
}
|
2018-08-10 04:02:56 +00:00
|
|
|
catch (const Poco::TimeoutException &)
|
2016-06-07 08:23:15 +00:00
|
|
|
{
|
2021-09-19 21:35:49 +00:00
|
|
|
throw NetException(fmt::format("Timeout exceeded while reading from socket ({}, {} ms)",
|
|
|
|
peer_address.toString(),
|
|
|
|
socket.impl()->getReceiveTimeout().totalMilliseconds()), ErrorCodes::SOCKET_TIMEOUT);
|
2016-06-07 08:23:15 +00:00
|
|
|
}
|
|
|
|
catch (const Poco::IOException & e)
|
|
|
|
{
|
2018-11-22 18:09:17 +00:00
|
|
|
throw NetException(e.displayText() + ", while reading from socket (" + peer_address.toString() + ")", ErrorCodes::NETWORK_ERROR);
|
2016-06-07 08:23:15 +00:00
|
|
|
}
|
2017-04-01 07:20:54 +00:00
|
|
|
|
2016-06-07 08:23:15 +00:00
|
|
|
if (bytes_read < 0)
|
|
|
|
throw NetException("Cannot read from socket (" + peer_address.toString() + ")", ErrorCodes::CANNOT_READ_FROM_SOCKET);
|
2017-04-01 07:20:54 +00:00
|
|
|
|
2016-06-07 08:23:15 +00:00
|
|
|
if (bytes_read)
|
|
|
|
working_buffer.resize(bytes_read);
|
|
|
|
else
|
|
|
|
return false;
|
2017-04-01 07:20:54 +00:00
|
|
|
|
2016-06-07 08:23:15 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
ReadBufferFromPocoSocket::ReadBufferFromPocoSocket(Poco::Net::Socket & socket_, size_t buf_size)
|
2021-02-06 00:54:27 +00:00
|
|
|
: BufferWithOwnMemory<ReadBuffer>(buf_size)
|
|
|
|
, socket(socket_)
|
|
|
|
, peer_address(socket.peerAddress())
|
|
|
|
, socket_description("socket (" + peer_address.toString() + ")")
|
2016-06-07 08:23:15 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2021-02-21 14:13:33 +00:00
|
|
|
bool ReadBufferFromPocoSocket::poll(size_t timeout_microseconds) const
|
2016-06-07 08:23:15 +00:00
|
|
|
{
|
2021-06-22 20:22:13 +00:00
|
|
|
if (available())
|
|
|
|
return true;
|
|
|
|
|
|
|
|
Stopwatch watch;
|
|
|
|
bool res = socket.poll(timeout_microseconds, Poco::Net::Socket::SELECT_READ | Poco::Net::Socket::SELECT_ERROR);
|
|
|
|
ProfileEvents::increment(ProfileEvents::NetworkReceiveElapsedMicroseconds, watch.elapsedMicroseconds());
|
|
|
|
return res;
|
2016-06-07 08:23:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|