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 .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 com .google .protobuf .Duration ;
27+ import com .google .protobuf .Timestamp ;
28+ // Uncomment the following line to use Interval or Date
29+ //import com.google.type.Interval;
30+ //import java.util.Date;
31+ import java .io .IOException ;
32+ import java .time .Instant ;
33+ import java .util .concurrent .ExecutionException ;
34+
35+ public class CreateTimeBoundQueuedResource {
36+
37+ public static void main (String [] args )
38+ throws IOException , ExecutionException , InterruptedException {
39+ // TODO(developer): Replace these variables before running the sample.
40+ // Project ID or project number of the Google Cloud project you want to create a node.
41+ String projectId = "YOUR_PROJECT_ID" ;
42+ // The zone in which to create the TPU.
43+ // For more information about supported TPU types for specific zones,
44+ // see https://cloud.google.com/tpu/docs/regions-zones
45+ String zone = "europe-west4-a" ;
46+ // The name for your TPU.
47+ String nodeName = "YOUR_NODE_ID" ;
48+ // The accelerator type that specifies the version and size of the Cloud TPU you want to create.
49+ // For more information about supported accelerator types for each TPU version,
50+ // see https://cloud.google.com/tpu/docs/system-architecture-tpu-vm#versions.
51+ String tpuType = "v2-8" ;
52+ // Software version that specifies the version of the TPU runtime to install.
53+ // For more information see https://cloud.google.com/tpu/docs/runtimes
54+ String tpuSoftwareVersion = "tpu-vm-tf-2.14.1" ;
55+ // The name for your Queued Resource.
56+ String queuedResourceId = "QUEUED_RESOURCE_ID" ;
57+
58+
59+ createTimeBoundQueuedResource (projectId , nodeName ,
60+ queuedResourceId , zone , tpuType , tpuSoftwareVersion );
61+ }
62+
63+ // Creates a Queued Resource with time bound configuration.
64+ public static QueuedResource createTimeBoundQueuedResource (
65+ String projectId , String nodeName , String queuedResourceName ,
66+ String zone , String tpuType , String tpuSoftwareVersion )
67+ throws IOException , ExecutionException , InterruptedException {
68+ // With these settings the client library handles the Operation's polling mechanism
69+ // and prevent CancellationException error
70+ TpuSettings .Builder clientSettings =
71+ TpuSettings .newBuilder ();
72+ clientSettings
73+ .createQueuedResourceSettings ()
74+ .setRetrySettings (
75+ RetrySettings .newBuilder ()
76+ .setInitialRetryDelay (org .threeten .bp .Duration .ofMillis (5000L ))
77+ .setRetryDelayMultiplier (2.0 )
78+ .setInitialRpcTimeout (org .threeten .bp .Duration .ZERO )
79+ .setRpcTimeoutMultiplier (1.0 )
80+ .setMaxRetryDelay (org .threeten .bp .Duration .ofMillis (45000L ))
81+ .setTotalTimeout (org .threeten .bp .Duration .ofHours (24L ))
82+ .build ());
83+ // Initialize client that will be used to send requests. This client only needs to be created
84+ // once, and can be reused for multiple requests.
85+ try (TpuClient tpuClient = TpuClient .create (clientSettings .build ())) {
86+ // Define parent for requests
87+ String parent = String .format ("projects/%s/locations/%s" , projectId , zone );
88+ // Create a Duration object representing 6 hours.
89+ Duration validAfterDuration = Duration .newBuilder ().setSeconds (6 * 3600 ).build ();
90+ // Uncomment the appropriate lines to use other time bound configurations
91+ // Duration validUntilDuration = Duration.newBuilder().setSeconds(6 * 3600).build();
92+ // String validAfterTime = "2024-10-14T09:00:00Z";
93+ // String validUntilTime = "2024-12-14T09:00:00Z";
94+
95+ // Create a node
96+ Node node =
97+ Node .newBuilder ()
98+ .setName (nodeName )
99+ .setAcceleratorType (tpuType )
100+ .setRuntimeVersion (tpuSoftwareVersion )
101+ .setQueuedResource (
102+ String .format (
103+ "projects/%s/locations/%s/queuedResources/%s" ,
104+ projectId , zone , queuedResourceName ))
105+ .build ();
106+
107+ // Create queued resource
108+ QueuedResource queuedResource =
109+ QueuedResource .newBuilder ()
110+ .setName (queuedResourceName )
111+ .setTpu (
112+ QueuedResource .Tpu .newBuilder ()
113+ .addNodeSpec (
114+ QueuedResource .Tpu .NodeSpec .newBuilder ()
115+ .setParent (parent )
116+ .setNode (node )
117+ .setNodeId (nodeName )
118+ .build ())
119+ .build ())
120+ .setQueueingPolicy (
121+ QueuedResource .QueueingPolicy .newBuilder ()
122+ // You can specify a duration after which a resource should be allocated.
123+ // corresponds --valid-after-duration flag
124+ .setValidAfterDuration (validAfterDuration )
125+ .build ())
126+ // Uncomment the appropriate lines to use other time bound configurations
127+ //.setQueueingPolicy(
128+ //QueuedResource.QueueingPolicy.newBuilder()
129+ // You can specify a time after which a resource should be allocated.
130+ // corresponds --valid-until-duration flag
131+ //.setValidUntilDuration(validUntilDuration)
132+ //.build())
133+ //.setQueueingPolicy(
134+ //QueuedResource.QueueingPolicy.newBuilder()
135+ // You can specify a time before which the resource should be allocated.
136+ // corresponds --valid-after-time flag
137+ //.setValidAfterTime(convertStringToTimestamp(validAfterTime))
138+ //.build())
139+ //.setQueueingPolicy(
140+ //QueuedResource.QueueingPolicy.newBuilder()
141+ // You can specify a time after which the resource should be allocated.
142+ // corresponds --valid-until-time flag
143+ //.setValidUntilTime(convertStringToTimestamp(validUntilTime))
144+ //.build())
145+ //.setQueueingPolicy(
146+ //QueuedResource.QueueingPolicy.newBuilder()
147+ // You can specify a time interval before and after which
148+ // the resource should be allocated.
149+ //.setValidInterval(
150+ //Interval.newBuilder()
151+ //.setStartTime(convertStringToTimestamp(validAfterTime))
152+ //.setEndTime(convertStringToTimestamp(validUntilTime))
153+ //.build())
154+ //.build())
155+ .build ();
156+
157+ CreateQueuedResourceRequest request =
158+ CreateQueuedResourceRequest .newBuilder ()
159+ .setParent (parent )
160+ .setQueuedResource (queuedResource )
161+ .setQueuedResourceId (queuedResourceName )
162+ .build ();
163+
164+ QueuedResource response = tpuClient .createQueuedResourceAsync (request ).get ();
165+ // You can wait until TPU Node is READY,
166+ // and check its status using getTpuVm() from "tpu_vm_get" sample.
167+ System .out .printf ("Queued Resource created: %s\n " , response .getName ());
168+ return response ;
169+ }
170+ }
171+
172+ // Method to convert a string timestamp to a Protobuf Timestamp object
173+ private static Timestamp convertStringToTimestamp (String timestampString ) {
174+ Instant instant = Instant .parse (timestampString );
175+ return Timestamp .newBuilder ()
176+ .setSeconds (instant .getEpochSecond ())
177+ .setNanos (instant .getNano ())
178+ .build ();
179+ }
180+ }
181+ // [END tpu_queued_resources_time_bound]
0 commit comments