be/src/storage/compaction_task_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 | | #pragma once |
19 | | |
20 | | #include <atomic> |
21 | | #include <cstdint> |
22 | | #include <deque> |
23 | | #include <shared_mutex> |
24 | | #include <string> |
25 | | #include <unordered_map> |
26 | | #include <vector> |
27 | | |
28 | | #include "storage/compaction_task_types.h" |
29 | | |
30 | | namespace doris { |
31 | | |
32 | | // Standardized compaction type enum covering base / cumulative / full. |
33 | | enum class CompactionProfileType : uint8_t { |
34 | | BASE = 0, |
35 | | CUMULATIVE = 1, |
36 | | FULL = 2, |
37 | | }; |
38 | | |
39 | | const char* to_string(CompactionProfileType type); |
40 | | |
41 | | // Task lifecycle status. |
42 | | enum class CompactionTaskStatus : uint8_t { |
43 | | PENDING = 0, |
44 | | RUNNING = 1, |
45 | | FINISHED = 2, |
46 | | FAILED = 3, |
47 | | }; |
48 | | |
49 | | const char* to_string(CompactionTaskStatus status); |
50 | | |
51 | | const char* to_string(TriggerMethod method); |
52 | | |
53 | | // Incremental info when transitioning from PENDING to RUNNING. |
54 | | struct RunningStats { |
55 | | int64_t start_time_ms {0}; |
56 | | bool is_vertical {false}; |
57 | | int64_t permits {0}; |
58 | | }; |
59 | | |
60 | | // Result info collected when a task completes or fails. |
61 | | struct CompletionStats { |
62 | | // Input stats backfill: local compaction populates these in build_basic_info() |
63 | | // which runs inside execute_compact_impl(), after register_task(). |
64 | | std::string input_version_range; |
65 | | int64_t input_rowsets_count {0}; |
66 | | int64_t input_row_num {0}; |
67 | | int64_t input_data_size {0}; |
68 | | int64_t input_index_size {0}; |
69 | | int64_t input_total_size {0}; |
70 | | int64_t input_segments_num {0}; |
71 | | int64_t end_time_ms {0}; |
72 | | int64_t merged_rows {0}; |
73 | | int64_t filtered_rows {0}; |
74 | | int64_t output_rows {0}; // _stats.output_rows (Merger statistics) |
75 | | int64_t output_row_num {0}; |
76 | | int64_t output_data_size {0}; |
77 | | int64_t output_index_size {0}; // _output_rowset->index_disk_size() |
78 | | int64_t output_total_size {0}; // _output_rowset->total_disk_size() |
79 | | int64_t output_segments_num {0}; |
80 | | std::string output_version; |
81 | | bool is_ordered_data_compaction {false}; |
82 | | int64_t merge_latency_ms {0}; // _merge_rowsets_latency_timer (converted to ms) |
83 | | int64_t bytes_read_from_local {0}; |
84 | | int64_t bytes_read_from_remote {0}; |
85 | | int64_t peak_memory_bytes {0}; |
86 | | }; |
87 | | |
88 | | // Unified metadata describing a compaction task across its full lifecycle. |
89 | | struct CompactionTaskInfo { |
90 | | // ===== Identity ===== |
91 | | int64_t compaction_id {0}; // unique task ID assigned by Tracker |
92 | | int64_t backend_id {0}; // BE node ID |
93 | | int64_t table_id {0}; // table ID |
94 | | int64_t partition_id {0}; // partition ID |
95 | | int64_t tablet_id {0}; // tablet ID |
96 | | |
97 | | // ===== Task attributes ===== |
98 | | CompactionProfileType compaction_type {CompactionProfileType::BASE}; |
99 | | CompactionTaskStatus status {CompactionTaskStatus::PENDING}; |
100 | | TriggerMethod trigger_method {TriggerMethod::AUTO}; |
101 | | int64_t compaction_score {0}; // tablet compaction score at register time |
102 | | |
103 | | // ===== Timestamps ===== |
104 | | int64_t scheduled_time_ms {0}; // task registration time |
105 | | int64_t start_time_ms {0}; // task execution start time (0 while PENDING) |
106 | | int64_t end_time_ms {0}; // task end time (0 while not completed) |
107 | | |
108 | | // ===== Input statistics (available after prepare_compact) ===== |
109 | | int64_t input_rowsets_count {0}; |
110 | | int64_t input_row_num {0}; |
111 | | int64_t input_data_size {0}; // bytes, corresponds to _input_rowsets_data_size |
112 | | int64_t input_index_size {0}; // bytes, corresponds to _input_rowsets_index_size |
113 | | int64_t input_total_size {0}; // bytes, = data + index |
114 | | int64_t input_segments_num {0}; |
115 | | std::string input_version_range; // e.g. "[0-5]" |
116 | | |
117 | | // ===== Output statistics (written at complete/fail) ===== |
118 | | int64_t merged_rows {0}; |
119 | | int64_t filtered_rows {0}; |
120 | | int64_t output_rows {0}; // Merger output rows (_stats.output_rows; 0 for ordered path) |
121 | | int64_t output_row_num {0}; // from _output_rowset->num_rows() |
122 | | int64_t output_data_size {0}; // bytes, from _output_rowset->data_disk_size() |
123 | | int64_t output_index_size {0}; // bytes, from _output_rowset->index_disk_size() |
124 | | int64_t output_total_size {0}; // bytes, from _output_rowset->total_disk_size() |
125 | | int64_t output_segments_num {0}; |
126 | | std::string output_version; // e.g. "[0-5]" |
127 | | bool is_ordered_data_compaction { |
128 | | false}; // ordered/meta-link compaction path, including changelog quick path |
129 | | |
130 | | // ===== Merge performance ===== |
131 | | int64_t merge_latency_ms {0}; // merge rowsets latency (ms; 0 for ordered path) |
132 | | |
133 | | // ===== IO statistics (written at complete/fail) ===== |
134 | | int64_t bytes_read_from_local {0}; |
135 | | int64_t bytes_read_from_remote {0}; |
136 | | |
137 | | // ===== Resources ===== |
138 | | int64_t peak_memory_bytes {0}; // peak memory usage (bytes) |
139 | | bool is_vertical {false}; // whether vertical merge is used |
140 | | int64_t permits {0}; // compaction permits used |
141 | | |
142 | | // ===== Vertical compaction progress ===== |
143 | | int64_t vertical_total_groups {0}; // total column groups (0 for horizontal) |
144 | | int64_t vertical_completed_groups { |
145 | | 0}; // completed column groups (updated in real-time during RUNNING) |
146 | | |
147 | | // ===== Error ===== |
148 | | std::string status_msg; // failure message (empty on success) |
149 | | }; |
150 | | |
151 | | // Global singleton managing compaction task lifecycle. |
152 | | // Receives push reports from compaction entries and execution layer, |
153 | | // provides pull query interfaces for system table and HTTP API. |
154 | | class CompactionTaskTracker { |
155 | | public: |
156 | | static CompactionTaskTracker* instance(); |
157 | | |
158 | | // ID allocation: globally unique monotonically increasing, restarts from 1 after BE restart. |
159 | 277k | int64_t next_compaction_id() { return _next_id.fetch_add(1, std::memory_order_relaxed); } |
160 | | |
161 | | // ===== Push interfaces: lifecycle management (write lock) ===== |
162 | | // All push interfaces are no-op when enable_compaction_task_tracker=false. |
163 | | void register_task(CompactionTaskInfo info); |
164 | | void update_to_running(int64_t compaction_id, const RunningStats& stats); |
165 | | void update_progress(int64_t compaction_id, int64_t total_groups, int64_t completed_groups); |
166 | | void complete(int64_t compaction_id, const CompletionStats& stats); |
167 | | void fail(int64_t compaction_id, const CompletionStats& stats, const std::string& msg); |
168 | | void remove_task(int64_t compaction_id); |
169 | | |
170 | | // ===== Pull interfaces: queries (read lock) ===== |
171 | | // For system table: returns full snapshot copy of _active_tasks + _completed_tasks. |
172 | | std::vector<CompactionTaskInfo> get_all_tasks() const; |
173 | | |
174 | | // For HTTP API: iterates _completed_tasks only, returns filtered subset copy. |
175 | | std::vector<CompactionTaskInfo> get_completed_tasks(int64_t tablet_id = 0, int64_t top_n = 0, |
176 | | const std::string& compaction_type = "", |
177 | | int success_filter = -1) const; |
178 | | |
179 | | // Test only: clear all active and completed tasks. |
180 | | void clear_for_test(); |
181 | | |
182 | | private: |
183 | 8 | CompactionTaskTracker() = default; |
184 | | |
185 | | void _apply_completion(CompactionTaskInfo& info, const CompletionStats& stats); |
186 | | void _trim_completed_locked(); |
187 | | |
188 | | std::atomic<int64_t> _next_id {1}; |
189 | | |
190 | | mutable std::shared_mutex _mutex; |
191 | | |
192 | | // Active tasks (PENDING + RUNNING), indexed by compaction_id. |
193 | | // Removed on complete/fail and moved to _completed_tasks. |
194 | | std::unordered_map<int64_t, CompactionTaskInfo> _active_tasks; |
195 | | |
196 | | // Completed tasks (FINISHED + FAILED), FIFO ring buffer. |
197 | | // Oldest records are evicted when exceeding compaction_task_tracker_max_records. |
198 | | std::deque<CompactionTaskInfo> _completed_tasks; |
199 | | }; |
200 | | |
201 | | } // namespace doris |