Search in sources :

Example 1 with Restore

use of com.google.cloud.spanner.Restore in project java-spanner by googleapis.

the class RestoreBackupWithEncryptionKey method restoreBackupWithEncryptionKey.

static Void restoreBackupWithEncryptionKey(DatabaseAdminClient adminClient, String projectId, String instanceId, String backupId, String restoreId, String kmsKeyName) {
    final Restore restore = adminClient.newRestoreBuilder(BackupId.of(projectId, instanceId, backupId), DatabaseId.of(projectId, instanceId, restoreId)).setEncryptionConfig(EncryptionConfigs.customerManagedEncryption(kmsKeyName)).build();
    final OperationFuture<Database, RestoreDatabaseMetadata> operation = adminClient.restoreDatabase(restore);
    Database database;
    try {
        System.out.println("Waiting for operation to complete...");
        database = operation.get();
    } catch (ExecutionException e) {
        // If the operation failed during execution, expose the cause.
        throw SpannerExceptionFactory.asSpannerException(e.getCause());
    } catch (InterruptedException e) {
        // and the thread is interrupted, either before or during the activity.
        throw SpannerExceptionFactory.propagateInterrupt(e);
    }
    System.out.printf("Database %s restored to %s from backup %s using encryption key %s%n", database.getRestoreInfo().getSourceDatabase(), database.getId(), database.getRestoreInfo().getBackup(), database.getEncryptionConfig().getKmsKeyName());
    return null;
}
Also used : Database(com.google.cloud.spanner.Database) RestoreDatabaseMetadata(com.google.spanner.admin.database.v1.RestoreDatabaseMetadata) ExecutionException(java.util.concurrent.ExecutionException) Restore(com.google.cloud.spanner.Restore)

Example 2 with Restore

use of com.google.cloud.spanner.Restore in project java-spanner by googleapis.

the class ITBackupTest method testRestore.

private void testRestore(Backup backup, Timestamp versionTime, String expectedKey) throws InterruptedException, ExecutionException {
    // Restore the backup to a new database.
    String restoredDb = testHelper.getUniqueDatabaseId();
    String restoreOperationName;
    OperationFuture<Database, RestoreDatabaseMetadata> restoreOperation;
    int attempts = 0;
    while (true) {
        try {
            logger.info(String.format("Restoring backup %s to database %s", backup.getId().getBackup(), restoredDb));
            final Restore restore = dbAdminClient.newRestoreBuilder(backup.getId(), DatabaseId.of(projectId, instanceId, restoredDb)).setEncryptionConfig(EncryptionConfigs.customerManagedEncryption(expectedKey)).build();
            restoreOperation = dbAdminClient.restoreDatabase(restore);
            restoreOperationName = restoreOperation.getName();
            break;
        } catch (ExecutionException e) {
            if (e.getCause() instanceof FailedPreconditionException && e.getCause().getMessage().contains("Please retry the operation once the pending restores complete")) {
                attempts++;
                if (attempts == 10) {
                    logger.info("Restore operation failed 10 times because of other pending restores. Skipping restore test.");
                    return;
                }
                // wait and then retry.
                logger.info(String.format("Restoring backup %s to database %s must wait because of other pending restore operation", backup.getId().getBackup(), restoredDb));
                // noinspection BusyWait
                Thread.sleep(60_000L);
            } else {
                throw e;
            }
        }
    }
    databases.add(restoredDb);
    logger.info(String.format("Restore operation %s running", restoreOperationName));
    RestoreDatabaseMetadata metadata = restoreOperation.getMetadata().get();
    assertEquals(backup.getId().getName(), metadata.getBackupInfo().getBackup());
    assertEquals(RestoreSourceType.BACKUP, metadata.getSourceType());
    assertEquals(DatabaseId.of(testHelper.getInstanceId(), restoredDb).getName(), metadata.getName());
    assertEquals(versionTime, Timestamp.fromProto(metadata.getBackupInfo().getVersionTime()));
    // Ensure the operations show up in the right collections.
    // TODO: Re-enable when it is clear why this fails on the CI environment.
    // verifyRestoreOperations(backupOp.getName(), restoreOperationName);
    // Wait until the restore operation has finished successfully.
    Database database = restoreOperation.get();
    assertEquals(restoredDb, database.getId().getDatabase());
    // Reloads the database
    final Database reloadedDatabase = database.reload();
    assertNotNull(reloadedDatabase.getProto());
    assertEquals(versionTime, Timestamp.fromProto(reloadedDatabase.getProto().getRestoreInfo().getBackupInfo().getVersionTime()));
    testDatabaseEncryption(reloadedDatabase, expectedKey);
    testDatabaseDialect(reloadedDatabase, Dialect.GOOGLE_STANDARD_SQL);
    // Restoring the backup to an existing database should fail.
    logger.info(String.format("Restoring backup %s to existing database %s", backup.getId().getBackup(), restoredDb));
    ExecutionException executionException = assertThrows(ExecutionException.class, () -> backup.restore(DatabaseId.of(testHelper.getInstanceId(), restoredDb)).get());
    assertEquals(SpannerException.class, executionException.getCause().getClass());
    SpannerException spannerException = (SpannerException) executionException.getCause();
    assertEquals(ErrorCode.ALREADY_EXISTS, spannerException.getErrorCode());
}
Also used : Database(com.google.cloud.spanner.Database) FailedPreconditionException(com.google.api.gax.rpc.FailedPreconditionException) RestoreDatabaseMetadata(com.google.spanner.admin.database.v1.RestoreDatabaseMetadata) SpannerException(com.google.cloud.spanner.SpannerException) ExecutionException(java.util.concurrent.ExecutionException) Restore(com.google.cloud.spanner.Restore)

Aggregations

Database (com.google.cloud.spanner.Database)2 Restore (com.google.cloud.spanner.Restore)2 RestoreDatabaseMetadata (com.google.spanner.admin.database.v1.RestoreDatabaseMetadata)2 ExecutionException (java.util.concurrent.ExecutionException)2 FailedPreconditionException (com.google.api.gax.rpc.FailedPreconditionException)1 SpannerException (com.google.cloud.spanner.SpannerException)1