Coverage Report

Created: 2026-03-12 17:06

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
be/src/cloud/cloud_compaction_stop_token.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 "cloud/cloud_compaction_stop_token.h"
19
20
#include <gen_cpp/cloud.pb.h>
21
22
#include "cloud/cloud_meta_mgr.h"
23
#include "cloud/config.h"
24
#include "common/logging.h"
25
26
namespace doris {
27
28
CloudCompactionStopToken::CloudCompactionStopToken(CloudStorageEngine& engine,
29
                                                   CloudTabletSPtr tablet, int64_t initiator)
30
1.18k
        : _engine {engine}, _tablet {std::move(tablet)}, _initiator(initiator) {
31
1.18k
    auto uuid = UUIDGenerator::instance()->next_uuid();
32
1.18k
    std::stringstream ss;
33
1.18k
    ss << uuid;
34
1.18k
    _uuid = ss.str();
35
1.18k
}
36
37
2
void CloudCompactionStopToken::do_lease() {
38
2
    cloud::TabletJobInfoPB job;
39
2
    auto* idx = job.mutable_idx();
40
2
    idx->set_tablet_id(_tablet->tablet_id());
41
2
    idx->set_table_id(_tablet->table_id());
42
2
    idx->set_index_id(_tablet->index_id());
43
2
    idx->set_partition_id(_tablet->partition_id());
44
2
    auto* compaction_job = job.add_compaction();
45
2
    compaction_job->set_id(_uuid);
46
2
    using namespace std::chrono;
47
2
    int64_t lease_time = duration_cast<seconds>(system_clock::now().time_since_epoch()).count() +
48
2
                         (config::lease_compaction_interval_seconds * 4);
49
2
    compaction_job->set_lease(lease_time);
50
2
    auto st = _engine.meta_mgr().lease_tablet_job(job);
51
2
    if (!st.ok()) {
52
0
        LOG_WARNING("failed to lease compaction stop token")
53
0
                .tag("job_id", _uuid)
54
0
                .tag("delete_bitmap_lock_initiator", _initiator)
55
0
                .tag("tablet_id", _tablet->tablet_id())
56
0
                .error(st);
57
0
    }
58
2
}
59
60
1.18k
Status CloudCompactionStopToken::do_register() {
61
1.18k
    int64_t base_compaction_cnt = 0;
62
1.18k
    int64_t cumulative_compaction_cnt = 0;
63
1.18k
    {
64
1.18k
        std::lock_guard lock {_tablet->get_header_lock()};
65
1.18k
        base_compaction_cnt = _tablet->base_compaction_cnt();
66
1.18k
        cumulative_compaction_cnt = _tablet->cumulative_compaction_cnt();
67
1.18k
    }
68
1.18k
    cloud::TabletJobInfoPB job;
69
1.18k
    auto* idx = job.mutable_idx();
70
1.18k
    idx->set_tablet_id(_tablet->tablet_id());
71
1.18k
    idx->set_table_id(_tablet->table_id());
72
1.18k
    idx->set_index_id(_tablet->index_id());
73
1.18k
    idx->set_partition_id(_tablet->partition_id());
74
1.18k
    auto* compaction_job = job.add_compaction();
75
1.18k
    compaction_job->set_id(_uuid);
76
1.18k
    compaction_job->set_delete_bitmap_lock_initiator(_initiator);
77
1.18k
    compaction_job->set_initiator(BackendOptions::get_localhost() + ':' +
78
1.18k
                                  std::to_string(config::heartbeat_service_port));
79
1.18k
    compaction_job->set_type(cloud::TabletCompactionJobPB::STOP_TOKEN);
80
    // required by MS to check if it's a valid compaction job
81
1.18k
    compaction_job->set_base_compaction_cnt(base_compaction_cnt);
82
1.18k
    compaction_job->set_cumulative_compaction_cnt(cumulative_compaction_cnt);
83
1.18k
    using namespace std::chrono;
84
1.18k
    int64_t now = duration_cast<seconds>(system_clock::now().time_since_epoch()).count();
85
1.18k
    compaction_job->set_expiration(now + config::compaction_timeout_seconds);
86
1.18k
    compaction_job->set_lease(now + (config::lease_compaction_interval_seconds * 4));
87
1.18k
    cloud::StartTabletJobResponse resp;
88
1.18k
    auto st = _engine.meta_mgr().prepare_tablet_job(job, &resp);
89
1.18k
    if (!st.ok()) {
90
0
        LOG_WARNING("failed to register compaction stop token")
91
0
                .tag("job_id", _uuid)
92
0
                .tag("delete_bitmap_lock_initiator", _initiator)
93
0
                .tag("tablet_id", _tablet->tablet_id())
94
0
                .error(st);
95
0
    }
96
1.18k
    return st;
97
1.18k
}
98
99
0
Status CloudCompactionStopToken::do_unregister() {
100
0
    cloud::TabletJobInfoPB job;
101
0
    auto* idx = job.mutable_idx();
102
0
    idx->set_tablet_id(_tablet->tablet_id());
103
0
    idx->set_table_id(_tablet->table_id());
104
0
    idx->set_index_id(_tablet->index_id());
105
0
    idx->set_partition_id(_tablet->partition_id());
106
0
    auto* compaction_job = job.add_compaction();
107
0
    compaction_job->set_id(_uuid);
108
0
    compaction_job->set_delete_bitmap_lock_initiator(_initiator);
109
0
    compaction_job->set_initiator(BackendOptions::get_localhost() + ':' +
110
0
                                  std::to_string(config::heartbeat_service_port));
111
0
    compaction_job->set_type(cloud::TabletCompactionJobPB::STOP_TOKEN);
112
0
    auto st = _engine.meta_mgr().abort_tablet_job(job);
113
0
    if (!st.ok()) {
114
0
        LOG_WARNING("failed to unregister compaction stop token")
115
0
                .tag("job_id", _uuid)
116
0
                .tag("delete_bitmap_lock_initiator", _initiator)
117
0
                .tag("tablet_id", _tablet->tablet_id())
118
0
                .error(st);
119
0
    }
120
0
    return st;
121
0
}
122
123
0
int64_t CloudCompactionStopToken::initiator() const {
124
0
    return _initiator;
125
0
}
126
} // namespace doris