Skip to content

Commit c30a036

Browse files
Created cleanup method for tpu
1 parent 8515b5c commit c30a036

File tree

2 files changed

+50
-6
lines changed

2 files changed

+50
-6
lines changed

compute/cloud-client/src/test/java/compute/Util.java

Lines changed: 37 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,15 +34,21 @@
3434
import com.google.cloud.compute.v1.SnapshotsClient;
3535
import com.google.cloud.compute.v1.StoragePool;
3636
import com.google.cloud.compute.v1.StoragePoolsClient;
37+
import com.google.cloud.tpu.v2.Node;
38+
import com.google.cloud.tpu.v2.TpuClient;
39+
import com.google.protobuf.Timestamp;
3740
import compute.deleteprotection.SetDeleteProtection;
3841
import compute.disks.DeleteDisk;
3942
import compute.disks.DeleteSnapshot;
4043
import compute.reservation.DeleteReservation;
44+
import compute.tpu.DeleteTpuVm;
4145
import java.io.IOException;
4246
import java.nio.charset.StandardCharsets;
4347
import java.security.SecureRandom;
4448
import java.time.Instant;
4549
import java.time.OffsetDateTime;
50+
import java.time.ZoneOffset;
51+
import java.time.format.DateTimeFormatter;
4652
import java.time.temporal.ChronoUnit;
4753
import java.util.Base64;
4854
import java.util.Random;
@@ -57,7 +63,7 @@ public abstract class Util {
5763
// resources
5864
// and delete the listed resources based on the timestamp.
5965

60-
private static final int DELETION_THRESHOLD_TIME_MINUTES = 45;
66+
private static final int DELETION_THRESHOLD_TIME_MINUTES = 3;
6167
// comma separate list of zone names
6268
private static final String TEST_ZONES_NAME = "JAVA_DOCS_COMPUTE_TEST_ZONES";
6369
private static final String DEFAULT_ZONES = "us-central1-a,us-west1-a,asia-south1-a";
@@ -231,14 +237,31 @@ && isCreatedBeforeThresholdTime(snapshot.getCreationTimestamp())) {
231237
}
232238
}
233239

240+
// Delete tpu which starts with the given prefixToDelete and
241+
// has creation timestamp >24 hours.
242+
public static void cleanUpExistingTpu(String prefixToDelete, String projectId, String zone)
243+
throws IOException, ExecutionException, InterruptedException {
244+
try (TpuClient tpuClient = TpuClient.create()) {
245+
String parent = String.format("projects/%s/locations/%s", projectId, zone);
246+
for (Node node : tpuClient.listNodes(parent).iterateAll()) {
247+
String creationTime = formatTimestamp(node.getCreateTime());
248+
String name = node.getName().substring(node.getName().lastIndexOf("/") + 1);
249+
if (containPrefixToDeleteAndZone(node, prefixToDelete, zone)
250+
&& isCreatedBeforeThresholdTime(creationTime)) {
251+
DeleteTpuVm.deleteTpuVm(projectId, zone, name);
252+
}
253+
}
254+
}
255+
}
256+
234257
// Delete storagePools which starts with the given prefixToDelete and
235258
// has creation timestamp >24 hours.
236259
public static void cleanUpExistingStoragePool(
237260
String prefixToDelete, String projectId, String zone)
238261
throws IOException, ExecutionException, InterruptedException, TimeoutException {
239262
try (StoragePoolsClient storagePoolsClient = StoragePoolsClient.create()) {
240263
for (StoragePool storagePool : storagePoolsClient.list(projectId, zone).iterateAll()) {
241-
if (containPrefixToDeleteAndZone(projectId, prefixToDelete, zone)
264+
if (containPrefixToDeleteAndZone(storagePool, prefixToDelete, zone)
242265
&& isCreatedBeforeThresholdTime(storagePool.getCreationTimestamp())) {
243266
deleteStoragePool(projectId, zone, storagePool.getName());
244267
}
@@ -291,6 +314,10 @@ public static boolean containPrefixToDeleteAndZone(
291314
containPrefixAndZone = ((StoragePool) resource).getName().contains(prefixToDelete)
292315
&& ((StoragePool) resource).getZone().contains(zone);
293316
}
317+
if (resource instanceof Node) {
318+
containPrefixAndZone = ((Node) resource).getName().contains(prefixToDelete)
319+
&& ((Node) resource).getName().split("/")[3].contains(zone);
320+
}
294321
} catch (NullPointerException e) {
295322
System.out.println("Resource not found, skipping deletion:");
296323
}
@@ -312,4 +339,12 @@ public static boolean containPrefixToDelete(
312339
}
313340
return containPrefixToDelete;
314341
}
342+
343+
private static String formatTimestamp(Timestamp timestamp) {
344+
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss.SSSXXX");
345+
OffsetDateTime offsetDateTime = OffsetDateTime.ofInstant(
346+
Instant.ofEpochSecond(timestamp.getSeconds(), timestamp.getNanos()),
347+
ZoneOffset.UTC);
348+
return formatter.format(offsetDateTime);
349+
}
315350
}

compute/cloud-client/src/test/java/compute/tpu/TpuVmTest.java renamed to compute/cloud-client/src/test/java/compute/tpu/TpuVmIT.java

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,27 +26,31 @@
2626
import com.google.api.gax.rpc.NotFoundException;
2727
import com.google.cloud.tpu.v2.Node;
2828
import com.google.cloud.tpu.v2.TpuClient;
29+
import compute.Util;
2930
import java.io.ByteArrayOutputStream;
3031
import java.io.IOException;
3132
import java.io.PrintStream;
3233
import java.util.UUID;
3334
import java.util.concurrent.ExecutionException;
35+
import java.util.concurrent.TimeoutException;
3436
import org.junit.Assert;
3537
import org.junit.FixMethodOrder;
36-
import org.junit.Test;
3738
import org.junit.jupiter.api.AfterAll;
3839
import org.junit.jupiter.api.Assertions;
3940
import org.junit.jupiter.api.BeforeAll;
41+
import org.junit.jupiter.api.Test;
4042
import org.junit.runner.RunWith;
4143
import org.junit.runners.JUnit4;
4244
import org.junit.runners.MethodSorters;
4345

4446
@RunWith(JUnit4.class)
4547
@FixMethodOrder(MethodSorters.NAME_ASCENDING)
46-
public class TpuVmTest {
48+
public class TpuVmIT {
4749
private static final String PROJECT_ID = System.getenv("GOOGLE_CLOUD_PROJECT");
4850
private static final String ZONE = "europe-west4-a";
49-
private static final String TPU_VM_NAME = "test-tpu-" + UUID.randomUUID();
51+
static String javaVersion = System.getProperty("java.version").substring(0, 2);
52+
private static final String TPU_VM_NAME = "test-tpu-" + javaVersion + "-"
53+
+ UUID.randomUUID().toString().substring(0, 8);
5054
private static final String ACCELERATOR_TYPE = "v2-8";
5155
private static final String VERSION = "tpu-vm-tf-2.14.1";
5256
private static final String TPU_VM_PATH_NAME =
@@ -58,15 +62,20 @@ public static void requireEnvVar(String envVarName) {
5862
}
5963

6064
@BeforeAll
61-
public static void setUp() throws IOException, ExecutionException, InterruptedException {
65+
public static void setUp()
66+
throws IOException, ExecutionException, InterruptedException, TimeoutException {
6267
requireEnvVar("GOOGLE_APPLICATION_CREDENTIALS");
6368
requireEnvVar("GOOGLE_CLOUD_PROJECT");
69+
70+
// Cleanup existing stale resources.
71+
Util.cleanUpExistingTpu("test-tpu-" + javaVersion, PROJECT_ID, ZONE);
6472
}
6573

6674
@AfterAll
6775
public static void cleanup() throws Exception {
6876
DeleteTpuVm.deleteTpuVm(PROJECT_ID, ZONE, TPU_VM_NAME);
6977
assertNull(GetTpuVm.getTpuVm(PROJECT_ID, ZONE, TPU_VM_NAME));
78+
7079
// Test that reservations are deleted
7180
Assertions.assertThrows(
7281
NotFoundException.class,

0 commit comments

Comments
 (0)