Search in sources :

Example 6 with DatabaseName

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

the class SpannerClientTest method createSessionTest.

@Test
public void createSessionTest() throws Exception {
    Session expectedResponse = Session.newBuilder().setName(SessionName.of("[PROJECT]", "[INSTANCE]", "[DATABASE]", "[SESSION]").toString()).putAllLabels(new HashMap<String, String>()).setCreateTime(Timestamp.newBuilder().build()).setApproximateLastUseTime(Timestamp.newBuilder().build()).build();
    mockSpanner.addResponse(expectedResponse);
    DatabaseName database = DatabaseName.of("[PROJECT]", "[INSTANCE]", "[DATABASE]");
    Session actualResponse = client.createSession(database);
    Assert.assertEquals(expectedResponse, actualResponse);
    List<AbstractMessage> actualRequests = mockSpanner.getRequests();
    Assert.assertEquals(1, actualRequests.size());
    CreateSessionRequest actualRequest = ((CreateSessionRequest) actualRequests.get(0));
    Assert.assertEquals(database.toString(), actualRequest.getDatabase());
    Assert.assertTrue(channelProvider.isHeaderSent(ApiClientHeaderProvider.getDefaultApiClientHeaderKey(), GaxGrpcProperties.getDefaultApiClientHeaderPattern()));
}
Also used : CreateSessionRequest(com.google.spanner.v1.CreateSessionRequest) AbstractMessage(com.google.protobuf.AbstractMessage) HashMap(java.util.HashMap) DatabaseName(com.google.spanner.v1.DatabaseName) Session(com.google.spanner.v1.Session) Test(org.junit.Test)

Example 7 with DatabaseName

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

the class SpannerClientTest method listSessionsExceptionTest.

@Test
public void listSessionsExceptionTest() throws Exception {
    StatusRuntimeException exception = new StatusRuntimeException(io.grpc.Status.INVALID_ARGUMENT);
    mockSpanner.addException(exception);
    try {
        DatabaseName database = DatabaseName.of("[PROJECT]", "[INSTANCE]", "[DATABASE]");
        client.listSessions(database);
        Assert.fail("No exception raised");
    } catch (InvalidArgumentException e) {
    // Expected exception.
    }
}
Also used : InvalidArgumentException(com.google.api.gax.rpc.InvalidArgumentException) StatusRuntimeException(io.grpc.StatusRuntimeException) DatabaseName(com.google.spanner.v1.DatabaseName) Test(org.junit.Test)

Example 8 with DatabaseName

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

the class SpannerClientTest method listSessionsTest.

@Test
public void listSessionsTest() throws Exception {
    Session responsesElement = Session.newBuilder().build();
    ListSessionsResponse expectedResponse = ListSessionsResponse.newBuilder().setNextPageToken("").addAllSessions(Arrays.asList(responsesElement)).build();
    mockSpanner.addResponse(expectedResponse);
    DatabaseName database = DatabaseName.of("[PROJECT]", "[INSTANCE]", "[DATABASE]");
    ListSessionsPagedResponse pagedListResponse = client.listSessions(database);
    List<Session> resources = Lists.newArrayList(pagedListResponse.iterateAll());
    Assert.assertEquals(1, resources.size());
    Assert.assertEquals(expectedResponse.getSessionsList().get(0), resources.get(0));
    List<AbstractMessage> actualRequests = mockSpanner.getRequests();
    Assert.assertEquals(1, actualRequests.size());
    ListSessionsRequest actualRequest = ((ListSessionsRequest) actualRequests.get(0));
    Assert.assertEquals(database.toString(), actualRequest.getDatabase());
    Assert.assertTrue(channelProvider.isHeaderSent(ApiClientHeaderProvider.getDefaultApiClientHeaderKey(), GaxGrpcProperties.getDefaultApiClientHeaderPattern()));
}
Also used : ListSessionsResponse(com.google.spanner.v1.ListSessionsResponse) AbstractMessage(com.google.protobuf.AbstractMessage) ListSessionsPagedResponse(com.google.cloud.spanner.v1.SpannerClient.ListSessionsPagedResponse) DatabaseName(com.google.spanner.v1.DatabaseName) ListSessionsRequest(com.google.spanner.v1.ListSessionsRequest) Session(com.google.spanner.v1.Session) Test(org.junit.Test)

Example 9 with DatabaseName

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

Example 10 with DatabaseName

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

the class DatabaseAdminClient method dropDatabase.

// AUTO-GENERATED DOCUMENTATION AND METHOD.
/**
 * Drops (aka deletes) a Cloud Spanner database. Completed backups for the database will be
 * retained according to their `expire_time`. Note: Cloud Spanner might continue to accept
 * requests for a few seconds after the database has been deleted.
 *
 * <p>Sample code:
 *
 * <pre>{@code
 * try (DatabaseAdminClient databaseAdminClient = DatabaseAdminClient.create()) {
 *   DatabaseName database = DatabaseName.of("[PROJECT]", "[INSTANCE]", "[DATABASE]");
 *   databaseAdminClient.dropDatabase(database);
 * }
 * }</pre>
 *
 * @param database Required. The database to be dropped.
 * @throws com.google.api.gax.rpc.ApiException if the remote call fails
 */
public final void dropDatabase(DatabaseName database) {
    DropDatabaseRequest request = DropDatabaseRequest.newBuilder().setDatabase(database == null ? null : database.toString()).build();
    dropDatabase(request);
}
Also used : DropDatabaseRequest(com.google.spanner.admin.database.v1.DropDatabaseRequest)

Aggregations

Test (org.junit.Test)19 AbstractMessage (com.google.protobuf.AbstractMessage)10 DatabaseName (com.google.spanner.admin.database.v1.DatabaseName)10 StatusRuntimeException (io.grpc.StatusRuntimeException)10 InvalidArgumentException (com.google.api.gax.rpc.InvalidArgumentException)9 DatabaseName (com.google.spanner.v1.DatabaseName)7 ArrayList (java.util.ArrayList)6 Empty (com.google.protobuf.Empty)5 ExecutionException (java.util.concurrent.ExecutionException)5 GrpcCallContext (com.google.api.gax.grpc.GrpcCallContext)4 DatabaseName (com.google.firestore.admin.v1.DatabaseName)4 Operation (com.google.longrunning.Operation)3 DropDatabaseRequest (com.google.spanner.admin.database.v1.DropDatabaseRequest)3 Session (com.google.spanner.v1.Session)3 ByteString (com.google.protobuf.ByteString)2 Database (com.google.spanner.admin.database.v1.Database)2 GetDatabaseDdlRequest (com.google.spanner.admin.database.v1.GetDatabaseDdlRequest)2 GetDatabaseRequest (com.google.spanner.admin.database.v1.GetDatabaseRequest)2 UpdateDatabaseDdlRequest (com.google.spanner.admin.database.v1.UpdateDatabaseDdlRequest)2 ListSessionsRequest (com.google.spanner.v1.ListSessionsRequest)2