Search in sources :

Example 6 with CreateBackupMetadata

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

the class SpannerSample method createBackup.

// [END spanner_query_with_query_options]
// [START spanner_create_backup]
static void createBackup(DatabaseAdminClient dbAdminClient, DatabaseId databaseId, BackupId backupId, Timestamp versionTime) {
    // Set expire time to 14 days from now.
    Timestamp expireTime = Timestamp.ofTimeMicroseconds(TimeUnit.MICROSECONDS.convert(System.currentTimeMillis() + TimeUnit.DAYS.toMillis(14), TimeUnit.MILLISECONDS));
    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() + "]...");
    OperationFuture<Backup, CreateBackupMetadata> op = backup.create();
    try {
        // Wait for the backup operation to complete.
        backup = op.get();
        System.out.println("Created backup [" + backup.getId() + "]");
    } catch (ExecutionException e) {
        throw (SpannerException) e.getCause();
    } catch (InterruptedException e) {
        throw SpannerExceptionFactory.propagateInterrupt(e);
    }
    // Reload the metadata of the backup from the server.
    backup = backup.reload();
    System.out.println(String.format("Backup %s of size %d bytes was created at %s for version of database at %s", backup.getId().getName(), backup.getSize(), LocalDateTime.ofEpochSecond(backup.getProto().getCreateTime().getSeconds(), backup.getProto().getCreateTime().getNanos(), OffsetDateTime.now().getOffset()), LocalDateTime.ofEpochSecond(backup.getProto().getVersionTime().getSeconds(), backup.getProto().getVersionTime().getNanos(), OffsetDateTime.now().getOffset())));
}
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)

Example 7 with CreateBackupMetadata

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

the class GapicSpannerRpc method createBackup.

@Override
public OperationFuture<Backup, CreateBackupMetadata> createBackup(final com.google.cloud.spanner.Backup backupInfo) throws SpannerException {
    final String instanceName = backupInfo.getInstanceId().getName();
    final String databaseName = backupInfo.getDatabase().getName();
    final String backupId = backupInfo.getId().getBackup();
    final Backup.Builder backupBuilder = com.google.spanner.admin.database.v1.Backup.newBuilder().setDatabase(databaseName).setExpireTime(backupInfo.getExpireTime().toProto());
    if (backupInfo.getVersionTime() != null) {
        backupBuilder.setVersionTime(backupInfo.getVersionTime().toProto());
    }
    final Backup backup = backupBuilder.build();
    final CreateBackupRequest.Builder requestBuilder = CreateBackupRequest.newBuilder().setParent(instanceName).setBackupId(backupId).setBackup(backup);
    if (backupInfo.getEncryptionConfig() != null) {
        requestBuilder.setEncryptionConfig(EncryptionConfigProtoMapper.createBackupEncryptionConfig(backupInfo.getEncryptionConfig()));
    }
    final CreateBackupRequest request = requestBuilder.build();
    final OperationFutureCallable<CreateBackupRequest, Backup, CreateBackupMetadata> callable = new OperationFutureCallable<>(databaseAdminStub.createBackupOperationCallable(), request, DatabaseAdminGrpc.getCreateBackupMethod(), instanceName, nextPageToken -> listBackupOperations(instanceName, 0, String.format("(metadata.@type:type.googleapis.com/%s) AND (metadata.name:%s)", CreateBackupMetadata.getDescriptor().getFullName(), String.format("%s/backups/%s", instanceName, backupId)), nextPageToken), input -> {
        try {
            return input.getMetadata().unpack(CreateBackupMetadata.class).getProgress().getStartTime();
        } catch (InvalidProtocolBufferException e) {
            return null;
        }
    });
    return RetryHelper.runWithRetries(callable, databaseAdminStubSettings.createBackupOperationSettings().getInitialCallSettings().getRetrySettings(), new OperationFutureRetryAlgorithm<>(), NanoClock.getDefaultClock());
}
Also used : CreateBackupRequest(com.google.spanner.admin.database.v1.CreateBackupRequest) Backup(com.google.spanner.admin.database.v1.Backup) CreateBackupMetadata(com.google.spanner.admin.database.v1.CreateBackupMetadata) InvalidProtocolBufferException(com.google.protobuf.InvalidProtocolBufferException)

Example 8 with CreateBackupMetadata

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

the class DatabaseAdminClientTest method backupCreateCancel.

@Test
public void backupCreateCancel() {
    final String backupId = "other-backup-id";
    // Set expire time to 14 days from now.
    long currentTimeInMicroSeconds = TimeUnit.MICROSECONDS.convert(System.currentTimeMillis(), TimeUnit.MILLISECONDS);
    long deltaTimeInMicroseconds = TimeUnit.MICROSECONDS.convert(14L, TimeUnit.DAYS);
    Timestamp expireTime = Timestamp.ofTimeMicroseconds(currentTimeInMicroSeconds + deltaTimeInMicroseconds);
    Backup backup = client.newBackupBuilder(BackupId.of(PROJECT_ID, INSTANCE_ID, backupId)).setDatabase(DatabaseId.of(PROJECT_ID, INSTANCE_ID, DB_ID)).setExpireTime(expireTime).build();
    // Start a creation of a backup.
    OperationFuture<Backup, CreateBackupMetadata> op = backup.create();
    try {
        // Try to cancel the backup operation.
        client.cancelOperation(op.getName());
        // Get a polling future for the running operation. This future will regularly poll the server
        // for the current status of the backup operation.
        RetryingFuture<OperationSnapshot> pollingFuture = op.getPollingFuture();
        // or any other error occurred.
        while (!pollingFuture.get().isDone()) {
            Thread.sleep(TimeUnit.MILLISECONDS.convert(5, TimeUnit.SECONDS));
        }
    } catch (CancellationException e) {
    // ignore, this exception may also occur if the polling future has been cancelled.
    } catch (ExecutionException e) {
        throw (RuntimeException) e.getCause();
    } catch (InterruptedException e) {
        throw SpannerExceptionFactory.propagateInterrupt(e);
    } finally {
        backup.delete();
    }
}
Also used : CancellationException(java.util.concurrent.CancellationException) CreateBackupMetadata(com.google.spanner.admin.database.v1.CreateBackupMetadata) OperationSnapshot(com.google.api.gax.longrunning.OperationSnapshot) ExecutionException(java.util.concurrent.ExecutionException) Timestamp(com.google.cloud.Timestamp) Test(org.junit.Test)

Example 9 with CreateBackupMetadata

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

the class DatabaseAdminClientTest method instanceListBackupOperationsWithProgress.

@Test
public void instanceListBackupOperationsWithProgress() throws InvalidProtocolBufferException {
    Instance instance = spanner.getInstanceAdminClient().newInstanceBuilder(InstanceId.of(PROJECT_ID, INSTANCE_ID)).build();
    String database = String.format("%s/databases/%s", TEST_PARENT, DB_ID);
    String filter = String.format("(metadata.database:%s) AND " + "(metadata.@type:type.googleapis.com/" + "google.spanner.admin.database.v1.CreateBackupMetadata)", database);
    Page<Operation> operations = instance.listBackupOperations(Options.filter(filter));
    for (Operation op : operations.iterateAll()) {
        CreateBackupMetadata metadata = op.getMetadata().unpack(CreateBackupMetadata.class);
        String progress = String.format("Backup %s on database %s pending: %d%% complete", metadata.getName(), metadata.getDatabase(), metadata.getProgress().getProgressPercent());
        assertThat(progress.contains("100%"));
    }
}
Also used : CreateBackupMetadata(com.google.spanner.admin.database.v1.CreateBackupMetadata) Operation(com.google.longrunning.Operation) Test(org.junit.Test)

Example 10 with CreateBackupMetadata

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

the class DatabaseAdminClientImplTest method createEncryptedBackup.

@Test
public void createEncryptedBackup() throws ExecutionException, InterruptedException {
    final OperationFuture<Backup, CreateBackupMetadata> rawOperationFuture = OperationFutureUtil.immediateOperationFuture("createBackup", getEncryptedBackupProto(), CreateBackupMetadata.getDefaultInstance());
    final Timestamp t = Timestamp.ofTimeMicroseconds(TimeUnit.MILLISECONDS.toMicros(System.currentTimeMillis()) + TimeUnit.HOURS.toMicros(28));
    final com.google.cloud.spanner.Backup backup = client.newBackupBuilder(BackupId.of(PROJECT_ID, INSTANCE_ID, BK_ID)).setDatabase(DatabaseId.of(PROJECT_ID, INSTANCE_ID, DB_ID)).setExpireTime(t).setEncryptionConfig(EncryptionConfigs.customerManagedEncryption(KMS_KEY_NAME)).build();
    when(rpc.createBackup(backup)).thenReturn(rawOperationFuture);
    final OperationFuture<com.google.cloud.spanner.Backup, CreateBackupMetadata> op = client.createBackup(backup);
    assertThat(op.isDone()).isTrue();
    assertThat(op.get().getId().getName()).isEqualTo(BK_NAME);
    assertThat(op.get().getEncryptionInfo().getKmsKeyVersion()).isEqualTo(KMS_KEY_VERSION);
}
Also used : Backup(com.google.spanner.admin.database.v1.Backup) CreateBackupMetadata(com.google.spanner.admin.database.v1.CreateBackupMetadata) Timestamp(com.google.cloud.Timestamp) Test(org.junit.Test)

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