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

docs(samples): fix flaky test case for undelete table #757

Merged
merged 2 commits into from Sep 25, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Expand Up @@ -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 {
Expand All @@ -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 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 createdEpoch = table.getCreationTime();
if (createdEpoch > snapshotEpoch) {
snapshotEpoch = createdEpoch;
}
// [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, snapshotEpoch);

// Construct and run a copy job.
CopyJobConfiguration configuration =
Expand Down
Expand Up @@ -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.");
}
}