summaryrefslogtreecommitdiff
path: root/src/mongo/util/concurrency/priority_ticketholder.h
diff options
context:
space:
mode:
Diffstat (limited to 'src/mongo/util/concurrency/priority_ticketholder.h')
-rw-r--r--src/mongo/util/concurrency/priority_ticketholder.h189
1 files changed, 189 insertions, 0 deletions
diff --git a/src/mongo/util/concurrency/priority_ticketholder.h b/src/mongo/util/concurrency/priority_ticketholder.h
new file mode 100644
index 00000000000..274fcb661c4
--- /dev/null
+++ b/src/mongo/util/concurrency/priority_ticketholder.h
@@ -0,0 +1,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