/Users/deen/code/yugabyte-db/src/yb/tablet/operations/operation_tracker.h
Line | Count | Source |
1 | | // Licensed to the Apache Software Foundation (ASF) under one |
2 | | // or more contributor license agreements. See the NOTICE file |
3 | | // distributed with this work for additional information |
4 | | // regarding copyright ownership. The ASF licenses this file |
5 | | // to you under the Apache License, Version 2.0 (the |
6 | | // "License"); you may not use this file except in compliance |
7 | | // with the License. You may obtain a copy of the License at |
8 | | // |
9 | | // http://www.apache.org/licenses/LICENSE-2.0 |
10 | | // |
11 | | // Unless required by applicable law or agreed to in writing, |
12 | | // software distributed under the License is distributed on an |
13 | | // "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
14 | | // KIND, either express or implied. See the License for the |
15 | | // specific language governing permissions and limitations |
16 | | // under the License. |
17 | | // |
18 | | // The following only applies to changes made to this file as part of YugaByte development. |
19 | | // |
20 | | // Portions Copyright (c) YugaByte, Inc. |
21 | | // |
22 | | // Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except |
23 | | // in compliance with the License. You may obtain a copy of the License at |
24 | | // |
25 | | // http://www.apache.org/licenses/LICENSE-2.0 |
26 | | // |
27 | | // Unless required by applicable law or agreed to in writing, software distributed under the License |
28 | | // is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express |
29 | | // or implied. See the License for the specific language governing permissions and limitations |
30 | | // under the License. |
31 | | // |
32 | | |
33 | | #ifndef YB_TABLET_OPERATIONS_OPERATION_TRACKER_H |
34 | | #define YB_TABLET_OPERATIONS_OPERATION_TRACKER_H |
35 | | |
36 | | #include <memory> |
37 | | #include <string> |
38 | | #include <unordered_map> |
39 | | #include <vector> |
40 | | |
41 | | #include "yb/gutil/ref_counted.h" |
42 | | #include "yb/tablet/operations/operation.h" |
43 | | #include "yb/util/locks.h" |
44 | | #include "yb/util/opid.h" |
45 | | |
46 | | namespace yb { |
47 | | |
48 | | template<class T> |
49 | | class AtomicGauge; |
50 | | class Counter; |
51 | | class MemTracker; |
52 | | class MetricEntity; |
53 | | |
54 | | namespace tablet { |
55 | | class OperationDriver; |
56 | | |
57 | | // Each TabletPeer has a OperationTracker which keeps track of pending operations. |
58 | | // Each "LeaderOperation" will register itself by calling Add(). |
59 | | // It will remove itself by calling Release(). |
60 | | class OperationTracker { |
61 | | public: |
62 | | explicit OperationTracker(const std::string& log_prefix); |
63 | | ~OperationTracker(); |
64 | | |
65 | | // Adds a operation to the set of tracked operations. |
66 | | // |
67 | | // In the event that the tracker's memory limit is exceeded, returns a |
68 | | // ServiceUnavailable status. |
69 | | CHECKED_STATUS Add(OperationDriver* driver); |
70 | | |
71 | | // Removes the operation from the pending list. |
72 | | // Also triggers the deletion of the Operation object, if its refcount == 0. |
73 | | void Release(OperationDriver* driver, OpIds* applied_op_ids); |
74 | | |
75 | | // Populates list of currently-running operations into 'pending_out' vector. |
76 | | std::vector<scoped_refptr<OperationDriver>> GetPendingOperations() const; |
77 | | |
78 | | // Returns number of pending operations. |
79 | | size_t TEST_GetNumPending() const; |
80 | | |
81 | | void WaitForAllToFinish() const; |
82 | | CHECKED_STATUS WaitForAllToFinish(const MonoDelta& timeout) const; |
83 | | |
84 | | void StartInstrumentation(const scoped_refptr<MetricEntity>& metric_entity); |
85 | | void StartMemoryTracking(const std::shared_ptr<MemTracker>& parent_mem_tracker); |
86 | | |
87 | | // Post tracker in called when operation tracker finishes tracking memory for corresponding op id. |
88 | | // So post tracker could start tracking this memory in case it is still keeping memory for this |
89 | | // op id. |
90 | 150k | void SetPostTracker(std::function<void(const OpId&)> post_tracker) { |
91 | 150k | post_tracker_ = std::move(post_tracker); |
92 | 150k | } |
93 | | |
94 | 150k | const std::string& LogPrefix() const { |
95 | 150k | return log_prefix_; |
96 | 150k | } |
97 | | |
98 | | private: |
99 | | struct Metrics { |
100 | | explicit Metrics(const scoped_refptr<MetricEntity>& entity); |
101 | | |
102 | | scoped_refptr<AtomicGauge<uint64_t> > all_operations_inflight; |
103 | | scoped_refptr<AtomicGauge<uint64_t> > operations_inflight[kOperationTypeMapSize]; |
104 | | |
105 | | scoped_refptr<Counter> operation_memory_pressure_rejections; |
106 | | }; |
107 | | |
108 | | // Increments relevant metric counters. |
109 | | void IncrementCounters(const OperationDriver& driver) const; |
110 | | |
111 | | // Decrements relevant metric counters. |
112 | | void DecrementCounters(const OperationDriver& driver) const; |
113 | | |
114 | | std::vector<scoped_refptr<OperationDriver>> GetPendingOperationsUnlocked() const REQUIRES(mutex_); |
115 | | |
116 | | const std::string log_prefix_; |
117 | | |
118 | | mutable std::mutex mutex_; |
119 | | mutable std::condition_variable cond_; |
120 | | |
121 | | // Per-operation state that is tracked along with the operation itself. |
122 | | struct State { |
123 | | // Approximate memory footprint of the operation. |
124 | | int64_t memory_footprint = 0; |
125 | | }; |
126 | | |
127 | | // Protected by 'lock_'. |
128 | | typedef std::unordered_map< |
129 | | scoped_refptr<OperationDriver>, |
130 | | State, |
131 | | ScopedRefPtrHashFunctor, |
132 | | ScopedRefPtrEqualsFunctor> OperationMap; |
133 | | OperationMap pending_operations_ GUARDED_BY(mutex_); |
134 | | |
135 | | std::unique_ptr<Metrics> metrics_; |
136 | | |
137 | | std::shared_ptr<MemTracker> mem_tracker_; |
138 | | |
139 | | std::function<void(const OpId&)> post_tracker_; |
140 | | |
141 | | DISALLOW_COPY_AND_ASSIGN(OperationTracker); |
142 | | }; |
143 | | |
144 | | } // namespace tablet |
145 | | } // namespace yb |
146 | | |
147 | | #endif // YB_TABLET_OPERATIONS_OPERATION_TRACKER_H |