@@ -141,8 +141,7 @@ protected void before() throws Throwable {
141141 .anyMatch (testEnvOption -> TestEnvOptions .USE_END_TO_END_TRACING .equals (testEnvOption ))) {
142142 // OpenTelemetry set up for enabling End to End tracing for all integration test env.
143143 // The gRPC stub and connections are created during test env set up using SpannerOptions and
144- // are
145- // reused for executing statements.
144+ // are reused for executing statements.
146145 options = spannerOptionsWithEndToEndTracing (options );
147146 }
148147 String instanceProperty = System .getProperty (TEST_INSTANCE_PROPERTY , "" );
@@ -293,31 +292,50 @@ static boolean isRetryableResourceExhaustedException(SpannerException exception)
293292 }
294293
295294 private void cleanUpOldDatabases (InstanceId instanceId ) {
296- long OLD_DB_THRESHOLD_SECS = TimeUnit .SECONDS .convert (6L , TimeUnit .HOURS );
295+ long OLD_DB_THRESHOLD_SECS = TimeUnit .SECONDS .convert (2L , TimeUnit .HOURS );
297296 Timestamp currentTimestamp = Timestamp .now ();
298297 int numDropped = 0 ;
299298 String TEST_DB_REGEX = "(testdb_(.*)_(.*))|(mysample-(.*))" ;
300299
301300 logger .log (Level .INFO , "Dropping old test databases from {0}" , instanceId .getName ());
302- for ( Database db : databaseAdminClient . listDatabases ( instanceId . getInstance ()). iterateAll () ) {
301+ while ( true ) {
303302 try {
304- long timeDiff = currentTimestamp .getSeconds () - db .getCreateTime ().getSeconds ();
305- // Delete all databases which are more than OLD_DB_THRESHOLD_SECS seconds old.
306- if ((db .getId ().getDatabase ().matches (TEST_DB_REGEX ))
307- && (timeDiff > OLD_DB_THRESHOLD_SECS )) {
308- logger .log (Level .INFO , "Dropping test database {0}" , db .getId ());
309- if (db .isDropProtectionEnabled ()) {
310- Database updatedDatabase =
311- databaseAdminClient .newDatabaseBuilder (db .getId ()).disableDropProtection ().build ();
312- databaseAdminClient
313- .updateDatabase (updatedDatabase , DatabaseField .DROP_PROTECTION )
314- .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 );
315326 }
316- db .drop ();
317- ++numDropped ;
318327 }
319- } catch (SpannerException | ExecutionException | InterruptedException e ) {
320- 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+ }
321339 }
322340 }
323341 logger .log (Level .INFO , "Dropped {0} test database(s)" , numDropped );
0 commit comments