Skip to content

Commit 2253b54

Browse files
Fixed DeleteTpuVm and naming
1 parent d3e1dee commit 2253b54

File tree

6 files changed

+86
-32
lines changed

6 files changed

+86
-32
lines changed

tpu/src/main/java/tpu/CreateTpuVm.java

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -29,33 +29,43 @@ public class CreateTpuVm {
2929
public static void main(String[] args)
3030
throws IOException, ExecutionException, InterruptedException {
3131
// TODO(developer): Replace these variables before running the sample.
32+
// Project ID or project number of the Google Cloud project you want to create a node.
3233
String projectId = "YOUR_PROJECT_ID";
34+
// The zone in which to create the TPU.
35+
// For more information about supported TPU types for specific zones,
36+
// see https://cloud.google.com/tpu/docs/regions-zones
3337
String zone = "europe-west4-a";
34-
String tpuVmName = "YOUR_TPU_NAME";
35-
String acceleratorType = "v2-8";
36-
String version = "tpu-vm-tf-2.14.1";
38+
// The name for your TPU.
39+
String nodeName = "YOUR_TPY_NAME";
40+
// The accelerator type that specifies the version and size of the Cloud TPU you want to create.
41+
// For more information about supported accelerator types for each TPU version,
42+
// see https://cloud.google.com/tpu/docs/system-architecture-tpu-vm#versions.
43+
String tpuType = "v2-8";
44+
// Software version that specifies the version of the TPU runtime to install.
45+
// For more information see https://cloud.google.com/tpu/docs/runtimes
46+
String tpuSoftwareVersion = "tpu-vm-tf-2.14.1";
3747

38-
createTpuVm(projectId, zone, tpuVmName, acceleratorType, version);
48+
createTpuVm(projectId, zone, nodeName, tpuType, tpuSoftwareVersion);
3949
}
4050

4151
// Creates a TPU VM with the specified name, zone, accelerator type, and version.
4252
public static void createTpuVm(
43-
String projectId, String zone, String tpuVmName, String acceleratorType, String version)
53+
String projectId, String zone, String nodeName, String tpuType, String tpuSoftwareVersion)
4454
throws IOException, ExecutionException, InterruptedException {
4555
// Initialize client that will be used to send requests. This client only needs to be created
4656
// once, and can be reused for multiple requests.
4757
try (TpuClient tpuClient = TpuClient.create()) {
4858
String parent = String.format("projects/%s/locations/%s", projectId, zone);
4959

5060
Node tpuVm = Node.newBuilder()
51-
.setName(tpuVmName)
52-
.setAcceleratorType(acceleratorType)
53-
.setRuntimeVersion(version)
61+
.setName(nodeName)
62+
.setAcceleratorType(tpuType)
63+
.setRuntimeVersion(tpuSoftwareVersion)
5464
.build();
5565

5666
CreateNodeRequest request = CreateNodeRequest.newBuilder()
5767
.setParent(parent)
58-
.setNodeId(tpuVmName)
68+
.setNodeId(nodeName)
5969
.setNode(tpuVm)
6070
.build();
6171

tpu/src/main/java/tpu/DeleteTpuVm.java

Lines changed: 33 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -18,37 +18,62 @@
1818

1919
//[START tpu_vm_delete]
2020

21+
import com.google.api.gax.longrunning.OperationTimedPollAlgorithm;
22+
import com.google.api.gax.retrying.RetrySettings;
2123
import com.google.cloud.tpu.v2.DeleteNodeRequest;
2224
import com.google.cloud.tpu.v2.NodeName;
2325
import com.google.cloud.tpu.v2.TpuClient;
26+
import com.google.cloud.tpu.v2.TpuSettings;
2427
import java.io.IOException;
2528
import java.util.concurrent.ExecutionException;
29+
import org.threeten.bp.Duration;
2630

2731
public class DeleteTpuVm {
2832

2933
public static void main(String[] args)
3034
throws IOException, ExecutionException, InterruptedException {
3135
// TODO(developer): Replace these variables before running the sample.
36+
// Project ID or project number of the Google Cloud project you want to create a node.
3237
String projectId = "YOUR_PROJECT_ID";
38+
// The zone in which to create the TPU.
39+
// For more information about supported TPU types for specific zones,
40+
// see https://cloud.google.com/tpu/docs/regions-zones
3341
String zone = "europe-west4-a";
34-
String tpuVmName = "YOUR_TPU_NAME";
42+
// The name for your TPU.
43+
String nodeName = "YOUR_TPY_NAME";
3544

36-
deleteTpuVm(projectId, zone, tpuVmName);
45+
deleteTpuVm(projectId, zone, nodeName);
3746
}
3847

3948
// Deletes a TPU VM with the specified name in the given project and zone.
40-
public static void deleteTpuVm(String projectId, String zone, String tpuVmName)
49+
public static void deleteTpuVm(String projectId, String zone, String nodeName)
4150
throws IOException, ExecutionException, InterruptedException {
51+
TpuSettings.Builder clientSettings =
52+
TpuSettings.newBuilder();
53+
clientSettings
54+
.deleteNodeOperationSettings()
55+
.setPollingAlgorithm(
56+
OperationTimedPollAlgorithm.create(
57+
RetrySettings.newBuilder()
58+
.setInitialRetryDelay(Duration.ofMillis(5000L))
59+
.setRetryDelayMultiplier(1.5)
60+
.setMaxRetryDelay(Duration.ofMillis(45000L))
61+
.setInitialRpcTimeout(Duration.ZERO)
62+
.setRpcTimeoutMultiplier(1.0)
63+
.setMaxRpcTimeout(Duration.ZERO)
64+
.setTotalTimeout(Duration.ofHours(24L))
65+
.build()));
66+
4267
// Initialize client that will be used to send requests. This client only needs to be created
4368
// once, and can be reused for multiple requests.
44-
try (TpuClient tpuClient = TpuClient.create()) {
45-
String nodeName = NodeName.of(projectId, zone, tpuVmName).toString();
69+
try (TpuClient tpuClient = TpuClient.create(clientSettings.build())) {
70+
String name = NodeName.of(projectId, zone, nodeName).toString();
4671

47-
DeleteNodeRequest request = DeleteNodeRequest.newBuilder().setName(nodeName).build();
48-
tpuClient.deleteNodeAsync(request).get();
72+
DeleteNodeRequest request = DeleteNodeRequest.newBuilder().setName(name).build();
4973

74+
tpuClient.deleteNodeAsync(request).get();
5075
System.out.println("TPU VM deleted");
5176
}
5277
}
5378
}
54-
//[END tpu_vm_delete]
79+
//[END tpu_vm_delete]

tpu/src/main/java/tpu/GetTpuVm.java

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -28,23 +28,28 @@ public class GetTpuVm {
2828

2929
public static void main(String[] args) throws IOException {
3030
// TODO(developer): Replace these variables before running the sample.
31+
// Project ID or project number of the Google Cloud project you want to create a node.
3132
String projectId = "YOUR_PROJECT_ID";
33+
// The zone in which to create the TPU.
34+
// For more information about supported TPU types for specific zones,
35+
// see https://cloud.google.com/tpu/docs/regions-zones
3236
String zone = "europe-west4-a";
33-
String tpuVmName = "YOUR_TPU_NAME";
37+
// The name for your TPU.
38+
String nodeName = "YOUR_TPY_NAME";
3439

35-
getTpuVm(projectId, zone, tpuVmName);
40+
getTpuVm(projectId, zone, nodeName);
3641
}
3742

3843
// Describes a TPU VM with the specified name in the given project and zone.
39-
public static Node getTpuVm(String projectId, String zone, String tpuVmName)
44+
public static Node getTpuVm(String projectId, String zone, String nodeName)
4045
throws IOException {
4146
Node node;
4247
// Initialize client that will be used to send requests. This client only needs to be created
4348
// once, and can be reused for multiple requests.
4449
try (TpuClient tpuClient = TpuClient.create()) {
45-
String nodeName = NodeName.of(projectId, zone, tpuVmName).toString();
50+
String name = NodeName.of(projectId, zone, nodeName).toString();
4651

47-
GetNodeRequest request = GetNodeRequest.newBuilder().setName(nodeName).build();
52+
GetNodeRequest request = GetNodeRequest.newBuilder().setName(name).build();
4853
node = tpuClient.getNode(request);
4954
}
5055
return node;

tpu/src/main/java/tpu/ListTpuVms.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,11 @@ public class ListTpuVms {
2727

2828
public static void main(String[] args) throws IOException {
2929
// TODO(developer): Replace these variables before running the sample.
30+
// Project ID or project number of the Google Cloud project you want to create a node.
3031
String projectId = "YOUR_PROJECT_ID";
32+
// The zone in which to create the TPU.
33+
// For more information about supported TPU types for specific zones,
34+
// see https://cloud.google.com/tpu/docs/regions-zones
3135
String zone = "europe-west4-a";
3236

3337
listTpuVms(projectId, zone);

tpu/src/main/java/tpu/StartTpuVm.java

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -30,22 +30,27 @@ public class StartTpuVm {
3030
public static void main(String[] args)
3131
throws IOException, ExecutionException, InterruptedException {
3232
// TODO(developer): Replace these variables before running the sample.
33+
// Project ID or project number of the Google Cloud project you want to create a node.
3334
String projectId = "YOUR_PROJECT_ID";
35+
// The zone in which to create the TPU.
36+
// For more information about supported TPU types for specific zones,
37+
// see https://cloud.google.com/tpu/docs/regions-zones
3438
String zone = "europe-west4-a";
35-
String tpuVmName = "YOUR_TPU_NAME";
39+
// The name for your TPU.
40+
String nodeName = "YOUR_TPY_NAME";
3641

37-
startTpuVm(projectId, zone, tpuVmName);
42+
startTpuVm(projectId, zone, nodeName);
3843
}
3944

4045
// Starts a TPU VM with the specified name in the given project and zone.
41-
public static void startTpuVm(String projectId, String zone, String tpuVmName)
46+
public static void startTpuVm(String projectId, String zone, String nodeName)
4247
throws IOException, ExecutionException, InterruptedException {
4348
// Initialize client that will be used to send requests. This client only needs to be created
4449
// once, and can be reused for multiple requests.
4550
try (TpuClient tpuClient = TpuClient.create()) {
46-
String nodeName = NodeName.of(projectId, zone, tpuVmName).toString();
51+
String name = NodeName.of(projectId, zone, nodeName).toString();
4752

48-
StartNodeRequest request = StartNodeRequest.newBuilder().setName(nodeName).build();
53+
StartNodeRequest request = StartNodeRequest.newBuilder().setName(name).build();
4954
Node response = tpuClient.startNodeAsync(request).get();
5055

5156
System.out.printf("TPU VM started: %s\n", response.getName());

tpu/src/main/java/tpu/StopTpuVm.java

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -30,22 +30,27 @@ public class StopTpuVm {
3030
public static void main(String[] args)
3131
throws IOException, ExecutionException, InterruptedException {
3232
// TODO(developer): Replace these variables before running the sample.
33+
// Project ID or project number of the Google Cloud project you want to create a node.
3334
String projectId = "YOUR_PROJECT_ID";
35+
// The zone in which to create the TPU.
36+
// For more information about supported TPU types for specific zones,
37+
// see https://cloud.google.com/tpu/docs/regions-zones
3438
String zone = "europe-west4-a";
35-
String tpuVmName = "YOUR_TPU_NAME";
39+
// The name for your TPU.
40+
String nodeName = "YOUR_TPY_NAME";
3641

37-
stopTpuVm(projectId, zone, tpuVmName);
42+
stopTpuVm(projectId, zone, nodeName);
3843
}
3944

4045
// Stops a TPU VM with the specified name in the given project and zone.
41-
public static void stopTpuVm(String projectId, String zone, String tpuVmName)
46+
public static void stopTpuVm(String projectId, String zone, String nodeName)
4247
throws IOException, ExecutionException, InterruptedException {
4348
// Initialize client that will be used to send requests. This client only needs to be created
4449
// once, and can be reused for multiple requests.
4550
try (TpuClient tpuClient = TpuClient.create()) {
46-
String nodeName = NodeName.of(projectId, zone, tpuVmName).toString();
51+
String name = NodeName.of(projectId, zone, nodeName).toString();
4752

48-
StopNodeRequest request = StopNodeRequest.newBuilder().setName(nodeName).build();
53+
StopNodeRequest request = StopNodeRequest.newBuilder().setName(name).build();
4954
Node response = tpuClient.stopNodeAsync(request).get();
5055

5156
System.out.printf("TPU VM stopped: %s\n", response.getName());

0 commit comments

Comments
 (0)