Coverage Report

Created: 2026-07-12 08:37

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 <butil/iobuf.h>
21
#include <glog/logging.h>
22
// IWYU pragma: no_include <bits/chrono.h>
23
#include <chrono> // IWYU pragma: keep
24
#include <sstream>
25
#include <string>
26
#include <thread>
27
28
#include "common/status.h"
29
#include "cpp/sync_point.h"
30
#include "io/cache/block_file_cache.h"
31
#include "io/cache/fs_file_cache_storage.h"
32
#include "io/cache/mem_file_cache_storage.h"
33
34
namespace doris {
35
namespace io {
36
37
namespace {
38
39
22
Status abort_pending_cache_write(FileCacheStorage* storage, const FileCacheKey& key) {
40
22
    if (auto* fs_storage = dynamic_cast<FSFileCacheStorage*>(storage); fs_storage != nullptr) {
41
22
        return fs_storage->abort(key);
42
22
    }
43
0
    if (auto* mem_storage = dynamic_cast<MemFileCacheStorage*>(storage); mem_storage != nullptr) {
44
0
        return mem_storage->abort(key);
45
0
    }
46
0
    return Status::OK();
47
0
}
48
49
} // namespace
50
51
2.69k
std::ostream& operator<<(std::ostream& os, const FileBlock::State& value) {
52
2.69k
    os << FileBlock::state_to_string(value);
53
2.69k
    return os;
54
2.69k
}
55
56
FileBlock::FileBlock(const FileCacheKey& key, size_t size, BlockFileCache* mgr,
57
                     State download_state)
58
360k
        : _block_range(key.offset, key.offset + size - 1),
59
360k
          _download_state(download_state),
60
360k
          _mgr(mgr),
61
360k
          _key(key) {
62
    /// On creation, file block state can be EMPTY, DOWNLOADED, SKIP_CACHE.
63
360k
    switch (_download_state) {
64
0
    case State::DOWNLOADING: {
65
0
        DCHECK(false) << "Can create cell with either EMPTY, DOWNLOADED, SKIP_CACHE ";
66
0
        break;
67
0
    }
68
360k
    default: {
69
360k
        break;
70
0
    }
71
360k
    }
72
360k
}
73
74
3.39M
FileBlock::State FileBlock::state() const {
75
3.39M
    std::lock_guard block_lock(_mutex);
76
3.39M
    return _download_state;
77
3.39M
}
78
79
1.61M
FileBlock::State FileBlock::state_unsafe() const {
80
1.61M
    return _download_state;
81
1.61M
}
82
83
1.87M
uint64_t FileBlock::get_caller_id() {
84
1.87M
    uint64_t id;
85
#if defined(__APPLE__)
86
    // On macOS, use pthread_threadid_np to get the thread ID
87
    pthread_threadid_np(nullptr, &id);
88
#else
89
1.87M
    id = static_cast<uint64_t>(pthread_self());
90
1.87M
#endif
91
1.87M
    DCHECK(id != 0);
92
1.87M
    return id;
93
1.87M
}
94
95
126k
uint64_t FileBlock::get_or_set_downloader() {
96
126k
    std::lock_guard block_lock(_mutex);
97
98
126k
    if (_downloader_id == 0 && _download_state != State::DOWNLOADED) {
99
126k
        DCHECK(_download_state != State::DOWNLOADING);
100
126k
        _downloader_id = get_caller_id();
101
126k
        _download_state = State::DOWNLOADING;
102
126k
    } else if (_downloader_id == get_caller_id()) {
103
8
        LOG(INFO) << "Attempt to set the same downloader for block " << range().to_string()
104
8
                  << " for the second time";
105
8
    }
106
107
126k
    return _downloader_id;
108
126k
}
109
110
44
void FileBlock::reset_downloader(std::lock_guard<std::mutex>& block_lock) {
111
44
    DCHECK(_downloader_id != 0) << "There is no downloader";
112
113
44
    DCHECK(get_caller_id() == _downloader_id) << "Downloader can be reset only by downloader";
114
115
44
    reset_downloader_impl(block_lock);
116
44
}
117
118
44
void FileBlock::reset_downloader_impl(std::lock_guard<std::mutex>& block_lock) {
119
44
    if (_downloaded_size == range().size()) {
120
4
        Status st = set_downloaded(block_lock);
121
4
        if (!st.ok()) {
122
2
            LOG(WARNING) << "reset downloader failed, err=" << st
123
2
                         << ", block=" << get_info_for_log_impl(block_lock);
124
2
        }
125
40
    } else {
126
40
        _downloaded_size = 0;
127
40
        _download_state = State::EMPTY;
128
40
        _downloader_id = 0;
129
40
    }
130
44
}
131
132
126k
Status FileBlock::set_downloaded(std::lock_guard<std::mutex>& block_lock) {
133
126k
    DCHECK(_download_state != State::DOWNLOADED);
134
126k
    if (_downloaded_size == 0) {
135
2
        _download_state = State::EMPTY;
136
2
        _downloader_id = 0;
137
2
        LOG(WARNING) << "set file cache block downloaded failed: empty block, block="
138
2
                     << get_info_for_log_impl(block_lock);
139
2
        return Status::InternalError("Try to set empty block {} as downloaded",
140
2
                                     _block_range.to_string());
141
2
    }
142
126k
    Status status = _mgr->_storage->finalize(_key, this->_block_range.size());
143
126k
    if (status.ok()) [[likely]] {
144
125k
        _download_state = State::DOWNLOADED;
145
125k
    } else {
146
486
        auto abort_st = abort_pending_cache_write(_mgr->_storage.get(), _key);
147
486
        LOG(WARNING) << "finalize file cache block failed, err=" << status
148
486
                     << ", abort_status=" << abort_st
149
486
                     << ", block=" << get_info_for_log_impl(block_lock);
150
486
        _download_state = State::EMPTY;
151
486
        _downloaded_size = 0;
152
486
    }
153
126k
    _downloader_id = 0;
154
126k
    return status;
155
126k
}
156
157
32
uint64_t FileBlock::get_downloader() const {
158
32
    std::lock_guard block_lock(_mutex);
159
32
    return _downloader_id;
160
32
}
161
162
115k
bool FileBlock::is_downloader() const {
163
115k
    std::lock_guard block_lock(_mutex);
164
115k
    return get_caller_id() == _downloader_id;
165
115k
}
166
167
1.61M
bool FileBlock::is_downloader_impl(std::lock_guard<std::mutex>& /* block_lock */) const {
168
1.61M
    return get_caller_id() == _downloader_id;
169
1.61M
}
170
171
126k
Status FileBlock::append(Slice data) {
172
126k
    return appendv(&data, 1);
173
126k
}
174
175
126k
Status FileBlock::appendv(const Slice* data, size_t data_cnt) {
176
126k
    size_t appended_size = 0;
177
253k
    for (size_t idx = 0; idx < data_cnt; ++idx) {
178
126k
        appended_size += data[idx].size;
179
126k
    }
180
18.4E
    DCHECK(appended_size != 0) << "Writing zero size is not allowed";
181
126k
    auto st = _mgr->_storage->appendv(_key, data, data_cnt);
182
126k
    if (!st.ok()) {
183
8
        auto abort_st = abort_pending_cache_write(_mgr->_storage.get(), _key);
184
8
        LOG(WARNING) << "appendv file cache block failed, append_size=" << appended_size
185
8
                     << ", slice_count=" << data_cnt << ", err=" << st
186
8
                     << ", abort_status=" << abort_st << ", block=" << get_info_for_log();
187
8
        return st;
188
8
    }
189
126k
    _downloaded_size += appended_size;
190
126k
    return Status::OK();
191
126k
}
192
193
96
Status FileBlock::append_iobuf(const butil::IOBuf& data) {
194
96
    const size_t appended_size = data.length();
195
96
    DCHECK(appended_size != 0) << "Writing zero size is not allowed";
196
96
    auto st = _mgr->_storage->append_iobuf(_key, data);
197
96
    if (!st.ok()) {
198
4
        auto abort_st = abort_pending_cache_write(_mgr->_storage.get(), _key);
199
4
        LOG(WARNING) << "append_iobuf file cache block failed, append_size=" << appended_size
200
4
                     << ", err=" << st << ", abort_status=" << abort_st
201
4
                     << ", block=" << get_info_for_log();
202
4
        return st;
203
4
    }
204
92
    _downloaded_size += appended_size;
205
92
    return Status::OK();
206
96
}
207
208
126k
Status FileBlock::finalize() {
209
126k
    if (_downloaded_size == 0) {
210
2
        std::lock_guard block_lock(_mutex);
211
2
        _download_state = State::EMPTY;
212
2
        _downloader_id = 0;
213
2
        _cv.notify_all();
214
2
        LOG(WARNING) << "finalize file cache block failed: empty block, block="
215
2
                     << get_info_for_log_impl(block_lock);
216
2
        return Status::InternalError("Try to finalize an empty file block {}",
217
2
                                     _block_range.to_string());
218
2
    }
219
126k
    if (_downloaded_size != _block_range.size()) {
220
5.54k
        SCOPED_CACHE_LOCK(_mgr->_mutex, _mgr);
221
5.54k
        size_t old_size = _block_range.size();
222
5.54k
        size_t new_size = _downloaded_size;
223
5.54k
        DCHECK(new_size < old_size);
224
5.54k
        _mgr->reset_range(_key.hash, _block_range.left, old_size, new_size, cache_lock);
225
5.54k
    }
226
126k
    std::lock_guard block_lock(_mutex);
227
126k
    Status st = set_downloaded(block_lock);
228
126k
    _cv.notify_all();
229
126k
    return st;
230
126k
}
231
232
3.23M
Status FileBlock::read(Slice buffer, size_t read_offset) {
233
3.23M
    return _mgr->_storage->read(_key, read_offset, buffer);
234
3.23M
}
235
236
Status FileBlock::read_to_iobuf(butil::IOBuf* out, size_t read_offset, size_t bytes_req,
237
18
                                size_t* bytes_read) {
238
18
    return _mgr->_storage->read_to_iobuf(_key, read_offset, bytes_req, out, bytes_read);
239
18
}
240
241
51
Status FileBlock::change_cache_type(FileCacheType new_type) {
242
51
    SCOPED_CACHE_LOCK(_mgr->_mutex, _mgr);
243
51
    return change_cache_type_lock(new_type, cache_lock);
244
51
}
245
246
Status FileBlock::change_cache_type_lock(FileCacheType new_type,
247
53
                                         std::lock_guard<std::mutex>& cache_lock) {
248
53
    std::lock_guard block_lock(_mutex);
249
250
53
    if (new_type == _key.meta.type) {
251
2
        return Status::OK();
252
2
    }
253
51
    if (_download_state == State::DOWNLOADED) {
254
12
        Status st;
255
12
        TEST_SYNC_POINT_CALLBACK("FileBlock::change_cache_type", &st);
256
12
        RETURN_IF_ERROR(_mgr->_storage->change_key_meta_type(_key, new_type, _block_range.size()));
257
12
    }
258
51
    _mgr->change_cache_type(_key.hash, _block_range.left, new_type, cache_lock);
259
51
    _key.meta.type = new_type;
260
51
    return Status::OK();
261
51
}
262
263
4.54k
FileBlock::State FileBlock::wait() {
264
4.54k
    std::unique_lock block_lock(_mutex);
265
266
4.54k
    if (_downloader_id == 0) {
267
4
        return _download_state;
268
4
    }
269
270
4.54k
    if (_download_state == State::DOWNLOADING) {
271
4.54k
        DCHECK(_downloader_id != 0 && _downloader_id != get_caller_id());
272
4.54k
        _cv.wait_for(block_lock, std::chrono::milliseconds(config::block_cache_wait_timeout_ms));
273
4.54k
    }
274
275
4.54k
    return _download_state;
276
4.54k
}
277
278
1.61M
void FileBlock::complete_unlocked(std::lock_guard<std::mutex>& block_lock) {
279
1.61M
    if (is_downloader_impl(block_lock)) {
280
44
        reset_downloader(block_lock);
281
44
        _cv.notify_all();
282
44
    }
283
1.61M
}
284
285
20
std::string FileBlock::get_info_for_log() const {
286
20
    std::lock_guard block_lock(_mutex);
287
20
    return get_info_for_log_impl(block_lock);
288
20
}
289
290
36
std::string FileBlock::get_info_for_log_impl(std::lock_guard<std::mutex>& block_lock) const {
291
36
    std::stringstream info;
292
36
    info << "File block: " << range().to_string() << ", ";
293
36
    info << "hash: " << _key.hash.to_string() << ", ";
294
36
    info << "cache_type: " << cache_type_to_string(_key.meta.type) << ", ";
295
36
    info << "state: " << state_to_string(_download_state) << ", ";
296
36
    info << "size: " << _block_range.size() << ", ";
297
36
    info << "downloaded_size: " << _downloaded_size << ", ";
298
36
    info << "downloader id: " << _downloader_id << ", ";
299
36
    info << "caller id: " << get_caller_id() << ", ";
300
301
36
    std::string cache_file = "<unknown>";
302
36
    if (_mgr != nullptr && _mgr->_storage != nullptr) {
303
34
        cache_file = _mgr->_storage->get_local_file(_key);
304
34
    }
305
36
    info << "cache_file: " << cache_file;
306
307
36
    return info.str();
308
36
}
309
310
1.60M
FileBlock::State FileBlock::state_unlock(std::lock_guard<std::mutex>&) const {
311
1.60M
    return _download_state;
312
1.60M
}
313
314
2.74k
std::string FileBlock::state_to_string(FileBlock::State state) {
315
2.74k
    switch (state) {
316
2.69k
    case FileBlock::State::DOWNLOADED:
317
2.69k
        return "DOWNLOADED";
318
18
    case FileBlock::State::EMPTY:
319
18
        return "EMPTY";
320
26
    case FileBlock::State::DOWNLOADING:
321
26
        return "DOWNLOADING";
322
2
    case FileBlock::State::SKIP_CACHE:
323
2
        return "SKIP_CACHE";
324
0
    default:
325
0
        DCHECK(false);
326
0
        return "";
327
2.74k
    }
328
2.74k
}
329
330
12
std::string FileBlock::get_cache_file() const {
331
12
    return _mgr->_storage->get_local_file(this->_key);
332
12
}
333
334
1.59M
FileBlocksHolder::~FileBlocksHolder() {
335
3.20M
    for (auto file_block_it = file_blocks.begin(); file_block_it != file_blocks.end();) {
336
1.61M
        auto current_file_block_it = file_block_it;
337
1.61M
        auto& file_block = *current_file_block_it;
338
1.61M
        BlockFileCache* _mgr = file_block->_mgr;
339
1.61M
        {
340
1.61M
            bool should_remove = false;
341
1.61M
            {
342
1.61M
                std::lock_guard block_lock(file_block->_mutex);
343
1.61M
                file_block->complete_unlocked(block_lock);
344
1.61M
                if (file_block.use_count() == 2 &&
345
1.61M
                    (file_block->is_deleting() ||
346
1.18M
                     file_block->state_unlock(block_lock) == FileBlock::State::EMPTY)) {
347
25.7k
                    should_remove = true;
348
25.7k
                }
349
1.61M
            }
350
1.61M
            if (should_remove) {
351
25.7k
                SCOPED_CACHE_LOCK(_mgr->_mutex, _mgr);
352
25.7k
                std::lock_guard block_lock(file_block->_mutex);
353
25.7k
                if (file_block.use_count() == 2) {
354
25.7k
                    DCHECK(file_block->state_unlock(block_lock) != FileBlock::State::DOWNLOADING);
355
                    // one in cache, one in here
356
25.7k
                    if (file_block->is_deleting() ||
357
25.7k
                        file_block->state_unlock(block_lock) == FileBlock::State::EMPTY) {
358
25.7k
                        _mgr->remove(file_block, cache_lock, block_lock, false);
359
25.7k
                    }
360
25.7k
                }
361
25.7k
            }
362
1.61M
        }
363
1.61M
        file_block_it = file_blocks.erase(current_file_block_it);
364
1.61M
    }
365
1.59M
}
366
367
} // namespace io
368
} // namespace doris