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;
}
});
}
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;
}
});
}
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);
}
}
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());
}
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());
}
}
Aggregations