2013-09-03 20:21:28 +00:00
|
|
|
#pragma once
|
|
|
|
|
2014-02-12 17:31:02 +00:00
|
|
|
#include <map>
|
2013-09-03 20:21:28 +00:00
|
|
|
#include <list>
|
2015-06-21 06:06:04 +00:00
|
|
|
#include <memory>
|
2016-05-28 10:15:36 +00:00
|
|
|
#include <mutex>
|
2013-09-07 04:54:59 +00:00
|
|
|
#include <Poco/Condition.h>
|
2013-11-03 05:32:42 +00:00
|
|
|
#include <Poco/Net/IPAddress.h>
|
2015-10-05 00:44:40 +00:00
|
|
|
#include <DB/Common/Stopwatch.h>
|
2013-09-07 04:54:59 +00:00
|
|
|
#include <DB/Core/Defines.h>
|
2014-10-25 18:33:52 +00:00
|
|
|
#include <DB/Core/Progress.h>
|
2015-10-05 01:35:28 +00:00
|
|
|
#include <DB/Common/Exception.h>
|
2014-05-03 22:57:43 +00:00
|
|
|
#include <DB/Common/MemoryTracker.h>
|
2013-09-03 20:21:28 +00:00
|
|
|
#include <DB/IO/WriteHelpers.h>
|
2015-06-21 06:06:04 +00:00
|
|
|
#include <DB/Interpreters/QueryPriorities.h>
|
2016-10-24 21:40:39 +00:00
|
|
|
#include <DB/Interpreters/ClientInfo.h>
|
2015-09-04 20:52:00 +00:00
|
|
|
#include <DB/Storages/IStorage.h>
|
2016-01-20 21:32:01 +00:00
|
|
|
#include <DB/Common/CurrentMetrics.h>
|
2013-09-03 20:21:28 +00:00
|
|
|
|
|
|
|
|
2016-10-24 04:06:27 +00:00
|
|
|
namespace CurrentMetrics
|
|
|
|
{
|
|
|
|
extern const Metric Query;
|
|
|
|
}
|
|
|
|
|
2013-09-03 20:21:28 +00:00
|
|
|
namespace DB
|
|
|
|
{
|
|
|
|
|
2016-10-24 04:06:27 +00:00
|
|
|
/** List of currently executing queries.
|
|
|
|
* Also implements limit on their number.
|
2013-09-03 20:21:28 +00:00
|
|
|
*/
|
|
|
|
|
2016-10-24 04:06:27 +00:00
|
|
|
/** Information of process list element.
|
|
|
|
* To output in SHOW PROCESSLIST query. Does not contain any complex objects, that do something on copy or destructor.
|
2016-01-20 21:32:01 +00:00
|
|
|
*/
|
|
|
|
struct ProcessInfo
|
|
|
|
{
|
|
|
|
String query;
|
|
|
|
double elapsed_seconds;
|
|
|
|
size_t rows;
|
|
|
|
size_t bytes;
|
|
|
|
size_t total_rows;
|
|
|
|
Int64 memory_usage;
|
2016-10-24 21:40:39 +00:00
|
|
|
ClientInfo client_info;
|
2016-01-20 21:32:01 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
2016-10-24 04:06:27 +00:00
|
|
|
/// Query and information about its execution.
|
2015-04-16 06:12:35 +00:00
|
|
|
struct ProcessListElement
|
2013-09-03 20:21:28 +00:00
|
|
|
{
|
2015-04-16 06:12:35 +00:00
|
|
|
String query;
|
2016-10-24 21:40:39 +00:00
|
|
|
ClientInfo client_info;
|
2014-09-10 11:34:26 +00:00
|
|
|
|
2015-04-16 06:12:35 +00:00
|
|
|
Stopwatch watch;
|
2013-09-03 20:21:28 +00:00
|
|
|
|
2015-04-16 06:12:35 +00:00
|
|
|
Progress progress;
|
2014-02-12 17:31:02 +00:00
|
|
|
|
2015-04-16 06:12:35 +00:00
|
|
|
MemoryTracker memory_tracker;
|
2014-05-03 22:57:43 +00:00
|
|
|
|
2015-06-21 06:06:04 +00:00
|
|
|
QueryPriorities::Handle priority_handle;
|
|
|
|
|
2016-01-20 21:32:01 +00:00
|
|
|
CurrentMetrics::Increment num_queries {CurrentMetrics::Query};
|
|
|
|
|
2015-04-16 06:12:35 +00:00
|
|
|
bool is_cancelled = false;
|
2014-09-10 11:34:26 +00:00
|
|
|
|
2016-10-24 04:06:27 +00:00
|
|
|
/// Temporary tables could be registered here. Modify under mutex.
|
2015-09-04 20:52:00 +00:00
|
|
|
Tables temporary_tables;
|
|
|
|
|
2013-11-03 05:32:42 +00:00
|
|
|
|
2016-10-24 21:40:39 +00:00
|
|
|
ProcessListElement(
|
|
|
|
const String & query_,
|
|
|
|
const ClientInfo & client_info_,
|
|
|
|
size_t max_memory_usage,
|
|
|
|
double memory_tracker_fault_probability,
|
2015-12-30 15:39:11 +00:00
|
|
|
QueryPriorities::Handle && priority_handle_)
|
2016-10-24 21:40:39 +00:00
|
|
|
: query(query_), client_info(client_info_), memory_tracker(max_memory_usage),
|
2015-06-21 06:06:04 +00:00
|
|
|
priority_handle(std::move(priority_handle_))
|
2015-04-16 06:12:35 +00:00
|
|
|
{
|
2016-01-13 04:30:21 +00:00
|
|
|
memory_tracker.setDescription("(for query)");
|
2015-04-16 06:12:35 +00:00
|
|
|
current_memory_tracker = &memory_tracker;
|
2015-12-23 07:39:28 +00:00
|
|
|
|
|
|
|
if (memory_tracker_fault_probability)
|
|
|
|
memory_tracker.setFaultProbability(memory_tracker_fault_probability);
|
2015-04-16 06:12:35 +00:00
|
|
|
}
|
2014-05-03 22:57:43 +00:00
|
|
|
|
2015-04-16 06:12:35 +00:00
|
|
|
~ProcessListElement()
|
|
|
|
{
|
|
|
|
current_memory_tracker = nullptr;
|
|
|
|
}
|
2013-11-03 05:32:42 +00:00
|
|
|
|
2015-04-16 06:12:35 +00:00
|
|
|
bool update(const Progress & value)
|
|
|
|
{
|
|
|
|
progress.incrementPiecewiseAtomically(value);
|
2015-06-21 06:06:04 +00:00
|
|
|
|
|
|
|
if (priority_handle)
|
2016-10-24 04:06:27 +00:00
|
|
|
priority_handle->waitIfNeed(std::chrono::seconds(1)); /// NOTE Could make timeout customizable.
|
2015-06-21 06:06:04 +00:00
|
|
|
|
2015-04-16 06:12:35 +00:00
|
|
|
return !is_cancelled;
|
|
|
|
}
|
2016-01-20 21:32:01 +00:00
|
|
|
|
|
|
|
ProcessInfo getInfo() const
|
|
|
|
{
|
2016-10-24 21:40:39 +00:00
|
|
|
ProcessInfo res;
|
|
|
|
|
|
|
|
res.query = query;
|
|
|
|
res.client_info = client_info;
|
|
|
|
res.elapsed_seconds = watch.elapsedSeconds();
|
|
|
|
res.rows = progress.rows;
|
|
|
|
res.bytes = progress.bytes;
|
|
|
|
res.total_rows = progress.total_rows;
|
|
|
|
res.memory_usage = memory_tracker.get();
|
|
|
|
|
|
|
|
return res;
|
2016-01-20 21:32:01 +00:00
|
|
|
}
|
2015-04-16 06:12:35 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
2016-10-24 04:06:27 +00:00
|
|
|
/// Data about queries for one user.
|
2015-12-30 15:39:11 +00:00
|
|
|
struct ProcessListForUser
|
|
|
|
{
|
|
|
|
/// Query_id -> ProcessListElement *
|
|
|
|
using QueryToElement = std::unordered_map<String, ProcessListElement *>;
|
|
|
|
QueryToElement queries;
|
2016-01-13 02:38:30 +00:00
|
|
|
|
2016-10-24 04:06:27 +00:00
|
|
|
/// Limit and counter for memory of all simultaneously running queries of single user.
|
2016-01-13 02:38:30 +00:00
|
|
|
MemoryTracker user_memory_tracker;
|
2015-12-30 15:39:11 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
2015-06-21 06:06:04 +00:00
|
|
|
class ProcessList;
|
|
|
|
|
|
|
|
|
2016-10-24 04:06:27 +00:00
|
|
|
/// Keeps iterator to process list and removes element in destructor.
|
2015-06-21 06:06:04 +00:00
|
|
|
class ProcessListEntry
|
|
|
|
{
|
|
|
|
private:
|
|
|
|
using Container = std::list<ProcessListElement>;
|
|
|
|
|
|
|
|
ProcessList & parent;
|
|
|
|
Container::iterator it;
|
|
|
|
public:
|
|
|
|
ProcessListEntry(ProcessList & parent_, Container::iterator it_)
|
|
|
|
: parent(parent_), it(it_) {}
|
|
|
|
|
|
|
|
~ProcessListEntry();
|
|
|
|
|
|
|
|
ProcessListElement * operator->() { return &*it; }
|
|
|
|
const ProcessListElement * operator->() const { return &*it; }
|
|
|
|
|
|
|
|
ProcessListElement & get() { return *it; }
|
|
|
|
const ProcessListElement & get() const { return *it; }
|
|
|
|
};
|
|
|
|
|
|
|
|
|
2015-04-16 06:12:35 +00:00
|
|
|
class ProcessList
|
|
|
|
{
|
2015-06-21 06:06:04 +00:00
|
|
|
friend class ProcessListEntry;
|
2015-04-16 06:12:35 +00:00
|
|
|
public:
|
|
|
|
using Element = ProcessListElement;
|
2015-06-21 06:06:04 +00:00
|
|
|
using Entry = ProcessListEntry;
|
2013-09-03 20:21:28 +00:00
|
|
|
|
2016-10-24 04:06:27 +00:00
|
|
|
/// list, for iterators not to invalidate. NOTE: could replace with cyclic buffer, but not worth.
|
2015-06-21 06:06:04 +00:00
|
|
|
using Container = std::list<Element>;
|
2016-01-20 21:32:01 +00:00
|
|
|
using Info = std::vector<ProcessInfo>;
|
2016-01-13 02:38:30 +00:00
|
|
|
/// User -> queries
|
|
|
|
using UserToQueries = std::unordered_map<String, ProcessListForUser>;
|
2013-09-03 20:21:28 +00:00
|
|
|
|
|
|
|
private:
|
2016-05-28 10:15:36 +00:00
|
|
|
mutable std::mutex mutex;
|
2016-10-24 04:06:27 +00:00
|
|
|
mutable Poco::Condition have_space; /// Number of currently running queries has become less than maximum.
|
2014-09-10 11:34:26 +00:00
|
|
|
|
2015-06-21 06:06:04 +00:00
|
|
|
Container cont;
|
2016-10-24 04:06:27 +00:00
|
|
|
size_t cur_size; /// In C++03 or C++11 and old ABI, std::list::size is not O(1).
|
|
|
|
size_t max_size; /// 0 means no limit. Otherwise, when limit exceeded, an exception is thrown.
|
2014-02-14 13:22:45 +00:00
|
|
|
UserToQueries user_to_queries;
|
2015-06-21 06:06:04 +00:00
|
|
|
QueryPriorities priorities;
|
2013-09-03 20:21:28 +00:00
|
|
|
|
2016-10-24 04:06:27 +00:00
|
|
|
/// Limit and counter for memory of all simultaneously running queries.
|
2016-01-13 03:59:24 +00:00
|
|
|
MemoryTracker total_memory_tracker;
|
|
|
|
|
2013-09-03 20:21:28 +00:00
|
|
|
public:
|
|
|
|
ProcessList(size_t max_size_ = 0) : cur_size(0), max_size(max_size_) {}
|
|
|
|
|
2016-01-13 03:59:24 +00:00
|
|
|
using EntryPtr = std::shared_ptr<ProcessListEntry>;
|
2013-09-03 20:21:28 +00:00
|
|
|
|
2016-10-24 04:06:27 +00:00
|
|
|
/** Register running query. Returns refcounted object, that will remove element from list in destructor.
|
|
|
|
* If too much running queries - wait for not more than specified (see settings) amount of time.
|
|
|
|
* If timeout is passed - throw an exception.
|
2013-09-07 04:54:59 +00:00
|
|
|
*/
|
2016-10-24 21:40:39 +00:00
|
|
|
EntryPtr insert(const String & query_, const ClientInfo & client_info, const Settings & settings);
|
2013-09-03 20:21:28 +00:00
|
|
|
|
2016-10-24 04:06:27 +00:00
|
|
|
/// Number of currently executing queries.
|
2014-09-12 16:05:29 +00:00
|
|
|
size_t size() const { return cur_size; }
|
2013-09-03 20:21:28 +00:00
|
|
|
|
2016-10-24 04:06:27 +00:00
|
|
|
/// Get current state of process list.
|
2016-01-20 21:32:01 +00:00
|
|
|
Info getInfo() const
|
2013-09-03 20:21:28 +00:00
|
|
|
{
|
2016-05-28 10:15:36 +00:00
|
|
|
std::lock_guard<std::mutex> lock(mutex);
|
2016-01-20 21:32:01 +00:00
|
|
|
|
|
|
|
Info res;
|
|
|
|
res.reserve(cur_size);
|
|
|
|
for (const auto & elem : cont)
|
|
|
|
res.emplace_back(elem.getInfo());
|
|
|
|
|
|
|
|
return res;
|
2013-09-03 20:21:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void setMaxSize(size_t max_size_)
|
|
|
|
{
|
2016-05-28 10:15:36 +00:00
|
|
|
std::lock_guard<std::mutex> lock(mutex);
|
2013-09-03 20:21:28 +00:00
|
|
|
max_size = max_size_;
|
|
|
|
}
|
2015-09-04 20:52:00 +00:00
|
|
|
|
2016-10-24 04:06:27 +00:00
|
|
|
/// Register temporary table. Then it is accessible by query_id and name.
|
2015-09-04 20:52:00 +00:00
|
|
|
void addTemporaryTable(ProcessListElement & elem, const String & table_name, StoragePtr storage);
|
|
|
|
|
2016-10-24 04:06:27 +00:00
|
|
|
/// Find temporary table by query_id and name. NOTE: doesn't work fine if there are many queries with same query_id.
|
2015-09-04 20:52:00 +00:00
|
|
|
StoragePtr tryGetTemporaryTable(const String & query_id, const String & table_name) const;
|
2013-09-03 20:21:28 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
}
|