From 249f294e465d153635796cc6b1c443a50f315df1 Mon Sep 17 00:00:00 2001 From: Praful Makani Date: Fri, 25 Sep 2020 11:45:43 +0530 Subject: [PATCH 1/2] docs(samples): fix flaky test case for undelete table --- .../com/example/bigquery/UndeleteTable.java | 22 ++++++++++++++----- .../com/example/bigquery/UndeleteTableIT.java | 5 ++--- 2 files changed, 19 insertions(+), 8 deletions(-) diff --git a/samples/snippets/src/main/java/com/example/bigquery/UndeleteTable.java b/samples/snippets/src/main/java/com/example/bigquery/UndeleteTable.java index 17deac762..641c840a6 100644 --- a/samples/snippets/src/main/java/com/example/bigquery/UndeleteTable.java +++ b/samples/snippets/src/main/java/com/example/bigquery/UndeleteTable.java @@ -23,7 +23,9 @@ import com.google.cloud.bigquery.CopyJobConfiguration; import com.google.cloud.bigquery.Job; import com.google.cloud.bigquery.JobInfo; +import com.google.cloud.bigquery.Table; import com.google.cloud.bigquery.TableId; +import org.threeten.bp.Instant; // Sample to undeleting a table public class UndeleteTable { @@ -42,15 +44,25 @@ public static void undeleteTable(String datasetName, String tableName, String re // once, and can be reused for multiple requests. BigQuery bigquery = BigQueryOptions.getDefaultInstance().getService(); - // "Accidentally" delete the table. - bigquery.delete(TableId.of(datasetName, tableName)); - // Record the current time. We'll use this as the snapshot time // for recovering the table. - long snapTime = System.currentTimeMillis(); + long snapshot_epoch = Instant.now().toEpochMilli(); + + // [START_EXCLUDE] + // Due to very short lifecycle of the table, ensure we're not picking a time + // prior to the table creation due to time drift between backend and client. + Table table = bigquery.getTable(TableId.of(datasetName, tableName)); + Long created_epoch = table.getCreationTime(); + if (created_epoch > snapshot_epoch) { + snapshot_epoch = created_epoch; + } + // [END_EXCLUDE] + + // "Accidentally" delete the table. + bigquery.delete(TableId.of(datasetName, tableName)); // Construct the restore-from tableID using a snapshot decorator. - String snapshotTableId = String.format("%s@%d", tableName, snapTime); + String snapshotTableId = String.format("%s@%d", tableName, snapshot_epoch); // Construct and run a copy job. CopyJobConfiguration configuration = diff --git a/samples/snippets/src/test/java/com/example/bigquery/UndeleteTableIT.java b/samples/snippets/src/test/java/com/example/bigquery/UndeleteTableIT.java index 61a91341f..f6c725db6 100644 --- a/samples/snippets/src/test/java/com/example/bigquery/UndeleteTableIT.java +++ b/samples/snippets/src/test/java/com/example/bigquery/UndeleteTableIT.java @@ -77,8 +77,7 @@ public void tearDown() { @Test public void testUndeleteTable() { - // TODO(pmakani): revisit this in the future due to recent flakiness - // UndeleteTable.undeleteTable(BIGQUERY_DATASET_NAME, tableName, recoverTableName); - // assertThat(bout.toString()).contains("Undelete table recovered successfully."); + UndeleteTable.undeleteTable(BIGQUERY_DATASET_NAME, tableName, recoverTableName); + assertThat(bout.toString()).contains("Undelete table recovered successfully."); } } From 3f64b234d01e4c2731d5f54972d793d97c67774f Mon Sep 17 00:00:00 2001 From: Praful Makani Date: Fri, 25 Sep 2020 12:13:29 +0530 Subject: [PATCH 2/2] docs(samples): lint --- .../main/java/com/example/bigquery/UndeleteTable.java | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/samples/snippets/src/main/java/com/example/bigquery/UndeleteTable.java b/samples/snippets/src/main/java/com/example/bigquery/UndeleteTable.java index 641c840a6..cc09afbcb 100644 --- a/samples/snippets/src/main/java/com/example/bigquery/UndeleteTable.java +++ b/samples/snippets/src/main/java/com/example/bigquery/UndeleteTable.java @@ -46,15 +46,15 @@ public static void undeleteTable(String datasetName, String tableName, String re // Record the current time. We'll use this as the snapshot time // for recovering the table. - long snapshot_epoch = Instant.now().toEpochMilli(); + long snapshotEpoch = Instant.now().toEpochMilli(); // [START_EXCLUDE] // Due to very short lifecycle of the table, ensure we're not picking a time // prior to the table creation due to time drift between backend and client. Table table = bigquery.getTable(TableId.of(datasetName, tableName)); - Long created_epoch = table.getCreationTime(); - if (created_epoch > snapshot_epoch) { - snapshot_epoch = created_epoch; + Long createdEpoch = table.getCreationTime(); + if (createdEpoch > snapshotEpoch) { + snapshotEpoch = createdEpoch; } // [END_EXCLUDE] @@ -62,7 +62,7 @@ public static void undeleteTable(String datasetName, String tableName, String re bigquery.delete(TableId.of(datasetName, tableName)); // Construct the restore-from tableID using a snapshot decorator. - String snapshotTableId = String.format("%s@%d", tableName, snapshot_epoch); + String snapshotTableId = String.format("%s@%d", tableName, snapshotEpoch); // Construct and run a copy job. CopyJobConfiguration configuration =