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]
0 commit comments