Skip to content

Commit b61c494

Browse files
Implemented CRUD samples for a Queued Resource, created tests
1 parent d29a6b5 commit b61c494

File tree

7 files changed

+513
-5
lines changed

7 files changed

+513
-5
lines changed
Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
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+
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 java.io.IOException;
26+
import java.util.concurrent.ExecutionException;
27+
28+
public class CreateQueuedResource {
29+
public static void main(String[] args)
30+
throws IOException, ExecutionException, InterruptedException {
31+
// TODO(developer): Replace these variables before running the sample.
32+
// Project ID or project number of the Google Cloud project you want to create a node.
33+
String projectId = "YOUR_PROJECT_ID";
34+
// The zone in which to create the TPU.
35+
// For more information about supported TPU types for specific zones,
36+
// see https://cloud.google.com/tpu/docs/regions-zones
37+
String zone = "europe-west4-a";
38+
// The name for your TPU.
39+
String nodeName = "YOUR_NODE_ID";
40+
// The accelerator type that specifies the version and size of the Cloud TPU you want to create.
41+
// For more information about supported accelerator types for each TPU version,
42+
// see https://cloud.google.com/tpu/docs/system-architecture-tpu-vm#versions.
43+
String tpuType = "v2-8";
44+
// Software version that specifies the version of the TPU runtime to install.
45+
// For more information see https://cloud.google.com/tpu/docs/runtimes
46+
String tpuSoftwareVersion = "tpu-vm-tf-2.14.1";
47+
// The name for your Queued Resource.
48+
String queuedResourceId = "QUEUED_RESOURCE_ID";
49+
50+
createQueuedResource(
51+
projectId, zone, queuedResourceId, nodeName, tpuType, tpuSoftwareVersion);
52+
}
53+
54+
// Creates a Queued Resource
55+
public static void createQueuedResource(String projectId, String zone,
56+
String queuedResourceId, String nodeName, String tpuType, String tpuSoftwareVersion)
57+
throws IOException, ExecutionException, InterruptedException {
58+
try (TpuClient tpuClient = TpuClient.create()) {
59+
String parent = String.format("projects/%s/locations/%s", projectId, zone);
60+
61+
Node node =
62+
Node.newBuilder()
63+
.setName(nodeName)
64+
.setAcceleratorType(tpuType)
65+
.setRuntimeVersion(tpuSoftwareVersion)
66+
.setQueuedResource(
67+
String.format(
68+
"projects/%s/locations/%s/queuedResources/%s",
69+
projectId, zone, queuedResourceId))
70+
.build();
71+
72+
QueuedResource queuedResource =
73+
QueuedResource.newBuilder()
74+
.setName(queuedResourceId)
75+
.setTpu(
76+
QueuedResource.Tpu.newBuilder()
77+
.addNodeSpec(
78+
QueuedResource.Tpu.NodeSpec.newBuilder()
79+
.setParent(parent)
80+
.setNode(node)
81+
.setNodeId(nodeName)
82+
.build())
83+
.build())
84+
.build();
85+
86+
CreateQueuedResourceRequest request =
87+
CreateQueuedResourceRequest.newBuilder()
88+
.setParent(parent)
89+
.setQueuedResourceId(queuedResourceId)
90+
.setQueuedResource(queuedResource)
91+
.build();
92+
93+
QueuedResource response = tpuClient.createQueuedResourceAsync(request).get();
94+
System.out.printf("Queued Resource created: %s\n", response.getName());
95+
}
96+
}
97+
}
98+
//[END tpu_queued_resources_create]
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
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_delete_force]
20+
21+
import com.google.api.gax.retrying.RetrySettings;
22+
import com.google.cloud.tpu.v2alpha1.DeleteQueuedResourceRequest;
23+
import com.google.cloud.tpu.v2alpha1.TpuClient;
24+
import com.google.cloud.tpu.v2alpha1.TpuSettings;
25+
import org.threeten.bp.Duration;
26+
27+
public class DeleteForceQueuedResource {
28+
public static void main(String[] args) {
29+
// TODO(developer): Replace these variables before running the sample.
30+
// Project ID or project number of the Google Cloud project.
31+
String projectId = "YOUR_PROJECT_ID";
32+
// The zone in which the TPU was created.
33+
String zone = "europe-west4-a";
34+
// The name for your Queued Resource.
35+
String queuedResourceId = "QUEUED_RESOURCE_ID";
36+
37+
deleteForceQueuedResource(projectId, zone, queuedResourceId);
38+
}
39+
40+
// Deletes a Queued Resource asynchronously with force=true flag.
41+
public static void deleteForceQueuedResource(
42+
String projectId, String zone, String queuedResourceId) {
43+
String name = String.format("projects/%s/locations/%s/queuedResources/%s",
44+
projectId, zone, queuedResourceId);
45+
// With these settings the client library handles the Operation's polling mechanism
46+
// and prevent CancellationException error
47+
TpuSettings.Builder clientSettings =
48+
TpuSettings.newBuilder();
49+
clientSettings
50+
.deleteQueuedResourceSettings()
51+
.setRetrySettings(
52+
RetrySettings.newBuilder()
53+
.setInitialRetryDelay(Duration.ofMillis(5000L))
54+
.setRetryDelayMultiplier(2.0)
55+
.setInitialRpcTimeout(Duration.ZERO)
56+
.setRpcTimeoutMultiplier(1.0)
57+
.setMaxRetryDelay(Duration.ofMillis(45000L))
58+
.setTotalTimeout(Duration.ofHours(24L))
59+
.build());
60+
61+
// Initialize client that will be used to send requests. This client only needs to be created
62+
// once, and can be reused for multiple requests.
63+
try (TpuClient tpuClient = TpuClient.create(clientSettings.build())) {
64+
DeleteQueuedResourceRequest request =
65+
DeleteQueuedResourceRequest.newBuilder().setName(name).setForce(true).build();
66+
67+
tpuClient.deleteQueuedResourceAsync(request).get();
68+
69+
} catch (Exception e) {
70+
System.out.println("Error unpacking QueuedResource: " + e.getMessage());
71+
}
72+
System.out.printf("Deleted Queued Resource: %s\n", name);
73+
}
74+
}
75+
//[END tpu_queued_resources_delete_force]
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
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_delete]
20+
21+
import com.google.api.gax.retrying.RetrySettings;
22+
import com.google.cloud.tpu.v2alpha1.DeleteQueuedResourceRequest;
23+
import com.google.cloud.tpu.v2alpha1.TpuClient;
24+
import com.google.cloud.tpu.v2alpha1.TpuSettings;
25+
import java.io.IOException;
26+
import java.util.concurrent.ExecutionException;
27+
import org.threeten.bp.Duration;
28+
29+
public class DeleteQueuedResource {
30+
public static void main(String[] args)
31+
throws IOException, ExecutionException, InterruptedException {
32+
// TODO(developer): Replace these variables before running the sample.
33+
// Project ID or project number of the Google Cloud project.
34+
String projectId = "YOUR_PROJECT_ID";
35+
// The zone in which the TPU was created.
36+
String zone = "europe-west4-a";
37+
// The name for your Queued Resource.
38+
String queuedResourceId = "QUEUED_RESOURCE_ID";
39+
40+
deleteQueuedResource(projectId, zone, queuedResourceId);
41+
}
42+
43+
// Deletes a Queued Resource asynchronously.
44+
public static void deleteQueuedResource(String projectId, String zone, String queuedResourceId)
45+
throws ExecutionException, InterruptedException {
46+
String name = String.format("projects/%s/locations/%s/queuedResources/%s",
47+
projectId, zone, queuedResourceId);
48+
// With these settings the client library handles the Operation's polling mechanism
49+
// and prevent CancellationException error
50+
TpuSettings.Builder clientSettings =
51+
TpuSettings.newBuilder();
52+
clientSettings
53+
.deleteQueuedResourceSettings()
54+
.setRetrySettings(
55+
RetrySettings.newBuilder()
56+
.setInitialRetryDelay(Duration.ofMillis(5000L))
57+
.setRetryDelayMultiplier(2.0)
58+
.setInitialRpcTimeout(Duration.ZERO)
59+
.setRpcTimeoutMultiplier(1.0)
60+
.setMaxRetryDelay(Duration.ofMillis(45000L))
61+
.setTotalTimeout(Duration.ofHours(24L))
62+
.build());
63+
// Initialize client that will be used to send requests. This client only needs to be created
64+
// once, and can be reused for multiple requests.
65+
try (TpuClient tpuClient = TpuClient.create(clientSettings.build())) {
66+
DeleteQueuedResourceRequest request =
67+
DeleteQueuedResourceRequest.newBuilder().setName(name).build();
68+
69+
tpuClient.deleteQueuedResourceAsync(request).get();
70+
71+
} catch (Exception e) {
72+
System.out.println("Error unpacking QueuedResource: " + e.getMessage());
73+
}
74+
System.out.printf("Deleted Queued Resource: %s\n", name);
75+
}
76+
}
77+
//[END tpu_queued_resources_delete]
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
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_get]
20+
21+
import com.google.cloud.tpu.v2alpha1.GetQueuedResourceRequest;
22+
import com.google.cloud.tpu.v2alpha1.QueuedResource;
23+
import com.google.cloud.tpu.v2alpha1.TpuClient;
24+
import java.io.IOException;
25+
26+
public class GetQueuedResource {
27+
public static void main(String[] args) throws IOException {
28+
// TODO(developer): Replace these variables before running the sample.
29+
// Project ID or project number of the Google Cloud project.
30+
String projectId = "YOUR_PROJECT_ID";
31+
// The zone in which the TPU was created.
32+
String zone = "europe-west4-a";
33+
// The name for your Queued Resource.
34+
String queuedResourceId = "QUEUED_RESOURCE_ID";
35+
36+
getQueuedResource(projectId, zone, queuedResourceId);
37+
}
38+
39+
// Get a Queued Resource.
40+
public static QueuedResource getQueuedResource(
41+
String projectId, String zone, String queuedResourceId) throws IOException {
42+
String name = String.format("projects/%s/locations/%s/queuedResources/%s",
43+
projectId, zone, queuedResourceId);
44+
// Initialize client that will be used to send requests. This client only needs to be created
45+
// once, and can be reused for multiple requests.
46+
try (TpuClient tpuClient = TpuClient.create()) {
47+
GetQueuedResourceRequest request =
48+
GetQueuedResourceRequest.newBuilder().setName(name).build();
49+
50+
return tpuClient.getQueuedResource(request);
51+
}
52+
}
53+
}
54+
//[END tpu_queued_resources_get]
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
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+
import com.google.cloud.tpu.v2alpha1.ListQueuedResourcesRequest;
20+
import com.google.cloud.tpu.v2alpha1.TpuClient;
21+
import java.io.IOException;
22+
23+
public class ListQueuedResources {
24+
public static void main(String[] args) throws IOException {
25+
// TODO(developer): Replace these variables before running the sample.
26+
// Project ID or project number of the Google Cloud project.
27+
String projectId = "YOUR_PROJECT_ID";
28+
// The zone in which the TPU was created.
29+
String zone = "europe-west4-a";
30+
31+
listQueuedResources(projectId, zone);
32+
}
33+
34+
// List Queued Resources.
35+
public static TpuClient.ListQueuedResourcesPagedResponse listQueuedResources(
36+
String projectId, String zone) throws IOException {
37+
String parent = String.format("projects/%s/locations/%s", projectId, zone);
38+
// Initialize client that will be used to send requests. This client only needs to be created
39+
// once, and can be reused for multiple requests.
40+
try (TpuClient tpuClient = TpuClient.create()) {
41+
ListQueuedResourcesRequest request =
42+
ListQueuedResourcesRequest.newBuilder().setParent(parent).build();
43+
44+
return tpuClient.listQueuedResources(request);
45+
}
46+
}
47+
}

0 commit comments

Comments
 (0)