Search in sources :

Example 56 with Backup

use of com.google.spanner.admin.database.v1.Backup in project java-spanner by googleapis.

the class DatabaseAdminClientTest method listBackupsTest.

@Test
public void listBackupsTest() throws Exception {
    Backup responsesElement = Backup.newBuilder().build();
    ListBackupsResponse expectedResponse = ListBackupsResponse.newBuilder().setNextPageToken("").addAllBackups(Arrays.asList(responsesElement)).build();
    mockDatabaseAdmin.addResponse(expectedResponse);
    InstanceName parent = InstanceName.of("[PROJECT]", "[INSTANCE]");
    ListBackupsPagedResponse pagedListResponse = client.listBackups(parent);
    List<Backup> resources = Lists.newArrayList(pagedListResponse.iterateAll());
    Assert.assertEquals(1, resources.size());
    Assert.assertEquals(expectedResponse.getBackupsList().get(0), resources.get(0));
    List<AbstractMessage> actualRequests = mockDatabaseAdmin.getRequests();
    Assert.assertEquals(1, actualRequests.size());
    ListBackupsRequest actualRequest = ((ListBackupsRequest) actualRequests.get(0));
    Assert.assertEquals(parent.toString(), actualRequest.getParent());
    Assert.assertTrue(channelProvider.isHeaderSent(ApiClientHeaderProvider.getDefaultApiClientHeaderKey(), GaxGrpcProperties.getDefaultApiClientHeaderPattern()));
}
Also used : InstanceName(com.google.spanner.admin.database.v1.InstanceName) AbstractMessage(com.google.protobuf.AbstractMessage) Backup(com.google.spanner.admin.database.v1.Backup) ListBackupsRequest(com.google.spanner.admin.database.v1.ListBackupsRequest) ListBackupsResponse(com.google.spanner.admin.database.v1.ListBackupsResponse) ListBackupsPagedResponse(com.google.cloud.spanner.admin.database.v1.DatabaseAdminClient.ListBackupsPagedResponse) Test(org.junit.Test)

Example 57 with Backup

use of com.google.spanner.admin.database.v1.Backup in project java-spanner by googleapis.

the class ITBackupTest method test01_Backups.

@Test
public void test01_Backups() throws InterruptedException, ExecutionException, TimeoutException {
    final String databaseId = testHelper.getUniqueDatabaseId() + "_db1";
    final Database sourceDatabase = dbAdminClient.newDatabaseBuilder(DatabaseId.of(projectId, instanceId, databaseId)).setEncryptionConfig(EncryptionConfigs.customerManagedEncryption(keyName)).build();
    logger.info(String.format("Creating test database %s", databaseId));
    OperationFuture<Database, CreateDatabaseMetadata> createDatabaseOperation = dbAdminClient.createDatabase(sourceDatabase, Collections.singletonList("CREATE TABLE FOO (ID INT64, NAME STRING(100)) PRIMARY KEY (ID)"));
    // Make sure the database has been created before we try to create a backup.
    Database database = createDatabaseOperation.get(DATABASE_TIMEOUT_MINUTES, TimeUnit.MINUTES);
    databases.add(database.getId().getDatabase());
    // Insert some data to make sure the backup will have a size>0.
    DatabaseClient client = testHelper.getDatabaseClient(database);
    client.writeAtLeastOnce(Collections.singletonList(Mutation.newInsertOrUpdateBuilder("FOO").set("ID").to(1L).set("NAME").to("TEST").build()));
    // Verifies that the database encryption has been properly set
    testDatabaseEncryption(database, keyName);
    // Verifies that the database dialect has been properly set
    testDatabaseDialect(database, Dialect.GOOGLE_STANDARD_SQL);
    // Create a backup of the database.
    String backupId = testHelper.getUniqueBackupId() + "_bck1";
    Timestamp expireTime = afterDays(7);
    Timestamp versionTime = getCurrentTimestamp(client);
    logger.info(String.format("Creating backup %s", backupId));
    // This backup has the version time specified as the server's current timestamp
    // This backup is encrypted with a customer managed key
    // The expiry time is 7 days in the future.
    final Backup backupToCreate = dbAdminClient.newBackupBuilder(BackupId.of(projectId, instanceId, backupId)).setDatabase(database.getId()).setExpireTime(expireTime).setVersionTime(versionTime).setExpireTime(expireTime).setEncryptionConfig(EncryptionConfigs.customerManagedEncryption(keyName)).build();
    OperationFuture<Backup, CreateBackupMetadata> operation = dbAdminClient.createBackup(backupToCreate);
    backups.add(backupId);
    // Execute metadata tests as part of this integration test to reduce total execution time.
    testMetadata(operation, backupId, database);
    // Ensure that the backup has been created before we proceed.
    logger.info("Waiting for backup operation to finish");
    Backup backup = operation.get(BACKUP_TIMEOUT_MINUTES, TimeUnit.MINUTES);
    // Verifies that backup version time is the specified one
    testBackupVersionTime(backup, versionTime);
    // Verifies that backup encryption has been properly set
    testBackupEncryption(backup, keyName);
    // Insert some more data into the database to get a timestamp from the server.
    Timestamp commitTs = client.writeAtLeastOnce(Collections.singletonList(Mutation.newInsertOrUpdateBuilder("FOO").set("ID").to(2L).set("NAME").to("TEST2").build()));
    // Test listing operations.
    // List all backups.
    logger.info("Listing all backups");
    assertTrue(Iterables.contains(instance.listBackups().iterateAll(), backup));
    // List all backups whose names contain 'bck1'.
    logger.info("Listing backups with name bck1");
    assertTrue(Iterables.elementsEqual(dbAdminClient.listBackups(instanceId, Options.filter(String.format("name:%s", backup.getId().getName()))).iterateAll(), Collections.singleton(backup)));
    logger.info("Listing ready backups");
    Iterable<Backup> readyBackups = dbAdminClient.listBackups(instanceId, Options.filter("state:READY")).iterateAll();
    assertTrue(Iterables.contains(readyBackups, backup));
    // List all backups for databases whose names contain 'db1'.
    logger.info("Listing backups for database db1");
    assertTrue(Iterables.elementsEqual(dbAdminClient.listBackups(instanceId, Options.filter(String.format("database:%s", database.getId().getName()))).iterateAll(), Collections.singleton(backup)));
    // List all backups that were created before a certain time.
    Timestamp ts = Timestamp.ofTimeSecondsAndNanos(commitTs.getSeconds(), 0);
    logger.info(String.format("Listing backups created before %s", ts));
    assertTrue(Iterables.contains(dbAdminClient.listBackups(instanceId, Options.filter(String.format("create_time<\"%s\"", ts))).iterateAll(), backup));
    // List all backups with a size > 0.
    logger.info("Listing backups with size>0");
    assertTrue(Iterables.contains(dbAdminClient.listBackups(instanceId, Options.filter("size_bytes>0")).iterateAll(), backup));
    // Test pagination.
    testPagination();
    logger.info("Finished listBackup tests");
    // Execute other tests as part of this integration test to reduce total execution time.
    testGetBackup(database, backupId, expireTime);
    testUpdateBackup(backup);
    testCreateInvalidExpirationDate(database);
    testRestore(backup, versionTime, keyName);
    testCancelBackupOperation(database);
    // Finished all tests.
    logger.info("Finished all backup tests");
}
Also used : DatabaseClient(com.google.cloud.spanner.DatabaseClient) Backup(com.google.cloud.spanner.Backup) Database(com.google.cloud.spanner.Database) CreateBackupMetadata(com.google.spanner.admin.database.v1.CreateBackupMetadata) CreateDatabaseMetadata(com.google.spanner.admin.database.v1.CreateDatabaseMetadata) Timestamp(com.google.cloud.Timestamp) Test(org.junit.Test) SlowTest(com.google.cloud.spanner.SlowTest)

Example 58 with Backup

use of com.google.spanner.admin.database.v1.Backup in project java-spanner by googleapis.

the class ITBackupTest method testCreateInvalidExpirationDate.

private void testCreateInvalidExpirationDate(Database database) {
    // This is not allowed, the expiration date must be at least 6 hours in the future.
    Timestamp expireTime = daysAgo(1);
    String backupId = testHelper.getUniqueBackupId();
    logger.info(String.format("Creating backup %s with invalid expiration date", backupId));
    OperationFuture<Backup, CreateBackupMetadata> op = dbAdminClient.createBackup(instanceId, backupId, database.getId().getDatabase(), expireTime);
    backups.add(backupId);
    ExecutionException executionException = assertThrows(ExecutionException.class, op::get);
    Throwable cause = executionException.getCause();
    assertEquals(SpannerException.class, cause.getClass());
    SpannerException spannerException = (SpannerException) cause;
    assertEquals(ErrorCode.INVALID_ARGUMENT, spannerException.getErrorCode());
}
Also used : Backup(com.google.cloud.spanner.Backup) CreateBackupMetadata(com.google.spanner.admin.database.v1.CreateBackupMetadata) SpannerException(com.google.cloud.spanner.SpannerException) ExecutionException(java.util.concurrent.ExecutionException) Timestamp(com.google.cloud.Timestamp)

Example 59 with Backup

use of com.google.spanner.admin.database.v1.Backup in project java-spanner by googleapis.

the class ITBackupTest method testCancelBackupOperation.

private void testCancelBackupOperation(Database database) throws InterruptedException, ExecutionException {
    Timestamp expireTime = afterDays(7);
    String backupId = testHelper.getUniqueBackupId();
    logger.info(String.format("Starting to create backup %s", backupId));
    OperationFuture<Backup, CreateBackupMetadata> op = dbAdminClient.createBackup(instanceId, backupId, database.getId().getDatabase(), expireTime);
    backups.add(backupId);
    // Cancel the backup operation.
    logger.info(String.format("Cancelling the creation of backup %s", backupId));
    dbAdminClient.cancelOperation(op.getName());
    logger.info("Fetching backup operations");
    boolean operationFound = false;
    for (Operation operation : dbAdminClient.listBackupOperations(instanceId, Options.filter(String.format("name:%s", op.getName()))).iterateAll()) {
        assertEquals(Code.CANCELLED.value(), operation.getError().getCode());
        operationFound = true;
    }
    assertTrue(operationFound);
    logger.info("Finished cancel test");
}
Also used : Backup(com.google.cloud.spanner.Backup) CreateBackupMetadata(com.google.spanner.admin.database.v1.CreateBackupMetadata) Operation(com.google.longrunning.Operation) Timestamp(com.google.cloud.Timestamp)

Example 60 with Backup

use of com.google.spanner.admin.database.v1.Backup in project java-spanner by googleapis.

the class ITBackupTest method setup.

@BeforeClass
public static void setup() {
    assumeFalse("backups are not supported on the emulator", isUsingEmulator());
    keyName = System.getProperty(KMS_KEY_NAME_PROPERTY);
    Preconditions.checkNotNull(keyName, "Key name is null, please set a key to be used for this test. The necessary permissions should be grant to the spanner service account according to the CMEK user guide.");
    logger.info("Setting up tests");
    testHelper = env.getTestHelper();
    dbAdminClient = testHelper.getClient().getDatabaseAdminClient();
    InstanceAdminClient instanceAdminClient = testHelper.getClient().getInstanceAdminClient();
    instance = instanceAdminClient.getInstance(testHelper.getInstanceId().getInstance());
    projectId = testHelper.getInstanceId().getProject();
    instanceId = testHelper.getInstanceId().getInstance();
    logger.info("Finished setup");
    // Cancel any backup operation that has been started by this integration test if it has been
    // running for at least 6 hours.
    logger.info("Cancelling long-running test backup operations");
    Pattern pattern = Pattern.compile(".*/backups/testbck_\\d{6}_\\d{4}_bck\\d/operations/.*");
    try {
        for (Operation operation : dbAdminClient.listBackupOperations(instance.getId().getInstance()).iterateAll()) {
            Matcher matcher = pattern.matcher(operation.getName());
            if (matcher.matches()) {
                if (!operation.getDone()) {
                    Timestamp currentTime = Timestamp.now();
                    Timestamp startTime = Timestamp.fromProto(operation.getMetadata().unpack(CreateBackupMetadata.class).getProgress().getStartTime());
                    long diffSeconds = currentTime.getSeconds() - startTime.getSeconds();
                    if (TimeUnit.HOURS.convert(diffSeconds, TimeUnit.SECONDS) >= 6L) {
                        logger.warning(String.format("Cancelling test backup operation %s that was started at %s", operation.getName(), startTime));
                        dbAdminClient.cancelOperation(operation.getName());
                    }
                }
            }
        }
    } catch (InvalidProtocolBufferException e) {
        logger.log(Level.WARNING, "Could not list all existing backup operations.", e);
    }
    logger.info("Finished checking existing test backup operations");
}
Also used : Pattern(java.util.regex.Pattern) Matcher(java.util.regex.Matcher) InstanceAdminClient(com.google.cloud.spanner.InstanceAdminClient) CreateBackupMetadata(com.google.spanner.admin.database.v1.CreateBackupMetadata) InvalidProtocolBufferException(com.google.protobuf.InvalidProtocolBufferException) Operation(com.google.longrunning.Operation) Timestamp(com.google.cloud.Timestamp) BeforeClass(org.junit.BeforeClass)

Aggregations

Test (org.junit.Test)43 Backup (com.google.spanner.admin.database.v1.Backup)21 AbstractMessage (com.google.protobuf.AbstractMessage)19 ByteString (com.google.protobuf.ByteString)19 CreateBackupMetadata (com.google.spanner.admin.database.v1.CreateBackupMetadata)18 ExecutionException (java.util.concurrent.ExecutionException)17 Timestamp (com.google.cloud.Timestamp)16 Operation (com.google.longrunning.Operation)14 InvalidArgumentException (com.google.api.gax.rpc.InvalidArgumentException)11 StatusRuntimeException (io.grpc.StatusRuntimeException)11 ArrayList (java.util.ArrayList)11 Backup (com.google.bigtable.admin.v2.Backup)10 Backup (com.google.cloud.spanner.Backup)8 BackupName (com.google.spanner.admin.database.v1.BackupName)8 InstanceName (com.google.spanner.admin.database.v1.InstanceName)7 Database (com.google.cloud.spanner.Database)5 FieldMask (com.google.protobuf.FieldMask)4 InvalidProtocolBufferException (com.google.protobuf.InvalidProtocolBufferException)4 CreateBackupRequest (com.google.spanner.admin.database.v1.CreateBackupRequest)4 Database (com.google.spanner.admin.database.v1.Database)4