Skip to content

Commit 55fa0cc

Browse files
authored
feat: adds samples for PITR (#837)
* 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
1 parent 34c9b8d commit 55fa0cc

File tree

4 files changed

+230
-7
lines changed

4 files changed

+230
-7
lines changed
Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
/*
2+
* Copyright 2021 Google LLC
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package com.example.spanner;
18+
19+
// [START spanner_create_database_with_version_retention_period]
20+
21+
import com.google.api.gax.longrunning.OperationFuture;
22+
import com.google.cloud.spanner.Database;
23+
import com.google.cloud.spanner.DatabaseAdminClient;
24+
import com.google.cloud.spanner.Spanner;
25+
import com.google.cloud.spanner.SpannerException;
26+
import com.google.cloud.spanner.SpannerExceptionFactory;
27+
import com.google.cloud.spanner.SpannerOptions;
28+
import com.google.spanner.admin.database.v1.CreateDatabaseMetadata;
29+
import java.util.Arrays;
30+
import java.util.concurrent.ExecutionException;
31+
32+
public class CreateDatabaseWithVersionRetentionPeriodSample {
33+
34+
static void createDatabaseWithVersionRetentionPeriod() {
35+
// TODO(developer): Replace these variables before running the sample.
36+
String projectId = "my-project";
37+
String instanceId = "my-instance";
38+
String databaseId = "my-database";
39+
String versionRetentionPeriod = "7d";
40+
41+
try (Spanner spanner =
42+
SpannerOptions.newBuilder().setProjectId(projectId).build().getService()) {
43+
DatabaseAdminClient adminClient = spanner.getDatabaseAdminClient();
44+
createDatabaseWithVersionRetentionPeriod(adminClient, instanceId, databaseId,
45+
versionRetentionPeriod);
46+
}
47+
}
48+
49+
static void createDatabaseWithVersionRetentionPeriod(DatabaseAdminClient adminClient,
50+
String instanceId, String databaseId, String versionRetentionPeriod) {
51+
OperationFuture<Database, CreateDatabaseMetadata> op =
52+
adminClient.createDatabase(
53+
instanceId,
54+
databaseId,
55+
Arrays.asList(
56+
"CREATE TABLE Singers ("
57+
+ " SingerId INT64 NOT NULL,"
58+
+ " FirstName STRING(1024),"
59+
+ " LastName STRING(1024),"
60+
+ " SingerInfo BYTES(MAX)"
61+
+ ") PRIMARY KEY (SingerId)",
62+
"CREATE TABLE Albums ("
63+
+ " SingerId INT64 NOT NULL,"
64+
+ " AlbumId INT64 NOT NULL,"
65+
+ " AlbumTitle STRING(MAX)"
66+
+ ") PRIMARY KEY (SingerId, AlbumId),"
67+
+ " INTERLEAVE IN PARENT Singers ON DELETE CASCADE",
68+
"ALTER DATABASE " + "`" + databaseId + "`"
69+
+ " SET OPTIONS ( version_retention_period = '" + versionRetentionPeriod + "' )"
70+
));
71+
try {
72+
Database database = op.get();
73+
System.out.println("Created database [" + database.getId() + "]");
74+
System.out.println("\tVersion retention period: " + database.getVersionRetentionPeriod());
75+
System.out.println("\tEarliest version time: " + database.getEarliestVersionTime());
76+
} catch (ExecutionException e) {
77+
// If the operation failed during execution, expose the cause.
78+
throw (SpannerException) e.getCause();
79+
} catch (InterruptedException e) {
80+
// Throw when a thread is waiting, sleeping, or otherwise occupied,
81+
// and the thread is interrupted, either before or during the activity.
82+
throw SpannerExceptionFactory.propagateInterrupt(e);
83+
}
84+
}
85+
}
86+
// [END spanner_create_database_with_version_retention_period]

samples/snippets/src/main/java/com/example/spanner/SpannerSample.java

Lines changed: 28 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1584,8 +1584,8 @@ static void queryWithQueryOptions(DatabaseClient dbClient) {
15841584
// [END spanner_query_with_query_options]
15851585

15861586
// [START spanner_create_backup]
1587-
static void createBackup(
1588-
DatabaseAdminClient dbAdminClient, DatabaseId databaseId, BackupId backupId) {
1587+
static void createBackup(DatabaseAdminClient dbAdminClient, DatabaseId databaseId,
1588+
BackupId backupId, Timestamp versionTime) {
15891589
// Set expire time to 14 days from now.
15901590
Timestamp expireTime = Timestamp.ofTimeMicroseconds(TimeUnit.MICROSECONDS.convert(
15911591
System.currentTimeMillis() + TimeUnit.DAYS.toMillis(14), TimeUnit.MILLISECONDS));
@@ -1594,6 +1594,7 @@ static void createBackup(
15941594
.newBackupBuilder(backupId)
15951595
.setDatabase(databaseId)
15961596
.setExpireTime(expireTime)
1597+
.setVersionTime(versionTime)
15971598
.build();
15981599
// Initiate the request which returns an OperationFuture.
15991600
System.out.println("Creating backup [" + backup.getId() + "]...");
@@ -1612,13 +1613,18 @@ static void createBackup(
16121613
backup = backup.reload();
16131614
System.out.println(
16141615
String.format(
1615-
"Backup %s of size %d bytes was created at %s",
1616+
"Backup %s of size %d bytes was created at %s for version of database at %s",
16161617
backup.getId().getName(),
16171618
backup.getSize(),
16181619
LocalDateTime.ofEpochSecond(
16191620
backup.getProto().getCreateTime().getSeconds(),
16201621
backup.getProto().getCreateTime().getNanos(),
1621-
OffsetDateTime.now().getOffset())).toString());
1622+
OffsetDateTime.now().getOffset()),
1623+
LocalDateTime.ofEpochSecond(
1624+
backup.getProto().getVersionTime().getSeconds(),
1625+
backup.getProto().getVersionTime().getNanos(),
1626+
OffsetDateTime.now().getOffset())
1627+
));
16221628
}
16231629
// [END spanner_create_backup]
16241630

@@ -1820,12 +1826,16 @@ static void restoreBackup(
18201826
Database db = op.get();
18211827
// Refresh database metadata and get the restore info.
18221828
RestoreInfo restore = db.reload().getRestoreInfo();
1829+
Timestamp versionTime = Timestamp.fromProto(restore
1830+
.getProto()
1831+
.getBackupInfo()
1832+
.getVersionTime());
18231833
System.out.println(
18241834
"Restored database ["
18251835
+ restore.getSourceDatabase().getName()
18261836
+ "] from ["
18271837
+ restore.getBackup().getName()
1828-
+ "]");
1838+
+ "] with version time [" + versionTime + "]");
18291839
} catch (ExecutionException e) {
18301840
throw SpannerExceptionFactory.newSpannerException(e.getCause());
18311841
} catch (InterruptedException e) {
@@ -2044,7 +2054,7 @@ static void run(
20442054
queryWithQueryOptions(dbClient);
20452055
break;
20462056
case "createbackup":
2047-
createBackup(dbAdminClient, database, backup);
2057+
createBackup(dbAdminClient, database, backup, getVersionTime(dbClient));
20482058
break;
20492059
case "cancelcreatebackup":
20502060
cancelCreateBackup(
@@ -2079,6 +2089,17 @@ static void run(
20792089
}
20802090
}
20812091

2092+
static Timestamp getVersionTime(DatabaseClient dbClient) {
2093+
// Generates a version time for the backup
2094+
Timestamp versionTime;
2095+
try (ResultSet resultSet = dbClient.singleUse()
2096+
.executeQuery(Statement.of("SELECT CURRENT_TIMESTAMP()"))) {
2097+
resultSet.next();
2098+
versionTime = resultSet.getTimestamp(0);
2099+
}
2100+
return versionTime;
2101+
}
2102+
20822103
static void printUsageAndExit() {
20832104
System.err.println("Usage:");
20842105
System.err.println(" SpannerExample <command> <instance_id> <database_id>");
@@ -2177,6 +2198,7 @@ public static void main(String[] args) throws Exception {
21772198
InstanceAdminClient instanceAdminClient = spanner.getInstanceAdminClient();
21782199
// Use client here...
21792200
// [END init_client]
2201+
21802202
run(dbClient, dbAdminClient, instanceAdminClient, command, db, backup);
21812203
// [START init_client]
21822204
} finally {
Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
1+
/*
2+
* Copyright 2021 Google LLC
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package com.example.spanner;
18+
19+
import static com.google.common.truth.Truth.assertThat;
20+
21+
import com.google.cloud.spanner.DatabaseAdminClient;
22+
import com.google.cloud.spanner.Spanner;
23+
import com.google.cloud.spanner.SpannerOptions;
24+
import java.io.ByteArrayOutputStream;
25+
import java.io.PrintStream;
26+
import java.util.ArrayList;
27+
import java.util.List;
28+
import java.util.UUID;
29+
import org.junit.AfterClass;
30+
import org.junit.BeforeClass;
31+
import org.junit.Test;
32+
import org.junit.runner.RunWith;
33+
import org.junit.runners.JUnit4;
34+
35+
/**
36+
* Integration tests for {@link CreateDatabaseWithVersionRetentionPeriodSample}
37+
*/
38+
@RunWith(JUnit4.class)
39+
public class CreateDatabaseWithVersionRetentionPeriodSampleIT {
40+
41+
private static String projectId;
42+
private static final String instanceId = System.getProperty("spanner.test.instance");
43+
private static final String baseDatabaseId = System.getProperty(
44+
"spanner.sample.database",
45+
"pitrsample"
46+
);
47+
private static DatabaseAdminClient databaseAdminClient;
48+
private static List<String> databasesToDrop;
49+
private static Spanner spanner;
50+
51+
private String runSample(
52+
String databaseId,
53+
String versionRetentionPeriod
54+
) {
55+
final PrintStream stdOut = System.out;
56+
final ByteArrayOutputStream bout = new ByteArrayOutputStream();
57+
final PrintStream out = new PrintStream(bout);
58+
System.setOut(out);
59+
CreateDatabaseWithVersionRetentionPeriodSample.createDatabaseWithVersionRetentionPeriod(
60+
databaseAdminClient,
61+
instanceId,
62+
databaseId,
63+
versionRetentionPeriod
64+
);
65+
System.setOut(stdOut);
66+
return bout.toString();
67+
}
68+
69+
@BeforeClass
70+
public static void setUp() {
71+
final SpannerOptions options = SpannerOptions
72+
.newBuilder()
73+
.setAutoThrottleAdministrativeRequests()
74+
.build();
75+
projectId = options.getProjectId();
76+
spanner = options.getService();
77+
databaseAdminClient = spanner.getDatabaseAdminClient();
78+
databasesToDrop = new ArrayList<>();
79+
}
80+
81+
@AfterClass
82+
public static void tearDown() {
83+
for (String databaseId : databasesToDrop) {
84+
try {
85+
databaseAdminClient.dropDatabase(instanceId, databaseId);
86+
} catch (Exception e) {
87+
System.out.println("Failed to drop database " + databaseId + ", skipping...");
88+
}
89+
}
90+
spanner.close();
91+
}
92+
93+
@Test
94+
public void createsDatabaseWithVersionRetentionPeriod() {
95+
final String databaseId = generateDatabaseId();
96+
final String versionRetentionPeriod = "7d";
97+
98+
final String out = runSample(databaseId, versionRetentionPeriod);
99+
100+
assertThat(out).contains(
101+
"Created database [projects/" + projectId + "/instances/" + instanceId + "/databases/"
102+
+ databaseId + "]");
103+
assertThat(out).contains("Version retention period: " + versionRetentionPeriod);
104+
}
105+
106+
static String generateDatabaseId() {
107+
final String databaseId = (
108+
baseDatabaseId
109+
+ "-"
110+
+ UUID.randomUUID().toString().replaceAll("-", "")
111+
).substring(0, 30);
112+
databasesToDrop.add(databaseId);
113+
return databaseId;
114+
}
115+
}

samples/snippets/src/test/java/com/example/spanner/SpannerSampleIT.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -439,4 +439,4 @@ private static Pattern getTestDbIdPattern(String baseDbId) {
439439
static String formatForTest(String name) {
440440
return name + "-" + UUID.randomUUID().toString().substring(0, DBID_LENGTH);
441441
}
442-
}
442+
}

0 commit comments

Comments
 (0)