From 509ffdc568b2bf5ff33cc26807a94e2ae15d1141 Mon Sep 17 00:00:00 2001 From: Mattie Fu Date: Fri, 1 Jul 2022 11:20:43 -0400 Subject: [PATCH] add integration tests and fix unit tests --- .../BigtableTracerStreamingCallable.java | 27 ++-- .../metrics/BigtableTracerUnaryCallable.java | 26 ++-- .../bigtable/data/v2/stub/metrics/Util.java | 2 +- .../data/v2/it/StreamingMetadataIT.java | 53 ------- .../v2/it/StreamingMetricsMetadataIT.java | 103 ++++++++++++++ .../data/v2/it/UnaryMetricsMetadataIT.java | 130 ++++++++++++++++++ .../bigtable/data/v2/it/UnaryTrailerIT.java | 80 ----------- .../metrics/BuiltinMetricsTracerTest.java | 50 ++----- 8 files changed, 267 insertions(+), 204 deletions(-) delete mode 100644 google-cloud-bigtable/src/test/java/com/google/cloud/bigtable/data/v2/it/StreamingMetadataIT.java create mode 100644 google-cloud-bigtable/src/test/java/com/google/cloud/bigtable/data/v2/it/StreamingMetricsMetadataIT.java create mode 100644 google-cloud-bigtable/src/test/java/com/google/cloud/bigtable/data/v2/it/UnaryMetricsMetadataIT.java delete mode 100644 google-cloud-bigtable/src/test/java/com/google/cloud/bigtable/data/v2/it/UnaryTrailerIT.java diff --git a/google-cloud-bigtable/src/main/java/com/google/cloud/bigtable/data/v2/stub/metrics/BigtableTracerStreamingCallable.java b/google-cloud-bigtable/src/main/java/com/google/cloud/bigtable/data/v2/stub/metrics/BigtableTracerStreamingCallable.java index 8b7f76e1b8..6f636bf55d 100644 --- a/google-cloud-bigtable/src/main/java/com/google/cloud/bigtable/data/v2/stub/metrics/BigtableTracerStreamingCallable.java +++ b/google-cloud-bigtable/src/main/java/com/google/cloud/bigtable/data/v2/stub/metrics/BigtableTracerStreamingCallable.java @@ -31,19 +31,16 @@ /** * This callable will - * - *
  • -Inject a {@link GrpcResponseMetadata} to access the headers returned by gRPC - * methods upon completion. The {@link BigtableTracer} will process metrics that were injected in - * the header/trailer and publish them to OpenCensus. If {@link GrpcResponseMetadata#getMetadata()} - * returned null, it probably means that the request has never reached GFE, and it'll increment the - * gfe_header_missing_counter in this case. - * - *
  • -This class will also access trailers from {@link GrpcResponseMetadata} to record zone and cluster ids. - * + *
  • -Inject a {@link GrpcResponseMetadata} to access the headers returned by gRPC methods upon + * completion. The {@link BigtableTracer} will process metrics that were injected in the + * header/trailer and publish them to OpenCensus. If {@link GrpcResponseMetadata#getMetadata()} + * returned null, it probably means that the request has never reached GFE, and it'll increment + * the gfe_header_missing_counter in this case. + *
  • -This class will also access trailers from {@link GrpcResponseMetadata} to record zone and + * cluster ids. *
  • -Call {@link BigtableTracer#onRequest(int)} to record the request events in a stream. - * *
  • This class is considered an internal implementation detail and not meant to be used by - * applications. + * applications. */ @InternalApi public class BigtableTracerStreamingCallable @@ -108,9 +105,7 @@ public void onError(Throwable t) { tracer.recordGfeMetadata(latency, t); try { byte[] trailers = - responseMetadata - .getTrailingMetadata() - .get(Metadata.Key.of(Util.TRAILER_KEY, Metadata.BINARY_BYTE_MARSHALLER)); + metadata.get(Metadata.Key.of(Util.RESPONSE_PRAMS_KEY, Metadata.BINARY_BYTE_MARSHALLER)); ResponseParams decodedTrailers = ResponseParams.parseFrom(trailers); tracer.setLocations(decodedTrailers.getZoneId(), decodedTrailers.getClusterId()); } catch (NullPointerException | InvalidProtocolBufferException e) { @@ -126,9 +121,7 @@ public void onComplete() { tracer.recordGfeMetadata(latency, null); try { byte[] trailers = - responseMetadata - .getTrailingMetadata() - .get(Metadata.Key.of(Util.TRAILER_KEY, Metadata.BINARY_BYTE_MARSHALLER)); + metadata.get(Metadata.Key.of(Util.RESPONSE_PRAMS_KEY, Metadata.BINARY_BYTE_MARSHALLER)); ResponseParams decodedTrailers = ResponseParams.parseFrom(trailers); tracer.setLocations(decodedTrailers.getZoneId(), decodedTrailers.getClusterId()); } catch (NullPointerException | InvalidProtocolBufferException e) { diff --git a/google-cloud-bigtable/src/main/java/com/google/cloud/bigtable/data/v2/stub/metrics/BigtableTracerUnaryCallable.java b/google-cloud-bigtable/src/main/java/com/google/cloud/bigtable/data/v2/stub/metrics/BigtableTracerUnaryCallable.java index a5284d17e5..0efc99fe4d 100644 --- a/google-cloud-bigtable/src/main/java/com/google/cloud/bigtable/data/v2/stub/metrics/BigtableTracerUnaryCallable.java +++ b/google-cloud-bigtable/src/main/java/com/google/cloud/bigtable/data/v2/stub/metrics/BigtableTracerUnaryCallable.java @@ -31,17 +31,15 @@ /** * This callable will: - * - *
  • - Inject a {@link GrpcResponseMetadata} to access the headers returned by gRPC - * methods upon completion. The {@link BigtableTracer} will process metrics that were injected in - * the header/trailer and publish them to OpenCensus. If {@link GrpcResponseMetadata#getMetadata()} - * returned null, it probably means that the request has never reached GFE, and it'll increment the - * gfe_header_missing_counter in this case. - * - *
  • -This class will also access trailers from {@link GrpcResponseMetadata} to record zone and cluster ids. - * + *
  • - Inject a {@link GrpcResponseMetadata} to access the headers returned by gRPC methods upon + * completion. The {@link BigtableTracer} will process metrics that were injected in the + * header/trailer and publish them to OpenCensus. If {@link GrpcResponseMetadata#getMetadata()} + * returned null, it probably means that the request has never reached GFE, and it'll increment + * the gfe_header_missing_counter in this case. + *
  • -This class will also access trailers from {@link GrpcResponseMetadata} to record zone and + * cluster ids. *
  • This class is considered an internal implementation detail and not meant to be used by - * applications. + * applications. */ @InternalApi public class BigtableTracerUnaryCallable @@ -86,9 +84,7 @@ public void onFailure(Throwable throwable) { tracer.recordGfeMetadata(latency, throwable); try { byte[] trailers = - responseMetadata - .getTrailingMetadata() - .get(Metadata.Key.of(Util.TRAILER_KEY, Metadata.BINARY_BYTE_MARSHALLER)); + metadata.get(Metadata.Key.of(Util.RESPONSE_PRAMS_KEY, Metadata.BINARY_BYTE_MARSHALLER)); ResponseParams decodedTrailers = ResponseParams.parseFrom(trailers); tracer.setLocations(decodedTrailers.getZoneId(), decodedTrailers.getClusterId()); } catch (NullPointerException | InvalidProtocolBufferException e) { @@ -102,9 +98,7 @@ public void onSuccess(ResponseT response) { tracer.recordGfeMetadata(latency, null); try { byte[] trailers = - responseMetadata - .getMetadata() - .get(Metadata.Key.of(Util.TRAILER_KEY, Metadata.BINARY_BYTE_MARSHALLER)); + metadata.get(Metadata.Key.of(Util.RESPONSE_PRAMS_KEY, Metadata.BINARY_BYTE_MARSHALLER)); ResponseParams decodedTrailers = ResponseParams.parseFrom(trailers); tracer.setLocations(decodedTrailers.getZoneId(), decodedTrailers.getClusterId()); } catch (NullPointerException | InvalidProtocolBufferException e) { diff --git a/google-cloud-bigtable/src/main/java/com/google/cloud/bigtable/data/v2/stub/metrics/Util.java b/google-cloud-bigtable/src/main/java/com/google/cloud/bigtable/data/v2/stub/metrics/Util.java index 0440029027..7487703fc0 100644 --- a/google-cloud-bigtable/src/main/java/com/google/cloud/bigtable/data/v2/stub/metrics/Util.java +++ b/google-cloud-bigtable/src/main/java/com/google/cloud/bigtable/data/v2/stub/metrics/Util.java @@ -58,7 +58,7 @@ public class Util { Metadata.Key.of("server-timing", Metadata.ASCII_STRING_MARSHALLER); private static final Pattern SERVER_TIMING_HEADER_PATTERN = Pattern.compile(".*dur=(?\\d+)"); - static final String TRAILER_KEY = "x-goog-ext-425905942-bin"; + static final String RESPONSE_PRAMS_KEY = "x-goog-ext-425905942-bin"; /** Convert an exception into a value that can be used to create an OpenCensus tag value. */ static String extractStatus(@Nullable Throwable error) { diff --git a/google-cloud-bigtable/src/test/java/com/google/cloud/bigtable/data/v2/it/StreamingMetadataIT.java b/google-cloud-bigtable/src/test/java/com/google/cloud/bigtable/data/v2/it/StreamingMetadataIT.java deleted file mode 100644 index 90748bba16..0000000000 --- a/google-cloud-bigtable/src/test/java/com/google/cloud/bigtable/data/v2/it/StreamingMetadataIT.java +++ /dev/null @@ -1,53 +0,0 @@ -package com.google.cloud.bigtable.data.v2.it; - -import com.google.api.core.ApiFuture; -import com.google.cloud.bigtable.data.v2.models.Query; -import com.google.cloud.bigtable.data.v2.models.Row; -import com.google.cloud.bigtable.data.v2.models.RowCell; -import com.google.cloud.bigtable.data.v2.models.RowMutation; -import com.google.cloud.bigtable.test_helpers.env.TestEnvRule; -import com.google.common.collect.ImmutableList; -import com.google.common.collect.Lists; -import com.google.protobuf.ByteString; -import org.junit.ClassRule; - -import java.util.ArrayList; -import java.util.List; -import java.util.UUID; - -public class StreamingMetadataIT { - @ClassRule - public static TestEnvRule testEnvRule = new TestEnvRule(); - - public void test() { - String prefix = UUID.randomUUID().toString(); - int numRows = 5; - List expectedRows = Lists.newArrayList(); - String uniqueKey = prefix + "-read"; - - long timestampMicros = System.currentTimeMillis() * 1_000; - - for (int i = 0; i < numRows; i++) { - testEnvRule - .env() - .getDataClient() - .mutateRowCallable() - .call( - RowMutation.create(testEnvRule.env().getTableId(), uniqueKey + "-" + i) - .setCell(testEnvRule.env().getFamilyId(), "q", timestampMicros, "my-value")); - - expectedRows.add( - Row.create( - ByteString.copyFromUtf8(uniqueKey + "-" + i), - ImmutableList.of( - RowCell.create( - testEnvRule.env().getFamilyId(), - ByteString.copyFromUtf8("q"), - timestampMicros, - ImmutableList.of(), - ByteString.copyFromUtf8("my-value"))))); - } - - String tableId = testEnvRule.env().getTableId(); - } -} diff --git a/google-cloud-bigtable/src/test/java/com/google/cloud/bigtable/data/v2/it/StreamingMetricsMetadataIT.java b/google-cloud-bigtable/src/test/java/com/google/cloud/bigtable/data/v2/it/StreamingMetricsMetadataIT.java new file mode 100644 index 0000000000..a71773dbba --- /dev/null +++ b/google-cloud-bigtable/src/test/java/com/google/cloud/bigtable/data/v2/it/StreamingMetricsMetadataIT.java @@ -0,0 +1,103 @@ +package com.google.cloud.bigtable.data.v2.it; + +import static com.google.common.truth.Truth.assertThat; +import static com.google.common.truth.TruthJUnit.assume; + +import com.google.api.core.ApiFuture; +import com.google.api.gax.rpc.NotFoundException; +import com.google.cloud.bigtable.admin.v2.models.Cluster; +import com.google.cloud.bigtable.data.v2.models.Query; +import com.google.cloud.bigtable.data.v2.models.Row; +import com.google.cloud.bigtable.stats.BuiltinViews; +import com.google.cloud.bigtable.test_helpers.env.EmulatorEnv; +import com.google.cloud.bigtable.test_helpers.env.TestEnvRule; +import com.google.common.collect.Lists; +import io.opencensus.stats.Stats; +import io.opencensus.stats.View; +import io.opencensus.stats.ViewData; +import io.opencensus.stats.ViewManager; +import io.opencensus.tags.TagValue; +import java.util.ArrayList; +import java.util.List; +import java.util.Map; +import java.util.UUID; +import java.util.concurrent.TimeUnit; +import java.util.stream.Collectors; +import org.junit.BeforeClass; +import org.junit.ClassRule; +import org.junit.Test; + +public class StreamingMetricsMetadataIT { + @ClassRule public static TestEnvRule testEnvRule = new TestEnvRule(); + + @BeforeClass + public static void setUpClass() { + assume() + .withMessage("StreamingMetricsMetadataIT is not supported on Emulator") + .that(testEnvRule.env()) + .isNotInstanceOf(EmulatorEnv.class); + BuiltinViews.registerBigtableBuiltinViews(); + } + + @Test + public void testSuccess() throws Exception { + String prefix = UUID.randomUUID().toString(); + String uniqueKey = prefix + "-read"; + + Query query = Query.create(testEnvRule.env().getTableId()).rowKey(uniqueKey); + ArrayList rows = Lists.newArrayList(testEnvRule.env().getDataClient().readRows(query)); + + // give opencensus some time to populate view data + Thread.sleep(100); + + ViewManager viewManager = Stats.getViewManager(); + ViewData viewData = + viewManager.getView( + View.Name.create("bigtable.googleapis.com/internal/client/operation_latencies")); + + List tagValues = + viewData.getAggregationMap().entrySet().stream() + .map(Map.Entry::getKey) + .flatMap(x -> x.stream()) + .collect(Collectors.toCollection(ArrayList::new)); + + ApiFuture> clustersFuture = + testEnvRule + .env() + .getInstanceAdminClient() + .listClustersAsync(testEnvRule.env().getInstanceId()); + List clusters = clustersFuture.get(1, TimeUnit.MINUTES); + + assertThat(tagValues).contains(TagValue.create(clusters.get(0).getZone())); + assertThat(tagValues).contains(TagValue.create(clusters.get(0).getId())); + } + + @Test + public void testFailure() throws InterruptedException { + String prefix = UUID.randomUUID().toString(); + String uniqueKey = prefix + "-read"; + + Query query = Query.create("non-exist-table"); + try { + Lists.newArrayList(testEnvRule.env().getDataClient().readRows(query)); + } catch (NotFoundException e) { + } + + // give opencensus some time to populate view data + Thread.sleep(100); + + ViewManager viewManager = Stats.getViewManager(); + ViewData viewData = + viewManager.getView( + View.Name.create("bigtable.googleapis.com/internal/client/operation_latencies")); + + List tagValues = + viewData.getAggregationMap().entrySet().stream() + .map(Map.Entry::getKey) + .flatMap(x -> x.stream()) + .collect(Collectors.toCollection(ArrayList::new)); + + assertThat(tagValues).contains(TagValue.create("undefined")); + assertThat(tagValues).contains(TagValue.create("undefined")); + } +} diff --git a/google-cloud-bigtable/src/test/java/com/google/cloud/bigtable/data/v2/it/UnaryMetricsMetadataIT.java b/google-cloud-bigtable/src/test/java/com/google/cloud/bigtable/data/v2/it/UnaryMetricsMetadataIT.java new file mode 100644 index 0000000000..1e2c90649b --- /dev/null +++ b/google-cloud-bigtable/src/test/java/com/google/cloud/bigtable/data/v2/it/UnaryMetricsMetadataIT.java @@ -0,0 +1,130 @@ +/* + * Copyright 2022 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.it; + +import static com.google.common.truth.Truth.assertThat; +import static com.google.common.truth.TruthJUnit.assume; + +import com.google.api.core.ApiFuture; +import com.google.api.gax.rpc.NotFoundException; +import com.google.cloud.bigtable.admin.v2.models.Cluster; +import com.google.cloud.bigtable.data.v2.models.RowMutation; +import com.google.cloud.bigtable.stats.BuiltinViews; +import com.google.cloud.bigtable.test_helpers.env.EmulatorEnv; +import com.google.cloud.bigtable.test_helpers.env.TestEnvRule; +import io.opencensus.stats.Stats; +import io.opencensus.stats.View; +import io.opencensus.stats.ViewData; +import io.opencensus.stats.ViewManager; +import io.opencensus.tags.TagValue; +import java.util.ArrayList; +import java.util.List; +import java.util.Map; +import java.util.UUID; +import java.util.concurrent.TimeUnit; +import java.util.stream.Collectors; +import org.junit.BeforeClass; +import org.junit.ClassRule; +import org.junit.Ignore; +import org.junit.Test; + +@Ignore +public class UnaryMetricsMetadataIT { + @ClassRule public static TestEnvRule testEnvRule = new TestEnvRule(); + + @BeforeClass + public static void setUpClass() { + assume() + .withMessage("UnaryMetricsMetadataIT is not supported on Emulator") + .that(testEnvRule.env()) + .isNotInstanceOf(EmulatorEnv.class); + + BuiltinViews.registerBigtableBuiltinViews(); + } + + @Test + public void testSuccess() throws Exception { + String rowKey = UUID.randomUUID().toString(); + String familyId = testEnvRule.env().getFamilyId(); + + ApiFuture future = + testEnvRule + .env() + .getDataClient() + .mutateRowCallable() + .futureCall( + RowMutation.create(testEnvRule.env().getTableId(), rowKey) + .setCell(familyId, "q", "myVal")); + + future.get(1, TimeUnit.MINUTES); + + // give opencensus some time to populate view data + Thread.sleep(100); + + ViewManager viewManager = Stats.getViewManager(); + ViewData viewData = + viewManager.getView( + View.Name.create("bigtable.googleapis.com/internal/client/operation_latencies")); + + List tagValues = + viewData.getAggregationMap().entrySet().stream() + .map(Map.Entry::getKey) + .flatMap(x -> x.stream()) + .collect(Collectors.toCollection(ArrayList::new)); + + ApiFuture> clustersFuture = + testEnvRule + .env() + .getInstanceAdminClient() + .listClustersAsync(testEnvRule.env().getInstanceId()); + List clusters = clustersFuture.get(1, TimeUnit.MINUTES); + + assertThat(tagValues).contains(TagValue.create(clusters.get(0).getZone())); + assertThat(tagValues).contains(TagValue.create(clusters.get(0).getId())); + } + + @Test + public void testFailure() throws InterruptedException { + String rowKey = UUID.randomUUID().toString(); + String familyId = testEnvRule.env().getFamilyId(); + + try { + testEnvRule + .env() + .getDataClient() + .mutateRowCallable() + .call(RowMutation.create("non-exist-table", rowKey).setCell(familyId, "q", "myVal")); + } catch (NotFoundException e) { + } + + // give opencensus some time to populate view data + Thread.sleep(100); + + ViewManager viewManager = Stats.getViewManager(); + ViewData viewData = + viewManager.getView( + View.Name.create("bigtable.googleapis.com/internal/client/operation_latencies")); + + List tagValues = + viewData.getAggregationMap().entrySet().stream() + .map(Map.Entry::getKey) + .flatMap(x -> x.stream()) + .collect(Collectors.toCollection(ArrayList::new)); + + assertThat(tagValues).contains(TagValue.create("undefined")); + assertThat(tagValues).contains(TagValue.create("undefined")); + } +} diff --git a/google-cloud-bigtable/src/test/java/com/google/cloud/bigtable/data/v2/it/UnaryTrailerIT.java b/google-cloud-bigtable/src/test/java/com/google/cloud/bigtable/data/v2/it/UnaryTrailerIT.java deleted file mode 100644 index 71810a06a5..0000000000 --- a/google-cloud-bigtable/src/test/java/com/google/cloud/bigtable/data/v2/it/UnaryTrailerIT.java +++ /dev/null @@ -1,80 +0,0 @@ -/* - * Copyright 2022 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.it; - -import com.google.api.core.ApiFuture; -import com.google.api.gax.grpc.GrpcResponseMetadata; -import com.google.cloud.bigtable.data.v2.models.Query; -import com.google.cloud.bigtable.data.v2.models.Row; -import com.google.cloud.bigtable.data.v2.models.RowMutation; -import com.google.cloud.bigtable.stats.BuiltinViews; -import com.google.cloud.bigtable.test_helpers.env.TestEnvRule; -import com.google.common.collect.ImmutableList; -import com.google.protobuf.ByteString; -import io.grpc.Metadata; -import io.opencensus.stats.AggregationData; -import io.opencensus.stats.Stats; -import io.opencensus.stats.View; -import io.opencensus.stats.ViewData; -import io.opencensus.stats.ViewManager; -import io.opencensus.tags.TagKey; -import io.opencensus.tags.TagValue; -import org.junit.ClassRule; -import org.junit.Test; - -import java.util.List; -import java.util.Map; -import java.util.Set; -import java.util.UUID; -import java.util.concurrent.TimeUnit; - -import static com.google.common.truth.Truth.assertThat; - -public class UnaryTrailerIT { - @ClassRule - public static TestEnvRule testEnvRule = new TestEnvRule(); - - @Test - public void test() throws Exception { - BuiltinViews.registerBigtableBuiltinViews(); - - String rowKey = UUID.randomUUID().toString(); - String familyId = testEnvRule.env().getFamilyId(); - - - ApiFuture future = - testEnvRule - .env() - .getDataClient() - .mutateRowCallable() - .futureCall(RowMutation.create(testEnvRule.env().getTableId(), rowKey) - .setCell(familyId, "q", "myVal")); - - future.get(1, TimeUnit.MINUTES); - - Thread.sleep(1000); - ViewManager viewManager = Stats.getViewManager(); - ViewData viewData = viewManager.getView(View.Name.create("bigtable.googleapis.com/internal/client/operation_latencies")); - - int clusterIndex = viewData.getView().getColumns().indexOf(TagKey.create("cluster")); - int zoneIndex = viewData.getView().getColumns().indexOf(TagKey.create("zone")); - - List tagValues = viewData.getAggregationMap().entrySet().stream().findFirst().get().getKey(); - assertThat(tagValues.get(zoneIndex).asString()).isEqualTo(testEnvRule.env().getPrimaryZone()); - - assertThat(tagValues.get(clusterIndex).asString()).isEqualTo(testEnvRule.env().getPrimaryClusterId()); - } -} diff --git a/google-cloud-bigtable/src/test/java/com/google/cloud/bigtable/data/v2/stub/metrics/BuiltinMetricsTracerTest.java b/google-cloud-bigtable/src/test/java/com/google/cloud/bigtable/data/v2/stub/metrics/BuiltinMetricsTracerTest.java index 3a7e0b4a32..b9bd2a926c 100644 --- a/google-cloud-bigtable/src/test/java/com/google/cloud/bigtable/data/v2/stub/metrics/BuiltinMetricsTracerTest.java +++ b/google-cloud-bigtable/src/test/java/com/google/cloud/bigtable/data/v2/stub/metrics/BuiltinMetricsTracerTest.java @@ -85,17 +85,15 @@ public class BuiltinMetricsTracerTest { private static final String INSTANCE_ID = "fake-instance"; private static final String APP_PROFILE_ID = "default"; private static final String TABLE_ID = "fake-table"; - private static final String ZONE_0 = "us-west-1"; - private static final String CLUSTER_0 = "cluster-0"; - private static final String ZONE_1 = "us-east-1"; - private static final String CLUSTER_1 = "cluster-1"; + private static final String ZONE = "us-west-1"; + private static final String CLUSTER = "cluster-0"; private static final long FAKE_SERVER_TIMING = 50; private static final long SERVER_LATENCY = 100; private static final long APPLICATION_LATENCY = 200; @Rule public final MockitoRule mockitoRule = MockitoJUnit.rule(); - private FakeService fakeService; + private final FakeService fakeService = new FakeService(); private Server server; private EnhancedBigtableStub stub; @@ -110,9 +108,6 @@ public class BuiltinMetricsTracerTest { @Before public void setUp() throws Exception { - fakeService = new FakeService(); - - final AtomicInteger count = new AtomicInteger(0); // Add an interceptor to add server-timing in headers ServerInterceptor trailersInterceptor = new ServerInterceptor() { @@ -128,34 +123,15 @@ public void sendHeaders(Metadata headers) { headers.put( Metadata.Key.of("server-timing", Metadata.ASCII_STRING_MARSHALLER), String.format("gfet4t7; dur=%d", FAKE_SERVER_TIMING)); - super.sendHeaders(headers); - } - @Override - public void close(Status status, Metadata trailers) { - int currentCount = count.getAndIncrement(); - if (currentCount == 0) { - ResponseParams params = - ResponseParams.newBuilder() - .setZoneId(ZONE_0) - .setClusterId(CLUSTER_0) - .build(); - byte[] byteArray = params.toByteArray(); - trailers.put( - Metadata.Key.of(Util.TRAILER_KEY, Metadata.BINARY_BYTE_MARSHALLER), - byteArray); - } else { - ResponseParams params = - ResponseParams.newBuilder() - .setClusterId(CLUSTER_1) - .setZoneId(ZONE_1) - .build(); - byte[] byteArray = params.toByteArray(); - trailers.put( - Metadata.Key.of(Util.TRAILER_KEY, Metadata.BINARY_BYTE_MARSHALLER), - byteArray); - } - super.close(status, trailers); + ResponseParams params = + ResponseParams.newBuilder().setZoneId(ZONE).setClusterId(CLUSTER).build(); + byte[] byteArray = params.toByteArray(); + headers.put( + Metadata.Key.of(Util.RESPONSE_PRAMS_KEY, Metadata.BINARY_BYTE_MARSHALLER), + byteArray); + + super.sendHeaders(headers); } }, metadata); @@ -365,8 +341,8 @@ public void testMutateRowAttempts() { // and when the record() is called in onOperationCompletion(). verify(statsRecorderWrapper, timeout(50).times(fakeService.getAttemptCounter().get() + 1)) .record(status.capture(), tableId.capture(), zone.capture(), cluster.capture()); - assertThat(zone.getAllValues()).containsExactly(ZONE_0, ZONE_1, ZONE_1, ZONE_1); - assertThat(cluster.getAllValues()).containsExactly(CLUSTER_0, CLUSTER_1, CLUSTER_1, CLUSTER_1); + assertThat(zone.getAllValues()).containsExactly("undefined", "undefined", ZONE, ZONE); + assertThat(cluster.getAllValues()).containsExactly("undefined", "undefined", CLUSTER, CLUSTER); assertThat(status.getAllValues()).containsExactly("UNAVAILABLE", "UNAVAILABLE", "OK", "OK"); }