Visitar URL original
fix(bigtable): copy Metadata per GetClientConfiguration call (#14950) · googleapis/google-cloud-java@278fe2f · GitHub
Skip to content

Commit 278fe2f

Browse files
authored
fix(bigtable): copy Metadata per GetClientConfiguration call (#14950)
### Problem `ClientConfigurationManager` constructs a single `io.grpc.Metadata` instance in its constructor and passes that same mutable reference to `ClientCall.start(listener, metadata)` on every `GetClientConfiguration` poll and retry. In `grpc-java`, `ClientCall.start()` takes ownership of the `Metadata` instance, and channel `ClientInterceptor`s wrap `realChannel` above `RetriableStream` and mutate `headers` in place before any defensive copy is made. Whenever a channel `ClientInterceptor` (for example `MetadataUtils.newAttachHeadersInterceptor` or a tracing interceptor calling `headers.put(...)` in `start()`) appends per-RPC headers without `discardAll`, duplicate header entries accumulate on `ClientConfigurationManager.metadata` every 5 minutes until outbound header encoding exceeds the server's HTTP/2 `SETTINGS_MAX_HEADER_LIST_SIZE` (`64 KiB`), causing Netty's `HpackEncoder` to fail every subsequent poll locally with `Status.INTERNAL: http2 exception` (`HeaderListSizeException`) — surviving `replaceChannel()` until JVM restart. ### Fix - Copy `this.metadata` into a fresh `Metadata` instance per `ClientCall` in `ClientConfigurationManager.sendRequest()`, matching `gax-grpc`'s `GrpcClientCalls`, `BigtableChannelPrimer`, and `SessionPoolImpl`. ### Testing - Added `metadataDoesNotAccumulateAcrossPollsWithHeaderAttachingInterceptor` verifying 26 consecutive polls against a server advertising `maxInboundMetadataSize(1024)` with a header-appending `ClientInterceptor` succeed without header accumulation (`1` copy of each header per poll on the wire).
1 parent 2947dfd commit 278fe2f

2 files changed

Lines changed: 110 additions & 1 deletion

File tree

‎java-bigtable/google-cloud-bigtable/src/main/java/com/google/cloud/bigtable/data/v2/internal/util/ClientConfigurationManager.java‎

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,9 @@ public void close() {
151151
}
152152
}
153153

154+
// Base metadata template copied into a fresh per-call Metadata in sendRequest().
155+
// ClientCall.start() takes ownership of the Metadata passed to it and channel
156+
// ClientInterceptors may mutate it in place, so this template must never be passed directly.
154157
private final Metadata metadata;
155158
private final GetClientConfigurationRequest request;
156159
private final ChannelProvider channelProvider;
@@ -392,6 +395,9 @@ private void sendRequestWithRetries(
392395
private CompletableFuture<ClientConfiguration> sendRequest() {
393396
CompletableFuture<ClientConfiguration> future = new CompletableFuture<>();
394397

398+
Metadata headers = new Metadata();
399+
headers.merge(this.metadata);
400+
395401
ClientCall<GetClientConfigurationRequest, ClientConfiguration> call =
396402
channel.newCall(
397403
BigtableGrpc.getGetClientConfigurationMethod(),
@@ -423,7 +429,7 @@ public void onClose(Status status, Metadata trailers) {
423429
}
424430
}
425431
},
426-
metadata);
432+
headers);
427433
call.sendMessage(request);
428434
call.halfClose();
429435
call.request(1);

‎java-bigtable/google-cloud-bigtable/src/test/java/com/google/cloud/bigtable/data/v2/internal/util/ClientConfigurationManagerTest.java‎

Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@
4444
import com.google.cloud.bigtable.data.v2.internal.csm.NoopMetrics;
4545
import com.google.cloud.bigtable.data.v2.internal.csm.attributes.ClientInfo;
4646
import com.google.cloud.bigtable.data.v2.internal.util.ClientConfigurationManager.ConfigListener;
47+
import com.google.common.collect.Iterables;
4748
import com.google.protobuf.TextFormat;
4849
import com.google.protobuf.util.Durations;
4950
import io.grpc.CallOptions;
@@ -56,10 +57,16 @@
5657
import io.grpc.Metadata;
5758
import io.grpc.MethodDescriptor;
5859
import io.grpc.Server;
60+
import io.grpc.ServerBuilder;
61+
import io.grpc.ServerCall;
62+
import io.grpc.ServerCallHandler;
63+
import io.grpc.ServerInterceptor;
5964
import io.grpc.Status;
6065
import io.grpc.stub.StreamObserver;
6166
import java.io.IOException;
67+
import java.util.List;
6268
import java.util.Properties;
69+
import java.util.concurrent.CopyOnWriteArrayList;
6370
import java.util.concurrent.CountDownLatch;
6471
import java.util.concurrent.ExecutionException;
6572
import java.util.concurrent.ScheduledExecutorService;
@@ -688,12 +695,107 @@ void stopPollingTest() throws Exception {
688695
verify(mockExecutor, never()).schedule(any(Runnable.class), anyLong(), any(TimeUnit.class));
689696
}
690697

698+
@Test
699+
void metadataDoesNotAccumulateAcrossPollsWithHeaderAttachingInterceptor() throws Exception {
700+
manager.close();
701+
702+
Metadata.Key<String> traceparentKey =
703+
Metadata.Key.of("traceparent", Metadata.ASCII_STRING_MARSHALLER);
704+
Metadata.Key<String> requestParamsKey =
705+
Metadata.Key.of("x-goog-request-params", Metadata.ASCII_STRING_MARSHALLER);
706+
Metadata.Key<String> featureFlagsKey =
707+
Metadata.Key.of("bigtable-features", Metadata.ASCII_STRING_MARSHALLER);
708+
709+
List<Integer> traceparentCountsOnServer = new CopyOnWriteArrayList<>();
710+
List<Integer> requestParamsCountsOnServer = new CopyOnWriteArrayList<>();
711+
List<Integer> featureFlagsCountsOnServer = new CopyOnWriteArrayList<>();
712+
713+
ServerInterceptor headerCapturingServerInterceptor =
714+
new ServerInterceptor() {
715+
@Override
716+
public <ReqT, RespT> ServerCall.Listener<ReqT> interceptCall(
717+
ServerCall<ReqT, RespT> call, Metadata headers, ServerCallHandler<ReqT, RespT> next) {
718+
traceparentCountsOnServer.add(Iterables.size(headers.getAll(traceparentKey)));
719+
requestParamsCountsOnServer.add(Iterables.size(headers.getAll(requestParamsKey)));
720+
featureFlagsCountsOnServer.add(Iterables.size(headers.getAll(featureFlagsKey)));
721+
return next.startCall(call, headers);
722+
}
723+
};
724+
725+
// Enforce a tight 1 KiB SETTINGS_MAX_HEADER_LIST_SIZE so any per-poll header accumulation in
726+
// ClientConfigurationManager.metadata trips Netty's HpackEncoder HeaderListSizeException
727+
// (Status.INTERNAL: "http2 exception") within ~8 polls.
728+
Server boundedHeaderServer =
729+
ServerBuilder.forPort(0)
730+
.addService(service)
731+
.intercept(headerCapturingServerInterceptor)
732+
.maxInboundMetadataSize(1024)
733+
.build()
734+
.start();
735+
736+
ClientInterceptor tracingInterceptor =
737+
new ClientInterceptor() {
738+
@Override
739+
public <ReqT, RespT> ClientCall<ReqT, RespT> interceptCall(
740+
MethodDescriptor<ReqT, RespT> method, CallOptions callOptions, Channel next) {
741+
return new ForwardingClientCall.SimpleForwardingClientCall<ReqT, RespT>(
742+
next.newCall(method, callOptions)) {
743+
@Override
744+
public void start(Listener<RespT> responseListener, Metadata headers) {
745+
headers.put(
746+
traceparentKey, "00-4bf92f3577b34da6a3ce929d0e0e4736-00f067aa0ba902b7-01");
747+
super.start(responseListener, headers);
748+
}
749+
};
750+
}
751+
};
752+
753+
ChannelProviders.ChannelProvider tracingChannelProvider =
754+
new ForwardingChannelProvider(
755+
new ChannelProviders.EmulatorChannelProvider(
756+
"localhost", boundedHeaderServer.getPort())) {
757+
@Override
758+
public ManagedChannelBuilder<?> newChannelBuilder() {
759+
return super.newChannelBuilder()
760+
.intercept(outstandingRpcCounter)
761+
.intercept(tracingInterceptor);
762+
}
763+
};
764+
765+
try (ClientConfigurationManager localManager =
766+
new ClientConfigurationManager(
767+
FEATURE_FLAGS, CLIENT_INFO, tracingChannelProvider, noopDebugTracer, mockExecutor)) {
768+
localManager.start().get();
769+
770+
ArgumentCaptor<Runnable> pollCaptor = ArgumentCaptor.forClass(Runnable.class);
771+
verify(mockExecutor, Mockito.atLeastOnce())
772+
.schedule(pollCaptor.capture(), anyLong(), any(TimeUnit.class));
773+
Runnable scheduledPoll = pollCaptor.getValue();
774+
775+
for (int i = 0; i < 25; i++) {
776+
scheduledPoll.run();
777+
outstandingRpcCounter.waitUntilRpcsDone();
778+
}
779+
780+
assertThat(service.requestCount.get()).isEqualTo(26);
781+
assertThat(traceparentCountsOnServer).hasSize(26);
782+
for (int i = 0; i < 26; i++) {
783+
assertThat(traceparentCountsOnServer.get(i)).isEqualTo(1);
784+
assertThat(requestParamsCountsOnServer.get(i)).isEqualTo(1);
785+
assertThat(featureFlagsCountsOnServer.get(i)).isEqualTo(1);
786+
}
787+
} finally {
788+
boundedHeaderServer.shutdownNow();
789+
}
790+
}
791+
691792
static class FakeConfigService extends BigtableGrpc.BigtableImplBase {
692793
private final AtomicReference<ClientConfiguration> config = new AtomicReference<>();
693794

694795
// Number of leading requests to answer with a clean (OK) close that delivers no message,
695796
// simulating the server closing the stream without ever returning a configuration.
696797
private final AtomicInteger emptyResponses = new AtomicInteger(0);
798+
private final AtomicInteger requestCount = new AtomicInteger(0);
697799

698800
public FakeConfigService() throws IOException {
699801
ClientConfiguration.Builder builder = ClientConfigurationManager.loadDefault().toBuilder();
@@ -716,6 +818,7 @@ public FakeConfigService() throws IOException {
716818
public void getClientConfiguration(
717819
GetClientConfigurationRequest request,
718820
StreamObserver<ClientConfiguration> responseObserver) {
821+
requestCount.incrementAndGet();
719822
if (emptyResponses.getAndDecrement() > 0) {
720823
// Close cleanly without sending a message.
721824
responseObserver.onCompleted();

0 commit comments

Comments
 (0)