From 30e26bc1d424c21d2a170dae60b2cb236fa7754f Mon Sep 17 00:00:00 2001 From: Steve Niemitz Date: Thu, 27 Jul 2023 15:15:23 -0400 Subject: [PATCH] chore: Fix flaky metrics tests --- .../metrics/BigtableTracerCallableTest.java | 11 ++++---- .../v2/stub/metrics/MetricsTracerTest.java | 25 +++---------------- .../v2/stub/metrics/SimpleStatsComponent.java | 12 +++++++++ 3 files changed, 20 insertions(+), 28 deletions(-) create mode 100644 google-cloud-bigtable/src/test/java/com/google/cloud/bigtable/data/v2/stub/metrics/SimpleStatsComponent.java diff --git a/google-cloud-bigtable/src/test/java/com/google/cloud/bigtable/data/v2/stub/metrics/BigtableTracerCallableTest.java b/google-cloud-bigtable/src/test/java/com/google/cloud/bigtable/data/v2/stub/metrics/BigtableTracerCallableTest.java index 1b833f5c06..e783352bf0 100644 --- a/google-cloud-bigtable/src/test/java/com/google/cloud/bigtable/data/v2/stub/metrics/BigtableTracerCallableTest.java +++ b/google-cloud-bigtable/src/test/java/com/google/cloud/bigtable/data/v2/stub/metrics/BigtableTracerCallableTest.java @@ -19,6 +19,7 @@ import static org.junit.Assert.fail; import com.google.api.gax.rpc.ClientContext; +import com.google.api.gax.rpc.ServerStream; import com.google.api.gax.rpc.UnavailableException; import com.google.bigtable.v2.BigtableGrpc.BigtableImplBase; import com.google.bigtable.v2.CheckAndMutateRowRequest; @@ -54,7 +55,6 @@ import io.grpc.Status; import io.grpc.StatusRuntimeException; import io.grpc.stub.StreamObserver; -import io.opencensus.impl.stats.StatsComponentImpl; import io.opencensus.stats.StatsComponent; import io.opencensus.tags.TagKey; import io.opencensus.tags.TagValue; @@ -74,7 +74,7 @@ public class BigtableTracerCallableTest { private FakeService fakeService = new FakeService(); - private final StatsComponent localStats = new StatsComponentImpl(); + private final StatsComponent localStats = new SimpleStatsComponent(); private EnhancedBigtableStub stub; private EnhancedBigtableStub noHeaderStub; private int attempts; @@ -157,10 +157,9 @@ public void tearDown() { } @Test - public void testGFELatencyMetricReadRows() throws InterruptedException { - stub.readRowsCallable().call(Query.create(TABLE_ID)); - - Thread.sleep(WAIT_FOR_METRICS_TIME_MS); + public void testGFELatencyMetricReadRows() { + ServerStream call = stub.readRowsCallable().call(Query.create(TABLE_ID)); + call.forEach(r -> {}); long latency = StatsTestUtils.getAggregationValueAsLong( diff --git a/google-cloud-bigtable/src/test/java/com/google/cloud/bigtable/data/v2/stub/metrics/MetricsTracerTest.java b/google-cloud-bigtable/src/test/java/com/google/cloud/bigtable/data/v2/stub/metrics/MetricsTracerTest.java index bb5e89aab4..db33e2f747 100644 --- a/google-cloud-bigtable/src/test/java/com/google/cloud/bigtable/data/v2/stub/metrics/MetricsTracerTest.java +++ b/google-cloud-bigtable/src/test/java/com/google/cloud/bigtable/data/v2/stub/metrics/MetricsTracerTest.java @@ -55,7 +55,7 @@ import io.grpc.Status; import io.grpc.StatusRuntimeException; import io.grpc.stub.StreamObserver; -import io.opencensus.impl.stats.StatsComponentImpl; +import io.opencensus.stats.StatsComponent; import io.opencensus.tags.TagKey; import io.opencensus.tags.TagValue; import io.opencensus.tags.Tags; @@ -105,7 +105,7 @@ public class MetricsTracerTest { @Mock(answer = Answers.CALLS_REAL_METHODS) private BigtableGrpc.BigtableImplBase mockService; - private final StatsComponentImpl localStats = new StatsComponentImpl(); + private final StatsComponent localStats = new SimpleStatsComponent(); private EnhancedBigtableStub stub; private BigtableDataSettings settings; @@ -157,9 +157,6 @@ public Object answer(InvocationOnMock invocation) throws Throwable { Lists.newArrayList(stub.readRowsCallable().call(Query.create(TABLE_ID))); long elapsed = stopwatch.elapsed(TimeUnit.MILLISECONDS); - // Give OpenCensus a chance to update the views asynchronously. - Thread.sleep(100); - long opLatency = StatsTestUtils.getAggregationValueAsLong( localStats, @@ -193,9 +190,6 @@ public Object answer(InvocationOnMock invocation) { Lists.newArrayList(stub.readRowsCallable().call(Query.create(TABLE_ID))); Lists.newArrayList(stub.readRowsCallable().call(Query.create(TABLE_ID))); - // Give OpenCensus a chance to update the views asynchronously. - Thread.sleep(100); - long opLatency = StatsTestUtils.getAggregationValueAsLong( localStats, @@ -247,8 +241,6 @@ public void testReadRowsFirstRow() throws InterruptedException { } long elapsed = stopwatch.elapsed(TimeUnit.MILLISECONDS); - // Give OpenCensus a chance to update the views asynchronously. - Thread.sleep(100); executor.shutdown(); long firstRowLatency = @@ -292,9 +284,6 @@ public Object answer(InvocationOnMock invocation) { Lists.newArrayList(stub.readRowsCallable().call(Query.create(TABLE_ID))); - // Give OpenCensus a chance to update the views asynchronously. - Thread.sleep(100); - long opLatency = StatsTestUtils.getAggregationValueAsLong( localStats, @@ -341,9 +330,6 @@ public Object answer(InvocationOnMock invocation) throws Throwable { Lists.newArrayList(stub.readRowsCallable().call(Query.create(TABLE_ID))); long elapsed = stopwatch.elapsed(TimeUnit.MILLISECONDS); - // Give OpenCensus a chance to update the views asynchronously. - Thread.sleep(100); - long attemptLatency = StatsTestUtils.getAggregationValueAsLong( localStats, @@ -360,12 +346,11 @@ public Object answer(InvocationOnMock invocation) throws Throwable { } @Test - public void testInvalidRequest() throws InterruptedException { + public void testInvalidRequest() { try { stub.bulkMutateRowsCallable().call(BulkMutation.create(TABLE_ID)); Assert.fail("Invalid request should throw exception"); } catch (IllegalStateException e) { - Thread.sleep(100); // Verify that the latency is recorded with an error code (in this case UNKNOWN) long attemptLatency = StatsTestUtils.getAggregationValueAsLong( @@ -403,9 +388,6 @@ public Object answer(InvocationOnMock invocation) { batcher.add(ByteString.copyFromUtf8("row1")); batcher.sendOutstanding(); - // Give OpenCensus a chance to update the views asynchronously. - Thread.sleep(100); - long throttledTimeMetric = StatsTestUtils.getAggregationValueAsLong( localStats, @@ -471,7 +453,6 @@ public Object answer(InvocationOnMock invocation) { batcher.add(RowMutationEntry.create("key")); batcher.sendOutstanding(); - Thread.sleep(100); long throttledTimeMetric = StatsTestUtils.getAggregationValueAsLong( localStats, diff --git a/google-cloud-bigtable/src/test/java/com/google/cloud/bigtable/data/v2/stub/metrics/SimpleStatsComponent.java b/google-cloud-bigtable/src/test/java/com/google/cloud/bigtable/data/v2/stub/metrics/SimpleStatsComponent.java new file mode 100644 index 0000000000..a7684e3288 --- /dev/null +++ b/google-cloud-bigtable/src/test/java/com/google/cloud/bigtable/data/v2/stub/metrics/SimpleStatsComponent.java @@ -0,0 +1,12 @@ +package com.google.cloud.bigtable.data.v2.stub.metrics; + +import io.opencensus.implcore.common.MillisClock; +import io.opencensus.implcore.internal.SimpleEventQueue; +import io.opencensus.implcore.stats.StatsComponentImplBase; + +/** A StatsComponent implementation for testing that executes all events inline. */ +public class SimpleStatsComponent extends StatsComponentImplBase { + public SimpleStatsComponent() { + super(new SimpleEventQueue(), MillisClock.getInstance()); + } +}