Search in sources :

Example 11 with QueryState

use of org.apache.cassandra.service.QueryState in project cassandra by apache.

the class CompactionAllocationTest method testMediumPartitions.

private static void testMediumPartitions(String name, int numSSTable, int sstablePartitions, boolean overlap, boolean overlapCK) throws Throwable {
    String ksname = "ks_" + name.toLowerCase();
    SchemaLoader.createKeyspace(ksname, KeyspaceParams.simple(1), CreateTableStatement.parse("CREATE TABLE tbl (k text, c text, v1 text, v2 text, v3 text, v4 text, PRIMARY KEY (k, c))", ksname).build());
    ColumnFamilyStore cfs = Schema.instance.getColumnFamilyStoreInstance(Schema.instance.getTableMetadata(ksname, "tbl").id);
    Assert.assertNotNull(cfs);
    cfs.disableAutoCompaction();
    int rowsPerPartition = 200;
    List<Runnable> reads = new ArrayList<>(numSSTable * (overlap ? 1 : sstablePartitions));
    measure(new Workload() {

        public void setup() {
            cfs.disableAutoCompaction();
            String insert = String.format("INSERT INTO %s.%s (k, c, v1, v2, v3, v4) VALUES (?, ?, ?, ?, ?, ?)", ksname, "tbl");
            String read = String.format("SELECT * FROM %s.%s WHERE k = ?", ksname, "tbl");
            SelectStatement select = (SelectStatement) QueryProcessor.parseStatement(read).prepare(ClientState.forInternalCalls());
            QueryState queryState = QueryState.forInternalCalls();
            for (int f = 0; f < numSSTable; f++) {
                for (int p = 0; p < sstablePartitions; p++) {
                    String key = String.format("%08d", overlap ? p : (f * sstablePartitions) + p);
                    for (int r = 0; r < rowsPerPartition; r++) {
                        QueryProcessor.executeInternal(insert, key, makeRandomString(6, overlapCK ? r : -1), makeRandomString(8), makeRandomString(8), makeRandomString(8), makeRandomString(8));
                    }
                    if (!overlap || f == 0) {
                        QueryOptions options = QueryProcessor.makeInternalOptions(select, new Object[] { key });
                        ReadQuery query = select.getQuery(options, queryState.getNowInSeconds());
                        reads.add(() -> runQuery(query, cfs.metadata.get()));
                    }
                }
                cfs.forceBlockingFlush();
            }
            Assert.assertEquals(numSSTable, cfs.getLiveSSTables().size());
        }

        public ColumnFamilyStore getCfs() {
            return cfs;
        }

        public List<Runnable> getReads() {
            return reads;
        }

        public String name() {
            return name;
        }
    });
}
Also used : ArrayList(java.util.ArrayList) QueryState(org.apache.cassandra.service.QueryState) QueryOptions(org.apache.cassandra.cql3.QueryOptions) SelectStatement(org.apache.cassandra.cql3.statements.SelectStatement) ColumnFamilyStore(org.apache.cassandra.db.ColumnFamilyStore) List(java.util.List) ArrayList(java.util.ArrayList) ReadQuery(org.apache.cassandra.db.ReadQuery)

Example 12 with QueryState

use of org.apache.cassandra.service.QueryState in project cassandra by apache.

the class CompactionAllocationTest method testWidePartitions.

private static void testWidePartitions(String name, int numSSTable, int sstablePartitions, boolean overlap, boolean overlapCK) throws Throwable {
    String ksname = "ks_" + name.toLowerCase();
    SchemaLoader.createKeyspace(ksname, KeyspaceParams.simple(1), CreateTableStatement.parse("CREATE TABLE tbl (k text, c text, v1 text, v2 text, v3 text, v4 text, PRIMARY KEY (k, c))", ksname).build());
    ColumnFamilyStore cfs = Schema.instance.getColumnFamilyStoreInstance(Schema.instance.getTableMetadata(ksname, "tbl").id);
    Assert.assertNotNull(cfs);
    cfs.disableAutoCompaction();
    int rowWidth = 100;
    int rowsPerPartition = 1000;
    List<Runnable> reads = new ArrayList<>(numSSTable * (overlap ? 1 : sstablePartitions));
    measure(new Workload() {

        public void setup() {
            cfs.disableAutoCompaction();
            String insert = String.format("INSERT INTO %s.%s (k, c, v1, v2, v3, v4) VALUES (?, ?, ?, ?, ?, ?)", ksname, "tbl");
            String read = String.format("SELECT * FROM %s.%s WHERE k = ?", ksname, "tbl");
            SelectStatement select = (SelectStatement) QueryProcessor.parseStatement(read).prepare(ClientState.forInternalCalls());
            QueryState queryState = QueryState.forInternalCalls();
            for (int f = 0; f < numSSTable; f++) {
                for (int p = 0; p < sstablePartitions; p++) {
                    String key = String.format("%08d", overlap ? p : (f * sstablePartitions) + p);
                    for (int r = 0; r < rowsPerPartition; r++) {
                        QueryProcessor.executeInternal(insert, key, makeRandomString(6, overlapCK ? r : -1), makeRandomString(rowWidth >> 2), makeRandomString(rowWidth >> 2), makeRandomString(rowWidth >> 2), makeRandomString(rowWidth >> 2));
                    }
                    if (!overlap || f == 0) {
                        QueryOptions options = QueryProcessor.makeInternalOptions(select, new Object[] { key });
                        ReadQuery query = select.getQuery(options, queryState.getNowInSeconds());
                        reads.add(() -> runQuery(query, cfs.metadata.get()));
                    }
                }
                cfs.forceBlockingFlush();
            }
            Assert.assertEquals(numSSTable, cfs.getLiveSSTables().size());
        }

        public ColumnFamilyStore getCfs() {
            return cfs;
        }

        public List<Runnable> getReads() {
            return reads;
        }

        public String name() {
            return name;
        }
    });
}
Also used : ArrayList(java.util.ArrayList) QueryState(org.apache.cassandra.service.QueryState) QueryOptions(org.apache.cassandra.cql3.QueryOptions) SelectStatement(org.apache.cassandra.cql3.statements.SelectStatement) ColumnFamilyStore(org.apache.cassandra.db.ColumnFamilyStore) List(java.util.List) ArrayList(java.util.ArrayList) ReadQuery(org.apache.cassandra.db.ReadQuery)

Example 13 with QueryState

use of org.apache.cassandra.service.QueryState in project cassandra by apache.

the class CQLTester method schemaChange.

protected static ResultMessage schemaChange(String query) {
    try {
        ClientState state = ClientState.forInternalCalls(SchemaConstants.SYSTEM_KEYSPACE_NAME);
        QueryState queryState = new QueryState(state);
        CQLStatement statement = QueryProcessor.parseStatement(query, queryState.getClientState());
        statement.validate(state);
        QueryOptions options = QueryOptions.forInternalCalls(Collections.<ByteBuffer>emptyList());
        return statement.executeLocally(queryState, options);
    } catch (Exception e) {
        logger.info("Error performing schema change", e);
        throw new RuntimeException("Error setting schema for test (query was: " + query + ")", e);
    }
}
Also used : ClientState(org.apache.cassandra.service.ClientState) QueryState(org.apache.cassandra.service.QueryState) SyntaxException(org.apache.cassandra.exceptions.SyntaxException) ConfigurationException(org.apache.cassandra.exceptions.ConfigurationException) UnauthorizedException(com.datastax.driver.core.exceptions.UnauthorizedException) InvalidRequestException(org.apache.cassandra.exceptions.InvalidRequestException) MalformedURLException(java.net.MalformedURLException) IOException(java.io.IOException)

Example 14 with QueryState

use of org.apache.cassandra.service.QueryState in project cassandra by apache.

the class DriverBurnTest method test.

@Test
public void test() throws Throwable {
    final SizeCaps smallMessageCap = new SizeCaps(10, 20, 5, 10);
    final SizeCaps largeMessageCap = new SizeCaps(1000, 2000, 5, 150);
    int largeMessageFrequency = 1000;
    Message.Type.QUERY.unsafeSetCodec(new Message.Codec<QueryMessage>() {

        public QueryMessage decode(ByteBuf body, ProtocolVersion version) {
            QueryMessage queryMessage = QueryMessage.codec.decode(body, version);
            return new QueryMessage(queryMessage.query, queryMessage.options) {

                protected Message.Response execute(QueryState state, long queryStartNanoTime, boolean traceRequest) {
                    try {
                        int idx = Integer.parseInt(queryMessage.query);
                        SizeCaps caps = idx % largeMessageFrequency == 0 ? largeMessageCap : smallMessageCap;
                        return generateRows(idx, caps);
                    } catch (NumberFormatException e) {
                        // for the requests driver issues under the hood
                        return super.execute(state, queryStartNanoTime, traceRequest);
                    }
                }
            };
        }

        public void encode(QueryMessage queryMessage, ByteBuf dest, ProtocolVersion version) {
            QueryMessage.codec.encode(queryMessage, dest, version);
        }

        public int encodedSize(QueryMessage queryMessage, ProtocolVersion version) {
            return 0;
        }
    });
    List<AssertUtil.ThrowingSupplier<Cluster.Builder>> suppliers = Arrays.asList(() -> Cluster.builder().addContactPoint(nativeAddr.getHostAddress()).withProtocolVersion(com.datastax.driver.core.ProtocolVersion.V4).withPort(nativePort), () -> Cluster.builder().addContactPoint(nativeAddr.getHostAddress()).allowBetaProtocolVersion().withPort(nativePort), () -> Cluster.builder().addContactPoint(nativeAddr.getHostAddress()).withCompression(ProtocolOptions.Compression.LZ4).allowBetaProtocolVersion().withPort(nativePort), () -> Cluster.builder().addContactPoint(nativeAddr.getHostAddress()).withCompression(ProtocolOptions.Compression.LZ4).withProtocolVersion(com.datastax.driver.core.ProtocolVersion.V4).withPort(nativePort));
    int threads = 10;
    ExecutorService executor = Executors.newFixedThreadPool(threads);
    AtomicReference<Throwable> error = new AtomicReference<>();
    CountDownLatch signal = new CountDownLatch(1);
    for (int t = 0; t < threads; t++) {
        int threadId = t;
        executor.execute(() -> {
            try (Cluster driver = suppliers.get(threadId % suppliers.size()).get().build();
                Session session = driver.connect()) {
                int counter = 0;
                while (!Thread.interrupted()) {
                    Map<Integer, ResultSetFuture> futures = new HashMap<>();
                    for (int j = 0; j < 10; j++) {
                        int descriptor = counter + j * 100 + threadId * 10000;
                        SizeCaps caps = descriptor % largeMessageFrequency == 0 ? largeMessageCap : smallMessageCap;
                        futures.put(j, session.executeAsync(generateQueryStatement(descriptor, caps)));
                    }
                    for (Map.Entry<Integer, ResultSetFuture> e : futures.entrySet()) {
                        final int j = e.getKey().intValue();
                        final int descriptor = counter + j * 100 + threadId * 10000;
                        SizeCaps caps = descriptor % largeMessageFrequency == 0 ? largeMessageCap : smallMessageCap;
                        ResultMessage.Rows expectedRS = generateRows(descriptor, caps);
                        List<Row> actualRS = e.getValue().get().all();
                        for (int i = 0; i < actualRS.size(); i++) {
                            List<ByteBuffer> expected = expectedRS.result.rows.get(i);
                            Row actual = actualRS.get(i);
                            for (int col = 0; col < expected.size(); col++) Assert.assertEquals(actual.getBytes(col), expected.get(col));
                        }
                    }
                    counter++;
                }
            } catch (Throwable e) {
                e.printStackTrace();
                error.set(e);
                signal.countDown();
            }
        });
    }
    Assert.assertFalse(signal.await(120, TimeUnit.SECONDS));
    executor.shutdown();
    executor.awaitTermination(10, TimeUnit.SECONDS);
    assertThat(allocationObserver.endpointAllocationTotal()).isEqualTo(allocationObserver.endpointReleaseTotal());
    assertThat(allocationObserver.globalAllocationTotal()).isEqualTo(allocationObserver.globalReleaseTotal());
}
Also used : BurnTestUtil.generateQueryMessage(org.apache.cassandra.transport.BurnTestUtil.generateQueryMessage) QueryMessage(org.apache.cassandra.transport.messages.QueryMessage) BurnTestUtil.generateQueryMessage(org.apache.cassandra.transport.BurnTestUtil.generateQueryMessage) ResultMessage(org.apache.cassandra.transport.messages.ResultMessage) QueryMessage(org.apache.cassandra.transport.messages.QueryMessage) ByteBuf(io.netty.buffer.ByteBuf) AtomicReference(java.util.concurrent.atomic.AtomicReference) QueryState(org.apache.cassandra.service.QueryState) SizeCaps(org.apache.cassandra.transport.BurnTestUtil.SizeCaps) ResultMessage(org.apache.cassandra.transport.messages.ResultMessage) ByteBuffer(java.nio.ByteBuffer) Test(org.junit.Test)

Example 15 with QueryState

use of org.apache.cassandra.service.QueryState in project cassandra by apache.

the class QueryEventsTest method errorInListenerTest.

@Test
public void errorInListenerTest() throws Throwable {
    createTable("create table %s (id int primary key, v int)");
    QueryEvents.Listener listener = new QueryEvents.Listener() {

        @Override
        public void querySuccess(CQLStatement statement, String query, QueryOptions options, QueryState state, long queryTime, Message.Response response) {
            throw new AssertionError("whoo");
        }

        @Override
        public void queryFailure(@Nullable CQLStatement statement, String query, QueryOptions options, QueryState state, Exception cause) {
            throw new AssertionError("whee");
        }
    };
    QueryEvents.instance.registerListener(listener);
    executeNet("insert into %s (id, v) values (2,2)");
    ResultSet rs = executeNet("select * from %s");
    assertEquals(2, rs.one().getInt("id"));
    // record the exception without the throwing listener:
    QueryEvents.instance.unregisterListener(listener);
    Exception expected = null;
    try {
        executeNet("select blabla from %s");
        fail("Query should throw");
    } catch (Exception e) {
        expected = e;
    }
    QueryEvents.instance.registerListener(listener);
    // and with the listener:
    try {
        executeNet("select blabla from %s");
        fail("Query should throw");
    } catch (Exception e) {
        // make sure we throw the same exception even if the listener throws;
        assertSame(expected.getClass(), e.getClass());
        assertEquals(expected.getMessage(), e.getMessage());
    }
}
Also used : ResultSet(com.datastax.driver.core.ResultSet) QueryState(org.apache.cassandra.service.QueryState) Nullable(javax.annotation.Nullable) Test(org.junit.Test)

Aggregations

QueryState (org.apache.cassandra.service.QueryState)19 QueryOptions (org.apache.cassandra.cql3.QueryOptions)6 ClientState (org.apache.cassandra.service.ClientState)6 ResultMessage (org.apache.cassandra.transport.messages.ResultMessage)6 ByteBuf (io.netty.buffer.ByteBuf)5 ArrayList (java.util.ArrayList)5 Test (org.junit.Test)5 List (java.util.List)4 AtomicReference (java.util.concurrent.atomic.AtomicReference)4 ColumnFamilyStore (org.apache.cassandra.db.ColumnFamilyStore)4 ByteBuffer (java.nio.ByteBuffer)3 java.util (java.util)3 Collectors (java.util.stream.Collectors)3 DatabaseDescriptor (org.apache.cassandra.config.DatabaseDescriptor)3 CQLStatement (org.apache.cassandra.cql3.CQLStatement)3 SelectStatement (org.apache.cassandra.cql3.statements.SelectStatement)3 InvalidRequestException (org.apache.cassandra.exceptions.InvalidRequestException)3 BurnTestUtil.generateQueryMessage (org.apache.cassandra.transport.BurnTestUtil.generateQueryMessage)3 QueryMessage (org.apache.cassandra.transport.messages.QueryMessage)3 IOException (java.io.IOException)2