Search in sources :

Example 1 with Session

use of jdk.incubator.sql2.Session in project java-spanner by googleapis.

the class MockSpannerServiceImpl method commit.

@Override
public void commit(CommitRequest request, StreamObserver<CommitResponse> responseObserver) {
    requests.add(request);
    Preconditions.checkNotNull(request.getSession());
    Session session = sessions.get(request.getSession());
    if (session == null) {
        setSessionNotFound(request.getSession(), responseObserver);
        return;
    }
    sessionLastUsed.put(session.getName(), Instant.now());
    try {
        commitExecutionTime.simulateExecutionTime(exceptions, stickyGlobalExceptions, freezeLock);
        // Find or start a transaction
        Transaction transaction;
        if (request.hasSingleUseTransaction()) {
            // Start a temporary transaction
            transaction = beginTransaction(session, TransactionOptions.newBuilder().setReadWrite(ReadWrite.getDefaultInstance()).build());
        } else if (request.getTransactionId() != null) {
            transaction = transactions.get(request.getTransactionId());
            Optional<Boolean> aborted = Optional.fromNullable(abortedTransactions.get(request.getTransactionId()));
            if (aborted.or(Boolean.FALSE)) {
                throwTransactionAborted(request.getTransactionId());
            }
        } else {
            // No transaction mode specified
            responseObserver.onError(Status.INVALID_ARGUMENT.withDescription("No transaction mode specified").asRuntimeException());
            return;
        }
        if (transaction == null) {
            setTransactionNotFound(request.getTransactionId(), responseObserver);
            return;
        }
        simulateAbort(session, request.getTransactionId());
        commitTransaction(transaction.getId());
        CommitResponse.Builder responseBuilder = CommitResponse.newBuilder().setCommitTimestamp(getCurrentGoogleTimestamp());
        if (request.getReturnCommitStats()) {
            responseBuilder.setCommitStats(com.google.spanner.v1.CommitResponse.CommitStats.newBuilder().setMutationCount(request.getMutationsCount()).build());
        }
        responseObserver.onNext(responseBuilder.build());
        responseObserver.onCompleted();
    } catch (StatusRuntimeException t) {
        responseObserver.onError(t);
    } catch (Throwable t) {
        responseObserver.onError(Status.INTERNAL.asRuntimeException());
    }
}
Also used : Transaction(com.google.spanner.v1.Transaction) Optional(com.google.common.base.Optional) StatusRuntimeException(io.grpc.StatusRuntimeException) CommitResponse(com.google.spanner.v1.CommitResponse) Session(com.google.spanner.v1.Session)

Example 2 with Session

use of jdk.incubator.sql2.Session in project java-spanner by googleapis.

the class MockSpannerServiceImpl method partition.

private void partition(String sessionName, TransactionSelector transactionSelector, StreamObserver<PartitionResponse> responseObserver) {
    Session session = sessions.get(sessionName);
    if (session == null) {
        setSessionNotFound(sessionName, responseObserver);
        return;
    }
    sessionLastUsed.put(session.getName(), Instant.now());
    try {
        ByteString transactionId = getTransactionId(session, transactionSelector);
        responseObserver.onNext(PartitionResponse.newBuilder().addPartitions(Partition.newBuilder().setPartitionToken(generatePartitionToken(session.getName(), transactionId)).build()).build());
        responseObserver.onCompleted();
    } catch (StatusRuntimeException e) {
        responseObserver.onError(e);
    } catch (Throwable t) {
        responseObserver.onError(Status.INTERNAL.asRuntimeException());
    }
}
Also used : ByteString(com.google.protobuf.ByteString) StatusRuntimeException(io.grpc.StatusRuntimeException) Session(com.google.spanner.v1.Session)

Example 3 with Session

use of jdk.incubator.sql2.Session in project java-spanner by googleapis.

the class MockSpannerServiceImpl method rollback.

@Override
public void rollback(RollbackRequest request, StreamObserver<Empty> responseObserver) {
    requests.add(request);
    Preconditions.checkNotNull(request.getTransactionId());
    Session session = sessions.get(request.getSession());
    if (session == null) {
        setSessionNotFound(request.getSession(), responseObserver);
        return;
    }
    sessionLastUsed.put(session.getName(), Instant.now());
    try {
        rollbackExecutionTime.simulateExecutionTime(exceptions, stickyGlobalExceptions, freezeLock);
        Transaction transaction = transactions.get(request.getTransactionId());
        if (transaction != null) {
            rollbackTransaction(transaction.getId());
        }
        responseObserver.onNext(Empty.getDefaultInstance());
        responseObserver.onCompleted();
    } catch (StatusRuntimeException t) {
        responseObserver.onError(t);
    } catch (Throwable t) {
        responseObserver.onError(Status.INTERNAL.asRuntimeException());
    }
}
Also used : Transaction(com.google.spanner.v1.Transaction) StatusRuntimeException(io.grpc.StatusRuntimeException) Session(com.google.spanner.v1.Session)

Example 4 with Session

use of jdk.incubator.sql2.Session in project java-spanner by googleapis.

the class MockSpannerServiceImpl method beginTransaction.

@Override
public void beginTransaction(BeginTransactionRequest request, StreamObserver<Transaction> responseObserver) {
    requests.add(request);
    Preconditions.checkNotNull(request.getSession());
    Session session = sessions.get(request.getSession());
    if (session == null) {
        setSessionNotFound(request.getSession(), responseObserver);
        return;
    }
    sessionLastUsed.put(session.getName(), Instant.now());
    try {
        beginTransactionExecutionTime.simulateExecutionTime(exceptions, stickyGlobalExceptions, freezeLock);
        Transaction transaction = beginTransaction(session, request.getOptions());
        responseObserver.onNext(transaction);
        responseObserver.onCompleted();
    } catch (StatusRuntimeException t) {
        responseObserver.onError(t);
    } catch (Throwable t) {
        responseObserver.onError(Status.INTERNAL.asRuntimeException());
    }
}
Also used : Transaction(com.google.spanner.v1.Transaction) StatusRuntimeException(io.grpc.StatusRuntimeException) Session(com.google.spanner.v1.Session)

Example 5 with Session

use of jdk.incubator.sql2.Session in project java-spanner by googleapis.

the class MockSpannerServiceImpl method getSession.

@Override
public void getSession(GetSessionRequest request, StreamObserver<Session> responseObserver) {
    requests.add(request);
    Preconditions.checkNotNull(request.getName());
    try {
        getSessionExecutionTime.simulateExecutionTime(exceptions, stickyGlobalExceptions, freezeLock);
        Session session = sessions.get(request.getName());
        if (session == null) {
            setSessionNotFound(request.getName(), responseObserver);
        } else {
            session = session.toBuilder().setApproximateLastUseTime(getCurrentGoogleTimestamp()).build();
            responseObserver.onNext(session);
            responseObserver.onCompleted();
        }
    } catch (StatusRuntimeException e) {
        responseObserver.onError(e);
    } catch (Throwable t) {
        responseObserver.onError(Status.INTERNAL.asRuntimeException());
    }
}
Also used : StatusRuntimeException(io.grpc.StatusRuntimeException) Session(com.google.spanner.v1.Session)

Aggregations

Test (org.junit.Test)151 Session (org.sagebionetworks.bridge.models.schedules2.Session)127 Test (org.testng.annotations.Test)118 Session (jdk.incubator.sql2.Session)100 DataSource (jdk.incubator.sql2.DataSource)90 SessionTest (org.sagebionetworks.bridge.models.schedules2.SessionTest)81 Session (org.neo4j.driver.v1.Session)80 SessionTest.createValidSession (org.sagebionetworks.bridge.models.schedules2.SessionTest.createValidSession)74 ArrayList (java.util.ArrayList)58 Session (com.google.spanner.v1.Session)56 Driver (org.neo4j.driver.v1.Driver)54 IOException (java.io.IOException)48 Session (com.trilead.ssh2.Session)47 AfterClass (org.junit.AfterClass)47 BeforeClass (org.junit.BeforeClass)47 TimeUnit (java.util.concurrent.TimeUnit)46 Schedule2 (org.sagebionetworks.bridge.models.schedules2.Schedule2)41 StatementResult (org.neo4j.driver.v1.StatementResult)39 InputStream (java.io.InputStream)36 Collector (java.util.stream.Collector)36