2015-06-21 06:06:04 +00:00
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#include <map>
|
|
|
|
#include <mutex>
|
|
|
|
#include <condition_variable>
|
|
|
|
#include <memory>
|
|
|
|
#include <chrono>
|
2017-04-01 09:19:00 +00:00
|
|
|
#include <Common/CurrentMetrics.h>
|
2015-06-21 06:06:04 +00:00
|
|
|
|
2016-10-24 04:06:27 +00:00
|
|
|
namespace CurrentMetrics
|
|
|
|
{
|
2017-04-01 07:20:54 +00:00
|
|
|
extern const Metric QueryPreempted;
|
2016-10-24 04:06:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
namespace DB
|
|
|
|
{
|
|
|
|
|
|
|
|
/** Implements query priorities in very primitive way.
|
|
|
|
* Allows to freeze query execution if at least one query of higher priority is executed.
|
2015-06-21 06:06:04 +00:00
|
|
|
*
|
2016-10-24 04:06:27 +00:00
|
|
|
* Priority value is integer, smaller means higher priority.
|
2015-06-21 06:06:04 +00:00
|
|
|
*
|
2016-10-24 04:06:27 +00:00
|
|
|
* Priority 0 is special - queries with that priority is always executed,
|
|
|
|
* not depends on other queries and not affect other queries.
|
|
|
|
* Thus 0 means - don't use priorities.
|
2015-06-21 06:06:04 +00:00
|
|
|
*
|
2016-10-24 04:06:27 +00:00
|
|
|
* NOTE Possibilities for improvement:
|
|
|
|
* - implement limit on maximum number of running queries with same priority.
|
2015-06-21 06:06:04 +00:00
|
|
|
*/
|
|
|
|
class QueryPriorities
|
|
|
|
{
|
|
|
|
public:
|
2017-04-01 07:20:54 +00:00
|
|
|
using Priority = int;
|
2015-06-21 06:06:04 +00:00
|
|
|
|
|
|
|
private:
|
2017-04-01 07:20:54 +00:00
|
|
|
friend struct Handle;
|
|
|
|
|
|
|
|
using Count = int;
|
|
|
|
|
|
|
|
/// Number of currently running queries for each priority.
|
|
|
|
using Container = std::map<Priority, Count>;
|
|
|
|
|
|
|
|
std::mutex mutex;
|
|
|
|
std::condition_variable condvar;
|
|
|
|
Container container;
|
|
|
|
|
|
|
|
|
|
|
|
/** If there are higher priority queries - sleep until they are finish or timeout happens.
|
|
|
|
*/
|
|
|
|
template <typename Duration>
|
2019-07-02 20:21:31 +00:00
|
|
|
void waitIfNeed(Priority priority, Duration timeout)
|
2017-04-01 07:20:54 +00:00
|
|
|
{
|
|
|
|
if (0 == priority)
|
2019-07-02 20:21:31 +00:00
|
|
|
return;
|
2017-07-09 00:33:03 +00:00
|
|
|
|
2019-01-02 06:44:36 +00:00
|
|
|
std::unique_lock lock(mutex);
|
2017-04-01 07:20:54 +00:00
|
|
|
|
2019-07-02 20:21:31 +00:00
|
|
|
/// Is there at least one more priority query?
|
|
|
|
bool found = false;
|
|
|
|
for (const auto & value : container)
|
2017-04-01 07:20:54 +00:00
|
|
|
{
|
2019-07-02 20:21:31 +00:00
|
|
|
if (value.first >= priority)
|
|
|
|
break;
|
2017-04-01 07:20:54 +00:00
|
|
|
|
2019-07-02 20:21:31 +00:00
|
|
|
if (value.second > 0)
|
2017-07-09 00:33:03 +00:00
|
|
|
{
|
2019-07-02 20:21:31 +00:00
|
|
|
found = true;
|
|
|
|
break;
|
2017-07-09 00:33:03 +00:00
|
|
|
}
|
2017-04-01 07:20:54 +00:00
|
|
|
}
|
2019-07-02 20:21:31 +00:00
|
|
|
|
|
|
|
if (!found)
|
|
|
|
return;
|
|
|
|
|
|
|
|
CurrentMetrics::Increment metric_increment{CurrentMetrics::QueryPreempted};
|
|
|
|
|
|
|
|
/// Spurious wakeups are Ok. We allow to wait less than requested.
|
|
|
|
condvar.wait_for(lock, timeout);
|
2017-04-01 07:20:54 +00:00
|
|
|
}
|
2015-06-21 06:06:04 +00:00
|
|
|
|
|
|
|
public:
|
2017-04-01 07:20:54 +00:00
|
|
|
struct HandleImpl
|
|
|
|
{
|
|
|
|
private:
|
|
|
|
QueryPriorities & parent;
|
|
|
|
QueryPriorities::Container::value_type & value;
|
|
|
|
|
|
|
|
public:
|
|
|
|
HandleImpl(QueryPriorities & parent_, QueryPriorities::Container::value_type & value_)
|
|
|
|
: parent(parent_), value(value_) {}
|
|
|
|
|
|
|
|
~HandleImpl()
|
|
|
|
{
|
|
|
|
{
|
2019-01-02 06:44:36 +00:00
|
|
|
std::lock_guard lock(parent.mutex);
|
2017-04-01 07:20:54 +00:00
|
|
|
--value.second;
|
|
|
|
}
|
|
|
|
parent.condvar.notify_all();
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename Duration>
|
2019-07-02 20:21:31 +00:00
|
|
|
void waitIfNeed(Duration timeout)
|
2017-04-01 07:20:54 +00:00
|
|
|
{
|
2019-07-02 20:21:31 +00:00
|
|
|
parent.waitIfNeed(value.first, timeout);
|
2017-04-01 07:20:54 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
using Handle = std::shared_ptr<HandleImpl>;
|
|
|
|
|
|
|
|
/** Register query with specified priority.
|
|
|
|
* Returns an object that remove record in destructor.
|
|
|
|
*/
|
|
|
|
Handle insert(Priority priority)
|
|
|
|
{
|
|
|
|
if (0 == priority)
|
|
|
|
return {};
|
|
|
|
|
2019-01-02 06:44:36 +00:00
|
|
|
std::lock_guard lock(mutex);
|
2017-04-01 07:20:54 +00:00
|
|
|
auto it = container.emplace(priority, 0).first;
|
|
|
|
++it->second;
|
|
|
|
return std::make_shared<HandleImpl>(*this, *it);
|
|
|
|
}
|
2015-06-21 06:06:04 +00:00
|
|
|
};
|
2016-10-24 04:06:27 +00:00
|
|
|
|
|
|
|
}
|