Repository navigation
feat(storage): add client feature tracking header #14764
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
cpriti-os
wants to merge
4
commits into
googleapis:main
Choose a base branch
from
cpriti-os:feature-tracking-header
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+826
−13
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
50ffe26
feat(storage): add client feature tracking header
cpriti-os 01e52f5
fix(storage): tighten FeatureTracking visibility and readability
cpriti-os 09d5004
test(storage): broaden feature tracking header coverage
cpriti-os e05316c
refactor(storage): rename TrackedFeature and drop test-only accessor
cpriti-os File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
86 changes: 86 additions & 0 deletions
86
...-storage/google-cloud-storage/src/main/java/com/google/cloud/storage/FeatureTracking.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,86 @@ | ||
| /* | ||
| * 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 | ||
| * | ||
| * http://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. | ||
| */ | ||
|
|
||
| package com.google.cloud.storage; | ||
|
|
||
| import com.google.common.annotations.VisibleForTesting; | ||
| import com.google.common.collect.ImmutableList; | ||
| import com.google.common.collect.ImmutableMap; | ||
| import com.google.common.primitives.Ints; | ||
| import java.util.Base64; | ||
| import java.util.List; | ||
| import java.util.concurrent.atomic.AtomicInteger; | ||
| import javax.annotation.concurrent.ThreadSafe; | ||
|
|
||
| /** | ||
| * Utilities for reporting {@link TrackedFeature client feature usage} to the service. | ||
| * | ||
| * <p>Encoding contract (shared across GCS SDKs): | ||
| * | ||
| * <ul> | ||
| * <li>The set of used features is a 32-bit mask, the bitwise OR of {@link | ||
| * TrackedFeature#bitMask()} for every feature used. | ||
| * <li>The mask is serialized as 4 bytes big-endian, then encoded with URL-safe base64 (RFC 4648 | ||
| * §5) <em>with</em> padding. The service rejects {@code +} and {@code /}. | ||
| * <li>When the mask is {@code 0} the header is omitted entirely. | ||
| * </ul> | ||
| */ | ||
| final class FeatureTracking { | ||
|
|
||
| /** Name of the request header / gRPC metadata key carrying the encoded feature mask. */ | ||
| static final String HEADER = "x-goog-storage-java-features"; | ||
|
|
||
| private static final Base64.Encoder ENCODER = Base64.getUrlEncoder(); | ||
|
|
||
| private FeatureTracking() {} | ||
|
|
||
| /** Encodes {@code mask} as a 4 byte big-endian uint32, URL-safe base64 with padding. */ | ||
| @VisibleForTesting | ||
| static String encode(int mask) { | ||
| return ENCODER.encodeToString(Ints.toByteArray(mask)); | ||
| } | ||
|
|
||
| /** | ||
| * Returns the header map to merge into a request's extra headers: an empty map when {@code mask | ||
| * == 0}, otherwise a single entry map for {@link #HEADER}. | ||
| */ | ||
| private static ImmutableMap<String, List<String>> headers(int mask) { | ||
| if (mask == 0) { | ||
| return ImmutableMap.of(); | ||
| } | ||
| return ImmutableMap.of(HEADER, ImmutableList.of(encode(mask))); | ||
| } | ||
|
|
||
| /** Monotonically growing feature bitmask. Bits can be set but never cleared. */ | ||
| @ThreadSafe | ||
| static final class Mask { | ||
| private final AtomicInteger bits = new AtomicInteger(); | ||
|
|
||
| void set(TrackedFeature feature) { | ||
| bits.accumulateAndGet(feature.bitMask(), (a, b) -> a | b); | ||
| } | ||
|
|
||
| @VisibleForTesting | ||
| int get() { | ||
| return bits.get(); | ||
| } | ||
|
|
||
| /** Returns the headers to attach to a request, see {@link FeatureTracking#headers(int)}. */ | ||
| ImmutableMap<String, List<String>> headers() { | ||
| return FeatureTracking.headers(bits.get()); | ||
| } | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
45 changes: 45 additions & 0 deletions
45
java-storage/google-cloud-storage/src/main/java/com/google/cloud/storage/TrackedFeature.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,45 @@ | ||
| /* | ||
| * 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 | ||
| * | ||
| * http://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. | ||
| */ | ||
|
|
||
| package com.google.cloud.storage; | ||
|
|
||
| /** | ||
| * A client feature whose usage is reported to the service via the {@link FeatureTracking#HEADER | ||
| * x-goog-storage-java-features} request header. | ||
| * | ||
| * <p>Each feature is assigned a stable bit position in a 32-bit mask. Bit positions are specific to | ||
| * the Java SDK but MUST NOT be renumbered or reused once released. This enum is the single source | ||
| * of truth and its constants are parsed by external tooling; the {@code NAME(bit)} shape of each | ||
| * constant must stay intact. | ||
| */ | ||
| enum TrackedFeature { | ||
| /** | ||
| * A {@link BlobReadSession} opened more than one BidiReadObject stream to serve concurrent reads | ||
| * which could not share a single stream. | ||
| */ | ||
| MULTISTREAM_IN_MRD(0); | ||
|
|
||
| private final int bitPosition; | ||
|
|
||
| TrackedFeature(int bitPosition) { | ||
| this.bitPosition = bitPosition; | ||
| } | ||
|
|
||
| /** Returns the 32-bit mask with only this feature's bit set. */ | ||
| int bitMask() { | ||
| return 1 << bitPosition; | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We can optimize the encoding process by avoiding the allocation of a
ByteBufferobject. Since we are dealing with a 32-bit integer, we can directly extract the bytes using bitwise shifts.