Skip to content

Commit 66ae551

Browse files
Implemented tpu_queued_resources_create, tpu_queued_resources_get, tpu_queued_resources_delete_force and tpu_queued_resources_delete samples, created tests
1 parent b804cc8 commit 66ae551

File tree

7 files changed

+484
-6
lines changed

7 files changed

+484
-6
lines changed
Lines changed: 122 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,122 @@
1+
/*
2+
* Copyright 2024 Google LLC
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package tpu;
18+
19+
//[START tpu_queued_resources_create]
20+
import com.google.api.gax.retrying.RetrySettings;
21+
import com.google.cloud.tpu.v2alpha1.CreateQueuedResourceRequest;
22+
import com.google.cloud.tpu.v2alpha1.Node;
23+
import com.google.cloud.tpu.v2alpha1.QueuedResource;
24+
import com.google.cloud.tpu.v2alpha1.TpuClient;
25+
import com.google.cloud.tpu.v2alpha1.TpuSettings;
26+
import java.io.IOException;
27+
import java.util.concurrent.ExecutionException;
28+
import org.threeten.bp.Duration;
29+
30+
public class CreateQueuedResource {
31+
public static void main(String[] args)
32+
throws IOException, ExecutionException, InterruptedException {
33+
// TODO(developer): Replace these variables before running the sample.
34+
// Project ID or project number of the Google Cloud project you want to create a node.
35+
String projectId = "YOUR_PROJECT_ID";
36+
// The zone in which to create the TPU.
37+
// For more information about supported TPU types for specific zones,
38+
// see https://cloud.google.com/tpu/docs/regions-zones
39+
String zone = "europe-west4-a";
40+
// The name for your TPU.
41+
String nodeName = "YOUR_NODE_ID";
42+
// The accelerator type that specifies the version and size of the Cloud TPU you want to create.
43+
// For more information about supported accelerator types for each TPU version,
44+
// see https://cloud.google.com/tpu/docs/system-architecture-tpu-vm#versions.
45+
String tpuType = "v2-8";
46+
// Software version that specifies the version of the TPU runtime to install.
47+
// For more information see https://cloud.google.com/tpu/docs/runtimes
48+
String tpuSoftwareVersion = "tpu-vm-tf-2.14.1";
49+
// The name for your Queued Resource.
50+
String queuedResourceId = "QUEUED_RESOURCE_ID";
51+
52+
createQueuedResource(
53+
projectId, zone, queuedResourceId, nodeName, tpuType, tpuSoftwareVersion);
54+
}
55+
56+
// Creates a Queued Resource
57+
public static QueuedResource createQueuedResource(String projectId, String zone,
58+
String queuedResourceId, String nodeName, String tpuType, String tpuSoftwareVersion)
59+
throws IOException, ExecutionException, InterruptedException {
60+
// With these settings the client library handles the Operation's polling mechanism
61+
// and prevent CancellationException error
62+
TpuSettings.Builder clientSettings =
63+
TpuSettings.newBuilder();
64+
clientSettings
65+
.createQueuedResourceSettings()
66+
.setRetrySettings(
67+
RetrySettings.newBuilder()
68+
.setInitialRetryDelay(Duration.ofMillis(5000L))
69+
.setRetryDelayMultiplier(2.0)
70+
.setInitialRpcTimeout(Duration.ZERO)
71+
.setRpcTimeoutMultiplier(1.0)
72+
.setMaxRetryDelay(Duration.ofMillis(45000L))
73+
.setTotalTimeout(Duration.ofHours(24L))
74+
.build());
75+
// Initialize client that will be used to send requests. This client only needs to be created
76+
// once, and can be reused for multiple requests.
77+
try (TpuClient tpuClient = TpuClient.create(clientSettings.build())) {
78+
String parent = String.format("projects/%s/locations/%s", projectId, zone);
79+
Node node =
80+
Node.newBuilder()
81+
.setName(nodeName)
82+
.setAcceleratorType(tpuType)
83+
.setRuntimeVersion(tpuSoftwareVersion)
84+
.setQueuedResource(
85+
String.format(
86+
"projects/%s/locations/%s/queuedResources/%s",
87+
projectId, zone, queuedResourceId))
88+
.build();
89+
90+
QueuedResource queuedResource =
91+
QueuedResource.newBuilder()
92+
.setName(queuedResourceId)
93+
.setTpu(
94+
QueuedResource.Tpu.newBuilder()
95+
.addNodeSpec(
96+
QueuedResource.Tpu.NodeSpec.newBuilder()
97+
.setParent(parent)
98+
.setNode(node)
99+
.setNodeId(nodeName)
100+
.build())
101+
.build())
102+
// You can request a queued resource using a reservation by specifying it in code
103+
//.setReservationName(
104+
// "projects/YOUR_PROJECT_ID/locations/YOUR_ZONE/reservations/YOUR_RESERVATION_NAME")
105+
.build();
106+
107+
CreateQueuedResourceRequest request =
108+
CreateQueuedResourceRequest.newBuilder()
109+
.setParent(parent)
110+
.setQueuedResourceId(queuedResourceId)
111+
.setQueuedResource(queuedResource)
112+
.build();
113+
114+
QueuedResource response = tpuClient.createQueuedResourceAsync(request).get();
115+
// You can wait until TPU Node is READY,
116+
// and check its status using getTpuVm() from "tpu_vm_get" sample.
117+
System.out.printf("Queued Resource created: %s\n", response.getName());
118+
return response;
119+
}
120+
}
121+
}
122+
//[END tpu_queued_resources_create]
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
/*
2+
* Copyright 2024 Google LLC
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package tpu;
18+
19+
//[START tpu_queued_resources_delete_force]
20+
import com.google.api.gax.retrying.RetrySettings;
21+
import com.google.api.gax.rpc.UnknownException;
22+
import com.google.cloud.tpu.v2alpha1.DeleteQueuedResourceRequest;
23+
import com.google.cloud.tpu.v2alpha1.TpuClient;
24+
import com.google.cloud.tpu.v2alpha1.TpuSettings;
25+
import java.io.IOException;
26+
import java.util.concurrent.ExecutionException;
27+
import org.threeten.bp.Duration;
28+
29+
public class DeleteForceQueuedResource {
30+
public static void main(String[] args) {
31+
// TODO(developer): Replace these variables before running the sample.
32+
// Project ID or project number of the Google Cloud project.
33+
String projectId = "YOUR_PROJECT_ID";
34+
// The zone in which the TPU was created.
35+
String zone = "europe-west4-a";
36+
// The name for your Queued Resource.
37+
String queuedResourceId = "QUEUED_RESOURCE_ID";
38+
39+
deleteForceQueuedResource(projectId, zone, queuedResourceId);
40+
}
41+
42+
// Deletes a Queued Resource asynchronously with --force flag.
43+
public static void deleteForceQueuedResource(
44+
String projectId, String zone, String queuedResourceId) {
45+
String name = String.format("projects/%s/locations/%s/queuedResources/%s",
46+
projectId, zone, queuedResourceId);
47+
// With these settings the client library handles the Operation's polling mechanism
48+
// and prevent CancellationException error
49+
TpuSettings.Builder clientSettings =
50+
TpuSettings.newBuilder();
51+
clientSettings
52+
.deleteQueuedResourceSettings()
53+
.setRetrySettings(
54+
RetrySettings.newBuilder()
55+
.setInitialRetryDelay(Duration.ofMillis(5000L))
56+
.setRetryDelayMultiplier(2.0)
57+
.setInitialRpcTimeout(Duration.ZERO)
58+
.setRpcTimeoutMultiplier(1.0)
59+
.setMaxRetryDelay(Duration.ofMillis(45000L))
60+
.setTotalTimeout(Duration.ofHours(24L))
61+
.build());
62+
63+
// Initialize client that will be used to send requests. This client only needs to be created
64+
// once, and can be reused for multiple requests.
65+
try (TpuClient tpuClient = TpuClient.create(clientSettings.build())) {
66+
DeleteQueuedResourceRequest request =
67+
DeleteQueuedResourceRequest.newBuilder().setName(name).setForce(true).build();
68+
69+
tpuClient.deleteQueuedResourceAsync(request).get();
70+
71+
} catch (UnknownException | InterruptedException | ExecutionException | IOException e) {
72+
System.out.println(e.getMessage());
73+
}
74+
System.out.printf("Deleted Queued Resource: %s\n", name);
75+
}
76+
}
77+
//[END tpu_queued_resources_delete_force]
Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
/*
2+
* Copyright 2024 Google LLC
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package tpu;
18+
19+
//[START tpu_queued_resources_delete]
20+
import com.google.api.gax.retrying.RetrySettings;
21+
import com.google.api.gax.rpc.UnknownException;
22+
import com.google.cloud.tpu.v2alpha1.DeleteQueuedResourceRequest;
23+
import com.google.cloud.tpu.v2alpha1.GetQueuedResourceRequest;
24+
import com.google.cloud.tpu.v2alpha1.QueuedResource;
25+
import com.google.cloud.tpu.v2alpha1.TpuClient;
26+
import com.google.cloud.tpu.v2alpha1.TpuSettings;
27+
import java.io.IOException;
28+
import java.util.concurrent.ExecutionException;
29+
import java.util.concurrent.TimeUnit;
30+
import org.threeten.bp.Duration;
31+
32+
public class DeleteQueuedResource {
33+
public static void main(String[] args) {
34+
// TODO(developer): Replace these variables before running the sample.
35+
// Project ID or project number of the Google Cloud project.
36+
String projectId = "YOUR_PROJECT_ID";
37+
// The zone in which the TPU was created.
38+
String zone = "europe-west4-a";
39+
// The name for your Queued Resource.
40+
String queuedResourceId = "QUEUED_RESOURCE_ID";
41+
42+
deleteQueuedResource(projectId, zone, queuedResourceId);
43+
}
44+
45+
// Deletes a Queued Resource asynchronously.
46+
public static void deleteQueuedResource(String projectId, String zone, String queuedResourceId) {
47+
String name = String.format("projects/%s/locations/%s/queuedResources/%s",
48+
projectId, zone, queuedResourceId);
49+
// With these settings the client library handles the Operation's polling mechanism
50+
// and prevent CancellationException error
51+
TpuSettings.Builder clientSettings =
52+
TpuSettings.newBuilder();
53+
clientSettings
54+
.deleteQueuedResourceSettings()
55+
.setRetrySettings(
56+
RetrySettings.newBuilder()
57+
.setInitialRetryDelay(Duration.ofMillis(5000L))
58+
.setRetryDelayMultiplier(2.0)
59+
.setInitialRpcTimeout(Duration.ZERO)
60+
.setRpcTimeoutMultiplier(1.0)
61+
.setMaxRetryDelay(Duration.ofMillis(45000L))
62+
.setTotalTimeout(Duration.ofHours(24L))
63+
.build());
64+
// Initialize client that will be used to send requests. This client only needs to be created
65+
// once, and can be reused for multiple requests.
66+
try (TpuClient tpuClient = TpuClient.create(clientSettings.build())) {
67+
// Retrive node name
68+
GetQueuedResourceRequest getRequest =
69+
GetQueuedResourceRequest.newBuilder().setName(name).build();
70+
QueuedResource queuedResource = tpuClient.getQueuedResource(getRequest);
71+
String nodeName = queuedResource.getTpu().getNodeSpec(0).getNode().getName();
72+
// Before deleting the queued resource it is required to delete the TPU VM.
73+
DeleteTpuVm.deleteTpuVm(projectId, zone, nodeName);
74+
// Wait until TpuVm is deleted
75+
TimeUnit.MINUTES.sleep(3);
76+
77+
DeleteQueuedResourceRequest request =
78+
DeleteQueuedResourceRequest.newBuilder().setName(name).build();
79+
80+
tpuClient.deleteQueuedResourceAsync(request).get();
81+
82+
} catch (UnknownException | InterruptedException | ExecutionException | IOException e) {
83+
System.out.println(e.getMessage());
84+
}
85+
System.out.printf("Deleted Queued Resource: %s\n", name);
86+
}
87+
}
88+
//[END tpu_queued_resources_delete]
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
/*
2+
* Copyright 2024 Google LLC
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package tpu;
18+
19+
//[START tpu_queued_resources_get]
20+
import com.google.cloud.tpu.v2alpha1.GetQueuedResourceRequest;
21+
import com.google.cloud.tpu.v2alpha1.QueuedResource;
22+
import com.google.cloud.tpu.v2alpha1.TpuClient;
23+
import java.io.IOException;
24+
25+
public class GetQueuedResource {
26+
public static void main(String[] args) throws IOException {
27+
// TODO(developer): Replace these variables before running the sample.
28+
// Project ID or project number of the Google Cloud project.
29+
String projectId = "YOUR_PROJECT_ID";
30+
// The zone in which the TPU was created.
31+
String zone = "europe-west4-a";
32+
// The name for your Queued Resource.
33+
String queuedResourceId = "QUEUED_RESOURCE_ID";
34+
35+
getQueuedResource(projectId, zone, queuedResourceId);
36+
}
37+
38+
// Get a Queued Resource.
39+
public static QueuedResource getQueuedResource(
40+
String projectId, String zone, String queuedResourceId) throws IOException {
41+
String name = String.format("projects/%s/locations/%s/queuedResources/%s",
42+
projectId, zone, queuedResourceId);
43+
// Initialize client that will be used to send requests. This client only needs to be created
44+
// once, and can be reused for multiple requests.
45+
try (TpuClient tpuClient = TpuClient.create()) {
46+
GetQueuedResourceRequest request =
47+
GetQueuedResourceRequest.newBuilder().setName(name).build();
48+
49+
return tpuClient.getQueuedResource(request);
50+
}
51+
}
52+
}
53+
//[END tpu_queued_resources_get]

0 commit comments

Comments
 (0)