Skip to content

Commit 7550386

Browse files
authored
automl: explictly update timeoutes due to default library changes (#2719)
* automl: explictly update timeoutes due to library changes in the defaults * Update ImportDataset.java * try bumping timeout AutoML provides no guarantee on how long this could take and normally would send an email followup. Though normally, I see this finish in under 10 mins. Trying to pin down if TASK CANCELLED is actually related to a timeout or not. * update timeouts for all import methods * undo changes to TablesImport, add catch for Cancellation Exception * update to correct CancelledException * wrong cancellation again * run tests * log to error * test * reset changes that printed error * set retry settings * add timeout check to beta code
1 parent 61079d9 commit 7550386

File tree

7 files changed

+78
-16
lines changed

7 files changed

+78
-16
lines changed

automl/beta/pom.xml

+1-2
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@
4040
<dependency>
4141
<groupId>com.google.cloud</groupId>
4242
<artifactId>libraries-bom</artifactId>
43-
<version>4.4.1</version>
43+
<version>5.2.0</version>
4444
<type>pom</type>
4545
<scope>import</scope>
4646
</dependency>
@@ -60,7 +60,6 @@
6060
<dependency>
6161
<groupId>com.google.cloud</groupId>
6262
<artifactId>google-cloud-storage</artifactId>
63-
<version>1.107.0</version>
6463
</dependency>
6564
<dependency>
6665
<groupId>net.sourceforge.argparse4j</groupId>

automl/beta/src/main/java/com/example/automl/ImportDataset.java

+31-4
Original file line numberDiff line numberDiff line change
@@ -17,19 +17,26 @@
1717
package com.example.automl;
1818

1919
// [START automl_import_dataset_beta]
20+
import com.google.api.gax.longrunning.OperationFuture;
21+
import com.google.api.gax.retrying.RetrySettings;
2022
import com.google.cloud.automl.v1beta1.AutoMlClient;
23+
import com.google.cloud.automl.v1beta1.AutoMlSettings;
2124
import com.google.cloud.automl.v1beta1.DatasetName;
2225
import com.google.cloud.automl.v1beta1.GcsSource;
2326
import com.google.cloud.automl.v1beta1.InputConfig;
27+
import com.google.cloud.automl.v1beta1.OperationMetadata;
2428
import com.google.protobuf.Empty;
2529
import java.io.IOException;
2630
import java.util.Arrays;
2731
import java.util.concurrent.ExecutionException;
32+
import java.util.concurrent.TimeUnit;
33+
import java.util.concurrent.TimeoutException;
34+
import org.threeten.bp.Duration;
2835

2936
class ImportDataset {
3037

3138
public static void main(String[] args)
32-
throws IOException, ExecutionException, InterruptedException {
39+
throws IOException, ExecutionException, InterruptedException, TimeoutException {
3340
// TODO(developer): Replace these variables before running the sample.
3441
String projectId = "YOUR_PROJECT_ID";
3542
String datasetId = "YOUR_DATASET_ID";
@@ -39,11 +46,17 @@ public static void main(String[] args)
3946

4047
// Import a dataset
4148
static void importDataset(String projectId, String datasetId, String path)
42-
throws IOException, ExecutionException, InterruptedException {
49+
throws IOException, ExecutionException, InterruptedException, TimeoutException {
50+
Duration totalTimeout = Duration.ofMinutes(45);
51+
RetrySettings retrySettings = RetrySettings.newBuilder().setTotalTimeout(totalTimeout).build();
52+
AutoMlSettings.Builder builder = AutoMlSettings.newBuilder();
53+
builder.importDataSettings().setRetrySettings(retrySettings).build();
54+
AutoMlSettings settings = builder.build();
55+
4356
// Initialize client that will be used to send requests. This client only needs to be created
4457
// once, and can be reused for multiple requests. After completing all of your requests, call
4558
// the "close" method on the client to safely clean up any remaining background resources.
46-
try (AutoMlClient client = AutoMlClient.create()) {
59+
try (AutoMlClient client = AutoMlClient.create(settings)) {
4760
// Get the complete path of the dataset.
4861
DatasetName datasetFullId = DatasetName.of(projectId, "us-central1", datasetId);
4962

@@ -55,8 +68,22 @@ static void importDataset(String projectId, String datasetId, String path)
5568
InputConfig inputConfig = InputConfig.newBuilder().setGcsSource(gcsSource).build();
5669
System.out.println("Processing import...");
5770

58-
Empty response = client.importDataAsync(datasetFullId, inputConfig).get();
71+
// Start the import job
72+
OperationFuture<Empty, OperationMetadata> operation = client
73+
.importDataAsync(datasetFullId, inputConfig);
74+
75+
System.out.format("Operation name: %s%n", operation.getName());
76+
77+
// If you want to wait for the operation to finish, adjust the timeout appropriately. The
78+
// operation will still run if you choose not to wait for it to complete. You can check the
79+
// status of your operation using the operation's name.
80+
Empty response = operation.get(45, TimeUnit.MINUTES);
5981
System.out.format("Dataset imported. %s%n", response);
82+
} catch (TimeoutException e) {
83+
System.out.println("The operation's polling period was not long enough.");
84+
System.out.println("You can use the Operation's name to get the current status.");
85+
System.out.println("The import job is still running and will complete as expected.");
86+
throw e;
6087
}
6188
}
6289
}

automl/beta/src/test/java/com/example/automl/ImportDatasetTest.java

+3-1
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
import java.io.PrintStream;
2929
import java.util.UUID;
3030
import java.util.concurrent.ExecutionException;
31+
import java.util.concurrent.TimeoutException;
3132
import org.junit.After;
3233
import org.junit.Before;
3334
import org.junit.BeforeClass;
@@ -92,7 +93,8 @@ public void tearDown() throws InterruptedException, ExecutionException, IOExcept
9293
}
9394

9495
@Test
95-
public void testImportDataset() throws IOException, ExecutionException, InterruptedException {
96+
public void testImportDataset()
97+
throws InterruptedException, ExecutionException, TimeoutException, IOException {
9698
ImportDataset.importDataset(PROJECT_ID, datasetId, BUCKET + "/entity-extraction/dataset.csv");
9799
String got = bout.toString();
98100
assertThat(got).contains("Dataset imported.");

automl/beta/src/test/java/com/example/automl/TablesImportDatasetTest.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ public void tearDown() {
6565
@Test
6666
public void testTablesImportDataset() {
6767
try {
68-
ImportDataset.importDataset(
68+
TablesImportDataset.importDataset(
6969
PROJECT_ID, "TEN0000000000000000000", "gs://cloud-ml-tables-data/bank-marketing.csv");
7070
String got = bout.toString();
7171
assertThat(got).contains("The Dataset doesn't exist or is inaccessible for use with AutoMl.");

automl/cloud-client/pom.xml

+17-3
Original file line numberDiff line numberDiff line change
@@ -32,18 +32,30 @@
3232
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
3333
</properties>
3434

35+
<!-- [START automl_java_dependencies] -->
36+
<!-- Using libraries-bom to manage versions.
37+
See https://github.com/GoogleCloudPlatform/cloud-opensource-java/wiki/The-Google-Cloud-Platform-Libraries-BOM -->
38+
<dependencyManagement>
39+
<dependencies>
40+
<dependency>
41+
<groupId>com.google.cloud</groupId>
42+
<artifactId>libraries-bom</artifactId>
43+
<version>5.2.0</version>
44+
<type>pom</type>
45+
<scope>import</scope>
46+
</dependency>
47+
</dependencies>
48+
</dependencyManagement>
49+
3550
<dependencies>
36-
<!-- [START automl_java_dependencies] -->
3751
<dependency>
3852
<groupId>com.google.cloud</groupId>
3953
<artifactId>google-cloud-automl</artifactId>
40-
<version>1.1.1</version>
4154
</dependency>
4255
<!-- [END automl_java_dependencies] -->
4356
<dependency>
4457
<groupId>com.google.cloud</groupId>
4558
<artifactId>google-cloud-storage</artifactId>
46-
<version>1.107.0</version>
4759
</dependency>
4860
<dependency>
4961
<groupId>net.sourceforge.argparse4j</groupId>
@@ -64,5 +76,7 @@
6476
<version>1.0.1</version>
6577
<scope>test</scope>
6678
</dependency>
79+
<!-- [START automl_java_dependencies] -->
6780
</dependencies>
81+
<!-- [END automl_java_dependencies] -->
6882
</project>

automl/cloud-client/src/main/java/com/example/automl/ImportDataset.java

+22-4
Original file line numberDiff line numberDiff line change
@@ -17,19 +17,23 @@
1717
package com.example.automl;
1818

1919
// [START automl_import_dataset]
20+
import com.google.api.gax.longrunning.OperationFuture;
2021
import com.google.cloud.automl.v1.AutoMlClient;
2122
import com.google.cloud.automl.v1.DatasetName;
2223
import com.google.cloud.automl.v1.GcsSource;
2324
import com.google.cloud.automl.v1.InputConfig;
25+
import com.google.cloud.automl.v1.OperationMetadata;
2426
import com.google.protobuf.Empty;
2527
import java.io.IOException;
2628
import java.util.Arrays;
2729
import java.util.concurrent.ExecutionException;
30+
import java.util.concurrent.TimeUnit;
31+
import java.util.concurrent.TimeoutException;
2832

2933
class ImportDataset {
3034

3135
public static void main(String[] args)
32-
throws IOException, ExecutionException, InterruptedException {
36+
throws IOException, ExecutionException, InterruptedException, TimeoutException {
3337
// TODO(developer): Replace these variables before running the sample.
3438
String projectId = "YOUR_PROJECT_ID";
3539
String datasetId = "YOUR_DATASET_ID";
@@ -39,7 +43,7 @@ public static void main(String[] args)
3943

4044
// Import a dataset
4145
static void importDataset(String projectId, String datasetId, String path)
42-
throws IOException, ExecutionException, InterruptedException {
46+
throws IOException, ExecutionException, InterruptedException, TimeoutException {
4347
// Initialize client that will be used to send requests. This client only needs to be created
4448
// once, and can be reused for multiple requests. After completing all of your requests, call
4549
// the "close" method on the client to safely clean up any remaining background resources.
@@ -55,8 +59,22 @@ static void importDataset(String projectId, String datasetId, String path)
5559
InputConfig inputConfig = InputConfig.newBuilder().setGcsSource(gcsSource).build();
5660
System.out.println("Processing import...");
5761

58-
Empty response = client.importDataAsync(datasetFullId, inputConfig).get();
59-
System.out.format("Dataset imported. %s\n", response);
62+
// Start the import job
63+
OperationFuture<Empty, OperationMetadata> operation =
64+
client.importDataAsync(datasetFullId, inputConfig);
65+
66+
System.out.format("Operation name: %s%n", operation.getName());
67+
68+
// If you want to wait for the operation to finish, adjust the timeout appropriately. The
69+
// operation will still run if you choose not to wait for it to complete. You can check the
70+
// status of your operation using the operation's name.
71+
Empty response = operation.get(45, TimeUnit.MINUTES);
72+
System.out.format("Dataset imported. %s%n", response);
73+
} catch (TimeoutException e) {
74+
System.out.println("The operation's polling period was not long enough.");
75+
System.out.println("You can use the Operation's name to get the current status.");
76+
System.out.println("The import job is still running and will complete as expected.");
77+
throw e;
6078
}
6179
}
6280
}

automl/cloud-client/src/test/java/com/example/automl/ImportDatasetTest.java

+3-1
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
import java.io.PrintStream;
2525
import java.util.UUID;
2626
import java.util.concurrent.ExecutionException;
27+
import java.util.concurrent.TimeoutException;
2728
import org.junit.After;
2829
import org.junit.Before;
2930
import org.junit.BeforeClass;
@@ -83,7 +84,8 @@ public void tearDown() throws InterruptedException, ExecutionException, IOExcept
8384
}
8485

8586
@Test
86-
public void testImportDataset() throws IOException, ExecutionException, InterruptedException {
87+
public void testImportDataset()
88+
throws IOException, ExecutionException, InterruptedException, TimeoutException {
8789
ImportDataset.importDataset(PROJECT_ID, datasetId, BUCKET + "/entity-extraction/dataset.csv");
8890
String got = bout.toString();
8991
assertThat(got).contains("Dataset imported.");

0 commit comments

Comments
 (0)