summaryrefslogtreecommitdiff
path: root/src/mongo/util/concurrency/priority_ticketholder.cpp
blob: ab234ee3c8facd38cfe14dd3b2988162976a0d27 (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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
/**
 *    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.
 */

#include "mongo/platform/basic.h"

#include "mongo/db/service_context.h"
#include "mongo/util/concurrency/admission_context.h"
#include "mongo/util/concurrency/priority_ticketholder.h"

#include <iostream>

#include "mongo/logv2/log.h"
#include "mongo/util/str.h"

#define MONGO_LOGV2_DEFAULT_COMPONENT ::mongo::logv2::LogComponent::kDefault

namespace mongo {
PriorityTicketHolder::PriorityTicketHolder(int numTickets,
                                           int lowPriorityBypassThreshold,
                                           ServiceContext* serviceContext)
    : TicketHolderWithQueueingStats(numTickets, serviceContext),
      _lowPriorityBypassThreshold(lowPriorityBypassThreshold),
      _serviceContext(serviceContext) {
    _ticketsAvailable.store(numTickets);
    _enqueuedElements.store(0);
}

void PriorityTicketHolder::updateLowPriorityAdmissionBypassThreshold(
    const int& newBypassThreshold) {
    ticket_queues::UniqueLockGuard uniqueQueueLock(_queueMutex);
    _lowPriorityBypassThreshold = newBypassThreshold;
}

boost::optional<Ticket> PriorityTicketHolder::_tryAcquireImpl(AdmissionContext* admCtx) {
    invariant(admCtx);
    // Low priority operations cannot use optimistic ticket acquisition and will go to the queue
    // instead. This is done to prevent them from skipping the line before other high-priority
    // operations.
    if (admCtx->getPriority() >= AdmissionContext::Priority::kNormal) {
        auto hasAcquired = _tryAcquireTicket();
        if (hasAcquired) {
            return Ticket{this, admCtx};
        }
    }
    return boost::none;
}

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

    auto queueType = _getQueueType(admCtx);
    auto& queue = _getQueue(queueType);

    bool interruptible = waitMode == WaitMode::kInterruptible;

    _enqueuedElements.addAndFetch(1);
    ON_BLOCK_EXIT([&] { _enqueuedElements.subtractAndFetch(1); });

    ticket_queues::UniqueLockGuard uniqueQueueLock(_queueMutex);
    do {
        while (_ticketsAvailable.load() <= 0 ||
               _hasToWaitForHigherPriority(uniqueQueueLock, queueType)) {
            bool hasTimedOut = !queue.enqueue(uniqueQueueLock, opCtx, until, interruptible);
            if (hasTimedOut) {
                return boost::none;
            }
        }
    } while (!_tryAcquireTicket());
    return Ticket{this, admCtx};
}

void PriorityTicketHolder::_releaseToTicketPoolImpl(AdmissionContext* admCtx) noexcept {
    // Tickets acquired with priority kImmediate are not generated from the pool of available
    // tickets, and thus should never be returned to the pool of available tickets.
    invariant(admCtx && admCtx->getPriority() != AdmissionContext::Priority::kImmediate);

    // The idea behind the release mechanism consists of a consistent view of queued elements
    // waiting for a ticket and many threads releasing tickets simultaneously. The releasers will
    // proceed to attempt to dequeue an element by seeing if there are threads not woken and waking
    // one, having increased the number of woken threads for accuracy. Once the thread gets woken it
    // will then decrease the number of woken threads (as it has been woken) and then attempt to
    // acquire a ticket. The two possible states are either one or more releasers releasing or a
    // thread waking up due to the RW mutex.
    //
    // Under this lock the queues cannot be modified in terms of someone attempting to enqueue on
    // them, only waking threads is allowed.
    ticket_queues::SharedLockGuard sharedQueueLock(_queueMutex);
    _ticketsAvailable.addAndFetch(1);
    _dequeueWaitingThread(sharedQueueLock);
}

void PriorityTicketHolder::_resize(int newSize, int oldSize) noexcept {
    auto difference = newSize - oldSize;

    _ticketsAvailable.fetchAndAdd(difference);

    if (difference > 0) {
        // As we're adding tickets the waiting threads need to be notified that there are new
        // tickets available.
        ticket_queues::SharedLockGuard sharedQueueLock(_queueMutex);
        for (int i = 0; i < difference; i++) {
            _dequeueWaitingThread(sharedQueueLock);
        }
    }

    // No need to do anything in the other cases as the number of tickets being <= 0 implies they'll
    // have to wait until the current ticket holders release their tickets.
}

TicketHolderWithQueueingStats::QueueStats& PriorityTicketHolder::_getQueueStatsToUse(
    const AdmissionContext* admCtx) noexcept {
    auto queueType = _getQueueType(admCtx);
    return _stats[_enumToInt(queueType)];
}

void PriorityTicketHolder::_appendImplStats(BSONObjBuilder& b) const {
    {
        BSONObjBuilder bbb(b.subobjStart("lowPriority"));
        const auto& lowPriorityTicketStats = _stats[_enumToInt(QueueType::kLowPriority)];
        _appendCommonQueueImplStats(bbb, lowPriorityTicketStats);
        bbb.append("expedited", expedited());
        bbb.append("bypassed", bypassed());
        bbb.done();
    }
    {
        BSONObjBuilder bbb(b.subobjStart("normalPriority"));
        const auto& normalPriorityTicketStats = _stats[_enumToInt(QueueType::kNormalPriority)];
        _appendCommonQueueImplStats(bbb, normalPriorityTicketStats);
        bbb.done();
    }
    {
        BSONObjBuilder bbb(b.subobjStart("immediatePriority"));
        // Since 'kImmediate' priority operations will never queue, omit queueing statistics that
        // will always be 0.
        const auto& immediateTicketStats = _stats[_enumToInt(QueueType::kImmediatePriority)];

        auto finished = immediateTicketStats.totalFinishedProcessing.loadRelaxed();
        auto started = immediateTicketStats.totalStartedProcessing.loadRelaxed();
        bbb.append("startedProcessing", started);
        bbb.append("processing", std::max(static_cast<int>(started - finished), 0));
        bbb.append("finishedProcessing", finished);
        bbb.append("totalTimeProcessingMicros",
                   immediateTicketStats.totalTimeProcessingMicros.loadRelaxed());
        bbb.append("newAdmissions", immediateTicketStats.totalNewAdmissions.loadRelaxed());
        bbb.done();
    }
}

bool PriorityTicketHolder::_tryAcquireTicket() {
    auto remaining = _ticketsAvailable.subtractAndFetch(1);
    if (remaining < 0) {
        _ticketsAvailable.addAndFetch(1);
        return false;
    }
    return true;
}

void PriorityTicketHolder::_dequeueWaitingThread(
    const ticket_queues::SharedLockGuard& sharedQueueLock) {
    // There are only 2 possible queues to dequeue from - the low priority and normal priority
    // queues. There will never be anything to dequeue from the immediate priority queue, given
    // immediate priority operations will never wait for ticket admission.
    auto& lowPriorityQueue = _getQueue(QueueType::kLowPriority);
    auto& normalPriorityQueue = _getQueue(QueueType::kNormalPriority);

    // There is a guarantee that the number of queued elements cannot change while holding the
    // shared queue lock.
    auto lowQueueCount = lowPriorityQueue.queuedElems();
    auto normalQueueCount = normalPriorityQueue.queuedElems();

    if (lowQueueCount == 0 && normalQueueCount == 0) {
        return;
    }
    if (lowQueueCount == 0) {
        normalPriorityQueue.attemptToDequeue(sharedQueueLock);
        return;
    }
    if (normalQueueCount == 0) {
        lowPriorityQueue.attemptToDequeue(sharedQueueLock);
        return;
    }

    // Both queues are non-empty, and the low priority queue is bypassed for dequeue in favor of the
    // normal priority queue until the bypass threshold is met.
    if (_lowPriorityBypassThreshold > 0 &&
        _lowPriorityBypassCount.addAndFetch(1) % _lowPriorityBypassThreshold == 0) {
        if (lowPriorityQueue.attemptToDequeue(sharedQueueLock)) {
            _expeditedLowPriorityAdmissions.addAndFetch(1);
        } else {
            normalPriorityQueue.attemptToDequeue(sharedQueueLock);
        }
        return;
    }

    if (!normalPriorityQueue.attemptToDequeue(sharedQueueLock)) {
        lowPriorityQueue.attemptToDequeue(sharedQueueLock);
    }
}

bool PriorityTicketHolder::_hasToWaitForHigherPriority(const ticket_queues::UniqueLockGuard& lk,
                                                       QueueType queue) {
    switch (queue) {
        case QueueType::kLowPriority: {
            const auto& normalQueue = _getQueue(QueueType::kNormalPriority);
            auto pending = normalQueue.getThreadsPendingToWake();
            return pending != 0 && pending >= _ticketsAvailable.load();
        }
        default:
            return false;
    }
}
}  // namespace mongo