Search in sources :

Example 56 with CommitRequest

use of com.swiftmq.impl.routing.single.smqpr.v942.CommitRequest in project pgadapter by GoogleCloudPlatform.

the class JdbcSimpleModeMockServerTest method testTransactionStatementsInBatch.

@Test
public void testTransactionStatementsInBatch() throws SQLException {
    String sql = "BEGIN TRANSACTION; INSERT INTO FOO VALUES (1); UPDATE FOO SET BAR=1 WHERE BAZ=2; COMMIT;";
    try (Connection connection = DriverManager.getConnection(createUrl())) {
        try (Statement statement = connection.createStatement()) {
            // Send a complete transaction as a single batch.
            assertFalse(statement.execute(sql));
            // The BEGIN statement should not return anything.
            assertEquals(0, statement.getUpdateCount());
            // INSERT
            assertFalse(statement.getMoreResults());
            assertEquals(1, statement.getUpdateCount());
            // UPDATE
            assertFalse(statement.getMoreResults());
            assertEquals(2, statement.getUpdateCount());
            // COMMIT
            assertFalse(statement.getMoreResults());
            assertEquals(0, statement.getUpdateCount());
            assertFalse(statement.getMoreResults());
            assertEquals(-1, statement.getUpdateCount());
        }
    }
    // As the two DML statements are adjacent in the batch, they can be combined into a BatchDML
    // request. This is the first statement in the transaction and will include the BeginTransaction
    // option.
    List<ExecuteBatchDmlRequest> batchDmlRequests = mockSpanner.getRequestsOfType(ExecuteBatchDmlRequest.class);
    assertEquals(1, batchDmlRequests.size());
    ExecuteBatchDmlRequest batchDmlRequest = batchDmlRequests.get(0);
    assertTrue(batchDmlRequest.getTransaction().hasBegin());
    assertEquals(INSERT_STATEMENT.getSql(), batchDmlRequest.getStatements(0).getSql());
    assertEquals(UPDATE_STATEMENT.getSql(), batchDmlRequest.getStatements(1).getSql());
    List<CommitRequest> commitRequests = mockSpanner.getRequestsOfType(CommitRequest.class);
    assertEquals(1, commitRequests.size());
}
Also used : CommitRequest(com.google.spanner.v1.CommitRequest) PreparedStatement(java.sql.PreparedStatement) Statement(java.sql.Statement) Connection(java.sql.Connection) CoreMatchers.containsString(org.hamcrest.CoreMatchers.containsString) ExecuteBatchDmlRequest(com.google.spanner.v1.ExecuteBatchDmlRequest) Test(org.junit.Test)

Example 57 with CommitRequest

use of com.swiftmq.impl.routing.single.smqpr.v942.CommitRequest in project pgadapter by GoogleCloudPlatform.

the class JdbcSimpleModeMockServerTest method testErrorHandlingOfDmlBatchAfterCommit.

@Test
public void testErrorHandlingOfDmlBatchAfterCommit() throws SQLException {
    String sql = String.format("%s; %s; %s; %s; %s;", INSERT_STATEMENT, "COMMIT", UPDATE_STATEMENT, INVALID_DML, SELECT1);
    try (Connection connection = DriverManager.getConnection(createUrl())) {
        try (java.sql.Statement statement = connection.createStatement()) {
            SQLException exception = assertThrows(SQLException.class, () -> statement.execute(sql));
            assertThat(exception.getMessage(), containsString("INVALID_ARGUMENT: Statement is invalid."));
        }
    }
    List<ExecuteSqlRequest> requests = mockSpanner.getRequestsOfType(ExecuteSqlRequest.class);
    assertEquals(1, requests.size());
    assertEquals(INSERT_STATEMENT.getSql(), requests.get(0).getSql());
    List<CommitRequest> commitRequests = mockSpanner.getRequestsOfType(CommitRequest.class);
    assertEquals(1, commitRequests.size());
    List<ExecuteBatchDmlRequest> batchDmlRequests = mockSpanner.getRequestsOfType(ExecuteBatchDmlRequest.class);
    assertEquals(1, batchDmlRequests.size());
    ExecuteBatchDmlRequest request = batchDmlRequests.get(0);
    assertEquals(2, request.getStatementsCount());
    assertEquals(UPDATE_STATEMENT.getSql(), request.getStatements(0).getSql());
    assertEquals(INVALID_DML.getSql(), request.getStatements(1).getSql());
}
Also used : CommitRequest(com.google.spanner.v1.CommitRequest) ExecuteSqlRequest(com.google.spanner.v1.ExecuteSqlRequest) SQLException(java.sql.SQLException) Connection(java.sql.Connection) Statement(java.sql.Statement) CoreMatchers.containsString(org.hamcrest.CoreMatchers.containsString) ExecuteBatchDmlRequest(com.google.spanner.v1.ExecuteBatchDmlRequest) Test(org.junit.Test)

Example 58 with CommitRequest

use of com.swiftmq.impl.routing.single.smqpr.v942.CommitRequest in project pgadapter by GoogleCloudPlatform.

the class CopyInMockServerTest method testCopyInError_BatchedNonAtomic.

@Test
public void testCopyInError_BatchedNonAtomic() throws SQLException {
    setupCopyInformationSchemaResults();
    mockSpanner.setCommitExecutionTime(SimulatedExecutionTime.ofException(Status.INVALID_ARGUMENT.asRuntimeException()));
    try (Connection connection = DriverManager.getConnection(createUrl())) {
        System.setProperty("copy_in_commit_limit", "10");
        connection.createStatement().execute("set spanner.autocommit_dml_mode='partitioned_non_atomic'");
        CopyManager copyManager = new CopyManager(connection.unwrap(BaseConnection.class));
        SQLException exception = assertThrows(SQLException.class, () -> copyManager.copyIn("COPY users FROM STDIN;", new StringReader("5\t5\t5\n6\t6\t6\n7\t7\t7\n")));
        assertTrue(exception.getMessage().contains("io.grpc.StatusRuntimeException: INVALID_ARGUMENT"));
    } finally {
        System.getProperties().remove("copy_in_commit_limit");
    }
    // The server should receive between 1 and 3 commit requests. We don't know exactly how many as
    // the commits can be executed in parallel.
    List<CommitRequest> commitRequests = mockSpanner.getRequestsOfType(CommitRequest.class);
    assertTrue("Number of commits should be between 1 and 3", commitRequests.size() >= 1 && commitRequests.size() <= 3);
}
Also used : CommitRequest(com.google.spanner.v1.CommitRequest) SQLException(java.sql.SQLException) Connection(java.sql.Connection) BaseConnection(org.postgresql.core.BaseConnection) StringReader(java.io.StringReader) CopyManager(org.postgresql.copy.CopyManager) BaseConnection(org.postgresql.core.BaseConnection) Test(org.junit.Test)

Example 59 with CommitRequest

use of com.swiftmq.impl.routing.single.smqpr.v942.CommitRequest in project pgadapter by GoogleCloudPlatform.

the class CopyInMockServerTest method testCopyIn_QueryDuringCopy.

@Test
public void testCopyIn_QueryDuringCopy() throws SQLException, NoSuchFieldException, IllegalAccessException, IOException {
    setupCopyInformationSchemaResults();
    byte[] payload = "5\t5\t5\n".getBytes(StandardCharsets.UTF_8);
    try (Connection connection = DriverManager.getConnection(createUrl())) {
        CopyManager copyManager = new CopyManager(connection.unwrap(BaseConnection.class));
        // Start a copy operation and then try to execute a query during the copy.
        CopyIn copyOperation = copyManager.copyIn("COPY users FROM STDIN;");
        copyOperation.writeToCopy(payload, 0, payload.length);
        copyOperation.flushCopy();
        // Use reflection to get hold of the underlying stream, so we can send an invalid message.
        java.lang.reflect.Field queryExecutorField = CopyOperationImpl.class.getDeclaredField("queryExecutor");
        queryExecutorField.setAccessible(true);
        QueryExecutorImpl queryExecutor = (QueryExecutorImpl) queryExecutorField.get(copyOperation);
        java.lang.reflect.Field pgStreamField = QueryExecutorBase.class.getDeclaredField("pgStream");
        pgStreamField.setAccessible(true);
        PGStream stream = (PGStream) pgStreamField.get(queryExecutor);
        stream.sendChar('Q');
        // Length = 4 + 8 + 1 = 13
        // (msg length 4 bytes, 8 bytes for SELECT 1, 1 byte for \0)
        stream.sendInteger4(13);
        stream.send("SELECT 1".getBytes(StandardCharsets.UTF_8));
        stream.sendChar(0);
        stream.flush();
        // PGAdapter drops the connection if an invalid message is received during a COPY. This is a
        // safety measure as there is no other error handling in the COPY protocol, and the server
        // could otherwise have been completely flushed with garbage if it continued to receive
        // messages after receiving an invalid message.
        SQLException exception = assertThrows(SQLException.class, copyOperation::endCopy);
        assertEquals("Database connection failed when ending copy", exception.getMessage());
    }
    List<CommitRequest> commitRequests = mockSpanner.getRequestsOfType(CommitRequest.class);
    assertTrue(commitRequests.isEmpty());
}
Also used : CommitRequest(com.google.spanner.v1.CommitRequest) PGStream(org.postgresql.core.PGStream) SQLException(java.sql.SQLException) Connection(java.sql.Connection) BaseConnection(org.postgresql.core.BaseConnection) CopyManager(org.postgresql.copy.CopyManager) QueryExecutorImpl(org.postgresql.core.v3.QueryExecutorImpl) BaseConnection(org.postgresql.core.BaseConnection) CopyIn(org.postgresql.copy.CopyIn) Test(org.junit.Test)

Example 60 with CommitRequest

use of com.swiftmq.impl.routing.single.smqpr.v942.CommitRequest in project pgadapter by GoogleCloudPlatform.

the class CopyInMockServerTest method testCopyInExceedsCommitSizeLimit_BatchesInNonAtomicMode.

@Test
public void testCopyInExceedsCommitSizeLimit_BatchesInNonAtomicMode() throws SQLException, IOException {
    setupCopyInformationSchemaResults();
    try (Connection connection = DriverManager.getConnection(createUrl())) {
        System.setProperty("copy_in_commit_limit", "10");
        connection.createStatement().execute("set spanner.autocommit_dml_mode='partitioned_non_atomic'");
        CopyManager copyManager = new CopyManager(connection.unwrap(BaseConnection.class));
        copyManager.copyIn("COPY users FROM STDIN;", new StringReader("5\t5\t5\n6\t6\t6\n7\t7\t7\n"));
    } finally {
        System.getProperties().remove("copy_in_commit_limit");
    }
    List<CommitRequest> commitRequests = mockSpanner.getRequestsOfType(CommitRequest.class);
    assertEquals(3, commitRequests.size());
    for (CommitRequest request : commitRequests) {
        assertEquals(1, request.getMutationsCount());
        Mutation mutation = request.getMutations(0);
        assertEquals(OperationCase.INSERT, mutation.getOperationCase());
        assertEquals(1, mutation.getInsert().getValuesCount());
    }
}
Also used : CommitRequest(com.google.spanner.v1.CommitRequest) Connection(java.sql.Connection) BaseConnection(org.postgresql.core.BaseConnection) StringReader(java.io.StringReader) CopyManager(org.postgresql.copy.CopyManager) Mutation(com.google.spanner.v1.Mutation) BaseConnection(org.postgresql.core.BaseConnection) Test(org.junit.Test)

Aggregations

Test (org.junit.Test)85 CommitRequest (com.google.spanner.v1.CommitRequest)47 CommitRequest (com.google.firestore.v1.CommitRequest)40 CommitResponse (com.google.firestore.v1.CommitResponse)40 ArrayList (java.util.ArrayList)26 Connection (java.sql.Connection)23 ByteString (com.google.protobuf.ByteString)14 ExecuteSqlRequest (com.google.spanner.v1.ExecuteSqlRequest)12 SQLException (java.sql.SQLException)12 Statement (java.sql.Statement)12 CopyManager (org.postgresql.copy.CopyManager)11 BaseConnection (org.postgresql.core.BaseConnection)11 ExecuteBatchDmlRequest (com.google.spanner.v1.ExecuteBatchDmlRequest)10 ArrayValue (com.google.firestore.v1.ArrayValue)9 MapValue (com.google.firestore.v1.MapValue)9 Value (com.google.firestore.v1.Value)9 Write (com.google.firestore.v1.Write)9 StringReader (java.io.StringReader)9 HashMap (java.util.HashMap)9 AbstractMessage (com.google.protobuf.AbstractMessage)8