use of com.google.cloud.bigtable.admin.v2.models.Backup in project java-bigtable by googleapis.
the class BigtableTableAdminClientTest method testListBackups.
@Test
public void testListBackups() {
// Setup
Mockito.when(mockStub.listBackupsPagedCallable()).thenReturn(mockListBackupCallable);
com.google.bigtable.admin.v2.ListBackupsRequest testRequest = com.google.bigtable.admin.v2.ListBackupsRequest.newBuilder().setParent(NameUtil.formatClusterName(PROJECT_ID, INSTANCE_ID, CLUSTER_ID)).build();
// 3 Backups spread across 2 pages
List<com.google.bigtable.admin.v2.Backup> expectedProtos = Lists.newArrayList();
for (int i = 0; i < 3; i++) {
expectedProtos.add(com.google.bigtable.admin.v2.Backup.newBuilder().setName(NameUtil.formatBackupName(PROJECT_ID, INSTANCE_ID, CLUSTER_ID, BACKUP_ID + i)).build());
}
// 2 on the first page
ListBackupsPage page0 = Mockito.mock(ListBackupsPage.class);
Mockito.when(page0.getValues()).thenReturn(expectedProtos.subList(0, 2));
Mockito.when(page0.hasNextPage()).thenReturn(true);
// 1 on the last page
ListBackupsPage page1 = Mockito.mock(ListBackupsPage.class);
Mockito.when(page1.getValues()).thenReturn(expectedProtos.subList(2, 3));
// Link page0 to page1
Mockito.when(page0.getNextPageAsync()).thenReturn(ApiFutures.immediateFuture(page1));
// Link page to the response
ListBackupsPagedResponse response0 = Mockito.mock(ListBackupsPagedResponse.class);
Mockito.when(response0.getPage()).thenReturn(page0);
Mockito.when(mockListBackupCallable.futureCall(testRequest)).thenReturn(ApiFutures.immediateFuture(response0));
// Execute
List<String> actualResults = adminClient.listBackups(CLUSTER_ID);
// Verify
List<String> expectedResults = Lists.newArrayList();
for (com.google.bigtable.admin.v2.Backup expectedProto : expectedProtos) {
expectedResults.add(NameUtil.extractBackupIdFromBackupName(expectedProto.getName()));
}
assertThat(actualResults).containsExactlyElementsIn(expectedResults);
}
use of com.google.cloud.bigtable.admin.v2.models.Backup in project java-bigtable by googleapis.
the class BigtableBackupIT method crossInstanceRestoreTest.
@Test
public void crossInstanceRestoreTest() throws InterruptedException, IOException, ExecutionException, TimeoutException {
String backupId = prefixGenerator.newPrefix();
String restoredTableId = prefixGenerator.newPrefix();
// Create the backup
tableAdmin.createBackup(CreateBackupRequest.of(targetCluster, backupId).setSourceTableId(testTable.getId()).setExpireTime(Instant.now().plus(Duration.ofHours(6))));
Stopwatch stopwatch = Stopwatch.createStarted();
// Set up a new instance to test cross-instance restore. The backup will be restored here
String targetInstance = prefixGenerator.newPrefix();
instanceAdmin.createInstance(CreateInstanceRequest.of(targetInstance).addCluster(targetInstance, testEnvRule.env().getSecondaryZone(), 1, StorageType.SSD).setDisplayName("backups-dest-test-instance").addLabel("state", "readytodelete").setType(Type.PRODUCTION));
try (BigtableTableAdminClient destTableAdmin = testEnvRule.env().getTableAdminClientForInstance(targetInstance)) {
// Wait 2 minutes so that the RestoreTable API will trigger an optimize restored
// table operation.
Thread.sleep(Duration.ofMinutes(2).minus(Duration.ofMillis(stopwatch.elapsed(TimeUnit.MILLISECONDS))).toMillis());
try {
RestoreTableRequest req = RestoreTableRequest.of(testEnvRule.env().getInstanceId(), targetCluster, backupId).setTableId(restoredTableId);
RestoredTableResult result = destTableAdmin.restoreTable(req);
assertWithMessage("Incorrect restored table id").that(result.getTable().getId()).isEqualTo(restoredTableId);
assertWithMessage("Incorrect instance id").that(result.getTable().getInstanceId()).isEqualTo(targetInstance);
// The assertion might be missing if the test is running against a HDD cluster or an
// optimization is not necessary.
assertWithMessage("Empty OptimizeRestoredTable token").that(result.getOptimizeRestoredTableOperationToken()).isNotNull();
destTableAdmin.awaitOptimizeRestoredTable(result.getOptimizeRestoredTableOperationToken());
destTableAdmin.getTable(restoredTableId);
} finally {
tableAdmin.deleteBackup(targetCluster, backupId);
instanceAdmin.deleteInstance(targetInstance);
}
}
}
use of com.google.cloud.bigtable.admin.v2.models.Backup 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.Backup in project java-bigtable by googleapis.
the class BigtableCmekIT method backupTest.
@Test
public void backupTest() {
// Create a backup.
// Backups are pinned to the primary version of their table's CMEK key at the time they are
// taken
tableAdmin.createBackup(CreateBackupRequest.of(clusterId1, BACKUP_ID).setExpireTime(Instant.now().plus(6, ChronoUnit.HOURS)).setSourceTableId(TEST_TABLE_ID));
Backup backup = tableAdmin.getBackup(clusterId1, BACKUP_ID);
// Confirm encryption details for an existing backup
// The backup will be returned with the CMEK key version that the backup is pinned to.
// The status of that key version will always be UNKNOWN.
assertThat(backup.getEncryptionInfo().getKmsKeyVersion()).startsWith(kmsKeyName);
assertThat(backup.getEncryptionInfo().getStatus().getCode()).isEqualTo(Status.Code.UNKNOWN);
assertThat(backup.getEncryptionInfo().getType()).isEqualTo(EncryptionInfo.Type.CUSTOMER_MANAGED_ENCRYPTION);
assertThat(backup.getEncryptionInfo().getStatus().getMessage()).isEqualTo("Status of the associated key version is not tracked.");
}
use of com.google.cloud.bigtable.admin.v2.models.Backup in project java-docs-samples by GoogleCloudPlatform.
the class CreateBackupTestIT method testCreateBackup.
@Test
public void testCreateBackup() throws Throwable {
String functionUrl = BASE_URL + "/createBackup";
String msg = String.format("{\"projectId\":\"%s\", \"instanceId\":\"%s\", \"tableId\":\"%s\", \"clusterId\":\"%s\"," + "\"expireHours\":%d}", projectId, INSTANCE_ID, TABLE_ID, CLUSTER_ID, 8);
String msgBase64 = Base64.getEncoder().encodeToString(msg.getBytes(StandardCharsets.UTF_8));
Map<String, String> msgMap = new HashMap<>();
msgMap.put("data", msgBase64);
Map<String, Map<String, String>> dataMap = new HashMap<>();
dataMap.put("data", msgMap);
String jsonStr = gson.toJson(dataMap);
HttpPost postRequest = new HttpPost(URI.create(functionUrl));
postRequest.setEntity(new StringEntity(jsonStr));
// The Functions Framework Maven plugin process takes time to start up
// Use resilience4j to retry the test HTTP request until the plugin responds
RetryRegistry registry = RetryRegistry.of(RetryConfig.custom().maxAttempts(8).retryExceptions(HttpHostConnectException.class).intervalFunction(IntervalFunction.ofExponentialBackoff(200, 2)).build());
Retry retry = registry.retry("my");
// Perform the request-retry process
CheckedRunnable retriableFunc = Retry.decorateCheckedRunnable(retry, () -> client.execute(postRequest));
retriableFunc.run();
// Wait 2 mins for the backup to be created.
TimeUnit.MINUTES.sleep(2);
// Check if backup exists
List<String> backups = new ArrayList<String>();
try (BigtableTableAdminClient tableAdmin = BigtableTableAdminClient.create(projectId, INSTANCE_ID)) {
backups = tableAdmin.listBackups(CLUSTER_ID);
} catch (IOException e) {
System.out.println("Unable to list backups: \n" + e.toString());
throw (e);
}
assertThat(backups.size()).isEqualTo(1);
String expectedBackupPrefix = TABLE_ID + "-backup-";
assertThat(backups.get(0).contains(expectedBackupPrefix));
}
Aggregations