Skip to content

Commit 6c24845

Browse files
authored
test: retry backup operation 20 times (#1117)
The backup operations can still fail because of too many pending operations. It would retry 10 times with waiting 60 seconds between each attempt, but that does not seem to be enough. The max attempts has therefore been increased to 20 and better logging has been created by throwing a custom exception when it happens. The previous log was not visible, because the sample runners are redirecting the standard out to a string. Fixes #1019
1 parent 7001030 commit 6c24845

File tree

1 file changed

+8
-5
lines changed

1 file changed

+8
-5
lines changed

samples/snippets/src/test/java/com/example/spanner/EncryptionKeyIT.java

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
import com.google.cloud.spanner.ErrorCode;
2323
import com.google.cloud.spanner.Spanner;
2424
import com.google.cloud.spanner.SpannerException;
25+
import com.google.cloud.spanner.SpannerExceptionFactory;
2526
import com.google.cloud.spanner.SpannerOptions;
2627
import com.google.common.base.Preconditions;
2728
import com.google.common.util.concurrent.Uninterruptibles;
@@ -138,8 +139,8 @@ public void testEncryptedDatabaseAndBackupAndRestore() throws Exception {
138139
+ "/backups/" + backupId + " using encryption key " + key);
139140
}
140141

141-
private static class ShouldRetryBackupOperation implements Predicate<SpannerException> {
142-
private static final int MAX_ATTEMPTS = 10;
142+
static class ShouldRetryBackupOperation implements Predicate<SpannerException> {
143+
private static final int MAX_ATTEMPTS = 20;
143144
private int attempts = 0;
144145

145146
@Override
@@ -148,9 +149,11 @@ public boolean test(SpannerException e) {
148149
&& e.getMessage().contains("Please retry the operation once the pending")) {
149150
attempts++;
150151
if (attempts == MAX_ATTEMPTS) {
151-
System.out.printf("Operation failed %d times because of other pending operations. "
152-
+ "Giving up operation.\n", attempts);
153-
return false;
152+
// Throw custom exception so it is easier to locate in the log why it went wrong.
153+
throw SpannerExceptionFactory.newSpannerException(ErrorCode.DEADLINE_EXCEEDED,
154+
String.format("Operation failed %d times because of other pending operations. "
155+
+ "Giving up operation.\n", attempts),
156+
e);
154157
}
155158
// Wait one minute before retrying.
156159
Uninterruptibles.sleepUninterruptibly(60L, TimeUnit.SECONDS);

0 commit comments

Comments
 (0)