Skip to content

Commit 06061c9

Browse files
nnegreyanguillanneuf
authored andcommitted
samples: 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 7c252da commit 06061c9

File tree

2 files changed

+25
-5
lines changed

2 files changed

+25
-5
lines changed

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

Lines changed: 22 additions & 4 deletions
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/snippets/src/test/java/com/example/automl/ImportDatasetTest.java

Lines changed: 3 additions & 1 deletion
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)