| 1 | #pragma once | 
|---|---|
| 2 | |
| 3 | #include <thread> | 
| 4 | #include <mutex> | 
| 5 | #include <condition_variable> | 
| 6 | #include <unordered_map> | 
| 7 | #include <string> | 
| 8 | #include <Common/ThreadPool.h> | 
| 9 | |
| 10 | |
| 11 | namespace DB | 
| 12 | { | 
| 13 | |
| 14 | class Context; | 
| 15 | |
| 16 | |
| 17 | /** Periodically (each minute, starting at 30 seconds offset) | 
| 18 | * calculates and updates some metrics, | 
| 19 | * that are not updated automatically (so, need to be asynchronously calculated). | 
| 20 | */ | 
| 21 | class AsynchronousMetrics | 
| 22 | { | 
| 23 | public: | 
| 24 | AsynchronousMetrics(Context & context_) | 
| 25 | : context(context_), thread([this] { run(); }) | 
| 26 | { | 
| 27 | } | 
| 28 | |
| 29 | ~AsynchronousMetrics(); | 
| 30 | |
| 31 | using Value = double; | 
| 32 | using Container = std::unordered_map<std::string, Value>; | 
| 33 | |
| 34 | /// Returns copy of all values. | 
| 35 | Container getValues() const; | 
| 36 | |
| 37 | private: | 
| 38 | Context & context; | 
| 39 | |
| 40 | bool quit {false}; | 
| 41 | std::mutex wait_mutex; | 
| 42 | std::condition_variable wait_cond; | 
| 43 | |
| 44 | Container container; | 
| 45 | mutable std::mutex container_mutex; | 
| 46 | |
| 47 | ThreadFromGlobalPool thread; | 
| 48 | |
| 49 | void run(); | 
| 50 | void update(); | 
| 51 | |
| 52 | void set(const std::string & name, Value value); | 
| 53 | }; | 
| 54 | |
| 55 | } | 
| 56 | 
