-
Notifications
You must be signed in to change notification settings - Fork 105
chore: introduce PeerInfo & MetadataExtractor #2788
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
igorbernstein2
wants to merge
13
commits into
main
Choose a base branch
from
peer-info
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.
+328
−397
Open
Changes from all commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
89703d5
chore: introduce PeerInfo & MetadataExtractor
igorbernstein2 06f15f7
format
igorbernstein2 9b5dd31
oops
igorbernstein2 729f9be
format
igorbernstein2 8a35ede
remove replaced location & gfe methods
igorbernstein2 8318285
Merge branch 'main' into peer-info
igorbernstein2 a1cc2f8
add todo
igorbernstein2 0255504
fix null handling of sideband data formating and remove stale code
igorbernstein2 2a95e0d
Merge branch 'main' into peer-info
igorbernstein2 eca71a8
todo
igorbernstein2 0a353dc
remove stale dep
igorbernstein2 9f1da68
Eagerly set sideband data instead of deferring until onClose
igorbernstein2 3137aed
Merge branch 'main' into peer-info
igorbernstein2 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
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
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
198 changes: 198 additions & 0 deletions
198
...le/src/main/java/com/google/cloud/bigtable/data/v2/stub/MetadataExtractorInterceptor.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,198 @@ | ||
| /* | ||
| * 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. | ||
| */ | ||
| package com.google.cloud.bigtable.data.v2.stub; | ||
|
|
||
| import com.google.api.core.InternalApi; | ||
| import com.google.api.gax.grpc.GrpcCallContext; | ||
| import com.google.bigtable.v2.PeerInfo; | ||
| import com.google.bigtable.v2.ResponseParams; | ||
| import com.google.common.base.Strings; | ||
| import com.google.protobuf.InvalidProtocolBufferException; | ||
| import io.grpc.Attributes; | ||
| import io.grpc.CallOptions; | ||
| import io.grpc.Channel; | ||
| import io.grpc.ClientCall; | ||
| import io.grpc.ClientInterceptor; | ||
| import io.grpc.ClientInterceptors; | ||
| import io.grpc.ForwardingClientCall; | ||
| import io.grpc.ForwardingClientCallListener; | ||
| import io.grpc.Metadata; | ||
| import io.grpc.MethodDescriptor; | ||
| import io.grpc.Status; | ||
| import io.grpc.alts.AltsContextUtil; | ||
| import java.util.Base64; | ||
| import java.util.regex.Matcher; | ||
| import java.util.regex.Pattern; | ||
| import javax.annotation.Nullable; | ||
|
|
||
| @InternalApi | ||
| public class MetadataExtractorInterceptor implements ClientInterceptor { | ||
| private final SidebandData sidebandData = new SidebandData(); | ||
|
|
||
| public GrpcCallContext injectInto(GrpcCallContext ctx) { | ||
| // TODO: migrate to using .withTransportChannel | ||
| // This will require a change on gax's side to expose the underlying ManagedChannel in | ||
| // GrpcTransportChannel (its currently package private). | ||
| return ctx.withChannel(ClientInterceptors.intercept(ctx.getChannel(), this)) | ||
| .withCallOptions(ctx.getCallOptions().withOption(SidebandData.KEY, sidebandData)); | ||
| } | ||
|
|
||
| @Override | ||
| public <ReqT, RespT> ClientCall<ReqT, RespT> interceptCall( | ||
| MethodDescriptor<ReqT, RespT> methodDescriptor, CallOptions callOptions, Channel channel) { | ||
| return new ForwardingClientCall.SimpleForwardingClientCall<ReqT, RespT>( | ||
| channel.newCall(methodDescriptor, callOptions)) { | ||
| @Override | ||
| public void start(Listener<RespT> responseListener, Metadata headers) { | ||
| sidebandData.reset(); | ||
|
|
||
| super.start( | ||
| new ForwardingClientCallListener.SimpleForwardingClientCallListener<RespT>( | ||
| responseListener) { | ||
| @Override | ||
| public void onHeaders(Metadata headers) { | ||
| sidebandData.onResponseHeaders(headers, getAttributes()); | ||
igorbernstein2 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| super.onHeaders(headers); | ||
| } | ||
|
|
||
| @Override | ||
| public void onClose(Status status, Metadata trailers) { | ||
| sidebandData.onClose(status, trailers); | ||
| super.onClose(status, trailers); | ||
| } | ||
| }, | ||
| headers); | ||
| } | ||
| }; | ||
| } | ||
|
|
||
| public SidebandData getSidebandData() { | ||
| return sidebandData; | ||
| } | ||
|
|
||
| public static class SidebandData { | ||
| private static final CallOptions.Key<SidebandData> KEY = | ||
| CallOptions.Key.create("bigtable-sideband"); | ||
|
|
||
| private static final Metadata.Key<String> SERVER_TIMING_HEADER_KEY = | ||
| Metadata.Key.of("server-timing", Metadata.ASCII_STRING_MARSHALLER); | ||
| private static final Pattern SERVER_TIMING_HEADER_PATTERN = | ||
| Pattern.compile(".*dur=(?<dur>\\d+)"); | ||
| private static final Metadata.Key<byte[]> LOCATION_METADATA_KEY = | ||
| Metadata.Key.of("x-goog-ext-425905942-bin", Metadata.BINARY_BYTE_MARSHALLER); | ||
| private static final Metadata.Key<String> PEER_INFO_KEY = | ||
| Metadata.Key.of("bigtable-peer-info", Metadata.ASCII_STRING_MARSHALLER); | ||
|
|
||
| @Nullable private volatile ResponseParams responseParams; | ||
| @Nullable private volatile PeerInfo peerInfo; | ||
| @Nullable private volatile Long gfeTiming; | ||
|
|
||
| @Nullable | ||
| public ResponseParams getResponseParams() { | ||
| return responseParams; | ||
| } | ||
|
|
||
| @Nullable | ||
| public PeerInfo getPeerInfo() { | ||
| return peerInfo; | ||
| } | ||
|
|
||
| @Nullable | ||
| public Long getGfeTiming() { | ||
| return gfeTiming; | ||
| } | ||
|
|
||
| private void reset() { | ||
| responseParams = null; | ||
| peerInfo = null; | ||
| gfeTiming = null; | ||
| } | ||
|
|
||
| void onResponseHeaders(Metadata md, Attributes attributes) { | ||
| responseParams = extractResponseParams(md); | ||
| gfeTiming = extractGfeLatency(md); | ||
| peerInfo = extractPeerInfo(md, gfeTiming, attributes); | ||
| } | ||
|
|
||
| void onClose(Status status, Metadata trailers) { | ||
| if (responseParams == null) { | ||
| responseParams = extractResponseParams(trailers); | ||
| } | ||
| } | ||
|
|
||
| @Nullable | ||
| private static Long extractGfeLatency(Metadata metadata) { | ||
| String serverTiming = metadata.get(SERVER_TIMING_HEADER_KEY); | ||
| if (serverTiming == null) { | ||
| return null; | ||
| } | ||
| Matcher matcher = SERVER_TIMING_HEADER_PATTERN.matcher(serverTiming); | ||
| // this should always be true | ||
| if (matcher.find()) { | ||
| return Long.parseLong(matcher.group("dur")); | ||
| } | ||
| return null; | ||
| } | ||
|
|
||
| @Nullable | ||
| private static PeerInfo extractPeerInfo( | ||
| Metadata metadata, Long gfeTiming, Attributes attributes) { | ||
| String encodedStr = metadata.get(PEER_INFO_KEY); | ||
| if (Strings.isNullOrEmpty(encodedStr)) { | ||
| return null; | ||
| } | ||
|
|
||
| try { | ||
| byte[] decoded = Base64.getUrlDecoder().decode(encodedStr); | ||
| PeerInfo peerInfo = PeerInfo.parseFrom(decoded); | ||
| PeerInfo.TransportType effectiveTransport = peerInfo.getTransportType(); | ||
|
|
||
| // TODO: remove this once transport_type is being sent by the server | ||
| // This is a temporary workaround to detect directpath until its available from the server | ||
| if (effectiveTransport == PeerInfo.TransportType.TRANSPORT_TYPE_UNKNOWN) { | ||
| boolean isAlts = AltsContextUtil.check(attributes); | ||
igorbernstein2 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| if (isAlts) { | ||
| effectiveTransport = PeerInfo.TransportType.TRANSPORT_TYPE_DIRECT_ACCESS; | ||
| } else if (gfeTiming != null) { | ||
| effectiveTransport = PeerInfo.TransportType.TRANSPORT_TYPE_CLOUD_PATH; | ||
| } | ||
| } | ||
| if (effectiveTransport != PeerInfo.TransportType.TRANSPORT_TYPE_UNKNOWN) { | ||
| peerInfo = peerInfo.toBuilder().setTransportType(effectiveTransport).build(); | ||
| } | ||
| return peerInfo; | ||
| } catch (Exception e) { | ||
| throw new IllegalArgumentException( | ||
| "Failed to parse " | ||
| + PEER_INFO_KEY.name() | ||
| + " from the response header value: " | ||
| + encodedStr); | ||
| } | ||
| } | ||
|
|
||
| @Nullable | ||
| private static ResponseParams extractResponseParams(Metadata metadata) { | ||
| byte[] responseParams = metadata.get(LOCATION_METADATA_KEY); | ||
| if (responseParams != null) { | ||
| try { | ||
| return ResponseParams.parseFrom(responseParams); | ||
| } catch (InvalidProtocolBufferException e) { | ||
| } | ||
| } | ||
| return null; | ||
| } | ||
| } | ||
| } | ||
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
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.
Uh oh!
There was an error while loading. Please reload this page.