Search in sources :

Example 11 with CreateBackupMetadata

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

the class MockDatabaseAdminServiceImpl method matchesFilter.

private boolean matchesFilter(Object obj, String filter) throws Exception {
    if (!Strings.isNullOrEmpty(filter)) {
        Set<String> matches = filterMatches.get(filter);
        if (matches != null) {
            String name = (String) obj.getClass().getMethod("getName").invoke(obj);
            return matches.contains(name);
        }
        if (obj instanceof Operation) {
            Operation operation = (Operation) obj;
            Pattern pattern = Pattern.compile("(?:\\(metadata.@type:type.googleapis.com/(.*)\\)) AND (?:\\(metadata.(?:name|database):(.*)\\)|\\(name:(.*)/operations/\\))");
            Matcher matcher = pattern.matcher(filter);
            if (matcher.matches()) {
                String type = matcher.group(1);
                String objectName = matcher.group(2);
                if (objectName == null) {
                    objectName = matcher.group(3);
                }
                Any anyMetadata = operation.getMetadata();
                if (anyMetadata.getTypeUrl().endsWith(type)) {
                    if (type.equals(CreateBackupMetadata.getDescriptor().getFullName())) {
                        CreateBackupMetadata metadata = operation.getMetadata().unpack(CreateBackupMetadata.class);
                        return metadata.getName().equals(objectName);
                    } else if (type.equals(CreateDatabaseMetadata.getDescriptor().getFullName())) {
                        CreateDatabaseMetadata metadata = operation.getMetadata().unpack(CreateDatabaseMetadata.class);
                        return metadata.getDatabase().equals(objectName);
                    } else if (type.equals(RestoreDatabaseMetadata.getDescriptor().getFullName())) {
                        RestoreDatabaseMetadata metadata = operation.getMetadata().unpack(RestoreDatabaseMetadata.class);
                        return metadata.getName().equals(objectName);
                    }
                }
            }
        }
        return false;
    }
    return true;
}
Also used : Pattern(java.util.regex.Pattern) Matcher(java.util.regex.Matcher) CreateBackupMetadata(com.google.spanner.admin.database.v1.CreateBackupMetadata) RestoreDatabaseMetadata(com.google.spanner.admin.database.v1.RestoreDatabaseMetadata) Operation(com.google.longrunning.Operation) CreateDatabaseMetadata(com.google.spanner.admin.database.v1.CreateDatabaseMetadata) Any(com.google.protobuf.Any)

Example 12 with CreateBackupMetadata

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

the class MockDatabaseAdminServiceImpl method createBackup.

@Override
public void createBackup(CreateBackupRequest request, StreamObserver<Operation> responseObserver) {
    requests.add(request);
    try {
        createBackupStartupExecutionTime.simulateExecutionTime(exceptions, false, freezeLock);
        String name = String.format("%s/backups/%s", request.getParent(), request.getBackupId());
        MockDatabase db = databases.get(request.getBackup().getDatabase());
        if (db == null) {
            responseObserver.onError(Status.NOT_FOUND.withDescription(String.format("Database with name %s not found", request.getBackup().getDatabase())).asRuntimeException());
            return;
        }
        MockBackup bck = new MockBackup(name, request.getBackup(), db);
        if (backups.putIfAbsent(name, bck) == null) {
            CreateBackupMetadata metadata = CreateBackupMetadata.newBuilder().setName(name).setDatabase(bck.database).setProgress(OperationProgress.newBuilder().setStartTime(Timestamp.newBuilder().setSeconds(System.currentTimeMillis() / 1000L).build()).setProgressPercent(0)).build();
            Operation operation = Operation.newBuilder().setMetadata(Any.pack(metadata)).setResponse(Any.pack(bck.toProto())).setName(operations.generateOperationName(name)).build();
            operations.addOperation(operation, new CreateBackupCallable(operation.getName(), name));
            createBackupResponseExecutionTime.simulateExecutionTime(exceptions, false, freezeLock);
            responseObserver.onNext(operation);
            responseObserver.onCompleted();
        } else {
            responseObserver.onError(Status.ALREADY_EXISTS.withDescription(String.format("Backup with name %s already exists", name)).asRuntimeException());
        }
    } catch (Throwable t) {
        responseObserver.onError(t);
    }
}
Also used : CreateBackupMetadata(com.google.spanner.admin.database.v1.CreateBackupMetadata) Operation(com.google.longrunning.Operation)

Example 13 with CreateBackupMetadata

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

the class MockDatabaseAdminServiceImplTest method createTestBackup.

private Backup createTestBackup() {
    CreateBackupRequest request = CreateBackupRequest.newBuilder().setBackupId("test-bck").setBackup(Backup.newBuilder().setDatabase(TEST_DB_NAME).setExpireTime(Timestamp.newBuilder().setSeconds(System.currentTimeMillis() * 1000L + TimeUnit.MILLISECONDS.convert(7, TimeUnit.DAYS))).build()).setParent(TEST_PARENT).build();
    OperationFuture<Backup, CreateBackupMetadata> op = client.createBackupOperationCallable().futureCall(request);
    try {
        return op.get();
    } catch (ExecutionException e) {
        if (e.getCause() != null && e.getCause() instanceof RuntimeException) {
            throw (RuntimeException) e.getCause();
        }
        throw new RuntimeException(e);
    } catch (InterruptedException e) {
        throw new CancelledException(e, FakeStatusCode.of(Code.CANCELLED), false);
    }
}
Also used : CreateBackupRequest(com.google.spanner.admin.database.v1.CreateBackupRequest) CancelledException(com.google.api.gax.rpc.CancelledException) Backup(com.google.spanner.admin.database.v1.Backup) CreateBackupMetadata(com.google.spanner.admin.database.v1.CreateBackupMetadata) ExecutionException(java.util.concurrent.ExecutionException)

Example 14 with CreateBackupMetadata

use of com.google.spanner.admin.database.v1.CreateBackupMetadata 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 15 with CreateBackupMetadata

use of com.google.spanner.admin.database.v1.CreateBackupMetadata 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)

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