|
| 1 | +/* |
| 2 | + * Copyright (c) Facebook, Inc. and its affiliates. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +#include "velox/dwio/common/GenericAdmissionController.h" |
| 18 | + |
| 19 | +#include "velox/common/base/Exceptions.h" |
| 20 | +#include "velox/common/base/StatsReporter.h" |
| 21 | +#include "velox/common/time/Timer.h" |
| 22 | + |
| 23 | +namespace facebook::velox::dwio::common { |
| 24 | + |
| 25 | +void GenericAdmissionController::accept(uint64_t resourceUnits) { |
| 26 | + ContinueFuture future; |
| 27 | + uint64_t updatedValue = 0; |
| 28 | + VELOX_CHECK_LE( |
| 29 | + resourceUnits, |
| 30 | + config_.maxLimit, |
| 31 | + "A single request cannot exceed the max limit"); |
| 32 | + { |
| 33 | + std::lock_guard<std::mutex> l(mtx); |
| 34 | + if (unitsUsed_ + resourceUnits > config_.maxLimit) { |
| 35 | + auto [unblockPromise, unblockFuture] = makeVeloxContinuePromiseContract(); |
| 36 | + Request req{resourceUnits}; |
| 37 | + req.promise = std::move(unblockPromise); |
| 38 | + queue_.push_back(std::move(req)); |
| 39 | + future = std::move(unblockFuture); |
| 40 | + } else { |
| 41 | + updatedValue = unitsUsed_ += resourceUnits; |
| 42 | + } |
| 43 | + } |
| 44 | + if (future.valid()) { |
| 45 | + if (!config_.resourceQueuedCountMetric.empty()) { |
| 46 | + RECORD_METRIC_VALUE(config_.resourceQueuedCountMetric); |
| 47 | + } |
| 48 | + uint64_t waitTimeUs{0}; |
| 49 | + { |
| 50 | + MicrosecondTimer timer(&waitTimeUs); |
| 51 | + future.wait(); |
| 52 | + } |
| 53 | + if (!config_.resourceQueuedTimeMsMetric.empty()) { |
| 54 | + RECORD_HISTOGRAM_METRIC_VALUE( |
| 55 | + config_.resourceQueuedTimeMsMetric, waitTimeUs / 1'000); |
| 56 | + } |
| 57 | + return; |
| 58 | + } |
| 59 | + // Only upadate if there was no wait, as the releasing thread is responsible |
| 60 | + // for updating the metric. |
| 61 | + if (!config_.resourceUsageMetric.empty()) { |
| 62 | + RECORD_METRIC_VALUE(config_.resourceUsageMetric, updatedValue); |
| 63 | + } |
| 64 | +} |
| 65 | + |
| 66 | +void GenericAdmissionController::release(uint64_t resourceUnits) { |
| 67 | + uint64_t updatedValue = 0; |
| 68 | + { |
| 69 | + std::lock_guard<std::mutex> l(mtx); |
| 70 | + VELOX_CHECK_LE( |
| 71 | + resourceUnits, |
| 72 | + unitsUsed_, |
| 73 | + "Cannot release more units than have been acquired"); |
| 74 | + unitsUsed_ -= resourceUnits; |
| 75 | + while (!queue_.empty()) { |
| 76 | + auto& request = queue_.front(); |
| 77 | + if (unitsUsed_ + request.unitsRequested > config_.maxLimit) { |
| 78 | + break; |
| 79 | + } |
| 80 | + unitsUsed_ += request.unitsRequested; |
| 81 | + request.promise.setValue(); |
| 82 | + queue_.pop_front(); |
| 83 | + } |
| 84 | + updatedValue = unitsUsed_; |
| 85 | + } |
| 86 | + if (!config_.resourceUsageMetric.empty()) { |
| 87 | + RECORD_METRIC_VALUE(config_.resourceUsageMetric, updatedValue); |
| 88 | + } |
| 89 | +} |
| 90 | +} // namespace facebook::velox::dwio::common |
0 commit comments