Search in sources :

Example 1 with RestoreTableRequest

use of com.google.cloud.bigtable.admin.v2.models.RestoreTableRequest in project java-bigtable by googleapis.

the class BigtableTableAdminClientTest method testRestoreTable.

@Test
public void testRestoreTable() throws ExecutionException, InterruptedException {
    // Setup
    Mockito.when(mockStub.restoreTableOperationCallable()).thenReturn(mockRestoreTableOperationCallable);
    Timestamp startTime = Timestamp.newBuilder().setSeconds(1234).build();
    Timestamp endTime = Timestamp.newBuilder().setSeconds(5678).build();
    String operationName = "my-operation";
    RestoreTableRequest req = RestoreTableRequest.of(CLUSTER_ID, BACKUP_ID).setTableId(TABLE_ID);
    mockOperationResult(mockRestoreTableOperationCallable, req.toProto(PROJECT_ID, INSTANCE_ID), com.google.bigtable.admin.v2.Table.newBuilder().setName(TABLE_NAME).build(), RestoreTableMetadata.newBuilder().setName(NameUtil.formatTableName(PROJECT_ID, INSTANCE_ID, TABLE_ID)).setOptimizeTableOperationName(operationName).setSourceType(RestoreSourceType.BACKUP).setBackupInfo(BackupInfo.newBuilder().setBackup(BACKUP_ID).setSourceTable(NameUtil.formatTableName(PROJECT_ID, INSTANCE_ID, TABLE_ID)).setStartTime(startTime).setEndTime(endTime).build()).build());
    // Execute
    RestoredTableResult actualResult = adminClient.restoreTable(req);
    // Verify
    assertThat(actualResult.getTable().getId()).isEqualTo(TABLE_ID);
}
Also used : RestoreTableRequest(com.google.cloud.bigtable.admin.v2.models.RestoreTableRequest) RestoredTableResult(com.google.cloud.bigtable.admin.v2.models.RestoredTableResult) ByteString(com.google.protobuf.ByteString) Timestamp(com.google.protobuf.Timestamp) Test(org.junit.Test)

Example 2 with RestoreTableRequest

use of com.google.cloud.bigtable.admin.v2.models.RestoreTableRequest 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);
        }
    }
}
Also used : RestoreTableRequest(com.google.cloud.bigtable.admin.v2.models.RestoreTableRequest) Stopwatch(com.google.common.base.Stopwatch) RestoredTableResult(com.google.cloud.bigtable.admin.v2.models.RestoredTableResult) ByteString(com.google.protobuf.ByteString) BigtableTableAdminClient(com.google.cloud.bigtable.admin.v2.BigtableTableAdminClient) Test(org.junit.Test)

Example 3 with RestoreTableRequest

use of com.google.cloud.bigtable.admin.v2.models.RestoreTableRequest in project java-bigtable-hbase by googleapis.

the class AbstractBigtableAdmin method cloneSnapshot.

/**
 * @param snapshotId a {@link String} object.
 * @param tableName a {@link TableName} object.
 * @throws IOException if any.
 */
@Override
public void cloneSnapshot(String snapshotId, TableName tableName) throws IOException, TableExistsException, RestoreSnapshotException {
    RestoreTableRequest request = RestoreTableRequest.of(getBackupClusterId(), snapshotId).setTableId(tableName.getNameAsString());
    FutureUtil.unwrap(adminClientWrapper.restoreTableAsync(request));
}
Also used : RestoreTableRequest(com.google.cloud.bigtable.admin.v2.models.RestoreTableRequest)

Example 4 with RestoreTableRequest

use of com.google.cloud.bigtable.admin.v2.models.RestoreTableRequest 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);
    }
}
Also used : RestoreTableRequest(com.google.cloud.bigtable.admin.v2.models.RestoreTableRequest) Table(com.google.cloud.bigtable.admin.v2.models.Table) RestoredTableResult(com.google.cloud.bigtable.admin.v2.models.RestoredTableResult) ByteString(com.google.protobuf.ByteString) Test(org.junit.Test)

Aggregations

RestoreTableRequest (com.google.cloud.bigtable.admin.v2.models.RestoreTableRequest)4 RestoredTableResult (com.google.cloud.bigtable.admin.v2.models.RestoredTableResult)3 ByteString (com.google.protobuf.ByteString)3 Test (org.junit.Test)3 BigtableTableAdminClient (com.google.cloud.bigtable.admin.v2.BigtableTableAdminClient)1 Table (com.google.cloud.bigtable.admin.v2.models.Table)1 Stopwatch (com.google.common.base.Stopwatch)1 Timestamp (com.google.protobuf.Timestamp)1