From 46244736b9db55e4d181c3373350f579861718ef Mon Sep 17 00:00:00 2001 From: Noah Negrey Date: Fri, 24 Apr 2020 16:31:45 -0600 Subject: [PATCH] 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 --- .../com/example/automl/ImportDataset.java | 26 ++++++++++++++++--- .../com/example/automl/ImportDatasetTest.java | 4 ++- 2 files changed, 25 insertions(+), 5 deletions(-) diff --git a/automl/snippets/src/main/java/com/example/automl/ImportDataset.java b/automl/snippets/src/main/java/com/example/automl/ImportDataset.java index 01996669f6a..3ead88326b3 100644 --- a/automl/snippets/src/main/java/com/example/automl/ImportDataset.java +++ b/automl/snippets/src/main/java/com/example/automl/ImportDataset.java @@ -17,19 +17,23 @@ package com.example.automl; // [START automl_import_dataset] +import com.google.api.gax.longrunning.OperationFuture; import com.google.cloud.automl.v1.AutoMlClient; import com.google.cloud.automl.v1.DatasetName; import com.google.cloud.automl.v1.GcsSource; import com.google.cloud.automl.v1.InputConfig; +import com.google.cloud.automl.v1.OperationMetadata; import com.google.protobuf.Empty; import java.io.IOException; import java.util.Arrays; import java.util.concurrent.ExecutionException; +import java.util.concurrent.TimeUnit; +import java.util.concurrent.TimeoutException; class ImportDataset { public static void main(String[] args) - throws IOException, ExecutionException, InterruptedException { + throws IOException, ExecutionException, InterruptedException, TimeoutException { // TODO(developer): Replace these variables before running the sample. String projectId = "YOUR_PROJECT_ID"; String datasetId = "YOUR_DATASET_ID"; @@ -39,7 +43,7 @@ public static void main(String[] args) // Import a dataset static void importDataset(String projectId, String datasetId, String path) - throws IOException, ExecutionException, InterruptedException { + throws IOException, ExecutionException, InterruptedException, TimeoutException { // Initialize client that will be used to send requests. This client only needs to be created // once, and can be reused for multiple requests. After completing all of your requests, call // 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) InputConfig inputConfig = InputConfig.newBuilder().setGcsSource(gcsSource).build(); System.out.println("Processing import..."); - Empty response = client.importDataAsync(datasetFullId, inputConfig).get(); - System.out.format("Dataset imported. %s\n", response); + // Start the import job + OperationFuture operation = + client.importDataAsync(datasetFullId, inputConfig); + + System.out.format("Operation name: %s%n", operation.getName()); + + // If you want to wait for the operation to finish, adjust the timeout appropriately. The + // operation will still run if you choose not to wait for it to complete. You can check the + // status of your operation using the operation's name. + Empty response = operation.get(45, TimeUnit.MINUTES); + System.out.format("Dataset imported. %s%n", response); + } catch (TimeoutException e) { + System.out.println("The operation's polling period was not long enough."); + System.out.println("You can use the Operation's name to get the current status."); + System.out.println("The import job is still running and will complete as expected."); + throw e; } } } diff --git a/automl/snippets/src/test/java/com/example/automl/ImportDatasetTest.java b/automl/snippets/src/test/java/com/example/automl/ImportDatasetTest.java index 164c6445c94..9933a5d7d1a 100644 --- a/automl/snippets/src/test/java/com/example/automl/ImportDatasetTest.java +++ b/automl/snippets/src/test/java/com/example/automl/ImportDatasetTest.java @@ -24,6 +24,7 @@ import java.io.PrintStream; import java.util.UUID; import java.util.concurrent.ExecutionException; +import java.util.concurrent.TimeoutException; import org.junit.After; import org.junit.Before; import org.junit.BeforeClass; @@ -83,7 +84,8 @@ public void tearDown() throws InterruptedException, ExecutionException, IOExcept } @Test - public void testImportDataset() throws IOException, ExecutionException, InterruptedException { + public void testImportDataset() + throws IOException, ExecutionException, InterruptedException, TimeoutException { ImportDataset.importDataset(PROJECT_ID, datasetId, BUCKET + "/entity-extraction/dataset.csv"); String got = bout.toString(); assertThat(got).contains("Dataset imported.");