Skip to content

Commit f8ba6fe

Browse files
authoredSep 13, 2023
fix: set wait timeout on watchdog (#1913)
And add wait timeout on CDC settings. Copy of https://github.com/googleapis/java-bigtable/pull/1898 with lower cdc timeouts
1 parent 8843813 commit f8ba6fe

File tree

3 files changed

+83
-6
lines changed

3 files changed

+83
-6
lines changed
 

‎google-cloud-bigtable/src/main/java/com/google/cloud/bigtable/data/v2/stub/EnhancedBigtableStub.java

+4
Original file line numberDiff line numberDiff line change
@@ -458,6 +458,7 @@ public Map<String, String> extract(ReadRowsRequest readRowsRequest) {
458458
.setRetryableCodes(readRowsSettings.getRetryableCodes())
459459
.setRetrySettings(readRowsSettings.getRetrySettings())
460460
.setIdleTimeout(readRowsSettings.getIdleTimeout())
461+
.setWaitTimeout(readRowsSettings.getWaitTimeout())
461462
.build();
462463

463464
ServerStreamingCallable<ReadRowsRequest, RowT> watched =
@@ -906,6 +907,8 @@ public Map<String, String> extract(
906907
settings.generateInitialChangeStreamPartitionsSettings().getRetrySettings())
907908
.setIdleTimeout(
908909
settings.generateInitialChangeStreamPartitionsSettings().getIdleTimeout())
910+
.setWaitTimeout(
911+
settings.generateInitialChangeStreamPartitionsSettings().getWaitTimeout())
909912
.build();
910913

911914
ServerStreamingCallable<String, ByteStringRange> watched =
@@ -980,6 +983,7 @@ public Map<String, String> extract(
980983
.setRetryableCodes(settings.readChangeStreamSettings().getRetryableCodes())
981984
.setRetrySettings(settings.readChangeStreamSettings().getRetrySettings())
982985
.setIdleTimeout(settings.readChangeStreamSettings().getIdleTimeout())
986+
.setWaitTimeout(settings.readChangeStreamSettings().getWaitTimeout())
983987
.build();
984988

985989
ServerStreamingCallable<ReadChangeStreamRequest, ChangeStreamRecordT> watched =

‎google-cloud-bigtable/src/main/java/com/google/cloud/bigtable/data/v2/stub/EnhancedBigtableStubSettings.java

+4-2
Original file line numberDiff line numberDiff line change
@@ -714,13 +714,15 @@ private Builder() {
714714
generateInitialChangeStreamPartitionsSettings
715715
.setRetryableCodes(GENERATE_INITIAL_CHANGE_STREAM_PARTITIONS_RETRY_CODES)
716716
.setRetrySettings(GENERATE_INITIAL_CHANGE_STREAM_PARTITIONS_RETRY_SETTINGS)
717-
.setIdleTimeout(Duration.ofMinutes(5));
717+
.setIdleTimeout(Duration.ofMinutes(5))
718+
.setWaitTimeout(Duration.ofMinutes(1));
718719

719720
readChangeStreamSettings = ServerStreamingCallSettings.newBuilder();
720721
readChangeStreamSettings
721722
.setRetryableCodes(READ_CHANGE_STREAM_RETRY_CODES)
722723
.setRetrySettings(READ_CHANGE_STREAM_RETRY_SETTINGS)
723-
.setIdleTimeout(Duration.ofMinutes(5));
724+
.setIdleTimeout(Duration.ofMinutes(5))
725+
.setWaitTimeout(Duration.ofMinutes(1));
724726

725727
pingAndWarmSettings = UnaryCallSettings.newUnaryCallSettingsBuilder();
726728
pingAndWarmSettings.setRetrySettings(

‎google-cloud-bigtable/src/test/java/com/google/cloud/bigtable/data/v2/stub/EnhancedBigtableStubTest.java

+75-4
Original file line numberDiff line numberDiff line change
@@ -30,14 +30,18 @@
3030
import com.google.api.gax.grpc.GrpcCallContext;
3131
import com.google.api.gax.grpc.GrpcTransportChannel;
3232
import com.google.api.gax.rpc.FixedTransportChannelProvider;
33+
import com.google.api.gax.rpc.InstantiatingWatchdogProvider;
3334
import com.google.api.gax.rpc.ServerStreamingCallable;
35+
import com.google.api.gax.rpc.WatchdogTimeoutException;
3436
import com.google.auth.oauth2.ServiceAccountJwtAccessCredentials;
3537
import com.google.bigtable.v2.BigtableGrpc;
3638
import com.google.bigtable.v2.FeatureFlags;
3739
import com.google.bigtable.v2.MutateRowsRequest;
3840
import com.google.bigtable.v2.MutateRowsResponse;
3941
import com.google.bigtable.v2.PingAndWarmRequest;
4042
import com.google.bigtable.v2.PingAndWarmResponse;
43+
import com.google.bigtable.v2.ReadChangeStreamRequest;
44+
import com.google.bigtable.v2.ReadChangeStreamResponse;
4145
import com.google.bigtable.v2.ReadRowsRequest;
4246
import com.google.bigtable.v2.ReadRowsResponse;
4347
import com.google.bigtable.v2.RowSet;
@@ -46,11 +50,8 @@
4650
import com.google.cloud.bigtable.data.v2.BigtableDataSettings;
4751
import com.google.cloud.bigtable.data.v2.FakeServiceBuilder;
4852
import com.google.cloud.bigtable.data.v2.internal.RequestContext;
49-
import com.google.cloud.bigtable.data.v2.models.BulkMutation;
50-
import com.google.cloud.bigtable.data.v2.models.DefaultRowAdapter;
51-
import com.google.cloud.bigtable.data.v2.models.Query;
53+
import com.google.cloud.bigtable.data.v2.models.*;
5254
import com.google.cloud.bigtable.data.v2.models.Row;
53-
import com.google.cloud.bigtable.data.v2.models.RowMutationEntry;
5455
import com.google.common.collect.ImmutableMap;
5556
import com.google.common.collect.Queues;
5657
import com.google.common.io.BaseEncoding;
@@ -82,11 +83,13 @@
8283
import java.security.NoSuchAlgorithmException;
8384
import java.util.Base64;
8485
import java.util.Collection;
86+
import java.util.Iterator;
8587
import java.util.concurrent.ArrayBlockingQueue;
8688
import java.util.concurrent.BlockingQueue;
8789
import java.util.concurrent.ExecutionException;
8890
import java.util.concurrent.TimeUnit;
8991
import org.junit.After;
92+
import org.junit.Assert;
9093
import org.junit.Before;
9194
import org.junit.Test;
9295
import org.junit.runner.RunWith;
@@ -101,6 +104,8 @@ public class EnhancedBigtableStubTest {
101104
private static final String TABLE_NAME =
102105
NameUtil.formatTableName(PROJECT_ID, INSTANCE_ID, "fake-table");
103106
private static final String APP_PROFILE_ID = "app-profile-id";
107+
private static final String WAIT_TIME_TABLE_ID = "test-wait-timeout";
108+
private static final Duration WATCHDOG_CHECK_DURATION = Duration.ofMillis(100);
104109

105110
private Server server;
106111
private MetadataInterceptor metadataInterceptor;
@@ -544,6 +549,46 @@ public void testBulkMutationFlowControlFeatureFlagIsNotSet() throws Exception {
544549
assertThat(featureFlags.getMutateRowsRateLimit()).isFalse();
545550
}
546551

552+
@Test
553+
public void testWaitTimeoutIsSet() throws Exception {
554+
EnhancedBigtableStubSettings.Builder settings = defaultSettings.toBuilder();
555+
// Set a shorter wait timeout and make watchdog checks more frequently
556+
settings.readRowsSettings().setWaitTimeout(WATCHDOG_CHECK_DURATION.dividedBy(2));
557+
settings.setStreamWatchdogProvider(
558+
InstantiatingWatchdogProvider.create().withCheckInterval(WATCHDOG_CHECK_DURATION));
559+
560+
EnhancedBigtableStub stub = EnhancedBigtableStub.create(settings.build());
561+
Iterator<Row> iterator =
562+
stub.readRowsCallable().call(Query.create(WAIT_TIME_TABLE_ID)).iterator();
563+
try {
564+
iterator.next();
565+
Assert.fail("Should throw watchdog timeout exception");
566+
} catch (WatchdogTimeoutException e) {
567+
assertThat(e.getMessage()).contains("Canceled due to timeout waiting for next response");
568+
}
569+
}
570+
571+
@Test
572+
public void testReadChangeStreamWaitTimeoutIsSet() throws Exception {
573+
EnhancedBigtableStubSettings.Builder settings = defaultSettings.toBuilder();
574+
// Set a shorter wait timeout and make watchdog checks more frequently
575+
settings.readChangeStreamSettings().setWaitTimeout(WATCHDOG_CHECK_DURATION.dividedBy(2));
576+
settings.setStreamWatchdogProvider(
577+
InstantiatingWatchdogProvider.create().withCheckInterval(WATCHDOG_CHECK_DURATION));
578+
579+
EnhancedBigtableStub stub = EnhancedBigtableStub.create(settings.build());
580+
Iterator<ChangeStreamRecord> iterator =
581+
stub.readChangeStreamCallable()
582+
.call(ReadChangeStreamQuery.create(WAIT_TIME_TABLE_ID))
583+
.iterator();
584+
try {
585+
iterator.next();
586+
Assert.fail("Should throw watchdog timeout exception");
587+
} catch (WatchdogTimeoutException e) {
588+
assertThat(e.getMessage()).contains("Canceled due to timeout waiting for next response");
589+
}
590+
}
591+
547592
private static class MetadataInterceptor implements ServerInterceptor {
548593
final BlockingQueue<Metadata> headers = Queues.newLinkedBlockingDeque();
549594

@@ -572,6 +617,8 @@ public <ReqT, RespT> Listener<ReqT> interceptCall(
572617

573618
private static class FakeDataService extends BigtableGrpc.BigtableImplBase {
574619
final BlockingQueue<ReadRowsRequest> requests = Queues.newLinkedBlockingDeque();
620+
final BlockingQueue<ReadChangeStreamRequest> readChangeReadStreamRequests =
621+
Queues.newLinkedBlockingDeque();
575622
final BlockingQueue<PingAndWarmRequest> pingRequests = Queues.newLinkedBlockingDeque();
576623

577624
@SuppressWarnings("unchecked")
@@ -593,6 +640,13 @@ public void mutateRows(
593640
@Override
594641
public void readRows(
595642
ReadRowsRequest request, StreamObserver<ReadRowsResponse> responseObserver) {
643+
if (request.getTableName().contains(WAIT_TIME_TABLE_ID)) {
644+
try {
645+
Thread.sleep(WATCHDOG_CHECK_DURATION.toMillis() * 2);
646+
} catch (Exception e) {
647+
648+
}
649+
}
596650
requests.add(request);
597651
// Dummy row for stream
598652
responseObserver.onNext(
@@ -608,6 +662,23 @@ public void readRows(
608662
responseObserver.onCompleted();
609663
}
610664

665+
@Override
666+
public void readChangeStream(
667+
ReadChangeStreamRequest request,
668+
StreamObserver<ReadChangeStreamResponse> responseObserver) {
669+
if (request.getTableName().contains(WAIT_TIME_TABLE_ID)) {
670+
try {
671+
Thread.sleep(WATCHDOG_CHECK_DURATION.toMillis() * 2);
672+
} catch (Exception e) {
673+
674+
}
675+
}
676+
readChangeReadStreamRequests.add(request);
677+
// Dummy row for stream
678+
responseObserver.onNext(ReadChangeStreamResponse.getDefaultInstance());
679+
responseObserver.onCompleted();
680+
}
681+
611682
@Override
612683
public void pingAndWarm(
613684
PingAndWarmRequest request, StreamObserver<PingAndWarmResponse> responseObserver) {

0 commit comments

Comments
 (0)