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

feat: improve batching summary errors #2509

Merged
merged 5 commits into from
Feb 29, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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 @@ -31,7 +31,9 @@

import com.google.api.gax.rpc.ApiException;
import com.google.api.gax.rpc.StatusCode.Code;
import com.google.common.base.Joiner;
import com.google.common.base.MoreObjects;
import com.google.common.collect.EvictingQueue;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
Expand All @@ -52,6 +54,9 @@ class BatcherStats {
private final Map<Class, Integer> entryExceptionCounts = new HashMap<>();
private final Map<Code, Integer> entryStatusCounts = new HashMap<>();

private final EvictingQueue<String> sampleOfRpcErrors = EvictingQueue.create(Integer.getInteger("com.google.api.gax.batching.errors.max-samples", 50));
private final EvictingQueue<String> sampleOfEntryErrors = EvictingQueue.create(Integer.getInteger("com.google.api.gax.batching.errors.max-samples", 50));

/**
* Records the count of the exception and it's type when a complete batch failed to apply.
*
Expand All @@ -69,6 +74,8 @@ synchronized void recordBatchFailure(Throwable throwable) {
requestStatusCounts.put(code, oldStatusCount + 1);
}

sampleOfRpcErrors.add(throwable.toString());

int oldExceptionCount = MoreObjects.firstNonNull(requestExceptionCounts.get(exceptionClass), 0);
requestExceptionCounts.put(exceptionClass, oldExceptionCount + 1);
}
Expand Down Expand Up @@ -96,6 +103,8 @@ synchronized <T extends BatchEntry> void recordBatchElementsCompletion(
Throwable actualCause = throwable.getCause();
Class exceptionClass = actualCause.getClass();

sampleOfEntryErrors.add(actualCause.toString());

if (actualCause instanceof ApiException) {
Code code = ((ApiException) actualCause).getStatusCode().getCode();
exceptionClass = ApiException.class;
Expand Down Expand Up @@ -144,6 +153,17 @@ synchronized BatchingException asException() {
.append(buildExceptionList(entryExceptionCounts, entryStatusCounts))
.append(".");
}

if (!sampleOfRpcErrors.isEmpty()) {
messageBuilder.append(" Sample of RPC errors: ");
messageBuilder.append(Joiner.on(", ").join(sampleOfRpcErrors));
messageBuilder.append(".");
}
if (!sampleOfEntryErrors.isEmpty()) {
messageBuilder.append(" Sample of entry errors: ");
messageBuilder.append(Joiner.on(", ").join(sampleOfEntryErrors));
messageBuilder.append(".");
}
return new BatchingException(messageBuilder.toString());
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ public void testRequestFailuresOnly() {

batcherStats.recordBatchFailure(
ApiExceptionFactory.createException(
new RuntimeException(), FakeStatusCode.of(StatusCode.Code.INVALID_ARGUMENT), false));
"fake api error", new RuntimeException(), FakeStatusCode.of(StatusCode.Code.INVALID_ARGUMENT), false));

batcherStats.recordBatchFailure(new RuntimeException("Request failed"));

Expand All @@ -65,6 +65,8 @@ public void testRequestFailuresOnly() {
assertThat(exception).hasMessageThat().contains("1 RuntimeException");
assertThat(exception).hasMessageThat().contains("1 ApiException(1 INVALID_ARGUMENT)");
assertThat(exception).hasMessageThat().contains("and 0 partial failures.");
assertThat(exception).hasMessageThat().contains("com.google.api.gax.rpc.InvalidArgumentException: fake api error, java.lang.RuntimeException: Request failed.");

}

@Test
Expand All @@ -79,7 +81,7 @@ public void testEntryFailureOnly() {
SettableApiFuture<Integer> batchTwoResult = SettableApiFuture.create();
batchTwoResult.setException(
ApiExceptionFactory.createException(
new RuntimeException(), FakeStatusCode.of(StatusCode.Code.UNAVAILABLE), false));
"fake entry error", new RuntimeException(), FakeStatusCode.of(StatusCode.Code.UNAVAILABLE), false));
batcherStats.recordBatchElementsCompletion(
ImmutableList.of(BatchEntry.create(2, batchTwoResult)));

Expand All @@ -89,6 +91,7 @@ public void testEntryFailureOnly() {
.contains("The 2 partial failures contained 2 entries that failed with:");
assertThat(ex).hasMessageThat().contains("1 ApiException(1 UNAVAILABLE)");
assertThat(ex).hasMessageThat().contains("1 IllegalStateException");
assertThat(ex).hasMessageThat().contains("Sample of entry errors: java.lang.IllegalStateException: local element failure, com.google.api.gax.rpc.UnavailableException: fake entry error.");
}

@Test
Expand All @@ -110,6 +113,8 @@ public void testRequestAndEntryFailures() {
.contains(
"Batching finished with 1 batches failed to apply due to: 1 RuntimeException and 1 "
+ "partial failures. The 1 partial failures contained 1 entries that failed with:"
+ " 1 ApiException(1 ALREADY_EXISTS).");
+ " 1 ApiException(1 ALREADY_EXISTS)."
+ " Sample of RPC errors: java.lang.RuntimeException: Batch failure."
+ " Sample of entry errors: com.google.api.gax.rpc.AlreadyExistsException: java.lang.RuntimeException.");
}
}
Loading