use of com.google.spanner.admin.database.v1.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())));
}
use of com.google.spanner.admin.database.v1.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());
}
use of com.google.spanner.admin.database.v1.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();
}
}
use of com.google.spanner.admin.database.v1.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%"));
}
}
use of com.google.spanner.admin.database.v1.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);
}
Aggregations