YugabyteDB (2.13.1.0-b60, 21121d69985fbf76aa6958d8f04a9bfa936293b5)

Coverage Report

Created: 2022-03-22 16:43

/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
94
      : outstanding_task_count_limit_(std::numeric_limits<int>::max()) {
39
94
  }
40
41
  AsyncTaskThrottler::AsyncTaskThrottler(uint64_t limit)
42
51
      : outstanding_task_count_limit_(limit) {
43
51
  }
44
45
249
  void AsyncTaskThrottler::RefreshLimit(uint64_t limit) {
46
249
    std::lock_guard<std::mutex> l(mutex_);
47
249
    outstanding_task_count_limit_ = limit;
48
249
  }
49
50
35
  bool AsyncTaskThrottler::Throttle() {
51
35
    std::lock_guard<std::mutex> l(mutex_);
52
35
    if (ShouldThrottle()) {
53
7
      return true;
54
7
    }
55
28
    AddOutstandingTask();
56
28
    return false;
57
35
  }
58
59
25
  bool AsyncTaskThrottler::RemoveOutstandingTask() {
60
25
    std::lock_guard<std::mutex> l(mutex_);
61
25
    DCHECK_GT(current_outstanding_task_count_, 0);
62
25
    current_outstanding_task_count_--;
63
25
    return current_outstanding_task_count_ == 0;
64
25
  }
65
66
35
  bool AsyncTaskThrottler::ShouldThrottle() {
67
35
    return current_outstanding_task_count_ >= outstanding_task_count_limit_;
68
35
  }
69
70
28
  void AsyncTaskThrottler::AddOutstandingTask() {
71
28
    DCHECK_LT(current_outstanding_task_count_, outstanding_task_count_limit_);
72
28
    current_outstanding_task_count_++;
73
28
  }
74
} // namespace yb