ClickHouse/src/Dictionaries/CacheDictionaryUpdateQueue.h

174 lines
6.0 KiB
C++
Raw Normal View History

2021-02-16 21:33:02 +00:00
#pragma once
#include <atomic>
#include <mutex>
#include <shared_mutex>
#include <utility>
#include <vector>
#include <functional>
#include <Common/ThreadPool.h>
#include <Common/ConcurrentBoundedQueue.h>
#include <Common/CurrentMetrics.h>
#include <Common/PODArray.h>
#include <Common/HashTable/HashMap.h>
#include <Columns/IColumn.h>
#include <Dictionaries/ICacheDictionaryStorage.h>
namespace CurrentMetrics
{
extern const Metric CacheDictionaryUpdateQueueBatches;
extern const Metric CacheDictionaryUpdateQueueKeys;
}
namespace DB
{
2021-02-17 11:48:06 +00:00
/** This class is passed between update queue and update queue client during update.
For simple keys we pass simple keys.
For complex keys we pass complex keys columns and requested rows to update.
During update cache dictionary should fill requested_keys_to_fetched_columns_during_update_index and
fetched_columns_during_update.
For complex key to extend lifetime of key complex key arena should be used.
*/
2021-02-16 21:33:02 +00:00
template <DictionaryKeyType dictionary_key_type>
class CacheDictionaryUpdateUnit
{
public:
using KeyType = std::conditional_t<dictionary_key_type == DictionaryKeyType::simple, UInt64, StringRef>;
2021-02-17 11:48:06 +00:00
/// Constructor for complex keys update request
2021-02-16 21:33:02 +00:00
explicit CacheDictionaryUpdateUnit(
const Columns & key_columns_,
const PaddedPODArray<KeyState> & key_index_to_state_from_storage_,
const DictionaryStorageFetchRequest & request_,
2021-03-04 14:34:39 +00:00
size_t keys_to_update_size_)
: key_columns(key_columns_)
, key_index_to_state(key_index_to_state_from_storage_.begin(), key_index_to_state_from_storage_.end())
2021-02-16 21:33:02 +00:00
, request(request_)
2021-03-04 14:34:39 +00:00
, keys_to_update_size(keys_to_update_size_)
, alive_keys(CurrentMetrics::CacheDictionaryUpdateQueueKeys, keys_to_update_size)
2021-02-16 21:33:02 +00:00
{}
2021-03-04 14:34:39 +00:00
CacheDictionaryUpdateUnit()
: keys_to_update_size(0)
, alive_keys(CurrentMetrics::CacheDictionaryUpdateQueueKeys, 0)
{}
2021-02-16 21:33:02 +00:00
const Columns key_columns;
const PaddedPODArray<KeyState> key_index_to_state;
2021-02-16 21:33:02 +00:00
const DictionaryStorageFetchRequest request;
2021-03-04 14:34:39 +00:00
const size_t keys_to_update_size;
2021-02-16 21:33:02 +00:00
HashMap<KeyType, size_t> requested_keys_to_fetched_columns_during_update_index;
MutableColumns fetched_columns_during_update;
2021-03-31 21:12:21 +00:00
2021-02-27 20:39:34 +00:00
/// Complex keys are serialized in this arena
2021-03-31 21:12:21 +00:00
DictionaryKeysArenaHolder<dictionary_key_type> complex_keys_arena_holder;
2021-02-16 21:33:02 +00:00
private:
template <DictionaryKeyType>
friend class CacheDictionaryUpdateQueue;
std::atomic<bool> is_done{false};
std::exception_ptr current_exception{nullptr};
/// While UpdateUnit is alive, it is accounted in update_queue size.
CurrentMetrics::Increment alive_batch{CurrentMetrics::CacheDictionaryUpdateQueueBatches};
CurrentMetrics::Increment alive_keys;
};
template <DictionaryKeyType dictionary_key_type>
using CacheDictionaryUpdateUnitPtr = std::shared_ptr<CacheDictionaryUpdateUnit<dictionary_key_type>>;
extern template class CacheDictionaryUpdateUnit<DictionaryKeyType::simple>;
extern template class CacheDictionaryUpdateUnit<DictionaryKeyType::complex>;
struct CacheDictionaryUpdateQueueConfiguration
{
2021-02-17 11:48:06 +00:00
/// Size of update queue
2021-02-16 21:33:02 +00:00
const size_t max_update_queue_size;
2021-02-17 11:48:06 +00:00
/// Size in thead pool of update queue
const size_t max_threads_for_updates;
/// Timeout for trying to push update unit into queue
2021-02-16 21:33:02 +00:00
const size_t update_queue_push_timeout_milliseconds;
2021-02-17 11:48:06 +00:00
/// Timeout during sync waititing of update unit
2021-02-16 21:33:02 +00:00
const size_t query_wait_timeout_milliseconds;
};
2021-02-17 11:48:06 +00:00
/** Responsibility of this class is to provide asynchronous and synchronous update support for CacheDictionary
It is responsibility of CacheDictionary to perform update with UpdateUnit using UpdateFunction.
*/
2021-02-16 21:33:02 +00:00
template <DictionaryKeyType dictionary_key_type>
class CacheDictionaryUpdateQueue
{
public:
2021-02-17 11:48:06 +00:00
/// Client of update queue must provide this function in constructor and perform update using update unit.
2021-03-01 22:23:14 +00:00
using UpdateFunction = std::function<void (CacheDictionaryUpdateUnitPtr<dictionary_key_type>)>;
2021-02-16 21:33:02 +00:00
static_assert(dictionary_key_type != DictionaryKeyType::range, "Range key type is not supported by CacheDictionaryUpdateQueue");
CacheDictionaryUpdateQueue(
String dictionary_name_for_logs_,
CacheDictionaryUpdateQueueConfiguration configuration_,
UpdateFunction && update_func_);
~CacheDictionaryUpdateQueue();
2021-02-17 11:48:06 +00:00
/// Get configuration that was passed to constructor
const CacheDictionaryUpdateQueueConfiguration & getConfiguration() const { return configuration; }
2021-02-16 21:33:02 +00:00
2021-02-17 11:48:06 +00:00
/// Is queue finished
bool isFinished() const { return finished; }
2021-02-16 21:33:02 +00:00
2021-02-17 11:48:06 +00:00
/// Synchronous wait for update queue to stop
2021-02-16 21:33:02 +00:00
void stopAndWait();
2021-02-17 11:48:06 +00:00
/** Try to add update unit into queue.
If queue is full and oush cannot be performed in update_queue_push_timeout_milliseconds from configuration
an exception will be thrown.
If queue already finished an exception will be thrown.
*/
2021-02-16 21:33:02 +00:00
void tryPushToUpdateQueueOrThrow(CacheDictionaryUpdateUnitPtr<dictionary_key_type> & update_unit_ptr);
2021-02-17 11:48:06 +00:00
/** Try to synchronously wait for update completion.
If exception was passed from update function during update it will be rethrowed.
If update will not be finished in query_wait_timeout_milliseconds from configuration
an exception will be thrown.
If queue already finished an exception will be thrown.
*/
2021-02-16 21:33:02 +00:00
void waitForCurrentUpdateFinish(CacheDictionaryUpdateUnitPtr<dictionary_key_type> & update_unit_ptr) const;
private:
void updateThreadFunction();
using UpdateQueue = ConcurrentBoundedQueue<CacheDictionaryUpdateUnitPtr<dictionary_key_type>>;
String dictionary_name_for_logs;
CacheDictionaryUpdateQueueConfiguration configuration;
UpdateFunction update_func;
UpdateQueue update_queue;
ThreadPool update_pool;
mutable std::mutex update_mutex;
mutable std::condition_variable is_update_finished;
std::atomic<bool> finished{false};
};
extern template class CacheDictionaryUpdateQueue<DictionaryKeyType::simple>;
extern template class CacheDictionaryUpdateQueue<DictionaryKeyType::complex>;
}