Merge pull request #16329 from ClickHouse/better-diagnostics-when-client-dropped-connection

Better diagnostics when client has dropped connection
This commit is contained in:
alexey-milovidov 2020-10-25 01:01:12 +03:00 committed by GitHub
commit 74b37f37fb
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 24 additions and 3 deletions

View File

@ -277,6 +277,9 @@ void TCPHandler::runImpl()
/// Do it before sending end of stream, to have a chance to show log message in client. /// Do it before sending end of stream, to have a chance to show log message in client.
query_scope->logPeakMemoryUsage(); query_scope->logPeakMemoryUsage();
if (state.is_connection_closed)
break;
sendLogs(); sendLogs();
sendEndOfStream(); sendEndOfStream();
@ -444,7 +447,11 @@ bool TCPHandler::readDataNext(const size_t & poll_interval, const int & receive_
/// If client disconnected. /// If client disconnected.
if (in->eof()) if (in->eof())
{
LOG_INFO(log, "Client has dropped the connection, cancel the query.");
state.is_connection_closed = true;
return false; return false;
}
/// We accept and process data. And if they are over, then we leave. /// We accept and process data. And if they are over, then we leave.
if (!receivePacket()) if (!receivePacket())
@ -477,9 +484,8 @@ void TCPHandler::readData(const Settings & connection_settings)
std::tie(poll_interval, receive_timeout) = getReadTimeouts(connection_settings); std::tie(poll_interval, receive_timeout) = getReadTimeouts(connection_settings);
sendLogs(); sendLogs();
while (true) while (readDataNext(poll_interval, receive_timeout))
if (!readDataNext(poll_interval, receive_timeout)) ;
return;
} }
@ -567,6 +573,9 @@ void TCPHandler::processOrdinaryQuery()
sendProgress(); sendProgress();
} }
if (state.is_connection_closed)
return;
sendData({}); sendData({});
} }
@ -632,6 +641,9 @@ void TCPHandler::processOrdinaryQueryWithProcessors()
sendLogs(); sendLogs();
} }
if (state.is_connection_closed)
return;
sendData({}); sendData({});
} }
@ -1179,6 +1191,14 @@ bool TCPHandler::isQueryCancelled()
/// During request execution the only packet that can come from the client is stopping the query. /// During request execution the only packet that can come from the client is stopping the query.
if (static_cast<ReadBufferFromPocoSocket &>(*in).poll(0)) if (static_cast<ReadBufferFromPocoSocket &>(*in).poll(0))
{ {
if (in->eof())
{
LOG_INFO(log, "Client has dropped the connection, cancel the query.");
state.is_cancelled = true;
state.is_connection_closed = true;
return true;
}
UInt64 packet_type = 0; UInt64 packet_type = 0;
readVarUInt(packet_type, *in); readVarUInt(packet_type, *in);

View File

@ -57,6 +57,7 @@ struct QueryState
/// Is request cancelled /// Is request cancelled
bool is_cancelled = false; bool is_cancelled = false;
bool is_connection_closed = false;
/// empty or not /// empty or not
bool is_empty = true; bool is_empty = true;
/// Data was sent. /// Data was sent.