Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/main' into kongwh-request-priori…
Browse files Browse the repository at this point in the history
…ty-patch-1

Change-Id: Ib9f2520b70cc570d76dbe04e5ecd1ae4a28ac8c5
  • Loading branch information
igorbernstein2 committed Sep 11, 2023
2 parents 761bab7 + e6cc5f6 commit 722e5ee
Show file tree
Hide file tree
Showing 14 changed files with 51 additions and 71 deletions.
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
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ public boolean equals(Object o) {
if (o == this) {
return true;
}
if (o != null || getClass() == o.getClass()) {
if (o != null && getClass() == o.getClass()) {
AppProfileName that = ((AppProfileName) o);
return Objects.equals(this.project, that.project)
&& Objects.equals(this.instance, that.instance)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,7 @@ public boolean equals(Object o) {
if (o == this) {
return true;
}
if (o != null || getClass() == o.getClass()) {
if (o != null && getClass() == o.getClass()) {
BackupName that = ((BackupName) o);
return Objects.equals(this.project, that.project)
&& Objects.equals(this.instance, that.instance)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ public boolean equals(Object o) {
if (o == this) {
return true;
}
if (o != null || getClass() == o.getClass()) {
if (o != null && getClass() == o.getClass()) {
ClusterName that = ((ClusterName) o);
return Objects.equals(this.project, that.project)
&& Objects.equals(this.instance, that.instance)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ public boolean equals(Object o) {
if (o == this) {
return true;
}
if (o != null || getClass() == o.getClass()) {
if (o != null && getClass() == o.getClass()) {
InstanceName that = ((InstanceName) o);
return Objects.equals(this.project, that.project)
&& Objects.equals(this.instance, that.instance);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ public boolean equals(Object o) {
if (o == this) {
return true;
}
if (o != null || getClass() == o.getClass()) {
if (o != null && getClass() == o.getClass()) {
LocationName that = ((LocationName) o);
return Objects.equals(this.project, that.project)
&& Objects.equals(this.location, that.location);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ public boolean equals(Object o) {
if (o == this) {
return true;
}
if (o != null || getClass() == o.getClass()) {
if (o != null && getClass() == o.getClass()) {
ProjectName that = ((ProjectName) o);
return Objects.equals(this.project, that.project);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,7 @@ public boolean equals(Object o) {
if (o == this) {
return true;
}
if (o != null || getClass() == o.getClass()) {
if (o != null && getClass() == o.getClass()) {
SnapshotName that = ((SnapshotName) o);
return Objects.equals(this.project, that.project)
&& Objects.equals(this.instance, that.instance)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ public boolean equals(Object o) {
if (o == this) {
return true;
}
if (o != null || getClass() == o.getClass()) {
if (o != null && getClass() == o.getClass()) {
TableName that = ((TableName) o);
return Objects.equals(this.project, that.project)
&& Objects.equals(this.instance, that.instance)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ public boolean equals(Object o) {
if (o == this) {
return true;
}
if (o != null || getClass() == o.getClass()) {
if (o != null && getClass() == o.getClass()) {
InstanceName that = ((InstanceName) o);
return Objects.equals(this.project, that.project)
&& Objects.equals(this.instance, that.instance);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ public boolean equals(Object o) {
if (o == this) {
return true;
}
if (o != null || getClass() == o.getClass()) {
if (o != null && getClass() == o.getClass()) {
TableName that = ((TableName) o);
return Objects.equals(this.project, that.project)
&& Objects.equals(this.instance, that.instance)
Expand Down

0 comments on commit 722e5ee

Please sign in to comment.