Skip to content

Commit 1b194b1

Browse files
committed
fix filesystem cache failture
Signed-off-by: edwinhzhang <[email protected]>
1 parent 7f9233b commit 1b194b1

File tree

2 files changed

+28
-15
lines changed

2 files changed

+28
-15
lines changed

be/src/service/staros_worker.cpp

Lines changed: 24 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -230,7 +230,7 @@ absl::StatusOr<std::shared_ptr<fslib::FileSystem>> StarOSWorker::get_shard_files
230230

231231
// Build the filesystem under no lock, so the op won't hold the lock for a long time.
232232
// 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);
233+
auto fs_or = build_filesystem_from_shard_info(id, shard_info, conf);
234234
if (!fs_or.ok()) {
235235
return fs_or.status();
236236
}
@@ -272,7 +272,7 @@ absl::StatusOr<std::shared_ptr<fslib::FileSystem>> StarOSWorker::build_filesyste
272272
if (!info_or.ok()) {
273273
return info_or.status();
274274
}
275-
auto fs_or = build_filesystem_from_shard_info(info_or.value(), conf);
275+
auto fs_or = build_filesystem_from_shard_info(id, info_or.value(), conf);
276276
if (!fs_or.ok()) {
277277
return fs_or.status();
278278
}
@@ -282,7 +282,7 @@ absl::StatusOr<std::shared_ptr<fslib::FileSystem>> StarOSWorker::build_filesyste
282282
}
283283

284284
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) {
285+
StarOSWorker::build_filesystem_from_shard_info(ShardId shard_id, const ShardInfo& info, const Configuration& conf) {
286286
auto localconf = build_conf_from_shard_info(info);
287287
if (!localconf.ok()) {
288288
return localconf.status();
@@ -292,7 +292,7 @@ StarOSWorker::build_filesystem_from_shard_info(const ShardInfo& info, const Conf
292292
return scheme.status();
293293
}
294294

295-
return new_shared_filesystem(*scheme, *localconf);
295+
return new_shared_filesystem(shard_id, *scheme, *localconf);
296296
}
297297

298298
bool StarOSWorker::need_enable_cache(const ShardInfo& info) {
@@ -334,7 +334,7 @@ absl::StatusOr<fslib::Configuration> StarOSWorker::build_conf_from_shard_info(co
334334
}
335335

336336
absl::StatusOr<std::pair<std::shared_ptr<std::string>, std::shared_ptr<fslib::FileSystem>>>
337-
StarOSWorker::new_shared_filesystem(std::string_view scheme, const Configuration& conf) {
337+
StarOSWorker::new_shared_filesystem(ShardId shard_id, std::string_view scheme, const Configuration& conf) {
338338
std::string cache_key = get_cache_key(scheme, conf);
339339

340340
// Lookup LRU cache
@@ -361,7 +361,7 @@ StarOSWorker::new_shared_filesystem(std::string_view scheme, const Configuration
361361
VLOG(9) << "Share filesystem";
362362
return value_or;
363363
}
364-
auto fs_cache_key = insert_fs_cache(cache_key, fs);
364+
auto fs_cache_key = insert_fs_cache(shard_id, cache_key, fs);
365365

366366
return std::make_pair(std::move(fs_cache_key), std::move(fs));
367367
}
@@ -378,14 +378,26 @@ std::string StarOSWorker::get_cache_key(std::string_view scheme, const Configura
378378
return sha256.hex();
379379
}
380380

381-
std::shared_ptr<std::string> StarOSWorker::insert_fs_cache(const std::string& key,
381+
std::shared_ptr<std::string> StarOSWorker::insert_fs_cache(ShardId shard_id, const std::string& key,
382382
const std::shared_ptr<FileSystem>& fs) {
383-
std::shared_ptr<std::string> fs_cache_key(new std::string(key), [](std::string* key) {
384-
if (g_worker) {
385-
g_worker->erase_fs_cache(*key);
383+
std::shared_ptr<std::string> fs_cache_key;
384+
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;
386390
}
387-
delete key;
388-
});
391+
}
392+
393+
if (fs_cache_key == nullptr) {
394+
fs_cache_key = std::shared_ptr<std::string>(new std::string(key), [](std::string* key) {
395+
if (g_worker) {
396+
g_worker->erase_fs_cache(*key);
397+
}
398+
delete key;
399+
});
400+
}
389401

390402
CacheKey cache_key(key);
391403
auto value = new CacheValue(fs_cache_key, fs);

be/src/service/staros_worker.h

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -123,12 +123,13 @@ 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(ShardId shard_id, const ShardInfo& info, const Configuration& conf);
127127
absl::StatusOr<std::pair<std::shared_ptr<std::string>, std::shared_ptr<FileSystem>>> new_shared_filesystem(
128-
std::string_view scheme, const Configuration& conf);
128+
ShardId shard_id, std::string_view scheme, const Configuration& conf);
129129
absl::Status invalidate_fs(const ShardInfo& shard);
130130

131-
std::shared_ptr<std::string> insert_fs_cache(const std::string& key, const std::shared_ptr<FileSystem>& fs);
131+
std::shared_ptr<std::string> insert_fs_cache(ShardId shard_id, const std::string& key,
132+
const std::shared_ptr<FileSystem>& fs);
132133
void erase_fs_cache(const std::string& key);
133134
std::shared_ptr<FileSystem> lookup_fs_cache(const std::string& key);
134135
std::shared_ptr<FileSystem> lookup_fs_cache(const std::shared_ptr<std::string>& key);

0 commit comments

Comments
 (0)