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+
117package tpu ;
218
319//[START tpu_queued_resources_network]
4-
520import com .google .api .gax .retrying .RetrySettings ;
621import com .google .cloud .tpu .v2alpha1 .CreateQueuedResourceRequest ;
722import com .google .cloud .tpu .v2alpha1 .NetworkConfig ;
1429import org .threeten .bp .Duration ;
1530
1631public class CreateQueuedResourceWithNetwork {
17-
18- private final TpuClient tpuClient ;
19- public CreateQueuedResourceWithNetwork (TpuClient tpuClient ) {
20- this .tpuClient = tpuClient ;
21- }
2232 public static void main (String [] args )
2333 throws IOException , ExecutionException , InterruptedException {
2434 // TODO(developer): Replace these variables before running the sample.
2535 // Project ID or project number of the Google Cloud project you want to create a node.
26- String projectId = "tyaho-softserve-project" ; //"your-project ";
36+ String projectId = "YOUR_PROJECT_ID " ;
2737 // The zone in which to create the TPU.
2838 // For more information about supported TPU types for specific zones,
2939 // see https://cloud.google.com/tpu/docs/regions-zones
3040 String zone = "europe-west4-a" ;
3141 // The name for your TPU.
32- String nodeName = "your-node-id " ;
42+ String nodeName = "YOUR_TPY_NAME " ;
3343 // The accelerator type that specifies the version and size of the Cloud TPU you want to create.
3444 // For more information about supported accelerator types for each TPU version,
3545 // see https://cloud.google.com/tpu/docs/system-architecture-tpu-vm#versions.
@@ -38,22 +48,24 @@ public static void main(String[] args)
3848 // For more information see https://cloud.google.com/tpu/docs/runtimes
3949 String tpuSoftwareVersion = "tpu-vm-tf-2.14.1" ;
4050 // The name for your Queued Resource.
41- String queuedResourceName = "your-queued-resource-id" ;
42- TpuClient tpuClient = TpuClient .create ();
43- CreateQueuedResourceWithNetwork creator = new CreateQueuedResourceWithNetwork (tpuClient );
44-
45- creator .createQueuedResourceWithNetwork (projectId , zone , queuedResourceName , nodeName , tpuType , tpuSoftwareVersion );
51+ String queuedResourceId = "QUEUED_RESOURCE_ID" ;
52+ // The name of the network you want the node to connect to.
53+ // The network should be assigned to your project.
54+ String networkName = "YOUR_COMPUTE_TPU_NETWORK" ;
4655
56+ createQueuedResourceWithNetwork (projectId , zone , queuedResourceId , nodeName ,
57+ tpuType , tpuSoftwareVersion , networkName );
4758 }
4859
4960 // Creates a Queued Resource with network configuration.
50- public static void createQueuedResourceWithNetwork (
61+ public static QueuedResource createQueuedResourceWithNetwork (
5162 String projectId ,
5263 String zone ,
53- String queuedResourceName ,
64+ String queuedResourceId ,
5465 String nodeName ,
5566 String tpuType ,
56- String tpuSoftwareVersion )
67+ String tpuSoftwareVersion ,
68+ String networkName )
5769 throws IOException , ExecutionException , InterruptedException {
5870 // With these settings the client library handles the Operation's polling mechanism
5971 // and prevent CancellationException error
@@ -75,10 +87,8 @@ public static void createQueuedResourceWithNetwork(
7587 try (TpuClient tpuClient = TpuClient .create (clientSettings .build ())) {
7688 String parent = String .format ("projects/%s/locations/%s" , projectId , zone );
7789 String region = zone .substring (0 , zone .length () - 2 );
78- // The name of the network you want the node to connect to. The network should be assigned to your project.
79- String networkName = "compute-tpu-network" ;
80- // Specify the network and subnetwork that you want to connect your TPU to.
8190
91+ // Specify the network and subnetwork that you want to connect your TPU to.
8292 NetworkConfig networkConfig =
8393 NetworkConfig .newBuilder ()
8494 .setEnableExternalIps (true )
@@ -98,13 +108,13 @@ public static void createQueuedResourceWithNetwork(
98108 .setQueuedResource (
99109 String .format (
100110 "projects/%s/locations/%s/queuedResources/%s" ,
101- projectId , zone , queuedResourceName ))
111+ projectId , zone , queuedResourceId ))
102112 .build ();
103113
104114 // Create queued resource
105115 QueuedResource queuedResource =
106116 QueuedResource .newBuilder ()
107- .setName (queuedResourceName )
117+ .setName (queuedResourceId )
108118 .setTpu (
109119 QueuedResource .Tpu .newBuilder ()
110120 .addNodeSpec (
@@ -120,15 +130,14 @@ public static void createQueuedResourceWithNetwork(
120130 CreateQueuedResourceRequest .newBuilder ()
121131 .setParent (parent )
122132 .setQueuedResource (queuedResource )
123- .setQueuedResourceId (queuedResourceName )
133+ .setQueuedResourceId (queuedResourceId )
124134 .build ();
125135
126136 QueuedResource response = tpuClient .createQueuedResourceAsync (request ).get ();
127137 // You can wait until TPU Node is READY,
128138 // and check its status using getTpuVm() from "tpu_vm_get" sample.
129- System .out .printf (
130- "Queued resource %s with specified network created.\n " , queuedResourceName );
131- System .out .println (response );
139+ System .out .println ("Queued Resource created: " + queuedResourceId );
140+ return response ;
132141 }
133142 }
134143}
0 commit comments