summaryrefslogtreecommitdiff
path: root/src/mongo/executor/task_executor_pool.h
blob: b70a24c705fbad6e3c38b55dadec51a17d206cb3 (plain)
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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
/**
 *    Copyright (C) 2018-present MongoDB, Inc.
 *
 *    This program is free software: you can redistribute it and/or modify
 *    it under the terms of the Server Side Public License, version 1,
 *    as published by MongoDB, Inc.
 *
 *    This program is distributed in the hope that it will be useful,
 *    but WITHOUT ANY WARRANTY; without even the implied warranty of
 *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *    Server Side Public License for more details.
 *
 *    You should have received a copy of the Server Side Public License
 *    along with this program. If not, see
 *    <http://www.mongodb.com/licensing/server-side-public-license>.
 *
 *    As a special exception, the copyright holders give permission to link the
 *    code of portions of this program with the OpenSSL library under certain
 *    conditions as described in each individual source file and distribute
 *    linked combinations including the program with the OpenSSL library. You
 *    must comply with the Server Side Public License in all respects for
 *    all of the code used other than as permitted herein. If you modify file(s)
 *    with this exception, you may extend this exception to your version of the
 *    file(s), but you are not obligated to do so. If you do not wish to do so,
 *    delete this exception statement from your version. If you delete this
 *    exception statement from all source files in the program, then also delete
 *    it in the license file.
 */

#pragma once

#include <memory>
#include <vector>

#include "mongo/platform/atomic_word.h"

namespace mongo {
namespace executor {

struct ConnectionPoolStats;
class TaskExecutor;

/**
 * Represents a pool of TaskExecutors. Work which requires a TaskExecutor can ask for an executor
 * from the pool. This allows for work to be distributed across several executors.
 *
 * The TaskExecutor and its owned NetworkInterface can become a performance bottleneck, so this
 * pooling approach provides better scalability under concurrent query workloads.
 *
 * Initialization and shutdown methods are not thread-safe, but getArbitraryExecutor() may be called
 * concurrently by multiple threads.
 */
class TaskExecutorPool final {
public:
    /**
     * Based on properties of the system (e.g. the number of cores), suggests a number of executors
     * to add to the pool.
     */
    static size_t getSuggestedPoolSize();

    /**
     * Initializes the underlying executors. This method may be called at most once for the lifetime
     * of an executor.
     *
     * May only be called after populating the pool with addExecutors().
     */
    void startup();

    /**
     * Shuts down all underlying executors and waits for them to finish. May block.
     *
     * If startup() has been called, this method must eventually be called exactly once. Must be
     * called after startup().
     */
    void shutdownAndJoin();

    /**
     * Adds 'executors' and 'fixedExecutor' to the pool. May be called at most once to initialize an
     * empty pool.
     */
    void addExecutors(std::vector<std::unique_ptr<TaskExecutor>> executors,
                      std::unique_ptr<TaskExecutor> fixedExecutor);

    /**
     * Returns a pointer to one of the executors in the pool. Two calls to this method may return
     * different executors. Invalid to call if the pool has not been initialized with
     * addExecutors().
     *
     * Use this method if you need a TaskExecutor for performing performance-critical work.
     *
     * Thread-safe.
     */
    TaskExecutor* getArbitraryExecutor();

    /**
     * Returns a pointer to the pool's fixed executor. Every call to this method will return the
     * same executor. Invalid to call if the fixed executor has not been initialized with
     * addExecutors().
     *
     * Use this method if you are *not* using the TaskExecutor for performance-critical work.
     *
     * Thread-safe.
     */
    TaskExecutor* getFixedExecutor();

    /**
     * Appends connection information from all of the executors in the pool.
     *
     * NOTE: this method returns approximate stats. To avoid blocking operations on the
     * pool, we don't lock for appendConnectionStats, so data gathered across connection pools
     * will be from slightly different points in time.
     */
    void appendConnectionStats(ConnectionPoolStats* stats) const;

private:
    AtomicWord<unsigned> _counter;

    std::vector<std::unique_ptr<TaskExecutor>> _executors;

    std::unique_ptr<TaskExecutor> _fixedExecutor;
};

}  // namespace executor
}  // namespace mongo