Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
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
5 changes: 3 additions & 2 deletions google/cloud/storage/client.cc
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
#include "google/cloud/storage/idempotency_policy.h"
#include "google/cloud/storage/internal/base64.h"
#include "google/cloud/storage/internal/connection_factory.h"
#include "google/cloud/storage/internal/feature_tracker.h"
#include "google/cloud/storage/options.h"
#include "google/cloud/internal/curl_handle.h"
#include "google/cloud/internal/curl_options.h"
Expand Down Expand Up @@ -624,8 +625,8 @@ Options DefaultOptions(Options opts) {
rest_defaults.set<rest::CAPathOption>(o.get<internal::CAPathOption>());
}

return google::cloud::internal::MergeOptions(std::move(o),
std::move(rest_defaults));
return internal::SetupFeatureTracker(google::cloud::internal::MergeOptions(
std::move(o), std::move(rest_defaults)));
}

Options DefaultOptionsWithCredentials(Options opts) {
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 @@ -61,6 +61,7 @@ google_cloud_cpp_storage_hdrs = [
"internal/curl/request_builder.h",
"internal/default_object_acl_requests.h",
"internal/empty_response.h",
"internal/feature_tracker.h",
"internal/generate_message_boundary.h",
"internal/generic_object_request.h",
"internal/generic_request.h",
Expand Down Expand Up @@ -173,6 +174,7 @@ google_cloud_cpp_storage_srcs = [
"internal/crc32c.cc",
"internal/default_object_acl_requests.cc",
"internal/empty_response.cc",
"internal/feature_tracker.cc",
"internal/generate_message_boundary.cc",
"internal/generic_stub_adapter.cc",
"internal/generic_stub_factory.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 @@ -95,6 +95,8 @@ add_library(
internal/default_object_acl_requests.h
internal/empty_response.cc
internal/empty_response.h
internal/feature_tracker.cc
internal/feature_tracker.h
internal/generate_message_boundary.cc
internal/generate_message_boundary.h
internal/generic_object_request.h
Expand Down Expand Up @@ -437,6 +439,7 @@ if (BUILD_TESTING)
internal/const_buffer_test.cc
internal/crc32c_test.cc
internal/default_object_acl_requests_test.cc
internal/feature_tracker_test.cc
internal/generate_message_boundary_test.cc
internal/generic_request_test.cc
internal/hash_function_impl_test.cc
Expand Down
4 changes: 3 additions & 1 deletion google/cloud/storage/internal/async/default_options.cc
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
#include "google/cloud/storage/async/resume_policy.h"
#include "google/cloud/storage/async/retry_policy.h"
#include "google/cloud/storage/async/writer_connection.h"
#include "google/cloud/storage/internal/feature_tracker.h"
#include "google/cloud/storage/internal/grpc/default_options.h"
#include <limits>

Expand Down Expand Up @@ -77,7 +78,8 @@ Options DefaultOptionsAsync(Options opts) {
.set<storage::EnableCrc32cValidationOption>(true)
.set<storage::MaximumRangeSizeOption>(128 * 1024 * 1024L)
.set<storage::EnableMultiStreamOptimizationOption>(true));
return Adjust(DefaultOptionsGrpc(std::move(opts)));
return storage::internal::SetupFeatureTracker(
Adjust(DefaultOptionsGrpc(std::move(opts))));
}

GOOGLE_CLOUD_CPP_INLINE_NAMESPACE_END
Expand Down
58 changes: 58 additions & 0 deletions google/cloud/storage/internal/feature_tracker.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
// 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/feature_tracker.h"
#include "google/cloud/storage/internal/base64.h"
#include "google/cloud/storage/options.h"
#include "google/cloud/options.h"
#include <memory>
#include <vector>

namespace google {
namespace cloud {
namespace storage {
GOOGLE_CLOUD_CPP_INLINE_NAMESPACE_BEGIN
namespace internal {

std::string EncodeFeatureTrackerBitmask(std::uint32_t mask) {
if (mask == 0) return {};

std::vector<std::uint8_t> bytes;
for (int i = 0; i < 4; ++i) {
auto b = static_cast<std::uint8_t>((mask >> (24 - i * 8)) & 0xFF);
if (!bytes.empty() || b != 0) {
bytes.push_back(b);
}
}
return Base64Encode(bytes);
}

Options SetupFeatureTracker(Options opts) {
if (opts.has<EnableFeatureReportsOption>() &&
!opts.get<EnableFeatureReportsOption>()) {
return std::move(opts);
}
if (opts.has<FeatureTrackerOption>()) return std::move(opts);

std::uint32_t mask = 0;
// Add checks for configuration-driven options here as they are introduced.
opts.set<FeatureTrackerOption>(std::make_shared<FeatureTracker>(mask));
return std::move(opts);
}

} // namespace internal
GOOGLE_CLOUD_CPP_INLINE_NAMESPACE_END
} // namespace storage
} // namespace cloud
} // namespace google
88 changes: 88 additions & 0 deletions google/cloud/storage/internal/feature_tracker.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
// 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_FEATURE_TRACKER_H
#define GOOGLE_CLOUD_CPP_GOOGLE_CLOUD_STORAGE_INTERNAL_FEATURE_TRACKER_H

#include "google/cloud/storage/version.h"
#include "google/cloud/options.h"
#include <atomic>
#include <cstdint>
#include <memory>
#include <string>

namespace google {
namespace cloud {
namespace storage {
GOOGLE_CLOUD_CPP_INLINE_NAMESPACE_BEGIN
namespace internal {

inline constexpr auto kFeatureTrackerHeaderName = "x-goog-storage-cpp-features";

// Tracked features represented as bit positions in a bitmask.
enum class TrackedFeature : std::uint32_t {
// Operation-Driven Optimizations
kMultiStreamInMRD = 0,
kPCU = 1,

// Configuration-Driven Options
kGrpcDirectPathEnforced = 2,
kJsonReads = 3,
};

// Converts a feature bitmask to a Base64-encoded string, stripping leading
// zero bytes to maintain compact representation.
std::string EncodeFeatureTrackerBitmask(std::uint32_t mask);

// A thread-safe state object that can be shared between operation connections
// (like ObjectDescriptorImpl) and RPC factories (like OpenStreamFactory)
// to track which features are adopted during an operation.
class FeatureTracker {
public:
FeatureTracker() = default;
explicit FeatureTracker(std::uint32_t initial) : mask_(initial) {}

void RegisterFeature(TrackedFeature feature) {
mask_.fetch_or(1U << static_cast<std::uint32_t>(feature),
std::memory_order_relaxed);
}

std::uint32_t GetMask() const {
return mask_.load(std::memory_order_relaxed);
}

std::string HeaderValue() const {
return EncodeFeatureTrackerBitmask(GetMask());
}

private:
std::atomic<std::uint32_t> mask_{0};
};

struct FeatureTrackerOption {
using Type = std::shared_ptr<FeatureTracker>;
};

// Evaluates client configuration options, creates a shared FeatureTracker
// initialized with configuration-driven feature flags (if any), and stores it
// into the Options list under FeatureTrackerOption.
Options SetupFeatureTracker(Options opts);

Comment on lines +80 to +81

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

medium

To adhere to the repository style guide regarding factoring out duplicated code (which appears 3 or more times in non-test files), we should introduce a helper function FeatureTrackerHeaderValue to centralize the logic of checking EnableFeatureReportsOption and retrieving the header value.

Options SetupFeatureTracker(Options opts);

std::string FeatureTrackerHeaderValue(Options const& opts);
References
  1. prefers to factor out duplicated code if it appears 3 or more times in non-test files. (link)

} // namespace internal
GOOGLE_CLOUD_CPP_INLINE_NAMESPACE_END
} // namespace storage
} // namespace cloud
} // namespace google

#endif // GOOGLE_CLOUD_CPP_GOOGLE_CLOUD_STORAGE_INTERNAL_FEATURE_TRACKER_H
16 changes: 16 additions & 0 deletions google/cloud/storage/internal/grpc/configure_client_context.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,9 @@
#ifndef GOOGLE_CLOUD_CPP_GOOGLE_CLOUD_STORAGE_INTERNAL_GRPC_CONFIGURE_CLIENT_CONTEXT_H
#define GOOGLE_CLOUD_CPP_GOOGLE_CLOUD_STORAGE_INTERNAL_GRPC_CONFIGURE_CLIENT_CONTEXT_H

#include "google/cloud/storage/internal/feature_tracker.h"
#include "google/cloud/storage/internal/generic_request.h"
#include "google/cloud/storage/options.h"
#include "google/cloud/storage/internal/object_requests.h"
#include "google/cloud/storage/version.h"
#include "google/cloud/grpc_options.h"
Expand Down Expand Up @@ -71,6 +73,20 @@ void ApplyQueryParameters(grpc::ClientContext& ctx, Options const& options,
ctx.AddMetadata("x-goog-fieldmask",
request.template GetOption<storage::Fields>().value());
}
bool const enable_reports =
!options.has<storage::EnableFeatureReportsOption>() ||
options.get<storage::EnableFeatureReportsOption>();
if (enable_reports &&
options.has<storage::internal::FeatureTrackerOption>()) {
auto const& tracker =
options.get<storage::internal::FeatureTrackerOption>();
if (tracker) {
auto const val = tracker->HeaderValue();
if (!val.empty()) {
ctx.AddMetadata(storage::internal::kFeatureTrackerHeaderName, val);
}
}
}
Comment on lines +76 to +89

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

medium

Use the centralized FeatureTrackerHeaderValue helper function to simplify the code and avoid duplication.

  auto const val = storage::internal::FeatureTrackerHeaderValue(options);
  if (!val.empty()) {
    ctx.AddMetadata(storage::internal::kFeatureTrackerHeaderName, val);
  }
References
  1. prefers to factor out duplicated code if it appears 3 or more times in non-test files. (link)

internal::ConfigureContext(ctx, options);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,21 @@ TEST_F(GrpcConfigureClientContext, ApplyQueryParametersGrpcOptions) {
storage::internal::ReadObjectRangeRequest("test-bucket", "test-object"));
}

TEST_F(GrpcConfigureClientContext, ApplyQueryParametersWithFeatureTracker) {
auto tracker = std::make_shared<storage::internal::FeatureTracker>();
tracker->RegisterFeature(storage::internal::TrackedFeature::kPCU);
auto const options =
Options{}.set<storage::internal::FeatureTrackerOption>(tracker);

grpc::ClientContext ctx;
ApplyQueryParameters(
ctx, options,
storage::internal::ReadObjectRangeRequest("test-bucket", "test-object"));
auto metadata = GetMetadata(ctx);
EXPECT_THAT(metadata, Contains(Pair(storage::internal::kFeatureTrackerHeaderName,
tracker->HeaderValue())));
}

TEST_F(GrpcConfigureClientContext, ApplyRoutingHeadersInsertObjectMedia) {
storage::internal::InsertObjectMediaRequest req("test-bucket", "test-object",
"content");
Expand Down
15 changes: 15 additions & 0 deletions google/cloud/storage/internal/rest/stub.cc
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@
#include "google/cloud/storage/internal/rest/stub.h"
#include "google/cloud/storage/internal/bucket_access_control_parser.h"
#include "google/cloud/storage/internal/bucket_metadata_parser.h"
#include "google/cloud/storage/internal/feature_tracker.h"
#include "google/cloud/storage/options.h"
#include "google/cloud/storage/internal/base64.h"
#include "google/cloud/storage/internal/bucket_requests.h"
#include "google/cloud/storage/internal/generate_message_boundary.h"
#include "google/cloud/storage/internal/hmac_key_metadata_parser.h"
Expand Down Expand Up @@ -116,6 +119,18 @@ void AddCustomHeaders(Options const& options, RestRequestBuilder& builder) {

Status AddHeaders(Options const& options, RestRequestBuilder& builder) {
AddCustomHeaders(options, builder);
bool const enable_reports =
!options.has<EnableFeatureReportsOption>() ||
options.get<EnableFeatureReportsOption>();
if (enable_reports && options.has<FeatureTrackerOption>()) {
auto const& tracker = options.get<FeatureTrackerOption>();
if (tracker) {
auto const val = tracker->HeaderValue();
if (!val.empty()) {
builder.AddHeader(kFeatureTrackerHeaderName, val);
}
}
}
Comment on lines +122 to +133

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

medium

Use the centralized FeatureTrackerHeaderValue helper function to simplify the code and avoid duplication.

  auto const val = FeatureTrackerHeaderValue(options);
  if (!val.empty()) {
    builder.AddHeader(kFeatureTrackerHeaderName, val);
  }
References
  1. prefers to factor out duplicated code if it appears 3 or more times in non-test files. (link)

return {};
}

Expand Down
22 changes: 22 additions & 0 deletions google/cloud/storage/internal/rest/stub_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
// limitations under the License.

#include "google/cloud/storage/internal/rest/stub.h"
#include "google/cloud/storage/internal/feature_tracker.h"
#include "google/cloud/storage/internal/hash_function.h"
#include "google/cloud/storage/testing/canonical_errors.h"
#include "google/cloud/internal/api_client_header.h"
Expand Down Expand Up @@ -1028,6 +1029,27 @@ TEST(RestStubTest, UploadChunkIntermediate) {
StatusIs(PermanentError().code(), PermanentError().message()));
}

TEST(RestStubTest, FeatureTrackerHeaderAppearsInRequest) {
auto tracker = std::make_shared<FeatureTracker>();
tracker->RegisterFeature(TrackedFeature::kPCU);
auto options = Options{}.set<FeatureTrackerOption>(tracker);

auto mock = std::make_shared<MockRestClient>();
EXPECT_CALL(
*mock,
Get(An<RestContext&>(),
ResultOf(
"request includes feature tracker header",
[](RestRequest const& r) { return r.headers(); },
Contains(Pair(kFeatureTrackerHeaderName,
ElementsAre(tracker->HeaderValue()))))))
.WillOnce(Return(PermanentError()));

auto tested = std::make_unique<RestStub>(options, mock, mock);
RestContext context;
tested->ListBuckets(context, options, ListBucketsRequest());
}

} // namespace
} // namespace internal
GOOGLE_CLOUD_CPP_INLINE_NAMESPACE_END
Expand Down
15 changes: 14 additions & 1 deletion google/cloud/storage/options.h
Original file line number Diff line number Diff line change
Expand Up @@ -317,14 +317,27 @@ struct IdempotencyPolicyOption {
using Type = std::shared_ptr<IdempotencyPolicy>;
};

/**
* Enable adoption reporting of client-side optimizations and configuration
* choices.
*
* When this option is enabled (the default), the GCS client sends a bitmask in
* the `x-goog-storage-cpp-features` header.
*
* @ingroup storage-options
*/
struct EnableFeatureReportsOption {
using Type = bool;
};

/// The complete list of options accepted by `storage::Client`.
using ClientOptionList = ::google::cloud::OptionList<
RestEndpointOption, IamEndpointOption, ProjectIdOption, ProjectIdOption,
ConnectionPoolSizeOption, DownloadBufferSizeOption, UploadBufferSizeOption,
EnableCurlSslLockingOption, EnableCurlSigpipeHandlerOption,
MaximumCurlSocketRecvSizeOption, MaximumCurlSocketSendSizeOption,
TransferStallTimeoutOption, RetryPolicyOption, BackoffPolicyOption,
IdempotencyPolicyOption, CARootsFilePathOption,
IdempotencyPolicyOption, CARootsFilePathOption, EnableFeatureReportsOption,
storage_experimental::HttpVersionOption>;

GOOGLE_CLOUD_CPP_INLINE_NAMESPACE_END
Expand Down
1 change: 1 addition & 0 deletions google/cloud/storage/storage_client_unit_tests.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ storage_client_unit_tests = [
"internal/const_buffer_test.cc",
"internal/crc32c_test.cc",
"internal/default_object_acl_requests_test.cc",
"internal/feature_tracker_test.cc",
"internal/generate_message_boundary_test.cc",
"internal/generic_request_test.cc",
"internal/hash_function_impl_test.cc",
Expand Down
Loading