Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

automl: explictly update timeoutes due to default library changes #2719

Merged
merged 14 commits into from
Apr 24, 2020
3 changes: 1 addition & 2 deletions automl/beta/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@
<dependency>
<groupId>com.google.cloud</groupId>
<artifactId>libraries-bom</artifactId>
<version>4.4.1</version>
<version>5.2.0</version>
<type>pom</type>
<scope>import</scope>
</dependency>
Expand All @@ -60,7 +60,6 @@
<dependency>
<groupId>com.google.cloud</groupId>
<artifactId>google-cloud-storage</artifactId>
<version>1.107.0</version>
</dependency>
<dependency>
<groupId>net.sourceforge.argparse4j</groupId>
Expand Down
35 changes: 31 additions & 4 deletions automl/beta/src/main/java/com/example/automl/ImportDataset.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,19 +17,26 @@
package com.example.automl;

// [START automl_import_dataset_beta]
import com.google.api.gax.longrunning.OperationFuture;
import com.google.api.gax.retrying.RetrySettings;
import com.google.cloud.automl.v1beta1.AutoMlClient;
import com.google.cloud.automl.v1beta1.AutoMlSettings;
import com.google.cloud.automl.v1beta1.DatasetName;
import com.google.cloud.automl.v1beta1.GcsSource;
import com.google.cloud.automl.v1beta1.InputConfig;
import com.google.cloud.automl.v1beta1.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;
import org.threeten.bp.Duration;

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";
Expand All @@ -39,11 +46,17 @@ 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 {
Duration totalTimeout = Duration.ofMinutes(45);
RetrySettings retrySettings = RetrySettings.newBuilder().setTotalTimeout(totalTimeout).build();
AutoMlSettings.Builder builder = AutoMlSettings.newBuilder();
builder.importDataSettings().setRetrySettings(retrySettings).build();
AutoMlSettings settings = builder.build();

// 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.
try (AutoMlClient client = AutoMlClient.create()) {
try (AutoMlClient client = AutoMlClient.create(settings)) {
// Get the complete path of the dataset.
DatasetName datasetFullId = DatasetName.of(projectId, "us-central1", datasetId);

Expand All @@ -55,8 +68,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();
// Start the import job
OperationFuture<Empty, OperationMetadata> 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;
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,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;
Expand Down Expand Up @@ -92,7 +93,8 @@ public void tearDown() throws InterruptedException, ExecutionException, IOExcept
}

@Test
public void testImportDataset() throws IOException, ExecutionException, InterruptedException {
public void testImportDataset()
throws InterruptedException, ExecutionException, TimeoutException, IOException {
ImportDataset.importDataset(PROJECT_ID, datasetId, BUCKET + "/entity-extraction/dataset.csv");
String got = bout.toString();
assertThat(got).contains("Dataset imported.");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ public void tearDown() {
@Test
public void testTablesImportDataset() {
try {
ImportDataset.importDataset(
TablesImportDataset.importDataset(
PROJECT_ID, "TEN0000000000000000000", "gs://cloud-ml-tables-data/bank-marketing.csv");
String got = bout.toString();
assertThat(got).contains("The Dataset doesn't exist or is inaccessible for use with AutoMl.");
Expand Down
20 changes: 17 additions & 3 deletions automl/cloud-client/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -32,18 +32,30 @@
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>

<!-- [START automl_java_dependencies] -->
<!-- Using libraries-bom to manage versions.
See https://github.com/GoogleCloudPlatform/cloud-opensource-java/wiki/The-Google-Cloud-Platform-Libraries-BOM -->
<dependencyManagement>
<dependencies>
<dependency>
<groupId>com.google.cloud</groupId>
<artifactId>libraries-bom</artifactId>
<version>5.2.0</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>

<dependencies>
<!-- [START automl_java_dependencies] -->
<dependency>
<groupId>com.google.cloud</groupId>
<artifactId>google-cloud-automl</artifactId>
<version>1.1.1</version>
</dependency>
<!-- [END automl_java_dependencies] -->
<dependency>
<groupId>com.google.cloud</groupId>
<artifactId>google-cloud-storage</artifactId>
<version>1.107.0</version>
</dependency>
<dependency>
<groupId>net.sourceforge.argparse4j</groupId>
Expand All @@ -64,5 +76,7 @@
<version>1.0.1</version>
<scope>test</scope>
</dependency>
<!-- [START automl_java_dependencies] -->
</dependencies>
<!-- [END automl_java_dependencies] -->
</project>
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand All @@ -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.
Expand All @@ -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<Empty, OperationMetadata> 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;
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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.");
Expand Down