Skip to content

Commit 7eaa1aa

Browse files
authored
test: wait and retry after RESOURCE_EXHAUSTED in tests (#4159)
The ListDatabases RPC is an administrative RPC. This can return a RESOURCE_EXHAUSTED error if too many administrative RPCs are being executed for the same project in a short period of time. Back-off and retry if that happens.
1 parent b32ebcf commit 7eaa1aa

File tree

1 file changed

+35
-16
lines changed

1 file changed

+35
-16
lines changed

google-cloud-spanner/src/test/java/com/google/cloud/spanner/IntegrationTestEnv.java

Lines changed: 35 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -298,25 +298,44 @@ private void cleanUpOldDatabases(InstanceId instanceId) {
298298
String TEST_DB_REGEX = "(testdb_(.*)_(.*))|(mysample-(.*))";
299299

300300
logger.log(Level.INFO, "Dropping old test databases from {0}", instanceId.getName());
301-
for (Database db : databaseAdminClient.listDatabases(instanceId.getInstance()).iterateAll()) {
301+
while (true) {
302302
try {
303-
long timeDiff = currentTimestamp.getSeconds() - db.getCreateTime().getSeconds();
304-
// Delete all databases which are more than OLD_DB_THRESHOLD_SECS seconds old.
305-
if ((db.getId().getDatabase().matches(TEST_DB_REGEX))
306-
&& (timeDiff > OLD_DB_THRESHOLD_SECS)) {
307-
logger.log(Level.INFO, "Dropping test database {0}", db.getId());
308-
if (db.isDropProtectionEnabled()) {
309-
Database updatedDatabase =
310-
databaseAdminClient.newDatabaseBuilder(db.getId()).disableDropProtection().build();
311-
databaseAdminClient
312-
.updateDatabase(updatedDatabase, DatabaseField.DROP_PROTECTION)
313-
.get();
303+
for (Database db :
304+
databaseAdminClient.listDatabases(instanceId.getInstance()).iterateAll()) {
305+
try {
306+
long timeDiff = currentTimestamp.getSeconds() - db.getCreateTime().getSeconds();
307+
// Delete all databases which are more than OLD_DB_THRESHOLD_SECS seconds old.
308+
if ((db.getId().getDatabase().matches(TEST_DB_REGEX))
309+
&& (timeDiff > OLD_DB_THRESHOLD_SECS)) {
310+
logger.log(Level.INFO, "Dropping test database {0}", db.getId());
311+
if (db.isDropProtectionEnabled()) {
312+
Database updatedDatabase =
313+
databaseAdminClient
314+
.newDatabaseBuilder(db.getId())
315+
.disableDropProtection()
316+
.build();
317+
databaseAdminClient
318+
.updateDatabase(updatedDatabase, DatabaseField.DROP_PROTECTION)
319+
.get();
320+
}
321+
db.drop();
322+
++numDropped;
323+
}
324+
} catch (SpannerException | ExecutionException | InterruptedException e) {
325+
logger.log(Level.SEVERE, "Failed to drop test database " + db.getId(), e);
314326
}
315-
db.drop();
316-
++numDropped;
317327
}
318-
} catch (SpannerException | ExecutionException | InterruptedException e) {
319-
logger.log(Level.SEVERE, "Failed to drop test database " + db.getId(), e);
328+
break;
329+
} catch (SpannerException exception) {
330+
if (exception.getErrorCode() != ErrorCode.RESOURCE_EXHAUSTED) {
331+
throw exception;
332+
}
333+
// Wait a little and try again.
334+
try {
335+
Thread.sleep(10_000);
336+
} catch (InterruptedException interruptedException) {
337+
throw SpannerExceptionFactory.propagateInterrupt(interruptedException);
338+
}
320339
}
321340
}
322341
logger.log(Level.INFO, "Dropped {0} test database(s)", numDropped);

0 commit comments

Comments
 (0)