use of com.google.cloud.bigtable.admin.v2.models.CreateBackupRequest in project java-bigtable by googleapis.
the class BigtableBackupIT method createAndGetBackupTest.
@Test
public void createAndGetBackupTest() {
String backupId = prefixGenerator.newPrefix();
Instant expireTime = Instant.now().plus(Duration.ofHours(6));
CreateBackupRequest request = CreateBackupRequest.of(targetCluster, backupId).setSourceTableId(testTable.getId()).setExpireTime(expireTime);
try {
Backup response = tableAdmin.createBackup(request);
assertWithMessage("Got wrong backup Id in CreateBackup").that(response.getId()).isEqualTo(backupId);
assertWithMessage("Got wrong source table name in CreateBackup").that(response.getSourceTableId()).isEqualTo(testTable.getId());
assertWithMessage("Got wrong expire time in CreateBackup").that(response.getExpireTime()).isEqualTo(expireTime);
Backup result = tableAdmin.getBackup(targetCluster, backupId);
assertWithMessage("Got wrong backup Id in GetBackup API").that(result.getId()).isEqualTo(backupId);
assertWithMessage("Got wrong source table name in GetBackup API").that(result.getSourceTableId()).isEqualTo(testTable.getId());
assertWithMessage("Got wrong expire time in GetBackup API").that(result.getExpireTime()).isEqualTo(expireTime);
assertWithMessage("Got empty start time in GetBackup API").that(result.getStartTime()).isNotNull();
assertWithMessage("Got wrong size bytes in GetBackup API").that(result.getSizeBytes()).isEqualTo(0L);
assertWithMessage("Got wrong state in GetBackup API").that(result.getState()).isAnyOf(Backup.State.CREATING, Backup.State.READY);
} finally {
tableAdmin.deleteBackup(targetCluster, backupId);
}
}
use of com.google.cloud.bigtable.admin.v2.models.CreateBackupRequest in project java-docs-samples by GoogleCloudPlatform.
the class CreateBackup method accept.
@Override
public void accept(PubSubMessage message, Context context) {
if (message != null && message.getData() != null) {
logger.info("Trigger event:" + message.getData());
try {
String payload = new String(Base64.getDecoder().decode(message.getData().getBytes(StandardCharsets.UTF_8)), StandardCharsets.UTF_8);
logger.info("Decoded payload:" + payload);
CreateBackupMessage cbMessage = mapper.readValue(payload, CreateBackupMessage.class);
logger.info("CreateBackup message:" + cbMessage.toString());
logger.info("Submitting the create backup request");
// Create an admin client
BigtableTableAdminSettings adminSettings = BigtableTableAdminSettings.newBuilder().setProjectId(cbMessage.getProjectId()).setInstanceId(cbMessage.getInstanceId()).build();
try (BigtableTableAdminClient adminClient = BigtableTableAdminClient.create(adminSettings)) {
CreateBackupRequest request = CreateBackupRequest.of(cbMessage.getClusterId(), buildBackupId(cbMessage.getTableId())).setSourceTableId(cbMessage.getTableId()).setExpireTime(buildExpireTime(cbMessage.getExpireHours()));
Backup backupDetails = adminClient.createBackup(request);
logger.info("Submitted backup request :" + backupDetails.getId() + ": that will expire at:" + backupDetails.getExpireTime());
} catch (IOException e) {
logger.log(Level.SEVERE, "Caught Exception creating backup:" + e.toString(), e);
}
} catch (Exception e) {
logger.log(Level.SEVERE, "Caught Exception running the create backup function:" + e.toString(), e);
}
return;
}
}
use of com.google.cloud.bigtable.admin.v2.models.CreateBackupRequest in project java-bigtable-hbase by googleapis.
the class AbstractBigtableAdmin method snapshotTable.
protected Backup snapshotTable(String snapshotId, TableName tableName) throws IOException {
Preconditions.checkArgument(!Strings.isNullOrEmpty(snapshotId));
Preconditions.checkArgument(!Strings.isNullOrEmpty(tableName.getNameAsString()));
CreateBackupRequest request = CreateBackupRequest.of(getBackupClusterId(), snapshotId).setSourceTableId(tableName.getNameAsString());
int ttlSecondsForBackup = settings.getTtlSecondsForBackup();
if (ttlSecondsForBackup <= 0) {
throw new IllegalArgumentException(BigtableOptionsFactory.BIGTABLE_SNAPSHOT_DEFAULT_TTL_SECS_KEY + " must be > 0");
}
Instant expireTime = Instant.now().plus(ttlSecondsForBackup, ChronoUnit.SECONDS);
request.setExpireTime(expireTime);
return FutureUtil.unwrap(adminClientWrapper.createBackupAsync(request));
}
use of com.google.cloud.bigtable.admin.v2.models.CreateBackupRequest in project java-bigtable by googleapis.
the class BigtableTableAdminClientTest method testCreateBackup.
@Test
public void testCreateBackup() {
// Setup
Mockito.when(mockStub.createBackupOperationCallable()).thenReturn(mockCreateBackupOperationCallable);
String backupName = NameUtil.formatBackupName(PROJECT_ID, INSTANCE_ID, CLUSTER_ID, BACKUP_ID);
Timestamp startTime = Timestamp.newBuilder().setSeconds(123).build();
Timestamp endTime = Timestamp.newBuilder().setSeconds(456).build();
Timestamp expireTime = Timestamp.newBuilder().setSeconds(789).build();
long sizeBytes = 123456789;
CreateBackupRequest req = CreateBackupRequest.of(CLUSTER_ID, BACKUP_ID).setSourceTableId(TABLE_ID);
mockOperationResult(mockCreateBackupOperationCallable, req.toProto(PROJECT_ID, INSTANCE_ID), com.google.bigtable.admin.v2.Backup.newBuilder().setName(backupName).setSourceTable(TABLE_NAME).setStartTime(startTime).setEndTime(endTime).setExpireTime(expireTime).setSizeBytes(sizeBytes).build(), CreateBackupMetadata.newBuilder().setName(backupName).setStartTime(startTime).setEndTime(endTime).setSourceTable(TABLE_NAME).build());
// Execute
Backup actualResult = adminClient.createBackup(req);
// Verify
assertThat(actualResult.getId()).isEqualTo(BACKUP_ID);
assertThat(actualResult.getSourceTableId()).isEqualTo(TABLE_ID);
assertThat(actualResult.getStartTime()).isEqualTo(Instant.ofEpochMilli(Timestamps.toMillis(startTime)));
assertThat(actualResult.getEndTime()).isEqualTo(Instant.ofEpochMilli(Timestamps.toMillis(endTime)));
assertThat(actualResult.getExpireTime()).isEqualTo(Instant.ofEpochMilli(Timestamps.toMillis(expireTime)));
assertThat(actualResult.getSizeBytes()).isEqualTo(sizeBytes);
}
use of com.google.cloud.bigtable.admin.v2.models.CreateBackupRequest in project java-bigtable by googleapis.
the class BigtableBackupIT method restoreTableTest.
@Test
public void restoreTableTest() throws InterruptedException, ExecutionException {
String backupId = prefixGenerator.newPrefix();
String restoredTableId = prefixGenerator.newPrefix();
tableAdmin.createBackup(createBackupRequest(backupId));
// Wait 2 minutes so that the RestoreTable API will trigger an optimize restored
// table operation.
Thread.sleep(120 * 1000);
try {
RestoreTableRequest req = RestoreTableRequest.of(targetCluster, backupId).setTableId(restoredTableId);
RestoredTableResult result = tableAdmin.restoreTable(req);
assertWithMessage("Incorrect restored table id").that(result.getTable().getId()).isEqualTo(restoredTableId);
if (result.getOptimizeRestoredTableOperationToken() != null) {
// The assertion might be missing if the test is running against a HDD cluster or an
// optimization is not necessary.
tableAdmin.awaitOptimizeRestoredTable(result.getOptimizeRestoredTableOperationToken());
Table restoredTable = tableAdmin.getTable(restoredTableId);
assertWithMessage("Incorrect restored table id").that(restoredTable.getId()).isEqualTo(restoredTableId);
}
} finally {
tableAdmin.deleteBackup(targetCluster, backupId);
tableAdmin.deleteTable(restoredTableId);
}
}
Aggregations