summaryrefslogtreecommitdiff
path: root/src/mongo/util/concurrency/priority_ticketholder.h
blob: 274fcb661c42aed57cf04df6e6ca6d605f95cc23 (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
/**
 *    Copyright (C) 2022-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 <queue>

#include "mongo/db/operation_context.h"
#include "mongo/db/service_context.h"
#include "mongo/platform/mutex.h"
#include "mongo/stdx/condition_variable.h"
#include "mongo/util/concurrency/admission_context.h"
#include "mongo/util/concurrency/mutex.h"
#include "mongo/util/concurrency/ticket_queues.h"
#include "mongo/util/concurrency/ticketholder.h"
#include "mongo/util/hierarchical_acquisition.h"
#include "mongo/util/time_support.h"

namespace mongo {

class Ticket;

/**
 * A ticketholder implementation that centralises all ticket acquisition/releases.  Waiters will get
 * placed in a specific internal queue according to some logic.  Releasers will wake up a waiter
 * from a group chosen according to some logic.
 */
class PriorityTicketHolder : public TicketHolderWithQueueingStats {
public:
    explicit PriorityTicketHolder(int numTickets,
                                  int lowPriorityBypassThreshold,
                                  ServiceContext* serviceContext);
    ~PriorityTicketHolder() override{};

    int available() const override final {
        return _ticketsAvailable.load();
    };

    int queued() const override final {
        return _enqueuedElements.loadRelaxed();
    }

    bool recordImmediateTicketStatistics() noexcept override final {
        return true;
    };

    /**
     * Number of times low priority operations are expedited for ticket admission over normal
     * priority operations.
     */
    std::int64_t expedited() const {
        return _expeditedLowPriorityAdmissions.loadRelaxed();
    }

    /**
     * Returns the number of times the low priority queue is bypassed in favor of dequeuing from the
     * normal priority queue when a ticket becomes available.
     */
    std::int64_t bypassed() const {
        return _lowPriorityBypassCount.loadRelaxed();
    };

    void updateLowPriorityAdmissionBypassThreshold(const int& newBypassThreshold);

private:
    enum class QueueType : unsigned int {
        kLowPriority = 0,
        kNormalPriority = 1,
        // Exclusively used for statistics tracking. This queue should never have any processes
        // 'queued'.
        kImmediatePriority = 2,
        QueueTypeSize = 3
    };

    boost::optional<Ticket> _tryAcquireImpl(AdmissionContext* admCtx) override final;

    boost::optional<Ticket> _waitForTicketUntilImpl(OperationContext* opCtx,
                                                    AdmissionContext* admCtx,
                                                    Date_t until,
                                                    WaitMode waitMode) override final;

    void _releaseToTicketPoolImpl(AdmissionContext* admCtx) noexcept override final;

    void _resize(int newSize, int oldSize) noexcept override final;

    QueueStats& _getQueueStatsToUse(const AdmissionContext* admCtx) noexcept override final;

    void _appendImplStats(BSONObjBuilder& b) const override final;

    bool _tryAcquireTicket();

    /**
     * Wakes up a waiting thread (if it exists) in order for it to attempt to obtain a ticket.
     * Implementors MUST wake at least one waiting thread if at least one thread is pending to be
     * woken between all the queues. In other words, attemptToDequeue on each non-empty Queue must
     * be called until either it returns true at least once or has been called on all queues.
     *
     * Care must be taken to ensure that only CPU-bound work is performed here and it doesn't block.
     *
     * When called the following invariants will be held:
     * - The number of items in each queue will not change during the execution
     * - No other thread will proceed to wait during the execution of the method
     */
    void _dequeueWaitingThread(const ticket_queues::SharedLockGuard& sharedQueueLock);

    /**
     * Returns whether there are higher priority threads pending to get a ticket in front of the
     * given queue type and not enough tickets for all of them.
     */
    bool _hasToWaitForHigherPriority(const ticket_queues::UniqueLockGuard& lk, QueueType queueType);

    unsigned int _enumToInt(QueueType queueType) {
        return static_cast<unsigned int>(queueType);
    }
    unsigned int _enumToInt(QueueType queueType) const {
        return static_cast<unsigned int>(queueType);
    }

    ticket_queues::Queue& _getQueue(QueueType queueType) {
        return _queues[_enumToInt(queueType)];
    }


    QueueType _getQueueType(const AdmissionContext* admCtx) {
        auto priority = admCtx->getPriority();
        switch (priority) {
            case AdmissionContext::Priority::kLow:
                return QueueType::kLowPriority;
            case AdmissionContext::Priority::kNormal:
                return QueueType::kNormalPriority;
            case AdmissionContext::Priority::kImmediate:
                return QueueType::kImmediatePriority;
            default:
                MONGO_UNREACHABLE;
        }
    }

    ticket_queues::QueueMutex _queueMutex;
    std::array<ticket_queues::Queue, static_cast<unsigned int>(QueueType::QueueTypeSize)> _queues;
    std::array<QueueStats, static_cast<unsigned int>(QueueType::QueueTypeSize)> _stats;

    /**
     * Limits the number times the low priority queue is non-empty and bypassed in favor of the
     * normal priority queue for the next ticket admission.
     *
     * Updates must be done under the ticket_queues::UniqueLockGuard.
     */
    int _lowPriorityBypassThreshold;

    /**
     * Counts the number of times normal operations are dequeued over operations queued in the low
     * priority queue.
     */
    AtomicWord<std::uint64_t> _lowPriorityBypassCount{0};

    /**
     * Number of times ticket admission is expedited for low priority operations.
     */
    AtomicWord<std::int64_t> _expeditedLowPriorityAdmissions{0};
    AtomicWord<int> _ticketsAvailable;
    AtomicWord<int> _enqueuedElements;
    ServiceContext* _serviceContext;
};
}  // namespace mongo