Coverage Report

Created: 2026-04-14 03:58

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
be/src/storage/version_graph.cpp
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
#include "storage/version_graph.h"
19
20
#include <cctz/time_zone.h>
21
#include <stddef.h>
22
23
#include <algorithm>
24
// IWYU pragma: no_include <bits/chrono.h>
25
#include <chrono> // IWYU pragma: keep
26
#include <list>
27
#include <memory>
28
#include <optional>
29
#include <ostream>
30
#include <ranges>
31
#include <utility>
32
33
#include "common/logging.h"
34
35
namespace doris {
36
37
using namespace ErrorCode;
38
39
void TimestampedVersionTracker::construct_versioned_tracker(
40
0
        const RowsetMetaMapContainer& rs_metas) {
41
0
    construct_versioned_tracker(std::views::values(rs_metas));
42
0
}
43
44
void TimestampedVersionTracker::construct_versioned_tracker(
45
331k
        const RowsetMetaMapContainer& rs_metas, const RowsetMetaMapContainer& stale_metas) {
46
331k
    construct_versioned_tracker(std::views::values(rs_metas), std::views::values(stale_metas));
47
331k
}
48
49
bool TimestampedVersionTracker::_find_path_from_stale_map(
50
        const std::unordered_map<int64_t, std::unordered_map<int64_t, RowsetMetaSharedPtr>>&
51
                stale_map,
52
        int64_t first_version, int64_t second_version,
53
81.3k
        std::vector<RowsetMetaSharedPtr>* stale_path) {
54
81.3k
    auto first_iter = stale_map.find(first_version);
55
    // If `first_version` not in `stale_map`, there is no path.
56
81.3k
    if (first_iter == stale_map.end()) {
57
43.2k
        return false;
58
43.2k
    }
59
38.1k
    auto& second_version_map = first_iter->second;
60
38.1k
    auto second_iter = second_version_map.find(second_version);
61
    // If second_version in `stale_map`, find a path.
62
38.1k
    if (second_iter != second_version_map.end()) {
63
6.18k
        auto row_meta = second_iter->second;
64
        // Add rowset to path.
65
6.18k
        stale_path->push_back(row_meta);
66
6.18k
        return true;
67
6.18k
    }
68
69
    // Traverse the first version map to backtracking  `_find_path_from_stale_map`.
70
31.9k
    auto map_iter = second_version_map.begin();
71
31.9k
    while (map_iter != second_version_map.end()) {
72
        // The version greater than `second_version`, we can't find path in `stale_map`.
73
31.9k
        if (map_iter->first > second_version) {
74
0
            map_iter++;
75
0
            continue;
76
0
        }
77
        // Backtracking `_find_path_from_stale_map` find from `map_iter->first + 1` to `second_version`.
78
31.9k
        stale_path->push_back(map_iter->second);
79
31.9k
        bool r = _find_path_from_stale_map(stale_map, map_iter->first + 1, second_version,
80
31.9k
                                           stale_path);
81
31.9k
        if (r) {
82
31.9k
            return true;
83
31.9k
        }
84
        // There is no path in current version, pop and continue.
85
1
        stale_path->pop_back();
86
1
        map_iter++;
87
1
    }
88
89
1
    return false;
90
31.9k
}
91
92
35
void TimestampedVersionTracker::get_stale_version_path_json_doc(rapidjson::Document& path_arr) {
93
35
    auto path_arr_iter = _stale_version_path_map.begin();
94
95
    // Do loop version path.
96
133
    while (path_arr_iter != _stale_version_path_map.end()) {
97
98
        auto path_id = path_arr_iter->first;
98
98
        auto path_version_path = path_arr_iter->second;
99
100
98
        rapidjson::Document item;
101
98
        item.SetObject();
102
        // Add `path_id` to item.
103
98
        auto path_id_str = std::to_string(path_id);
104
98
        rapidjson::Value path_id_value;
105
98
        path_id_value.SetString(path_id_str.c_str(),
106
98
                                static_cast<rapidjson::SizeType>(path_id_str.length()),
107
98
                                path_arr.GetAllocator());
108
98
        item.AddMember("path id", path_id_value, path_arr.GetAllocator());
109
110
        // Add max create time to item.
111
98
        auto time_zone = cctz::local_time_zone();
112
113
98
        auto tp = std::chrono::system_clock::from_time_t(path_version_path->max_create_time());
114
98
        auto create_time_str = cctz::format("%Y-%m-%d %H:%M:%S %z", tp, time_zone);
115
116
98
        rapidjson::Value create_time_value;
117
98
        create_time_value.SetString(create_time_str.c_str(),
118
98
                                    static_cast<rapidjson::SizeType>(create_time_str.length()),
119
98
                                    path_arr.GetAllocator());
120
98
        item.AddMember("last create time", create_time_value, path_arr.GetAllocator());
121
122
        // Add path list to item.
123
98
        std::stringstream path_list_stream;
124
98
        path_list_stream << path_id_str;
125
98
        auto path_list_ptr = path_version_path->timestamped_versions();
126
98
        auto path_list_iter = path_list_ptr.begin();
127
645
        while (path_list_iter != path_list_ptr.end()) {
128
547
            path_list_stream << " -> ";
129
547
            path_list_stream << "[";
130
547
            path_list_stream << (*path_list_iter)->version().first;
131
547
            path_list_stream << "-";
132
547
            path_list_stream << (*path_list_iter)->version().second;
133
547
            path_list_stream << "]";
134
547
            path_list_iter++;
135
547
        }
136
98
        std::string path_list = path_list_stream.str();
137
98
        rapidjson::Value path_list_value;
138
98
        path_list_value.SetString(path_list.c_str(),
139
98
                                  static_cast<rapidjson::SizeType>(path_list.length()),
140
98
                                  path_arr.GetAllocator());
141
98
        item.AddMember("path list", path_list_value, path_arr.GetAllocator());
142
143
        // Add item to `path_arr`.
144
98
        path_arr.PushBack(item, path_arr.GetAllocator());
145
146
98
        path_arr_iter++;
147
98
    }
148
35
}
149
150
void TimestampedVersionTracker::recover_versioned_tracker(
151
1
        const std::map<int64_t, PathVersionListSharedPtr>& stale_version_path_map) {
152
1
    auto _path_map_iter = stale_version_path_map.begin();
153
    // Recover `stale_version_path_map`.
154
1
    while (_path_map_iter != stale_version_path_map.end()) {
155
        // Add `PathVersionListSharedPtr` to map.
156
0
        _stale_version_path_map[_path_map_iter->first] = _path_map_iter->second;
157
158
0
        std::vector<TimestampedVersionSharedPtr>& timestamped_versions =
159
0
                _path_map_iter->second->timestamped_versions();
160
0
        std::vector<TimestampedVersionSharedPtr>::iterator version_path_iter =
161
0
                timestamped_versions.begin();
162
0
        while (version_path_iter != timestamped_versions.end()) {
163
            // Add version to `_version_graph`.
164
0
            _version_graph.add_version_to_graph((*version_path_iter)->version());
165
0
            ++version_path_iter;
166
0
        }
167
0
        ++_path_map_iter;
168
0
    }
169
1
    LOG(INFO) << "recover_versioned_tracker current map info " << get_current_path_map_str();
170
1
}
171
172
80.7k
void TimestampedVersionTracker::add_version(const Version& version) {
173
80.7k
    _version_graph.add_version_to_graph(version);
174
80.7k
}
175
176
void TimestampedVersionTracker::add_stale_path_version(
177
7.70k
        const std::vector<RowsetMetaSharedPtr>& stale_rs_metas) {
178
7.70k
    if (stale_rs_metas.empty()) {
179
1
        VLOG_NOTICE << "there is no version in the stale_rs_metas.";
180
1
        return;
181
1
    }
182
183
7.70k
    PathVersionListSharedPtr ptr(new TimestampedVersionPathContainer());
184
48.3k
    for (auto rs : stale_rs_metas) {
185
48.3k
        TimestampedVersionSharedPtr vt_ptr(new TimestampedVersion(rs->version(), rs->stale_at()));
186
48.3k
        ptr->add_timestamped_version(vt_ptr);
187
48.3k
    }
188
189
7.70k
    std::vector<TimestampedVersionSharedPtr>& timestamped_versions = ptr->timestamped_versions();
190
191
7.70k
    struct TimestampedVersionPtrCompare {
192
7.70k
        bool operator()(const TimestampedVersionSharedPtr ptr1,
193
83.7k
                        const TimestampedVersionSharedPtr ptr2) {
194
83.7k
            return ptr1->version().first < ptr2->version().first;
195
83.7k
        }
196
7.70k
    };
197
7.70k
    sort(timestamped_versions.begin(), timestamped_versions.end(), TimestampedVersionPtrCompare());
198
7.70k
    _stale_version_path_map[_next_path_id] = ptr;
199
7.70k
    _next_path_id++;
200
7.70k
}
201
202
// Capture consistent versions from graph.
203
Status TimestampedVersionTracker::capture_consistent_versions(
204
364k
        const Version& spec_version, std::vector<Version>* version_path) const {
205
364k
    return _version_graph.capture_consistent_versions(spec_version, version_path);
206
364k
}
207
208
Status TimestampedVersionTracker::capture_consistent_versions_with_validator(
209
        const Version& spec_version, std::vector<Version>& version_path,
210
12
        const std::function<bool(int64_t, int64_t)>& validator) const {
211
12
    return _version_graph.capture_consistent_versions_with_validator(spec_version, version_path,
212
12
                                                                     validator);
213
12
}
214
215
Status TimestampedVersionTracker::capture_consistent_versions_prefer_cache(
216
        const Version& spec_version, std::vector<Version>& version_path,
217
13
        const std::function<bool(int64_t, int64_t)>& validator) const {
218
13
    return _version_graph.capture_consistent_versions_prefer_cache(spec_version, version_path,
219
13
                                                                   validator);
220
13
}
221
222
Status TimestampedVersionTracker::capture_consistent_versions_with_validator_mow(
223
        const Version& spec_version, std::vector<Version>& version_path,
224
12
        const std::function<bool(int64_t, int64_t)>& validator) const {
225
12
    return _version_graph.capture_consistent_versions_with_validator_mow(spec_version, version_path,
226
12
                                                                         validator);
227
12
}
228
229
void TimestampedVersionTracker::capture_expired_paths(
230
326k
        int64_t stale_sweep_endtime, std::vector<int64_t>* path_version_vec) const {
231
326k
    std::map<int64_t, PathVersionListSharedPtr>::const_iterator iter =
232
326k
            _stale_version_path_map.begin();
233
234
333k
    while (iter != _stale_version_path_map.end()) {
235
7.54k
        int64_t max_create_time = iter->second->max_create_time();
236
7.54k
        if (max_create_time <= stale_sweep_endtime) {
237
2.74k
            int64_t path_version = iter->first;
238
2.74k
            path_version_vec->push_back(path_version);
239
2.74k
        }
240
7.54k
        ++iter;
241
7.54k
    }
242
326k
}
243
244
2.75k
PathVersionListSharedPtr TimestampedVersionTracker::fetch_path_version_by_id(int64_t path_id) {
245
2.75k
    if (_stale_version_path_map.count(path_id) == 0) {
246
0
        VLOG_NOTICE << "path version " << path_id << " does not exist!";
247
0
        return nullptr;
248
0
    }
249
250
2.75k
    return _stale_version_path_map[path_id];
251
2.75k
}
252
253
2.75k
PathVersionListSharedPtr TimestampedVersionTracker::fetch_and_delete_path_by_id(int64_t path_id) {
254
2.75k
    if (_stale_version_path_map.count(path_id) == 0) {
255
0
        VLOG_NOTICE << "path version " << path_id << " does not exist!";
256
0
        return nullptr;
257
0
    }
258
259
2.75k
    VLOG_NOTICE << get_current_path_map_str();
260
2.75k
    PathVersionListSharedPtr ptr = fetch_path_version_by_id(path_id);
261
262
2.75k
    _stale_version_path_map.erase(path_id);
263
264
15.7k
    for (auto& version : ptr->timestamped_versions()) {
265
15.7k
        static_cast<void>(_version_graph.delete_version_from_graph(version->version()));
266
15.7k
    }
267
2.75k
    return ptr;
268
2.75k
}
269
270
14
std::string TimestampedVersionTracker::get_current_path_map_str() {
271
14
    std::stringstream tracker_info;
272
14
    tracker_info << "current expired next_path_id " << _next_path_id << std::endl;
273
274
14
    std::map<int64_t, PathVersionListSharedPtr>::const_iterator iter =
275
14
            _stale_version_path_map.begin();
276
49
    while (iter != _stale_version_path_map.end()) {
277
35
        tracker_info << "current expired path_version " << iter->first;
278
35
        std::vector<TimestampedVersionSharedPtr>& timestamped_versions =
279
35
                iter->second->timestamped_versions();
280
35
        std::vector<TimestampedVersionSharedPtr>::iterator version_path_iter =
281
35
                timestamped_versions.begin();
282
35
        int64_t max_create_time = -1;
283
95
        while (version_path_iter != timestamped_versions.end()) {
284
60
            if (max_create_time < (*version_path_iter)->get_create_time()) {
285
35
                max_create_time = (*version_path_iter)->get_create_time();
286
35
            }
287
60
            tracker_info << " -> [";
288
60
            tracker_info << (*version_path_iter)->version().first;
289
60
            tracker_info << ",";
290
60
            tracker_info << (*version_path_iter)->version().second;
291
60
            tracker_info << "]";
292
293
60
            ++version_path_iter;
294
60
        }
295
296
35
        tracker_info << std::endl;
297
35
        ++iter;
298
35
    }
299
14
    return tracker_info.str();
300
14
}
301
302
1.69k
double TimestampedVersionTracker::get_orphan_vertex_ratio() {
303
1.69k
    return _version_graph.get_orphan_vertex_ratio();
304
1.69k
}
305
306
5
std::string TimestampedVersionTracker::debug_string() const {
307
5
    return _version_graph.debug_string();
308
5
}
309
310
48.3k
void TimestampedVersionPathContainer::add_timestamped_version(TimestampedVersionSharedPtr version) {
311
    // Compare and refresh `_max_create_time`.
312
48.3k
    if (version->get_create_time() > _max_create_time) {
313
14.9k
        _max_create_time = version->get_create_time();
314
14.9k
    }
315
48.3k
    _timestamped_versions_container.push_back(version);
316
48.3k
}
317
318
13.3k
std::vector<TimestampedVersionSharedPtr>& TimestampedVersionPathContainer::timestamped_versions() {
319
13.3k
    return _timestamped_versions_container;
320
13.3k
}
321
322
void VersionGraph::construct_version_graph(const RowsetMetaMapContainer& rs_metas,
323
0
                                           int64_t* max_version) {
324
0
    construct_version_graph(std::views::values(rs_metas), max_version);
325
0
}
326
327
void VersionGraph::reconstruct_version_graph(const RowsetMetaMapContainer& rs_metas,
328
0
                                             int64_t* max_version) {
329
0
    reconstruct_version_graph(std::views::values(rs_metas), max_version);
330
0
}
331
332
80.7k
void VersionGraph::add_version_to_graph(const Version& version) {
333
    // Add version.first as new vertex of version graph if not exist.
334
80.7k
    int64_t start_vertex_value = version.first;
335
80.7k
    int64_t end_vertex_value = version.second + 1;
336
337
    // Add vertex to graph.
338
80.7k
    _add_vertex_to_graph(start_vertex_value);
339
80.7k
    _add_vertex_to_graph(end_vertex_value);
340
341
80.7k
    int64_t start_vertex_index = _vertex_index_map[start_vertex_value];
342
80.7k
    int64_t end_vertex_index = _vertex_index_map[end_vertex_value];
343
344
    // We assume this version is new version, so we just add two edges
345
    // into version graph. add one edge from `start_version` to `end_version`
346
    // Make sure the vertex's edges are sorted by version in descending order when inserting.
347
80.7k
    auto end_vertex_it = _version_graph[start_vertex_index].edges.begin();
348
86.2k
    while (end_vertex_it != _version_graph[start_vertex_index].edges.end()) {
349
71.1k
        if (_version_graph[*end_vertex_it].value < _version_graph[end_vertex_index].value) {
350
65.5k
            break;
351
65.5k
        }
352
5.56k
        end_vertex_it++;
353
5.56k
    }
354
80.7k
    _version_graph[start_vertex_index].edges.insert(end_vertex_it, end_vertex_index);
355
356
    // We add reverse edge(from end_version to start_version) to graph
357
    // Make sure the vertex's edges are sorted by version in descending order when inserting.
358
80.7k
    auto start_vertex_it = _version_graph[end_vertex_index].edges.begin();
359
96.0k
    while (start_vertex_it != _version_graph[end_vertex_index].edges.end()) {
360
19.6k
        if (_version_graph[*start_vertex_it].value < _version_graph[start_vertex_index].value) {
361
4.34k
            break;
362
4.34k
        }
363
15.2k
        start_vertex_it++;
364
15.2k
    }
365
80.7k
    _version_graph[end_vertex_index].edges.insert(start_vertex_it, start_vertex_index);
366
80.7k
}
367
368
15.7k
Status VersionGraph::delete_version_from_graph(const Version& version) {
369
15.7k
    int64_t start_vertex_value = version.first;
370
15.7k
    int64_t end_vertex_value = version.second + 1;
371
372
15.7k
    if (_vertex_index_map.find(start_vertex_value) == _vertex_index_map.end() ||
373
15.7k
        _vertex_index_map.find(end_vertex_value) == _vertex_index_map.end()) {
374
0
        return Status::Error<HEADER_DELETE_VERSION>(
375
0
                "vertex for version does not exists. version={}-{}", version.first, version.second);
376
0
    }
377
378
15.7k
    int64_t start_vertex_index = _vertex_index_map[start_vertex_value];
379
15.7k
    int64_t end_vertex_index = _vertex_index_map[end_vertex_value];
380
    // Remove edge and its reverse edge.
381
    // When there are same versions in edges, just remove the first version.
382
15.7k
    auto start_edges_iter = _version_graph[start_vertex_index].edges.begin();
383
23.2k
    while (start_edges_iter != _version_graph[start_vertex_index].edges.end()) {
384
23.2k
        if (*start_edges_iter == end_vertex_index) {
385
15.7k
            _version_graph[start_vertex_index].edges.erase(start_edges_iter);
386
15.7k
            break;
387
15.7k
        }
388
7.52k
        start_edges_iter++;
389
7.52k
    }
390
391
15.7k
    auto end_edges_iter = _version_graph[end_vertex_index].edges.begin();
392
30.7k
    while (end_edges_iter != _version_graph[end_vertex_index].edges.end()) {
393
30.7k
        if (*end_edges_iter == start_vertex_index) {
394
15.7k
            _version_graph[end_vertex_index].edges.erase(end_edges_iter);
395
15.7k
            break;
396
15.7k
        }
397
14.9k
        end_edges_iter++;
398
14.9k
    }
399
400
    // Here we do not delete vertex in `_version_graph` even if its edges are empty.
401
    // the `_version_graph` will be rebuilt when doing trash sweep.
402
15.7k
    return Status::OK();
403
15.7k
}
404
405
1.03M
void VersionGraph::_add_vertex_to_graph(int64_t vertex_value) {
406
    // Vertex with vertex_value already exists.
407
1.03M
    if (_vertex_index_map.find(vertex_value) != _vertex_index_map.end()) {
408
81.8k
        VLOG_NOTICE << "vertex with vertex value already exists. value=" << vertex_value;
409
81.8k
        return;
410
81.8k
    }
411
412
955k
    _version_graph.emplace_back(Vertex(vertex_value));
413
955k
    _vertex_index_map[vertex_value] = _version_graph.size() - 1;
414
955k
}
415
416
Status VersionGraph::capture_consistent_versions(const Version& spec_version,
417
364k
                                                 std::vector<Version>* version_path) const {
418
364k
    if (spec_version.first > spec_version.second) {
419
0
        return Status::Error<INVALID_ARGUMENT, false>(
420
0
                "invalid specified version. spec_version={}-{}", spec_version.first,
421
0
                spec_version.second);
422
0
    }
423
424
364k
    int64_t cur_idx = -1;
425
364k
    for (size_t i = 0; i < _version_graph.size(); i++) {
426
364k
        if (_version_graph[i].value == spec_version.first) {
427
364k
            cur_idx = i;
428
364k
            break;
429
364k
        }
430
364k
    }
431
432
364k
    if (cur_idx < 0) {
433
0
        return Status::InternalError<false>(
434
0
                "failed to find path in version_graph. spec_version: {}-{}", spec_version.first,
435
0
                spec_version.second);
436
0
    }
437
438
364k
    int64_t end_value = spec_version.second + 1;
439
1.69M
    while (_version_graph[cur_idx].value < end_value) {
440
1.33M
        int64_t next_idx = -1;
441
1.33M
        for (const auto& it : _version_graph[cur_idx].edges) {
442
            // Only consider incremental versions.
443
1.33M
            if (_version_graph[it].value < _version_graph[cur_idx].value) {
444
1
                break;
445
1
            }
446
447
1.33M
            if (_version_graph[it].value > end_value) {
448
4
                continue;
449
4
            }
450
451
            // Considering edges had been sorted by version in descending order,
452
            // This version is the largest version that smaller than `end_version`.
453
1.33M
            next_idx = it;
454
1.33M
            break;
455
1.33M
        }
456
457
1.33M
        if (next_idx > -1) {
458
1.33M
            if (version_path != nullptr) {
459
1.33M
                version_path->emplace_back(_version_graph[cur_idx].value,
460
1.33M
                                           _version_graph[next_idx].value - 1);
461
1.33M
            }
462
1.33M
            cur_idx = next_idx;
463
1.33M
        } else {
464
1.43k
            return Status::InternalError<false>(
465
1.43k
                    "fail to find path in version_graph. spec_version: {}-{}", spec_version.first,
466
1.43k
                    spec_version.second);
467
1.43k
        }
468
1.33M
    }
469
470
362k
    if (VLOG_TRACE_IS_ON && version_path != nullptr) {
471
0
        std::stringstream shortest_path_for_debug;
472
0
        for (const auto& version : *version_path) {
473
0
            shortest_path_for_debug << version << ' ';
474
0
        }
475
0
        VLOG_TRACE << "success to find path for spec_version. spec_version=" << spec_version
476
0
                   << ", path=" << shortest_path_for_debug.str();
477
0
    }
478
479
362k
    return Status::OK();
480
364k
}
481
482
Status VersionGraph::capture_consistent_versions_prefer_cache(
483
        const Version& spec_version, std::vector<Version>& version_path,
484
13
        const std::function<bool(int64_t, int64_t)>& validator) const {
485
13
    if (spec_version.first > spec_version.second) {
486
0
        return Status::Error<INVALID_ARGUMENT, false>(
487
0
                "invalid specified version. spec_version={}-{}", spec_version.first,
488
0
                spec_version.second);
489
0
    }
490
491
13
    int64_t cur_idx = -1;
492
13
    for (size_t i = 0; i < _version_graph.size(); i++) {
493
13
        if (_version_graph[i].value == spec_version.first) {
494
13
            cur_idx = i;
495
13
            break;
496
13
        }
497
13
    }
498
499
13
    if (cur_idx < 0) {
500
0
        return Status::InternalError<false>("failed to find path in version_graph. spec_version={}",
501
0
                                            spec_version.to_string());
502
0
    }
503
504
13
    int64_t end_value = spec_version.second + 1;
505
90
    while (_version_graph[cur_idx].value < end_value) {
506
77
        int64_t next_idx = -1;
507
77
        int64_t first_idx = -1;
508
107
        for (const auto& it : _version_graph[cur_idx].edges) {
509
            // Only consider incremental versions.
510
107
            if (_version_graph[it].value < _version_graph[cur_idx].value) {
511
15
                break;
512
15
            }
513
92
            if (first_idx == -1) {
514
77
                first_idx = it;
515
77
            }
516
517
92
            if (!validator(_version_graph[cur_idx].value, _version_graph[it].value - 1)) {
518
30
                continue;
519
30
            }
520
521
62
            next_idx = it;
522
62
            break;
523
92
        }
524
525
77
        if (next_idx > -1) {
526
62
            version_path.emplace_back(_version_graph[cur_idx].value,
527
62
                                      _version_graph[next_idx].value - 1);
528
529
62
            cur_idx = next_idx;
530
62
        } else if (first_idx != -1) {
531
            // if all edges are not in cache, use the first edge if possible
532
15
            version_path.emplace_back(_version_graph[cur_idx].value,
533
15
                                      _version_graph[first_idx].value - 1);
534
15
            cur_idx = first_idx;
535
15
        } else {
536
0
            return Status::OK();
537
0
        }
538
77
    }
539
13
    return Status::OK();
540
13
}
541
542
Status VersionGraph::capture_consistent_versions_with_validator(
543
        const Version& spec_version, std::vector<Version>& version_path,
544
12
        const std::function<bool(int64_t, int64_t)>& validator) const {
545
12
    if (spec_version.first > spec_version.second) {
546
0
        return Status::Error<INVALID_ARGUMENT, false>(
547
0
                "invalid specified version. spec_version={}-{}", spec_version.first,
548
0
                spec_version.second);
549
0
    }
550
551
12
    int64_t cur_idx = -1;
552
12
    for (size_t i = 0; i < _version_graph.size(); i++) {
553
12
        if (_version_graph[i].value == spec_version.first) {
554
12
            cur_idx = i;
555
12
            break;
556
12
        }
557
12
    }
558
559
12
    if (cur_idx < 0) {
560
0
        return Status::InternalError<false>("failed to find path in version_graph. spec_version={}",
561
0
                                            spec_version.to_string());
562
0
    }
563
564
12
    int64_t end_value = spec_version.second + 1;
565
67
    while (_version_graph[cur_idx].value < end_value) {
566
62
        int64_t next_idx = -1;
567
74
        for (const auto& it : _version_graph[cur_idx].edges) {
568
            // Only consider incremental versions.
569
74
            if (_version_graph[it].value < _version_graph[cur_idx].value) {
570
7
                break;
571
7
            }
572
573
67
            if (!validator(_version_graph[cur_idx].value, _version_graph[it].value - 1)) {
574
12
                continue;
575
12
            }
576
577
55
            next_idx = it;
578
55
            break;
579
67
        }
580
581
62
        if (next_idx > -1) {
582
55
            version_path.emplace_back(_version_graph[cur_idx].value,
583
55
                                      _version_graph[next_idx].value - 1);
584
585
55
            cur_idx = next_idx;
586
55
        } else {
587
7
            return Status::OK();
588
7
        }
589
62
    }
590
5
    return Status::OK();
591
12
}
592
593
Status VersionGraph::capture_consistent_versions_with_validator_mow(
594
        const Version& spec_version, std::vector<Version>& version_path,
595
12
        const std::function<bool(int64_t, int64_t)>& validator) const {
596
12
    if (spec_version.first > spec_version.second) {
597
0
        return Status::Error<INVALID_ARGUMENT, false>(
598
0
                "invalid specified version. spec_version={}-{}", spec_version.first,
599
0
                spec_version.second);
600
0
    }
601
602
12
    int64_t cur_idx = -1;
603
12
    for (size_t i = 0; i < _version_graph.size(); i++) {
604
12
        if (_version_graph[i].value == spec_version.first) {
605
12
            cur_idx = i;
606
12
            break;
607
12
        }
608
12
    }
609
610
12
    if (cur_idx < 0) {
611
0
        return Status::InternalError<false>("failed to find path in version_graph. spec_version={}",
612
0
                                            spec_version.to_string());
613
0
    }
614
615
12
    int64_t end_value = spec_version.second + 1;
616
67
    while (_version_graph[cur_idx].value < end_value) {
617
61
        int64_t next_idx = -1;
618
71
        for (const auto& it : _version_graph[cur_idx].edges) {
619
            // Only consider incremental versions.
620
71
            if (_version_graph[it].value < _version_graph[cur_idx].value) {
621
0
                break;
622
0
            }
623
624
71
            if (!validator(_version_graph[cur_idx].value, _version_graph[it].value - 1)) {
625
16
                if (_version_graph[cur_idx].value + 1 == _version_graph[it].value) {
626
6
                    break;
627
6
                }
628
10
                end_value = std::min(_version_graph[it].value, end_value);
629
10
                continue;
630
16
            }
631
632
55
            next_idx = it;
633
55
            break;
634
71
        }
635
636
61
        if (next_idx > -1) {
637
55
            version_path.emplace_back(_version_graph[cur_idx].value,
638
55
                                      _version_graph[next_idx].value - 1);
639
640
55
            cur_idx = next_idx;
641
55
        } else {
642
6
            return Status::OK();
643
6
        }
644
61
    }
645
6
    return Status::OK();
646
12
}
647
648
1.69k
double VersionGraph::get_orphan_vertex_ratio() {
649
1.69k
    int64_t vertex_num = _version_graph.size();
650
1.69k
    int64_t orphan_vertex_num = 0;
651
28.7k
    for (auto& iter : _version_graph) {
652
28.7k
        if (iter.edges.empty()) {
653
13.0k
            ++orphan_vertex_num;
654
13.0k
        }
655
28.7k
    }
656
1.69k
    return static_cast<double>(orphan_vertex_num) / static_cast<double>(vertex_num);
657
1.69k
}
658
659
5
std::string VersionGraph::debug_string() const {
660
5
    std::stringstream ss;
661
5
    ss << "VersionGraph: [";
662
100
    for (size_t i = 0; i < _version_graph.size(); ++i) {
663
95
        ss << "{value: " << _version_graph[i].value << ", edges: [";
664
208
        for (const auto& edge : _version_graph[i].edges) {
665
208
            if (_version_graph[edge].value > _version_graph[i].value) {
666
104
                ss << _version_graph[edge].value << ", ";
667
104
            }
668
208
        }
669
95
        ss << "]}, ";
670
95
    }
671
5
    ss << "]";
672
5
    return ss.str();
673
5
}
674
675
} // namespace doris