Coverage Report

Created: 2026-06-03 04:12

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
be/src/io/cache/file_block.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 "io/cache/file_block.h"
19
20
#include <glog/logging.h>
21
// IWYU pragma: no_include <bits/chrono.h>
22
#include <chrono> // IWYU pragma: keep
23
#include <sstream>
24
#include <string>
25
#include <thread>
26
27
#include "common/status.h"
28
#include "cpp/sync_point.h"
29
#include "io/cache/block_file_cache.h"
30
31
namespace doris {
32
namespace io {
33
34
26.6k
std::ostream& operator<<(std::ostream& os, const FileBlock::State& value) {
35
26.6k
    os << FileBlock::state_to_string(value);
36
26.6k
    return os;
37
26.6k
}
38
39
FileBlock::FileBlock(const FileCacheKey& key, size_t size, BlockFileCache* mgr,
40
                     State download_state)
41
227k
        : _block_range(key.offset, key.offset + size - 1),
42
227k
          _download_state(download_state),
43
227k
          _mgr(mgr),
44
227k
          _key(key) {
45
    /// On creation, file block state can be EMPTY, DOWNLOADED, SKIP_CACHE.
46
227k
    switch (_download_state) {
47
0
    case State::DOWNLOADING: {
48
0
        DCHECK(false) << "Can create cell with either EMPTY, DOWNLOADED, SKIP_CACHE ";
49
0
        break;
50
0
    }
51
227k
    default: {
52
227k
        break;
53
0
    }
54
227k
    }
55
227k
}
56
57
3.37M
FileBlock::State FileBlock::state() const {
58
3.37M
    std::lock_guard block_lock(_mutex);
59
3.37M
    return _download_state;
60
3.37M
}
61
62
998k
FileBlock::State FileBlock::state_unsafe() const {
63
998k
    return _download_state;
64
998k
}
65
66
1.24M
uint64_t FileBlock::get_caller_id() {
67
1.24M
    uint64_t id;
68
#if defined(__APPLE__)
69
    // On macOS, use pthread_threadid_np to get the thread ID
70
    pthread_threadid_np(nullptr, &id);
71
#else
72
1.24M
    id = static_cast<uint64_t>(pthread_self());
73
1.24M
#endif
74
1.24M
    DCHECK(id != 0);
75
1.24M
    return id;
76
1.24M
}
77
78
118k
uint64_t FileBlock::get_or_set_downloader() {
79
118k
    std::lock_guard block_lock(_mutex);
80
81
118k
    if (_downloader_id == 0 && _download_state != State::DOWNLOADED) {
82
118k
        DCHECK(_download_state != State::DOWNLOADING);
83
118k
        _downloader_id = get_caller_id();
84
118k
        _download_state = State::DOWNLOADING;
85
118k
    } else if (_downloader_id == get_caller_id()) {
86
4
        LOG(INFO) << "Attempt to set the same downloader for block " << range().to_string()
87
4
                  << " for the second time";
88
4
    }
89
90
118k
    return _downloader_id;
91
118k
}
92
93
24
void FileBlock::reset_downloader(std::lock_guard<std::mutex>& block_lock) {
94
24
    DCHECK(_downloader_id != 0) << "There is no downloader";
95
96
24
    DCHECK(get_caller_id() == _downloader_id) << "Downloader can be reset only by downloader";
97
98
24
    reset_downloader_impl(block_lock);
99
24
}
100
101
24
void FileBlock::reset_downloader_impl(std::lock_guard<std::mutex>& block_lock) {
102
24
    if (_downloaded_size == range().size()) {
103
2
        Status st = set_downloaded(block_lock);
104
2
        if (!st.ok()) {
105
1
            LOG(WARNING) << "reset downloader error" << st;
106
1
        }
107
22
    } else {
108
22
        _downloaded_size = 0;
109
22
        _download_state = State::EMPTY;
110
22
        _downloader_id = 0;
111
22
    }
112
24
}
113
114
118k
Status FileBlock::set_downloaded(std::lock_guard<std::mutex>& /* block_lock */) {
115
118k
    DCHECK(_download_state != State::DOWNLOADED);
116
118k
    if (_downloaded_size == 0) {
117
1
        _download_state = State::EMPTY;
118
1
        _downloader_id = 0;
119
1
        return Status::InternalError("Try to set empty block {} as downloaded",
120
1
                                     _block_range.to_string());
121
1
    }
122
118k
    Status status = _mgr->_storage->finalize(_key, this->_block_range.size());
123
118k
    if (status.ok()) [[likely]] {
124
117k
        _download_state = State::DOWNLOADED;
125
117k
    } else {
126
586
        _download_state = State::EMPTY;
127
586
        _downloaded_size = 0;
128
586
    }
129
118k
    _downloader_id = 0;
130
118k
    return status;
131
118k
}
132
133
16
uint64_t FileBlock::get_downloader() const {
134
16
    std::lock_guard block_lock(_mutex);
135
16
    return _downloader_id;
136
16
}
137
138
113k
bool FileBlock::is_downloader() const {
139
113k
    std::lock_guard block_lock(_mutex);
140
113k
    return get_caller_id() == _downloader_id;
141
113k
}
142
143
998k
bool FileBlock::is_downloader_impl(std::lock_guard<std::mutex>& /* block_lock */) const {
144
998k
    return get_caller_id() == _downloader_id;
145
998k
}
146
147
118k
Status FileBlock::append(Slice data) {
148
18.4E
    DCHECK(data.size != 0) << "Writing zero size is not allowed";
149
118k
    RETURN_IF_ERROR(_mgr->_storage->append(_key, data));
150
118k
    _downloaded_size += data.size;
151
118k
    return Status::OK();
152
118k
}
153
154
118k
Status FileBlock::finalize() {
155
118k
    if (_downloaded_size == 0) {
156
1
        std::lock_guard block_lock(_mutex);
157
1
        _download_state = State::EMPTY;
158
1
        _downloader_id = 0;
159
1
        _cv.notify_all();
160
1
        return Status::InternalError("Try to finalize an empty file block {}",
161
1
                                     _block_range.to_string());
162
1
    }
163
118k
    if (_downloaded_size != _block_range.size()) {
164
5.55k
        SCOPED_CACHE_LOCK(_mgr->_mutex, _mgr);
165
5.55k
        size_t old_size = _block_range.size();
166
5.55k
        size_t new_size = _downloaded_size;
167
5.55k
        DCHECK(new_size < old_size);
168
5.55k
        _mgr->reset_range(_key.hash, _block_range.left, old_size, new_size, cache_lock);
169
5.55k
    }
170
118k
    std::lock_guard block_lock(_mutex);
171
118k
    Status st = set_downloaded(block_lock);
172
118k
    _cv.notify_all();
173
118k
    return st;
174
118k
}
175
176
3.56M
Status FileBlock::read(Slice buffer, size_t read_offset) {
177
3.56M
    return _mgr->_storage->read(_key, read_offset, buffer);
178
3.56M
}
179
180
43
Status FileBlock::change_cache_type(FileCacheType new_type) {
181
43
    SCOPED_CACHE_LOCK(_mgr->_mutex, _mgr);
182
43
    return change_cache_type_lock(new_type, cache_lock);
183
43
}
184
185
Status FileBlock::change_cache_type_lock(FileCacheType new_type,
186
44
                                         std::lock_guard<std::mutex>& cache_lock) {
187
44
    std::lock_guard block_lock(_mutex);
188
189
44
    if (new_type == _key.meta.type) {
190
1
        return Status::OK();
191
1
    }
192
43
    if (_download_state == State::DOWNLOADED) {
193
6
        Status st;
194
6
        TEST_SYNC_POINT_CALLBACK("FileBlock::change_cache_type", &st);
195
6
        RETURN_IF_ERROR(_mgr->_storage->change_key_meta_type(_key, new_type, _block_range.size()));
196
6
    }
197
43
    _mgr->change_cache_type(_key.hash, _block_range.left, new_type, cache_lock);
198
43
    _key.meta.type = new_type;
199
43
    return Status::OK();
200
43
}
201
202
8.03k
FileBlock::State FileBlock::wait() {
203
8.03k
    std::unique_lock block_lock(_mutex);
204
205
8.03k
    if (_downloader_id == 0) {
206
1
        return _download_state;
207
1
    }
208
209
8.03k
    if (_download_state == State::DOWNLOADING) {
210
8.03k
        DCHECK(_downloader_id != 0 && _downloader_id != get_caller_id());
211
8.03k
        _cv.wait_for(block_lock, std::chrono::milliseconds(config::block_cache_wait_timeout_ms));
212
8.03k
    }
213
214
8.03k
    return _download_state;
215
8.03k
}
216
217
998k
void FileBlock::complete_unlocked(std::lock_guard<std::mutex>& block_lock) {
218
998k
    if (is_downloader_impl(block_lock)) {
219
24
        reset_downloader(block_lock);
220
24
        _cv.notify_all();
221
24
    }
222
998k
}
223
224
1
std::string FileBlock::get_info_for_log() const {
225
1
    std::lock_guard block_lock(_mutex);
226
1
    return get_info_for_log_impl(block_lock);
227
1
}
228
229
1
std::string FileBlock::get_info_for_log_impl(std::lock_guard<std::mutex>& block_lock) const {
230
1
    std::stringstream info;
231
1
    info << "File block: " << range().to_string() << ", ";
232
1
    info << "state: " << state_to_string(_download_state) << ", ";
233
1
    info << "size: " << _block_range.size() << ", ";
234
1
    info << "downloader id: " << _downloader_id << ", ";
235
1
    info << "caller id: " << get_caller_id();
236
237
1
    return info.str();
238
1
}
239
240
843k
FileBlock::State FileBlock::state_unlock(std::lock_guard<std::mutex>&) const {
241
843k
    return _download_state;
242
843k
}
243
244
26.6k
std::string FileBlock::state_to_string(FileBlock::State state) {
245
26.6k
    switch (state) {
246
26.6k
    case FileBlock::State::DOWNLOADED:
247
26.6k
        return "DOWNLOADED";
248
2
    case FileBlock::State::EMPTY:
249
2
        return "EMPTY";
250
2
    case FileBlock::State::DOWNLOADING:
251
2
        return "DOWNLOADING";
252
1
    case FileBlock::State::SKIP_CACHE:
253
1
        return "SKIP_CACHE";
254
0
    default:
255
0
        DCHECK(false);
256
0
        return "";
257
26.6k
    }
258
26.6k
}
259
260
0
std::string FileBlock::get_cache_file() const {
261
0
    return _mgr->_storage->get_local_file(this->_key);
262
0
}
263
264
968k
FileBlocksHolder::~FileBlocksHolder() {
265
1.96M
    for (auto file_block_it = file_blocks.begin(); file_block_it != file_blocks.end();) {
266
998k
        auto current_file_block_it = file_block_it;
267
998k
        auto& file_block = *current_file_block_it;
268
998k
        BlockFileCache* _mgr = file_block->_mgr;
269
998k
        {
270
998k
            bool should_remove = false;
271
998k
            {
272
998k
                std::lock_guard block_lock(file_block->_mutex);
273
998k
                file_block->complete_unlocked(block_lock);
274
998k
                if (file_block.use_count() == 2 &&
275
998k
                    (file_block->is_deleting() ||
276
641k
                     file_block->state_unlock(block_lock) == FileBlock::State::EMPTY)) {
277
23.8k
                    should_remove = true;
278
23.8k
                }
279
998k
            }
280
998k
            if (should_remove) {
281
23.8k
                SCOPED_CACHE_LOCK(_mgr->_mutex, _mgr);
282
23.8k
                std::lock_guard block_lock(file_block->_mutex);
283
23.8k
                if (file_block.use_count() == 2) {
284
23.8k
                    DCHECK(file_block->state_unlock(block_lock) != FileBlock::State::DOWNLOADING);
285
                    // one in cache, one in here
286
23.8k
                    if (file_block->is_deleting() ||
287
23.8k
                        file_block->state_unlock(block_lock) == FileBlock::State::EMPTY) {
288
23.8k
                        _mgr->remove(file_block, cache_lock, block_lock, false);
289
23.8k
                    }
290
23.8k
                }
291
23.8k
            }
292
998k
        }
293
998k
        file_block_it = file_blocks.erase(current_file_block_it);
294
998k
    }
295
968k
}
296
297
} // namespace io
298
} // namespace doris