Search in sources :

Example 16 with CreateBackupMetadata

use of com.google.bigtable.admin.v2.CreateBackupMetadata in project java-spanner by googleapis.

the class ITBackupTest method testCancelBackupOperation.

private void testCancelBackupOperation(Database database) throws InterruptedException, ExecutionException {
    Timestamp expireTime = afterDays(7);
    String backupId = testHelper.getUniqueBackupId();
    logger.info(String.format("Starting to create backup %s", backupId));
    OperationFuture<Backup, CreateBackupMetadata> op = dbAdminClient.createBackup(instanceId, backupId, database.getId().getDatabase(), expireTime);
    backups.add(backupId);
    // Cancel the backup operation.
    logger.info(String.format("Cancelling the creation of backup %s", backupId));
    dbAdminClient.cancelOperation(op.getName());
    logger.info("Fetching backup operations");
    boolean operationFound = false;
    for (Operation operation : dbAdminClient.listBackupOperations(instanceId, Options.filter(String.format("name:%s", op.getName()))).iterateAll()) {
        assertEquals(Code.CANCELLED.value(), operation.getError().getCode());
        operationFound = true;
    }
    assertTrue(operationFound);
    logger.info("Finished cancel test");
}
Also used : Backup(com.google.cloud.spanner.Backup) CreateBackupMetadata(com.google.spanner.admin.database.v1.CreateBackupMetadata) Operation(com.google.longrunning.Operation) Timestamp(com.google.cloud.Timestamp)

Example 17 with CreateBackupMetadata

use of com.google.bigtable.admin.v2.CreateBackupMetadata in project java-spanner by googleapis.

the class CreateBackupWithEncryptionKey method createBackupWithEncryptionKey.

static Void createBackupWithEncryptionKey(DatabaseAdminClient adminClient, String projectId, String instanceId, String databaseId, String backupId, String kmsKeyName) throws InterruptedException {
    // Set expire time to 14 days from now.
    final Timestamp expireTime = Timestamp.ofTimeMicroseconds(TimeUnit.MICROSECONDS.convert(System.currentTimeMillis() + TimeUnit.DAYS.toMillis(14), TimeUnit.MILLISECONDS));
    final Backup backupToCreate = adminClient.newBackupBuilder(BackupId.of(projectId, instanceId, backupId)).setDatabase(DatabaseId.of(projectId, instanceId, databaseId)).setExpireTime(expireTime).setEncryptionConfig(EncryptionConfigs.customerManagedEncryption(kmsKeyName)).build();
    final OperationFuture<Backup, CreateBackupMetadata> operation = adminClient.createBackup(backupToCreate);
    Backup backup;
    try {
        System.out.println("Waiting for operation to complete...");
        backup = operation.get(1200, TimeUnit.SECONDS);
    } 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);
    } catch (TimeoutException e) {
        // If the operation timed out propagates the timeout
        throw SpannerExceptionFactory.propagateTimeout(e);
    }
    System.out.printf("Backup %s of size %d bytes was created at %s using encryption key %s%n", backup.getId().getName(), backup.getSize(), LocalDateTime.ofEpochSecond(backup.getProto().getCreateTime().getSeconds(), backup.getProto().getCreateTime().getNanos(), OffsetDateTime.now().getOffset()), kmsKeyName);
    return null;
}
Also used : Backup(com.google.cloud.spanner.Backup) CreateBackupMetadata(com.google.spanner.admin.database.v1.CreateBackupMetadata) ExecutionException(java.util.concurrent.ExecutionException) Timestamp(com.google.cloud.Timestamp) TimeoutException(java.util.concurrent.TimeoutException)

Example 18 with CreateBackupMetadata

use of com.google.bigtable.admin.v2.CreateBackupMetadata in project java-spanner by googleapis.

the class SpannerSample method listBackupOperations.

// [END spanner_cancel_backup_create]
// [START spanner_list_backup_operations]
static void listBackupOperations(InstanceAdminClient instanceAdminClient, DatabaseId databaseId) {
    Instance instance = instanceAdminClient.getInstance(databaseId.getInstanceId().getInstance());
    // Get create backup operations for the sample database.
    Timestamp last24Hours = Timestamp.ofTimeSecondsAndNanos(TimeUnit.SECONDS.convert(TimeUnit.HOURS.convert(Timestamp.now().getSeconds(), TimeUnit.SECONDS) - 24, TimeUnit.HOURS), 0);
    String filter = String.format("(metadata.database:%s) AND " + "(metadata.@type:type.googleapis.com/" + "google.spanner.admin.database.v1.CreateBackupMetadata) AND " + "(metadata.progress.start_time > \"%s\")", databaseId.getName(), last24Hours);
    Page<Operation> operations = instance.listBackupOperations(Options.filter(filter));
    for (Operation op : operations.iterateAll()) {
        try {
            CreateBackupMetadata metadata = op.getMetadata().unpack(CreateBackupMetadata.class);
            System.out.println(String.format("Backup %s on database %s pending: %d%% complete", metadata.getName(), metadata.getDatabase(), metadata.getProgress().getProgressPercent()));
        } catch (InvalidProtocolBufferException e) {
            // The returned operation does not contain CreateBackupMetadata.
            System.err.println(e.getMessage());
        }
    }
}
Also used : Instance(com.google.cloud.spanner.Instance) CreateBackupMetadata(com.google.spanner.admin.database.v1.CreateBackupMetadata) InvalidProtocolBufferException(com.google.protobuf.InvalidProtocolBufferException) Operation(com.google.longrunning.Operation) Timestamp(com.google.cloud.Timestamp)

Aggregations

CreateBackupMetadata (com.google.spanner.admin.database.v1.CreateBackupMetadata)18 Timestamp (com.google.cloud.Timestamp)12 Backup (com.google.cloud.spanner.Backup)7 ExecutionException (java.util.concurrent.ExecutionException)7 Test (org.junit.Test)7 Operation (com.google.longrunning.Operation)5 Backup (com.google.spanner.admin.database.v1.Backup)5 CreateDatabaseMetadata (com.google.spanner.admin.database.v1.CreateDatabaseMetadata)3 OperationSnapshot (com.google.api.gax.longrunning.OperationSnapshot)2 Database (com.google.cloud.spanner.Database)2 SlowTest (com.google.cloud.spanner.SlowTest)2 SpannerException (com.google.cloud.spanner.SpannerException)2 InvalidProtocolBufferException (com.google.protobuf.InvalidProtocolBufferException)2 CreateBackupRequest (com.google.spanner.admin.database.v1.CreateBackupRequest)2 RestoreDatabaseMetadata (com.google.spanner.admin.database.v1.RestoreDatabaseMetadata)2 CancelledException (com.google.api.gax.rpc.CancelledException)1 DatabaseAdminClient (com.google.cloud.spanner.DatabaseAdminClient)1 DatabaseClient (com.google.cloud.spanner.DatabaseClient)1 Instance (com.google.cloud.spanner.Instance)1 Spanner (com.google.cloud.spanner.Spanner)1