use of com.google.cloud.bigtable.admin.v2.models.Backup 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.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));
}
use of com.google.cloud.bigtable.admin.v2.models.Backup 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.Backup in project java-bigtable by googleapis.
the class BigtableTableAdminClient method listBackupsAsync.
/**
* Lists backups in the specified cluster asynchronously.
*
* <p>Sample code:
*
* <pre>{@code
* ApiFuture<List<String>> listFuture = client.listBackupsAsync(clusterId);
*
* ApiFutures.addCallback(
* listFuture,
* new ApiFutureCallback<List<String>>() {
* public void onSuccess(List<String> backupIds) {
* System.out.println("Got list of backups:");
* for (String backupId : backupIds) {
* System.out.println(backupId);
* }
* }
*
* public void onFailure(Throwable t) {
* t.printStackTrace();
* }
* },
* MoreExecutors.directExecutor()
* );
* }</pre>
*/
public ApiFuture<List<String>> listBackupsAsync(String clusterId) {
ListBackupsRequest request = ListBackupsRequest.newBuilder().setParent(NameUtil.formatClusterName(projectId, instanceId, clusterId)).build();
// TODO(igorbernstein2): try to upstream pagination spooling or figure out a way to expose the
// paginated responses while maintaining the wrapper facade.
// Fetches the first page.
ApiFuture<ListBackupsPage> firstPageFuture = ApiFutures.transform(stub.listBackupsPagedCallable().futureCall(request), new ApiFunction<ListBackupsPagedResponse, ListBackupsPage>() {
@Override
public ListBackupsPage apply(ListBackupsPagedResponse response) {
return response.getPage();
}
}, MoreExecutors.directExecutor());
// Fetches the rest of the pages by chaining the futures.
ApiFuture<List<com.google.bigtable.admin.v2.Backup>> allProtos = ApiFutures.transformAsync(firstPageFuture, new ApiAsyncFunction<ListBackupsPage, List<com.google.bigtable.admin.v2.Backup>>() {
List<com.google.bigtable.admin.v2.Backup> responseAccumulator = Lists.newArrayList();
@Override
public ApiFuture<List<com.google.bigtable.admin.v2.Backup>> apply(ListBackupsPage page) {
// Add all entries from the page
responseAccumulator.addAll(Lists.newArrayList(page.getValues()));
// If this is the last page, just return the accumulated responses.
if (!page.hasNextPage()) {
return ApiFutures.immediateFuture(responseAccumulator);
}
// Otherwise fetch the next page.
return ApiFutures.transformAsync(page.getNextPageAsync(), this, MoreExecutors.directExecutor());
}
}, MoreExecutors.directExecutor());
// Wraps all of the accumulated protos.
return ApiFutures.transform(allProtos, new ApiFunction<List<com.google.bigtable.admin.v2.Backup>, List<String>>() {
@Override
public List<String> apply(List<com.google.bigtable.admin.v2.Backup> protos) {
List<String> results = Lists.newArrayListWithCapacity(protos.size());
for (com.google.bigtable.admin.v2.Backup proto : protos) {
results.add(NameUtil.extractBackupIdFromBackupName(proto.getName()));
}
return results;
}
}, MoreExecutors.directExecutor());
}
use of com.google.cloud.bigtable.admin.v2.models.Backup in project java-bigtable by googleapis.
the class BigtableTableAdminClientTests 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);
}
Aggregations