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