Skip to content

Commit 8b2953e

Browse files
authored
fix: fix first response latencies (#2382)
* fix: fix first response latencies * fix
1 parent 3801961 commit 8b2953e

File tree

2 files changed

+57
-0
lines changed

2 files changed

+57
-0
lines changed

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -208,6 +208,9 @@ public void onRequest(int requestCount) {
208208

209209
@Override
210210
public void responseReceived() {
211+
if (firstResponsePerOpTimer.isRunning()) {
212+
firstResponsePerOpTimer.stop();
213+
}
211214
// When auto flow control is enabled, server latency is measured between afterResponse and
212215
// responseReceived.
213216
// When auto flow control is disabled, server latency is measured between onRequest and

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

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
import static com.google.cloud.bigtable.data.v2.stub.metrics.BuiltinMetricsConstants.CLIENT_NAME_KEY;
2222
import static com.google.cloud.bigtable.data.v2.stub.metrics.BuiltinMetricsConstants.CLUSTER_ID_KEY;
2323
import static com.google.cloud.bigtable.data.v2.stub.metrics.BuiltinMetricsConstants.CONNECTIVITY_ERROR_COUNT_NAME;
24+
import static com.google.cloud.bigtable.data.v2.stub.metrics.BuiltinMetricsConstants.FIRST_RESPONSE_LATENCIES_NAME;
2425
import static com.google.cloud.bigtable.data.v2.stub.metrics.BuiltinMetricsConstants.METHOD_KEY;
2526
import static com.google.cloud.bigtable.data.v2.stub.metrics.BuiltinMetricsConstants.OPERATION_LATENCIES_NAME;
2627
import static com.google.cloud.bigtable.data.v2.stub.metrics.BuiltinMetricsConstants.RETRY_COUNT_NAME;
@@ -121,6 +122,7 @@ public class BuiltinMetricsTracerTest {
121122
private static final String TABLE = "fake-table";
122123

123124
private static final String BAD_TABLE_ID = "non-exist-table";
125+
private static final String FIRST_RESPONSE_TABLE_ID = "first-response";
124126
private static final String ZONE = "us-west-1";
125127
private static final String CLUSTER = "cluster-0";
126128
private static final long FAKE_SERVER_TIMING = 50;
@@ -305,6 +307,52 @@ public void testReadRowsOperationLatenciesOnAuthorizedView() {
305307
assertThat(value).isIn(Range.closed(SERVER_LATENCY, elapsed));
306308
}
307309

310+
@Test
311+
public void testFirstResponseLatencies() {
312+
Stopwatch firstResponseTimer = Stopwatch.createStarted();
313+
stub.readRowsCallable()
314+
.call(
315+
Query.create(FIRST_RESPONSE_TABLE_ID),
316+
new ResponseObserver<Row>() {
317+
@Override
318+
public void onStart(StreamController controller) {}
319+
320+
@Override
321+
public void onResponse(Row response) {
322+
// Server sends back 2 responses for this test
323+
if (firstResponseTimer.isRunning()) {
324+
firstResponseTimer.stop();
325+
}
326+
try {
327+
Thread.sleep(100);
328+
} catch (InterruptedException e) {
329+
}
330+
}
331+
332+
@Override
333+
public void onError(Throwable t) {}
334+
335+
@Override
336+
public void onComplete() {}
337+
});
338+
339+
Attributes expectedAttributes =
340+
baseAttributes
341+
.toBuilder()
342+
.put(STATUS_KEY, "OK")
343+
.put(TABLE_ID_KEY, FIRST_RESPONSE_TABLE_ID)
344+
.put(ZONE_ID_KEY, ZONE)
345+
.put(CLUSTER_ID_KEY, CLUSTER)
346+
.put(METHOD_KEY, "Bigtable.ReadRows")
347+
.put(CLIENT_NAME_KEY, CLIENT_NAME)
348+
.build();
349+
350+
MetricData metricData = getMetricData(metricReader, FIRST_RESPONSE_LATENCIES_NAME);
351+
352+
long value = getAggregatedValue(metricData, expectedAttributes);
353+
assertThat(value).isAtMost(firstResponseTimer.elapsed(TimeUnit.MILLISECONDS));
354+
}
355+
308356
@Test
309357
public void testGfeMetrics() {
310358
Lists.newArrayList(stub.readRowsCallable().call(Query.create(TABLE)));
@@ -732,6 +780,12 @@ static List<ReadRowsResponse> createFakeResponse() {
732780
@Override
733781
public void readRows(
734782
ReadRowsRequest request, StreamObserver<ReadRowsResponse> responseObserver) {
783+
if (request.getTableName().contains(FIRST_RESPONSE_TABLE_ID)) {
784+
responseObserver.onNext(source.next());
785+
responseObserver.onNext(source.next());
786+
responseObserver.onCompleted();
787+
return;
788+
}
735789
if (request.getTableName().contains(BAD_TABLE_ID)) {
736790
responseObserver.onError(new StatusRuntimeException(Status.NOT_FOUND));
737791
return;

0 commit comments

Comments
 (0)