Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion google/cloud/storage/client.cc
Original file line number Diff line number Diff line change
Expand Up @@ -555,7 +555,8 @@ Options DefaultOptions(Options opts) {
STORAGE_CLIENT_DEFAULT_MAXIMUM_BACKOFF_DELAY,
STORAGE_CLIENT_DEFAULT_BACKOFF_SCALING)
.clone())
.set<IdempotencyPolicyOption>(AlwaysRetryIdempotencyPolicy().clone());
.set<IdempotencyPolicyOption>(AlwaysRetryIdempotencyPolicy().clone())
.set<storage_experimental::OTelSpanEnrichmentOption>(true);

o = google::cloud::internal::MergeOptions(std::move(opts), std::move(o));
// If the application did not set `DownloadStallTimeoutOption` then use the
Expand Down
2 changes: 2 additions & 0 deletions google/cloud/storage/google_cloud_cpp_storage.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ google_cloud_cpp_storage_hdrs = [
"internal/binary_data_as_debug_string.h",
"internal/bucket_access_control_parser.h",
"internal/bucket_acl_requests.h",
"internal/bucket_metadata_cache.h",
"internal/bucket_metadata_parser.h",
"internal/bucket_requests.h",
"internal/complex_option.h",
Expand Down Expand Up @@ -164,6 +165,7 @@ google_cloud_cpp_storage_srcs = [
"internal/base64.cc",
"internal/bucket_access_control_parser.cc",
"internal/bucket_acl_requests.cc",
"internal/bucket_metadata_cache.cc",
"internal/bucket_metadata_parser.cc",
"internal/bucket_requests.cc",
"internal/compute_engine_util.cc",
Expand Down
3 changes: 3 additions & 0 deletions google/cloud/storage/google_cloud_cpp_storage.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,8 @@ add_library(
internal/bucket_access_control_parser.h
internal/bucket_acl_requests.cc
internal/bucket_acl_requests.h
internal/bucket_metadata_cache.cc
internal/bucket_metadata_cache.h
internal/bucket_metadata_parser.cc
internal/bucket_metadata_parser.h
internal/bucket_requests.cc
Expand Down Expand Up @@ -420,6 +422,7 @@ if (BUILD_TESTING)
idempotency_policy_test.cc
internal/base64_test.cc
internal/bucket_acl_requests_test.cc
internal/bucket_metadata_cache_test.cc
internal/bucket_requests_test.cc
internal/complex_option_test.cc
internal/compute_engine_util_test.cc
Expand Down
14 changes: 13 additions & 1 deletion google/cloud/storage/grpc_plugin.cc
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,12 @@
#include "google/cloud/storage/internal/grpc/default_options.h"
#include "google/cloud/storage/internal/grpc/enable_metrics.h"
#include "google/cloud/storage/internal/grpc/stub.h"
#include "google/cloud/storage/internal/tracing_connection.h"
#include "google/cloud/background_threads.h"
#include "google/cloud/grpc_options.h"
#include "google/cloud/internal/getenv.h"
#include "google/cloud/internal/opentelemetry.h"
#include <functional>
#include <memory>
#include <utility>

Expand All @@ -31,8 +36,15 @@ google::cloud::storage::Client MakeGrpcClient(Options opts) {
opts = google::cloud::storage_internal::DefaultOptionsGrpc(std::move(opts));
storage_internal::EnableGrpcMetrics(opts);
auto stub = std::make_unique<storage_internal::GrpcStub>(opts);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

GrpcStub also calls MakeBackgroundThreadsFactory(opts)() internally, which means we are now creating two separate thread pools (and two separate completion queues) for every client. Is there a way to ensure the single pool is cleanly shared?

background_(MakeBackgroundThreadsFactory(options_)()),

storage_internal::TracingConnection::AsyncRunner runner;
if (google::cloud::internal::TracingEnabled(opts)) {
runner = [cq = stub->cq()](std::function<void()> f) mutable {
cq.RunAsync(std::move(f));
};
}
return storage::internal::ClientImplDetails::CreateWithoutDecorations(
MakeStorageConnection(std::move(opts), std::move(stub)));
MakeStorageConnection(std::move(opts), std::move(stub),
std::move(runner)));
}

GOOGLE_CLOUD_CPP_INLINE_NAMESPACE_END
Expand Down
19 changes: 19 additions & 0 deletions google/cloud/storage/grpc_plugin_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,10 @@

#include "google/cloud/storage/grpc_plugin.h"
#include "google/cloud/storage/options.h"
#include "google/cloud/background_threads.h"
#include "google/cloud/common_options.h"
#include "google/cloud/credentials.h"
#include "google/cloud/grpc_options.h"
#include "google/cloud/testing_util/scoped_environment.h"
#include <gmock/gmock.h>

Expand Down Expand Up @@ -97,6 +99,23 @@ TEST(GrpcPluginTest, GrpcMetricsExcludedLabelsOptionSingle) {
opts.get<storage_experimental::GrpcMetricsExcludedLabelsOption>());
}

TEST(GrpcPluginTest, GrpcBackgroundThreadsFactoryOptionWithTracing) {
struct Fake : google::cloud::BackgroundThreads {
google::cloud::CompletionQueue cq() const override { return {}; }
};
bool invoked = false;
auto factory = [&invoked] {
invoked = true;
return std::make_unique<Fake>();
};
auto logging = ScopedEnvironment("CLOUD_STORAGE_ENABLE_TRACING", "1");
auto client = MakeGrpcClient(
TestOptions()
.set<GrpcBackgroundThreadsFactoryOption>(factory)
.set<storage_experimental::OTelSpanEnrichmentOption>(true));
EXPECT_TRUE(invoked);
}

} // namespace
GOOGLE_CLOUD_CPP_INLINE_NAMESPACE_END
} // namespace storage
Expand Down
105 changes: 105 additions & 0 deletions google/cloud/storage/internal/bucket_metadata_cache.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
// Copyright 2026 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

#include "google/cloud/storage/internal/bucket_metadata_cache.h"
#include "google/cloud/storage/bucket_metadata.h"
#include <mutex>
#include <utility>

namespace google {
namespace cloud {
namespace storage_internal {
GOOGLE_CLOUD_CPP_INLINE_NAMESPACE_BEGIN

BucketCacheEntry BucketCacheEntry::FromMetadata(
storage::BucketMetadata const& m) {
std::string loc = m.location();
if (m.location_type() == "multi-region" ||
m.location_type() == "dual-region") {
loc = "global";
}
return {
"projects/" + std::to_string(m.project_number()) + "/buckets/" + m.name(),
std::move(loc)};
}

void BucketMetadataCache::MoveToFront(std::list<std::string>::iterator it) {
list_.splice(list_.begin(), list_, it);
}

absl::optional<BucketCacheEntry> BucketMetadataCache::Get(
std::string const& bucket_name) {
std::unique_lock<std::mutex> lk(mu_);
auto it = map_.find(bucket_name);
if (it == map_.end()) return absl::nullopt;

MoveToFront(it->second.second);
return it->second.first;
}

void BucketMetadataCache::Put(std::string const& bucket_name,
BucketCacheEntry entry) {
if (max_size_ == 0) return;
std::unique_lock<std::mutex> lk(mu_);
auto it = map_.find(bucket_name);
if (it != map_.end()) {
it->second.first = std::move(entry);
MoveToFront(it->second.second);
return;
}

if (map_.size() >= max_size_ && !list_.empty()) {
auto oldest = list_.back();
list_.pop_back();
map_.erase(oldest);
}

list_.push_front(bucket_name);
map_[bucket_name] = {std::move(entry), list_.begin()};
}

void BucketMetadataCache::Invalidate(std::string const& bucket_name) {
std::unique_lock<std::mutex> lk(mu_);
auto it = map_.find(bucket_name);
if (it != map_.end()) {
list_.erase(it->second.second);
map_.erase(it);
}
}

void BucketMetadataCache::Clear() {
std::unique_lock<std::mutex> lk(mu_);
map_.clear();
list_.clear();
in_flight_fetch_.clear();
}

bool BucketMetadataCache::StartFetch(std::string const& bucket_name) {
std::unique_lock<std::mutex> lk(mu_);
if (in_flight_fetch_.find(bucket_name) != in_flight_fetch_.end()) {
return false;
}
in_flight_fetch_.insert(bucket_name);
return true;
}

void BucketMetadataCache::EndFetch(std::string const& bucket_name) {
std::unique_lock<std::mutex> lk(mu_);
in_flight_fetch_.erase(bucket_name);
}

GOOGLE_CLOUD_CPP_INLINE_NAMESPACE_END
} // namespace storage_internal
} // namespace cloud
} // namespace google
74 changes: 74 additions & 0 deletions google/cloud/storage/internal/bucket_metadata_cache.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
// Copyright 2026 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

#ifndef GOOGLE_CLOUD_CPP_GOOGLE_CLOUD_STORAGE_INTERNAL_BUCKET_METADATA_CACHE_H
#define GOOGLE_CLOUD_CPP_GOOGLE_CLOUD_STORAGE_INTERNAL_BUCKET_METADATA_CACHE_H

#include "google/cloud/storage/version.h"
#include "absl/types/optional.h"
#include <cstddef>
#include <list>
#include <mutex>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <utility>

namespace google {
namespace cloud {
namespace storage {
GOOGLE_CLOUD_CPP_INLINE_NAMESPACE_BEGIN
class BucketMetadata;
GOOGLE_CLOUD_CPP_INLINE_NAMESPACE_END
} // namespace storage
namespace storage_internal {
GOOGLE_CLOUD_CPP_INLINE_NAMESPACE_BEGIN

struct BucketCacheEntry {
std::string id;
std::string location;

static BucketCacheEntry FromMetadata(storage::BucketMetadata const& m);
};

class BucketMetadataCache {
public:
explicit BucketMetadataCache(std::size_t max_size = 10000)
Comment thread
bajajneha27 marked this conversation as resolved.
: max_size_(max_size) {}

absl::optional<BucketCacheEntry> Get(std::string const& bucket_name);
void Put(std::string const& bucket_name, BucketCacheEntry entry);
void Invalidate(std::string const& bucket_name);
void Clear();
bool StartFetch(std::string const& bucket_name);
void EndFetch(std::string const& bucket_name);

private:
void MoveToFront(std::list<std::string>::iterator it);

std::size_t max_size_;
std::mutex mu_;
std::list<std::string> list_;
std::unordered_map<std::string, std::pair<BucketCacheEntry,
std::list<std::string>::iterator>>
map_;
std::unordered_set<std::string> in_flight_fetch_;
};

GOOGLE_CLOUD_CPP_INLINE_NAMESPACE_END
} // namespace storage_internal
} // namespace cloud
} // namespace google

#endif // GOOGLE_CLOUD_CPP_GOOGLE_CLOUD_STORAGE_INTERNAL_BUCKET_METADATA_CACHE_H
103 changes: 103 additions & 0 deletions google/cloud/storage/internal/bucket_metadata_cache_test.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
// Copyright 2026 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

#include "google/cloud/storage/internal/bucket_metadata_cache.h"
#include <gmock/gmock.h>

namespace google {
namespace cloud {
namespace storage_internal {
GOOGLE_CLOUD_CPP_INLINE_NAMESPACE_BEGIN
namespace {

using ::testing::Eq;
using ::testing::IsFalse;
using ::testing::IsTrue;

TEST(BucketMetadataCacheTest, HitAndMiss) {
BucketMetadataCache cache(10);
EXPECT_FALSE(cache.Get("test-bucket").has_value());

BucketCacheEntry entry{"projects/123/buckets/test-bucket", "us-central1"};
cache.Put("test-bucket", entry);

auto res = cache.Get("test-bucket");
ASSERT_TRUE(res.has_value());
EXPECT_THAT(res->id, Eq("projects/123/buckets/test-bucket"));
EXPECT_THAT(res->location, Eq("us-central1"));
}

TEST(BucketMetadataCacheTest, PutUpdatesExisting) {
BucketMetadataCache cache(10);
BucketCacheEntry entry1{"projects/123/buckets/test-bucket", "us-central1"};
cache.Put("test-bucket", entry1);

BucketCacheEntry entry2{"projects/456/buckets/test-bucket", "global"};
cache.Put("test-bucket", entry2);

auto res = cache.Get("test-bucket");
ASSERT_TRUE(res.has_value());
EXPECT_THAT(res->id, Eq("projects/456/buckets/test-bucket"));
EXPECT_THAT(res->location, Eq("global"));
}

TEST(BucketMetadataCacheTest, InvalidateAndClear) {
BucketMetadataCache cache(10);
BucketCacheEntry entry{"projects/123/buckets/test-bucket", "us-central1"};
cache.Put("test-bucket", entry);
EXPECT_TRUE(cache.Get("test-bucket").has_value());

cache.Invalidate("test-bucket");
EXPECT_FALSE(cache.Get("test-bucket").has_value());

cache.Put("bucket1", entry);
cache.Put("bucket2", entry);
EXPECT_TRUE(cache.Get("bucket1").has_value());
EXPECT_TRUE(cache.Get("bucket2").has_value());

cache.Clear();
EXPECT_FALSE(cache.Get("bucket1").has_value());
EXPECT_FALSE(cache.Get("bucket2").has_value());
}

TEST(BucketMetadataCacheTest, EvictsOldest) {
BucketMetadataCache cache(2);
BucketCacheEntry entry{"id", "loc"};

cache.Put("b1", entry);
cache.Put("b2", entry);
EXPECT_TRUE(cache.Get("b1").has_value()); // b1 becomes most recent

cache.Put("b3", entry); // pushes out b2 (oldest)
EXPECT_TRUE(cache.Get("b1").has_value());
EXPECT_FALSE(cache.Get("b2").has_value());
EXPECT_TRUE(cache.Get("b3").has_value());
}

TEST(BucketMetadataCacheTest, InFlightFetch) {
BucketMetadataCache cache(10);
EXPECT_THAT(cache.StartFetch("b1"), IsTrue());
EXPECT_THAT(cache.StartFetch("b1"), IsFalse());

EXPECT_THAT(cache.StartFetch("b2"), IsTrue());

cache.EndFetch("b1");
EXPECT_THAT(cache.StartFetch("b1"), IsTrue());
}

} // namespace
GOOGLE_CLOUD_CPP_INLINE_NAMESPACE_END
} // namespace storage_internal
} // namespace cloud
} // namespace google
Loading
Loading