Merge branch 'cassandra-2.1' into trunk

Conflicts:
	test/unit/org/apache/cassandra/cql3/DeleteTest.java
This commit is contained in:
Aleksey Yeschenko 2014-06-20 15:23:46 -07:00
commit e88c83006a
6 changed files with 215 additions and 44 deletions

View File

@ -12,6 +12,7 @@
2.1.0-rc2
* Allow counter mutations in UNLOGGED batches (CASSANDRA-7351)
* Modify reconcile logic to always pick a tombstone over a counter cell
(CASSANDRA-7346)
* Avoid incremental compaction on Windows (CASSANDRA-7365)
@ -43,7 +44,6 @@ Merged from 1.2:
* Handle possible integer overflow in FastByteArrayOutputStream (CASSANDRA-7373)
* cqlsh: 'ascii' values weren't formatted as text (CASSANDRA-7407)
* cqlsh: ignore .cassandra permission errors (CASSANDRA-7266)
* Errors in FlushRunnable may leave threads hung (CASSANDRA-7275)
* reduce failure detector initial value to 2s (CASSANDRA-7307)
* Fix problem truncating on a node that was previously in a dead state (CASSANDRA-7318)
* Don't insert tombstones that hide indexed values into 2i (CASSANDRA-7268)

View File

@ -417,6 +417,7 @@ public class QueryProcessor implements QueryHandler
{
ClientState clientState = queryState.getClientState();
batch.checkAccess(clientState);
batch.validate();
batch.validate(clientState);
return batch.execute(queryState, options);
}

View File

@ -95,7 +95,8 @@ public class BatchStatement implements CQLStatement, MeasurableForPreparedCache
statement.checkAccess(state);
}
public void validate(ClientState state) throws InvalidRequestException
// Validates a prepared batch statement without validating its nested statements.
public void validate() throws InvalidRequestException
{
if (attrs.isTimeToLiveSet())
throw new InvalidRequestException("Global TTL on the BATCH statement is not supported.");
@ -109,13 +110,52 @@ public class BatchStatement implements CQLStatement, MeasurableForPreparedCache
throw new InvalidRequestException("Cannot provide custom timestamp for counter BATCH");
}
boolean hasCounters = false;
boolean hasNonCounters = false;
for (ModificationStatement statement : statements)
{
if (timestampSet && statement.isCounter())
throw new InvalidRequestException("Cannot provide custom timestamp for a BATCH containing counters");
if (timestampSet && statement.isTimestampSet())
throw new InvalidRequestException("Timestamp must be set either on BATCH or individual statements");
statement.validate(state);
if (type == Type.COUNTER && !statement.isCounter())
throw new InvalidRequestException("Cannot include non-counter statement in a counter batch");
if (type == Type.LOGGED && statement.isCounter())
throw new InvalidRequestException("Cannot include a counter statement in a logged batch");
if (statement.isCounter())
hasCounters = true;
else
hasNonCounters = true;
}
if (hasCounters && hasNonCounters)
throw new InvalidRequestException("Counter and non-counter mutations cannot exist in the same batch");
if (hasConditions)
{
String ksName = null;
String cfName = null;
for (ModificationStatement stmt : statements)
{
if (ksName != null && (!stmt.keyspace().equals(ksName) || !stmt.columnFamily().equals(cfName)))
throw new InvalidRequestException("Batch with conditions cannot span multiple tables");
ksName = stmt.keyspace();
cfName = stmt.columnFamily();
}
}
}
// The batch itself will be validated in either Parsed#prepare() - for regular CQL3 batches,
// or in QueryProcessor.processBatch() - for native protocol batches.
public void validate(ClientState state) throws InvalidRequestException
{
for (ModificationStatement statement : statements)
statement.validate(state);
}
public List<ModificationStatement> getStatements()
@ -180,12 +220,12 @@ public class BatchStatement implements CQLStatement, MeasurableForPreparedCache
{
mut = new Mutation(ksName, key);
mut.setSourceFrame(sourceFrame);
mutation = type == Type.COUNTER ? new CounterMutation(mut, options.getConsistency()) : mut;
mutation = statement.cfm.isCounter() ? new CounterMutation(mut, options.getConsistency()) : mut;
ksMap.put(key, mutation);
}
else
{
mut = type == Type.COUNTER ? ((CounterMutation)mutation).getMutation() : (Mutation)mutation;
mut = statement.cfm.isCounter() ? ((CounterMutation)mutation).getMutation() : (Mutation)mutation;
}
statement.addUpdateForKey(mut.addOrGet(statement.cfm), key, clusteringPrefix, params);
@ -356,40 +396,25 @@ public class BatchStatement implements CQLStatement, MeasurableForPreparedCache
{
VariableSpecifications boundNames = getBoundVariables();
List<ModificationStatement> statements = new ArrayList<ModificationStatement>(parsedStatements.size());
List<ModificationStatement> statements = new ArrayList<>(parsedStatements.size());
boolean hasConditions = false;
for (ModificationStatement.Parsed parsed : parsedStatements)
{
ModificationStatement stmt = parsed.prepare(boundNames);
if (stmt.hasConditions())
hasConditions = true;
if (stmt.isCounter() && type != Type.COUNTER)
throw new InvalidRequestException("Counter mutations are only allowed in COUNTER batches");
if (!stmt.isCounter() && type == Type.COUNTER)
throw new InvalidRequestException("Only counter mutations are allowed in COUNTER batches");
statements.add(stmt);
}
if (hasConditions)
{
String ksName = null;
String cfName = null;
for (ModificationStatement stmt : statements)
{
if (ksName != null && (!stmt.keyspace().equals(ksName) || !stmt.columnFamily().equals(cfName)))
throw new InvalidRequestException("Batch with conditions cannot span multiple tables");
ksName = stmt.keyspace();
cfName = stmt.columnFamily();
}
}
Attributes prepAttrs = attrs.prepare("[batch]", "[batch]");
prepAttrs.collectMarkerSpecification(boundNames);
return new ParsedStatement.Prepared(new BatchStatement(boundNames.size(), type, statements, prepAttrs, hasConditions), boundNames);
BatchStatement batchStatement = new BatchStatement(boundNames.size(), type, statements, prepAttrs, hasConditions);
batchStatement.validate();
return new ParsedStatement.Prepared(batchStatement, boundNames);
}
}
}

View File

@ -208,16 +208,7 @@ public class BatchMessage extends Message.Request
ModificationStatement mst = (ModificationStatement)statement;
hasConditions |= mst.hasConditions();
if (mst.isCounter())
{
if (type != BatchStatement.Type.COUNTER)
throw new InvalidRequestException("Cannot include counter statement in a non-counter batch");
}
else
{
if (type == BatchStatement.Type.COUNTER)
throw new InvalidRequestException("Cannot include non-counter statement in a counter batch");
}
statements.add(mst);
}

View File

@ -0,0 +1,145 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.cassandra.cql3;
import com.datastax.driver.core.BatchStatement;
import com.datastax.driver.core.Cluster;
import com.datastax.driver.core.PreparedStatement;
import com.datastax.driver.core.Session;
import com.datastax.driver.core.exceptions.InvalidQueryException;
import org.apache.cassandra.config.DatabaseDescriptor;
import org.apache.cassandra.exceptions.ConfigurationException;
import org.apache.cassandra.service.EmbeddedCassandraService;
import org.junit.BeforeClass;
import org.junit.Test;
import java.io.IOException;
import java.util.concurrent.TimeUnit;
public class BatchTests
{
private static EmbeddedCassandraService cassandra;
private static Cluster cluster;
private static Session session;
private static PreparedStatement counter;
private static PreparedStatement noncounter;
@BeforeClass()
public static void setup() throws ConfigurationException, IOException
{
cassandra = new EmbeddedCassandraService();
cassandra.start();
cluster = Cluster.builder().addContactPoint("127.0.0.1").withPort(DatabaseDescriptor.getNativeTransportPort()).build();
session = cluster.connect();
session.execute("drop keyspace if exists junit;");
session.execute("create keyspace junit WITH REPLICATION = { 'class' : 'SimpleStrategy', 'replication_factor' : 1 };");
session.execute("CREATE TABLE junit.noncounter (\n" +
" id int PRIMARY KEY,\n" +
" val text\n" +
");");
session.execute("CREATE TABLE junit.counter (\n" +
" id int PRIMARY KEY,\n" +
" val counter,\n" +
");");
noncounter = session.prepare("insert into junit.noncounter(id, val)values(?,?)");
counter = session.prepare("update junit.counter set val = val + ? where id = ?");
}
@Test(expected = InvalidQueryException.class)
public void testMixedInCounterBatch()
{
sendBatch(BatchStatement.Type.COUNTER, true, true);
}
@Test(expected = InvalidQueryException.class)
public void testMixedInLoggedBatch()
{
sendBatch(BatchStatement.Type.LOGGED, true, true);
}
@Test(expected = InvalidQueryException.class)
public void testMixedInUnLoggedBatch()
{
sendBatch(BatchStatement.Type.UNLOGGED, true, true);
}
@Test(expected = InvalidQueryException.class)
public void testNonCounterInCounterBatch()
{
sendBatch(BatchStatement.Type.COUNTER, false, true);
}
@Test
public void testNonCounterInLoggedBatch()
{
sendBatch(BatchStatement.Type.LOGGED, false, true);
}
@Test
public void testNonCounterInUnLoggedBatch()
{
sendBatch(BatchStatement.Type.UNLOGGED, false, true);
}
@Test
public void testCounterInCounterBatch()
{
sendBatch(BatchStatement.Type.COUNTER, true, false);
}
@Test
public void testCounterInUnLoggedBatch()
{
sendBatch(BatchStatement.Type.UNLOGGED, true, false);
}
@Test(expected = InvalidQueryException.class)
public void testCounterInLoggedBatch()
{
sendBatch(BatchStatement.Type.LOGGED, true, false);
}
public void sendBatch(BatchStatement.Type type, boolean addCounter, boolean addNonCounter)
{
assert addCounter || addNonCounter;
BatchStatement b = new BatchStatement(type);
for (int i = 0; i < 10; i++)
{
if (addNonCounter)
b.add(noncounter.bind(i, "foo"));
if (addCounter)
b.add(counter.bind((long)i, i));
}
session.execute(b);
}
}

View File

@ -1,3 +1,20 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.cassandra.cql3;
@ -18,7 +35,6 @@ import java.io.IOException;
public class DeleteTest
{
private static EmbeddedCassandraService cassandra;
private static Cluster cluster;
@ -35,8 +51,6 @@ public class DeleteTest
@BeforeClass()
public static void setup() throws ConfigurationException, IOException
{
SchemaLoader.loadSchema();
Schema.instance.clear(); // Schema are now written on disk and will be reloaded
cassandra = new EmbeddedCassandraService();
cassandra.start();
@ -80,11 +94,6 @@ public class DeleteTest
" val text ,\n" +
" PRIMARY KEY ( ( id ), cid )\n" +
");");
try {
Thread.sleep(2000L);
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
pstmtI = session.prepare("insert into junit.tpc_inherit_b ( id, cid, inh_b, val) values (?, ?, ?, ?)");
pstmtU = session.prepare("update junit.tpc_inherit_b set inh_b=?, val=? where id=? and cid=?");