Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: aggregate batching throttling latency per attempt and reset it between #1905

Merged
merged 2 commits into from
Sep 11, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -273,7 +273,8 @@ private void recordAttemptCompletion(@Nullable Throwable status) {
}
}

recorder.putClientBlockingLatencies(totalClientBlockingTime.get());
// Make sure to reset the blocking time after recording it for the next attempt
recorder.putClientBlockingLatencies(totalClientBlockingTime.getAndSet(0));

// Patch the status until it's fixed in gax. When an attempt failed,
// it'll throw a ServerStreamingAttemptException. Unwrap the exception
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,9 @@ class MetricsTracer extends BigtableTracer {

private volatile int attempt = 0;

private volatile boolean reportBatchingLatency = false;
private volatile long batchThrottledLatency = 0;

MetricsTracer(
OperationType operationType,
Tagger tagger,
Expand Down Expand Up @@ -167,6 +170,14 @@ private void recordAttemptCompletion(@Nullable Throwable throwable) {
RpcMeasureConstants.BIGTABLE_ATTEMPT_LATENCY,
attemptTimer.elapsed(TimeUnit.MILLISECONDS));

if (reportBatchingLatency) {
measures.put(RpcMeasureConstants.BIGTABLE_BATCH_THROTTLED_TIME, batchThrottledLatency);

// Reset batch throttling latency for next attempt. This can't be done in attemptStarted
// because batching flow control will add batching latency before the attempt has started.
batchThrottledLatency = 0;
}

// Patch the throwable until it's fixed in gax. When an attempt failed,
// it'll throw a ServerStreamingAttemptException. Unwrap the exception
// so it could get processed by extractStatus
Expand Down Expand Up @@ -216,11 +227,8 @@ public void recordGfeMetadata(@Nullable Long latency, @Nullable Throwable throwa

@Override
public void batchRequestThrottled(long totalThrottledMs) {
MeasureMap measures =
stats
.newMeasureMap()
.put(RpcMeasureConstants.BIGTABLE_BATCH_THROTTLED_TIME, totalThrottledMs);
measures.record(newTagCtxBuilder().build());
reportBatchingLatency = true;
batchThrottledLatency += totalThrottledMs;
}

private TagContextBuilder newTagCtxBuilder() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,8 @@
import static org.mockito.Mockito.doAnswer;
import static org.mockito.Mockito.when;

import com.google.api.gax.batching.BatchResource;
import com.google.api.gax.batching.Batcher;
import com.google.api.gax.batching.BatcherImpl;
import com.google.api.gax.batching.BatchingDescriptor;
import com.google.api.gax.batching.FlowController;
import com.google.api.gax.grpc.GrpcCallContext;
import com.google.api.gax.rpc.ApiCallContext;
Expand Down Expand Up @@ -387,45 +385,38 @@ public Object answer(InvocationOnMock invocation) {
.when(mockService)
.readRows(any(ReadRowsRequest.class), any());

try (Batcher batcher =
try (Batcher<ByteString, Row> batcher =
stub.newBulkReadRowsBatcher(Query.create(TABLE_ID), GrpcCallContext.createDefault())) {
batcher.add(ByteString.copyFromUtf8("row1"));
batcher.sendOutstanding();

long throttledTimeMetric =
StatsTestUtils.getAggregationValueAsLong(
localStats,
RpcViewConstants.BIGTABLE_BATCH_THROTTLED_TIME_VIEW,
ImmutableMap.of(
RpcMeasureConstants.BIGTABLE_OP, TagValue.create("Bigtable.ReadRows")),
PROJECT_ID,
INSTANCE_ID,
APP_PROFILE_ID);
assertThat(throttledTimeMetric).isEqualTo(0);
}

long throttledTimeMetric =
StatsTestUtils.getAggregationValueAsLong(
localStats,
RpcViewConstants.BIGTABLE_BATCH_THROTTLED_TIME_VIEW,
ImmutableMap.of(RpcMeasureConstants.BIGTABLE_OP, TagValue.create("Bigtable.ReadRows")),
PROJECT_ID,
INSTANCE_ID,
APP_PROFILE_ID);
assertThat(throttledTimeMetric).isEqualTo(0);
}

@Test
public void testBatchMutateRowsThrottledTime() throws Exception {
FlowController flowController = Mockito.mock(FlowController.class);
BatchingDescriptor batchingDescriptor = Mockito.mock(MutateRowsBatchingDescriptor.class);
when(batchingDescriptor.createResource(any())).thenReturn(new FakeBatchResource());
when(batchingDescriptor.createEmptyResource()).thenReturn(new FakeBatchResource());
MutateRowsBatchingDescriptor batchingDescriptor = new MutateRowsBatchingDescriptor();

// Mock throttling
final long throttled = 50;
doAnswer(
new Answer() {
@Override
public Object answer(InvocationOnMock invocation) throws Throwable {
Thread.sleep(throttled);
return null;
}
invocation -> {
Thread.sleep(throttled);
return null;
})
.when(flowController)
.reserve(any(Long.class), any(Long.class));
when(flowController.getMaxElementCountLimit()).thenReturn(null);
when(flowController.getMaxRequestBytesLimit()).thenReturn(null);
when(batchingDescriptor.newRequestBuilder(any())).thenCallRealMethod();

doAnswer(
new Answer() {
Expand All @@ -444,18 +435,18 @@ public Object answer(InvocationOnMock invocation) {

ApiCallContext defaultContext = GrpcCallContext.createDefault();

Batcher batcher =
new BatcherImpl(
try (Batcher<RowMutationEntry, Void> batcher =
new BatcherImpl<>(
batchingDescriptor,
stub.bulkMutateRowsCallable().withDefaultCallContext(defaultContext),
BulkMutation.create(TABLE_ID),
settings.getStubSettings().bulkMutateRowsSettings().getBatchingSettings(),
Executors.newSingleThreadScheduledExecutor(),
flowController,
defaultContext);
defaultContext)) {

batcher.add(RowMutationEntry.create("key"));
batcher.sendOutstanding();
batcher.add(RowMutationEntry.create("key").deleteRow());
}

long throttledTimeMetric =
StatsTestUtils.getAggregationValueAsLong(
Expand All @@ -473,29 +464,4 @@ public Object answer(InvocationOnMock invocation) {
private static <T> StreamObserver<T> anyObserver(Class<T> returnType) {
return (StreamObserver<T>) any(returnType);
}

private class FakeBatchResource implements BatchResource {

FakeBatchResource() {}

@Override
public BatchResource add(BatchResource resource) {
return new FakeBatchResource();
}

@Override
public long getElementCount() {
return 1;
}

@Override
public long getByteCount() {
return 1;
}

@Override
public boolean shouldFlush(long maxElementThreshold, long maxBytesThreshold) {
return false;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -299,6 +299,11 @@ public static long getAggregationValueAsLong(

AggregationData aggregationData = aggregationMap.get(tagValues);

if (aggregationData == null) {
throw new RuntimeException(
"Failed to find metric for: " + tags + ". Current aggregation data: " + aggregationMap);
}

return aggregationData.match(
new io.opencensus.common.Function<AggregationData.SumDataDouble, Long>() {
@Override
Expand Down