Skip to content

Commit 98ec7ca

Browse files
Merge branch 'main' into compute_consume_reservations
2 parents 7f8cede + 282ec81 commit 98ec7ca

File tree

8 files changed

+340
-110
lines changed

8 files changed

+340
-110
lines changed

tpu/src/main/java/tpu/CreateQueuedResourceWithNetwork.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -128,11 +128,10 @@ public static QueuedResource createQueuedResourceWithNetwork(
128128
.setQueuedResourceId(queuedResourceId)
129129
.build();
130130

131-
QueuedResource response = tpuClient.createQueuedResourceAsync(request).get();
132131
// You can wait until TPU Node is READY,
133132
// and check its status using getTpuVm() from "tpu_vm_get" sample.
134-
System.out.println("Queued Resource created: " + queuedResourceId);
135-
return response;
133+
134+
return tpuClient.createQueuedResourceAsync(request).get();
136135
}
137136
}
138137
}
Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
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_vm_create]
20+
import com.google.api.gax.longrunning.OperationTimedPollAlgorithm;
21+
import com.google.api.gax.retrying.RetrySettings;
22+
import com.google.cloud.tpu.v2.CreateNodeRequest;
23+
import com.google.cloud.tpu.v2.Node;
24+
import com.google.cloud.tpu.v2.TpuClient;
25+
import com.google.cloud.tpu.v2.TpuSettings;
26+
import java.io.IOException;
27+
import java.util.concurrent.ExecutionException;
28+
import org.threeten.bp.Duration;
29+
30+
public class CreateTpuVm {
31+
32+
public static void main(String[] args)
33+
throws IOException, ExecutionException, InterruptedException {
34+
// TODO(developer): Replace these variables before running the sample.
35+
// Project ID or project number of the Google Cloud project you want to create a node.
36+
String projectId = "YOUR_PROJECT_ID";
37+
// The zone in which to create the TPU.
38+
// For more information about supported TPU types for specific zones,
39+
// see https://cloud.google.com/tpu/docs/regions-zones
40+
String zone = "europe-west4-a";
41+
// The name for your TPU.
42+
String nodeName = "YOUR_TPU_NAME";
43+
// The accelerator type that specifies the version and size of the Cloud TPU you want to create.
44+
// For more information about supported accelerator types for each TPU version,
45+
// see https://cloud.google.com/tpu/docs/system-architecture-tpu-vm#versions.
46+
String tpuType = "v2-8";
47+
// Software version that specifies the version of the TPU runtime to install.
48+
// For more information see https://cloud.google.com/tpu/docs/runtimes
49+
String tpuSoftwareVersion = "tpu-vm-tf-2.14.1";
50+
51+
createTpuVm(projectId, zone, nodeName, tpuType, tpuSoftwareVersion);
52+
}
53+
54+
// Creates a TPU VM with the specified name, zone, accelerator type, and version.
55+
public static Node createTpuVm(
56+
String projectId, String zone, String nodeName, String tpuType, String tpuSoftwareVersion)
57+
throws IOException, ExecutionException, InterruptedException {
58+
// With these settings the client library handles the Operation's polling mechanism
59+
// and prevent CancellationException error
60+
TpuSettings.Builder clientSettings =
61+
TpuSettings.newBuilder();
62+
clientSettings
63+
.createNodeOperationSettings()
64+
.setPollingAlgorithm(
65+
OperationTimedPollAlgorithm.create(
66+
RetrySettings.newBuilder()
67+
.setInitialRetryDelay(Duration.ofMillis(5000L))
68+
.setRetryDelayMultiplier(1.5)
69+
.setMaxRetryDelay(Duration.ofMillis(45000L))
70+
.setInitialRpcTimeout(Duration.ZERO)
71+
.setRpcTimeoutMultiplier(1.0)
72+
.setMaxRpcTimeout(Duration.ZERO)
73+
.setTotalTimeout(Duration.ofHours(24L))
74+
.build()));
75+
76+
// Initialize client that will be used to send requests. This client only needs to be created
77+
// once, and can be reused for multiple requests.
78+
try (TpuClient tpuClient = TpuClient.create(clientSettings.build())) {
79+
String parent = String.format("projects/%s/locations/%s", projectId, zone);
80+
81+
Node tpuVm = Node.newBuilder()
82+
.setName(nodeName)
83+
.setAcceleratorType(tpuType)
84+
.setRuntimeVersion(tpuSoftwareVersion)
85+
.build();
86+
87+
CreateNodeRequest request = CreateNodeRequest.newBuilder()
88+
.setParent(parent)
89+
.setNodeId(nodeName)
90+
.setNode(tpuVm)
91+
.build();
92+
93+
return tpuClient.createNodeAsync(request).get();
94+
}
95+
}
96+
}
97+
//[END tpu_vm_create]

tpu/src/main/java/tpu/DeleteForceQueuedResource.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717
package tpu;
1818

1919
//[START tpu_queued_resources_delete_force]
20-
2120
import com.google.api.gax.retrying.RetrySettings;
2221
import com.google.api.gax.rpc.UnknownException;
2322
import com.google.cloud.tpu.v2alpha1.DeleteQueuedResourceRequest;
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
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_vm_delete]
20+
import com.google.api.gax.longrunning.OperationTimedPollAlgorithm;
21+
import com.google.api.gax.retrying.RetrySettings;
22+
import com.google.cloud.tpu.v2.DeleteNodeRequest;
23+
import com.google.cloud.tpu.v2.NodeName;
24+
import com.google.cloud.tpu.v2.TpuClient;
25+
import com.google.cloud.tpu.v2.TpuSettings;
26+
import java.io.IOException;
27+
import java.util.concurrent.ExecutionException;
28+
import org.threeten.bp.Duration;
29+
30+
public class DeleteTpuVm {
31+
32+
public static void main(String[] args)
33+
throws IOException, ExecutionException, InterruptedException {
34+
// TODO(developer): Replace these variables before running the sample.
35+
// Project ID or project number of the Google Cloud project you want to create a node.
36+
String projectId = "YOUR_PROJECT_ID";
37+
// The zone in which to create the TPU.
38+
// For more information about supported TPU types for specific zones,
39+
// see https://cloud.google.com/tpu/docs/regions-zones
40+
String zone = "europe-west4-a";
41+
// The name for your TPU.
42+
String nodeName = "YOUR_TPU_NAME";
43+
44+
deleteTpuVm(projectId, zone, nodeName);
45+
}
46+
47+
// Deletes a TPU VM with the specified name in the given project and zone.
48+
public static void deleteTpuVm(String projectId, String zone, String nodeName)
49+
throws IOException, ExecutionException, InterruptedException {
50+
// With these settings the client library handles the Operation's polling mechanism
51+
// and prevent CancellationException error
52+
TpuSettings.Builder clientSettings =
53+
TpuSettings.newBuilder();
54+
clientSettings
55+
.deleteNodeOperationSettings()
56+
.setPollingAlgorithm(
57+
OperationTimedPollAlgorithm.create(
58+
RetrySettings.newBuilder()
59+
.setInitialRetryDelay(Duration.ofMillis(5000L))
60+
.setRetryDelayMultiplier(1.5)
61+
.setMaxRetryDelay(Duration.ofMillis(45000L))
62+
.setInitialRpcTimeout(Duration.ZERO)
63+
.setRpcTimeoutMultiplier(1.0)
64+
.setMaxRpcTimeout(Duration.ZERO)
65+
.setTotalTimeout(Duration.ofHours(24L))
66+
.build()));
67+
68+
// Initialize client that will be used to send requests. This client only needs to be created
69+
// once, and can be reused for multiple requests.
70+
try (TpuClient tpuClient = TpuClient.create(clientSettings.build())) {
71+
String name = NodeName.of(projectId, zone, nodeName).toString();
72+
73+
DeleteNodeRequest request = DeleteNodeRequest.newBuilder().setName(name).build();
74+
75+
tpuClient.deleteNodeAsync(request).get();
76+
System.out.println("TPU VM deleted");
77+
}
78+
}
79+
}
80+
//[END tpu_vm_delete]
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
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_vm_get]
20+
import com.google.cloud.tpu.v2.GetNodeRequest;
21+
import com.google.cloud.tpu.v2.Node;
22+
import com.google.cloud.tpu.v2.NodeName;
23+
import com.google.cloud.tpu.v2.TpuClient;
24+
import java.io.IOException;
25+
26+
public class GetTpuVm {
27+
28+
public static void main(String[] args) throws IOException {
29+
// TODO(developer): Replace these variables before running the sample.
30+
// Project ID or project number of the Google Cloud project you want to create a node.
31+
String projectId = "YOUR_PROJECT_ID";
32+
// The zone in which to create the TPU.
33+
// For more information about supported TPU types for specific zones,
34+
// see https://cloud.google.com/tpu/docs/regions-zones
35+
String zone = "europe-west4-a";
36+
// The name for your TPU.
37+
String nodeName = "YOUR_TPU_NAME";
38+
39+
getTpuVm(projectId, zone, nodeName);
40+
}
41+
42+
// Describes a TPU VM with the specified name in the given project and zone.
43+
public static Node getTpuVm(String projectId, String zone, String nodeName)
44+
throws IOException {
45+
// Initialize client that will be used to send requests. This client only needs to be created
46+
// once, and can be reused for multiple requests.
47+
try (TpuClient tpuClient = TpuClient.create()) {
48+
String name = NodeName.of(projectId, zone, nodeName).toString();
49+
50+
GetNodeRequest request = GetNodeRequest.newBuilder().setName(name).build();
51+
52+
return tpuClient.getNode(request);
53+
}
54+
}
55+
}
56+
//[END tpu_vm_get]

tpu/src/test/java/tpu/CreateQueuedResourceWithNetworkIT.java renamed to tpu/src/test/java/tpu/QueuedResourceIT.java

Lines changed: 12 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -17,50 +17,43 @@
1717
package tpu;
1818

1919
import static com.google.common.truth.Truth.assertThat;
20+
import static com.google.common.truth.Truth.assertWithMessage;
2021

21-
import com.google.api.gax.rpc.NotFoundException;
2222
import com.google.cloud.tpu.v2alpha1.QueuedResource;
23-
import java.io.IOException;
2423
import java.util.UUID;
2524
import java.util.concurrent.TimeUnit;
2625
import org.junit.Test;
2726
import org.junit.jupiter.api.AfterAll;
28-
import org.junit.jupiter.api.Assertions;
2927
import org.junit.jupiter.api.BeforeAll;
3028
import org.junit.jupiter.api.Timeout;
3129
import org.junit.runner.RunWith;
3230
import org.junit.runners.JUnit4;
3331

3432
@RunWith(JUnit4.class)
3533
@Timeout(value = 6, unit = TimeUnit.MINUTES)
36-
public class CreateQueuedResourceWithNetworkIT {
37-
34+
public class QueuedResourceIT {
3835
private static final String PROJECT_ID = System.getenv("GOOGLE_CLOUD_PROJECT");
3936
private static final String ZONE = "europe-west4-a";
40-
static String javaVersion = System.getProperty("java.version").substring(0, 2);
41-
private static final String NODE_NAME = "test-tpu-queued-resource-network-" + javaVersion + "-"
42-
+ UUID.randomUUID().toString().substring(0, 8);
37+
private static final String NODE_NAME = "test-tpu-queued-resource-network-" + UUID.randomUUID();
4338
private static final String TPU_TYPE = "v2-8";
4439
private static final String TPU_SOFTWARE_VERSION = "tpu-vm-tf-2.14.1";
45-
private static final String QUEUED_RESOURCE_NAME = "queued-resource-network-" + javaVersion + "-"
46-
+ UUID.randomUUID().toString().substring(0, 8);
40+
private static final String QUEUED_RESOURCE_NAME = "queued-resource-network-" + UUID.randomUUID();
4741
private static final String NETWORK_NAME = "default";
4842

49-
@BeforeAll
50-
public static void setUp() throws IOException {
43+
public static void requireEnvVar(String envVarName) {
44+
assertWithMessage(String.format("Missing environment variable '%s' ", envVarName))
45+
.that(System.getenv(envVarName)).isNotEmpty();
46+
}
5147

52-
// Cleanup existing stale resources.
53-
Util.cleanUpExistingQueuedResources("queued-resource-network-", PROJECT_ID, ZONE);
48+
@BeforeAll
49+
public static void setUp() {
50+
requireEnvVar("GOOGLE_APPLICATION_CREDENTIALS");
51+
requireEnvVar("GOOGLE_CLOUD_PROJECT");
5452
}
5553

5654
@AfterAll
5755
public static void cleanup() {
5856
DeleteForceQueuedResource.deleteForceQueuedResource(PROJECT_ID, ZONE, QUEUED_RESOURCE_NAME);
59-
60-
// Test that resource is deleted
61-
Assertions.assertThrows(
62-
NotFoundException.class,
63-
() -> GetQueuedResource.getQueuedResource(PROJECT_ID, ZONE, QUEUED_RESOURCE_NAME));
6457
}
6558

6659
@Test

0 commit comments

Comments
 (0)