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

feat: adds samples for PITR #837

Merged
merged 15 commits into from Mar 9, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
@@ -0,0 +1,83 @@
/*
* 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]
thiagotnunes marked this conversation as resolved.
Show resolved Hide resolved
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";

try (Spanner spanner =
SpannerOptions.newBuilder().setProjectId(projectId).build().getService()) {
DatabaseAdminClient adminClient = spanner.getDatabaseAdminClient();
createDatabaseWithVersionRetentionPeriod(adminClient, instanceId, databaseId);
}
}

static void createDatabaseWithVersionRetentionPeriod(DatabaseAdminClient adminClient,
thiagotnunes marked this conversation as resolved.
Show resolved Hide resolved
String instanceId, String databaseId) {
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 = '7d' )"
));
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]
Expand Up @@ -1589,11 +1589,14 @@ static void createBackup(
// Set expire time to 14 days from now.
Timestamp expireTime = Timestamp.ofTimeMicroseconds(TimeUnit.MICROSECONDS.convert(
System.currentTimeMillis() + TimeUnit.DAYS.toMillis(14), TimeUnit.MILLISECONDS));
// Sets the version time to the current time.
Timestamp versionTime = Timestamp.now();
thiagotnunes marked this conversation as resolved.
Show resolved Hide resolved
Backup backup =
dbAdminClient
.newBackupBuilder(backupId)
.setDatabase(databaseId)
.setExpireTime(expireTime)
.setVersionTime(versionTime)
.build();
// Initiate the request which returns an OperationFuture.
System.out.println("Creating backup [" + backup.getId() + "]...");
Expand Down Expand Up @@ -1820,12 +1823,16 @@ static void restoreBackup(
Database db = op.get();
// Refresh database metadata and get the restore info.
RestoreInfo restore = db.reload().getRestoreInfo();
Timestamp versionTime = Timestamp.fromProto(restore
.getProto()
.getBackupInfo()
.getVersionTime());
System.out.println(
"Restored database ["
+ restore.getSourceDatabase().getName()
+ "] from ["
+ restore.getBackup().getName()
+ "]");
+ "] with version time [" + versionTime + "]");
} catch (ExecutionException e) {
throw SpannerExceptionFactory.newSpannerException(e.getCause());
} catch (InterruptedException e) {
Expand Down