2018-06-13 19:01:07 +00:00
|
|
|
#include "AsynchronousBlockInputStream.h"
|
2019-02-10 21:15:14 +00:00
|
|
|
#include <Common/setThreadName.h>
|
2018-06-13 19:01:07 +00:00
|
|
|
#include <Common/CurrentThread.h>
|
|
|
|
|
|
|
|
|
|
|
|
namespace DB
|
|
|
|
{
|
|
|
|
|
|
|
|
Block AsynchronousBlockInputStream::readImpl()
|
|
|
|
{
|
|
|
|
/// If there were no calculations yet, calculate the first block synchronously
|
|
|
|
if (!started)
|
|
|
|
{
|
|
|
|
calculate();
|
|
|
|
started = true;
|
|
|
|
}
|
|
|
|
else /// If the calculations are already in progress - wait for the result
|
|
|
|
pool.wait();
|
|
|
|
|
|
|
|
if (exception)
|
|
|
|
std::rethrow_exception(exception);
|
|
|
|
|
|
|
|
Block res = block;
|
|
|
|
if (!res)
|
|
|
|
return res;
|
|
|
|
|
|
|
|
/// Start the next block calculation
|
|
|
|
block.clear();
|
|
|
|
next();
|
|
|
|
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void AsynchronousBlockInputStream::next()
|
|
|
|
{
|
|
|
|
ready.reset();
|
|
|
|
|
2019-10-17 14:41:27 +00:00
|
|
|
pool.scheduleOrThrowOnError([this, thread_group = CurrentThread::getGroup()]()
|
2018-06-15 13:45:19 +00:00
|
|
|
{
|
2018-06-13 19:01:07 +00:00
|
|
|
CurrentMetrics::Increment metric_increment{CurrentMetrics::QueryThread};
|
|
|
|
|
|
|
|
try
|
|
|
|
{
|
|
|
|
if (first)
|
|
|
|
setThreadName("AsyncBlockInput");
|
2018-06-20 15:21:42 +00:00
|
|
|
|
|
|
|
/// AsynchronousBlockInputStream is used in Client which does not create queries and thread groups
|
|
|
|
if (thread_group)
|
|
|
|
CurrentThread::attachToIfDetached(thread_group);
|
2018-06-13 19:01:07 +00:00
|
|
|
}
|
|
|
|
catch (...)
|
|
|
|
{
|
|
|
|
exception = std::current_exception();
|
|
|
|
ready.set();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
calculate();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void AsynchronousBlockInputStream::calculate()
|
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
|
|
|
if (first)
|
|
|
|
{
|
|
|
|
first = false;
|
|
|
|
children.back()->readPrefix();
|
|
|
|
}
|
|
|
|
|
|
|
|
block = children.back()->read();
|
|
|
|
}
|
|
|
|
catch (...)
|
|
|
|
{
|
|
|
|
exception = std::current_exception();
|
|
|
|
}
|
|
|
|
|
|
|
|
ready.set();
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|