|
59 | 59 | import java.util.concurrent.atomic.AtomicInteger; |
60 | 60 | import java.util.logging.Level; |
61 | 61 | import java.util.logging.Logger; |
| 62 | +import java.util.regex.Matcher; |
| 63 | +import java.util.regex.Pattern; |
62 | 64 | import org.junit.After; |
63 | 65 | import org.junit.Before; |
64 | 66 | import org.junit.ClassRule; |
@@ -98,6 +100,40 @@ public void setUp() throws Exception { |
98 | 100 | instanceAdminClient = testHelper.getClient().getInstanceAdminClient(); |
99 | 101 | instance = instanceAdminClient.getInstance(testHelper.getInstanceId().getInstance()); |
100 | 102 | logger.info("Finished setup"); |
| 103 | + |
| 104 | + // Cancel any backup operation that has been started by this integration test if it has been |
| 105 | + // running for at least 6 hours. |
| 106 | + logger.info("Cancelling long-running test backup operations"); |
| 107 | + Pattern pattern = Pattern.compile(".*/backups/testbck_\\d{6}_\\d{4}_bck\\d/operations/.*"); |
| 108 | + try { |
| 109 | + for (Operation operation : |
| 110 | + dbAdminClient.listBackupOperations(instance.getId().getInstance()).iterateAll()) { |
| 111 | + Matcher matcher = pattern.matcher(operation.getName()); |
| 112 | + if (matcher.matches()) { |
| 113 | + if (!operation.getDone()) { |
| 114 | + Timestamp currentTime = Timestamp.now(); |
| 115 | + Timestamp startTime = |
| 116 | + Timestamp.fromProto( |
| 117 | + operation |
| 118 | + .getMetadata() |
| 119 | + .unpack(CreateBackupMetadata.class) |
| 120 | + .getProgress() |
| 121 | + .getStartTime()); |
| 122 | + long diffSeconds = currentTime.getSeconds() - startTime.getSeconds(); |
| 123 | + if (TimeUnit.HOURS.convert(diffSeconds, TimeUnit.SECONDS) >= 6L) { |
| 124 | + logger.warning( |
| 125 | + String.format( |
| 126 | + "Cancelling test backup operation %s that was started at %s", |
| 127 | + operation.getName(), startTime.toString())); |
| 128 | + dbAdminClient.cancelOperation(operation.getName()); |
| 129 | + } |
| 130 | + } |
| 131 | + } |
| 132 | + } |
| 133 | + } catch (InvalidProtocolBufferException e) { |
| 134 | + logger.log(Level.WARNING, "Could not list all existing backup operations.", e); |
| 135 | + } |
| 136 | + logger.info("Finished checking existing test backup operations"); |
101 | 137 | } |
102 | 138 |
|
103 | 139 | @After |
@@ -242,26 +278,6 @@ public void testBackups() throws InterruptedException, ExecutionException { |
242 | 278 | } catch (TimeoutException e) { |
243 | 279 | logger.warning( |
244 | 280 | "Waiting for backup operations to finish timed out. Getting long-running operations."); |
245 | | - |
246 | | - try { |
247 | | - for (Operation operation : |
248 | | - dbAdminClient.listBackupOperations(instance.getId().getInstance()).iterateAll()) { |
249 | | - logger.info( |
250 | | - String.format( |
251 | | - "Existing backup operation: %s, done: %s, end time: %s", |
252 | | - operation.getName(), |
253 | | - String.valueOf(operation.getDone()), |
254 | | - operation |
255 | | - .getMetadata() |
256 | | - .unpack(CreateBackupMetadata.class) |
257 | | - .getProgress() |
258 | | - .getEndTime() |
259 | | - .toString())); |
260 | | - } |
261 | | - } catch (InvalidProtocolBufferException ipbe) { |
262 | | - logger.log(Level.WARNING, "Could not list all existing backup operations.", ipbe); |
263 | | - } |
264 | | - |
265 | 281 | while (watch.elapsed(TimeUnit.MINUTES) < 12L |
266 | 282 | && (!dbAdminClient.getOperation(op1.getName()).getDone() |
267 | 283 | || !dbAdminClient.getOperation(op2.getName()).getDone())) { |
|
0 commit comments