Skip to content

Commit fe97842

Browse files
feat(tpu): add tpu queued resources time bound sample. (GoogleCloudPlatform#9617)
* Changed package, added information to CODEOWNERS * Added information to CODEOWNERS * Added timeout * Fixed parameters for test * Fixed DeleteTpuVm and naming * Added comment, created Util class * Fixed naming * Fixed whitespace * Split PR into smaller, deleted redundant code * Implemented tpu_queued_resources_create, tpu_queued_resources_get, tpu_queued_resources_delete_force and tpu_queued_resources_delete samples, created tests * Implemented tpu_queued_resources_time_bound sample, created test * Changed zone for tpu * Cleanup resources * Fixed tests * Fixed test * Fixed code as requested in the comments * Fixed code as requested in the comments
1 parent c16bb4c commit fe97842

File tree

2 files changed

+136
-0
lines changed

2 files changed

+136
-0
lines changed
Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
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_time_bound]
20+
import com.google.cloud.tpu.v2alpha1.CreateQueuedResourceRequest;
21+
import com.google.cloud.tpu.v2alpha1.Node;
22+
import com.google.cloud.tpu.v2alpha1.QueuedResource;
23+
import com.google.cloud.tpu.v2alpha1.TpuClient;
24+
import com.google.protobuf.Duration;
25+
import java.io.IOException;
26+
import java.util.concurrent.ExecutionException;
27+
28+
public class CreateTimeBoundQueuedResource {
29+
30+
public static void main(String[] args)
31+
throws IOException, ExecutionException, InterruptedException {
32+
// 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.
34+
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
38+
String zone = "us-central2-b";
39+
// The name of your node.
40+
String nodeId = "YOUR_NODE_ID";
41+
// The accelerator type that specifies the version and size of the Cloud TPU you want to create.
42+
// For more information about supported accelerator types for each TPU version,
43+
// see https://cloud.google.com/tpu/docs/system-architecture-tpu-vm#versions.
44+
String acceleratorType = "v2-8";
45+
// Software version that specifies the version of the TPU runtime to install.
46+
// For more information see https://cloud.google.com/tpu/docs/runtimes
47+
String runtimeVersion = "tpu-vm-tf-2.14.1";
48+
// The name of your Queued Resource.
49+
String queuedResourceId = "YOUR_QUEUED_RESOURCE_ID";
50+
51+
createTimeBoundQueuedResource(projectId, nodeId,
52+
queuedResourceId, zone, acceleratorType, runtimeVersion);
53+
}
54+
55+
// Creates a Queued Resource with time bound configuration.
56+
public static QueuedResource createTimeBoundQueuedResource(
57+
String projectId, String nodeId, String queuedResourceId,
58+
String zone, String acceleratorType, String runtimeVersion)
59+
throws IOException, ExecutionException, InterruptedException {
60+
// Initialize client that will be used to send requests. This client only needs to be created
61+
// once, and can be reused for multiple requests.
62+
try (TpuClient tpuClient = TpuClient.create()) {
63+
String parent = String.format("projects/%s/locations/%s", projectId, zone);
64+
// Create a Duration object representing 6 hours.
65+
Duration validAfterDuration = Duration.newBuilder().setSeconds(6 * 3600).build();
66+
// You could also use timestamps like this:
67+
// Timestamp validAfterTime = Timestamps.parse("2024-10-14T09:00:00Z");
68+
69+
Node node =
70+
Node.newBuilder()
71+
.setName(nodeId)
72+
.setAcceleratorType(acceleratorType)
73+
.setRuntimeVersion(runtimeVersion)
74+
.setQueuedResource(
75+
String.format(
76+
"projects/%s/locations/%s/queuedResources/%s",
77+
projectId, zone, queuedResourceId))
78+
.build();
79+
80+
QueuedResource queuedResource =
81+
QueuedResource.newBuilder()
82+
.setName(queuedResourceId)
83+
.setTpu(
84+
QueuedResource.Tpu.newBuilder()
85+
.addNodeSpec(
86+
QueuedResource.Tpu.NodeSpec.newBuilder()
87+
.setParent(parent)
88+
.setNode(node)
89+
.setNodeId(nodeId)
90+
.build())
91+
.build())
92+
.setQueueingPolicy(
93+
QueuedResource.QueueingPolicy.newBuilder()
94+
.setValidAfterDuration(validAfterDuration)
95+
// .setValidAfterTime(validAfterTime)
96+
.build())
97+
.build();
98+
99+
CreateQueuedResourceRequest request =
100+
CreateQueuedResourceRequest.newBuilder()
101+
.setParent(parent)
102+
.setQueuedResource(queuedResource)
103+
.setQueuedResourceId(queuedResourceId)
104+
.build();
105+
106+
return tpuClient.createQueuedResourceAsync(request).get();
107+
}
108+
}
109+
}
110+
// [END tpu_queued_resources_time_bound]

tpu/src/test/java/tpu/QueuedResourceIT.java

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -240,4 +240,30 @@ public void testCreateSpotQueuedResource() throws Exception {
240240
assertEquals(returnedQueuedResource.getName(), mockQueuedResource.getName());
241241
}
242242
}
243+
244+
@Test
245+
public void testCreateTimeBoundQueuedResource() throws Exception {
246+
try (MockedStatic<TpuClient> mockedTpuClient = mockStatic(TpuClient.class)) {
247+
QueuedResource mockQueuedResource = QueuedResource.newBuilder()
248+
.setName("QueuedResourceName")
249+
.build();
250+
TpuClient mockTpuClient = mock(TpuClient.class);
251+
OperationFuture mockFuture = mock(OperationFuture.class);
252+
253+
mockedTpuClient.when(TpuClient::create).thenReturn(mockTpuClient);
254+
when(mockTpuClient.createQueuedResourceAsync(any(CreateQueuedResourceRequest.class)))
255+
.thenReturn(mockFuture);
256+
when(mockFuture.get()).thenReturn(mockQueuedResource);
257+
258+
QueuedResource returnedQueuedResource =
259+
CreateTimeBoundQueuedResource.createTimeBoundQueuedResource(
260+
PROJECT_ID, ZONE, QUEUED_RESOURCE_NAME, NODE_NAME,
261+
TPU_TYPE, TPU_SOFTWARE_VERSION);
262+
263+
verify(mockTpuClient, times(1))
264+
.createQueuedResourceAsync(any(CreateQueuedResourceRequest.class));
265+
verify(mockFuture, times(1)).get();
266+
assertEquals(returnedQueuedResource.getName(), mockQueuedResource.getName());
267+
}
268+
}
243269
}

0 commit comments

Comments
 (0)