YugabyteDB (2.13.0.0-b42, bfc6a6643e7399ac8a0e81d06a3ee6d6571b33ab)

Coverage Report

Created: 2022-03-09 17:30

/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
10.9k
                             : FLAGS_tasks_tracker_num_tasks) {}
40
41
4.92k
void TasksTracker::Reset() {
42
4.92k
  std::lock_guard<decltype(lock_)> l(lock_);
43
4.92k
  tasks_.clear();
44
4.92k
}
45
46
246k
void TasksTracker::AddTask(std::shared_ptr<server::MonitoredTask> task) {
47
246k
  std::lock_guard<decltype(lock_)> l(lock_);
48
246k
  tasks_.push_back(task);
49
246k
}
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
90.0k
void TasksTracker::CleanupOldTasks() {
61
90.0k
  auto timeout_ms =
62
90.0k
      FLAGS_catalog_manager_bg_task_wait_ms *
63
90.0k
      GetAtomicFlag(user_initiated_
64
0
                        ? &FLAGS_long_term_tasks_tracker_keep_time_multiplier
65
90.0k
                        : &FLAGS_tasks_tracker_keep_time_multiplier);
66
90.0k
  std::lock_guard<decltype(lock_)> l(lock_);
67
91.2k
  for (auto iter = tasks_.begin(); iter != tasks_.end(); ) {
68
57.5k
    if (MonoTime::Now()
69
57.5k
            .GetDeltaSince((*iter)->start_timestamp())
70
1.22k
            .ToMilliseconds() > timeout_ms) {
71
1.22k
      iter = tasks_.erase(iter);
72
56.3k
    } else {
73
      // Tasks are implicitly sorted by time, so we can break once a task is within
74
      // the keep time.
75
56.3k
      break;
76
56.3k
    }
77
57.5k
  }
78
90.0k
}
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