YugabyteDB (2.13.0.0-b42, bfc6a6643e7399ac8a0e81d06a3ee6d6571b33ab)

Coverage Report

Created: 2022-03-09 17:30

/Users/deen/code/yugabyte-db/src/yb/util/async_task_util.cc
Line
Count
Source (jump to first uncovered line)
1
// Copyright (c) YugaByte, Inc.
2
//
3
// Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
4
// in compliance with the License.  You may obtain a copy of the License at
5
//
6
// http://www.apache.org/licenses/LICENSE-2.0
7
//
8
// Unless required by applicable law or agreed to in writing, software distributed under the License
9
// is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
10
// or implied.  See the License for the specific language governing permissions and limitations
11
// under the License.
12
//
13
14
#include "yb/util/async_task_util.h"
15
16
#include <glog/logging.h>
17
18
namespace yb {
19
  // AsyncTaskTracker methods.
20
0
  Status AsyncTaskTracker::Start() {
21
0
    if (started_) {
22
0
      return STATUS(IllegalState, "Task has already started");
23
0
    }
24
0
    started_ = true;
25
0
    return Status::OK();
26
0
  }
27
28
0
  bool AsyncTaskTracker::Started() const {
29
0
    return started_;
30
0
  }
31
32
0
  void AsyncTaskTracker::Abort() {
33
0
    started_ = false;
34
0
  }
35
36
  // AsyncTaskThrottler methods.
37
  AsyncTaskThrottler::AsyncTaskThrottler()
38
0
      : outstanding_task_count_limit_(std::numeric_limits<int>::max()) {
39
0
  }
40
41
  AsyncTaskThrottler::AsyncTaskThrottler(uint64_t limit)
42
0
      : outstanding_task_count_limit_(limit) {
43
0
  }
44
45
0
  void AsyncTaskThrottler::RefreshLimit(uint64_t limit) {
46
0
    std::lock_guard<std::mutex> l(mutex_);
47
0
    outstanding_task_count_limit_ = limit;
48
0
  }
49
50
0
  bool AsyncTaskThrottler::Throttle() {
51
0
    std::lock_guard<std::mutex> l(mutex_);
52
0
    if (ShouldThrottle()) {
53
0
      return true;
54
0
    }
55
0
    AddOutstandingTask();
56
0
    return false;
57
0
  }
58
59
0
  bool AsyncTaskThrottler::RemoveOutstandingTask() {
60
0
    std::lock_guard<std::mutex> l(mutex_);
61
0
    DCHECK_GT(current_outstanding_task_count_, 0);
62
0
    current_outstanding_task_count_--;
63
0
    return current_outstanding_task_count_ == 0;
64
0
  }
65
66
0
  bool AsyncTaskThrottler::ShouldThrottle() {
67
0
    return current_outstanding_task_count_ >= outstanding_task_count_limit_;
68
0
  }
69
70
0
  void AsyncTaskThrottler::AddOutstandingTask() {
71
0
    DCHECK_LT(current_outstanding_task_count_, outstanding_task_count_limit_);
72
0
    current_outstanding_task_count_++;
73
0
  }
74
} // namespace yb