Skip to content

Commit 3a7dd32

Browse files
committed
address review comments
1 parent 8f5deb3 commit 3a7dd32

4 files changed

Lines changed: 29 additions & 21 deletions

File tree

google/cloud/storage/internal/bucket_metadata_cache.h

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,19 @@ class BucketMetadataCache {
6666
std::unordered_set<std::string> in_flight_fetch_;
6767
};
6868

69+
class ScopedFetch {
70+
public:
71+
ScopedFetch(BucketMetadataCache* cache, std::string bucket_name)
72+
: cache_(cache), bucket_name_(std::move(bucket_name)) {}
73+
~ScopedFetch() {
74+
if (cache_) cache_->EndFetch(bucket_name_);
75+
}
76+
77+
private:
78+
BucketMetadataCache* cache_;
79+
std::string bucket_name_;
80+
};
81+
6982
GOOGLE_CLOUD_CPP_INLINE_NAMESPACE_END
7083
} // namespace storage_internal
7184
} // namespace cloud

google/cloud/storage/internal/tracing_connection.cc

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,9 @@ GOOGLE_CLOUD_CPP_INLINE_NAMESPACE_BEGIN
3030

3131
TracingConnection::TracingConnection(std::shared_ptr<StorageConnection> impl,
3232
AsyncRunner runner)
33-
: impl_(std::move(impl)), runner_(std::move(runner)) {}
33+
: impl_(std::move(impl)),
34+
runner_(std::move(runner)),
35+
cache_(std::make_shared<BucketMetadataCache>(10000)) {}
3436

3537
TracingConnection::~TracingConnection() = default;
3638

@@ -49,10 +51,7 @@ TracingConnection::AsyncRunner const& TracingConnection::runner() {
4951
return runner_;
5052
}
5153

52-
BucketMetadataCache& TracingConnection::cache() {
53-
static BucketMetadataCache instance(10000);
54-
return instance;
55-
}
54+
BucketMetadataCache& TracingConnection::cache() const { return *cache_; }
5655

5756
void TracingConnection::ResetCacheForTesting() { cache().Clear(); }
5857

@@ -71,18 +70,18 @@ void TracingConnection::MaybeTriggerBackgroundFetch(
7170
}
7271

7372
auto current_options = google::cloud::internal::SaveCurrentOptions();
74-
runner()([this, bucket_name, current_options]() {
73+
runner()([impl = impl_, cache = cache_, bucket_name, current_options]() {
74+
ScopedFetch guard(cache.get(), bucket_name);
75+
7576
google::cloud::internal::OptionsSpan span(current_options);
7677
storage::internal::GetBucketMetadataRequest request(bucket_name);
77-
auto result = impl_->GetBucketMetadata(request);
78+
auto result = impl->GetBucketMetadata(request);
7879

7980
if (result.ok()) {
80-
cache().Put(bucket_name, BucketCacheEntry::FromMetadata(*result));
81+
cache->Put(bucket_name, BucketCacheEntry::FromMetadata(*result));
8182
} else if (result.status().code() == StatusCode::kPermissionDenied) {
82-
cache().Put(bucket_name, {"projects/_/buckets/" + bucket_name, "global"});
83+
cache->Put(bucket_name, {"projects/_/buckets/" + bucket_name, "global"});
8384
}
84-
85-
cache().EndFetch(bucket_name);
8685
});
8786
}
8887

google/cloud/storage/internal/tracing_connection.h

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ class TracingConnection : public storage::internal::StorageConnection {
3636
AsyncRunner runner = {});
3737
~TracingConnection() override;
3838

39-
static void ResetCacheForTesting();
39+
void ResetCacheForTesting();
4040

4141
Options options() const override;
4242

@@ -192,25 +192,25 @@ class TracingConnection : public storage::internal::StorageConnection {
192192
BucketCacheEntry const& entry);
193193
void MaybeTriggerBackgroundFetch(std::string const& bucket_name);
194194

195-
static void MaybeInvalidate(Status const& status,
196-
std::string const& bucket_name) {
195+
void MaybeInvalidate(Status const& status, std::string const& bucket_name) {
197196
if (!status.ok() && status.code() == StatusCode::kNotFound) {
198197
cache().Invalidate(bucket_name);
199198
}
200199
}
201200

202201
template <typename T>
203-
static void MaybeInvalidate(StatusOr<T> const& result,
204-
std::string const& bucket_name) {
202+
void MaybeInvalidate(StatusOr<T> const& result,
203+
std::string const& bucket_name) {
205204
MaybeInvalidate(result.status(), bucket_name);
206205
}
207206

208-
static BucketMetadataCache& cache();
207+
BucketMetadataCache& cache() const;
209208

210209
AsyncRunner const& runner();
211210

212211
std::shared_ptr<StorageConnection> impl_;
213212
AsyncRunner runner_;
213+
std::shared_ptr<BucketMetadataCache> cache_;
214214
absl::once_flag once_flag_;
215215
};
216216

google/cloud/storage/internal/tracing_connection_test.cc

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,6 @@ TEST(TracingClientTest, Options) {
6565
}
6666

6767
TEST(TracingClientTest, CustomAsyncRunner) {
68-
TracingConnection::ResetCacheForTesting();
6968
bool invoked = false;
7069
auto runner = [&invoked](std::function<void()> const& f) {
7170
invoked = true;
@@ -217,7 +216,6 @@ TEST(TracingClientTest, GetBucketMetadataSuccess) {
217216
}
218217

219218
TEST(TracingClientTest, BucketMetadataCacheSuccess) {
220-
TracingConnection::ResetCacheForTesting();
221219
auto span_catcher = InstallSpanCatcher();
222220
auto mock = std::make_shared<MockClient>();
223221

@@ -1713,7 +1711,6 @@ TEST(TracingClientTest, DeleteNotification) {
17131711
}
17141712

17151713
TEST(TracingClientTest, BucketMetadataMaybeInvalidateBucketLevelEvict) {
1716-
TracingConnection::ResetCacheForTesting();
17171714
auto mock = std::make_shared<MockClient>();
17181715

17191716
EXPECT_CALL(*mock, options)
@@ -1760,7 +1757,6 @@ TEST(TracingClientTest, BucketMetadataMaybeInvalidateBucketLevelEvict) {
17601757
}
17611758

17621759
TEST(TracingClientTest, BucketMetadataMaybeInvalidateObjectLevelNoEvict) {
1763-
TracingConnection::ResetCacheForTesting();
17641760
auto mock = std::make_shared<MockClient>();
17651761

17661762
EXPECT_CALL(*mock, options)

0 commit comments

Comments
 (0)