Skip to content

Commit d51a156

Browse files
branch-4.0: [fix](file cache) Fix inverted index cache not cleaned on compaction failure #58141 (#58165)
Cherry-picked from #58141 Co-authored-by: zzzxl <yangsiyu@selectdb.com>
1 parent 0cf45d1 commit d51a156

File tree

4 files changed

+71
-0
lines changed

4 files changed

+71
-0
lines changed

be/src/olap/compaction.cpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1643,6 +1643,13 @@ Status CloudCompactionMixin::garbage_collection() {
16431643
auto* file_cache = io::FileCacheFactory::instance()->get_by_path(file_key);
16441644
file_cache->remove_if_cached_async(file_key);
16451645
}
1646+
for (const auto& [_, index_writer] : beta_rowset_writer->index_file_writers()) {
1647+
for (const auto& file_name : index_writer->get_index_file_names()) {
1648+
auto file_key = io::BlockFileCache::hash(file_name);
1649+
auto* file_cache = io::FileCacheFactory::instance()->get_by_path(file_key);
1650+
file_cache->remove_if_cached_async(file_key);
1651+
}
1652+
}
16461653
}
16471654
return Status::OK();
16481655
}

be/src/olap/rowset/segment_v2/index_file_writer.cpp

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -226,6 +226,20 @@ Status IndexFileWriter::close() {
226226
return Status::OK();
227227
}
228228

229+
std::vector<std::string> IndexFileWriter::get_index_file_names() const {
230+
std::vector<std::string> file_names;
231+
if (_storage_format == InvertedIndexStorageFormatPB::V2) {
232+
file_names.emplace_back(
233+
InvertedIndexDescriptor::get_index_file_name_v2(_rowset_id, _seg_id));
234+
} else {
235+
for (const auto& [index_info, _] : _indices_dirs) {
236+
file_names.emplace_back(InvertedIndexDescriptor::get_index_file_name_v1(
237+
_rowset_id, _seg_id, index_info.first, index_info.second));
238+
}
239+
}
240+
return file_names;
241+
}
242+
229243
std::string IndexFileWriter::debug_string() const {
230244
std::stringstream indices_dirs;
231245
for (const auto& [index, dir] : _indices_dirs) {

be/src/olap/rowset/segment_v2/index_file_writer.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@ class IndexFileWriter {
6969
const io::FileSystemSPtr& get_fs() const { return _fs; }
7070
InvertedIndexStorageFormatPB get_storage_format() const { return _storage_format; }
7171
void set_file_writer_opts(const io::FileWriterOptions& opts) { _opts = opts; }
72+
std::vector<std::string> get_index_file_names() const;
7273
std::string debug_string() const;
7374

7475
private:

be/test/olap/rowset/segment_v2/inverted_index_file_writer_test.cpp

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1658,6 +1658,55 @@ TEST_F(IndexFileWriterTest, DeleteIndexNullMetaTest) {
16581658
ASSERT_TRUE(status.msg().find("Index metadata is null") != std::string::npos);
16591659
}
16601660

1661+
TEST_F(IndexFileWriterTest, GetIndexFileNamesTest) {
1662+
// Test V2 format
1663+
{
1664+
IndexFileWriter writer(_fs, _index_path_prefix, _rowset_id, _seg_id,
1665+
InvertedIndexStorageFormatPB::V2);
1666+
std::vector<std::string> file_names = writer.get_index_file_names();
1667+
ASSERT_EQ(file_names.size(), 1);
1668+
EXPECT_EQ(file_names[0],
1669+
InvertedIndexDescriptor::get_index_file_name_v2(_rowset_id, _seg_id));
1670+
}
1671+
1672+
// Test V1 format
1673+
{
1674+
IndexFileWriter writer(_fs, _index_path_prefix, _rowset_id, _seg_id,
1675+
InvertedIndexStorageFormatPB::V1);
1676+
1677+
// Insert some directories
1678+
int64_t index_id_1 = 1;
1679+
std::string suffix_1 = "suffix1";
1680+
EXPECT_TRUE(writer._insert_directory_into_map(index_id_1, suffix_1,
1681+
std::make_shared<DorisFSDirectory>())
1682+
.ok());
1683+
1684+
int64_t index_id_2 = 2;
1685+
std::string suffix_2 = "suffix2";
1686+
EXPECT_TRUE(writer._insert_directory_into_map(index_id_2, suffix_2,
1687+
std::make_shared<DorisFSDirectory>())
1688+
.ok());
1689+
1690+
std::vector<std::string> file_names = writer.get_index_file_names();
1691+
ASSERT_EQ(file_names.size(), 2);
1692+
1693+
std::string expected_name_1 = InvertedIndexDescriptor::get_index_file_name_v1(
1694+
_rowset_id, _seg_id, index_id_1, suffix_1);
1695+
std::string expected_name_2 = InvertedIndexDescriptor::get_index_file_name_v1(
1696+
_rowset_id, _seg_id, index_id_2, suffix_2);
1697+
1698+
bool found_1 = false;
1699+
bool found_2 = false;
1700+
for (const auto& name : file_names) {
1701+
if (name == expected_name_1) found_1 = true;
1702+
if (name == expected_name_2) found_2 = true;
1703+
}
1704+
1705+
EXPECT_TRUE(found_1);
1706+
EXPECT_TRUE(found_2);
1707+
}
1708+
}
1709+
16611710
// Test for add_into_searcher_cache with StreamSinkFileWriter nullptr check
16621711
TEST_F(IndexFileWriterTest, AddIntoSearcherCacheStreamSinkNullTest) {
16631712
IndexFileWriter writer(_fs, _index_path_prefix, _rowset_id, _seg_id,

0 commit comments

Comments
 (0)