summaryrefslogtreecommitdiff
path: root/src/mongo/executor/thread_pool_task_executor.h
blob: 35dca3ce6b7d72d0d13dec9bfcc753d85bbd942b (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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
/**
 *    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 <list>
#include <memory>

#include "mongo/executor/task_executor.h"
#include "mongo/platform/condition_variable.h"
#include "mongo/platform/mutex.h"
#include "mongo/stdx/thread.h"
#include "mongo/transport/baton.h"
#include "mongo/util/fail_point_service.h"

namespace mongo {

class ThreadPoolInterface;

namespace executor {
extern FailPoint initialSyncFuzzerSynchronizationPoint1;
extern FailPoint initialSyncFuzzerSynchronizationPoint2;

struct ConnectionPoolStats;
class NetworkInterface;

/**
 * Implementation of a TaskExecutor that uses a pool of threads to execute work items.
 */
class ThreadPoolTaskExecutor final : public TaskExecutor {
    ThreadPoolTaskExecutor(const ThreadPoolTaskExecutor&) = delete;
    ThreadPoolTaskExecutor& operator=(const ThreadPoolTaskExecutor&) = delete;

public:
    /**
     * Constructs an instance of ThreadPoolTaskExecutor that runs tasks in "pool" and uses "net"
     * for network operations.
     */
    ThreadPoolTaskExecutor(std::unique_ptr<ThreadPoolInterface> pool,
                           std::shared_ptr<NetworkInterface> net);

    /**
     * Destroys a ThreadPoolTaskExecutor.
     */
    ~ThreadPoolTaskExecutor();

    void startup() override;
    void shutdown() override;
    void join() override;
    void appendDiagnosticBSON(BSONObjBuilder* b) const override;
    Date_t now() override;
    StatusWith<EventHandle> makeEvent() override;
    void signalEvent(const EventHandle& event) override;
    StatusWith<CallbackHandle> onEvent(const EventHandle& event, CallbackFn&& work) override;
    StatusWith<stdx::cv_status> waitForEvent(OperationContext* opCtx,
                                             const EventHandle& event,
                                             Date_t deadline) override;
    void waitForEvent(const EventHandle& event) override;
    StatusWith<CallbackHandle> scheduleWork(CallbackFn&& work) override;
    StatusWith<CallbackHandle> scheduleWorkAt(Date_t when, CallbackFn&& work) override;
    StatusWith<CallbackHandle> scheduleRemoteCommandOnAny(
        const RemoteCommandRequestOnAny& request,
        const RemoteCommandOnAnyCallbackFn& cb,
        const BatonHandle& baton = nullptr) override;
    void cancel(const CallbackHandle& cbHandle) override;
    void wait(const CallbackHandle& cbHandle,
              Interruptible* interruptible = Interruptible::notInterruptible()) override;

    void appendConnectionStats(ConnectionPoolStats* stats) const override;

    /**
     * Drops all connections to the given host on the network interface.
     */
    void dropConnections(const HostAndPort& hostAndPort);

private:
    class CallbackState;
    class EventState;
    using WorkQueue = std::list<std::shared_ptr<CallbackState>>;
    using EventList = std::list<std::shared_ptr<EventState>>;

    /**
     * Representation of the stage of life of a thread pool.
     *
     * A pool starts out in the preStart state, and ends life in the shutdownComplete state.  Work
     * may only be scheduled in the preStart and running states. Threads may only be started in the
     * running state. In shutdownComplete, there are no remaining threads or pending tasks to
     * execute.
     *
     * Diagram of legal transitions:
     *
     * preStart -> running -> joinRequired -> joining -> shutdownComplete
     *        \               ^
     *         \_____________/
     *
     * NOTE: The enumeration values below are compared using operator<, etc, with the expectation
     * that a -> b in the diagram above implies that a < b in the enum below.
     */
    enum State { preStart, running, joinRequired, joining, shutdownComplete };

    /**
     * Returns an EventList containing one unsignaled EventState. This is a helper function for
     * performing allocations outside of _mutex, and should only be called by makeSingletonWork and
     * makeEvent().
     */
    static EventList makeSingletonEventList();

    /**
     * Returns an object suitable for passing to enqueueCallbackState_inlock that represents
     * executing "work" no sooner than "when" (defaults to ASAP). This function may and should be
     * called outside of _mutex.
     */
    static WorkQueue makeSingletonWorkQueue(CallbackFn work,
                                            const BatonHandle& baton,
                                            Date_t when = {});

    /**
     * Moves the single callback in "wq" to the end of "queue". It is required that "wq" was
     * produced via a call to makeSingletonWorkQueue().
     */
    StatusWith<CallbackHandle> enqueueCallbackState_inlock(WorkQueue* queue, WorkQueue* wq);

    /**
     * Signals the given event.
     */
    void signalEvent_inlock(const EventHandle& event, stdx::unique_lock<Latch> lk);

    /**
     * Schedules all items from "fromQueue" into the thread pool and moves them into
     * _poolInProgressQueue.
     */
    void scheduleIntoPool_inlock(WorkQueue* fromQueue, stdx::unique_lock<Latch> lk);

    /**
     * Schedules the given item from "fromQueue" into the thread pool and moves it into
     * _poolInProgressQueue.
     */
    void scheduleIntoPool_inlock(WorkQueue* fromQueue,
                                 const WorkQueue::iterator& iter,
                                 stdx::unique_lock<Latch> lk);

    /**
     * Schedules entries from "begin" through "end" in "fromQueue" into the thread pool
     * and moves them into _poolInProgressQueue.
     */
    void scheduleIntoPool_inlock(WorkQueue* fromQueue,
                                 const WorkQueue::iterator& begin,
                                 const WorkQueue::iterator& end,
                                 stdx::unique_lock<Latch> lk);

    /**
     * Executes the callback specified by "cbState".
     */
    void runCallback(std::shared_ptr<CallbackState> cbState);

    bool _inShutdown_inlock() const;
    void _setState_inlock(State newState);
    stdx::unique_lock<Latch> _join(stdx::unique_lock<Latch> lk);

    // The network interface used for remote command execution and waiting.
    std::shared_ptr<NetworkInterface> _net;

    // The thread pool that executes scheduled work items.
    std::shared_ptr<ThreadPoolInterface> _pool;

    // Mutex guarding all remaining fields.
    mutable Mutex _mutex = MONGO_MAKE_LATCH("ThreadPoolTaskExecutor::_mutex");

    // Queue containing all items currently scheduled into the thread pool but not yet completed.
    WorkQueue _poolInProgressQueue;

    // Queue containing all items currently scheduled into the network interface.
    WorkQueue _networkInProgressQueue;

    // Queue containing all items waiting for a particular point in time to execute.
    WorkQueue _sleepersQueue;

    // List of all events that have yet to be signaled.
    EventList _unsignaledEvents;

    // Lifecycle state of this executor.
    stdx::condition_variable _stateChange;
    State _state = preStart;
};

}  // namespace executor
}  // namespace mongo