Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* feat: adds samples for pitr Adds sample for creating a database with a version retention period. Updates sample for creating a backup with a version time. Updates sample for restoring a backup, printing the backup version time. * fix: prints out version time for backup sample * test: adds test to the pitr create db sample * test: cleans up after pitr sample test finishes * test: fixes checkstyle violations * samples: updates backup samples for pitr Uses the earliest version time from the database as the version time for the backup. * test: fixes pitr samples test * test: fixes checkstyle violations * samples: PITR samples backup fix (#921) * samples: creates a backup using the current time Instead of using the earliest version time of the database, uses the current time (from spanner). If we used the earliest version time of the database instead we would be creating an empty backup, since the database we are backing up is not 1 hour old (default version retention period). * samples: remove unused variable Clean up create backup samples by removing unused variable. * samples: parameterises create backup version time (#930) * samples: fixes spanner samples tests
- Loading branch information
1 parent
34c9b8d
commit 55fa0cc
Showing
4 changed files
with
230 additions
and
7 deletions.
There are no files selected for viewing
86 changes: 86 additions & 0 deletions
86
...ets/src/main/java/com/example/spanner/CreateDatabaseWithVersionRetentionPeriodSample.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
/* | ||
* Copyright 2021 Google LLC | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package com.example.spanner; | ||
|
||
// [START spanner_create_database_with_version_retention_period] | ||
|
||
import com.google.api.gax.longrunning.OperationFuture; | ||
import com.google.cloud.spanner.Database; | ||
import com.google.cloud.spanner.DatabaseAdminClient; | ||
import com.google.cloud.spanner.Spanner; | ||
import com.google.cloud.spanner.SpannerException; | ||
import com.google.cloud.spanner.SpannerExceptionFactory; | ||
import com.google.cloud.spanner.SpannerOptions; | ||
import com.google.spanner.admin.database.v1.CreateDatabaseMetadata; | ||
import java.util.Arrays; | ||
import java.util.concurrent.ExecutionException; | ||
|
||
public class CreateDatabaseWithVersionRetentionPeriodSample { | ||
|
||
static void createDatabaseWithVersionRetentionPeriod() { | ||
// TODO(developer): Replace these variables before running the sample. | ||
String projectId = "my-project"; | ||
String instanceId = "my-instance"; | ||
String databaseId = "my-database"; | ||
String versionRetentionPeriod = "7d"; | ||
|
||
try (Spanner spanner = | ||
SpannerOptions.newBuilder().setProjectId(projectId).build().getService()) { | ||
DatabaseAdminClient adminClient = spanner.getDatabaseAdminClient(); | ||
createDatabaseWithVersionRetentionPeriod(adminClient, instanceId, databaseId, | ||
versionRetentionPeriod); | ||
} | ||
} | ||
|
||
static void createDatabaseWithVersionRetentionPeriod(DatabaseAdminClient adminClient, | ||
String instanceId, String databaseId, String versionRetentionPeriod) { | ||
OperationFuture<Database, CreateDatabaseMetadata> op = | ||
adminClient.createDatabase( | ||
instanceId, | ||
databaseId, | ||
Arrays.asList( | ||
"CREATE TABLE Singers (" | ||
+ " SingerId INT64 NOT NULL," | ||
+ " FirstName STRING(1024)," | ||
+ " LastName STRING(1024)," | ||
+ " SingerInfo BYTES(MAX)" | ||
+ ") PRIMARY KEY (SingerId)", | ||
"CREATE TABLE Albums (" | ||
+ " SingerId INT64 NOT NULL," | ||
+ " AlbumId INT64 NOT NULL," | ||
+ " AlbumTitle STRING(MAX)" | ||
+ ") PRIMARY KEY (SingerId, AlbumId)," | ||
+ " INTERLEAVE IN PARENT Singers ON DELETE CASCADE", | ||
"ALTER DATABASE " + "`" + databaseId + "`" | ||
+ " SET OPTIONS ( version_retention_period = '" + versionRetentionPeriod + "' )" | ||
)); | ||
try { | ||
Database database = op.get(); | ||
System.out.println("Created database [" + database.getId() + "]"); | ||
System.out.println("\tVersion retention period: " + database.getVersionRetentionPeriod()); | ||
System.out.println("\tEarliest version time: " + database.getEarliestVersionTime()); | ||
} catch (ExecutionException e) { | ||
// If the operation failed during execution, expose the cause. | ||
throw (SpannerException) e.getCause(); | ||
} catch (InterruptedException e) { | ||
// Throw when a thread is waiting, sleeping, or otherwise occupied, | ||
// and the thread is interrupted, either before or during the activity. | ||
throw SpannerExceptionFactory.propagateInterrupt(e); | ||
} | ||
} | ||
} | ||
// [END spanner_create_database_with_version_retention_period] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
115 changes: 115 additions & 0 deletions
115
...s/src/test/java/com/example/spanner/CreateDatabaseWithVersionRetentionPeriodSampleIT.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,115 @@ | ||
/* | ||
* Copyright 2021 Google LLC | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package com.example.spanner; | ||
|
||
import static com.google.common.truth.Truth.assertThat; | ||
|
||
import com.google.cloud.spanner.DatabaseAdminClient; | ||
import com.google.cloud.spanner.Spanner; | ||
import com.google.cloud.spanner.SpannerOptions; | ||
import java.io.ByteArrayOutputStream; | ||
import java.io.PrintStream; | ||
import java.util.ArrayList; | ||
import java.util.List; | ||
import java.util.UUID; | ||
import org.junit.AfterClass; | ||
import org.junit.BeforeClass; | ||
import org.junit.Test; | ||
import org.junit.runner.RunWith; | ||
import org.junit.runners.JUnit4; | ||
|
||
/** | ||
* Integration tests for {@link CreateDatabaseWithVersionRetentionPeriodSample} | ||
*/ | ||
@RunWith(JUnit4.class) | ||
public class CreateDatabaseWithVersionRetentionPeriodSampleIT { | ||
|
||
private static String projectId; | ||
private static final String instanceId = System.getProperty("spanner.test.instance"); | ||
private static final String baseDatabaseId = System.getProperty( | ||
"spanner.sample.database", | ||
"pitrsample" | ||
); | ||
private static DatabaseAdminClient databaseAdminClient; | ||
private static List<String> databasesToDrop; | ||
private static Spanner spanner; | ||
|
||
private String runSample( | ||
String databaseId, | ||
String versionRetentionPeriod | ||
) { | ||
final PrintStream stdOut = System.out; | ||
final ByteArrayOutputStream bout = new ByteArrayOutputStream(); | ||
final PrintStream out = new PrintStream(bout); | ||
System.setOut(out); | ||
CreateDatabaseWithVersionRetentionPeriodSample.createDatabaseWithVersionRetentionPeriod( | ||
databaseAdminClient, | ||
instanceId, | ||
databaseId, | ||
versionRetentionPeriod | ||
); | ||
System.setOut(stdOut); | ||
return bout.toString(); | ||
} | ||
|
||
@BeforeClass | ||
public static void setUp() { | ||
final SpannerOptions options = SpannerOptions | ||
.newBuilder() | ||
.setAutoThrottleAdministrativeRequests() | ||
.build(); | ||
projectId = options.getProjectId(); | ||
spanner = options.getService(); | ||
databaseAdminClient = spanner.getDatabaseAdminClient(); | ||
databasesToDrop = new ArrayList<>(); | ||
} | ||
|
||
@AfterClass | ||
public static void tearDown() { | ||
for (String databaseId : databasesToDrop) { | ||
try { | ||
databaseAdminClient.dropDatabase(instanceId, databaseId); | ||
} catch (Exception e) { | ||
System.out.println("Failed to drop database " + databaseId + ", skipping..."); | ||
} | ||
} | ||
spanner.close(); | ||
} | ||
|
||
@Test | ||
public void createsDatabaseWithVersionRetentionPeriod() { | ||
final String databaseId = generateDatabaseId(); | ||
final String versionRetentionPeriod = "7d"; | ||
|
||
final String out = runSample(databaseId, versionRetentionPeriod); | ||
|
||
assertThat(out).contains( | ||
"Created database [projects/" + projectId + "/instances/" + instanceId + "/databases/" | ||
+ databaseId + "]"); | ||
assertThat(out).contains("Version retention period: " + versionRetentionPeriod); | ||
} | ||
|
||
static String generateDatabaseId() { | ||
final String databaseId = ( | ||
baseDatabaseId | ||
+ "-" | ||
+ UUID.randomUUID().toString().replaceAll("-", "") | ||
).substring(0, 30); | ||
databasesToDrop.add(databaseId); | ||
return databaseId; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters