Skip to content

Commit 64c314c

Browse files
Fixed code as requested in comments
1 parent 4116a36 commit 64c314c

File tree

3 files changed

+4
-49
lines changed

3 files changed

+4
-49
lines changed

tpu/src/main/java/tpu/CreateQueuedResource.java

Lines changed: 1 addition & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -17,17 +17,14 @@
1717
package tpu;
1818

1919
//[START tpu_queued_resources_create]
20-
import com.google.api.gax.retrying.RetrySettings;
2120
import com.google.cloud.tpu.v2alpha1.CreateQueuedResourceRequest;
2221
import com.google.cloud.tpu.v2alpha1.Node;
2322
import com.google.cloud.tpu.v2alpha1.QueuedResource;
2423
import com.google.cloud.tpu.v2alpha1.TpuClient;
25-
import com.google.cloud.tpu.v2alpha1.TpuSettings;
2624
import java.io.IOException;
2725
import java.util.concurrent.ExecutionException;
2826
import java.util.concurrent.TimeUnit;
2927
import java.util.concurrent.TimeoutException;
30-
import org.threeten.bp.Duration;
3128

3229
public class CreateQueuedResource {
3330
public static void main(String[] args)
@@ -59,26 +56,11 @@ public static void main(String[] args)
5956
public static QueuedResource createQueuedResource(String projectId, String zone,
6057
String queuedResourceId, String nodeName, String tpuType, String tpuSoftwareVersion)
6158
throws IOException, ExecutionException, InterruptedException, TimeoutException {
62-
// With these settings the client library handles the Operation's polling mechanism
63-
// and prevent CancellationException error
64-
TpuSettings.Builder clientSettings =
65-
TpuSettings.newBuilder();
66-
clientSettings
67-
.createQueuedResourceSettings()
68-
.setRetrySettings(
69-
RetrySettings.newBuilder()
70-
.setInitialRetryDelay(Duration.ofMillis(5000L))
71-
.setRetryDelayMultiplier(2.0)
72-
.setInitialRpcTimeout(Duration.ZERO)
73-
.setRpcTimeoutMultiplier(1.0)
74-
.setMaxRetryDelay(Duration.ofMillis(45000L))
75-
.setTotalTimeout(Duration.ofHours(24L))
76-
.build());
7759
String resource = String.format("projects/%s/locations/%s/queuedResources/%s",
7860
projectId, zone, queuedResourceId);
7961
// Initialize client that will be used to send requests. This client only needs to be created
8062
// once, and can be reused for multiple requests.
81-
try (TpuClient tpuClient = TpuClient.create(clientSettings.build())) {
63+
try (TpuClient tpuClient = TpuClient.create()) {
8264
String parent = String.format("projects/%s/locations/%s", projectId, zone);
8365
Node node =
8466
Node.newBuilder()
@@ -100,9 +82,6 @@ public static QueuedResource createQueuedResource(String projectId, String zone,
10082
.setNodeId(nodeName)
10183
.build())
10284
.build())
103-
// You can request a queued resource using a reservation by specifying it in code
104-
//.setReservationName(
105-
// "projects/YOUR_PROJECT_ID/locations/YOUR_ZONE/reservations/YOUR_RESERVATION_NAME")
10685
.build();
10786

10887
CreateQueuedResourceRequest request =

tpu/src/main/java/tpu/DeleteQueuedResource.java

Lines changed: 1 addition & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,10 @@
1717
package tpu;
1818

1919
//[START tpu_queued_resources_delete]
20-
import com.google.api.gax.retrying.RetrySettings;
2120
import com.google.cloud.tpu.v2alpha1.DeleteQueuedResourceRequest;
2221
import com.google.cloud.tpu.v2alpha1.TpuClient;
23-
import com.google.cloud.tpu.v2alpha1.TpuSettings;
2422
import java.io.IOException;
2523
import java.util.concurrent.ExecutionException;
26-
import org.threeten.bp.Duration;
2724

2825
public class DeleteQueuedResource {
2926
public static void main(String[] args)
@@ -44,36 +41,17 @@ public static void deleteQueuedResource(String projectId, String zone, String qu
4441
throws ExecutionException, InterruptedException, IOException {
4542
String name = String.format("projects/%s/locations/%s/queuedResources/%s",
4643
projectId, zone, queuedResourceId);
47-
// With these settings the client library handles the Operation's polling mechanism
48-
// and prevent CancellationException error
49-
TpuSettings.Builder clientSettings =
50-
TpuSettings.newBuilder();
51-
clientSettings
52-
.deleteQueuedResourceSettings()
53-
.setRetrySettings(
54-
RetrySettings.newBuilder()
55-
.setInitialRetryDelay(Duration.ofMillis(5000L))
56-
.setRetryDelayMultiplier(2.0)
57-
.setInitialRpcTimeout(Duration.ZERO)
58-
.setRpcTimeoutMultiplier(1.0)
59-
.setMaxRetryDelay(Duration.ofMillis(45000L))
60-
.setTotalTimeout(Duration.ofHours(24L))
61-
.build());
6244
// Initialize client that will be used to send requests. This client only needs to be created
6345
// once, and can be reused for multiple requests.
64-
try (TpuClient tpuClient = TpuClient.create(clientSettings.build())) {
46+
try (TpuClient tpuClient = TpuClient.create()) {
6547
// Before deleting the queued resource it is required to delete the TPU VM.
6648
// For more information about deleting TPU
6749
// see https://cloud.google.com/tpu/docs/managing-tpus-tpu-vm
6850

6951
DeleteQueuedResourceRequest request =
7052
DeleteQueuedResourceRequest.newBuilder().setName(name).build();
7153

72-
// Waiting for updates in the library. Until then, the operation will complete successfully,
73-
// but the user will receive an error message with UnknownException and IllegalStateException.
7454
tpuClient.deleteQueuedResourceAsync(request).get();
75-
76-
System.out.printf("Deleted Queued Resource: %s\n", name);
7755
}
7856
}
7957
}

tpu/src/test/java/tpu/QueuedResourceIT.java

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -59,8 +59,7 @@ public void testCreateQueuedResource() throws Exception {
5959
TpuClient mockTpuClient = mock(TpuClient.class);
6060
OperationFuture mockFuture = mock(OperationFuture.class);
6161

62-
mockedTpuClient.when(() -> TpuClient.create(any(TpuSettings.class)))
63-
.thenReturn(mockTpuClient);
62+
mockedTpuClient.when(TpuClient::create).thenReturn(mockTpuClient);
6463
when(mockTpuClient.createQueuedResourceAsync(any(CreateQueuedResourceRequest.class)))
6564
.thenReturn(mockFuture);
6665
when(mockFuture.get(anyLong(), any(TimeUnit.class))).thenReturn(mockQueuedResource);
@@ -147,8 +146,7 @@ public void testDeleteQueuedResource()
147146
TpuClient mockTpuClient = mock(TpuClient.class);
148147
OperationFuture mockFuture = mock(OperationFuture.class);
149148

150-
mockedTpuClient.when(() -> TpuClient.create(any(TpuSettings.class)))
151-
.thenReturn(mockTpuClient);
149+
mockedTpuClient.when(TpuClient::create).thenReturn(mockTpuClient);
152150
when(mockTpuClient.deleteQueuedResourceAsync(any(DeleteQueuedResourceRequest.class)))
153151
.thenReturn(mockFuture);
154152
when(mockFuture.get()).thenReturn(null);

0 commit comments

Comments
 (0)