Coverage Report

Created: 2026-06-25 13:23

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
be/src/io/cache/block_file_cache.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
// This file is copied from
18
// https://github.com/ClickHouse/ClickHouse/blob/master/src/Interpreters/Cache/FileCache.cpp
19
// and modified by Doris
20
21
#include "io/cache/block_file_cache.h"
22
23
#include <gen_cpp/file_cache.pb.h>
24
25
#include <cstdio>
26
#include <exception>
27
#include <fstream>
28
#include <unordered_set>
29
30
#include "common/status.h"
31
#include "cpp/sync_point.h"
32
#include "runtime/exec_env.h"
33
34
#if defined(__APPLE__)
35
#include <sys/mount.h>
36
#else
37
#include <sys/statfs.h>
38
#endif
39
40
#include <chrono> // IWYU pragma: keep
41
#include <mutex>
42
#include <ranges>
43
44
#include "common/cast_set.h"
45
#include "common/check.h"
46
#include "common/config.h"
47
#include "common/logging.h"
48
#include "core/uint128.h"
49
#include "exec/common/sip_hash.h"
50
#include "io/cache/block_file_cache_ttl_mgr.h"
51
#include "io/cache/file_block.h"
52
#include "io/cache/file_cache_common.h"
53
#include "io/cache/fs_file_cache_storage.h"
54
#include "io/cache/mem_file_cache_storage.h"
55
#include "runtime/runtime_profile.h"
56
#include "util/concurrency_stats.h"
57
#include "util/stack_util.h"
58
#include "util/stopwatch.hpp"
59
#include "util/thread.h"
60
#include "util/time.h"
61
namespace doris::io {
62
63
namespace {
64
65
constexpr std::array<FileCacheType, 4> LRU_LOG_REPLAY_TYPES = {
66
        FileCacheType::TTL, FileCacheType::INDEX, FileCacheType::NORMAL, FileCacheType::DISPOSABLE};
67
68
732
size_t file_cache_type_index(FileCacheType type) {
69
732
    return static_cast<size_t>(type);
70
732
}
71
72
} // namespace
73
74
// Insert a block pointer into one shard while swallowing allocation failures.
75
193k
bool NeedUpdateLRUBlocks::insert(FileBlockSPtr block, size_t max_queue_size) {
76
193k
    if (!block || max_queue_size == 0) {
77
1
        return false;
78
1
    }
79
193k
    bool reserved = false;
80
193k
    try {
81
193k
        auto* raw_ptr = block.get();
82
193k
        auto idx = shard_index(raw_ptr);
83
193k
        auto& shard = _shards[idx];
84
193k
        std::lock_guard lock(shard.mutex);
85
193k
        if (shard.entries.contains(raw_ptr)) {
86
191k
            return false;
87
191k
        }
88
1.56k
        size_t cur_size = _size.load(std::memory_order_relaxed);
89
1.56k
        while (cur_size < max_queue_size) {
90
1.56k
            if (_size.compare_exchange_weak(cur_size, cur_size + 1, std::memory_order_relaxed)) {
91
1.56k
                reserved = true;
92
1.56k
                break;
93
1.56k
            }
94
1.56k
        }
95
1.56k
        if (!reserved) {
96
1
            return false;
97
1
        }
98
1.56k
        auto [_, inserted] = shard.entries.emplace(raw_ptr, std::move(block));
99
1.56k
        DORIS_CHECK(inserted);
100
1.56k
        return true;
101
1.56k
    } catch (const std::exception& e) {
102
0
        if (reserved) {
103
0
            decrease_size(1);
104
0
        }
105
0
        LOG(WARNING) << "Failed to enqueue block for LRU update: " << e.what();
106
0
    } catch (...) {
107
0
        if (reserved) {
108
0
            decrease_size(1);
109
0
        }
110
0
        LOG(WARNING) << "Failed to enqueue block for LRU update: unknown error";
111
0
    }
112
0
    return false;
113
193k
}
114
115
// Drain up to `limit` unique blocks to the caller, keeping the structure consistent on failures.
116
46
size_t NeedUpdateLRUBlocks::drain(size_t limit, std::vector<FileBlockSPtr>* output) {
117
46
    if (limit == 0 || output == nullptr) {
118
2
        return 0;
119
2
    }
120
44
    size_t drained = 0;
121
44
    try {
122
44
        output->reserve(output->size() + std::min(limit, size()));
123
2.77k
        for (auto& shard : _shards) {
124
2.77k
            if (drained >= limit) {
125
1
                break;
126
1
            }
127
2.76k
            std::lock_guard lock(shard.mutex);
128
2.76k
            auto it = shard.entries.begin();
129
2.76k
            size_t shard_drained = 0;
130
2.77k
            while (it != shard.entries.end() && drained + shard_drained < limit) {
131
10
                output->emplace_back(std::move(it->second));
132
10
                it = shard.entries.erase(it);
133
10
                ++shard_drained;
134
10
            }
135
2.76k
            if (shard_drained > 0) {
136
7
                decrease_size(shard_drained);
137
7
                drained += shard_drained;
138
7
            }
139
2.76k
        }
140
44
    } catch (const std::exception& e) {
141
0
        LOG(WARNING) << "Failed to drain LRU update blocks: " << e.what();
142
0
    } catch (...) {
143
0
        LOG(WARNING) << "Failed to drain LRU update blocks: unknown error";
144
0
    }
145
44
    return drained;
146
44
}
147
148
// Remove every pending block, guarding against unexpected exceptions.
149
26
void NeedUpdateLRUBlocks::clear() {
150
26
    try {
151
1.66k
        for (auto& shard : _shards) {
152
1.66k
            std::lock_guard lock(shard.mutex);
153
1.66k
            if (!shard.entries.empty()) {
154
2
                auto removed = shard.entries.size();
155
2
                shard.entries.clear();
156
2
                decrease_size(removed);
157
2
            }
158
1.66k
        }
159
26
    } catch (const std::exception& e) {
160
0
        LOG(WARNING) << "Failed to clear LRU update blocks: " << e.what();
161
0
    } catch (...) {
162
0
        LOG(WARNING) << "Failed to clear LRU update blocks: unknown error";
163
0
    }
164
26
}
165
166
9
void NeedUpdateLRUBlocks::decrease_size(size_t delta) {
167
9
    size_t cur_size = _size.load(std::memory_order_relaxed);
168
9
    while (true) {
169
9
        DORIS_CHECK_GE(cur_size, delta);
170
9
        if (_size.compare_exchange_weak(cur_size, cur_size - delta, std::memory_order_relaxed)) {
171
9
            return;
172
9
        }
173
9
    }
174
9
}
175
176
193k
size_t NeedUpdateLRUBlocks::shard_index(FileBlock* ptr) const {
177
193k
    DCHECK(ptr != nullptr);
178
193k
    return std::hash<FileBlock*> {}(ptr)&kShardMask;
179
193k
}
180
181
BlockFileCache::BlockFileCache(const std::string& cache_base_path,
182
                               const FileCacheSettings& cache_settings)
183
183
        : _cache_base_path(cache_base_path),
184
183
          _capacity(cache_settings.capacity),
185
183
          _max_file_block_size(cache_settings.max_file_block_size) {
186
183
    _cur_cache_size_metrics = std::make_shared<bvar::Status<size_t>>(_cache_base_path.c_str(),
187
183
                                                                     "file_cache_cache_size", 0);
188
183
    _cache_capacity_metrics = std::make_shared<bvar::Status<size_t>>(
189
183
            _cache_base_path.c_str(), "file_cache_capacity", _capacity);
190
183
    _cur_ttl_cache_size_metrics = std::make_shared<bvar::Status<size_t>>(
191
183
            _cache_base_path.c_str(), "file_cache_ttl_cache_size", 0);
192
183
    _cur_normal_queue_element_count_metrics = std::make_shared<bvar::Status<size_t>>(
193
183
            _cache_base_path.c_str(), "file_cache_normal_queue_element_count", 0);
194
183
    _cur_ttl_cache_lru_queue_cache_size_metrics = std::make_shared<bvar::Status<size_t>>(
195
183
            _cache_base_path.c_str(), "file_cache_ttl_cache_lru_queue_size", 0);
196
183
    _cur_ttl_cache_lru_queue_element_count_metrics = std::make_shared<bvar::Status<size_t>>(
197
183
            _cache_base_path.c_str(), "file_cache_ttl_cache_lru_queue_element_count", 0);
198
183
    _cur_normal_queue_cache_size_metrics = std::make_shared<bvar::Status<size_t>>(
199
183
            _cache_base_path.c_str(), "file_cache_normal_queue_cache_size", 0);
200
183
    _cur_index_queue_element_count_metrics = std::make_shared<bvar::Status<size_t>>(
201
183
            _cache_base_path.c_str(), "file_cache_index_queue_element_count", 0);
202
183
    _cur_index_queue_cache_size_metrics = std::make_shared<bvar::Status<size_t>>(
203
183
            _cache_base_path.c_str(), "file_cache_index_queue_cache_size", 0);
204
183
    _cur_disposable_queue_element_count_metrics = std::make_shared<bvar::Status<size_t>>(
205
183
            _cache_base_path.c_str(), "file_cache_disposable_queue_element_count", 0);
206
183
    _cur_disposable_queue_cache_size_metrics = std::make_shared<bvar::Status<size_t>>(
207
183
            _cache_base_path.c_str(), "file_cache_disposable_queue_cache_size", 0);
208
209
183
    _queue_evict_size_metrics[0] = std::make_shared<bvar::Adder<size_t>>(
210
183
            _cache_base_path.c_str(), "file_cache_index_queue_evict_size");
211
183
    _queue_evict_size_metrics[1] = std::make_shared<bvar::Adder<size_t>>(
212
183
            _cache_base_path.c_str(), "file_cache_normal_queue_evict_size");
213
183
    _queue_evict_size_metrics[2] = std::make_shared<bvar::Adder<size_t>>(
214
183
            _cache_base_path.c_str(), "file_cache_disposable_queue_evict_size");
215
183
    _queue_evict_size_metrics[3] = std::make_shared<bvar::Adder<size_t>>(
216
183
            _cache_base_path.c_str(), "file_cache_ttl_cache_evict_size");
217
183
    _total_evict_size_metrics = std::make_shared<bvar::Adder<size_t>>(
218
183
            _cache_base_path.c_str(), "file_cache_total_evict_size");
219
183
    _total_read_size_metrics = std::make_shared<bvar::Adder<size_t>>(_cache_base_path.c_str(),
220
183
                                                                     "file_cache_total_read_size");
221
183
    _total_hit_size_metrics = std::make_shared<bvar::Adder<size_t>>(_cache_base_path.c_str(),
222
183
                                                                    "file_cache_total_hit_size");
223
183
    _gc_evict_bytes_metrics = std::make_shared<bvar::Adder<size_t>>(_cache_base_path.c_str(),
224
183
                                                                    "file_cache_gc_evict_bytes");
225
183
    _gc_evict_count_metrics = std::make_shared<bvar::Adder<size_t>>(_cache_base_path.c_str(),
226
183
                                                                    "file_cache_gc_evict_count");
227
228
183
    _evict_by_time_metrics_matrix[FileCacheType::DISPOSABLE][FileCacheType::NORMAL] =
229
183
            std::make_shared<bvar::Adder<size_t>>(_cache_base_path.c_str(),
230
183
                                                  "file_cache_evict_by_time_disposable_to_normal");
231
183
    _evict_by_time_metrics_matrix[FileCacheType::DISPOSABLE][FileCacheType::INDEX] =
232
183
            std::make_shared<bvar::Adder<size_t>>(_cache_base_path.c_str(),
233
183
                                                  "file_cache_evict_by_time_disposable_to_index");
234
183
    _evict_by_time_metrics_matrix[FileCacheType::DISPOSABLE][FileCacheType::TTL] =
235
183
            std::make_shared<bvar::Adder<size_t>>(_cache_base_path.c_str(),
236
183
                                                  "file_cache_evict_by_time_disposable_to_ttl");
237
183
    _evict_by_time_metrics_matrix[FileCacheType::NORMAL][FileCacheType::DISPOSABLE] =
238
183
            std::make_shared<bvar::Adder<size_t>>(_cache_base_path.c_str(),
239
183
                                                  "file_cache_evict_by_time_normal_to_disposable");
240
183
    _evict_by_time_metrics_matrix[FileCacheType::NORMAL][FileCacheType::INDEX] =
241
183
            std::make_shared<bvar::Adder<size_t>>(_cache_base_path.c_str(),
242
183
                                                  "file_cache_evict_by_time_normal_to_index");
243
183
    _evict_by_time_metrics_matrix[FileCacheType::NORMAL][FileCacheType::TTL] =
244
183
            std::make_shared<bvar::Adder<size_t>>(_cache_base_path.c_str(),
245
183
                                                  "file_cache_evict_by_time_normal_to_ttl");
246
183
    _evict_by_time_metrics_matrix[FileCacheType::INDEX][FileCacheType::DISPOSABLE] =
247
183
            std::make_shared<bvar::Adder<size_t>>(_cache_base_path.c_str(),
248
183
                                                  "file_cache_evict_by_time_index_to_disposable");
249
183
    _evict_by_time_metrics_matrix[FileCacheType::INDEX][FileCacheType::NORMAL] =
250
183
            std::make_shared<bvar::Adder<size_t>>(_cache_base_path.c_str(),
251
183
                                                  "file_cache_evict_by_time_index_to_normal");
252
183
    _evict_by_time_metrics_matrix[FileCacheType::INDEX][FileCacheType::TTL] =
253
183
            std::make_shared<bvar::Adder<size_t>>(_cache_base_path.c_str(),
254
183
                                                  "file_cache_evict_by_time_index_to_ttl");
255
183
    _evict_by_time_metrics_matrix[FileCacheType::TTL][FileCacheType::DISPOSABLE] =
256
183
            std::make_shared<bvar::Adder<size_t>>(_cache_base_path.c_str(),
257
183
                                                  "file_cache_evict_by_time_ttl_to_disposable");
258
183
    _evict_by_time_metrics_matrix[FileCacheType::TTL][FileCacheType::NORMAL] =
259
183
            std::make_shared<bvar::Adder<size_t>>(_cache_base_path.c_str(),
260
183
                                                  "file_cache_evict_by_time_ttl_to_normal");
261
183
    _evict_by_time_metrics_matrix[FileCacheType::TTL][FileCacheType::INDEX] =
262
183
            std::make_shared<bvar::Adder<size_t>>(_cache_base_path.c_str(),
263
183
                                                  "file_cache_evict_by_time_ttl_to_index");
264
265
183
    _evict_by_self_lru_metrics_matrix[FileCacheType::DISPOSABLE] =
266
183
            std::make_shared<bvar::Adder<size_t>>(_cache_base_path.c_str(),
267
183
                                                  "file_cache_evict_by_self_lru_disposable");
268
183
    _evict_by_self_lru_metrics_matrix[FileCacheType::NORMAL] =
269
183
            std::make_shared<bvar::Adder<size_t>>(_cache_base_path.c_str(),
270
183
                                                  "file_cache_evict_by_self_lru_normal");
271
183
    _evict_by_self_lru_metrics_matrix[FileCacheType::INDEX] = std::make_shared<bvar::Adder<size_t>>(
272
183
            _cache_base_path.c_str(), "file_cache_evict_by_self_lru_index");
273
183
    _evict_by_self_lru_metrics_matrix[FileCacheType::TTL] = std::make_shared<bvar::Adder<size_t>>(
274
183
            _cache_base_path.c_str(), "file_cache_evict_by_self_lru_ttl");
275
276
183
    _evict_by_size_metrics_matrix[FileCacheType::DISPOSABLE][FileCacheType::NORMAL] =
277
183
            std::make_shared<bvar::Adder<size_t>>(_cache_base_path.c_str(),
278
183
                                                  "file_cache_evict_by_size_disposable_to_normal");
279
183
    _evict_by_size_metrics_matrix[FileCacheType::DISPOSABLE][FileCacheType::INDEX] =
280
183
            std::make_shared<bvar::Adder<size_t>>(_cache_base_path.c_str(),
281
183
                                                  "file_cache_evict_by_size_disposable_to_index");
282
183
    _evict_by_size_metrics_matrix[FileCacheType::DISPOSABLE][FileCacheType::TTL] =
283
183
            std::make_shared<bvar::Adder<size_t>>(_cache_base_path.c_str(),
284
183
                                                  "file_cache_evict_by_size_disposable_to_ttl");
285
183
    _evict_by_size_metrics_matrix[FileCacheType::NORMAL][FileCacheType::DISPOSABLE] =
286
183
            std::make_shared<bvar::Adder<size_t>>(_cache_base_path.c_str(),
287
183
                                                  "file_cache_evict_by_size_normal_to_disposable");
288
183
    _evict_by_size_metrics_matrix[FileCacheType::NORMAL][FileCacheType::INDEX] =
289
183
            std::make_shared<bvar::Adder<size_t>>(_cache_base_path.c_str(),
290
183
                                                  "file_cache_evict_by_size_normal_to_index");
291
183
    _evict_by_size_metrics_matrix[FileCacheType::NORMAL][FileCacheType::TTL] =
292
183
            std::make_shared<bvar::Adder<size_t>>(_cache_base_path.c_str(),
293
183
                                                  "file_cache_evict_by_size_normal_to_ttl");
294
183
    _evict_by_size_metrics_matrix[FileCacheType::INDEX][FileCacheType::DISPOSABLE] =
295
183
            std::make_shared<bvar::Adder<size_t>>(_cache_base_path.c_str(),
296
183
                                                  "file_cache_evict_by_size_index_to_disposable");
297
183
    _evict_by_size_metrics_matrix[FileCacheType::INDEX][FileCacheType::NORMAL] =
298
183
            std::make_shared<bvar::Adder<size_t>>(_cache_base_path.c_str(),
299
183
                                                  "file_cache_evict_by_size_index_to_normal");
300
183
    _evict_by_size_metrics_matrix[FileCacheType::INDEX][FileCacheType::TTL] =
301
183
            std::make_shared<bvar::Adder<size_t>>(_cache_base_path.c_str(),
302
183
                                                  "file_cache_evict_by_size_index_to_ttl");
303
183
    _evict_by_size_metrics_matrix[FileCacheType::TTL][FileCacheType::DISPOSABLE] =
304
183
            std::make_shared<bvar::Adder<size_t>>(_cache_base_path.c_str(),
305
183
                                                  "file_cache_evict_by_size_ttl_to_disposable");
306
183
    _evict_by_size_metrics_matrix[FileCacheType::TTL][FileCacheType::NORMAL] =
307
183
            std::make_shared<bvar::Adder<size_t>>(_cache_base_path.c_str(),
308
183
                                                  "file_cache_evict_by_size_ttl_to_normal");
309
183
    _evict_by_size_metrics_matrix[FileCacheType::TTL][FileCacheType::INDEX] =
310
183
            std::make_shared<bvar::Adder<size_t>>(_cache_base_path.c_str(),
311
183
                                                  "file_cache_evict_by_size_ttl_to_index");
312
313
183
    _evict_by_try_release = std::make_shared<bvar::Adder<size_t>>(
314
183
            _cache_base_path.c_str(), "file_cache_evict_by_try_release");
315
316
183
    _num_read_blocks = std::make_shared<bvar::Adder<size_t>>(_cache_base_path.c_str(),
317
183
                                                             "file_cache_num_read_blocks");
318
183
    _num_hit_blocks = std::make_shared<bvar::Adder<size_t>>(_cache_base_path.c_str(),
319
183
                                                            "file_cache_num_hit_blocks");
320
183
    _num_removed_blocks = std::make_shared<bvar::Adder<size_t>>(_cache_base_path.c_str(),
321
183
                                                                "file_cache_num_removed_blocks");
322
323
183
    _no_warmup_num_read_blocks = std::make_shared<bvar::Adder<size_t>>(
324
183
            _cache_base_path.c_str(), "file_cache_no_warmup_num_read_blocks");
325
183
    _no_warmup_num_hit_blocks = std::make_shared<bvar::Adder<size_t>>(
326
183
            _cache_base_path.c_str(), "file_cache_no_warmup_num_hit_blocks");
327
328
#ifndef BE_TEST
329
    _num_hit_blocks_5m = std::make_shared<bvar::Window<bvar::Adder<size_t>>>(
330
            _cache_base_path.c_str(), "file_cache_num_hit_blocks_5m", _num_hit_blocks.get(), 300);
331
    _num_read_blocks_5m = std::make_shared<bvar::Window<bvar::Adder<size_t>>>(
332
            _cache_base_path.c_str(), "file_cache_num_read_blocks_5m", _num_read_blocks.get(), 300);
333
    _num_hit_blocks_1h = std::make_shared<bvar::Window<bvar::Adder<size_t>>>(
334
            _cache_base_path.c_str(), "file_cache_num_hit_blocks_1h", _num_hit_blocks.get(), 3600);
335
    _num_read_blocks_1h = std::make_shared<bvar::Window<bvar::Adder<size_t>>>(
336
            _cache_base_path.c_str(), "file_cache_num_read_blocks_1h", _num_read_blocks.get(),
337
            3600);
338
    _no_warmup_num_hit_blocks_5m = std::make_shared<bvar::Window<bvar::Adder<size_t>>>(
339
            _cache_base_path.c_str(), "file_cache_no_warmup_num_hit_blocks_5m",
340
            _no_warmup_num_hit_blocks.get(), 300);
341
    _no_warmup_num_read_blocks_5m = std::make_shared<bvar::Window<bvar::Adder<size_t>>>(
342
            _cache_base_path.c_str(), "file_cache_no_warmup_num_read_blocks_5m",
343
            _no_warmup_num_read_blocks.get(), 300);
344
    _no_warmup_num_hit_blocks_1h = std::make_shared<bvar::Window<bvar::Adder<size_t>>>(
345
            _cache_base_path.c_str(), "file_cache_no_warmup_num_hit_blocks_1h",
346
            _no_warmup_num_hit_blocks.get(), 3600);
347
    _no_warmup_num_read_blocks_1h = std::make_shared<bvar::Window<bvar::Adder<size_t>>>(
348
            _cache_base_path.c_str(), "file_cache_no_warmup_num_read_blocks_1h",
349
            _no_warmup_num_read_blocks.get(), 3600);
350
#endif
351
352
183
    _hit_ratio = std::make_shared<bvar::Status<double>>(_cache_base_path.c_str(),
353
183
                                                        "file_cache_hit_ratio", 0.0);
354
183
    _hit_ratio_5m = std::make_shared<bvar::Status<double>>(_cache_base_path.c_str(),
355
183
                                                           "file_cache_hit_ratio_5m", 0.0);
356
183
    _hit_ratio_1h = std::make_shared<bvar::Status<double>>(_cache_base_path.c_str(),
357
183
                                                           "file_cache_hit_ratio_1h", 0.0);
358
359
183
    _no_warmup_hit_ratio = std::make_shared<bvar::Status<double>>(
360
183
            _cache_base_path.c_str(), "file_cache_no_warmup_hit_ratio", 0.0);
361
183
    _no_warmup_hit_ratio_5m = std::make_shared<bvar::Status<double>>(
362
183
            _cache_base_path.c_str(), "file_cache_no_warmup_hit_ratio_5m", 0.0);
363
183
    _no_warmup_hit_ratio_1h = std::make_shared<bvar::Status<double>>(
364
183
            _cache_base_path.c_str(), "file_cache_no_warmup_hit_ratio_1h", 0.0);
365
366
183
    _disk_limit_mode_metrics = std::make_shared<bvar::Status<size_t>>(
367
183
            _cache_base_path.c_str(), "file_cache_disk_limit_mode", 0);
368
183
    _need_evict_cache_in_advance_metrics = std::make_shared<bvar::Status<size_t>>(
369
183
            _cache_base_path.c_str(), "file_cache_need_evict_cache_in_advance", 0);
370
183
    _meta_store_write_queue_size_metrics = std::make_shared<bvar::Status<size_t>>(
371
183
            _cache_base_path.c_str(), "file_cache_meta_store_write_queue_size", 0);
372
373
183
    _cache_lock_wait_time_us = std::make_shared<bvar::LatencyRecorder>(
374
183
            _cache_base_path.c_str(), "file_cache_cache_lock_wait_time_us");
375
183
    _get_or_set_latency_us = std::make_shared<bvar::LatencyRecorder>(
376
183
            _cache_base_path.c_str(), "file_cache_get_or_set_latency_us");
377
183
    _storage_sync_remove_latency_us = std::make_shared<bvar::LatencyRecorder>(
378
183
            _cache_base_path.c_str(), "file_cache_storage_sync_remove_latency_us");
379
183
    _storage_retry_sync_remove_latency_us = std::make_shared<bvar::LatencyRecorder>(
380
183
            _cache_base_path.c_str(), "file_cache_storage_retry_sync_remove_latency_us");
381
183
    _storage_async_remove_latency_us = std::make_shared<bvar::LatencyRecorder>(
382
183
            _cache_base_path.c_str(), "file_cache_storage_async_remove_latency_us");
383
183
    _evict_in_advance_latency_us = std::make_shared<bvar::LatencyRecorder>(
384
183
            _cache_base_path.c_str(), "file_cache_evict_in_advance_latency_us");
385
183
    _lru_dump_latency_us = std::make_shared<bvar::LatencyRecorder>(
386
183
            _cache_base_path.c_str(), "file_cache_lru_dump_latency_us");
387
183
    _recycle_keys_length_recorder = std::make_shared<bvar::LatencyRecorder>(
388
183
            _cache_base_path.c_str(), "file_cache_recycle_keys_length");
389
183
    _need_update_lru_blocks_length_recorder = std::make_shared<bvar::LatencyRecorder>(
390
183
            _cache_base_path.c_str(), "file_cache_need_update_lru_blocks_length");
391
183
    _need_update_lru_blocks_produce_metrics = std::make_shared<bvar::Adder<size_t>>(
392
183
            _cache_base_path.c_str(), "file_cache_need_update_lru_blocks_produce");
393
183
    _need_update_lru_blocks_consume_metrics = std::make_shared<bvar::Adder<size_t>>(
394
183
            _cache_base_path.c_str(), "file_cache_need_update_lru_blocks_consume");
395
183
    _update_lru_blocks_latency_us = std::make_shared<bvar::LatencyRecorder>(
396
183
            _cache_base_path.c_str(), "file_cache_update_lru_blocks_latency_us");
397
183
    _ttl_gc_latency_us = std::make_shared<bvar::LatencyRecorder>(_cache_base_path.c_str(),
398
183
                                                                 "file_cache_ttl_gc_latency_us");
399
183
    _shadow_queue_levenshtein_distance = std::make_shared<bvar::LatencyRecorder>(
400
183
            _cache_base_path.c_str(), "file_cache_shadow_queue_levenshtein_distance");
401
183
    for (FileCacheType type : {FileCacheType::DISPOSABLE, FileCacheType::NORMAL,
402
732
                               FileCacheType::INDEX, FileCacheType::TTL}) {
403
732
        size_t idx = file_cache_type_index(type);
404
732
        std::string metric_prefix =
405
732
                "file_cache_lru_recorder_" + cache_type_to_string(type) + "_record_queue";
406
732
        _lru_recorder_queue_length_recorder[idx] = std::make_shared<bvar::LatencyRecorder>(
407
732
                _cache_base_path.c_str(), metric_prefix + "_length");
408
732
        _lru_recorder_queue_produce_metrics[idx] = std::make_shared<bvar::Adder<size_t>>(
409
732
                _cache_base_path.c_str(), metric_prefix + "_produce");
410
732
        _lru_recorder_queue_consume_metrics[idx] = std::make_shared<bvar::Adder<size_t>>(
411
732
                _cache_base_path.c_str(), metric_prefix + "_consume");
412
732
    }
413
183
    _lru_recorder_log_replay_idle_metrics = std::make_shared<bvar::Adder<size_t>>(
414
183
            _cache_base_path.c_str(), "file_cache_lru_recorder_log_replay_idle");
415
416
183
    _disposable_queue = LRUQueue(cache_settings.disposable_queue_size,
417
183
                                 cache_settings.disposable_queue_elements, 60 * 60);
418
183
    _index_queue = LRUQueue(cache_settings.index_queue_size, cache_settings.index_queue_elements,
419
183
                            7 * 24 * 60 * 60);
420
183
    _normal_queue = LRUQueue(cache_settings.query_queue_size, cache_settings.query_queue_elements,
421
183
                             24 * 60 * 60);
422
183
    _ttl_queue = LRUQueue(cache_settings.ttl_queue_size, cache_settings.ttl_queue_elements,
423
183
                          std::numeric_limits<int>::max());
424
425
183
    _lru_recorder = std::make_unique<LRUQueueRecorder>(this);
426
183
    _lru_dumper = std::make_unique<CacheLRUDumper>(this, _lru_recorder.get());
427
183
    if (cache_settings.storage == "memory") {
428
24
        _storage = std::make_unique<MemFileCacheStorage>();
429
24
        _cache_base_path = "memory";
430
159
    } else {
431
159
        _storage = std::make_unique<FSFileCacheStorage>();
432
159
    }
433
434
183
    LOG(INFO) << "file cache path= " << _cache_base_path << " " << cache_settings.to_string();
435
183
}
436
437
14.5k
UInt128Wrapper BlockFileCache::hash(const std::string& path) {
438
14.5k
    uint128_t value;
439
14.5k
    sip_hash128(path.data(), path.size(), reinterpret_cast<char*>(&value));
440
14.5k
    return UInt128Wrapper(value);
441
14.5k
}
442
443
BlockFileCache::QueryFileCacheContextHolderPtr BlockFileCache::get_query_context_holder(
444
8
        const TUniqueId& query_id, int file_cache_query_limit_percent) {
445
8
    SCOPED_CACHE_LOCK(_mutex, this);
446
8
    if (!config::enable_file_cache_query_limit) {
447
1
        return {};
448
1
    }
449
450
    /// if enable_filesystem_query_cache_limit is true,
451
    /// we create context query for current query.
452
7
    auto context = get_or_set_query_context(query_id, cache_lock, file_cache_query_limit_percent);
453
7
    return std::make_unique<QueryFileCacheContextHolder>(query_id, this, context);
454
8
}
455
456
BlockFileCache::QueryFileCacheContextPtr BlockFileCache::get_query_context(
457
3.40k
        const TUniqueId& query_id, std::lock_guard<std::mutex>& cache_lock) {
458
3.40k
    auto query_iter = _query_map.find(query_id);
459
3.40k
    return (query_iter == _query_map.end()) ? nullptr : query_iter->second;
460
3.40k
}
461
462
6
void BlockFileCache::remove_query_context(const TUniqueId& query_id) {
463
6
    SCOPED_CACHE_LOCK(_mutex, this);
464
6
    const auto& query_iter = _query_map.find(query_id);
465
466
6
    if (query_iter != _query_map.end() && query_iter->second.use_count() <= 1) {
467
5
        _query_map.erase(query_iter);
468
5
    }
469
6
}
470
471
BlockFileCache::QueryFileCacheContextPtr BlockFileCache::get_or_set_query_context(
472
        const TUniqueId& query_id, std::lock_guard<std::mutex>& cache_lock,
473
7
        int file_cache_query_limit_percent) {
474
7
    if (query_id.lo == 0 && query_id.hi == 0) {
475
1
        return nullptr;
476
1
    }
477
478
6
    auto context = get_query_context(query_id, cache_lock);
479
6
    if (context) {
480
1
        return context;
481
1
    }
482
483
5
    size_t file_cache_query_limit_size = _capacity * file_cache_query_limit_percent / 100;
484
5
    if (file_cache_query_limit_size < 268435456) {
485
5
        LOG(WARNING) << "The user-set file cache query limit (" << file_cache_query_limit_size
486
5
                     << " bytes) is less than the 256MB recommended minimum. "
487
5
                     << "Consider increasing the session variable 'file_cache_query_limit_percent'"
488
5
                     << " from its current value " << file_cache_query_limit_percent << "%.";
489
5
    }
490
5
    auto query_context = std::make_shared<QueryFileCacheContext>(file_cache_query_limit_size);
491
5
    auto query_iter = _query_map.emplace(query_id, query_context).first;
492
5
    return query_iter->second;
493
6
}
494
495
void BlockFileCache::QueryFileCacheContext::remove(const UInt128Wrapper& hash, size_t offset,
496
20
                                                   std::lock_guard<std::mutex>& cache_lock) {
497
20
    auto pair = std::make_pair(hash, offset);
498
20
    auto record = records.find(pair);
499
20
    DCHECK(record != records.end());
500
20
    auto iter = record->second;
501
20
    records.erase(pair);
502
20
    lru_queue.remove(iter, cache_lock);
503
20
}
504
505
void BlockFileCache::QueryFileCacheContext::reserve(const UInt128Wrapper& hash, size_t offset,
506
                                                    size_t size,
507
30
                                                    std::lock_guard<std::mutex>& cache_lock) {
508
30
    auto pair = std::make_pair(hash, offset);
509
30
    if (records.find(pair) == records.end()) {
510
29
        auto queue_iter = lru_queue.add(hash, offset, size, cache_lock);
511
29
        records.insert({pair, queue_iter});
512
29
    }
513
30
}
514
515
158
Status BlockFileCache::initialize() {
516
158
    SCOPED_CACHE_LOCK(_mutex, this);
517
158
    return initialize_unlocked(cache_lock);
518
158
}
519
520
158
Status BlockFileCache::initialize_unlocked(std::lock_guard<std::mutex>& cache_lock) {
521
158
    DCHECK(!_is_initialized);
522
158
    _is_initialized = true;
523
158
    if (config::file_cache_background_lru_dump_tail_record_num > 0) {
524
        // requirements:
525
        // 1. restored data should not overwrite the last dump
526
        // 2. restore should happen before load and async load
527
        // 3. all queues should be restored sequencially to avoid conflict
528
        // TODO(zhengyu): we can parralize them but will increase complexity, so lets check the time cost
529
        // to see if any improvement is a necessary
530
158
        restore_lru_queues_from_disk(cache_lock);
531
158
    }
532
158
    RETURN_IF_ERROR(_storage->init(this));
533
534
158
    if (auto* fs_storage = dynamic_cast<FSFileCacheStorage*>(_storage.get())) {
535
136
        if (auto* meta_store = fs_storage->get_meta_store()) {
536
136
            _ttl_mgr = std::make_unique<BlockFileCacheTtlMgr>(this, meta_store);
537
136
        }
538
136
    }
539
540
158
    _cache_background_monitor_thread = std::thread(&BlockFileCache::run_background_monitor, this);
541
158
    _cache_background_gc_thread = std::thread(&BlockFileCache::run_background_gc, this);
542
158
    _cache_background_evict_in_advance_thread =
543
158
            std::thread(&BlockFileCache::run_background_evict_in_advance, this);
544
158
    _cache_background_block_lru_update_thread =
545
158
            std::thread(&BlockFileCache::run_background_block_lru_update, this);
546
547
    // Initialize LRU dump thread and restore queues
548
158
    _cache_background_lru_dump_thread = std::thread(&BlockFileCache::run_background_lru_dump, this);
549
158
    _cache_background_lru_log_replay_thread =
550
158
            std::thread(&BlockFileCache::run_background_lru_log_replay, this);
551
552
158
    return Status::OK();
553
158
}
554
555
void BlockFileCache::update_block_lru(FileBlockSPtr block,
556
7
                                      std::lock_guard<std::mutex>& cache_lock) {
557
7
    if (!block) {
558
1
        return;
559
1
    }
560
561
6
    FileBlockCell* cell = get_cell(block->get_hash_value(), block->offset(), cache_lock);
562
6
    if (!cell || cell->file_block.get() != block.get()) {
563
1
        return;
564
1
    }
565
566
5
    if (cell->queue_iterator) {
567
5
        auto& queue = get_queue(block->cache_type());
568
5
        queue.move_to_end(*cell->queue_iterator, cache_lock);
569
5
        _lru_recorder->record_queue_event(block->cache_type(), CacheLRULogType::MOVETOBACK,
570
5
                                          block->_key.hash, block->_key.offset,
571
5
                                          block->_block_range.size());
572
5
    }
573
5
    cell->update_atime();
574
5
}
575
576
void BlockFileCache::use_cell(const FileBlockCell& cell, FileBlocks* result, bool move_iter_flag,
577
712k
                              std::lock_guard<std::mutex>& cache_lock) {
578
712k
    if (result) {
579
712k
        result->push_back(cell.file_block);
580
712k
    }
581
582
712k
    auto& queue = get_queue(cell.file_block->cache_type());
583
    /// Move to the end of the queue. The iterator remains valid.
584
712k
    if (!config::enable_file_cache_async_touch_on_get_or_set && cell.queue_iterator &&
585
712k
        move_iter_flag) {
586
520k
        queue.move_to_end(*cell.queue_iterator, cache_lock);
587
520k
        _lru_recorder->record_queue_event(cell.file_block->cache_type(),
588
520k
                                          CacheLRULogType::MOVETOBACK, cell.file_block->_key.hash,
589
520k
                                          cell.file_block->_key.offset, cell.size());
590
520k
    }
591
592
712k
    cell.update_atime();
593
712k
}
594
595
template <class T>
596
    requires IsXLock<T>
597
FileBlockCell* BlockFileCache::get_cell(const UInt128Wrapper& hash, size_t offset,
598
110k
                                        T& /* cache_lock */) {
599
110k
    auto it = _files.find(hash);
600
110k
    if (it == _files.end()) {
601
3
        return nullptr;
602
3
    }
603
604
110k
    auto& offsets = it->second;
605
110k
    auto cell_it = offsets.find(offset);
606
110k
    if (cell_it == offsets.end()) {
607
3
        return nullptr;
608
3
    }
609
610
110k
    return &cell_it->second;
611
110k
}
612
613
904k
bool BlockFileCache::need_to_move(FileCacheType cell_type, FileCacheType query_type) const {
614
904k
    return query_type != FileCacheType::DISPOSABLE && cell_type != FileCacheType::DISPOSABLE;
615
904k
}
616
617
FileBlocks BlockFileCache::get_impl(const UInt128Wrapper& hash, const CacheContext& context,
618
                                    const FileBlock::Range& range,
619
725k
                                    std::lock_guard<std::mutex>& cache_lock) {
620
    /// Given range = [left, right] and non-overlapping ordered set of file blocks,
621
    /// find list [block1, ..., blockN] of blocks which intersect with given range.
622
725k
    auto it = _files.find(hash);
623
725k
    if (it == _files.end()) {
624
7.10k
        if (_async_open_done) {
625
6.10k
            return {};
626
6.10k
        }
627
1.00k
        FileCacheKey key;
628
1.00k
        key.hash = hash;
629
1.00k
        key.meta.type = context.cache_type;
630
1.00k
        key.meta.expiration_time = context.expiration_time;
631
1.00k
        key.meta.tablet_id = context.tablet_id;
632
1.00k
        _storage->load_blocks_directly_unlocked(this, key, cache_lock);
633
634
1.00k
        it = _files.find(hash);
635
1.00k
        if (it == _files.end()) [[unlikely]] {
636
1.00k
            return {};
637
1.00k
        }
638
1.00k
    }
639
640
718k
    auto& file_blocks = it->second;
641
718k
    if (file_blocks.empty()) {
642
0
        LOG(WARNING) << "file_blocks is empty for hash=" << hash.to_string()
643
0
                     << " cache type=" << context.cache_type
644
0
                     << " cache expiration time=" << context.expiration_time
645
0
                     << " cache range=" << range.left << " " << range.right
646
0
                     << " query id=" << context.query_id;
647
0
        DCHECK(false);
648
0
        _files.erase(hash);
649
0
        return {};
650
0
    }
651
652
718k
    FileBlocks result;
653
718k
    auto block_it = file_blocks.lower_bound(range.left);
654
718k
    if (block_it == file_blocks.end()) {
655
        /// N - last cached block for given file hash, block{N}.offset < range.left:
656
        ///   block{N}                       block{N}
657
        /// [________                         [_______]
658
        ///     [__________]         OR                  [________]
659
        ///     ^                                        ^
660
        ///     range.left                               range.left
661
662
6.19k
        const auto& cell = file_blocks.rbegin()->second;
663
6.19k
        if (cell.file_block->range().right < range.left) {
664
6.18k
            return {};
665
6.18k
        }
666
667
14
        use_cell(cell, &result, need_to_move(cell.file_block->cache_type(), context.cache_type),
668
14
                 cache_lock);
669
712k
    } else { /// block_it <-- segmment{k}
670
712k
        if (block_it != file_blocks.begin()) {
671
218
            const auto& prev_cell = std::prev(block_it)->second;
672
218
            const auto& prev_cell_range = prev_cell.file_block->range();
673
674
218
            if (range.left <= prev_cell_range.right) {
675
                ///   block{k-1}  block{k}
676
                ///   [________]   [_____
677
                ///       [___________
678
                ///       ^
679
                ///       range.left
680
681
137
                use_cell(prev_cell, &result,
682
137
                         need_to_move(prev_cell.file_block->cache_type(), context.cache_type),
683
137
                         cache_lock);
684
137
            }
685
218
        }
686
687
        ///  block{k} ...       block{k-1}  block{k}                      block{k}
688
        ///  [______              [______]     [____                        [________
689
        ///  [_________     OR              [________      OR    [______]   ^
690
        ///  ^                              ^                           ^   block{k}.offset
691
        ///  range.left                     range.left                  range.right
692
693
1.42M
        while (block_it != file_blocks.end()) {
694
712k
            const auto& cell = block_it->second;
695
712k
            if (range.right < cell.file_block->range().left) {
696
174
                break;
697
174
            }
698
699
712k
            use_cell(cell, &result, need_to_move(cell.file_block->cache_type(), context.cache_type),
700
712k
                     cache_lock);
701
712k
            ++block_it;
702
712k
        }
703
712k
    }
704
705
712k
    return result;
706
718k
}
707
708
193k
void BlockFileCache::add_need_update_lru_block(FileBlockSPtr block) {
709
193k
    int64_t queue_limit = config::file_cache_background_block_lru_update_queue_max_size;
710
193k
    size_t max_queue_size = queue_limit <= 0 ? 0 : static_cast<size_t>(queue_limit);
711
193k
    if (_need_update_lru_blocks.insert(std::move(block), max_queue_size)) {
712
1.55k
        *_need_update_lru_blocks_produce_metrics << 1;
713
1.55k
        *_need_update_lru_blocks_length_recorder << _need_update_lru_blocks.size();
714
1.55k
    }
715
193k
}
716
717
4
std::string BlockFileCache::clear_file_cache_async() {
718
4
    return clear_file_cache_impl(false);
719
4
}
720
721
20
std::string BlockFileCache::clear_file_cache_sync() {
722
20
    return clear_file_cache_impl(true);
723
20
}
724
725
24
std::string BlockFileCache::clear_file_cache_impl(bool sync_remove) {
726
24
    const char* action = sync_remove ? "clear_file_cache_sync" : "clear_file_cache_async";
727
24
    LOG(INFO) << "start " << action << ", path=" << _cache_base_path;
728
24
    _lru_dumper->remove_lru_dump_files();
729
24
    int64_t num_cells_all = 0;
730
24
    int64_t num_cells_to_delete = 0;
731
24
    int64_t num_cells_wait_recycle = 0;
732
24
    int64_t num_files_all = 0;
733
24
    TEST_SYNC_POINT_CALLBACK("BlockFileCache::clear_file_cache_async");
734
24
    {
735
24
        SCOPED_CACHE_LOCK(_mutex, this);
736
737
24
        std::vector<FileBlockCell*> deleting_cells;
738
5.13k
        for (auto& [_, offset_to_cell] : _files) {
739
5.13k
            ++num_files_all;
740
105k
            for (auto& [_1, cell] : offset_to_cell) {
741
105k
                ++num_cells_all;
742
105k
                deleting_cells.push_back(&cell);
743
105k
            }
744
5.13k
        }
745
746
        // Do not erase while walking _files above: remove() may erase the current map element.
747
        //
748
        // sync_remove only changes how already releasable DOWNLOADED blocks are deleted from
749
        // storage. Busy blocks keep the existing holder lifecycle: mark them deleting and leave
750
        // them in _files until the last holder releases them.
751
105k
        for (auto& cell : deleting_cells) {
752
105k
            if (!cell->releasable()) {
753
103
                LOG(INFO) << "cell is not releasable, hash="
754
103
                          << " offset=" << cell->file_block->offset();
755
103
                cell->file_block->set_deleting();
756
103
                ++num_cells_wait_recycle;
757
103
                continue;
758
103
            }
759
105k
            FileBlockSPtr file_block = cell->file_block;
760
105k
            if (file_block) {
761
105k
                std::lock_guard block_lock(file_block->_mutex);
762
105k
                remove(file_block, cache_lock, block_lock, sync_remove);
763
105k
                ++num_cells_to_delete;
764
105k
            }
765
105k
        }
766
24
        clear_need_update_lru_blocks();
767
24
    }
768
769
24
    std::stringstream ss;
770
24
    ss << "finish " << action << ", path=" << _cache_base_path << " sync_remove=" << sync_remove
771
24
       << " num_files_all=" << num_files_all << " num_cells_all=" << num_cells_all
772
24
       << " num_cells_to_delete=" << num_cells_to_delete
773
24
       << " num_cells_wait_recycle=" << num_cells_wait_recycle;
774
24
    auto msg = ss.str();
775
24
    LOG(INFO) << msg;
776
24
    _lru_dumper->remove_lru_dump_files();
777
24
    return msg;
778
24
}
779
780
FileBlocks BlockFileCache::split_range_into_cells(const UInt128Wrapper& hash,
781
                                                  const CacheContext& context, size_t offset,
782
                                                  size_t size, FileBlock::State state,
783
13.4k
                                                  std::lock_guard<std::mutex>& cache_lock) {
784
13.4k
    DCHECK(size > 0);
785
786
13.4k
    auto current_pos = offset;
787
13.4k
    auto end_pos_non_included = offset + size;
788
789
13.4k
    size_t current_size = 0;
790
13.4k
    size_t remaining_size = size;
791
792
13.4k
    FileBlocks file_blocks;
793
27.0k
    while (current_pos < end_pos_non_included) {
794
13.5k
        current_size = std::min(remaining_size, _max_file_block_size);
795
13.5k
        remaining_size -= current_size;
796
13.5k
        state = try_reserve(hash, context, current_pos, current_size, cache_lock)
797
13.5k
                        ? state
798
13.5k
                        : FileBlock::State::SKIP_CACHE;
799
13.5k
        if (state == FileBlock::State::SKIP_CACHE) [[unlikely]] {
800
62
            FileCacheKey key;
801
62
            key.hash = hash;
802
62
            key.offset = current_pos;
803
62
            key.meta.type = context.cache_type;
804
62
            key.meta.expiration_time = context.expiration_time;
805
62
            key.meta.tablet_id = context.tablet_id;
806
62
            auto file_block = std::make_shared<FileBlock>(key, current_size, this,
807
62
                                                          FileBlock::State::SKIP_CACHE);
808
62
            file_blocks.push_back(std::move(file_block));
809
13.4k
        } else {
810
13.4k
            auto* cell = add_cell(hash, context, current_pos, current_size, state, cache_lock);
811
13.4k
            if (cell) {
812
13.4k
                file_blocks.push_back(cell->file_block);
813
13.4k
                if (!context.is_cold_data) {
814
13.4k
                    cell->update_atime();
815
13.4k
                }
816
13.4k
            }
817
13.4k
            if (_ttl_mgr && context.tablet_id != 0) {
818
1.09k
                _ttl_mgr->register_tablet_id(context.tablet_id);
819
1.09k
            }
820
13.4k
        }
821
822
13.5k
        current_pos += current_size;
823
13.5k
    }
824
825
13.4k
    DCHECK(file_blocks.empty() || offset + size - 1 == file_blocks.back()->range().right);
826
13.4k
    return file_blocks;
827
13.4k
}
828
829
void BlockFileCache::fill_holes_with_empty_file_blocks(FileBlocks& file_blocks,
830
                                                       const UInt128Wrapper& hash,
831
                                                       const CacheContext& context,
832
                                                       const FileBlock::Range& range,
833
712k
                                                       std::lock_guard<std::mutex>& cache_lock) {
834
    /// There are blocks [block1, ..., blockN]
835
    /// (non-overlapping, non-empty, ascending-ordered) which (maybe partially)
836
    /// intersect with given range.
837
838
    /// It can have holes:
839
    /// [____________________]         -- requested range
840
    ///     [____]  [_]   [_________]  -- intersecting cache [block1, ..., blockN]
841
    ///
842
    /// For each such hole create a cell with file block state EMPTY.
843
844
712k
    auto it = file_blocks.begin();
845
712k
    auto block_range = (*it)->range();
846
847
712k
    size_t current_pos = 0;
848
712k
    if (block_range.left < range.left) {
849
        ///    [_______     -- requested range
850
        /// [_______
851
        /// ^
852
        /// block1
853
854
151
        current_pos = block_range.right + 1;
855
151
        ++it;
856
712k
    } else {
857
712k
        current_pos = range.left;
858
712k
    }
859
860
1.42M
    while (current_pos <= range.right && it != file_blocks.end()) {
861
712k
        block_range = (*it)->range();
862
863
712k
        if (current_pos == block_range.left) {
864
712k
            current_pos = block_range.right + 1;
865
712k
            ++it;
866
712k
            continue;
867
712k
        }
868
869
712k
        DCHECK(current_pos < block_range.left);
870
871
109
        auto hole_size = block_range.left - current_pos;
872
873
109
        file_blocks.splice(it, split_range_into_cells(hash, context, current_pos, hole_size,
874
109
                                                      FileBlock::State::EMPTY, cache_lock));
875
876
109
        current_pos = block_range.right + 1;
877
109
        ++it;
878
109
    }
879
880
712k
    if (current_pos <= range.right) {
881
        ///   ________]     -- requested range
882
        ///   _____]
883
        ///        ^
884
        /// blockN
885
886
86
        auto hole_size = range.right - current_pos + 1;
887
888
86
        file_blocks.splice(file_blocks.end(),
889
86
                           split_range_into_cells(hash, context, current_pos, hole_size,
890
86
                                                  FileBlock::State::EMPTY, cache_lock));
891
86
    }
892
712k
}
893
894
FileBlocksHolder BlockFileCache::get_or_set(const UInt128Wrapper& hash, size_t offset, size_t size,
895
725k
                                            CacheContext& context) {
896
725k
    FileBlock::Range range(offset, offset + size - 1);
897
898
725k
    ReadStatistics* stats = context.stats;
899
725k
    DCHECK(stats != nullptr);
900
725k
    MonotonicStopWatch sw;
901
725k
    sw.start();
902
725k
    FileBlocks file_blocks;
903
725k
    std::vector<FileBlockSPtr> need_update_lru_blocks;
904
725k
    const bool async_touch_on_get_or_set = config::enable_file_cache_async_touch_on_get_or_set;
905
725k
    int64_t duration = 0;
906
725k
    {
907
725k
        ConcurrencyStatsManager::instance().cached_remote_reader_get_or_set_wait_lock->increment();
908
725k
        std::lock_guard cache_lock(_mutex);
909
725k
        ConcurrencyStatsManager::instance().cached_remote_reader_get_or_set_wait_lock->decrement();
910
725k
        stats->lock_wait_timer += sw.elapsed_time();
911
725k
        SCOPED_RAW_TIMER(&duration);
912
        /// Get all blocks which intersect with the given range.
913
725k
        {
914
725k
            SCOPED_RAW_TIMER(&stats->get_timer);
915
725k
            file_blocks = get_impl(hash, context, range, cache_lock);
916
725k
        }
917
918
725k
        if (file_blocks.empty()) {
919
13.2k
            SCOPED_RAW_TIMER(&stats->set_timer);
920
13.2k
            file_blocks = split_range_into_cells(hash, context, offset, size,
921
13.2k
                                                 FileBlock::State::EMPTY, cache_lock);
922
712k
        } else {
923
712k
            SCOPED_RAW_TIMER(&stats->set_timer);
924
712k
            fill_holes_with_empty_file_blocks(file_blocks, hash, context, range, cache_lock);
925
712k
        }
926
725k
        DCHECK(!file_blocks.empty());
927
725k
        *_num_read_blocks << file_blocks.size();
928
725k
        if (!context.is_warmup) {
929
725k
            *_no_warmup_num_read_blocks << file_blocks.size();
930
725k
        }
931
725k
        if (async_touch_on_get_or_set) {
932
193k
            need_update_lru_blocks.reserve(file_blocks.size());
933
193k
        }
934
726k
        for (auto& block : file_blocks) {
935
726k
            size_t block_size = block->range().size();
936
726k
            *_total_read_size_metrics << block_size;
937
726k
            if (block->state_unsafe() == FileBlock::State::DOWNLOADED) {
938
712k
                *_num_hit_blocks << 1;
939
712k
                *_total_hit_size_metrics << block_size;
940
712k
                if (!context.is_warmup) {
941
712k
                    *_no_warmup_num_hit_blocks << 1;
942
712k
                }
943
712k
                if (async_touch_on_get_or_set &&
944
712k
                    need_to_move(block->cache_type(), context.cache_type)) {
945
192k
                    need_update_lru_blocks.emplace_back(block);
946
192k
                }
947
712k
            }
948
726k
        }
949
725k
    }
950
951
725k
    if (async_touch_on_get_or_set) {
952
193k
        for (auto& block : need_update_lru_blocks) {
953
192k
            add_need_update_lru_block(std::move(block));
954
192k
        }
955
193k
    }
956
957
725k
    *_get_or_set_latency_us << (duration / 1000);
958
725k
    return FileBlocksHolder(std::move(file_blocks));
959
725k
}
960
961
FileBlockCell* BlockFileCache::add_cell(const UInt128Wrapper& hash, const CacheContext& context,
962
                                        size_t offset, size_t size, FileBlock::State state,
963
113k
                                        std::lock_guard<std::mutex>& cache_lock) {
964
    /// Create a file block cell and put it in `files` map by [hash][offset].
965
113k
    if (size == 0) {
966
0
        return nullptr; /// Empty files are not cached.
967
0
    }
968
969
113k
    VLOG_DEBUG << "Adding file block to cache. size=" << size << " hash=" << hash.to_string()
970
0
               << " offset=" << offset << " cache_type=" << cache_type_to_string(context.cache_type)
971
0
               << " expiration_time=" << context.expiration_time
972
0
               << " tablet_id=" << context.tablet_id;
973
974
113k
    if (size > 1024 * 1024 * 1024) {
975
1
        LOG(WARNING) << "File block size is too large for a block, reject. size=" << size
976
1
                     << " hash=" << hash.to_string() << " offset=" << offset
977
1
                     << " stack:" << get_stack_trace();
978
1
        return nullptr;
979
1
    }
980
981
113k
    auto& offsets = _files[hash];
982
113k
    auto itr = offsets.find(offset);
983
113k
    if (itr != offsets.end()) {
984
5
        VLOG_DEBUG << "Cache already exists for hash: " << hash.to_string()
985
0
                   << ", offset: " << offset << ", size: " << size
986
0
                   << ".\nCurrent cache structure: " << dump_structure_unlocked(hash, cache_lock);
987
5
        return &(itr->second);
988
5
    }
989
990
113k
    FileCacheKey key;
991
113k
    key.hash = hash;
992
113k
    key.offset = offset;
993
113k
    key.meta.type = context.cache_type;
994
113k
    key.meta.expiration_time = context.expiration_time;
995
113k
    key.meta.tablet_id = context.tablet_id;
996
113k
    FileBlockCell cell(std::make_shared<FileBlock>(key, size, this, state), cache_lock);
997
113k
    Status st;
998
113k
    if (context.expiration_time == 0 && context.cache_type == FileCacheType::TTL) {
999
0
        st = cell.file_block->change_cache_type_lock(FileCacheType::NORMAL, cache_lock);
1000
113k
    } else if (context.cache_type != FileCacheType::TTL && context.expiration_time != 0) {
1001
1
        st = cell.file_block->change_cache_type_lock(FileCacheType::TTL, cache_lock);
1002
1
    }
1003
113k
    if (!st.ok()) {
1004
0
        LOG(WARNING) << "Cannot change cache type. expiration_time=" << context.expiration_time
1005
0
                     << " cache_type=" << cache_type_to_string(context.cache_type)
1006
0
                     << " error=" << st.msg();
1007
0
    }
1008
1009
113k
    auto& queue = get_queue(cell.file_block->cache_type());
1010
113k
    cell.queue_iterator = queue.add(hash, offset, size, cache_lock);
1011
113k
    _lru_recorder->record_queue_event(cell.file_block->cache_type(), CacheLRULogType::ADD,
1012
113k
                                      cell.file_block->get_hash_value(), cell.file_block->offset(),
1013
113k
                                      cell.size());
1014
1015
113k
    if (cell.file_block->cache_type() == FileCacheType::TTL) {
1016
3.92k
        _cur_ttl_size += cell.size();
1017
3.92k
    }
1018
113k
    auto [it, _] = offsets.insert(std::make_pair(offset, std::move(cell)));
1019
113k
    _cur_cache_size += size;
1020
113k
    return &(it->second);
1021
113k
}
1022
1023
0
size_t BlockFileCache::try_release() {
1024
0
    SCOPED_CACHE_LOCK(_mutex, this);
1025
0
    std::vector<FileBlockCell*> trash;
1026
0
    for (auto& [hash, blocks] : _files) {
1027
0
        for (auto& [offset, cell] : blocks) {
1028
0
            if (cell.releasable()) {
1029
0
                trash.emplace_back(&cell);
1030
0
            } else {
1031
0
                cell.file_block->set_deleting();
1032
0
            }
1033
0
        }
1034
0
    }
1035
0
    size_t remove_size = 0;
1036
0
    for (auto& cell : trash) {
1037
0
        FileBlockSPtr file_block = cell->file_block;
1038
0
        std::lock_guard lc(cell->file_block->_mutex);
1039
0
        remove_size += file_block->range().size();
1040
0
        remove(file_block, cache_lock, lc);
1041
0
        VLOG_DEBUG << "try_release " << _cache_base_path
1042
0
                   << " hash=" << file_block->get_hash_value().to_string()
1043
0
                   << " offset=" << file_block->offset();
1044
0
    }
1045
0
    *_evict_by_try_release << remove_size;
1046
0
    LOG(INFO) << "Released " << trash.size() << " blocks in file cache " << _cache_base_path;
1047
0
    return trash.size();
1048
0
}
1049
1050
967k
LRUQueue& BlockFileCache::get_queue(FileCacheType type) {
1051
967k
    switch (type) {
1052
13.5k
    case FileCacheType::INDEX:
1053
13.5k
        return _index_queue;
1054
13.3k
    case FileCacheType::DISPOSABLE:
1055
13.3k
        return _disposable_queue;
1056
934k
    case FileCacheType::NORMAL:
1057
934k
        return _normal_queue;
1058
5.79k
    case FileCacheType::TTL:
1059
5.79k
        return _ttl_queue;
1060
0
    default:
1061
0
        DCHECK(false);
1062
967k
    }
1063
0
    return _normal_queue;
1064
967k
}
1065
1066
140
const LRUQueue& BlockFileCache::get_queue(FileCacheType type) const {
1067
140
    switch (type) {
1068
33
    case FileCacheType::INDEX:
1069
33
        return _index_queue;
1070
1
    case FileCacheType::DISPOSABLE:
1071
1
        return _disposable_queue;
1072
106
    case FileCacheType::NORMAL:
1073
106
        return _normal_queue;
1074
0
    case FileCacheType::TTL:
1075
0
        return _ttl_queue;
1076
0
    default:
1077
0
        DCHECK(false);
1078
140
    }
1079
0
    return _normal_queue;
1080
140
}
1081
1082
void BlockFileCache::remove_file_blocks(std::vector<FileBlockCell*>& to_evict,
1083
                                        std::lock_guard<std::mutex>& cache_lock, bool sync,
1084
14.4k
                                        std::string& reason) {
1085
14.4k
    auto remove_file_block_if = [&](FileBlockCell* cell) {
1086
850
        FileBlockSPtr file_block = cell->file_block;
1087
850
        if (file_block) {
1088
850
            std::lock_guard block_lock(file_block->_mutex);
1089
850
            remove(file_block, cache_lock, block_lock, sync);
1090
850
            VLOG_DEBUG << "remove_file_blocks"
1091
0
                       << " hash=" << file_block->get_hash_value().to_string()
1092
0
                       << " offset=" << file_block->offset() << " reason=" << reason;
1093
850
        }
1094
850
    };
1095
14.4k
    std::for_each(to_evict.begin(), to_evict.end(), remove_file_block_if);
1096
14.4k
}
1097
1098
void BlockFileCache::find_evict_candidates(LRUQueue& queue, size_t size, size_t cur_cache_size,
1099
                                           size_t& removed_size,
1100
                                           std::vector<FileBlockCell*>& to_evict,
1101
                                           std::lock_guard<std::mutex>& cache_lock,
1102
981
                                           size_t& cur_removed_size, bool evict_in_advance) {
1103
2.22k
    for (const auto& [entry_key, entry_offset, entry_size] : queue) {
1104
2.22k
        if (!is_overflow(removed_size, size, cur_cache_size, evict_in_advance)) {
1105
920
            break;
1106
920
        }
1107
1.30k
        auto* cell = get_cell(entry_key, entry_offset, cache_lock);
1108
1109
1.30k
        DCHECK(cell) << "Cache became inconsistent. key: " << entry_key.to_string()
1110
0
                     << ", offset: " << entry_offset;
1111
1112
1.30k
        size_t cell_size = cell->size();
1113
1.30k
        DCHECK(entry_size == cell_size);
1114
1115
1.30k
        if (cell->releasable()) {
1116
833
            auto& file_block = cell->file_block;
1117
1118
833
            std::lock_guard block_lock(file_block->_mutex);
1119
833
            DCHECK(file_block->_download_state == FileBlock::State::DOWNLOADED);
1120
833
            to_evict.push_back(cell);
1121
833
            removed_size += cell_size;
1122
833
            cur_removed_size += cell_size;
1123
833
        }
1124
1.30k
    }
1125
981
}
1126
1127
// 1. if async load file cache not finish
1128
//     a. evict from lru queue
1129
// 2. if ttl cache
1130
//     a. evict from disposable/normal/index queue one by one
1131
// 3. if dont reach query limit or dont have query limit
1132
//     a. evict from other queue
1133
//     b. evict from current queue
1134
//         a.1 if the data belong write, then just evict cold data
1135
// 4. if reach query limit
1136
//     a. evict from query queue
1137
//     b. evict from other queue
1138
bool BlockFileCache::try_reserve(const UInt128Wrapper& hash, const CacheContext& context,
1139
                                 size_t offset, size_t size,
1140
13.5k
                                 std::lock_guard<std::mutex>& cache_lock) {
1141
13.5k
    if (!_async_open_done) {
1142
1.00k
        return try_reserve_during_async_load(size, cache_lock);
1143
1.00k
    }
1144
    // use this strategy in scenarios where there is insufficient disk capacity or insufficient number of inodes remaining
1145
    // directly eliminate 5 times the size of the space
1146
12.5k
    if (_disk_resource_limit_mode) {
1147
1
        size = 5 * size;
1148
1
    }
1149
1150
12.5k
    auto query_context = config::enable_file_cache_query_limit &&
1151
12.5k
                                         (context.query_id.hi != 0 || context.query_id.lo != 0)
1152
12.5k
                                 ? get_query_context(context.query_id, cache_lock)
1153
12.5k
                                 : nullptr;
1154
12.5k
    if (!query_context) {
1155
12.4k
        return try_reserve_for_lru(hash, nullptr, context, offset, size, cache_lock);
1156
12.4k
    } else if (query_context->get_cache_size(cache_lock) + size <=
1157
31
               query_context->get_max_cache_size()) {
1158
16
        return try_reserve_for_lru(hash, query_context, context, offset, size, cache_lock);
1159
16
    }
1160
15
    int64_t cur_time = std::chrono::duration_cast<std::chrono::seconds>(
1161
15
                               std::chrono::steady_clock::now().time_since_epoch())
1162
15
                               .count();
1163
15
    auto& queue = get_queue(context.cache_type);
1164
15
    size_t removed_size = 0;
1165
15
    size_t ghost_remove_size = 0;
1166
15
    size_t queue_size = queue.get_capacity(cache_lock);
1167
15
    size_t cur_cache_size = _cur_cache_size;
1168
15
    size_t query_context_cache_size = query_context->get_cache_size(cache_lock);
1169
1170
15
    std::vector<LRUQueue::Iterator> ghost;
1171
15
    std::vector<FileBlockCell*> to_evict;
1172
1173
15
    size_t max_size = queue.get_max_size();
1174
48
    auto is_overflow = [&] {
1175
48
        return _disk_resource_limit_mode ? removed_size < size
1176
48
                                         : cur_cache_size + size - removed_size > _capacity ||
1177
48
                                                   (queue_size + size - removed_size > max_size) ||
1178
48
                                                   (query_context_cache_size + size -
1179
38
                                                            (removed_size + ghost_remove_size) >
1180
38
                                                    query_context->get_max_cache_size());
1181
48
    };
1182
1183
    /// Select the cache from the LRU queue held by query for expulsion.
1184
35
    for (auto iter = query_context->queue().begin(); iter != query_context->queue().end(); iter++) {
1185
33
        if (!is_overflow()) {
1186
13
            break;
1187
13
        }
1188
1189
20
        auto* cell = get_cell(iter->hash, iter->offset, cache_lock);
1190
1191
20
        if (!cell) {
1192
            /// The cache corresponding to this record may be swapped out by
1193
            /// other queries, so it has become invalid.
1194
4
            ghost.push_back(iter);
1195
4
            ghost_remove_size += iter->size;
1196
16
        } else {
1197
16
            size_t cell_size = cell->size();
1198
16
            DCHECK(iter->size == cell_size);
1199
1200
16
            if (cell->releasable()) {
1201
16
                auto& file_block = cell->file_block;
1202
1203
16
                std::lock_guard block_lock(file_block->_mutex);
1204
16
                DCHECK(file_block->_download_state == FileBlock::State::DOWNLOADED);
1205
16
                to_evict.push_back(cell);
1206
16
                removed_size += cell_size;
1207
16
            }
1208
16
        }
1209
20
    }
1210
1211
16
    auto remove_file_block_if = [&](FileBlockCell* cell) {
1212
16
        FileBlockSPtr file_block = cell->file_block;
1213
16
        if (file_block) {
1214
16
            query_context->remove(file_block->get_hash_value(), file_block->offset(), cache_lock);
1215
16
            std::lock_guard block_lock(file_block->_mutex);
1216
16
            remove(file_block, cache_lock, block_lock);
1217
16
        }
1218
16
    };
1219
1220
15
    for (auto& iter : ghost) {
1221
4
        query_context->remove(iter->hash, iter->offset, cache_lock);
1222
4
    }
1223
1224
15
    std::for_each(to_evict.begin(), to_evict.end(), remove_file_block_if);
1225
1226
15
    if (is_overflow() &&
1227
15
        !try_reserve_from_other_queue(context.cache_type, size, cur_time, cache_lock)) {
1228
1
        return false;
1229
1
    }
1230
14
    query_context->reserve(hash, offset, size, cache_lock);
1231
14
    return true;
1232
15
}
1233
1234
1
void BlockFileCache::try_evict_in_advance(size_t size, std::lock_guard<std::mutex>& cache_lock) {
1235
1
    UInt128Wrapper hash = UInt128Wrapper();
1236
1
    size_t offset = 0;
1237
1
    CacheContext context;
1238
    /* we pick NORMAL and TTL cache to evict in advance
1239
     * we reserve for them but won't acutually give space to them
1240
     * on the contrary, NORMAL and TTL may sacrifice by LRU evicting themselves
1241
     * other cache types cannot be exempted because we will evict what they have stolen before LRU evicting
1242
     * in summary: all cache types will shrink somewhat, and NORMAL and TTL shrink the most, to make sure the cache is not full
1243
     */
1244
1
    context.cache_type = FileCacheType::NORMAL;
1245
1
    try_reserve_for_lru(hash, nullptr, context, offset, size, cache_lock, true);
1246
1
    context.cache_type = FileCacheType::TTL;
1247
1
    try_reserve_for_lru(hash, nullptr, context, offset, size, cache_lock, true);
1248
1
}
1249
1250
// remove specific cache synchronously, for critical operations
1251
// if in use, cache meta will be deleted after use and the block file is then deleted asynchronously
1252
8
void BlockFileCache::remove_if_cached(const UInt128Wrapper& file_key) {
1253
8
    std::string reason = "remove_if_cached";
1254
8
    SCOPED_CACHE_LOCK(_mutex, this);
1255
8
    auto iter = _files.find(file_key);
1256
8
    std::vector<FileBlockCell*> to_remove;
1257
8
    if (iter != _files.end()) {
1258
14
        for (auto& [_, cell] : iter->second) {
1259
14
            if (cell.releasable()) {
1260
13
                to_remove.push_back(&cell);
1261
13
            } else {
1262
1
                cell.file_block->set_deleting();
1263
1
            }
1264
14
        }
1265
6
    }
1266
8
    remove_file_blocks(to_remove, cache_lock, true, reason);
1267
8
}
1268
1269
// the async version of remove_if_cached, for background operations
1270
// cache meta is deleted synchronously if not in use, and the block file is deleted asynchronously
1271
// if in use, cache meta will be deleted after use and the block file is then deleted asynchronously
1272
2
void BlockFileCache::remove_if_cached_async(const UInt128Wrapper& file_key) {
1273
2
    std::string reason = "remove_if_cached_async";
1274
2
    SCOPED_CACHE_LOCK(_mutex, this);
1275
1276
2
    auto iter = _files.find(file_key);
1277
2
    std::vector<FileBlockCell*> to_remove;
1278
2
    if (iter != _files.end()) {
1279
2
        for (auto& [_, cell] : iter->second) {
1280
2
            *_gc_evict_bytes_metrics << cell.size();
1281
2
            *_gc_evict_count_metrics << 1;
1282
2
            if (cell.releasable()) {
1283
1
                to_remove.push_back(&cell);
1284
1
            } else {
1285
1
                cell.file_block->set_deleting();
1286
1
            }
1287
2
        }
1288
2
    }
1289
2
    remove_file_blocks(to_remove, cache_lock, false, reason);
1290
2
}
1291
1292
std::vector<FileCacheType> BlockFileCache::get_other_cache_type_without_ttl(
1293
12.5k
        FileCacheType cur_cache_type) {
1294
12.5k
    switch (cur_cache_type) {
1295
3.91k
    case FileCacheType::TTL:
1296
3.91k
        return {FileCacheType::DISPOSABLE, FileCacheType::NORMAL, FileCacheType::INDEX};
1297
671
    case FileCacheType::INDEX:
1298
671
        return {FileCacheType::DISPOSABLE, FileCacheType::NORMAL};
1299
7.31k
    case FileCacheType::NORMAL:
1300
7.31k
        return {FileCacheType::DISPOSABLE, FileCacheType::INDEX};
1301
617
    case FileCacheType::DISPOSABLE:
1302
617
        return {FileCacheType::NORMAL, FileCacheType::INDEX};
1303
0
    default:
1304
0
        return {};
1305
12.5k
    }
1306
0
    return {};
1307
12.5k
}
1308
1309
889
std::vector<FileCacheType> BlockFileCache::get_other_cache_type(FileCacheType cur_cache_type) {
1310
889
    switch (cur_cache_type) {
1311
283
    case FileCacheType::TTL:
1312
283
        return {FileCacheType::DISPOSABLE, FileCacheType::NORMAL, FileCacheType::INDEX};
1313
143
    case FileCacheType::INDEX:
1314
143
        return {FileCacheType::DISPOSABLE, FileCacheType::NORMAL, FileCacheType::TTL};
1315
311
    case FileCacheType::NORMAL:
1316
311
        return {FileCacheType::DISPOSABLE, FileCacheType::INDEX, FileCacheType::TTL};
1317
152
    case FileCacheType::DISPOSABLE:
1318
152
        return {FileCacheType::NORMAL, FileCacheType::INDEX, FileCacheType::TTL};
1319
0
    default:
1320
0
        return {};
1321
889
    }
1322
0
    return {};
1323
889
}
1324
1325
void BlockFileCache::reset_range(const UInt128Wrapper& hash, size_t offset, size_t old_size,
1326
5
                                 size_t new_size, std::lock_guard<std::mutex>& cache_lock) {
1327
5
    DCHECK(_files.find(hash) != _files.end() &&
1328
5
           _files.find(hash)->second.find(offset) != _files.find(hash)->second.end());
1329
5
    FileBlockCell* cell = get_cell(hash, offset, cache_lock);
1330
5
    DCHECK(cell != nullptr);
1331
5
    if (cell == nullptr) {
1332
0
        LOG(WARNING) << "reset_range skipped because cache cell is missing. hash="
1333
0
                     << hash.to_string() << " offset=" << offset << " old_size=" << old_size
1334
0
                     << " new_size=" << new_size;
1335
0
        return;
1336
0
    }
1337
5
    DCHECK_EQ(cell->file_block->_block_range.size(), old_size);
1338
5
    if (cell->queue_iterator) {
1339
5
        auto& queue = get_queue(cell->file_block->cache_type());
1340
5
        DCHECK(queue.contains(hash, offset, cache_lock));
1341
5
        queue.resize(*cell->queue_iterator, new_size, cache_lock);
1342
5
        _lru_recorder->record_queue_event(cell->file_block->cache_type(), CacheLRULogType::RESIZE,
1343
5
                                          cell->file_block->get_hash_value(),
1344
5
                                          cell->file_block->offset(), new_size);
1345
5
    }
1346
5
    cell->file_block->_block_range.right = cell->file_block->_block_range.left + new_size - 1;
1347
5
    _cur_cache_size -= old_size;
1348
5
    _cur_cache_size += new_size;
1349
5
    if (cell->file_block->cache_type() == FileCacheType::TTL) {
1350
0
        _cur_ttl_size -= old_size;
1351
0
        _cur_ttl_size += new_size;
1352
0
    }
1353
5
}
1354
1355
bool BlockFileCache::try_reserve_from_other_queue_by_time_interval(
1356
        FileCacheType cur_type, std::vector<FileCacheType> other_cache_types, size_t size,
1357
12.5k
        int64_t cur_time, std::lock_guard<std::mutex>& cache_lock, bool evict_in_advance) {
1358
12.5k
    size_t removed_size = 0;
1359
12.5k
    size_t cur_cache_size = _cur_cache_size;
1360
12.5k
    std::vector<FileBlockCell*> to_evict;
1361
28.9k
    for (FileCacheType cache_type : other_cache_types) {
1362
28.9k
        auto& queue = get_queue(cache_type);
1363
28.9k
        size_t remove_size_per_type = 0;
1364
28.9k
        for (const auto& [entry_key, entry_offset, entry_size] : queue) {
1365
1.43k
            if (!is_overflow(removed_size, size, cur_cache_size, evict_in_advance)) {
1366
697
                break;
1367
697
            }
1368
738
            auto* cell = get_cell(entry_key, entry_offset, cache_lock);
1369
738
            DCHECK(cell) << "Cache became inconsistent. UInt128Wrapper: " << entry_key.to_string()
1370
0
                         << ", offset: " << entry_offset;
1371
1372
738
            size_t cell_size = cell->size();
1373
738
            DCHECK(entry_size == cell_size);
1374
1375
738
            if (cell->atime == 0 ? true : cell->atime + queue.get_hot_data_interval() > cur_time) {
1376
736
                break;
1377
736
            }
1378
1379
2
            if (cell->releasable()) {
1380
2
                auto& file_block = cell->file_block;
1381
2
                std::lock_guard block_lock(file_block->_mutex);
1382
2
                DCHECK(file_block->_download_state == FileBlock::State::DOWNLOADED);
1383
2
                to_evict.push_back(cell);
1384
2
                removed_size += cell_size;
1385
2
                remove_size_per_type += cell_size;
1386
2
            }
1387
2
        }
1388
28.9k
        *(_evict_by_time_metrics_matrix[cache_type][cur_type]) << remove_size_per_type;
1389
28.9k
    }
1390
12.5k
    bool is_sync_removal = !evict_in_advance;
1391
12.5k
    std::string reason = std::string("try_reserve_by_time ") +
1392
12.5k
                         " evict_in_advance=" + (evict_in_advance ? "true" : "false");
1393
12.5k
    remove_file_blocks(to_evict, cache_lock, is_sync_removal, reason);
1394
1395
12.5k
    return !is_overflow(removed_size, size, cur_cache_size, evict_in_advance);
1396
12.5k
}
1397
1398
bool BlockFileCache::is_overflow(size_t removed_size, size_t need_size, size_t cur_cache_size,
1399
17.0k
                                 bool evict_in_advance) const {
1400
17.0k
    bool ret = false;
1401
17.0k
    if (evict_in_advance) { // we don't need to check _need_evict_cache_in_advance
1402
11
        ret = (removed_size < need_size);
1403
11
        return ret;
1404
11
    }
1405
17.0k
    if (_disk_resource_limit_mode) {
1406
4
        ret = (removed_size < need_size);
1407
17.0k
    } else {
1408
17.0k
        ret = (cur_cache_size + need_size - removed_size > _capacity);
1409
17.0k
    }
1410
17.0k
    return ret;
1411
17.0k
}
1412
1413
bool BlockFileCache::try_reserve_from_other_queue_by_size(
1414
        FileCacheType cur_type, std::vector<FileCacheType> other_cache_types, size_t size,
1415
413
        std::lock_guard<std::mutex>& cache_lock, bool evict_in_advance) {
1416
413
    size_t removed_size = 0;
1417
413
    size_t cur_cache_size = _cur_cache_size;
1418
413
    std::vector<FileBlockCell*> to_evict;
1419
    // we follow the privilege defined in get_other_cache_types to evict
1420
1.23k
    for (FileCacheType cache_type : other_cache_types) {
1421
1.23k
        auto& queue = get_queue(cache_type);
1422
1423
        // we will not drain each of them to the bottom -- i.e., we only
1424
        // evict what they have stolen.
1425
1.23k
        size_t cur_queue_size = queue.get_capacity(cache_lock);
1426
1.23k
        size_t cur_queue_max_size = queue.get_max_size();
1427
1.23k
        if (cur_queue_size <= cur_queue_max_size) {
1428
735
            continue;
1429
735
        }
1430
504
        size_t cur_removed_size = 0;
1431
504
        find_evict_candidates(queue, size, cur_cache_size, removed_size, to_evict, cache_lock,
1432
504
                              cur_removed_size, evict_in_advance);
1433
504
        *(_evict_by_size_metrics_matrix[cache_type][cur_type]) << cur_removed_size;
1434
504
    }
1435
413
    bool is_sync_removal = !evict_in_advance;
1436
413
    std::string reason = std::string("try_reserve_by_size") +
1437
413
                         " evict_in_advance=" + (evict_in_advance ? "true" : "false");
1438
413
    remove_file_blocks(to_evict, cache_lock, is_sync_removal, reason);
1439
413
    return !is_overflow(removed_size, size, cur_cache_size, evict_in_advance);
1440
413
}
1441
1442
bool BlockFileCache::try_reserve_from_other_queue(FileCacheType cur_cache_type, size_t size,
1443
                                                  int64_t cur_time,
1444
                                                  std::lock_guard<std::mutex>& cache_lock,
1445
12.5k
                                                  bool evict_in_advance) {
1446
    // currently, TTL cache is not considered as a candidate
1447
12.5k
    auto other_cache_types = get_other_cache_type_without_ttl(cur_cache_type);
1448
12.5k
    bool reserve_success = try_reserve_from_other_queue_by_time_interval(
1449
12.5k
            cur_cache_type, other_cache_types, size, cur_time, cache_lock, evict_in_advance);
1450
12.5k
    if (reserve_success || !config::file_cache_enable_evict_from_other_queue_by_size) {
1451
11.6k
        return reserve_success;
1452
11.6k
    }
1453
1454
889
    other_cache_types = get_other_cache_type(cur_cache_type);
1455
889
    auto& cur_queue = get_queue(cur_cache_type);
1456
889
    size_t cur_queue_size = cur_queue.get_capacity(cache_lock);
1457
889
    size_t cur_queue_max_size = cur_queue.get_max_size();
1458
    // Hit the soft limit by self, cannot remove from other queues
1459
889
    if (_cur_cache_size + size > _capacity && cur_queue_size + size > cur_queue_max_size) {
1460
476
        return false;
1461
476
    }
1462
413
    return try_reserve_from_other_queue_by_size(cur_cache_type, other_cache_types, size, cache_lock,
1463
413
                                                evict_in_advance);
1464
889
}
1465
1466
bool BlockFileCache::try_reserve_for_lru(const UInt128Wrapper& hash,
1467
                                         QueryFileCacheContextPtr query_context,
1468
                                         const CacheContext& context, size_t offset, size_t size,
1469
                                         std::lock_guard<std::mutex>& cache_lock,
1470
12.5k
                                         bool evict_in_advance) {
1471
12.5k
    int64_t cur_time = std::chrono::duration_cast<std::chrono::seconds>(
1472
12.5k
                               std::chrono::steady_clock::now().time_since_epoch())
1473
12.5k
                               .count();
1474
12.5k
    if (!try_reserve_from_other_queue(context.cache_type, size, cur_time, cache_lock,
1475
12.5k
                                      evict_in_advance)) {
1476
477
        auto& queue = get_queue(context.cache_type);
1477
477
        size_t removed_size = 0;
1478
477
        size_t cur_cache_size = _cur_cache_size;
1479
1480
477
        std::vector<FileBlockCell*> to_evict;
1481
477
        size_t cur_removed_size = 0;
1482
477
        find_evict_candidates(queue, size, cur_cache_size, removed_size, to_evict, cache_lock,
1483
477
                              cur_removed_size, evict_in_advance);
1484
477
        bool is_sync_removal = !evict_in_advance;
1485
477
        std::string reason = std::string("try_reserve for cache type ") +
1486
477
                             cache_type_to_string(context.cache_type) +
1487
477
                             " evict_in_advance=" + (evict_in_advance ? "true" : "false");
1488
477
        remove_file_blocks(to_evict, cache_lock, is_sync_removal, reason);
1489
477
        *(_evict_by_self_lru_metrics_matrix[context.cache_type]) << cur_removed_size;
1490
1491
477
        if (is_overflow(removed_size, size, cur_cache_size, evict_in_advance)) {
1492
61
            return false;
1493
61
        }
1494
477
    }
1495
1496
12.4k
    if (query_context) {
1497
16
        query_context->reserve(hash, offset, size, cache_lock);
1498
16
    }
1499
12.4k
    return true;
1500
12.5k
}
1501
1502
template <class T, class U>
1503
    requires IsXLock<T> && IsXLock<U>
1504
107k
void BlockFileCache::remove(FileBlockSPtr file_block, T& cache_lock, U& block_lock, bool sync) {
1505
107k
    auto hash = file_block->get_hash_value();
1506
107k
    auto offset = file_block->offset();
1507
107k
    auto type = file_block->cache_type();
1508
107k
    auto expiration_time = file_block->expiration_time();
1509
107k
    auto tablet_id = file_block->tablet_id();
1510
107k
    auto* cell = get_cell(hash, offset, cache_lock);
1511
107k
    file_block->cell = nullptr;
1512
    // Holder cleanup can race with prior cache metadata cleanup. In that case,
1513
    // skip the duplicate remove instead of touching a detached or replaced cell.
1514
107k
    if (cell == nullptr) {
1515
1
        LOG(WARNING) << "remove skipped because cache cell is missing. hash=" << hash.to_string()
1516
1
                     << " offset=" << offset << " size=" << file_block->range().size()
1517
1
                     << " type=" << cache_type_to_string(type)
1518
1
                     << " state=" << FileBlock::state_to_string(file_block->state_unsafe())
1519
1
                     << " expiration_time=" << expiration_time << " sync=" << sync;
1520
1
        return;
1521
1
    }
1522
107k
    if (cell->file_block.get() != file_block.get()) {
1523
1
        auto* cell_file_block = cell->file_block.get();
1524
1
        LOG(WARNING)
1525
1
                << "remove skipped because cache cell points to a different file block. hash="
1526
1
                << hash.to_string() << " offset=" << offset
1527
1
                << " size=" << file_block->range().size() << " type=" << cache_type_to_string(type)
1528
1
                << " state=" << FileBlock::state_to_string(file_block->state_unsafe())
1529
1
                << " expiration_time=" << expiration_time << " sync=" << sync << " cell_block_hash="
1530
1
                << (cell_file_block ? cell_file_block->get_hash_value().to_string() : "<null>")
1531
1
                << " cell_block_offset="
1532
1
                << (cell_file_block ? std::to_string(cell_file_block->offset()) : "<null>")
1533
1
                << " cell_block_size="
1534
1
                << (cell_file_block ? std::to_string(cell_file_block->range().size()) : "<null>")
1535
1
                << " cell_block_type="
1536
1
                << (cell_file_block ? cache_type_to_string(cell_file_block->cache_type())
1537
1
                                    : "<null>")
1538
1
                << " cell_block_state="
1539
1
                << (cell_file_block ? FileBlock::state_to_string(cell_file_block->state_unsafe())
1540
1
                                    : "<null>");
1541
1
        return;
1542
1
    }
1543
107k
    DCHECK(cell->queue_iterator);
1544
107k
    if (cell->queue_iterator) {
1545
107k
        auto& queue = get_queue(file_block->cache_type());
1546
107k
        queue.remove(*cell->queue_iterator, cache_lock);
1547
107k
        _lru_recorder->record_queue_event(file_block->cache_type(), CacheLRULogType::REMOVE,
1548
107k
                                          cell->file_block->get_hash_value(),
1549
107k
                                          cell->file_block->offset(), cell->size());
1550
107k
    }
1551
107k
    *_queue_evict_size_metrics[static_cast<int>(file_block->cache_type())]
1552
107k
            << file_block->range().size();
1553
107k
    *_total_evict_size_metrics << file_block->range().size();
1554
1555
107k
    VLOG_DEBUG << "Removing file block from cache. hash: " << hash.to_string()
1556
0
               << ", offset: " << offset << ", size: " << file_block->range().size()
1557
0
               << ", type: " << cache_type_to_string(type);
1558
1559
107k
    if (file_block->state_unlock(block_lock) == FileBlock::State::DOWNLOADED) {
1560
105k
        FileCacheKey key;
1561
105k
        key.hash = hash;
1562
105k
        key.offset = offset;
1563
105k
        key.meta.type = type;
1564
105k
        key.meta.expiration_time = expiration_time;
1565
105k
        key.meta.tablet_id = tablet_id;
1566
105k
        if (sync) {
1567
105k
            int64_t duration_ns = 0;
1568
105k
            Status st;
1569
105k
            {
1570
105k
                SCOPED_RAW_TIMER(&duration_ns);
1571
105k
                st = _storage->remove(key);
1572
105k
            }
1573
105k
            *_storage_sync_remove_latency_us << (duration_ns / 1000);
1574
105k
            if (!st.ok()) {
1575
0
                LOG_WARNING("").error(st);
1576
0
            }
1577
105k
        } else {
1578
            // the file will be deleted in the bottom half
1579
            // so there will be a window that the file is not in the cache but still in the storage
1580
            // but it's ok, because the rowset is stale already
1581
45
            bool ret = _recycle_keys.enqueue(key);
1582
45
            if (ret) [[likely]] {
1583
45
                *_recycle_keys_length_recorder << _recycle_keys.size_approx();
1584
45
            } else {
1585
0
                LOG_WARNING("Failed to push recycle key to queue, do it synchronously");
1586
0
                int64_t duration_ns = 0;
1587
0
                Status st;
1588
0
                {
1589
0
                    SCOPED_RAW_TIMER(&duration_ns);
1590
0
                    st = _storage->remove(key);
1591
0
                }
1592
0
                *_storage_retry_sync_remove_latency_us << (duration_ns / 1000);
1593
0
                if (!st.ok()) {
1594
0
                    LOG_WARNING("").error(st);
1595
0
                }
1596
0
            }
1597
45
        }
1598
105k
    } else if (file_block->state_unlock(block_lock) == FileBlock::State::DOWNLOADING) {
1599
100
        file_block->set_deleting();
1600
100
        return;
1601
100
    }
1602
107k
    _cur_cache_size -= file_block->range().size();
1603
107k
    if (FileCacheType::TTL == type) {
1604
1.29k
        _cur_ttl_size -= file_block->range().size();
1605
1.29k
    }
1606
107k
    auto it = _files.find(hash);
1607
107k
    if (it != _files.end()) {
1608
107k
        it->second.erase(file_block->offset());
1609
107k
        if (it->second.empty()) {
1610
5.95k
            _files.erase(hash);
1611
5.95k
        }
1612
107k
    }
1613
107k
    *_num_removed_blocks << 1;
1614
107k
}
1615
1616
46
size_t BlockFileCache::get_used_cache_size(FileCacheType cache_type) const {
1617
46
    SCOPED_CACHE_LOCK(_mutex, this);
1618
46
    return get_used_cache_size_unlocked(cache_type, cache_lock);
1619
46
}
1620
1621
size_t BlockFileCache::get_used_cache_size_unlocked(FileCacheType cache_type,
1622
46
                                                    std::lock_guard<std::mutex>& cache_lock) const {
1623
46
    return get_queue(cache_type).get_capacity(cache_lock);
1624
46
}
1625
1626
0
size_t BlockFileCache::get_available_cache_size(FileCacheType cache_type) const {
1627
0
    SCOPED_CACHE_LOCK(_mutex, this);
1628
0
    return get_available_cache_size_unlocked(cache_type, cache_lock);
1629
0
}
1630
1631
size_t BlockFileCache::get_available_cache_size_unlocked(
1632
0
        FileCacheType cache_type, std::lock_guard<std::mutex>& cache_lock) const {
1633
0
    return get_queue(cache_type).get_max_element_size() -
1634
0
           get_used_cache_size_unlocked(cache_type, cache_lock);
1635
0
}
1636
1637
91
size_t BlockFileCache::get_file_blocks_num(FileCacheType cache_type) const {
1638
91
    SCOPED_CACHE_LOCK(_mutex, this);
1639
91
    return get_file_blocks_num_unlocked(cache_type, cache_lock);
1640
91
}
1641
1642
size_t BlockFileCache::get_file_blocks_num_unlocked(FileCacheType cache_type,
1643
91
                                                    std::lock_guard<std::mutex>& cache_lock) const {
1644
91
    return get_queue(cache_type).get_elements_num(cache_lock);
1645
91
}
1646
1647
FileBlockCell::FileBlockCell(FileBlockSPtr file_block, std::lock_guard<std::mutex>& cache_lock)
1648
113k
        : file_block(file_block) {
1649
113k
    file_block->cell = this;
1650
    /**
1651
     * Cell can be created with either DOWNLOADED or EMPTY file block's state.
1652
     * File block acquires DOWNLOADING state and creates LRUQueue iterator on first
1653
     * successful getOrSetDownaloder call.
1654
     */
1655
1656
113k
    switch (file_block->_download_state) {
1657
100k
    case FileBlock::State::DOWNLOADED:
1658
113k
    case FileBlock::State::EMPTY:
1659
113k
    case FileBlock::State::SKIP_CACHE: {
1660
113k
        break;
1661
113k
    }
1662
0
    default:
1663
0
        DCHECK(false) << "Can create cell with either EMPTY, DOWNLOADED, SKIP_CACHE state, got: "
1664
0
                      << FileBlock::state_to_string(file_block->_download_state);
1665
113k
    }
1666
113k
    if (file_block->cache_type() == FileCacheType::TTL) {
1667
3.92k
        update_atime();
1668
3.92k
    }
1669
113k
}
1670
1671
LRUQueue::Iterator LRUQueue::add(const UInt128Wrapper& hash, size_t offset, size_t size,
1672
226k
                                 std::lock_guard<std::mutex>& /* cache_lock */) {
1673
226k
    cache_size += size;
1674
226k
    auto iter = queue.insert(queue.end(), FileKeyAndOffset(hash, offset, size));
1675
226k
    map.insert(std::make_pair(std::make_pair(hash, offset), iter));
1676
226k
    return iter;
1677
226k
}
1678
1679
0
void LRUQueue::remove_all(std::lock_guard<std::mutex>& /* cache_lock */) {
1680
0
    queue.clear();
1681
0
    map.clear();
1682
0
    cache_size = 0;
1683
0
}
1684
1685
1.03M
void LRUQueue::move_to_end(Iterator queue_it, std::lock_guard<std::mutex>& /* cache_lock */) {
1686
1.03M
    queue.splice(queue.end(), queue, queue_it);
1687
1.03M
}
1688
1689
void LRUQueue::resize(Iterator queue_it, size_t new_size,
1690
9
                      std::lock_guard<std::mutex>& /* cache_lock */) {
1691
9
    cache_size -= queue_it->size;
1692
9
    queue_it->size = new_size;
1693
9
    cache_size += new_size;
1694
9
}
1695
bool LRUQueue::contains(const UInt128Wrapper& hash, size_t offset,
1696
5
                        std::lock_guard<std::mutex>& /* cache_lock */) const {
1697
5
    return map.find(std::make_pair(hash, offset)) != map.end();
1698
5
}
1699
1700
LRUQueue::Iterator LRUQueue::get(const UInt128Wrapper& hash, size_t offset,
1701
628k
                                 std::lock_guard<std::mutex>& /* cache_lock */) const {
1702
628k
    auto itr = map.find(std::make_pair(hash, offset));
1703
628k
    if (itr != map.end()) {
1704
628k
        return itr->second;
1705
628k
    }
1706
118
    return std::list<FileKeyAndOffset>::iterator();
1707
628k
}
1708
1709
2
std::string LRUQueue::to_string(std::lock_guard<std::mutex>& /* cache_lock */) const {
1710
2
    std::string result;
1711
18
    for (const auto& [hash, offset, size] : queue) {
1712
18
        if (!result.empty()) {
1713
16
            result += ", ";
1714
16
        }
1715
18
        result += fmt::format("{}: [{}, {}]", hash.to_string(), offset, offset + size - 1);
1716
18
    }
1717
2
    return result;
1718
2
}
1719
1720
size_t LRUQueue::levenshtein_distance_from(LRUQueue& base,
1721
5
                                           std::lock_guard<std::mutex>& cache_lock) {
1722
5
    std::list<FileKeyAndOffset> target_queue = this->queue;
1723
5
    std::list<FileKeyAndOffset> base_queue = base.queue;
1724
5
    std::vector<FileKeyAndOffset> vec1(target_queue.begin(), target_queue.end());
1725
5
    std::vector<FileKeyAndOffset> vec2(base_queue.begin(), base_queue.end());
1726
1727
5
    size_t m = vec1.size();
1728
5
    size_t n = vec2.size();
1729
1730
    // Create a 2D vector (matrix) to store the Levenshtein distances
1731
    // dp[i][j] will hold the distance between the first i elements of vec1 and the first j elements of vec2
1732
5
    std::vector<std::vector<size_t>> dp(m + 1, std::vector<size_t>(n + 1, 0));
1733
1734
    // Initialize the first row and column of the matrix
1735
    // The distance between an empty list and a list of length k is k (all insertions or deletions)
1736
22
    for (size_t i = 0; i <= m; ++i) {
1737
17
        dp[i][0] = i;
1738
17
    }
1739
20
    for (size_t j = 0; j <= n; ++j) {
1740
15
        dp[0][j] = j;
1741
15
    }
1742
1743
    // Fill the matrix using dynamic programming
1744
17
    for (size_t i = 1; i <= m; ++i) {
1745
38
        for (size_t j = 1; j <= n; ++j) {
1746
            // Check if the current elements of both vectors are equal
1747
26
            size_t cost = (vec1[i - 1].hash == vec2[j - 1].hash &&
1748
26
                           vec1[i - 1].offset == vec2[j - 1].offset)
1749
26
                                  ? 0
1750
26
                                  : 1;
1751
            // Calculate the minimum cost of three possible operations:
1752
            // 1. Insertion: dp[i][j-1] + 1
1753
            // 2. Deletion: dp[i-1][j] + 1
1754
            // 3. Substitution: dp[i-1][j-1] + cost (0 if elements are equal, 1 if not)
1755
26
            dp[i][j] = std::min({dp[i - 1][j] + 1, dp[i][j - 1] + 1, dp[i - 1][j - 1] + cost});
1756
26
        }
1757
12
    }
1758
    // The bottom-right cell of the matrix contains the Levenshtein distance
1759
5
    return dp[m][n];
1760
5
}
1761
1762
1
std::string BlockFileCache::dump_structure(const UInt128Wrapper& hash) {
1763
1
    SCOPED_CACHE_LOCK(_mutex, this);
1764
1
    return dump_structure_unlocked(hash, cache_lock);
1765
1
}
1766
1767
std::string BlockFileCache::dump_structure_unlocked(const UInt128Wrapper& hash,
1768
1
                                                    std::lock_guard<std::mutex>&) {
1769
1
    std::stringstream result;
1770
1
    auto it = _files.find(hash);
1771
1
    if (it == _files.end()) {
1772
0
        return std::string("");
1773
0
    }
1774
1
    const auto& cells_by_offset = it->second;
1775
1776
1
    for (const auto& [_, cell] : cells_by_offset) {
1777
1
        result << cell.file_block->get_info_for_log() << " "
1778
1
               << cache_type_to_string(cell.file_block->cache_type()) << "\n";
1779
1
    }
1780
1781
1
    return result.str();
1782
1
}
1783
1784
0
std::string BlockFileCache::dump_single_cache_type(const UInt128Wrapper& hash, size_t offset) {
1785
0
    SCOPED_CACHE_LOCK(_mutex, this);
1786
0
    return dump_single_cache_type_unlocked(hash, offset, cache_lock);
1787
0
}
1788
1789
std::string BlockFileCache::dump_single_cache_type_unlocked(const UInt128Wrapper& hash,
1790
                                                            size_t offset,
1791
0
                                                            std::lock_guard<std::mutex>&) {
1792
0
    std::stringstream result;
1793
0
    auto it = _files.find(hash);
1794
0
    if (it == _files.end()) {
1795
0
        return std::string("");
1796
0
    }
1797
0
    const auto& cells_by_offset = it->second;
1798
0
    const auto& cell = cells_by_offset.find(offset);
1799
1800
0
    return cache_type_to_string(cell->second.file_block->cache_type());
1801
0
}
1802
1803
void BlockFileCache::change_cache_type(const UInt128Wrapper& hash, size_t offset,
1804
                                       FileCacheType new_type,
1805
11
                                       std::lock_guard<std::mutex>& cache_lock) {
1806
11
    if (auto iter = _files.find(hash); iter != _files.end()) {
1807
11
        auto& file_blocks = iter->second;
1808
11
        if (auto cell_it = file_blocks.find(offset); cell_it != file_blocks.end()) {
1809
10
            FileBlockCell& cell = cell_it->second;
1810
10
            auto& cur_queue = get_queue(cell.file_block->cache_type());
1811
10
            DCHECK(cell.queue_iterator.has_value());
1812
10
            cur_queue.remove(*cell.queue_iterator, cache_lock);
1813
10
            _lru_recorder->record_queue_event(
1814
10
                    cell.file_block->cache_type(), CacheLRULogType::REMOVE,
1815
10
                    cell.file_block->get_hash_value(), cell.file_block->offset(), cell.size());
1816
10
            auto& new_queue = get_queue(new_type);
1817
10
            cell.queue_iterator =
1818
10
                    new_queue.add(hash, offset, cell.file_block->range().size(), cache_lock);
1819
10
            _lru_recorder->record_queue_event(new_type, CacheLRULogType::ADD,
1820
10
                                              cell.file_block->get_hash_value(),
1821
10
                                              cell.file_block->offset(), cell.size());
1822
10
        }
1823
11
    }
1824
11
}
1825
1826
// @brief: get a path's disk capacity used percent, inode used percent
1827
// @param: path
1828
// @param: percent.first disk used percent, percent.second inode used percent
1829
161
int disk_used_percentage(const std::string& path, std::pair<int, int>* percent) {
1830
161
    struct statfs stat;
1831
161
    int ret = statfs(path.c_str(), &stat);
1832
161
    if (ret != 0) {
1833
2
        return ret;
1834
2
    }
1835
    // https://github.com/coreutils/coreutils/blob/master/src/df.c#L1195
1836
    // v->used = stat.f_blocks - stat.f_bfree
1837
    // nonroot_total = stat.f_blocks - stat.f_bfree + stat.f_bavail
1838
159
    uintmax_t u100 = (stat.f_blocks - stat.f_bfree) * 100;
1839
159
    uintmax_t nonroot_total = stat.f_blocks - stat.f_bfree + stat.f_bavail;
1840
159
    int capacity_percentage = int(u100 / nonroot_total + (u100 % nonroot_total != 0));
1841
1842
159
    unsigned long long inode_free = stat.f_ffree;
1843
159
    unsigned long long inode_total = stat.f_files;
1844
159
    int inode_percentage = cast_set<int>(inode_free * 100 / inode_total);
1845
159
    percent->first = capacity_percentage;
1846
159
    percent->second = 100 - inode_percentage;
1847
1848
    // Add sync point for testing
1849
159
    TEST_SYNC_POINT_CALLBACK("BlockFileCache::disk_used_percentage:1", percent);
1850
1851
159
    return 0;
1852
161
}
1853
1854
10
std::string BlockFileCache::reset_capacity(size_t new_capacity) {
1855
10
    using namespace std::chrono;
1856
10
    int64_t space_released = 0;
1857
10
    size_t old_capacity = 0;
1858
10
    std::stringstream ss;
1859
10
    ss << "finish reset_capacity, path=" << _cache_base_path;
1860
10
    auto adjust_start_time = steady_clock::time_point();
1861
10
    {
1862
10
        SCOPED_CACHE_LOCK(_mutex, this);
1863
10
        if (new_capacity < _capacity && new_capacity < _cur_cache_size) {
1864
1
            int64_t need_remove_size = _cur_cache_size - new_capacity;
1865
4
            auto remove_blocks = [&](LRUQueue& queue) -> int64_t {
1866
4
                int64_t queue_released = 0;
1867
4
                std::vector<FileBlockCell*> to_evict;
1868
13
                for (const auto& [entry_key, entry_offset, entry_size] : queue) {
1869
13
                    if (need_remove_size <= 0) {
1870
1
                        break;
1871
1
                    }
1872
12
                    need_remove_size -= entry_size;
1873
12
                    space_released += entry_size;
1874
12
                    queue_released += entry_size;
1875
12
                    auto* cell = get_cell(entry_key, entry_offset, cache_lock);
1876
12
                    if (!cell->releasable()) {
1877
0
                        cell->file_block->set_deleting();
1878
0
                        continue;
1879
0
                    }
1880
12
                    to_evict.push_back(cell);
1881
12
                }
1882
12
                for (auto& cell : to_evict) {
1883
12
                    FileBlockSPtr file_block = cell->file_block;
1884
12
                    std::lock_guard block_lock(file_block->_mutex);
1885
12
                    remove(file_block, cache_lock, block_lock);
1886
12
                }
1887
4
                return queue_released;
1888
4
            };
1889
1
            int64_t queue_released = remove_blocks(_disposable_queue);
1890
1
            ss << " disposable_queue released " << queue_released;
1891
1
            queue_released = remove_blocks(_normal_queue);
1892
1
            ss << " normal_queue released " << queue_released;
1893
1
            queue_released = remove_blocks(_index_queue);
1894
1
            ss << " index_queue released " << queue_released;
1895
1
            queue_released = remove_blocks(_ttl_queue);
1896
1
            ss << " ttl_queue released " << queue_released;
1897
1898
1
            _disk_resource_limit_mode = true;
1899
1
            _disk_limit_mode_metrics->set_value(1);
1900
1
            ss << " total_space_released=" << space_released;
1901
1
        }
1902
10
        old_capacity = _capacity;
1903
10
        _capacity = new_capacity;
1904
10
        _cache_capacity_metrics->set_value(_capacity);
1905
10
    }
1906
10
    auto use_time = duration_cast<milliseconds>(steady_clock::time_point() - adjust_start_time);
1907
10
    LOG(INFO) << "Finish tag deleted block. path=" << _cache_base_path
1908
10
              << " use_time=" << cast_set<int64_t>(use_time.count());
1909
10
    ss << " old_capacity=" << old_capacity << " new_capacity=" << new_capacity;
1910
10
    LOG(INFO) << ss.str();
1911
10
    return ss.str();
1912
10
}
1913
1914
171
void BlockFileCache::check_disk_resource_limit() {
1915
171
    if (_storage->get_type() != FileCacheStorageType::DISK) {
1916
24
        return;
1917
24
    }
1918
1919
147
    bool previous_mode = _disk_resource_limit_mode;
1920
147
    if (_capacity > _cur_cache_size) {
1921
145
        _disk_resource_limit_mode = false;
1922
145
        _disk_limit_mode_metrics->set_value(0);
1923
145
    }
1924
147
    std::pair<int, int> percent;
1925
147
    int ret = disk_used_percentage(_cache_base_path, &percent);
1926
147
    if (ret != 0) {
1927
1
        LOG_ERROR("").tag("file cache path", _cache_base_path).tag("error", strerror(errno));
1928
1
        return;
1929
1
    }
1930
146
    auto [space_percentage, inode_percentage] = percent;
1931
292
    auto is_insufficient = [](const int& percentage) {
1932
292
        return percentage >= config::file_cache_enter_disk_resource_limit_mode_percent;
1933
292
    };
1934
146
    DCHECK_GE(space_percentage, 0);
1935
146
    DCHECK_LE(space_percentage, 100);
1936
146
    DCHECK_GE(inode_percentage, 0);
1937
146
    DCHECK_LE(inode_percentage, 100);
1938
    // ATTN: due to that can be changed dynamically, set it to default value if it's invalid
1939
    // FIXME: reject with config validator
1940
146
    if (config::file_cache_enter_disk_resource_limit_mode_percent <
1941
146
        config::file_cache_exit_disk_resource_limit_mode_percent) {
1942
1
        LOG_WARNING("config error, set to default value")
1943
1
                .tag("enter", config::file_cache_enter_disk_resource_limit_mode_percent)
1944
1
                .tag("exit", config::file_cache_exit_disk_resource_limit_mode_percent);
1945
1
        config::file_cache_enter_disk_resource_limit_mode_percent = 88;
1946
1
        config::file_cache_exit_disk_resource_limit_mode_percent = 80;
1947
1
    }
1948
146
    bool is_space_insufficient = is_insufficient(space_percentage);
1949
146
    bool is_inode_insufficient = is_insufficient(inode_percentage);
1950
146
    if (is_space_insufficient || is_inode_insufficient) {
1951
1
        _disk_resource_limit_mode = true;
1952
1
        _disk_limit_mode_metrics->set_value(1);
1953
145
    } else if (_disk_resource_limit_mode &&
1954
145
               (space_percentage < config::file_cache_exit_disk_resource_limit_mode_percent) &&
1955
145
               (inode_percentage < config::file_cache_exit_disk_resource_limit_mode_percent)) {
1956
0
        _disk_resource_limit_mode = false;
1957
0
        _disk_limit_mode_metrics->set_value(0);
1958
0
    }
1959
146
    if (previous_mode != _disk_resource_limit_mode) {
1960
        // add log for disk resource limit mode switching
1961
2
        if (_disk_resource_limit_mode) {
1962
1
            LOG(WARNING) << "Entering disk resource limit mode: file_cache=" << get_base_path()
1963
1
                         << " space_percent=" << space_percentage
1964
1
                         << " inode_percent=" << inode_percentage
1965
1
                         << " is_space_insufficient=" << is_space_insufficient
1966
1
                         << " is_inode_insufficient=" << is_inode_insufficient
1967
1
                         << " enter threshold="
1968
1
                         << config::file_cache_enter_disk_resource_limit_mode_percent;
1969
1
        } else {
1970
1
            LOG(INFO) << "Exiting disk resource limit mode: file_cache=" << get_base_path()
1971
1
                      << " space_percent=" << space_percentage
1972
1
                      << " inode_percent=" << inode_percentage << " exit threshold="
1973
1
                      << config::file_cache_exit_disk_resource_limit_mode_percent;
1974
1
        }
1975
144
    } else if (_disk_resource_limit_mode) {
1976
        // print log for disk resource limit mode running, but less frequently
1977
0
        LOG_EVERY_N(WARNING, 10) << "file_cache=" << get_base_path()
1978
0
                                 << " space_percent=" << space_percentage
1979
0
                                 << " inode_percent=" << inode_percentage
1980
0
                                 << " is_space_insufficient=" << is_space_insufficient
1981
0
                                 << " is_inode_insufficient=" << is_inode_insufficient
1982
0
                                 << " mode run in resource limit";
1983
0
    }
1984
146
}
1985
1986
15
void BlockFileCache::check_need_evict_cache_in_advance() {
1987
15
    if (_storage->get_type() != FileCacheStorageType::DISK) {
1988
1
        return;
1989
1
    }
1990
1991
14
    std::pair<int, int> percent;
1992
14
    int ret = disk_used_percentage(_cache_base_path, &percent);
1993
14
    if (ret != 0) {
1994
1
        LOG_ERROR("").tag("file cache path", _cache_base_path).tag("error", strerror(errno));
1995
1
        return;
1996
1
    }
1997
13
    auto [space_percentage, inode_percentage] = percent;
1998
13
    int size_percentage = static_cast<int>(_cur_cache_size * 100 / _capacity);
1999
39
    auto is_insufficient = [](const int& percentage) {
2000
39
        return percentage >= config::file_cache_enter_need_evict_cache_in_advance_percent;
2001
39
    };
2002
13
    DCHECK_GE(space_percentage, 0);
2003
13
    DCHECK_LE(space_percentage, 100);
2004
13
    DCHECK_GE(inode_percentage, 0);
2005
13
    DCHECK_LE(inode_percentage, 100);
2006
    // ATTN: due to that can be changed dynamically, set it to default value if it's invalid
2007
    // FIXME: reject with config validator
2008
13
    if (config::file_cache_enter_need_evict_cache_in_advance_percent <=
2009
13
        config::file_cache_exit_need_evict_cache_in_advance_percent) {
2010
1
        LOG_WARNING("config error, set to default value")
2011
1
                .tag("enter", config::file_cache_enter_need_evict_cache_in_advance_percent)
2012
1
                .tag("exit", config::file_cache_exit_need_evict_cache_in_advance_percent);
2013
1
        config::file_cache_enter_need_evict_cache_in_advance_percent = 78;
2014
1
        config::file_cache_exit_need_evict_cache_in_advance_percent = 75;
2015
1
    }
2016
13
    bool previous_mode = _need_evict_cache_in_advance;
2017
13
    bool is_space_insufficient = is_insufficient(space_percentage);
2018
13
    bool is_inode_insufficient = is_insufficient(inode_percentage);
2019
13
    bool is_size_insufficient = is_insufficient(size_percentage);
2020
13
    if (is_space_insufficient || is_inode_insufficient || is_size_insufficient) {
2021
9
        _need_evict_cache_in_advance = true;
2022
9
        _need_evict_cache_in_advance_metrics->set_value(1);
2023
9
    } else if (_need_evict_cache_in_advance &&
2024
4
               (space_percentage < config::file_cache_exit_need_evict_cache_in_advance_percent) &&
2025
4
               (inode_percentage < config::file_cache_exit_need_evict_cache_in_advance_percent) &&
2026
4
               (size_percentage < config::file_cache_exit_need_evict_cache_in_advance_percent)) {
2027
1
        _need_evict_cache_in_advance = false;
2028
1
        _need_evict_cache_in_advance_metrics->set_value(0);
2029
1
    }
2030
13
    if (previous_mode != _need_evict_cache_in_advance) {
2031
        // add log for evict cache in advance mode switching
2032
6
        if (_need_evict_cache_in_advance) {
2033
5
            LOG(WARNING) << "Entering evict cache in advance mode: "
2034
5
                         << "file_cache=" << get_base_path()
2035
5
                         << " space_percent=" << space_percentage
2036
5
                         << " inode_percent=" << inode_percentage
2037
5
                         << " size_percent=" << size_percentage
2038
5
                         << " is_space_insufficient=" << is_space_insufficient
2039
5
                         << " is_inode_insufficient=" << is_inode_insufficient
2040
5
                         << " is_size_insufficient=" << is_size_insufficient << " enter threshold="
2041
5
                         << config::file_cache_enter_need_evict_cache_in_advance_percent;
2042
5
        } else {
2043
1
            LOG(INFO) << "Exiting evict cache in advance mode: "
2044
1
                      << "file_cache=" << get_base_path() << " space_percent=" << space_percentage
2045
1
                      << " inode_percent=" << inode_percentage
2046
1
                      << " size_percent=" << size_percentage << " exit threshold="
2047
1
                      << config::file_cache_exit_need_evict_cache_in_advance_percent;
2048
1
        }
2049
7
    } else if (_need_evict_cache_in_advance) {
2050
        // print log for evict cache in advance mode running, but less frequently
2051
4
        LOG_EVERY_N(WARNING, 10) << "file_cache=" << get_base_path()
2052
1
                                 << " space_percent=" << space_percentage
2053
1
                                 << " inode_percent=" << inode_percentage
2054
1
                                 << " size_percent=" << size_percentage
2055
1
                                 << " is_space_insufficient=" << is_space_insufficient
2056
1
                                 << " is_inode_insufficient=" << is_inode_insufficient
2057
1
                                 << " is_size_insufficient=" << is_size_insufficient
2058
1
                                 << " need evict cache in advance";
2059
4
    }
2060
13
}
2061
2062
158
void BlockFileCache::run_background_monitor() {
2063
158
    Thread::set_self_name("run_background_monitor");
2064
171
    while (!_close) {
2065
171
        int64_t interval_ms = config::file_cache_background_monitor_interval_ms;
2066
171
        TEST_SYNC_POINT_CALLBACK("BlockFileCache::set_sleep_time", &interval_ms);
2067
171
        check_disk_resource_limit();
2068
171
        if (config::enable_evict_file_cache_in_advance) {
2069
7
            check_need_evict_cache_in_advance();
2070
164
        } else {
2071
164
            _need_evict_cache_in_advance = false;
2072
164
            _need_evict_cache_in_advance_metrics->set_value(0);
2073
164
        }
2074
2075
171
        {
2076
171
            std::unique_lock close_lock(_close_mtx);
2077
171
            _close_cv.wait_for(close_lock, std::chrono::milliseconds(interval_ms));
2078
171
            if (_close) {
2079
158
                break;
2080
158
            }
2081
171
        }
2082
        // report
2083
13
        {
2084
13
            SCOPED_CACHE_LOCK(_mutex, this);
2085
13
            _cur_cache_size_metrics->set_value(_cur_cache_size);
2086
13
            _cur_ttl_cache_size_metrics->set_value(_cur_cache_size -
2087
13
                                                   _index_queue.get_capacity(cache_lock) -
2088
13
                                                   _normal_queue.get_capacity(cache_lock) -
2089
13
                                                   _disposable_queue.get_capacity(cache_lock));
2090
13
            _cur_ttl_cache_lru_queue_cache_size_metrics->set_value(
2091
13
                    _ttl_queue.get_capacity(cache_lock));
2092
13
            _cur_ttl_cache_lru_queue_element_count_metrics->set_value(
2093
13
                    _ttl_queue.get_elements_num(cache_lock));
2094
13
            _cur_normal_queue_cache_size_metrics->set_value(_normal_queue.get_capacity(cache_lock));
2095
13
            _cur_normal_queue_element_count_metrics->set_value(
2096
13
                    _normal_queue.get_elements_num(cache_lock));
2097
13
            _cur_index_queue_cache_size_metrics->set_value(_index_queue.get_capacity(cache_lock));
2098
13
            _cur_index_queue_element_count_metrics->set_value(
2099
13
                    _index_queue.get_elements_num(cache_lock));
2100
13
            _cur_disposable_queue_cache_size_metrics->set_value(
2101
13
                    _disposable_queue.get_capacity(cache_lock));
2102
13
            _cur_disposable_queue_element_count_metrics->set_value(
2103
13
                    _disposable_queue.get_elements_num(cache_lock));
2104
2105
            // Update meta store write queue size if storage is FSFileCacheStorage
2106
13
            if (_storage->get_type() == FileCacheStorageType::DISK) {
2107
11
                auto* fs_storage = dynamic_cast<FSFileCacheStorage*>(_storage.get());
2108
11
                if (fs_storage != nullptr) {
2109
11
                    auto* meta_store = fs_storage->get_meta_store();
2110
11
                    if (meta_store != nullptr) {
2111
11
                        _meta_store_write_queue_size_metrics->set_value(
2112
11
                                meta_store->get_write_queue_size());
2113
11
                    }
2114
11
                }
2115
11
            }
2116
2117
13
            if (_num_read_blocks->get_value() > 0) {
2118
12
                _hit_ratio->set_value((double)_num_hit_blocks->get_value() /
2119
12
                                      (double)_num_read_blocks->get_value());
2120
12
            }
2121
13
            if (_num_read_blocks_5m && _num_read_blocks_5m->get_value() > 0) {
2122
0
                _hit_ratio_5m->set_value((double)_num_hit_blocks_5m->get_value() /
2123
0
                                         (double)_num_read_blocks_5m->get_value());
2124
0
            }
2125
13
            if (_num_read_blocks_1h && _num_read_blocks_1h->get_value() > 0) {
2126
0
                _hit_ratio_1h->set_value((double)_num_hit_blocks_1h->get_value() /
2127
0
                                         (double)_num_read_blocks_1h->get_value());
2128
0
            }
2129
2130
13
            if (_no_warmup_num_read_blocks->get_value() > 0) {
2131
12
                _no_warmup_hit_ratio->set_value((double)_no_warmup_num_hit_blocks->get_value() /
2132
12
                                                (double)_no_warmup_num_read_blocks->get_value());
2133
12
            }
2134
13
            if (_no_warmup_num_read_blocks_5m && _no_warmup_num_read_blocks_5m->get_value() > 0) {
2135
0
                _no_warmup_hit_ratio_5m->set_value(
2136
0
                        (double)_no_warmup_num_hit_blocks_5m->get_value() /
2137
0
                        (double)_no_warmup_num_read_blocks_5m->get_value());
2138
0
            }
2139
13
            if (_no_warmup_num_read_blocks_1h && _no_warmup_num_read_blocks_1h->get_value() > 0) {
2140
0
                _no_warmup_hit_ratio_1h->set_value(
2141
0
                        (double)_no_warmup_num_hit_blocks_1h->get_value() /
2142
0
                        (double)_no_warmup_num_read_blocks_1h->get_value());
2143
0
            }
2144
13
        }
2145
13
    }
2146
158
}
2147
2148
158
void BlockFileCache::run_background_gc() {
2149
158
    Thread::set_self_name("run_background_gc");
2150
158
    FileCacheKey key;
2151
158
    size_t batch_count = 0;
2152
432
    while (!_close) {
2153
432
        int64_t interval_ms = config::file_cache_background_gc_interval_ms;
2154
432
        size_t batch_limit = config::file_cache_remove_block_qps_limit * interval_ms / 1000;
2155
432
        {
2156
432
            std::unique_lock close_lock(_close_mtx);
2157
432
            _close_cv.wait_for(close_lock, std::chrono::milliseconds(interval_ms));
2158
432
            if (_close) {
2159
158
                break;
2160
158
            }
2161
432
        }
2162
2163
279
        while (batch_count < batch_limit && _recycle_keys.try_dequeue(key)) {
2164
5
            int64_t duration_ns = 0;
2165
5
            Status st;
2166
5
            {
2167
5
                SCOPED_RAW_TIMER(&duration_ns);
2168
5
                st = _storage->remove(key);
2169
5
            }
2170
5
            *_storage_async_remove_latency_us << (duration_ns / 1000);
2171
2172
5
            if (!st.ok()) {
2173
0
                LOG_WARNING("").error(st);
2174
0
            }
2175
5
            batch_count++;
2176
5
        }
2177
274
        *_recycle_keys_length_recorder << _recycle_keys.size_approx();
2178
274
        batch_count = 0;
2179
274
    }
2180
158
}
2181
2182
158
void BlockFileCache::run_background_evict_in_advance() {
2183
158
    Thread::set_self_name("run_background_evict_in_advance");
2184
158
    LOG(INFO) << "Starting background evict in advance thread";
2185
158
    int64_t batch = 0;
2186
275
    while (!_close) {
2187
275
        {
2188
275
            std::unique_lock close_lock(_close_mtx);
2189
275
            _close_cv.wait_for(
2190
275
                    close_lock,
2191
275
                    std::chrono::milliseconds(config::file_cache_evict_in_advance_interval_ms));
2192
275
            if (_close) {
2193
158
                LOG(INFO) << "Background evict in advance thread exiting due to cache closing";
2194
158
                break;
2195
158
            }
2196
275
        }
2197
117
        batch = config::file_cache_evict_in_advance_batch_bytes;
2198
2199
        // Skip if eviction not needed or too many pending recycles
2200
117
        if (!_need_evict_cache_in_advance ||
2201
117
            _recycle_keys.size_approx() >=
2202
116
                    config::file_cache_evict_in_advance_recycle_keys_num_threshold) {
2203
116
            continue;
2204
116
        }
2205
2206
1
        int64_t duration_ns = 0;
2207
1
        {
2208
1
            SCOPED_CACHE_LOCK(_mutex, this);
2209
1
            SCOPED_RAW_TIMER(&duration_ns);
2210
1
            try_evict_in_advance(batch, cache_lock);
2211
1
        }
2212
1
        *_evict_in_advance_latency_us << (duration_ns / 1000);
2213
1
    }
2214
158
}
2215
2216
158
void BlockFileCache::run_background_block_lru_update() {
2217
158
    Thread::set_self_name("run_background_block_lru_update");
2218
158
    std::vector<FileBlockSPtr> batch;
2219
198
    while (!_close) {
2220
198
        int64_t interval_ms = config::file_cache_background_block_lru_update_interval_ms;
2221
198
        size_t batch_limit =
2222
198
                config::file_cache_background_block_lru_update_qps_limit * interval_ms / 1000;
2223
198
        {
2224
198
            std::unique_lock close_lock(_close_mtx);
2225
198
            _close_cv.wait_for(close_lock, std::chrono::milliseconds(interval_ms));
2226
198
            if (_close) {
2227
158
                break;
2228
158
            }
2229
198
        }
2230
2231
40
        batch.clear();
2232
40
        batch.reserve(batch_limit);
2233
40
        size_t drained = _need_update_lru_blocks.drain(batch_limit, &batch);
2234
40
        if (drained == 0) {
2235
35
            *_need_update_lru_blocks_length_recorder << _need_update_lru_blocks.size();
2236
35
            continue;
2237
35
        }
2238
5
        *_need_update_lru_blocks_consume_metrics << drained;
2239
2240
5
        int64_t duration_ns = 0;
2241
5
        {
2242
5
            SCOPED_CACHE_LOCK(_mutex, this);
2243
5
            SCOPED_RAW_TIMER(&duration_ns);
2244
5
            for (auto& block : batch) {
2245
5
                update_block_lru(block, cache_lock);
2246
5
            }
2247
5
        }
2248
5
        *_update_lru_blocks_latency_us << (duration_ns / 1000);
2249
5
        *_need_update_lru_blocks_length_recorder << _need_update_lru_blocks.size();
2250
5
    }
2251
158
}
2252
2253
std::vector<std::tuple<size_t, size_t, FileCacheType, uint64_t>>
2254
2
BlockFileCache::get_hot_blocks_meta(const UInt128Wrapper& hash) const {
2255
2
    int64_t cur_time = std::chrono::duration_cast<std::chrono::seconds>(
2256
2
                               std::chrono::steady_clock::now().time_since_epoch())
2257
2
                               .count();
2258
2
    SCOPED_CACHE_LOCK(_mutex, this);
2259
2
    std::vector<std::tuple<size_t, size_t, FileCacheType, uint64_t>> blocks_meta;
2260
2
    if (auto iter = _files.find(hash); iter != _files.end()) {
2261
5
        for (auto& pair : _files.find(hash)->second) {
2262
5
            const FileBlockCell* cell = &pair.second;
2263
5
            if (cell->file_block->cache_type() != FileCacheType::DISPOSABLE) {
2264
4
                if (cell->file_block->cache_type() == FileCacheType::TTL ||
2265
4
                    (cell->atime != 0 &&
2266
3
                     cur_time - cell->atime <
2267
3
                             get_queue(cell->file_block->cache_type()).get_hot_data_interval())) {
2268
3
                    blocks_meta.emplace_back(pair.first, cell->size(),
2269
3
                                             cell->file_block->cache_type(),
2270
3
                                             cell->file_block->expiration_time());
2271
3
                }
2272
4
            }
2273
5
        }
2274
2
    }
2275
2
    return blocks_meta;
2276
2
}
2277
2278
bool BlockFileCache::try_reserve_during_async_load(size_t size,
2279
1.00k
                                                   std::lock_guard<std::mutex>& cache_lock) {
2280
1.00k
    size_t removed_size = 0;
2281
1.00k
    size_t normal_queue_size = _normal_queue.get_capacity(cache_lock);
2282
1.00k
    size_t disposable_queue_size = _disposable_queue.get_capacity(cache_lock);
2283
1.00k
    size_t index_queue_size = _index_queue.get_capacity(cache_lock);
2284
2285
1.00k
    std::vector<FileBlockCell*> to_evict;
2286
1.00k
    auto collect_eliminate_fragments = [&](LRUQueue& queue) {
2287
1.00k
        for (const auto& [entry_key, entry_offset, entry_size] : queue) {
2288
1.00k
            if (!_disk_resource_limit_mode || removed_size >= size) {
2289
1.00k
                break;
2290
1.00k
            }
2291
1
            auto* cell = get_cell(entry_key, entry_offset, cache_lock);
2292
2293
1
            DCHECK(cell) << "Cache became inconsistent. UInt128Wrapper: " << entry_key.to_string()
2294
0
                         << ", offset: " << entry_offset;
2295
2296
1
            size_t cell_size = cell->size();
2297
1
            DCHECK(entry_size == cell_size);
2298
2299
1
            if (cell->releasable()) {
2300
1
                auto& file_block = cell->file_block;
2301
2302
1
                std::lock_guard block_lock(file_block->_mutex);
2303
1
                DCHECK(file_block->_download_state == FileBlock::State::DOWNLOADED);
2304
1
                to_evict.push_back(cell);
2305
1
                removed_size += cell_size;
2306
1
            }
2307
1
        }
2308
1.00k
    };
2309
1.00k
    if (disposable_queue_size != 0) {
2310
0
        collect_eliminate_fragments(get_queue(FileCacheType::DISPOSABLE));
2311
0
    }
2312
1.00k
    if (normal_queue_size != 0) {
2313
1.00k
        collect_eliminate_fragments(get_queue(FileCacheType::NORMAL));
2314
1.00k
    }
2315
1.00k
    if (index_queue_size != 0) {
2316
0
        collect_eliminate_fragments(get_queue(FileCacheType::INDEX));
2317
0
    }
2318
1.00k
    std::string reason = "async load";
2319
1.00k
    remove_file_blocks(to_evict, cache_lock, true, reason);
2320
2321
1.00k
    return !_disk_resource_limit_mode || removed_size >= size;
2322
1.00k
}
2323
2324
24
void BlockFileCache::clear_need_update_lru_blocks() {
2325
24
    _need_update_lru_blocks.clear();
2326
24
    *_need_update_lru_blocks_length_recorder << _need_update_lru_blocks.size();
2327
24
}
2328
2329
5.14k
std::map<size_t, FileBlockSPtr> BlockFileCache::get_blocks_by_key(const UInt128Wrapper& hash) {
2330
5.14k
    std::map<size_t, FileBlockSPtr> offset_to_block;
2331
5.14k
    SCOPED_CACHE_LOCK(_mutex, this);
2332
5.14k
    if (_files.contains(hash)) {
2333
5.14k
        for (auto& [offset, cell] : _files[hash]) {
2334
5.14k
            if (cell.file_block->state() == FileBlock::State::DOWNLOADED) {
2335
5.14k
                cell.file_block->_owned_by_cached_reader = true;
2336
5.14k
                offset_to_block.emplace(offset, cell.file_block);
2337
5.14k
            }
2338
5.14k
        }
2339
5.13k
    }
2340
5.14k
    return offset_to_block;
2341
5.14k
}
2342
2343
0
void BlockFileCache::update_ttl_atime(const UInt128Wrapper& hash) {
2344
0
    SCOPED_CACHE_LOCK(_mutex, this);
2345
0
    if (auto iter = _files.find(hash); iter != _files.end()) {
2346
0
        for (auto& [_, cell] : iter->second) {
2347
0
            cell.update_atime();
2348
0
        }
2349
0
    };
2350
0
}
2351
2352
158
void BlockFileCache::run_background_lru_log_replay() {
2353
158
    Thread::set_self_name("run_background_lru_log_replay");
2354
102k
    while (!_close) {
2355
102k
        int64_t interval_ms = config::file_cache_background_lru_log_replay_interval_ms;
2356
102k
        {
2357
102k
            std::unique_lock close_lock(_close_mtx);
2358
102k
            _close_cv.wait_for(close_lock, std::chrono::milliseconds(interval_ms));
2359
102k
            if (_close) {
2360
140
                break;
2361
140
            }
2362
102k
        }
2363
2364
102k
        replay_lru_logs_once();
2365
102k
    }
2366
158
}
2367
2368
102k
size_t BlockFileCache::replay_lru_logs_once() {
2369
102k
    size_t replayed = 0;
2370
409k
    for (FileCacheType type : LRU_LOG_REPLAY_TYPES) {
2371
409k
        replayed += _lru_recorder->replay_queue_event(type);
2372
409k
    }
2373
2374
102k
    if (replayed == 0) {
2375
84.5k
        *_lru_recorder_log_replay_idle_metrics << 1;
2376
84.5k
    }
2377
2378
102k
    if (config::enable_evaluate_shadow_queue_diff) {
2379
0
        SCOPED_CACHE_LOCK(_mutex, this);
2380
0
        _lru_recorder->evaluate_queue_diff(_ttl_queue, "ttl", cache_lock);
2381
0
        _lru_recorder->evaluate_queue_diff(_index_queue, "index", cache_lock);
2382
0
        _lru_recorder->evaluate_queue_diff(_normal_queue, "normal", cache_lock);
2383
0
        _lru_recorder->evaluate_queue_diff(_disposable_queue, "disposable", cache_lock);
2384
0
    }
2385
102k
    return replayed;
2386
102k
}
2387
2388
14
void BlockFileCache::dump_lru_queues(bool force) {
2389
14
    std::unique_lock dump_lock(_dump_lru_queues_mtx);
2390
14
    if (config::file_cache_background_lru_dump_tail_record_num > 0 &&
2391
14
        !ExecEnv::GetInstance()->get_is_upgrading()) {
2392
14
        _lru_dumper->dump_queue("disposable", force);
2393
14
        _lru_dumper->dump_queue("normal", force);
2394
14
        _lru_dumper->dump_queue("index", force);
2395
14
        _lru_dumper->dump_queue("ttl", force);
2396
14
        _lru_dumper->set_first_dump_done();
2397
14
    }
2398
14
}
2399
2400
158
void BlockFileCache::run_background_lru_dump() {
2401
158
    Thread::set_self_name("run_background_lru_dump");
2402
172
    while (!_close) {
2403
172
        int64_t interval_ms = config::file_cache_background_lru_dump_interval_ms;
2404
172
        {
2405
172
            std::unique_lock close_lock(_close_mtx);
2406
172
            _close_cv.wait_for(close_lock, std::chrono::milliseconds(interval_ms));
2407
172
            if (_close) {
2408
158
                break;
2409
158
            }
2410
172
        }
2411
14
        dump_lru_queues(false);
2412
14
    }
2413
158
}
2414
2415
158
void BlockFileCache::restore_lru_queues_from_disk(std::lock_guard<std::mutex>& cache_lock) {
2416
    // keep this order coz may be duplicated in different queue, we use the first appearence
2417
158
    _lru_dumper->restore_queue(_ttl_queue, "ttl", cache_lock);
2418
158
    _lru_dumper->restore_queue(_index_queue, "index", cache_lock);
2419
158
    _lru_dumper->restore_queue(_normal_queue, "normal", cache_lock);
2420
158
    _lru_dumper->restore_queue(_disposable_queue, "disposable", cache_lock);
2421
158
}
2422
2423
0
std::map<std::string, double> BlockFileCache::get_stats() {
2424
0
    std::map<std::string, double> stats;
2425
0
    stats["hits_ratio"] = (double)_hit_ratio->get_value();
2426
0
    stats["hits_ratio_5m"] = (double)_hit_ratio_5m->get_value();
2427
0
    stats["hits_ratio_1h"] = (double)_hit_ratio_1h->get_value();
2428
2429
0
    stats["index_queue_max_size"] = (double)_index_queue.get_max_size();
2430
0
    stats["index_queue_curr_size"] = (double)_cur_index_queue_cache_size_metrics->get_value();
2431
0
    stats["index_queue_max_elements"] = (double)_index_queue.get_max_element_size();
2432
0
    stats["index_queue_curr_elements"] =
2433
0
            (double)_cur_index_queue_element_count_metrics->get_value();
2434
2435
0
    stats["ttl_queue_max_size"] = (double)_ttl_queue.get_max_size();
2436
0
    stats["ttl_queue_curr_size"] = (double)_cur_ttl_cache_lru_queue_cache_size_metrics->get_value();
2437
0
    stats["ttl_queue_max_elements"] = (double)_ttl_queue.get_max_element_size();
2438
0
    stats["ttl_queue_curr_elements"] =
2439
0
            (double)_cur_ttl_cache_lru_queue_element_count_metrics->get_value();
2440
2441
0
    stats["normal_queue_max_size"] = (double)_normal_queue.get_max_size();
2442
0
    stats["normal_queue_curr_size"] = (double)_cur_normal_queue_cache_size_metrics->get_value();
2443
0
    stats["normal_queue_max_elements"] = (double)_normal_queue.get_max_element_size();
2444
0
    stats["normal_queue_curr_elements"] =
2445
0
            (double)_cur_normal_queue_element_count_metrics->get_value();
2446
2447
0
    stats["disposable_queue_max_size"] = (double)_disposable_queue.get_max_size();
2448
0
    stats["disposable_queue_curr_size"] =
2449
0
            (double)_cur_disposable_queue_cache_size_metrics->get_value();
2450
0
    stats["disposable_queue_max_elements"] = (double)_disposable_queue.get_max_element_size();
2451
0
    stats["disposable_queue_curr_elements"] =
2452
0
            (double)_cur_disposable_queue_element_count_metrics->get_value();
2453
2454
0
    stats["need_evict_cache_in_advance"] = (double)_need_evict_cache_in_advance;
2455
0
    stats["disk_resource_limit_mode"] = (double)_disk_resource_limit_mode;
2456
2457
0
    stats["total_removed_counts"] = (double)_num_removed_blocks->get_value();
2458
0
    stats["total_hit_counts"] = (double)_num_hit_blocks->get_value();
2459
0
    stats["total_read_counts"] = (double)_num_read_blocks->get_value();
2460
2461
0
    stats["total_read_size"] = (double)_total_read_size_metrics->get_value();
2462
0
    stats["total_hit_size"] = (double)_total_hit_size_metrics->get_value();
2463
0
    stats["total_removed_size"] = (double)_total_evict_size_metrics->get_value();
2464
2465
0
    return stats;
2466
0
}
2467
2468
// for be UTs
2469
172
std::map<std::string, double> BlockFileCache::get_stats_unsafe() {
2470
172
    std::map<std::string, double> stats;
2471
172
    stats["hits_ratio"] = (double)_hit_ratio->get_value();
2472
172
    stats["hits_ratio_5m"] = (double)_hit_ratio_5m->get_value();
2473
172
    stats["hits_ratio_1h"] = (double)_hit_ratio_1h->get_value();
2474
2475
172
    stats["index_queue_max_size"] = (double)_index_queue.get_max_size();
2476
172
    stats["index_queue_curr_size"] = (double)_index_queue.get_capacity_unsafe();
2477
172
    stats["index_queue_max_elements"] = (double)_index_queue.get_max_element_size();
2478
172
    stats["index_queue_curr_elements"] = (double)_index_queue.get_elements_num_unsafe();
2479
2480
172
    stats["ttl_queue_max_size"] = (double)_ttl_queue.get_max_size();
2481
172
    stats["ttl_queue_curr_size"] = (double)_ttl_queue.get_capacity_unsafe();
2482
172
    stats["ttl_queue_max_elements"] = (double)_ttl_queue.get_max_element_size();
2483
172
    stats["ttl_queue_curr_elements"] = (double)_ttl_queue.get_elements_num_unsafe();
2484
2485
172
    stats["normal_queue_max_size"] = (double)_normal_queue.get_max_size();
2486
172
    stats["normal_queue_curr_size"] = (double)_normal_queue.get_capacity_unsafe();
2487
172
    stats["normal_queue_max_elements"] = (double)_normal_queue.get_max_element_size();
2488
172
    stats["normal_queue_curr_elements"] = (double)_normal_queue.get_elements_num_unsafe();
2489
2490
172
    stats["disposable_queue_max_size"] = (double)_disposable_queue.get_max_size();
2491
172
    stats["disposable_queue_curr_size"] = (double)_disposable_queue.get_capacity_unsafe();
2492
172
    stats["disposable_queue_max_elements"] = (double)_disposable_queue.get_max_element_size();
2493
172
    stats["disposable_queue_curr_elements"] = (double)_disposable_queue.get_elements_num_unsafe();
2494
2495
172
    stats["need_evict_cache_in_advance"] = (double)_need_evict_cache_in_advance;
2496
172
    stats["disk_resource_limit_mode"] = (double)_disk_resource_limit_mode;
2497
2498
172
    stats["total_removed_counts"] = (double)_num_removed_blocks->get_value();
2499
172
    stats["total_hit_counts"] = (double)_num_hit_blocks->get_value();
2500
172
    stats["total_read_counts"] = (double)_num_read_blocks->get_value();
2501
2502
172
    stats["total_read_size"] = (double)_total_read_size_metrics->get_value();
2503
172
    stats["total_hit_size"] = (double)_total_hit_size_metrics->get_value();
2504
172
    stats["total_removed_size"] = (double)_total_evict_size_metrics->get_value();
2505
2506
172
    return stats;
2507
172
}
2508
2509
template void BlockFileCache::remove(FileBlockSPtr file_block,
2510
                                     std::lock_guard<std::mutex>& cache_lock,
2511
                                     std::lock_guard<std::mutex>& block_lock, bool sync);
2512
2513
1
Status BlockFileCache::report_file_cache_inconsistency(std::vector<std::string>& results) {
2514
1
    InconsistencyContext inconsistency_context;
2515
1
    RETURN_IF_ERROR(check_file_cache_consistency(inconsistency_context));
2516
1
    auto n = inconsistency_context.types.size();
2517
1
    results.reserve(n);
2518
1
    for (size_t i = 0; i < n; i++) {
2519
0
        std::string result;
2520
0
        result += "File cache info in manager:\n";
2521
0
        result += inconsistency_context.infos_in_manager[i].to_string();
2522
0
        result += "File cache info in storage:\n";
2523
0
        result += inconsistency_context.infos_in_storage[i].to_string();
2524
0
        result += inconsistency_context.types[i].to_string();
2525
0
        result += "\n";
2526
0
        results.push_back(std::move(result));
2527
0
    }
2528
1
    return Status::OK();
2529
1
}
2530
2531
1
Status BlockFileCache::check_file_cache_consistency(InconsistencyContext& inconsistency_context) {
2532
1
    std::lock_guard<std::mutex> cache_lock(_mutex);
2533
1
    std::vector<FileCacheInfo> infos_in_storage;
2534
1
    RETURN_IF_ERROR(_storage->get_file_cache_infos(infos_in_storage, cache_lock));
2535
1
    std::unordered_set<AccessKeyAndOffset, KeyAndOffsetHash> confirmed_blocks;
2536
1
    for (const auto& info_in_storage : infos_in_storage) {
2537
0
        confirmed_blocks.insert({info_in_storage.hash, info_in_storage.offset});
2538
0
        auto* cell = get_cell(info_in_storage.hash, info_in_storage.offset, cache_lock);
2539
0
        if (cell == nullptr || cell->file_block == nullptr) {
2540
0
            inconsistency_context.infos_in_manager.emplace_back();
2541
0
            inconsistency_context.infos_in_storage.push_back(info_in_storage);
2542
0
            inconsistency_context.types.emplace_back(InconsistencyType::NOT_LOADED);
2543
0
            continue;
2544
0
        }
2545
0
        FileCacheInfo info_in_manager {
2546
0
                .hash = info_in_storage.hash,
2547
0
                .expiration_time = cell->file_block->expiration_time(),
2548
0
                .size = cell->size(),
2549
0
                .offset = info_in_storage.offset,
2550
0
                .is_tmp = cell->file_block->state() == FileBlock::State::DOWNLOADING,
2551
0
                .cache_type = cell->file_block->cache_type()};
2552
0
        InconsistencyType inconsistent_type;
2553
0
        if (info_in_storage.is_tmp != info_in_manager.is_tmp) {
2554
0
            inconsistent_type |= InconsistencyType::TMP_FILE_EXPECT_DOWNLOADING_STATE;
2555
0
        }
2556
0
        size_t expected_size =
2557
0
                info_in_manager.is_tmp ? cell->dowloading_size() : info_in_manager.size;
2558
0
        if (info_in_storage.size != expected_size) {
2559
0
            inconsistent_type |= InconsistencyType::SIZE_INCONSISTENT;
2560
0
        }
2561
        // Only if it is not a tmp file need we check the cache type.
2562
0
        if ((inconsistent_type & InconsistencyType::TMP_FILE_EXPECT_DOWNLOADING_STATE) == 0 &&
2563
0
            info_in_storage.cache_type != info_in_manager.cache_type) {
2564
0
            inconsistent_type |= InconsistencyType::CACHE_TYPE_INCONSISTENT;
2565
0
        }
2566
0
        if (info_in_storage.expiration_time != info_in_manager.expiration_time) {
2567
0
            inconsistent_type |= InconsistencyType::EXPIRATION_TIME_INCONSISTENT;
2568
0
        }
2569
0
        if (inconsistent_type != InconsistencyType::NONE) {
2570
0
            inconsistency_context.infos_in_manager.push_back(info_in_manager);
2571
0
            inconsistency_context.infos_in_storage.push_back(info_in_storage);
2572
0
            inconsistency_context.types.push_back(inconsistent_type);
2573
0
        }
2574
0
    }
2575
2576
1
    for (const auto& [hash, offset_to_cell] : _files) {
2577
0
        for (const auto& [offset, cell] : offset_to_cell) {
2578
0
            if (confirmed_blocks.contains({hash, offset})) {
2579
0
                continue;
2580
0
            }
2581
0
            const auto& block = cell.file_block;
2582
0
            inconsistency_context.infos_in_manager.emplace_back(
2583
0
                    hash, block->expiration_time(), cell.size(), offset,
2584
0
                    cell.file_block->state() == FileBlock::State::DOWNLOADING, block->cache_type());
2585
0
            inconsistency_context.infos_in_storage.emplace_back();
2586
0
            inconsistency_context.types.emplace_back(InconsistencyType::MISSING_IN_STORAGE);
2587
0
        }
2588
0
    }
2589
1
    return Status::OK();
2590
1
}
2591
2592
} // namespace doris::io