aboutsummaryrefslogtreecommitdiffstats
path: root/contrib/clickhouse/src/IO/SharedThreadPools.h
blob: 188a2a4f003cb567148c5f5ea00d6118a02f2d96 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
#pragma once

#include <base/types.h>
#include <Common/ThreadPool_fwd.h>
#include <Common/CurrentMetrics.h>

#include <cstdlib>
#include <memory>
#include <mutex>

namespace DB
{

class StaticThreadPool
{
public:
    StaticThreadPool(
        const String & name_,
        CurrentMetrics::Metric threads_metric_,
        CurrentMetrics::Metric threads_active_metric_);

    ThreadPool & get();

    void initialize(size_t max_threads, size_t max_free_threads, size_t queue_size);
    void reloadConfiguration(size_t max_threads, size_t max_free_threads, size_t queue_size);

    /// At runtime we can increase the number of threads up the specified limit
    /// This is needed to utilize as much a possible resources to accomplish some task.
    void setMaxTurboThreads(size_t max_threads_turbo_);
    void enableTurboMode();
    void disableTurboMode();

private:
    const String name;
    const CurrentMetrics::Metric threads_metric;
    const CurrentMetrics::Metric threads_active_metric;

    std::unique_ptr<ThreadPool> instance;
    std::mutex mutex;
    size_t max_threads_turbo = 0;
    size_t max_threads_normal = 0;
    /// If this counter is > 0 - this specific mode is enabled
    size_t turbo_mode_enabled = 0;
};

/// ThreadPool used for the IO.
StaticThreadPool & getIOThreadPool();

/// ThreadPool used for the Backup IO.
StaticThreadPool & getBackupsIOThreadPool();

/// ThreadPool used for the loading of Outdated data parts for MergeTree tables.
StaticThreadPool & getActivePartsLoadingThreadPool();

/// ThreadPool used for deleting data parts for MergeTree tables.
StaticThreadPool & getPartsCleaningThreadPool();

/// This ThreadPool is used for the loading of Outdated data parts for MergeTree tables.
/// Normally we will just load Outdated data parts concurrently in background, but in
/// case when we need to synchronously wait for the loading to be finished, we can increase
/// the number of threads by calling enableTurboMode() :-)
StaticThreadPool & getOutdatedPartsLoadingThreadPool();

}