1717package tpu ;
1818
1919//[START tpu_queued_resources_create]
20- import com .google .api .gax .retrying .RetrySettings ;
2120import com .google .cloud .tpu .v2alpha1 .CreateQueuedResourceRequest ;
2221import com .google .cloud .tpu .v2alpha1 .Node ;
2322import com .google .cloud .tpu .v2alpha1 .QueuedResource ;
2423import com .google .cloud .tpu .v2alpha1 .TpuClient ;
25- import com .google .cloud .tpu .v2alpha1 .TpuSettings ;
2624import java .io .IOException ;
2725import java .util .concurrent .ExecutionException ;
28- import org .threeten .bp .Duration ;
26+ import java .util .concurrent .TimeUnit ;
27+ import java .util .concurrent .TimeoutException ;
2928
3029public class CreateQueuedResource {
3130 public static void main (String [] args )
32- throws IOException , ExecutionException , InterruptedException {
31+ throws IOException , ExecutionException , InterruptedException , TimeoutException {
3332 // TODO(developer): Replace these variables before running the sample.
3433 // Project ID or project number of the Google Cloud project you want to create a node.
3534 String projectId = "YOUR_PROJECT_ID" ;
3635 // The zone in which to create the TPU.
3736 // For more information about supported TPU types for specific zones,
3837 // see https://cloud.google.com/tpu/docs/regions-zones
39- String zone = "europe-west4-a " ;
38+ String zone = "us-central1-f " ;
4039 // The name for your TPU.
4140 String nodeName = "YOUR_NODE_ID" ;
4241 // The accelerator type that specifies the version and size of the Cloud TPU you want to create.
@@ -56,35 +55,19 @@ public static void main(String[] args)
5655 // Creates a Queued Resource
5756 public static QueuedResource createQueuedResource (String projectId , String zone ,
5857 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 ());
58+ throws IOException , ExecutionException , InterruptedException , TimeoutException {
59+ String resource = String .format ("projects/%s/locations/%s/queuedResources/%s" ,
60+ projectId , zone , queuedResourceId );
7561 // Initialize client that will be used to send requests. This client only needs to be created
7662 // once, and can be reused for multiple requests.
77- try (TpuClient tpuClient = TpuClient .create (clientSettings . build () )) {
63+ try (TpuClient tpuClient = TpuClient .create ()) {
7864 String parent = String .format ("projects/%s/locations/%s" , projectId , zone );
7965 Node node =
8066 Node .newBuilder ()
8167 .setName (nodeName )
8268 .setAcceleratorType (tpuType )
8369 .setRuntimeVersion (tpuSoftwareVersion )
84- .setQueuedResource (
85- String .format (
86- "projects/%s/locations/%s/queuedResources/%s" ,
87- projectId , zone , queuedResourceId ))
70+ .setQueuedResource (resource )
8871 .build ();
8972
9073 QueuedResource queuedResource =
@@ -99,9 +82,6 @@ public static QueuedResource createQueuedResource(String projectId, String zone,
9982 .setNodeId (nodeName )
10083 .build ())
10184 .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")
10585 .build ();
10686
10787 CreateQueuedResourceRequest request =
@@ -111,11 +91,7 @@ public static QueuedResource createQueuedResource(String projectId, String zone,
11191 .setQueuedResource (queuedResource )
11292 .build ();
11393
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 ;
94+ return tpuClient .createQueuedResourceAsync (request ).get (1 , TimeUnit .MINUTES );
11995 }
12096 }
12197}
0 commit comments