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
0 commit comments