use of com.google.spanner.admin.database.v1.Backup 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());
}
use of com.google.spanner.admin.database.v1.Backup in project java-spanner by googleapis.
the class DatabaseAdminClient method deleteBackup.
// AUTO-GENERATED DOCUMENTATION AND METHOD.
/**
* Deletes a pending or completed [Backup][google.spanner.admin.database.v1.Backup].
*
* <p>Sample code:
*
* <pre>{@code
* try (DatabaseAdminClient databaseAdminClient = DatabaseAdminClient.create()) {
* BackupName name = BackupName.of("[PROJECT]", "[INSTANCE]", "[BACKUP]");
* databaseAdminClient.deleteBackup(name);
* }
* }</pre>
*
* @param name Required. Name of the backup to delete. Values are of the form
* `projects/<project>/instances/<instance>/backups/<backup>`.
* @throws com.google.api.gax.rpc.ApiException if the remote call fails
*/
public final void deleteBackup(BackupName name) {
DeleteBackupRequest request = DeleteBackupRequest.newBuilder().setName(name == null ? null : name.toString()).build();
deleteBackup(request);
}
use of com.google.spanner.admin.database.v1.Backup 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();
}
}
use of com.google.spanner.admin.database.v1.Backup 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%"));
}
}
use of com.google.spanner.admin.database.v1.Backup 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);
}
Aggregations