Skip to content

Commit a198247

Browse files
committed
update on comments
1 parent 15af318 commit a198247

File tree

5 files changed

+35
-47
lines changed

5 files changed

+35
-47
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -154,7 +154,7 @@ public static EnhancedBigtableStubSettings finalizeSettings(
154154
EnhancedBigtableStubSettings settings, Tagger tagger, StatsRecorder stats)
155155
throws IOException {
156156
EnhancedBigtableStubSettings.Builder builder = settings.toBuilder();
157-
StatsWrapper statsWrapper = StatsWrapper.get();
157+
StatsWrapper statsWrapper = StatsWrapper.create();
158158

159159
// TODO: this implementation is on the cusp of unwieldy, if we end up adding more features
160160
// consider splitting it up by feature.

google-cloud-bigtable/src/main/java/com/google/cloud/bigtable/data/v2/stub/metrics/BigtableTracer.java

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ public void attemptStarted(int attemptNumber) {
3535
this.attempt = attemptNumber;
3636
}
3737

38-
/** annotate when onRequest is called */
38+
/** annotate when onRequest is called. This will be called in BuiltinMetricsTracer. */
3939
public void onRequest() {
4040
// noop
4141
}
@@ -63,7 +63,10 @@ public void batchRequestThrottled(long throttledTimeMs) {
6363
// noop
6464
}
6565

66-
/** Set the Bigtable zone and cluster so metrics can be tagged with location information. */
66+
/**
67+
* Set the Bigtable zone and cluster so metrics can be tagged with location information. This will
68+
* be called in BuiltinMetricsTracer.
69+
*/
6770
public void setLocations(String zone, String cluster) {
6871
// noop
6972
}

google-cloud-bigtable/src/main/java/com/google/cloud/bigtable/data/v2/stub/metrics/BuiltinMetricsTracer.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -66,11 +66,12 @@ class BuiltinMetricsTracer extends BigtableTracer {
6666
SpanName spanName,
6767
Map<String, String> attributes,
6868
StatsWrapper statsWrapper,
69-
StatsRecorderWrapper recorder) {
69+
@Nullable StatsRecorderWrapper statsRecorderWrapper) {
7070
this.operationType = operationType;
7171
this.spanName = spanName;
72-
if (recorder != null) {
73-
this.recorder = recorder;
72+
if (statsRecorderWrapper != null) {
73+
// A workaround for test to pass in a mock StatsRecorderWrapper
74+
this.recorder = statsRecorderWrapper;
7475
} else {
7576
this.recorder = new StatsRecorderWrapper(operationType, spanName, attributes, statsWrapper);
7677
}

google-cloud-bigtable/src/main/java/com/google/cloud/bigtable/data/v2/stub/metrics/BuiltinMetricsTracerFactory.java

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -41,21 +41,22 @@ public static BuiltinMetricsTracerFactory create(
4141
return new BuiltinMetricsTracerFactory(statsWrapper, statsAttributes, null);
4242
}
4343

44+
// A workaround for test to pass in a mock StatsRecorderWrapper
4445
@VisibleForTesting
45-
static BuiltinMetricsTracerFactory create(
46+
public static BuiltinMetricsTracerFactory createWithRecorder(
4647
StatsWrapper statsWrapper,
4748
ImmutableMap<String, String> statsAttributes,
48-
StatsRecorderWrapper recorder) {
49-
return new BuiltinMetricsTracerFactory(statsWrapper, statsAttributes, recorder);
49+
StatsRecorderWrapper statsRecorderWrapper) {
50+
return new BuiltinMetricsTracerFactory(statsWrapper, statsAttributes, statsRecorderWrapper);
5051
}
5152

5253
private BuiltinMetricsTracerFactory(
5354
StatsWrapper statsWrapper,
5455
ImmutableMap<String, String> statsAttributes,
55-
StatsRecorderWrapper recorder) {
56+
StatsRecorderWrapper statsRecorderWrapper) {
5657
this.statsAttributes = statsAttributes;
5758
this.statsWrapper = statsWrapper;
58-
this.statsRecorderWrapper = recorder;
59+
this.statsRecorderWrapper = statsRecorderWrapper;
5960
}
6061

6162
@Override

google-cloud-bigtable/src/test/java/com/google/cloud/bigtable/data/v2/stub/metrics/BuiltinMetricsTracerTest.java

Lines changed: 19 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2021 Google LLC
2+
* Copyright 2022 Google LLC
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -61,7 +61,8 @@
6161
import org.junit.Rule;
6262
import org.junit.Test;
6363
import org.mockito.ArgumentCaptor;
64-
import org.mockito.Mockito;
64+
import org.mockito.Captor;
65+
import org.mockito.Mock;
6566
import org.mockito.junit.MockitoJUnit;
6667
import org.mockito.junit.MockitoRule;
6768
import org.threeten.bp.Duration;
@@ -118,13 +119,14 @@ public class BuiltinMetricsTracerTest {
118119

119120
private EnhancedBigtableStub stub;
120121

121-
private StatsRecorderWrapper statsRecorderWrapper;
122-
private ArgumentCaptor<Long> longValue;
123-
private ArgumentCaptor<Integer> intValue;
124-
private ArgumentCaptor<String> status;
125-
private ArgumentCaptor<String> tableId;
126-
private ArgumentCaptor<String> zone;
127-
private ArgumentCaptor<String> cluster;
122+
@Mock private StatsRecorderWrapper statsRecorderWrapper;
123+
124+
@Captor private ArgumentCaptor<Long> longValue;
125+
@Captor private ArgumentCaptor<Integer> intValue;
126+
@Captor private ArgumentCaptor<String> status;
127+
@Captor private ArgumentCaptor<String> tableId;
128+
@Captor private ArgumentCaptor<String> zone;
129+
@Captor private ArgumentCaptor<String> cluster;
128130

129131
private Stopwatch serverRetryDelayStopwatch;
130132
private AtomicLong serverTotalRetryDelay;
@@ -136,8 +138,7 @@ public void setUp() throws Exception {
136138
serverRetryDelayStopwatch = Stopwatch.createUnstarted();
137139
serverTotalRetryDelay = new AtomicLong(0);
138140

139-
// Add an interceptor to send location information in the trailers and add server-timing in
140-
// headers
141+
// Add an interceptor to add server-timing in headers
141142
ServerInterceptor trailersInterceptor =
142143
new ServerInterceptor() {
143144
private AtomicInteger count = new AtomicInteger(0);
@@ -156,20 +157,13 @@ public void sendHeaders(Metadata headers) {
156157
String.format("gfet4t7; dur=%d", FAKE_SERVER_TIMING));
157158
super.sendHeaders(headers);
158159
}
159-
160-
@Override
161-
public void close(Status status, Metadata trailers) {
162-
super.close(status, trailers);
163-
}
164160
},
165161
metadata);
166162
}
167163
};
168164

169165
server = FakeServiceBuilder.create(mockService).intercept(trailersInterceptor).start();
170166

171-
statsRecorderWrapper = Mockito.mock(StatsRecorderWrapper.class);
172-
173167
BigtableDataSettings settings =
174168
BigtableDataSettings.newBuilderForEmulator(server.getPort())
175169
.setProjectId(PROJECT_ID)
@@ -183,18 +177,11 @@ public void close(Status status, Metadata trailers) {
183177
.retrySettings()
184178
.setInitialRetryDelay(Duration.ofMillis(200));
185179
stubSettingsBuilder.setTracerFactory(
186-
BuiltinMetricsTracerFactory.create(
187-
StatsWrapper.get(), ImmutableMap.of(), statsRecorderWrapper));
180+
BuiltinMetricsTracerFactory.createWithRecorder(
181+
StatsWrapper.create(), ImmutableMap.of(), statsRecorderWrapper));
188182

189183
EnhancedBigtableStubSettings stubSettings = stubSettingsBuilder.build();
190184
stub = new EnhancedBigtableStub(stubSettings, ClientContext.create(stubSettings));
191-
192-
longValue = ArgumentCaptor.forClass(long.class);
193-
intValue = ArgumentCaptor.forClass(int.class);
194-
status = ArgumentCaptor.forClass(String.class);
195-
tableId = ArgumentCaptor.forClass(String.class);
196-
zone = ArgumentCaptor.forClass(String.class);
197-
cluster = ArgumentCaptor.forClass(String.class);
198185
}
199186

200187
@After
@@ -291,17 +278,13 @@ public void testMutateRowAttempts() {
291278
stub.mutateRowCallable()
292279
.call(RowMutation.create(TABLE_ID, "random-row").setCell("cf", "q", "value"));
293280

281+
// record will get called 4 times, 3 times for attempts and 1 for recording operation level
282+
// metrics.
294283
verify(statsRecorderWrapper, times(4))
295284
.record(status.capture(), tableId.capture(), zone.capture(), cluster.capture());
296-
assertThat(zone.getAllValues().get(0)).isEqualTo(UNDEFINED);
297-
assertThat(zone.getAllValues().get(1)).isEqualTo(UNDEFINED);
298-
assertThat(zone.getAllValues().get(2)).isEqualTo(UNDEFINED);
299-
assertThat(cluster.getAllValues().get(0)).isEqualTo(UNDEFINED);
300-
assertThat(cluster.getAllValues().get(1)).isEqualTo(UNDEFINED);
301-
assertThat(cluster.getAllValues().get(2)).isEqualTo(UNDEFINED);
302-
assertThat(status.getAllValues().get(0)).isEqualTo("UNAVAILABLE");
303-
assertThat(status.getAllValues().get(1)).isEqualTo("UNAVAILABLE");
304-
assertThat(status.getAllValues().get(2)).isEqualTo("OK");
285+
assertThat(zone.getAllValues()).containsExactly(UNDEFINED, UNDEFINED, UNDEFINED, UNDEFINED);
286+
assertThat(cluster.getAllValues()).containsExactly(UNDEFINED, UNDEFINED, UNDEFINED, UNDEFINED);
287+
assertThat(status.getAllValues()).containsExactly("UNAVAILABLE", "UNAVAILABLE", "OK", "OK");
305288
}
306289

307290
private class FakeService extends BigtableGrpc.BigtableImplBase {

0 commit comments

Comments
 (0)