Coverage Report

Created: 2026-08-06 13:04

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