Coverage Report

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