Search in sources :

Example 1 with AdminRequestsPerMinuteExceededException

use of com.google.cloud.spanner.AdminRequestsPerMinuteExceededException in project java-spanner by googleapis.

the class GapicSpannerRpc method updateDatabaseDdl.

/**
 * If the update database ddl operation returns an ALREADY_EXISTS error, meaning the operation id
 * used is already in flight, this method will simply resume the original operation. The returned
 * future will be completed when the original operation finishes.
 *
 * <p>This mechanism is necessary, because the update database ddl can be retried. If a retryable
 * failure occurs, the backend has already started processing the update database ddl operation
 * with the given id and the library issues a retry, an ALREADY_EXISTS error will be returned. If
 * we were to bubble this error up, it would be confusing for the caller, who used originally
 * called the method with a new operation id.
 */
@Override
public OperationFuture<Empty, UpdateDatabaseDdlMetadata> updateDatabaseDdl(final String databaseName, final Iterable<String> updateDatabaseStatements, @Nullable final String updateId) throws SpannerException {
    acquireAdministrativeRequestsRateLimiter();
    final UpdateDatabaseDdlRequest request = UpdateDatabaseDdlRequest.newBuilder().setDatabase(databaseName).addAllStatements(updateDatabaseStatements).setOperationId(MoreObjects.firstNonNull(updateId, "")).build();
    final GrpcCallContext context = newCallContext(null, databaseName, request, DatabaseAdminGrpc.getUpdateDatabaseDdlMethod());
    final OperationCallable<UpdateDatabaseDdlRequest, Empty, UpdateDatabaseDdlMetadata> callable = databaseAdminStub.updateDatabaseDdlOperationCallable();
    return runWithRetryOnAdministrativeRequestsExceeded(() -> {
        OperationFuture<Empty, UpdateDatabaseDdlMetadata> operationFuture = callable.futureCall(request, context);
        try {
            operationFuture.getInitialFuture().get();
        } catch (InterruptedException e) {
            throw newSpannerException(e);
        } catch (ExecutionException e) {
            Throwable t = e.getCause();
            SpannerException se = SpannerExceptionFactory.asSpannerException(t);
            if (se instanceof AdminRequestsPerMinuteExceededException) {
                // Propagate this to trigger a retry.
                throw se;
            }
            if (t instanceof AlreadyExistsException) {
                String operationName = OPERATION_NAME_TEMPLATE.instantiate("database", databaseName, "operation", updateId);
                return callable.resumeFutureCall(operationName, context);
            }
        }
        return operationFuture;
    });
}
Also used : AlreadyExistsException(com.google.api.gax.rpc.AlreadyExistsException) AdminRequestsPerMinuteExceededException(com.google.cloud.spanner.AdminRequestsPerMinuteExceededException) UpdateDatabaseDdlRequest(com.google.spanner.admin.database.v1.UpdateDatabaseDdlRequest) Empty(com.google.protobuf.Empty) GrpcCallContext(com.google.api.gax.grpc.GrpcCallContext) UpdateDatabaseDdlMetadata(com.google.spanner.admin.database.v1.UpdateDatabaseDdlMetadata) SpannerExceptionFactory.newSpannerException(com.google.cloud.spanner.SpannerExceptionFactory.newSpannerException) SpannerException(com.google.cloud.spanner.SpannerException) ExecutionException(java.util.concurrent.ExecutionException)

Aggregations

GrpcCallContext (com.google.api.gax.grpc.GrpcCallContext)1 AlreadyExistsException (com.google.api.gax.rpc.AlreadyExistsException)1 AdminRequestsPerMinuteExceededException (com.google.cloud.spanner.AdminRequestsPerMinuteExceededException)1 SpannerException (com.google.cloud.spanner.SpannerException)1 SpannerExceptionFactory.newSpannerException (com.google.cloud.spanner.SpannerExceptionFactory.newSpannerException)1 Empty (com.google.protobuf.Empty)1 UpdateDatabaseDdlMetadata (com.google.spanner.admin.database.v1.UpdateDatabaseDdlMetadata)1 UpdateDatabaseDdlRequest (com.google.spanner.admin.database.v1.UpdateDatabaseDdlRequest)1 ExecutionException (java.util.concurrent.ExecutionException)1