Skip to content

Commit

Permalink
[Enhancement] Add tablet_metadata_exists to avoid not found exception (
Browse files Browse the repository at this point in the history
…#51122)

Signed-off-by: xiangguangyxg <[email protected]>
  • Loading branch information
xiangguangyxg authored Sep 24, 2024
1 parent e8afab3 commit 801b191
Show file tree
Hide file tree
Showing 6 changed files with 35 additions and 1 deletion.
4 changes: 4 additions & 0 deletions be/src/storage/lake/tablet.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,10 @@ Status Tablet::delete_metadata(int64_t version) {
return _mgr->delete_tablet_metadata(_id, version);
}

Status Tablet::metadata_exists(int64_t version) {
return _mgr->tablet_metadata_exists(_id, version);
}

Status Tablet::put_txn_log(const TxnLog& log) {
return _mgr->put_txn_log(log);
}
Expand Down
2 changes: 2 additions & 0 deletions be/src/storage/lake/tablet.h
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,8 @@ class Tablet : public BaseTablet {

Status delete_metadata(int64_t version);

Status metadata_exists(int64_t version);

Status put_txn_log(const TxnLog& log);

Status put_txn_log(const TxnLogPtr& log);
Expand Down
20 changes: 20 additions & 0 deletions be/src/storage/lake/tablet_manager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -280,6 +280,26 @@ Status TabletManager::delete_tablet_metadata(int64_t tablet_id, int64_t version)
return fs::delete_file(location);
}

Status TabletManager::tablet_metadata_exists(int64_t tablet_id, int64_t version) {
if (version <= kInitialVersion) {
// Handle tablet initial metadata
auto status = tablet_metadata_exists(tablet_initial_metadata_location(tablet_id));
if (status.ok()) {
return status;
}
}
return tablet_metadata_exists(tablet_metadata_location(tablet_id, version));
}

Status TabletManager::tablet_metadata_exists(const std::string& path) {
if (auto ptr = _metacache->lookup_tablet_metadata(path); ptr != nullptr) {
TRACE("got cached tablet metadata");
return Status::OK();
}
ASSIGN_OR_RETURN(auto fs, FileSystem::CreateSharedFromString(path));
return fs->path_exists(path);
}

StatusOr<TabletMetadataIter> TabletManager::list_tablet_metadata(int64_t tablet_id) {
std::vector<std::string> objects{};
// TODO: construct prefix in LocationProvider
Expand Down
6 changes: 6 additions & 0 deletions be/src/storage/lake/tablet_manager.h
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,12 @@ class TabletManager {

Status delete_tablet_metadata(int64_t tablet_id, int64_t version);

// Use this function instead of get_tablet_metadata where you just need to check if tablet metadata exists
Status tablet_metadata_exists(int64_t tablet_id, int64_t version);

// Do not use this function except in a list dir
Status tablet_metadata_exists(const std::string& path);

Status put_txn_log(const TxnLog& log);

Status put_txn_log(const TxnLogPtr& log);
Expand Down
2 changes: 1 addition & 1 deletion be/src/storage/lake/transactions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ int64_t cal_new_base_version(int64_t tablet_id, TabletManager* tablet_mgr, int64
if (index_version > version) {
// There is a possibility that the index version is newer than the version in remote storage.
// Check whether the index version exists in remote storage. If not, clear and rebuild the index.
auto res = tablet_mgr->get_tablet_metadata(tablet_id, index_version);
auto res = tablet_mgr->tablet_metadata_exists(tablet_id, index_version);
if (res.ok()) {
version = index_version;
} else {
Expand Down
2 changes: 2 additions & 0 deletions be/test/storage/lake/tablet_manager_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -87,11 +87,13 @@ TEST_F(LakeTabletManagerTest, tablet_meta_write_and_read) {
rowset_meta_pb->set_data_size(1024);
rowset_meta_pb->set_num_rows(5);
EXPECT_OK(_tablet_manager->put_tablet_metadata(metadata));
EXPECT_OK(_tablet_manager->tablet_metadata_exists(12345, 2));
auto res = _tablet_manager->get_tablet_metadata(12345, 2);
EXPECT_TRUE(res.ok());
EXPECT_EQ(res.value()->id(), 12345);
EXPECT_EQ(res.value()->version(), 2);
EXPECT_OK(_tablet_manager->delete_tablet_metadata(12345, 2));
EXPECT_STATUS(Status::NotFound(""), _tablet_manager->tablet_metadata_exists(12345, 2));
res = _tablet_manager->get_tablet_metadata(12345, 2);
EXPECT_TRUE(res.status().is_not_found());
}
Expand Down

0 comments on commit 801b191

Please sign in to comment.