2017-04-01 09:19:00 +00:00
|
|
|
#include <DataStreams/RemoteBlockInputStream.h>
|
|
|
|
#include <DataStreams/OneBlockInputStream.h>
|
|
|
|
#include <Common/VirtualColumnUtils.h>
|
|
|
|
#include <Common/NetException.h>
|
|
|
|
#include <Interpreters/Context.h>
|
|
|
|
#include <Storages/IStorage.h>
|
2017-01-21 04:24:28 +00:00
|
|
|
|
2015-11-06 17:44:01 +00:00
|
|
|
|
|
|
|
namespace DB
|
|
|
|
{
|
|
|
|
|
2016-01-11 21:46:36 +00:00
|
|
|
namespace ErrorCodes
|
|
|
|
{
|
2017-04-01 07:20:54 +00:00
|
|
|
extern const int UNKNOWN_PACKET_FROM_SERVER;
|
|
|
|
extern const int LOGICAL_ERROR;
|
2016-01-11 21:46:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-11-06 17:44:01 +00:00
|
|
|
RemoteBlockInputStream::RemoteBlockInputStream(Connection & connection_, const String & query_,
|
2017-06-19 20:31:23 +00:00
|
|
|
const Settings * settings_, const Context & context_, ThrottlerPtr throttler_,
|
|
|
|
const Tables & external_tables_, QueryProcessingStage::Enum stage_)
|
2017-04-01 07:20:54 +00:00
|
|
|
: connection(&connection_), query(query_), throttler(throttler_), external_tables(external_tables_),
|
|
|
|
stage(stage_), context(context_)
|
2015-11-06 17:44:01 +00:00
|
|
|
{
|
2017-04-01 07:20:54 +00:00
|
|
|
init(settings_);
|
2015-11-06 17:44:01 +00:00
|
|
|
}
|
|
|
|
|
2017-04-19 17:40:55 +00:00
|
|
|
RemoteBlockInputStream::RemoteBlockInputStream(const ConnectionPoolWithFailoverPtr & pool_, const String & query_,
|
2017-06-19 20:31:23 +00:00
|
|
|
const Settings * settings_, const Context & context_, ThrottlerPtr throttler_,
|
|
|
|
const Tables & external_tables_, QueryProcessingStage::Enum stage_)
|
2017-04-01 07:20:54 +00:00
|
|
|
: pool(pool_), query(query_), throttler(throttler_), external_tables(external_tables_),
|
|
|
|
stage(stage_), context(context_)
|
2015-11-06 17:44:01 +00:00
|
|
|
{
|
2017-04-01 07:20:54 +00:00
|
|
|
init(settings_);
|
2015-11-06 17:44:01 +00:00
|
|
|
}
|
|
|
|
|
2017-04-19 17:40:55 +00:00
|
|
|
RemoteBlockInputStream::RemoteBlockInputStream(ConnectionPoolWithFailoverPtrs && pools_, const String & query_,
|
2017-06-19 20:31:23 +00:00
|
|
|
const Settings * settings_, const Context & context_, ThrottlerPtr throttler_,
|
|
|
|
const Tables & external_tables_, QueryProcessingStage::Enum stage_)
|
2017-04-19 17:40:55 +00:00
|
|
|
: pools(std::move(pools_)), query(query_), throttler(throttler_), external_tables(external_tables_),
|
2017-04-01 07:20:54 +00:00
|
|
|
stage(stage_), context(context_)
|
2015-11-06 17:44:01 +00:00
|
|
|
{
|
2017-04-01 07:20:54 +00:00
|
|
|
init(settings_);
|
2015-11-06 17:44:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
RemoteBlockInputStream::~RemoteBlockInputStream()
|
|
|
|
{
|
2017-04-01 07:20:54 +00:00
|
|
|
/** If interrupted in the middle of the loop of communication with replicas, then interrupt
|
|
|
|
* all connections, then read and skip the remaining packets to make sure
|
|
|
|
* these connections did not remain hanging in the out-of-sync state.
|
|
|
|
*/
|
|
|
|
if (established || isQueryPending())
|
|
|
|
multiplexed_connections->disconnect();
|
2015-11-06 17:44:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void RemoteBlockInputStream::appendExtraInfo()
|
|
|
|
{
|
2017-04-01 07:20:54 +00:00
|
|
|
append_extra_info = true;
|
2015-11-06 17:44:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void RemoteBlockInputStream::readPrefix()
|
|
|
|
{
|
2017-04-01 07:20:54 +00:00
|
|
|
if (!sent_query)
|
|
|
|
sendQuery();
|
2015-11-06 17:44:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void RemoteBlockInputStream::cancel()
|
|
|
|
{
|
2017-04-01 07:20:54 +00:00
|
|
|
bool old_val = false;
|
|
|
|
if (!is_cancelled.compare_exchange_strong(old_val, true, std::memory_order_seq_cst, std::memory_order_relaxed))
|
|
|
|
return;
|
2015-11-06 17:44:01 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
{
|
|
|
|
std::lock_guard<std::mutex> lock(external_tables_mutex);
|
2015-11-06 17:44:01 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
/// Stop sending external data.
|
|
|
|
for (auto & vec : external_tables_data)
|
|
|
|
for (auto & elem : vec)
|
|
|
|
if (IProfilingBlockInputStream * stream = dynamic_cast<IProfilingBlockInputStream *>(elem.first.get()))
|
|
|
|
stream->cancel();
|
|
|
|
}
|
2015-11-06 17:44:01 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
if (!isQueryPending() || hasThrownException())
|
|
|
|
return;
|
2015-11-06 17:44:01 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
tryCancel("Cancelling query");
|
2015-11-06 17:44:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void RemoteBlockInputStream::sendExternalTables()
|
|
|
|
{
|
2017-04-01 07:20:54 +00:00
|
|
|
size_t count = multiplexed_connections->size();
|
|
|
|
|
|
|
|
{
|
|
|
|
std::lock_guard<std::mutex> lock(external_tables_mutex);
|
|
|
|
|
|
|
|
external_tables_data.reserve(count);
|
|
|
|
|
|
|
|
for (size_t i = 0; i < count; ++i)
|
|
|
|
{
|
|
|
|
ExternalTablesData res;
|
|
|
|
for (const auto & table : external_tables)
|
|
|
|
{
|
|
|
|
StoragePtr cur = table.second;
|
|
|
|
QueryProcessingStage::Enum stage = QueryProcessingStage::Complete;
|
2017-05-24 21:06:29 +00:00
|
|
|
DB::BlockInputStreams input = cur->read(cur->getColumnNamesList(), ASTPtr(), context,
|
2017-04-01 07:20:54 +00:00
|
|
|
stage, DEFAULT_BLOCK_SIZE, 1);
|
|
|
|
if (input.size() == 0)
|
|
|
|
res.push_back(std::make_pair(std::make_shared<OneBlockInputStream>(cur->getSampleBlock()), table.first));
|
|
|
|
else
|
|
|
|
res.push_back(std::make_pair(input[0], table.first));
|
|
|
|
}
|
|
|
|
external_tables_data.push_back(std::move(res));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
multiplexed_connections->sendExternalTablesData(external_tables_data);
|
2015-11-06 17:44:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Block RemoteBlockInputStream::readImpl()
|
|
|
|
{
|
2017-04-01 07:20:54 +00:00
|
|
|
if (!sent_query)
|
|
|
|
{
|
|
|
|
sendQuery();
|
|
|
|
|
2017-05-24 21:06:29 +00:00
|
|
|
if (context.getSettingsRef().skip_unavailable_shards && (0 == multiplexed_connections->size()))
|
2017-04-01 07:20:54 +00:00
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
|
|
|
while (true)
|
|
|
|
{
|
|
|
|
if (isCancelled())
|
|
|
|
return Block();
|
|
|
|
|
|
|
|
Connection::Packet packet = multiplexed_connections->receivePacket();
|
|
|
|
|
|
|
|
switch (packet.type)
|
|
|
|
{
|
|
|
|
case Protocol::Server::Data:
|
|
|
|
/// If the block is not empty and is not a header block
|
|
|
|
if (packet.block && (packet.block.rows() > 0))
|
|
|
|
return packet.block;
|
|
|
|
break; /// If the block is empty - we will receive other packets before EndOfStream.
|
|
|
|
|
|
|
|
case Protocol::Server::Exception:
|
|
|
|
got_exception_from_replica = true;
|
|
|
|
packet.exception->rethrow();
|
|
|
|
break;
|
|
|
|
|
|
|
|
case Protocol::Server::EndOfStream:
|
|
|
|
if (!multiplexed_connections->hasActiveConnections())
|
|
|
|
{
|
|
|
|
finished = true;
|
|
|
|
return Block();
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case Protocol::Server::Progress:
|
|
|
|
/** We use the progress from a remote server.
|
|
|
|
* We also include in ProcessList,
|
|
|
|
* and we use it to check
|
|
|
|
* constraints (for example, the minimum speed of query execution)
|
|
|
|
* and quotas (for example, the number of lines to read).
|
|
|
|
*/
|
|
|
|
progressImpl(packet.progress);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case Protocol::Server::ProfileInfo:
|
|
|
|
/// Use own (client-side) info about read bytes, it is more correct info than server-side one.
|
|
|
|
info.setFrom(packet.profile_info, true);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case Protocol::Server::Totals:
|
|
|
|
totals = packet.block;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case Protocol::Server::Extremes:
|
|
|
|
extremes = packet.block;
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
got_unknown_packet_from_replica = true;
|
|
|
|
throw Exception("Unknown packet from server", ErrorCodes::UNKNOWN_PACKET_FROM_SERVER);
|
|
|
|
}
|
|
|
|
}
|
2015-11-06 17:44:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void RemoteBlockInputStream::readSuffixImpl()
|
|
|
|
{
|
2017-04-01 07:20:54 +00:00
|
|
|
/** If one of:
|
|
|
|
* - nothing started to do;
|
|
|
|
* - received all packets before EndOfStream;
|
|
|
|
* - received exception from one replica;
|
|
|
|
* - received an unknown packet from one replica;
|
|
|
|
* then you do not need to read anything.
|
|
|
|
*/
|
|
|
|
if (!isQueryPending() || hasThrownException())
|
|
|
|
return;
|
|
|
|
|
|
|
|
/** If you have not read all the data yet, but they are no longer needed.
|
|
|
|
* This may be due to the fact that the data is sufficient (for example, when using LIMIT).
|
|
|
|
*/
|
|
|
|
|
|
|
|
/// Send the request to abort the execution of the request, if not already sent.
|
|
|
|
tryCancel("Cancelling query because enough data has been read");
|
|
|
|
|
|
|
|
/// Get the remaining packages so that there is no out of sync in the connections to the replicas.
|
|
|
|
Connection::Packet packet = multiplexed_connections->drain();
|
|
|
|
switch (packet.type)
|
|
|
|
{
|
|
|
|
case Protocol::Server::EndOfStream:
|
|
|
|
finished = true;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case Protocol::Server::Exception:
|
|
|
|
got_exception_from_replica = true;
|
|
|
|
packet.exception->rethrow();
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
got_unknown_packet_from_replica = true;
|
|
|
|
throw Exception("Unknown packet from server", ErrorCodes::UNKNOWN_PACKET_FROM_SERVER);
|
|
|
|
}
|
2015-11-06 17:44:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void RemoteBlockInputStream::createMultiplexedConnections()
|
|
|
|
{
|
2017-05-24 21:06:29 +00:00
|
|
|
Settings * multiplexed_connections_settings = send_settings ? &context.getSettingsRef() : nullptr;
|
2017-04-17 16:16:04 +00:00
|
|
|
const QualifiedTableName * main_table_ptr = main_table ? &main_table.value() : nullptr;
|
2017-04-01 07:20:54 +00:00
|
|
|
if (connection != nullptr)
|
2017-04-17 16:16:04 +00:00
|
|
|
multiplexed_connections = std::make_unique<MultiplexedConnections>(
|
|
|
|
connection, multiplexed_connections_settings, throttler);
|
2017-04-01 07:20:54 +00:00
|
|
|
else if (pool != nullptr)
|
2017-04-17 16:16:04 +00:00
|
|
|
multiplexed_connections = std::make_unique<MultiplexedConnections>(
|
2017-04-19 17:40:55 +00:00
|
|
|
*pool, multiplexed_connections_settings, throttler,
|
2017-04-17 16:16:04 +00:00
|
|
|
append_extra_info, pool_mode, main_table_ptr);
|
2017-04-19 17:40:55 +00:00
|
|
|
else if (!pools.empty())
|
2017-04-17 16:16:04 +00:00
|
|
|
multiplexed_connections = std::make_unique<MultiplexedConnections>(
|
2017-04-19 17:40:55 +00:00
|
|
|
pools, multiplexed_connections_settings, throttler,
|
2017-04-17 16:16:04 +00:00
|
|
|
append_extra_info, pool_mode, main_table_ptr);
|
2017-04-01 07:20:54 +00:00
|
|
|
else
|
|
|
|
throw Exception("Internal error", ErrorCodes::LOGICAL_ERROR);
|
2015-11-06 17:44:01 +00:00
|
|
|
}
|
|
|
|
|
2017-05-24 21:06:29 +00:00
|
|
|
void RemoteBlockInputStream::init(const Settings * settings)
|
2015-11-06 17:44:01 +00:00
|
|
|
{
|
2017-05-24 21:06:29 +00:00
|
|
|
if (settings)
|
2017-04-01 07:20:54 +00:00
|
|
|
{
|
|
|
|
send_settings = true;
|
2017-05-24 21:06:29 +00:00
|
|
|
context.setSettings(*settings);
|
2017-04-01 07:20:54 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
send_settings = false;
|
2015-11-06 17:44:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void RemoteBlockInputStream::sendQuery()
|
|
|
|
{
|
2017-04-01 07:20:54 +00:00
|
|
|
createMultiplexedConnections();
|
2016-03-01 17:47:53 +00:00
|
|
|
|
2017-05-24 21:06:29 +00:00
|
|
|
if (context.getSettingsRef().skip_unavailable_shards && 0 == multiplexed_connections->size())
|
2017-04-01 07:20:54 +00:00
|
|
|
return;
|
2015-11-06 17:44:01 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
established = true;
|
2015-11-06 17:44:01 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
multiplexed_connections->sendQuery(query, "", stage, &context.getClientInfo(), true);
|
2015-11-06 17:44:01 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
established = false;
|
|
|
|
sent_query = true;
|
2015-11-06 17:44:01 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
sendExternalTables();
|
2015-11-06 17:44:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void RemoteBlockInputStream::tryCancel(const char * reason)
|
|
|
|
{
|
2017-04-01 07:20:54 +00:00
|
|
|
bool old_val = false;
|
|
|
|
if (!was_cancelled.compare_exchange_strong(old_val, true, std::memory_order_seq_cst, std::memory_order_relaxed))
|
|
|
|
return;
|
2015-11-06 17:44:01 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
LOG_TRACE(log, "(" << multiplexed_connections->dumpAddresses() << ") " << reason);
|
|
|
|
multiplexed_connections->sendCancel();
|
2015-11-06 17:44:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool RemoteBlockInputStream::isQueryPending() const
|
|
|
|
{
|
2017-04-01 07:20:54 +00:00
|
|
|
return sent_query && !finished;
|
2015-11-06 17:44:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool RemoteBlockInputStream::hasThrownException() const
|
|
|
|
{
|
2017-04-01 07:20:54 +00:00
|
|
|
return got_exception_from_replica || got_unknown_packet_from_replica;
|
2015-11-06 17:44:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|