Search in sources :

Example 96 with PreparedStatement

use of com.datastax.driver.core.PreparedStatement in project cassandra by apache.

the class ReprepareTestBase method testReprepare.

public void testReprepare(BiConsumer<ClassLoader, Integer> instanceInitializer, ReprepareTestConfiguration... configs) throws Throwable {
    try (ICluster<IInvokableInstance> c = init(builder().withNodes(2).withConfig(config -> config.with(GOSSIP, NETWORK, NATIVE_PROTOCOL)).withInstanceInitializer(instanceInitializer).start())) {
        ForceHostLoadBalancingPolicy lbp = new ForceHostLoadBalancingPolicy();
        c.schemaChange(withKeyspace("CREATE TABLE %s.tbl (pk int, ck int, v int, PRIMARY KEY (pk, ck));"));
        for (ReprepareTestConfiguration config : configs) {
            // 1 has old behaviour
            for (int firstContact : new int[] { 1, 2 }) {
                try (com.datastax.driver.core.Cluster cluster = com.datastax.driver.core.Cluster.builder().addContactPoint("127.0.0.1").addContactPoint("127.0.0.2").withLoadBalancingPolicy(lbp).build();
                    Session session = cluster.connect()) {
                    lbp.setPrimary(firstContact);
                    final PreparedStatement select = session.prepare(withKeyspace("SELECT * FROM %s.tbl"));
                    session.execute(select.bind());
                    c.stream().forEach((i) -> i.runOnInstance(QueryProcessor::clearPreparedStatementsCache));
                    lbp.setPrimary(firstContact == 1 ? 2 : 1);
                    if (config.withUse)
                        session.execute(withKeyspace("USE %s"));
                    // Re-preparing on the node
                    if (!config.skipBrokenBehaviours && firstContact == 1)
                        session.execute(select.bind());
                    c.stream().forEach((i) -> i.runOnInstance(QueryProcessor::clearPreparedStatementsCache));
                    lbp.setPrimary(firstContact);
                    // Re-preparing on the node with old behaviour will break no matter where the statement was initially prepared
                    if (!config.skipBrokenBehaviours)
                        session.execute(select.bind());
                    c.stream().forEach((i) -> i.runOnInstance(QueryProcessor::clearPreparedStatementsCache));
                }
            }
        }
    }
}
Also used : LoadBalancingPolicy(com.datastax.driver.core.policies.LoadBalancingPolicy) MethodDelegation(net.bytebuddy.implementation.MethodDelegation) ByteBuddy(net.bytebuddy.ByteBuddy) ElementMatchers.takesArguments(net.bytebuddy.matcher.ElementMatchers.takesArguments) QueryProcessor(org.apache.cassandra.cql3.QueryProcessor) QueryHandler(org.apache.cassandra.cql3.QueryHandler) Iterators(com.google.common.collect.Iterators) NATIVE_PROTOCOL(org.apache.cassandra.distributed.api.Feature.NATIVE_PROTOCOL) PreparedStatement(com.datastax.driver.core.PreparedStatement) FixedValue(net.bytebuddy.implementation.FixedValue) Session(com.datastax.driver.core.Session) BiConsumer(java.util.function.BiConsumer) AssertUtils.fail(org.apache.cassandra.distributed.shared.AssertUtils.fail) InvalidRequestException(org.apache.cassandra.exceptions.InvalidRequestException) NETWORK(org.apache.cassandra.distributed.api.Feature.NETWORK) ResultMessage(org.apache.cassandra.transport.messages.ResultMessage) FBUtilities(org.apache.cassandra.utils.FBUtilities) Iterator(java.util.Iterator) ElementMatchers.named(net.bytebuddy.matcher.ElementMatchers.named) Collection(java.util.Collection) ClientState(org.apache.cassandra.service.ClientState) ICluster(org.apache.cassandra.distributed.api.ICluster) ClassLoadingStrategy(net.bytebuddy.dynamic.loading.ClassLoadingStrategy) DriverInternalError(com.datastax.driver.core.exceptions.DriverInternalError) List(java.util.List) IInvokableInstance(org.apache.cassandra.distributed.api.IInvokableInstance) Cluster(com.datastax.driver.core.Cluster) Host(com.datastax.driver.core.Host) HostDistance(com.datastax.driver.core.HostDistance) Comparator(java.util.Comparator) Assert(org.junit.Assert) Collections(java.util.Collections) Statement(com.datastax.driver.core.Statement) CopyOnWriteArrayList(java.util.concurrent.CopyOnWriteArrayList) GOSSIP(org.apache.cassandra.distributed.api.Feature.GOSSIP) IInvokableInstance(org.apache.cassandra.distributed.api.IInvokableInstance) Cluster(com.datastax.driver.core.Cluster) PreparedStatement(com.datastax.driver.core.PreparedStatement) Session(com.datastax.driver.core.Session)

Example 97 with PreparedStatement

use of com.datastax.driver.core.PreparedStatement in project cassandra by apache.

the class BinAuditLoggerTest method testSelectRoundTripQuery.

@Test
public void testSelectRoundTripQuery() throws Throwable {
    createTable("CREATE TABLE %s (id int primary key, v1 text, v2 text)");
    execute("INSERT INTO %s (id, v1, v2) VALUES (?, ?, ?)", 1, "Apache", "Cassandra");
    execute("INSERT INTO %s (id, v1, v2) VALUES (?, ?, ?)", 2, "trace", "test");
    String cql = "SELECT id, v1, v2 FROM " + KEYSPACE + '.' + currentTable() + " WHERE id = ?";
    Session session = sessionNet();
    PreparedStatement pstmt = session.prepare(cql);
    ResultSet rs = session.execute(pstmt.bind(1));
    assertEquals(1, rs.all().size());
    try (ChronicleQueue queue = SingleChronicleQueueBuilder.single(tempDir.toFile()).rollCycle(RollCycles.TEST_SECONDLY).build()) {
        ExcerptTailer tailer = queue.createTailer();
        assertTrue(tailer.readDocument(wire -> {
            assertEquals(0L, wire.read("version").int16());
            assertEquals("audit", wire.read("type").text());
            assertThat(wire.read("message").text(), containsString(AuditLogEntryType.PREPARE_STATEMENT.toString()));
        }));
        assertTrue(tailer.readDocument(wire -> {
            assertEquals(0L, wire.read("version").int16());
            assertEquals("audit", wire.read("type").text());
            assertThat(wire.read("message").text(), containsString(AuditLogEntryType.SELECT.toString()));
        }));
        assertFalse(tailer.readDocument(wire -> {
        }));
    }
}
Also used : ExcerptTailer(net.openhft.chronicle.queue.ExcerptTailer) BeforeClass(org.junit.BeforeClass) CQLTester(org.apache.cassandra.cql3.CQLTester) ChronicleQueue(net.openhft.chronicle.queue.ChronicleQueue) Assert.assertTrue(org.junit.Assert.assertTrue) Test(org.junit.Test) PreparedStatement(com.datastax.driver.core.PreparedStatement) Assert.assertThat(org.junit.Assert.assertThat) SingleChronicleQueueBuilder(net.openhft.chronicle.queue.impl.single.SingleChronicleQueueBuilder) StringContains.containsString(org.hamcrest.core.StringContains.containsString) ResultSet(com.datastax.driver.core.ResultSet) ParameterizedClass(org.apache.cassandra.config.ParameterizedClass) Assert.assertFalse(org.junit.Assert.assertFalse) Session(com.datastax.driver.core.Session) RollCycles(net.openhft.chronicle.queue.RollCycles) BinLogTest(org.apache.cassandra.utils.binlog.BinLogTest) Path(java.nio.file.Path) DatabaseDescriptor(org.apache.cassandra.config.DatabaseDescriptor) Assert.assertEquals(org.junit.Assert.assertEquals) ChronicleQueue(net.openhft.chronicle.queue.ChronicleQueue) ResultSet(com.datastax.driver.core.ResultSet) PreparedStatement(com.datastax.driver.core.PreparedStatement) StringContains.containsString(org.hamcrest.core.StringContains.containsString) ExcerptTailer(net.openhft.chronicle.queue.ExcerptTailer) Session(com.datastax.driver.core.Session) Test(org.junit.Test) BinLogTest(org.apache.cassandra.utils.binlog.BinLogTest)

Example 98 with PreparedStatement

use of com.datastax.driver.core.PreparedStatement in project cassandra by apache.

the class AuditLoggerTest method testCqlPrepareQueryError.

@Test
public void testCqlPrepareQueryError() {
    createTable("CREATE TABLE %s (id int primary key, v1 text, v2 text)");
    String cql = "INSERT INTO " + KEYSPACE + '.' + currentTable() + " (id, v1, v2) VALUES (?,?,?)";
    try {
        Session session = sessionNet();
        PreparedStatement pstmt = session.prepare(cql);
        AuditLogEntry logEntry = ((InMemoryAuditLogger) AuditLogManager.instance.getLogger()).inMemQueue.poll();
        assertLogEntry(cql, AuditLogEntryType.PREPARE_STATEMENT, logEntry, false);
        dropTable("DROP TABLE %s");
        ResultSet rs = session.execute(pstmt.bind(1, "insert_audit", "test"));
        Assert.fail("should not succeed");
    } catch (NoHostAvailableException e) {
    // nop
    }
    AuditLogEntry logEntry = ((InMemoryAuditLogger) AuditLogManager.instance.getLogger()).inMemQueue.poll();
    assertLogEntry(logEntry, null);
    logEntry = ((InMemoryAuditLogger) AuditLogManager.instance.getLogger()).inMemQueue.poll();
    assertLogEntry(logEntry, cql);
    assertEquals(0, ((InMemoryAuditLogger) AuditLogManager.instance.getLogger()).inMemQueue.size());
}
Also used : NoHostAvailableException(com.datastax.driver.core.exceptions.NoHostAvailableException) ResultSet(com.datastax.driver.core.ResultSet) PreparedStatement(com.datastax.driver.core.PreparedStatement) CoreMatchers.containsString(org.hamcrest.CoreMatchers.containsString) Session(com.datastax.driver.core.Session) Test(org.junit.Test)

Example 99 with PreparedStatement

use of com.datastax.driver.core.PreparedStatement in project cassandra by apache.

the class AuditLoggerTest method executeAndAssertWithPrepare.

private ResultSet executeAndAssertWithPrepare(String cql, AuditLogEntryType executeType, boolean isTableNull, Object... bindValues) throws Throwable {
    Session session = sessionNet();
    PreparedStatement pstmt = session.prepare(cql);
    ResultSet rs = session.execute(pstmt.bind(bindValues));
    AuditLogEntry logEntry1 = ((InMemoryAuditLogger) AuditLogManager.instance.getLogger()).inMemQueue.poll();
    assertLogEntry(cql, AuditLogEntryType.PREPARE_STATEMENT, logEntry1, isTableNull);
    AuditLogEntry logEntry2 = ((InMemoryAuditLogger) AuditLogManager.instance.getLogger()).inMemQueue.poll();
    assertLogEntry(cql, executeType, logEntry2, isTableNull);
    assertEquals(0, ((InMemoryAuditLogger) AuditLogManager.instance.getLogger()).inMemQueue.size());
    return rs;
}
Also used : ResultSet(com.datastax.driver.core.ResultSet) PreparedStatement(com.datastax.driver.core.PreparedStatement) Session(com.datastax.driver.core.Session)

Example 100 with PreparedStatement

use of com.datastax.driver.core.PreparedStatement in project cassandra by apache.

the class AuditLoggerTest method testCqlBatch_MultipleTablesAuditing.

@Test
public void testCqlBatch_MultipleTablesAuditing() {
    createTable("CREATE TABLE %s (id int primary key, v1 text, v2 text)");
    String table1 = currentTable();
    Session session = sessionNet();
    BatchStatement batchStatement = new BatchStatement();
    String cqlInsert1 = "INSERT INTO " + KEYSPACE + "." + table1 + " (id, v1, v2) VALUES (?, ?, ?)";
    PreparedStatement prep = session.prepare(cqlInsert1);
    AuditLogEntry logEntry = ((InMemoryAuditLogger) AuditLogManager.instance.getLogger()).inMemQueue.poll();
    assertLogEntry(cqlInsert1, AuditLogEntryType.PREPARE_STATEMENT, logEntry, false);
    batchStatement.add(prep.bind(1, "Apapche", "Cassandra"));
    createTable("CREATE TABLE %s (id int primary key, v1 text, v2 text)");
    String table2 = currentTable();
    String cqlInsert2 = "INSERT INTO " + KEYSPACE + "." + table2 + " (id, v1, v2) VALUES (?, ?, ?)";
    prep = session.prepare(cqlInsert2);
    logEntry = ((InMemoryAuditLogger) AuditLogManager.instance.getLogger()).inMemQueue.poll();
    assertLogEntry(cqlInsert2, AuditLogEntryType.PREPARE_STATEMENT, logEntry, false);
    batchStatement.add(prep.bind(1, "Apapche", "Cassandra"));
    createKeyspace("CREATE KEYSPACE %s WITH replication={ 'class' : 'SimpleStrategy', 'replication_factor' : 1 }");
    String ks2 = currentKeyspace();
    createTable(ks2, "CREATE TABLE %s (id int primary key, v1 text, v2 text)");
    String table3 = currentTable();
    String cqlInsert3 = "INSERT INTO " + ks2 + "." + table3 + " (id, v1, v2) VALUES (?, ?, ?)";
    prep = session.prepare(cqlInsert3);
    logEntry = ((InMemoryAuditLogger) AuditLogManager.instance.getLogger()).inMemQueue.poll();
    assertLogEntry(cqlInsert3, AuditLogEntryType.PREPARE_STATEMENT, logEntry, false, ks2);
    batchStatement.add(prep.bind(1, "Apapche", "Cassandra"));
    ResultSet rs = session.execute(batchStatement);
    assertEquals(4, ((InMemoryAuditLogger) AuditLogManager.instance.getLogger()).inMemQueue.size());
    logEntry = ((InMemoryAuditLogger) AuditLogManager.instance.getLogger()).inMemQueue.poll();
    logEntry = ((InMemoryAuditLogger) AuditLogManager.instance.getLogger()).inMemQueue.poll();
    assertLogEntry(cqlInsert1, table1, AuditLogEntryType.UPDATE, logEntry, false, KEYSPACE);
    logEntry = ((InMemoryAuditLogger) AuditLogManager.instance.getLogger()).inMemQueue.poll();
    assertLogEntry(cqlInsert2, table2, AuditLogEntryType.UPDATE, logEntry, false, KEYSPACE);
    logEntry = ((InMemoryAuditLogger) AuditLogManager.instance.getLogger()).inMemQueue.poll();
    assertLogEntry(cqlInsert3, table3, AuditLogEntryType.UPDATE, logEntry, false, ks2);
    int size = rs.all().size();
    assertEquals(0, size);
}
Also used : BatchStatement(com.datastax.driver.core.BatchStatement) ResultSet(com.datastax.driver.core.ResultSet) PreparedStatement(com.datastax.driver.core.PreparedStatement) CoreMatchers.containsString(org.hamcrest.CoreMatchers.containsString) Session(com.datastax.driver.core.Session) Test(org.junit.Test)

Aggregations

PreparedStatement (com.datastax.driver.core.PreparedStatement)113 ResultSet (com.datastax.driver.core.ResultSet)60 BoundStatement (com.datastax.driver.core.BoundStatement)59 Session (com.datastax.driver.core.Session)39 Test (org.junit.Test)30 Row (com.datastax.driver.core.Row)27 InvalidQueryException (com.datastax.driver.core.exceptions.InvalidQueryException)27 XMLStreamException (javolution.xml.stream.XMLStreamException)25 PersistenceException (org.mobicents.smsc.cassandra.PersistenceException)15 Cluster (com.datastax.driver.core.Cluster)9 Date (java.util.Date)9 IInvokableInstance (org.apache.cassandra.distributed.api.IInvokableInstance)8 ArrayList (java.util.ArrayList)7 List (java.util.List)7 Map (java.util.Map)7 QueryProcessor (org.apache.cassandra.cql3.QueryProcessor)7 GOSSIP (org.apache.cassandra.distributed.api.Feature.GOSSIP)7 NATIVE_PROTOCOL (org.apache.cassandra.distributed.api.Feature.NATIVE_PROTOCOL)7 NETWORK (org.apache.cassandra.distributed.api.Feature.NETWORK)7 ICluster (org.apache.cassandra.distributed.api.ICluster)7