Skip to content

Commit 4cbae8a

Browse files
[enhancement](filecache) fine-grained cache space observation (#57783)
1 parent acc1bcf commit 4cbae8a

9 files changed

Lines changed: 388 additions & 0 deletions

File tree

‎be/src/exec/schema_scanner.cpp‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@
3939
#include "exec/schema_scanner/schema_columns_scanner.h"
4040
#include "exec/schema_scanner/schema_dummy_scanner.h"
4141
#include "exec/schema_scanner/schema_encryption_keys_scanner.h"
42+
#include "exec/schema_scanner/schema_file_cache_info_scanner.h"
4243
#include "exec/schema_scanner/schema_file_cache_statistics.h"
4344
#include "exec/schema_scanner/schema_files_scanner.h"
4445
#include "exec/schema_scanner/schema_load_job_scanner.h"
@@ -257,6 +258,8 @@ std::unique_ptr<SchemaScanner> SchemaScanner::create(TSchemaTableType::type type
257258
return SchemaClusterSnapshotPropertiesScanner::create_unique();
258259
case TSchemaTableType::SCH_COLUMN_DATA_SIZES:
259260
return SchemaColumnDataSizesScanner::create_unique();
261+
case TSchemaTableType::SCH_FILE_CACHE_INFO:
262+
return SchemaFileCacheInfoScanner::create_unique();
260263
default:
261264
return SchemaDummyScanner::create_unique();
262265
break;
Lines changed: 189 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,189 @@
1+
// Licensed to the Apache Software Foundation (ASF) under one
2+
// or more contributor license agreements. See the NOTICE file
3+
// distributed with this work for additional information
4+
// regarding copyright ownership. The ASF licenses this file
5+
// to you under the Apache License, Version 2.0 (the
6+
// "License"); you may not use this file except in compliance
7+
// with the License. You may obtain a copy of the License at
8+
//
9+
// http://www.apache.org/licenses/LICENSE-2.0
10+
//
11+
// Unless required by applicable law or agreed to in writing,
12+
// software distributed under the License is distributed on an
13+
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14+
// KIND, either express or implied. See the License for the
15+
// specific language governing permissions and limitations
16+
// under the License.
17+
18+
#include "exec/schema_scanner/schema_file_cache_info_scanner.h"
19+
20+
#include "io/cache/file_cache_common.h"
21+
#include "runtime/exec_env.h"
22+
#include "runtime/runtime_state.h"
23+
#include "vec/common/string_ref.h"
24+
#include "vec/core/block.h"
25+
#include "vec/data_types/data_type_factory.hpp"
26+
27+
namespace doris {
28+
#include "common/compile_check_begin.h"
29+
30+
std::vector<SchemaScanner::ColumnDesc> SchemaFileCacheInfoScanner::_s_tbls_columns = {
31+
// name, type, size, is_null
32+
{"HASH", TYPE_STRING, sizeof(StringRef), true},
33+
{"TABLET_ID", TYPE_BIGINT, sizeof(int64_t), true},
34+
{"SIZE", TYPE_BIGINT, sizeof(int64_t), true},
35+
{"TYPE", TYPE_STRING, sizeof(StringRef), true},
36+
{"REMOTE_PATH", TYPE_STRING, sizeof(StringRef), true},
37+
{"CACHE_PATH", TYPE_STRING, sizeof(StringRef), true},
38+
{"BE_ID", TYPE_BIGINT, sizeof(int64_t), true}};
39+
40+
SchemaFileCacheInfoScanner::SchemaFileCacheInfoScanner()
41+
: SchemaScanner(_s_tbls_columns, TSchemaTableType::SCH_FILE_CACHE_INFO) {}
42+
43+
SchemaFileCacheInfoScanner::~SchemaFileCacheInfoScanner() {}
44+
45+
Status SchemaFileCacheInfoScanner::start(RuntimeState* state) {
46+
return Status::OK();
47+
}
48+
49+
Status SchemaFileCacheInfoScanner::get_next_block_internal(vectorized::Block* block, bool* eos) {
50+
if (!_is_init) {
51+
return Status::InternalError("Used before initialized.");
52+
}
53+
54+
if (nullptr == block || nullptr == eos) {
55+
return Status::InternalError("input pointer is nullptr.");
56+
}
57+
58+
*eos = true;
59+
return _fill_block_impl(block);
60+
}
61+
62+
Status SchemaFileCacheInfoScanner::_fill_block_impl(vectorized::Block* block) {
63+
SCOPED_TIMER(_fill_block_timer);
64+
65+
auto* file_cache_factory = ExecEnv::GetInstance()->file_cache_factory();
66+
if (!file_cache_factory) {
67+
return Status::OK();
68+
}
69+
70+
// Collect all cache entries from all file cache instances
71+
std::vector<std::tuple<std::string, int64_t, int64_t, int, std::string>> cache_entries;
72+
73+
// Get all cache instances using the public getter
74+
const auto& caches = file_cache_factory->get_caches();
75+
for (const auto& cache : caches) {
76+
const std::string& cache_path = cache->get_base_path();
77+
78+
// Get the storage from cache using the public getter
79+
auto* storage = cache->get_storage();
80+
if (!storage) {
81+
continue;
82+
}
83+
84+
// Try to get meta_store from FSFileCacheStorage using the public getter
85+
auto* fs_storage = dynamic_cast<doris::io::FSFileCacheStorage*>(storage);
86+
if (!fs_storage) {
87+
continue;
88+
}
89+
90+
auto* meta_store = fs_storage->get_meta_store();
91+
if (!meta_store) {
92+
continue;
93+
}
94+
95+
// Get iterator for all BlockMeta records
96+
auto iterator = meta_store->get_all();
97+
if (!iterator) {
98+
continue;
99+
}
100+
101+
// Iterate through all cache entries
102+
while (iterator->valid()) {
103+
const auto& key = iterator->key();
104+
const auto& value = iterator->value();
105+
106+
// Check for deserialization errors
107+
if (!iterator->get_last_key_error().ok() || !iterator->get_last_value_error().ok()) {
108+
LOG(WARNING) << "Failed to deserialize cache block metadata: "
109+
<< "key_error=" << iterator->get_last_key_error().to_string()
110+
<< ", value_error=" << iterator->get_last_value_error().to_string();
111+
iterator->next();
112+
continue; // Skip invalid records
113+
}
114+
115+
// Convert hash to string
116+
std::string hash_str = key.hash.to_string();
117+
118+
// Add to cache entries
119+
cache_entries.emplace_back(hash_str, key.tablet_id, value.size, value.type, cache_path);
120+
121+
iterator->next();
122+
}
123+
}
124+
125+
const size_t row_num = cache_entries.size();
126+
if (row_num == 0) {
127+
return Status::OK();
128+
}
129+
130+
for (size_t col_idx = 0; col_idx < _s_tbls_columns.size(); ++col_idx) {
131+
const auto& col_desc = _s_tbls_columns[col_idx];
132+
133+
std::vector<StringRef> str_refs(row_num);
134+
std::vector<int64_t> int64_vals(row_num);
135+
std::vector<void*> datas(row_num);
136+
std::vector<std::string> column_values(row_num);
137+
138+
for (size_t row_idx = 0; row_idx < row_num; ++row_idx) {
139+
const auto& entry = cache_entries[row_idx];
140+
const auto& [hash, tablet_id, size, type, cache_path] = entry;
141+
142+
if (col_desc.type == TYPE_STRING) {
143+
switch (col_idx) {
144+
case 0: // HASH
145+
column_values[row_idx] = hash;
146+
break;
147+
case 3: // TYPE
148+
column_values[row_idx] = doris::io::cache_type_to_string(
149+
static_cast<doris::io::FileCacheType>(type));
150+
break;
151+
case 4: // REMOTE_PATH
152+
column_values[row_idx] = ""; // TODO: Implement remote path retrieval
153+
break;
154+
case 5: // CACHE_PATH
155+
column_values[row_idx] = cache_path;
156+
break;
157+
default:
158+
column_values[row_idx] = "";
159+
break;
160+
}
161+
str_refs[row_idx] =
162+
StringRef(column_values[row_idx].data(), column_values[row_idx].size());
163+
datas[row_idx] = &str_refs[row_idx];
164+
} else if (col_desc.type == TYPE_BIGINT) {
165+
switch (col_idx) {
166+
case 1: // TABLET_ID
167+
int64_vals[row_idx] = tablet_id;
168+
break;
169+
case 2: // SIZE
170+
int64_vals[row_idx] = size;
171+
break;
172+
case 6: // BE_ID
173+
int64_vals[row_idx] = ExecEnv::GetInstance()->cluster_info()->backend_id;
174+
break;
175+
default:
176+
int64_vals[row_idx] = 0;
177+
break;
178+
}
179+
datas[row_idx] = &int64_vals[row_idx];
180+
}
181+
}
182+
183+
RETURN_IF_ERROR(fill_dest_column_for_range(block, col_idx, datas));
184+
}
185+
186+
return Status::OK();
187+
}
188+
189+
} // namespace doris
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
// Licensed to the Apache Software Foundation (ASF) under one
2+
// or more contributor license agreements. See the NOTICE file
3+
// distributed with this work for additional information
4+
// regarding copyright ownership. The ASF licenses this file
5+
// to you under the Apache License, Version 2.0 (the
6+
// "License"); you may not use this file except in compliance
7+
// with the License. You may obtain a copy of the License at
8+
//
9+
// http://www.apache.org/licenses/LICENSE-2.0
10+
//
11+
// Unless required by applicable law or agreed to in writing,
12+
// software distributed under the License is distributed on an
13+
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14+
// KIND, either express or implied. See the License for the
15+
// specific language governing permissions and limitations
16+
// under the License.
17+
18+
#pragma once
19+
20+
#include <vector>
21+
22+
#include "common/status.h"
23+
#include "exec/schema_scanner.h"
24+
25+
namespace doris {
26+
class RuntimeState;
27+
namespace vectorized {
28+
class Block;
29+
} // namespace vectorized
30+
31+
class SchemaFileCacheInfoScanner : public SchemaScanner {
32+
ENABLE_FACTORY_CREATOR(SchemaFileCacheInfoScanner);
33+
34+
public:
35+
SchemaFileCacheInfoScanner();
36+
~SchemaFileCacheInfoScanner() override;
37+
38+
Status start(RuntimeState* state) override;
39+
Status get_next_block_internal(vectorized::Block* block, bool* eos) override;
40+
41+
static std::vector<SchemaScanner::ColumnDesc> _s_tbls_columns;
42+
43+
private:
44+
Status _fill_block_impl(vectorized::Block* block);
45+
};
46+
47+
} // namespace doris

‎be/src/io/cache/block_file_cache.h‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -171,6 +171,9 @@ class BlockFileCache {
171171

172172
[[nodiscard]] const std::string& get_base_path() const { return _cache_base_path; }
173173

174+
// Get storage for inspection
175+
FileCacheStorage* get_storage() const { return _storage.get(); }
176+
174177
/**
175178
* Given an `offset` and `size` representing [offset, offset + size) bytes interval,
176179
* return list of cached non-overlapping non-empty

‎be/src/io/cache/block_file_cache_factory.h‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,9 @@ class FileCacheFactory {
102102

103103
void get_cache_stats_block(vectorized::Block* block);
104104

105+
// Get all cache instances for inspection
106+
const std::vector<std::unique_ptr<BlockFileCache>>& get_caches() const { return _caches; }
107+
105108
FileCacheFactory() = default;
106109
FileCacheFactory& operator=(const FileCacheFactory&) = delete;
107110
FileCacheFactory(const FileCacheFactory&) = delete;

‎fe/fe-core/src/main/java/org/apache/doris/analysis/SchemaTableType.java‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,8 @@ public enum SchemaTableType {
9898
TSchemaTableType.SCH_ROUTINE_LOAD_JOBS),
9999
SCH_LOAD_JOBS("LOAD_JOBS", "LOAD_JOBS",
100100
TSchemaTableType.SCH_LOAD_JOBS),
101+
SCH_FILE_CACHE_INFO("FILE_CACHE_INFO", "FILE_CACHE_INFO",
102+
TSchemaTableType.SCH_FILE_CACHE_INFO),
101103
SCH_VIEW_DEPENDENCY("VIEW_DEPENDENCY", "VIEW_DEPENDENCY",
102104
TSchemaTableType.SCH_VIEW_DEPENDENCY),
103105
SQL_BLOCK_RULE_STATUS("SQL_BLOCK_RULE_STATUS", "SQL_BLOCK_RULE_STATUS",

‎fe/fe-core/src/main/java/org/apache/doris/catalog/SchemaTable.java‎

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -702,6 +702,17 @@ public class SchemaTable extends Table {
702702
.column("FIRST_ERROR_MSG", ScalarType.createStringType())
703703
.build())
704704
)
705+
.put("file_cache_info",
706+
new SchemaTable(SystemIdGenerator.getNextId(), "file_cache_info", TableType.SCHEMA,
707+
builder().column("HASH", ScalarType.createStringType())
708+
.column("TABLET_ID", ScalarType.createType(PrimitiveType.BIGINT))
709+
.column("SIZE", ScalarType.createType(PrimitiveType.BIGINT))
710+
.column("TYPE", ScalarType.createStringType())
711+
.column("REMOTE_PATH", ScalarType.createStringType())
712+
.column("CACHE_PATH", ScalarType.createStringType())
713+
.column("BE_ID", ScalarType.createType(PrimitiveType.BIGINT))
714+
.build())
715+
)
705716
.put("backend_tablets", new SchemaTable(SystemIdGenerator.getNextId(), "backend_tablets", TableType.SCHEMA,
706717
builder().column("BE_ID", ScalarType.createType(PrimitiveType.BIGINT))
707718
.column("TABLET_ID", ScalarType.createType(PrimitiveType.BIGINT))

‎gensrc/thrift/Descriptors.thrift‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,7 @@ enum TSchemaTableType {
162162
SCH_BLACKHOLE = 62;
163163
SCH_COLUMN_DATA_SIZES = 63;
164164
SCH_LOAD_JOBS = 64;
165+
SCH_FILE_CACHE_INFO = 65;
165166
}
166167

167168
enum THdfsCompression {

0 commit comments

Comments
 (0)