2424import com .google .cloud .tpu .v2alpha1 .TpuClient ;
2525import com .google .cloud .tpu .v2alpha1 .TpuSettings ;
2626import java .io .IOException ;
27- import java .nio .file .Files ;
28- import java .nio .file .Paths ;
2927import java .util .HashMap ;
3028import java .util .Map ;
3129import java .util .concurrent .ExecutionException ;
@@ -52,18 +50,15 @@ public static void main(String[] args)
5250 String tpuSoftwareVersion = "tpu-vm-tf-2.14.1" ;
5351 // The name for your Queued Resource.
5452 String queuedResourceId = "QUEUED_RESOURCE_ID" ;
55- // Path to the startup script file.
56- String startupScriptPath = "YOUR_STARTUP_SCRIPT_PATH" ;
5753
58- createQueuedResource (
59- projectId , zone , queuedResourceId , nodeName ,
60- tpuType , tpuSoftwareVersion , startupScriptPath );
54+ createQueuedResource (projectId , zone , queuedResourceId , nodeName ,
55+ tpuType , tpuSoftwareVersion );
6156 }
6257
6358 // Creates a Queued Resource with startup script.
6459 public static QueuedResource createQueuedResource (
6560 String projectId , String zone , String queuedResourceId ,
66- String nodeName , String tpuType , String tpuSoftwareVersion , String startupScriptPath )
61+ String nodeName , String tpuType , String tpuSoftwareVersion )
6762 throws IOException , ExecutionException , InterruptedException {
6863 // With these settings the client library handles the Operation's polling mechanism
6964 // and prevent CancellationException error
@@ -80,13 +75,14 @@ public static QueuedResource createQueuedResource(
8075 .setMaxRetryDelay (Duration .ofMillis (45000L ))
8176 .setTotalTimeout (Duration .ofHours (24L ))
8277 .build ());
83- String parent = String .format ("projects/%s/locations/%s" , projectId , zone );
84- // Read the startup script content from the file
85- String startupScriptContent = new String (Files .readAllBytes (Paths .get (startupScriptPath )));
86- // Add startup script to metadata
87- Map <String , String > metadata = new HashMap <>();
88- metadata .put ("startup-script" , startupScriptContent );
89-
78+ String parent = String .format ("projects/%s/locations/%s" , projectId , zone );
79+ // Read the startup script content from the file
80+ String startupScriptContent = "#!/bin/bash\n echo \" Hello from the startup script!\" " ;
81+ // Add startup script to metadata
82+ Map <String , String > metadata = new HashMap <>();
83+ metadata .put ("startup-script" , startupScriptContent );
84+ String queuedResourceForTpu = String .format ("projects/%s/locations/%s/queuedResources/%s" ,
85+ projectId , zone , queuedResourceId );
9086 // Initialize client that will be used to send requests. This client only needs to be created
9187 // once, and can be reused for multiple requests.
9288 try (TpuClient tpuClient = TpuClient .create (clientSettings .build ())) {
@@ -95,10 +91,7 @@ public static QueuedResource createQueuedResource(
9591 .setName (nodeName )
9692 .setAcceleratorType (tpuType )
9793 .setRuntimeVersion (tpuSoftwareVersion )
98- .setQueuedResource (
99- String .format (
100- "projects/%s/locations/%s/queuedResources/%s" ,
101- projectId , zone , queuedResourceId ))
94+ .setQueuedResource (queuedResourceForTpu )
10295 .putAllMetadata (metadata )
10396 .build ();
10497
@@ -122,12 +115,10 @@ public static QueuedResource createQueuedResource(
122115 .setQueuedResourceId (queuedResourceId )
123116 .setQueuedResource (queuedResource )
124117 .build ();
125-
126- QueuedResource response = tpuClient .createQueuedResourceAsync (request ).get ();
127118 // You can wait until TPU Node is READY,
128119 // and check its status using getTpuVm() from "tpu_vm_get" sample.
129- System . out . printf ( "Queued Resource created: %s \n " , queuedResourceId );
130- return response ;
120+
121+ return tpuClient . createQueuedResourceAsync ( request ). get () ;
131122 }
132123 }
133124}
0 commit comments