Search in sources :

Example 1 with OperationSnapshot

use of com.google.api.gax.longrunning.OperationSnapshot 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)

Example 2 with OperationSnapshot

use of com.google.api.gax.longrunning.OperationSnapshot in project gax-java by googleapis.

the class ProtoOperationTransformersTest method testAnyResponseTransformer_mismatchedTypes.

@Test
public void testAnyResponseTransformer_mismatchedTypes() {
    ResponseTransformer<Money> transformer = ResponseTransformer.create(Money.class);
    Status status = Status.newBuilder().setCode(Code.OK.value()).build();
    OperationSnapshot operationSnapshot = GrpcOperationSnapshot.create(Operation.newBuilder().setResponse(Any.pack(Color.getDefaultInstance())).setError(status).build());
    Exception exception = assertThrows(UnknownException.class, () -> transformer.apply(operationSnapshot));
    Truth.assertThat(exception).hasMessageThat().contains("encountered a problem unpacking it");
}
Also used : Status(com.google.rpc.Status) Money(com.google.type.Money) OperationSnapshot(com.google.api.gax.longrunning.OperationSnapshot) UnknownException(com.google.api.gax.rpc.UnknownException) UnavailableException(com.google.api.gax.rpc.UnavailableException) Test(org.junit.Test)

Example 3 with OperationSnapshot

use of com.google.api.gax.longrunning.OperationSnapshot in project gax-java by googleapis.

the class ProtoOperationTransformersTest method testAnyResponseTransformer_exception.

@Test
public void testAnyResponseTransformer_exception() {
    ResponseTransformer<Money> transformer = ResponseTransformer.create(Money.class);
    Money inputMoney = Money.newBuilder().setCurrencyCode("USD").build();
    Status status = Status.newBuilder().setCode(Code.UNAVAILABLE.value()).build();
    OperationSnapshot operationSnapshot = GrpcOperationSnapshot.create(Operation.newBuilder().setResponse(Any.pack(inputMoney)).setError(status).build());
    Exception exception = assertThrows(UnavailableException.class, () -> transformer.apply(operationSnapshot));
    Truth.assertThat(exception).hasMessageThat().contains("failed with status = GrpcStatusCode{transportCode=UNAVAILABLE}");
}
Also used : Status(com.google.rpc.Status) Money(com.google.type.Money) OperationSnapshot(com.google.api.gax.longrunning.OperationSnapshot) UnknownException(com.google.api.gax.rpc.UnknownException) UnavailableException(com.google.api.gax.rpc.UnavailableException) Test(org.junit.Test)

Example 4 with OperationSnapshot

use of com.google.api.gax.longrunning.OperationSnapshot in project gax-java by googleapis.

the class ProtoOperationTransformersTest method testAnyMetadataTransformer.

@Test
public void testAnyMetadataTransformer() {
    MetadataTransformer<Money> transformer = MetadataTransformer.create(Money.class);
    Money inputMoney = Money.newBuilder().setCurrencyCode("USD").build();
    OperationSnapshot operationSnapshot = GrpcOperationSnapshot.create(Operation.newBuilder().setMetadata(Any.pack(inputMoney)).build());
    Truth.assertThat(transformer.apply(operationSnapshot)).isEqualTo(inputMoney);
}
Also used : Money(com.google.type.Money) OperationSnapshot(com.google.api.gax.longrunning.OperationSnapshot) Test(org.junit.Test)

Example 5 with OperationSnapshot

use of com.google.api.gax.longrunning.OperationSnapshot in project gax-java by googleapis.

the class HttpJsonOperationSnapshotCallableTest method futureCallTestException.

@Test
public void futureCallTestException() throws InterruptedException {
    Option request = Option.newBuilder().setName("Arizona").build();
    Field field = Field.newBuilder().setName("Georgia").build();
    ApiCallContext context = mock(ApiCallContext.class);
    OperationSnapshot operationSnapshot = HttpJsonOperationSnapshot.newBuilder().setName("California").setMetadata(2).setDone(true).setResponse("Florida").setError(0, "no error").build();
    SettableApiFuture<Field> settableApiFuture = SettableApiFuture.create();
    settableApiFuture.setException(new RuntimeException("Seattle"));
    when(operationSnapshotFactory.create(request, field)).thenReturn(operationSnapshot);
    when(innerCallable.futureCall(request, context)).thenReturn(settableApiFuture);
    try {
        operationSnapCallable.futureCall(request, context).get();
        Assert.fail("Exception should have been thrown");
    } catch (ExecutionException e) {
        Truth.assertThat(e).hasMessageThat().contains("Seattle");
    }
}
Also used : Field(com.google.protobuf.Field) Option(com.google.protobuf.Option) ApiCallContext(com.google.api.gax.rpc.ApiCallContext) OperationSnapshot(com.google.api.gax.longrunning.OperationSnapshot) ExecutionException(java.util.concurrent.ExecutionException) Test(org.junit.Test)

Aggregations

OperationSnapshot (com.google.api.gax.longrunning.OperationSnapshot)52 Test (org.junit.Test)44 FakeOperationSnapshot (com.google.api.gax.rpc.testing.FakeOperationSnapshot)26 Color (java.awt.Color)23 Currency (java.util.Currency)23 Money (com.google.type.Money)13 UnavailableException (com.google.api.gax.rpc.UnavailableException)7 UnknownException (com.google.api.gax.rpc.UnknownException)6 Status (com.google.rpc.Status)6 FakeStatusCode (com.google.api.gax.rpc.testing.FakeStatusCode)4 ExecutionException (java.util.concurrent.ExecutionException)4 FakeApiClock (com.google.api.gax.core.FakeApiClock)2 FakeApiMessage (com.google.api.gax.httpjson.testing.FakeApiMessage)2 TimedRetryAlgorithm (com.google.api.gax.retrying.TimedRetryAlgorithm)2 ApiCallContext (com.google.api.gax.rpc.ApiCallContext)2 ApiException (com.google.api.gax.rpc.ApiException)2 FakeChannel (com.google.api.gax.rpc.testing.FakeChannel)2 Timestamp (com.google.cloud.Timestamp)2 Field (com.google.protobuf.Field)2 Option (com.google.protobuf.Option)2