use of com.google.api.gax.longrunning.OperationSnapshot in project gax-java by googleapis.
the class Callables method longRunningOperationImpl.
static <RequestT, ResponseT, MetadataT> OperationCallableImpl<RequestT, ResponseT, MetadataT> longRunningOperationImpl(UnaryCallable<RequestT, OperationSnapshot> initialCallable, OperationCallSettings<RequestT, ResponseT, MetadataT> operationCallSettings, ClientContext clientContext, LongRunningClient longRunningClient) {
RetryAlgorithm<OperationSnapshot> pollingAlgorithm = new RetryAlgorithm<>(new OperationResponsePollAlgorithm(), operationCallSettings.getPollingAlgorithm());
ScheduledRetryingExecutor<OperationSnapshot> scheduler = new ScheduledRetryingExecutor<>(pollingAlgorithm, clientContext.getExecutor());
return new OperationCallableImpl<>(initialCallable, scheduler, longRunningClient, operationCallSettings);
}
use of com.google.api.gax.longrunning.OperationSnapshot in project java-bigtable by googleapis.
the class BigtableInstanceAdminClientTests method mockOperationResult.
private <ReqT, RespT, MetaT> void mockOperationResult(OperationCallable<ReqT, RespT, MetaT> callable, ReqT request, RespT response) {
OperationSnapshot operationSnapshot = FakeOperationSnapshot.newBuilder().setDone(true).setErrorCode(GrpcStatusCode.of(Code.OK)).setName("fake-name").setResponse(response).build();
OperationFuture<RespT, MetaT> operationFuture = OperationFutures.immediateOperationFuture(operationSnapshot);
Mockito.when(callable.futureCall(request)).thenReturn(operationFuture);
}
use of com.google.api.gax.longrunning.OperationSnapshot in project java-bigtable by googleapis.
the class BigtableTableAdminClientTests method mockOperationResult.
private <ReqT, RespT, MetaT> void mockOperationResult(OperationCallable<ReqT, RespT, MetaT> callable, ReqT request, RespT response, MetaT metadata) {
OperationSnapshot operationSnapshot = FakeOperationSnapshot.newBuilder().setDone(true).setErrorCode(GrpcStatusCode.of(Code.OK)).setName("fake-name").setResponse(response).setMetadata(metadata).build();
OperationFuture<RespT, MetaT> operationFuture = OperationFutures.immediateOperationFuture(operationSnapshot);
Mockito.when(callable.futureCall(request)).thenReturn(operationFuture);
}
use of com.google.api.gax.longrunning.OperationSnapshot in project java-spanner by googleapis.
the class DatabaseAdminClientTest method backupCreateCancel.
@Test
public void backupCreateCancel() {
final String backupId = "other-backup-id";
// Set expire time to 14 days from now.
long currentTimeInMicroSeconds = TimeUnit.MICROSECONDS.convert(System.currentTimeMillis(), TimeUnit.MILLISECONDS);
long deltaTimeInMicroseconds = TimeUnit.MICROSECONDS.convert(14L, TimeUnit.DAYS);
Timestamp expireTime = Timestamp.ofTimeMicroseconds(currentTimeInMicroSeconds + deltaTimeInMicroseconds);
Backup backup = client.newBackupBuilder(BackupId.of(PROJECT_ID, INSTANCE_ID, backupId)).setDatabase(DatabaseId.of(PROJECT_ID, INSTANCE_ID, DB_ID)).setExpireTime(expireTime).build();
// Start a creation of a backup.
OperationFuture<Backup, CreateBackupMetadata> op = backup.create();
try {
// Try to cancel the backup operation.
client.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();
// or any other error occurred.
while (!pollingFuture.get().isDone()) {
Thread.sleep(TimeUnit.MILLISECONDS.convert(5, TimeUnit.SECONDS));
}
} catch (CancellationException e) {
// ignore, this exception may also occur if the polling future has been cancelled.
} catch (ExecutionException e) {
throw (RuntimeException) e.getCause();
} catch (InterruptedException e) {
throw SpannerExceptionFactory.propagateInterrupt(e);
} finally {
backup.delete();
}
}
use of com.google.api.gax.longrunning.OperationSnapshot in project gapic-generator-java by googleapis.
the class RetrySettingsComposer method createLroSettingsBuilderExpr.
public static Expr createLroSettingsBuilderExpr(Service service, GapicServiceConfig serviceConfig, Method method, VariableExpr builderVarExpr, VariableExpr retryableCodeDefsVarExpr, VariableExpr retryParamDefsVarExpr, TypeNode operationResponseTransformer, TypeNode operationMetadataTransformer) {
Preconditions.checkState(method.hasLro(), String.format("Tried to create LRO settings initialization for non-LRO method %s", method.name()));
String codeName = serviceConfig.getRetryCodeName(service, method);
String retryParamName = serviceConfig.getRetryParamsName(service, method);
String settingsGetterMethodName = String.format("%sOperationSettings", JavaStyle.toLowerCamelCase(method.name()));
Function<String, ValueExpr> strValExprFn = s -> ValueExpr.withValue(StringObjectValue.withValue(s));
// Argument for setInitialCallSettings.
Expr unaryCallSettingsExpr = MethodInvocationExpr.builder().setStaticReferenceType(FIXED_TYPESTORE.get("UnaryCallSettings")).setGenerics(Arrays.asList(method.inputType().reference(), FIXED_TYPESTORE.get("OperationSnapshot").reference())).setMethodName("newUnaryCallSettingsBuilder").build();
unaryCallSettingsExpr = MethodInvocationExpr.builder().setExprReferenceExpr(unaryCallSettingsExpr).setMethodName("setRetryableCodes").setArguments(MethodInvocationExpr.builder().setExprReferenceExpr(retryableCodeDefsVarExpr).setMethodName("get").setArguments(strValExprFn.apply(codeName)).build()).build();
unaryCallSettingsExpr = MethodInvocationExpr.builder().setExprReferenceExpr(unaryCallSettingsExpr).setMethodName("setRetrySettings").setArguments(MethodInvocationExpr.builder().setExprReferenceExpr(retryParamDefsVarExpr).setMethodName("get").setArguments(strValExprFn.apply(retryParamName)).build()).build();
unaryCallSettingsExpr = MethodInvocationExpr.builder().setExprReferenceExpr(unaryCallSettingsExpr).setMethodName("build").build();
Expr builderSettingsExpr = MethodInvocationExpr.builder().setExprReferenceExpr(builderVarExpr).setMethodName(settingsGetterMethodName).build();
builderSettingsExpr = MethodInvocationExpr.builder().setExprReferenceExpr(builderSettingsExpr).setMethodName("setInitialCallSettings").setArguments(unaryCallSettingsExpr).build();
Function<TypeNode, VariableExpr> classFieldRefFn = t -> VariableExpr.builder().setVariable(Variable.builder().setType(TypeNode.CLASS_OBJECT).setName("class").build()).setStaticReferenceType(t).build();
builderSettingsExpr = MethodInvocationExpr.builder().setExprReferenceExpr(builderSettingsExpr).setMethodName("setResponseTransformer").setArguments(MethodInvocationExpr.builder().setStaticReferenceType(operationResponseTransformer).setMethodName("create").setArguments(classFieldRefFn.apply(method.lro().responseType())).build()).build();
builderSettingsExpr = MethodInvocationExpr.builder().setExprReferenceExpr(builderSettingsExpr).setMethodName("setMetadataTransformer").setArguments(MethodInvocationExpr.builder().setStaticReferenceType(operationMetadataTransformer).setMethodName("create").setArguments(classFieldRefFn.apply(method.lro().metadataType())).build()).build();
Expr lroRetrySettingsExpr = createLroRetrySettingsExpr(service, method, serviceConfig);
Expr pollAlgoExpr = MethodInvocationExpr.builder().setStaticReferenceType(FIXED_TYPESTORE.get("OperationTimedPollAlgorithm")).setMethodName("create").setArguments(lroRetrySettingsExpr).build();
builderSettingsExpr = MethodInvocationExpr.builder().setExprReferenceExpr(builderSettingsExpr).setMethodName("setPollingAlgorithm").setArguments(pollAlgoExpr).build();
return builderSettingsExpr;
}
Aggregations