Skip to content

Commit 361c294

Browse files
committed
update cache key to avoid searching for it again
1 parent 46c7e34 commit 361c294

File tree

2 files changed

+21
-17
lines changed

2 files changed

+21
-17
lines changed

be/src/service/staros_worker.cpp

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -212,6 +212,7 @@ absl::Status StarOSWorker::update_worker_info(const staros::starlet::WorkerInfo&
212212
absl::StatusOr<std::shared_ptr<fslib::FileSystem>> StarOSWorker::get_shard_filesystem(ShardId id,
213213
const Configuration& conf) {
214214
ShardInfo shard_info;
215+
std::shared_ptr<std::string> existing_fs_cache_key;
215216
{ // shared_lock, check if the filesystem already created
216217
std::shared_lock l(_mtx);
217218
auto it = _shards.find(id);
@@ -226,11 +227,12 @@ absl::StatusOr<std::shared_ptr<fslib::FileSystem>> StarOSWorker::get_shard_files
226227
return fs;
227228
}
228229
shard_info = it->second.shard_info;
230+
existing_fs_cache_key = it->second.fs_cache_key;
229231
}
230232

231233
// Build the filesystem under no lock, so the op won't hold the lock for a long time.
232234
// It is possible that multiple filesystems are built for the same shard from multiple threads under no lock here.
233-
auto fs_or = build_filesystem_from_shard_info(shard_info, conf);
235+
auto fs_or = build_filesystem_from_shard_info(shard_info, conf, existing_fs_cache_key);
234236
if (!fs_or.ok()) {
235237
return fs_or.status();
236238
}
@@ -282,7 +284,8 @@ absl::StatusOr<std::shared_ptr<fslib::FileSystem>> StarOSWorker::build_filesyste
282284
}
283285

284286
absl::StatusOr<std::pair<std::shared_ptr<std::string>, std::shared_ptr<fslib::FileSystem>>>
285-
StarOSWorker::build_filesystem_from_shard_info(const ShardInfo& info, const Configuration& conf) {
287+
StarOSWorker::build_filesystem_from_shard_info(const ShardInfo& info, const Configuration& conf,
288+
std::shared_ptr<std::string> existing_fs_cache_key) {
286289
auto localconf = build_conf_from_shard_info(info);
287290
if (!localconf.ok()) {
288291
return localconf.status();
@@ -292,7 +295,7 @@ StarOSWorker::build_filesystem_from_shard_info(const ShardInfo& info, const Conf
292295
return scheme.status();
293296
}
294297

295-
return new_shared_filesystem(info.id, *scheme, *localconf);
298+
return new_shared_filesystem(info.id, *scheme, *localconf, existing_fs_cache_key);
296299
}
297300

298301
bool StarOSWorker::need_enable_cache(const ShardInfo& info) {
@@ -334,7 +337,8 @@ absl::StatusOr<fslib::Configuration> StarOSWorker::build_conf_from_shard_info(co
334337
}
335338

336339
absl::StatusOr<std::pair<std::shared_ptr<std::string>, std::shared_ptr<fslib::FileSystem>>>
337-
StarOSWorker::new_shared_filesystem(ShardId shard_id, std::string_view scheme, const Configuration& conf) {
340+
StarOSWorker::new_shared_filesystem(ShardId shard_id, std::string_view scheme, const Configuration& conf,
341+
std::shared_ptr<std::string> existing_fs_cache_key) {
338342
std::string cache_key = get_cache_key(scheme, conf);
339343

340344
// Lookup LRU cache
@@ -361,7 +365,7 @@ StarOSWorker::new_shared_filesystem(ShardId shard_id, std::string_view scheme, c
361365
VLOG(9) << "Share filesystem";
362366
return value_or;
363367
}
364-
auto fs_cache_key = insert_fs_cache(shard_id, cache_key, fs);
368+
auto fs_cache_key = insert_fs_cache(cache_key, fs, existing_fs_cache_key);
365369

366370
return std::make_pair(std::move(fs_cache_key), std::move(fs));
367371
}
@@ -378,16 +382,14 @@ std::string StarOSWorker::get_cache_key(std::string_view scheme, const Configura
378382
return sha256.hex();
379383
}
380384

381-
std::shared_ptr<std::string> StarOSWorker::insert_fs_cache(ShardId shard_id, const std::string& key,
382-
const std::shared_ptr<FileSystem>& fs) {
385+
std::shared_ptr<std::string> StarOSWorker::insert_fs_cache(const std::string& key,
386+
const std::shared_ptr<FileSystem>& fs,
387+
std::shared_ptr<std::string> existing_fs_cache_key) {
383388
std::shared_ptr<std::string> fs_cache_key;
384389

385-
{
386-
std::shared_lock l(_mtx);
387-
auto it = _shards.find(shard_id);
388-
if (it != _shards.end() && it->second.fs_cache_key != nullptr && *it->second.fs_cache_key == key) {
389-
fs_cache_key = it->second.fs_cache_key;
390-
}
390+
// Reuse existing fs_cache_key if it matches the current key
391+
if (existing_fs_cache_key != nullptr && *existing_fs_cache_key == key) {
392+
fs_cache_key = existing_fs_cache_key;
391393
}
392394

393395
if (fs_cache_key == nullptr) {

be/src/service/staros_worker.h

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -123,13 +123,15 @@ class StarOSWorker : public staros::starlet::Worker {
123123

124124
absl::StatusOr<std::shared_ptr<FileSystem>> build_filesystem_on_demand(ShardId id, const Configuration& conf);
125125
absl::StatusOr<std::pair<std::shared_ptr<std::string>, std::shared_ptr<FileSystem>>>
126-
build_filesystem_from_shard_info(const ShardInfo& info, const Configuration& conf);
126+
build_filesystem_from_shard_info(const ShardInfo& info, const Configuration& conf,
127+
std::shared_ptr<std::string> existing_fs_cache_key = nullptr);
127128
absl::StatusOr<std::pair<std::shared_ptr<std::string>, std::shared_ptr<FileSystem>>> new_shared_filesystem(
128-
ShardId shard_id, std::string_view scheme, const Configuration& conf);
129+
ShardId shard_id, std::string_view scheme, const Configuration& conf,
130+
std::shared_ptr<std::string> existing_fs_cache_key = nullptr);
129131
absl::Status invalidate_fs(const ShardInfo& shard);
130132

131-
std::shared_ptr<std::string> insert_fs_cache(ShardId shard_id, const std::string& key,
132-
const std::shared_ptr<FileSystem>& fs);
133+
std::shared_ptr<std::string> insert_fs_cache(const std::string& key, const std::shared_ptr<FileSystem>& fs,
134+
std::shared_ptr<std::string> existing_fs_cache_key = nullptr);
133135
void erase_fs_cache(const std::string& key);
134136
std::shared_ptr<FileSystem> lookup_fs_cache(const std::string& key);
135137
std::shared_ptr<FileSystem> lookup_fs_cache(const std::shared_ptr<std::string>& key);

0 commit comments

Comments
 (0)