YugabyteDB (2.13.1.0-b60, 21121d69985fbf76aa6958d8f04a9bfa936293b5)

Coverage Report

Created: 2022-03-22 16:43

/Users/deen/code/yugabyte-db/src/yb/master/tasks_tracker.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
#include "yb/master/tasks_tracker.h"
14
15
#include "yb/util/atomic.h"
16
17
DEFINE_int32(tasks_tracker_num_tasks, 100,
18
             "Number of most recent tasks to track for displaying in utilities UI.");
19
20
DEFINE_int32(tasks_tracker_keep_time_multiplier, 300,
21
             "How long we should keep tasks before cleaning them up, as a multiple of the "
22
             "load balancer interval (catalog_manager_bg_task_wait_ms).");
23
24
DEFINE_int32(tasks_tracker_num_long_term_tasks, 20,
25
             "Number of most recent tasks to track for displaying in utilities UI.");
26
27
DEFINE_int32(long_term_tasks_tracker_keep_time_multiplier, 86400,
28
             "How long we should keep long-term tasks before cleaning them up, "
29
             "as a multiple of the load balancer interval (catalog_manager_bg_task_wait_ms).");
30
31
namespace yb {
32
namespace master {
33
34
using strings::Substitute;
35
36
TasksTracker::TasksTracker(IsUserInitiated user_initiated)
37
    : user_initiated_(user_initiated),
38
      tasks_(user_initiated_ ? FLAGS_tasks_tracker_num_long_term_tasks
39
16.1k
                             : FLAGS_tasks_tracker_num_tasks) {}
40
41
7.69k
void TasksTracker::Reset() {
42
7.69k
  std::lock_guard<decltype(lock_)> l(lock_);
43
7.69k
  tasks_.clear();
44
7.69k
}
45
46
422k
void TasksTracker::AddTask(std::shared_ptr<server::MonitoredTask> task) {
47
422k
  std::lock_guard<decltype(lock_)> l(lock_);
48
422k
  tasks_.push_back(task);
49
422k
}
50
51
3
std::vector<std::shared_ptr<server::MonitoredTask>> TasksTracker::GetTasks() {
52
3
  shared_lock<decltype(lock_)> l(lock_);
53
3
  std::vector<std::shared_ptr<server::MonitoredTask>> tasks;
54
200
  for (const auto& task : tasks_) {
55
200
    tasks.push_back(task);
56
200
  }
57
3
  return tasks;
58
3
}
59
60
1.56M
void TasksTracker::CleanupOldTasks() {
61
1.56M
  auto timeout_ms =
62
1.56M
      FLAGS_catalog_manager_bg_task_wait_ms *
63
1.56M
      GetAtomicFlag(user_initiated_
64
1.56M
                        ? 
&FLAGS_long_term_tasks_tracker_keep_time_multiplier0
65
1.56M
                        : &FLAGS_tasks_tracker_keep_time_multiplier);
66
1.56M
  std::lock_guard<decltype(lock_)> l(lock_);
67
1.57M
  for (auto iter = tasks_.begin(); iter != tasks_.end(); ) {
68
185k
    if (MonoTime::Now()
69
185k
            .GetDeltaSince((*iter)->start_timestamp())
70
185k
            .ToMilliseconds() > timeout_ms) {
71
2.52k
      iter = tasks_.erase(iter);
72
182k
    } else {
73
      // Tasks are implicitly sorted by time, so we can break once a task is within
74
      // the keep time.
75
182k
      break;
76
182k
    }
77
185k
  }
78
1.56M
}
79
80
0
std::string TasksTracker::ToString() {
81
0
  shared_lock<decltype(lock_)> l(lock_);
82
0
  return Substitute("TasksTracker has $0 tasks in buffer.",
83
0
                    tasks_.size());
84
0
}
85
86
} // namespace master
87
} // namespace yb