2016-06-07 08:23:15 +00:00
|
|
|
#include <Poco/Net/NetException.h>
|
|
|
|
|
2022-02-20 15:59:39 +00:00
|
|
|
#include <base/scope_guard.h>
|
|
|
|
|
2017-04-01 09:19:00 +00:00
|
|
|
#include <IO/WriteBufferFromPocoSocket.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>
|
2023-03-03 19:30:43 +00:00
|
|
|
#include <Common/AsyncTaskExecutor.h>
|
2023-05-26 23:11:57 +00:00
|
|
|
#include <Common/checkSSLReturnCode.h>
|
2018-05-28 19:53:03 +00:00
|
|
|
|
|
|
|
namespace ProfileEvents
|
|
|
|
{
|
|
|
|
extern const Event NetworkSendElapsedMicroseconds;
|
2021-07-03 21:00:50 +00:00
|
|
|
extern const Event NetworkSendBytes;
|
|
|
|
}
|
|
|
|
|
|
|
|
namespace CurrentMetrics
|
|
|
|
{
|
|
|
|
extern const Metric NetworkSend;
|
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_WRITE_TO_SOCKET;
|
2022-10-07 10:46:45 +00:00
|
|
|
extern const int LOGICAL_ERROR;
|
2016-06-07 08:23:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void WriteBufferFromPocoSocket::nextImpl()
|
|
|
|
{
|
|
|
|
if (!offset())
|
|
|
|
return;
|
|
|
|
|
2018-05-31 15:54:08 +00:00
|
|
|
Stopwatch watch;
|
2016-06-07 08:23:15 +00:00
|
|
|
size_t bytes_written = 0;
|
2022-02-17 15:45:15 +00:00
|
|
|
|
|
|
|
SCOPE_EXIT({
|
|
|
|
ProfileEvents::increment(ProfileEvents::NetworkSendElapsedMicroseconds, watch.elapsedMicroseconds());
|
|
|
|
ProfileEvents::increment(ProfileEvents::NetworkSendBytes, bytes_written);
|
|
|
|
});
|
|
|
|
|
2016-06-07 08:23:15 +00:00
|
|
|
while (bytes_written < offset())
|
|
|
|
{
|
|
|
|
ssize_t res = 0;
|
|
|
|
|
2017-03-26 01:28:07 +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::NetworkSend);
|
2022-10-07 10:46:45 +00:00
|
|
|
char * pos = working_buffer.begin() + bytes_written;
|
|
|
|
size_t size = offset() - bytes_written;
|
|
|
|
if (size > INT_MAX)
|
|
|
|
throw Exception(ErrorCodes::LOGICAL_ERROR, "Buffer overflow");
|
2023-03-03 19:30:43 +00:00
|
|
|
|
2023-05-26 23:11:57 +00:00
|
|
|
/// If async_callback is specified, set socket to non-blocking mode
|
|
|
|
/// and try to write data to it, if socket is not ready for writing,
|
|
|
|
/// run async_callback and try again later.
|
|
|
|
/// It is expected that file descriptor may be polled externally.
|
|
|
|
/// Note that send timeout is not checked here. External code should check it while polling.
|
|
|
|
if (async_callback)
|
|
|
|
{
|
|
|
|
socket.setBlocking(false);
|
|
|
|
/// Set socket to blocking mode at the end.
|
|
|
|
SCOPE_EXIT(socket.setBlocking(true));
|
|
|
|
bool secure = socket.secure();
|
|
|
|
res = socket.impl()->sendBytes(pos, static_cast<int>(size));
|
|
|
|
|
|
|
|
/// Check EAGAIN and ERR_SSL_WANT_WRITE/ERR_SSL_WANT_READ for secure socket (writing to secure socket can read too).
|
|
|
|
while (res < 0 && (errno == EAGAIN || (secure && (checkSSLWantRead(res) || checkSSLWantWrite(res)))))
|
|
|
|
{
|
|
|
|
/// In case of ERR_SSL_WANT_READ we should wait for socket to be ready for reading, otherwise - for writing.
|
|
|
|
if (secure && checkSSLWantRead(res))
|
|
|
|
async_callback(socket.impl()->sockfd(), socket.getReceiveTimeout(), AsyncEventTimeoutType::RECEIVE, socket_description, AsyncTaskExecutor::Event::READ | AsyncTaskExecutor::Event::ERROR);
|
|
|
|
else
|
|
|
|
async_callback(socket.impl()->sockfd(), socket.getSendTimeout(), AsyncEventTimeoutType::SEND, socket_description, AsyncTaskExecutor::Event::WRITE | AsyncTaskExecutor::Event::ERROR);
|
|
|
|
|
|
|
|
/// Try to write again.
|
|
|
|
res = socket.impl()->sendBytes(pos, static_cast<int>(size));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
2023-05-25 20:24:03 +00:00
|
|
|
{
|
2023-05-26 23:11:57 +00:00
|
|
|
res = socket.impl()->sendBytes(pos, static_cast<int>(size));
|
2023-05-25 20:24:03 +00:00
|
|
|
}
|
2016-06-07 08:23:15 +00:00
|
|
|
}
|
|
|
|
catch (const Poco::Net::NetException & e)
|
|
|
|
{
|
2023-02-25 00:18:34 +00:00
|
|
|
throw NetException(ErrorCodes::NETWORK_ERROR, "{}, while writing to socket ({} -> {})", e.displayText(),
|
|
|
|
our_address.toString(), peer_address.toString());
|
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
|
|
|
{
|
2023-01-23 21:13:58 +00:00
|
|
|
throw NetException(ErrorCodes::SOCKET_TIMEOUT, "Timeout exceeded while writing to socket ({}, {} ms)",
|
2021-09-19 21:35:49 +00:00
|
|
|
peer_address.toString(),
|
2023-01-23 21:13:58 +00:00
|
|
|
socket.impl()->getSendTimeout().totalMilliseconds());
|
2016-06-07 08:23:15 +00:00
|
|
|
}
|
|
|
|
catch (const Poco::IOException & e)
|
|
|
|
{
|
2023-02-25 00:18:34 +00:00
|
|
|
throw NetException(ErrorCodes::NETWORK_ERROR, "{}, while writing to socket ({} -> {})", e.displayText(),
|
|
|
|
our_address.toString(), peer_address.toString());
|
2016-06-07 08:23:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (res < 0)
|
2023-02-25 00:18:34 +00:00
|
|
|
throw NetException(ErrorCodes::CANNOT_WRITE_TO_SOCKET, "Cannot write to socket ({} -> {})",
|
|
|
|
our_address.toString(), peer_address.toString());
|
2018-05-28 19:53:03 +00:00
|
|
|
|
2016-06-07 08:23:15 +00:00
|
|
|
bytes_written += res;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
WriteBufferFromPocoSocket::WriteBufferFromPocoSocket(Poco::Net::Socket & socket_, size_t buf_size)
|
2023-03-03 19:30:43 +00:00
|
|
|
: BufferWithOwnMemory<WriteBuffer>(buf_size)
|
|
|
|
, socket(socket_)
|
|
|
|
, peer_address(socket.peerAddress())
|
|
|
|
, our_address(socket.address())
|
|
|
|
, socket_description("socket (" + peer_address.toString() + ")")
|
2016-06-07 08:23:15 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
WriteBufferFromPocoSocket::~WriteBufferFromPocoSocket()
|
|
|
|
{
|
2023-05-11 12:16:48 +00:00
|
|
|
try
|
|
|
|
{
|
|
|
|
finalize();
|
|
|
|
}
|
|
|
|
catch (...)
|
|
|
|
{
|
|
|
|
tryLogCurrentException(__PRETTY_FUNCTION__);
|
|
|
|
}
|
2016-06-07 08:23:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|