Search in sources :

Example 1 with CreateBackupMetadata

use of com.google.bigtable.admin.v2.CreateBackupMetadata in project java-spanner by googleapis.

the class DatabaseAdminClientImplTest method createBackupWithParams.

@Test
public void createBackupWithParams() throws Exception {
    OperationFuture<Backup, CreateBackupMetadata> rawOperationFuture = OperationFutureUtil.immediateOperationFuture("createBackup", getBackupProto(), CreateBackupMetadata.getDefaultInstance());
    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).build();
    when(rpc.createBackup(backup)).thenReturn(rawOperationFuture);
    OperationFuture<com.google.cloud.spanner.Backup, CreateBackupMetadata> op = client.createBackup(INSTANCE_ID, BK_ID, DB_ID, t);
    assertThat(op.isDone()).isTrue();
    assertThat(op.get().getId().getName()).isEqualTo(BK_NAME);
}
Also used : Backup(com.google.spanner.admin.database.v1.Backup) CreateBackupMetadata(com.google.spanner.admin.database.v1.CreateBackupMetadata) Timestamp(com.google.cloud.Timestamp) Test(org.junit.Test)

Example 2 with CreateBackupMetadata

use of com.google.bigtable.admin.v2.CreateBackupMetadata in project java-spanner by googleapis.

the class DatabaseAdminClientImplTest method createBackupWithBackupObject.

@Test
public void createBackupWithBackupObject() throws ExecutionException, InterruptedException {
    final OperationFuture<Backup, CreateBackupMetadata> rawOperationFuture = OperationFutureUtil.immediateOperationFuture("createBackup", getBackupProto(), CreateBackupMetadata.getDefaultInstance());
    final Timestamp expireTime = Timestamp.ofTimeMicroseconds(TimeUnit.MILLISECONDS.toMicros(System.currentTimeMillis()) + TimeUnit.HOURS.toMicros(28));
    final Timestamp versionTime = Timestamp.ofTimeMicroseconds(TimeUnit.MILLISECONDS.toMicros(System.currentTimeMillis()) - TimeUnit.DAYS.toMicros(2));
    final com.google.cloud.spanner.Backup requestBackup = client.newBackupBuilder(BackupId.of(PROJECT_ID, INSTANCE_ID, BK_ID)).setDatabase(DatabaseId.of(PROJECT_ID, INSTANCE_ID, DB_ID)).setExpireTime(expireTime).setVersionTime(versionTime).build();
    when(rpc.createBackup(requestBackup)).thenReturn(rawOperationFuture);
    final OperationFuture<com.google.cloud.spanner.Backup, CreateBackupMetadata> op = client.createBackup(requestBackup);
    assertThat(op.isDone()).isTrue();
    assertThat(op.get().getId().getName()).isEqualTo(BK_NAME);
}
Also used : Backup(com.google.spanner.admin.database.v1.Backup) CreateBackupMetadata(com.google.spanner.admin.database.v1.CreateBackupMetadata) Timestamp(com.google.cloud.Timestamp) Test(org.junit.Test)

Example 3 with CreateBackupMetadata

use of com.google.bigtable.admin.v2.CreateBackupMetadata in project java-spanner by googleapis.

the class ITBackupTest method testMetadata.

private void testMetadata(OperationFuture<Backup, CreateBackupMetadata> operation, String backupId, Database database) throws InterruptedException, ExecutionException {
    logger.info("Getting operation metadata");
    CreateBackupMetadata metadata1 = operation.getMetadata().get();
    String expectedOperationName1 = String.format(EXPECTED_OP_NAME_FORMAT, testHelper.getInstanceId().getName(), backupId);
    assertTrue(operation.getName().startsWith(expectedOperationName1));
    assertEquals(database.getId().getName(), metadata1.getDatabase());
    assertEquals(BackupId.of(testHelper.getInstanceId(), backupId).getName(), metadata1.getName());
    logger.info("Finished metadata tests");
}
Also used : CreateBackupMetadata(com.google.spanner.admin.database.v1.CreateBackupMetadata)

Example 4 with CreateBackupMetadata

use of com.google.bigtable.admin.v2.CreateBackupMetadata in project java-spanner by googleapis.

the class ITBackupTest method test02_RetryNonIdempotentRpcsReturningLongRunningOperations.

@Test
public void test02_RetryNonIdempotentRpcsReturningLongRunningOperations() throws Exception {
    assumeFalse("Querying long-running operations is not supported on the emulator", isUsingEmulator());
    // RPCs that return a long-running operation such as CreateDatabase, CreateBackup and
    // RestoreDatabase are non-idempotent and can normally not be automatically retried in case of a
    // transient failure. The client library will however automatically query the backend to check
    // whether the corresponding operation was started or not, and if it was, it will pick up the
    // existing operation. If no operation is found, a new RPC call will be executed to start the
    // operation.
    List<Database> databases = new ArrayList<>();
    String initialDatabaseId;
    Timestamp initialDbCreateTime;
    // CreateDatabase
    InjectErrorInterceptorProvider createDbInterceptor = new InjectErrorInterceptorProvider("CreateDatabase");
    SpannerOptions options = testHelper.getOptions().toBuilder().setInterceptorProvider(createDbInterceptor).build();
    try (Spanner spanner = options.getService()) {
        initialDatabaseId = testHelper.getUniqueDatabaseId();
        DatabaseAdminClient client = spanner.getDatabaseAdminClient();
        OperationFuture<Database, CreateDatabaseMetadata> op = client.createDatabase(testHelper.getInstanceId().getInstance(), initialDatabaseId, Collections.emptyList());
        databases.add(op.get(DATABASE_TIMEOUT_MINUTES, TimeUnit.MINUTES));
        // Keep track of the original create time of this database, as we will drop this database
        // later and create another one with the exact same name. That means that the ListOperations
        // call will return at least two CreateDatabase operations. The retry logic should always
        // pick the last one.
        initialDbCreateTime = op.get(DATABASE_TIMEOUT_MINUTES, TimeUnit.MINUTES).getCreateTime();
        // Assert that the CreateDatabase RPC was called only once, and that the operation tracking
        // was resumed through a GetOperation call.
        assertEquals(1, createDbInterceptor.methodCount.get());
        assertTrue(createDbInterceptor.getOperationCount.get() >= 1);
    }
    // CreateBackup
    InjectErrorInterceptorProvider createBackupInterceptor = new InjectErrorInterceptorProvider("CreateBackup");
    options = testHelper.getOptions().toBuilder().setInterceptorProvider(createBackupInterceptor).build();
    String backupId = String.format("test-bck-%08d", new Random().nextInt(100000000));
    try (Spanner spanner = options.getService()) {
        String databaseId = databases.get(0).getId().getDatabase();
        DatabaseAdminClient client = spanner.getDatabaseAdminClient();
        OperationFuture<Backup, CreateBackupMetadata> op = client.createBackup(testHelper.getInstanceId().getInstance(), backupId, databaseId, Timestamp.ofTimeSecondsAndNanos(Timestamp.now().getSeconds() + TimeUnit.SECONDS.convert(7L, TimeUnit.DAYS), 0));
        Stopwatch watch = Stopwatch.createStarted();
        while (createBackupInterceptor.methodCount.get() < 1 && createBackupInterceptor.getOperationCount.get() < 1 && watch.elapsed(TimeUnit.SECONDS) < 120) {
            // noinspection BusyWait
            Thread.sleep(5000L);
        }
        client.cancelOperation(op.getName());
        // Assert that the CreateBackup RPC was called only once, and that the operation tracking
        // was resumed through a GetOperation call.
        assertEquals(1, createBackupInterceptor.methodCount.get());
        assertTrue(createBackupInterceptor.getOperationCount.get() >= 1);
    }
    // RestoreBackup
    if (!backups.isEmpty()) {
        InjectErrorInterceptorProvider restoreBackupInterceptor = new InjectErrorInterceptorProvider("RestoreDatabase");
        options = testHelper.getOptions().toBuilder().setInterceptorProvider(restoreBackupInterceptor).build();
        try (Spanner spanner = options.getService()) {
            String restoredDbId = testHelper.getUniqueDatabaseId();
            DatabaseAdminClient client = spanner.getDatabaseAdminClient();
            OperationFuture<Database, RestoreDatabaseMetadata> op = client.restoreDatabase(testHelper.getInstanceId().getInstance(), backups.get(0), testHelper.getInstanceId().getInstance(), restoredDbId);
            Stopwatch watch = Stopwatch.createStarted();
            while (restoreBackupInterceptor.methodCount.get() < 1 && restoreBackupInterceptor.getOperationCount.get() < 1 && watch.elapsed(TimeUnit.SECONDS) < 120) {
                // noinspection BusyWait
                Thread.sleep(5000L);
            }
            try {
                client.cancelOperation(op.getName());
            } catch (SpannerException | ExecutionException e) {
            // Ignore, this can happen, as the restore operation sometimes fails to start if there
            // is already a restore operation running on the instance.
            }
            // Assert that the RestoreDatabase RPC was called only once, and that the operation
            // tracking was resumed through a GetOperation call.
            assertEquals(1, restoreBackupInterceptor.methodCount.get());
            assertTrue(restoreBackupInterceptor.getOperationCount.get() >= 1);
        }
    }
    // Create another database with the exact same name as the first database.
    createDbInterceptor = new InjectErrorInterceptorProvider("CreateDatabase");
    options = testHelper.getOptions().toBuilder().setInterceptorProvider(createDbInterceptor).build();
    try (Spanner spanner = options.getService()) {
        DatabaseAdminClient client = spanner.getDatabaseAdminClient();
        // First drop the initial database.
        client.dropDatabase(testHelper.getInstanceId().getInstance(), initialDatabaseId);
        // Now re-create a database with the exact same name.
        OperationFuture<Database, CreateDatabaseMetadata> op = client.createDatabase(testHelper.getInstanceId().getInstance(), initialDatabaseId, Collections.emptyList());
        // Check that the second database was created and has a greater creation time than the
        // first.
        Timestamp secondCreationTime = op.get(DATABASE_TIMEOUT_MINUTES, TimeUnit.MINUTES).getCreateTime();
        // TODO: Change this to greaterThan when the create time of a database is reported back by
        // the server.
        assertTrue(secondCreationTime.compareTo(initialDbCreateTime) >= 0);
        // Assert that the CreateDatabase RPC was called only once, and that the operation tracking
        // was resumed through a GetOperation call.
        assertEquals(1, createDbInterceptor.methodCount.get());
        assertTrue(createDbInterceptor.getOperationCount.get() >= 1);
    }
}
Also used : Backup(com.google.cloud.spanner.Backup) ArrayList(java.util.ArrayList) Stopwatch(com.google.common.base.Stopwatch) Timestamp(com.google.cloud.Timestamp) SpannerOptions(com.google.cloud.spanner.SpannerOptions) Random(java.util.Random) Database(com.google.cloud.spanner.Database) CreateBackupMetadata(com.google.spanner.admin.database.v1.CreateBackupMetadata) DatabaseAdminClient(com.google.cloud.spanner.DatabaseAdminClient) RestoreDatabaseMetadata(com.google.spanner.admin.database.v1.RestoreDatabaseMetadata) SpannerException(com.google.cloud.spanner.SpannerException) CreateDatabaseMetadata(com.google.spanner.admin.database.v1.CreateDatabaseMetadata) ExecutionException(java.util.concurrent.ExecutionException) Spanner(com.google.cloud.spanner.Spanner) Test(org.junit.Test) SlowTest(com.google.cloud.spanner.SlowTest)

Example 5 with CreateBackupMetadata

use of com.google.bigtable.admin.v2.CreateBackupMetadata in project java-spanner by googleapis.

the class SpannerSample method cancelCreateBackup.

// [END spanner_create_backup]
// [START spanner_cancel_backup_create]
static void cancelCreateBackup(DatabaseAdminClient dbAdminClient, DatabaseId databaseId, BackupId backupId) {
    // Set expire time to 14 days from now.
    Timestamp expireTime = Timestamp.ofTimeMicroseconds(TimeUnit.MICROSECONDS.convert(System.currentTimeMillis() + TimeUnit.DAYS.toMillis(14), TimeUnit.MILLISECONDS));
    // Create a backup instance.
    Backup backup = dbAdminClient.newBackupBuilder(backupId).setDatabase(databaseId).setExpireTime(expireTime).build();
    // Start the creation of a backup.
    System.out.println("Creating backup [" + backup.getId() + "]...");
    OperationFuture<Backup, CreateBackupMetadata> op = backup.create();
    try {
        // Try to cancel the backup operation.
        System.out.println("Cancelling create backup operation for [" + backup.getId() + "]...");
        dbAdminClient.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();
        // successful or not.
        while (!pollingFuture.get().isDone()) {
            System.out.println("Waiting for the cancelled backup operation to finish...");
            Thread.sleep(TimeUnit.MILLISECONDS.convert(5, TimeUnit.SECONDS));
        }
        if (pollingFuture.get().getErrorCode() == null) {
            // Backup was created before it could be cancelled. Delete the backup.
            backup.delete();
            System.out.println("Backup operation for [" + backup.getId() + "] successfully finished before it could be cancelled");
        } else if (pollingFuture.get().getErrorCode().getCode() == StatusCode.Code.CANCELLED) {
            System.out.println("Backup operation for [" + backup.getId() + "] successfully cancelled");
        }
    } catch (ExecutionException e) {
        throw SpannerExceptionFactory.newSpannerException(e.getCause());
    } catch (InterruptedException e) {
        throw SpannerExceptionFactory.propagateInterrupt(e);
    }
}
Also used : Backup(com.google.cloud.spanner.Backup) CreateBackupMetadata(com.google.spanner.admin.database.v1.CreateBackupMetadata) OperationSnapshot(com.google.api.gax.longrunning.OperationSnapshot) ExecutionException(java.util.concurrent.ExecutionException) Timestamp(com.google.cloud.Timestamp)

Aggregations

CreateBackupMetadata (com.google.spanner.admin.database.v1.CreateBackupMetadata)18 Timestamp (com.google.cloud.Timestamp)12 Backup (com.google.cloud.spanner.Backup)7 ExecutionException (java.util.concurrent.ExecutionException)7 Test (org.junit.Test)7 Operation (com.google.longrunning.Operation)5 Backup (com.google.spanner.admin.database.v1.Backup)5 CreateDatabaseMetadata (com.google.spanner.admin.database.v1.CreateDatabaseMetadata)3 OperationSnapshot (com.google.api.gax.longrunning.OperationSnapshot)2 Database (com.google.cloud.spanner.Database)2 SlowTest (com.google.cloud.spanner.SlowTest)2 SpannerException (com.google.cloud.spanner.SpannerException)2 InvalidProtocolBufferException (com.google.protobuf.InvalidProtocolBufferException)2 CreateBackupRequest (com.google.spanner.admin.database.v1.CreateBackupRequest)2 RestoreDatabaseMetadata (com.google.spanner.admin.database.v1.RestoreDatabaseMetadata)2 CancelledException (com.google.api.gax.rpc.CancelledException)1 DatabaseAdminClient (com.google.cloud.spanner.DatabaseAdminClient)1 DatabaseClient (com.google.cloud.spanner.DatabaseClient)1 Instance (com.google.cloud.spanner.Instance)1 Spanner (com.google.cloud.spanner.Spanner)1