Migrate CQL tests from dtest to unit tests Patch by stefania; reviewed by jmckenzie for CASSANDRA-9160
Project: http://git-wip-us.apache.org/repos/asf/cassandra/repo Commit: http://git-wip-us.apache.org/repos/asf/cassandra/commit/f797bfa4 Tree: http://git-wip-us.apache.org/repos/asf/cassandra/tree/f797bfa4 Diff: http://git-wip-us.apache.org/repos/asf/cassandra/diff/f797bfa4 Branch: refs/heads/cassandra-2.1 Commit: f797bfa4da53315b49f8d97b784047f33ba1bf5f Parents: 3caf0e0 Author: Stefania Alborghetti <[email protected]> Authored: Wed Jun 24 12:10:29 2015 -0400 Committer: Josh McKenzie <[email protected]> Committed: Wed Jun 24 12:10:29 2015 -0400 ---------------------------------------------------------------------- .../cassandra/config/DatabaseDescriptor.java | 6 + .../org/apache/cassandra/cql3/ResultSet.java | 9 + .../apache/cassandra/cql3/UntypedResultSet.java | 7 +- .../cql3/statements/BatchStatement.java | 69 +- .../cql3/statements/CQL3CasRequest.java | 8 +- .../cql3/statements/ModificationStatement.java | 80 +- .../cql3/statements/SelectStatement.java | 39 +- .../cql3/statements/TruncateStatement.java | 17 +- .../apache/cassandra/db/ColumnFamilyStore.java | 5 + .../apache/cassandra/service/StorageProxy.java | 4 +- .../org/apache/cassandra/utils/UUIDGen.java | 16 +- .../org/apache/cassandra/cql3/ManyRowsTest.java | 92 ++ .../apache/cassandra/cql3/AlterTableTest.java | 113 -- .../org/apache/cassandra/cql3/CQLTester.java | 216 ++- .../apache/cassandra/cql3/CollectionsTest.java | 240 ---- .../cassandra/cql3/ContainsRelationTest.java | 269 ---- .../cassandra/cql3/CrcCheckChanceTest.java | 159 --- .../cql3/CreateAndAlterKeyspaceTest.java | 37 - .../cql3/CreateIndexStatementTest.java | 101 -- .../apache/cassandra/cql3/CreateTableTest.java | 32 - .../cql3/CreateTriggerStatementTest.java | 121 -- .../cassandra/cql3/FrozenCollectionsTest.java | 1101 ---------------- .../cql3/IndexedValuesValidationTest.java | 149 --- .../cassandra/cql3/MultiColumnRelationTest.java | 808 ------------ .../cassandra/cql3/RangeDeletionTest.java | 35 - .../cql3/SSTableMetadataTrackingTest.java | 160 --- .../cql3/SelectWithTokenFunctionTest.java | 77 -- .../cassandra/cql3/SelectionOrderingTest.java | 233 ---- .../cql3/SingleColumnRelationTest.java | 107 -- .../SliceQueryFilterWithTombstonesTest.java | 166 --- .../cassandra/cql3/StaticColumnsQueryTest.java | 280 ---- .../cassandra/cql3/ThriftCompatibilityTest.java | 1 + .../apache/cassandra/cql3/TimestampTest.java | 36 - .../apache/cassandra/cql3/TupleTypeTest.java | 101 -- .../org/apache/cassandra/cql3/TypeTest.java | 86 -- .../apache/cassandra/cql3/UseStatementTest.java | 29 - .../apache/cassandra/cql3/UserTypesTest.java | 319 ----- .../statements/SelectionColumnMappingTest.java | 9 + .../validation/entities/CollectionsTest.java | 488 +++++++ .../cql3/validation/entities/CountersTest.java | 85 ++ .../cql3/validation/entities/DateTypeTest.java | 39 + .../entities/FrozenCollectionsTest.java | 1111 ++++++++++++++++ .../validation/entities/SecondaryIndexTest.java | 644 +++++++++ .../validation/entities/StaticColumnsTest.java | 271 ++++ .../cql3/validation/entities/TimestampTest.java | 147 +++ .../cql3/validation/entities/TimeuuidTest.java | 81 ++ .../cql3/validation/entities/TupleTypeTest.java | 158 +++ .../cql3/validation/entities/TypeTest.java | 88 ++ .../cql3/validation/entities/UserTypesTest.java | 389 ++++++ .../miscellaneous/CrcCheckChanceTest.java | 160 +++ .../validation/miscellaneous/OverflowTest.java | 331 +++++ .../SSTableMetadataTrackingTest.java | 161 +++ .../miscellaneous/TombstonesTest.java | 167 +++ .../cql3/validation/operations/AlterTest.java | 201 +++ .../cql3/validation/operations/BatchTest.java | 88 ++ .../cql3/validation/operations/CreateTest.java | 462 +++++++ .../cql3/validation/operations/DeleteTest.java | 329 +++++ .../operations/InsertUpdateIfCondition.java | 861 ++++++++++++ .../validation/operations/SelectLimitTest.java | 111 ++ .../SelectMultiColumnRelationTest.java | 982 ++++++++++++++ .../operations/SelectOrderByTest.java | 503 +++++++ .../SelectOrderedPartitionerTest.java | 325 +++++ .../SelectSingleColumnRelationTest.java | 109 ++ .../cql3/validation/operations/SelectTest.java | 1238 ++++++++++++++++++ .../cql3/validation/operations/UpdateTest.java | 59 + .../cql3/validation/operations/UseTest.java | 31 + .../stress/generate/values/TimeUUIDs.java | 2 +- 67 files changed, 10137 insertions(+), 4821 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/cassandra/blob/f797bfa4/src/java/org/apache/cassandra/config/DatabaseDescriptor.java ---------------------------------------------------------------------- diff --git a/src/java/org/apache/cassandra/config/DatabaseDescriptor.java b/src/java/org/apache/cassandra/config/DatabaseDescriptor.java index 803591f..a2a9e48 100644 --- a/src/java/org/apache/cassandra/config/DatabaseDescriptor.java +++ b/src/java/org/apache/cassandra/config/DatabaseDescriptor.java @@ -1565,6 +1565,12 @@ public class DatabaseDescriptor return conf.row_cache_size_in_mb; } + @VisibleForTesting + public static void setRowCacheSizeInMB(long val) + { + conf.row_cache_size_in_mb = val; + } + public static int getRowCacheSavePeriod() { return conf.row_cache_save_period; http://git-wip-us.apache.org/repos/asf/cassandra/blob/f797bfa4/src/java/org/apache/cassandra/cql3/ResultSet.java ---------------------------------------------------------------------- diff --git a/src/java/org/apache/cassandra/cql3/ResultSet.java b/src/java/org/apache/cassandra/cql3/ResultSet.java index 4ff513b..62857c9 100644 --- a/src/java/org/apache/cassandra/cql3/ResultSet.java +++ b/src/java/org/apache/cassandra/cql3/ResultSet.java @@ -269,6 +269,15 @@ public class ResultSet return new Metadata(EnumSet.copyOf(flags), names, columnCount, pagingState); } + /** + * Return only the column names requested by the user, excluding those added for post-query re-orderings, + * see definition of names and columnCount. + **/ + public List<ColumnSpecification> requestNames() + { + return names.subList(0, columnCount); + } + // The maximum number of values that the ResultSet can hold. This can be bigger than columnCount due to CASSANDRA-4911 public int valueCount() { http://git-wip-us.apache.org/repos/asf/cassandra/blob/f797bfa4/src/java/org/apache/cassandra/cql3/UntypedResultSet.java ---------------------------------------------------------------------- diff --git a/src/java/org/apache/cassandra/cql3/UntypedResultSet.java b/src/java/org/apache/cassandra/cql3/UntypedResultSet.java index fef70fb..81482ef 100644 --- a/src/java/org/apache/cassandra/cql3/UntypedResultSet.java +++ b/src/java/org/apache/cassandra/cql3/UntypedResultSet.java @@ -96,7 +96,7 @@ public abstract class UntypedResultSet implements Iterable<UntypedResultSet.Row> public List<ColumnSpecification> metadata() { - return cqlRows.metadata.names; + return cqlRows.metadata.requestNames(); } } @@ -219,6 +219,11 @@ public abstract class UntypedResultSet implements Iterable<UntypedResultSet.Row> return data.get(column) != null; } + public ByteBuffer getBlob(String column) + { + return data.get(column); + } + public String getString(String column) { return UTF8Type.instance.compose(data.get(column)); http://git-wip-us.apache.org/repos/asf/cassandra/blob/f797bfa4/src/java/org/apache/cassandra/cql3/statements/BatchStatement.java ---------------------------------------------------------------------- diff --git a/src/java/org/apache/cassandra/cql3/statements/BatchStatement.java b/src/java/org/apache/cassandra/cql3/statements/BatchStatement.java index 6d4d3a1..2005521 100644 --- a/src/java/org/apache/cassandra/cql3/statements/BatchStatement.java +++ b/src/java/org/apache/cassandra/cql3/statements/BatchStatement.java @@ -39,6 +39,7 @@ import org.apache.cassandra.service.QueryState; import org.apache.cassandra.service.StorageProxy; import org.apache.cassandra.transport.messages.ResultMessage; import org.apache.cassandra.utils.NoSpamLogger; +import org.apache.cassandra.utils.Pair; /** * A <code>BATCH</code> statement parsed from a CQL query. @@ -318,10 +319,32 @@ public class BatchStatement implements CQLStatement private ResultMessage executeWithConditions(BatchQueryOptions options, QueryState state) throws RequestExecutionException, RequestValidationException { + Pair<CQL3CasRequest, Set<ColumnDefinition>> p = makeCasRequest(options, state); + CQL3CasRequest casRequest = p.left; + Set<ColumnDefinition> columnsWithConditions = p.right; + + ColumnFamily result = StorageProxy.cas(casRequest.cfm.ksName, + casRequest.cfm.cfName, + casRequest.key, + casRequest, + options.getSerialConsistency(), + options.getConsistency(), + state.getClientState()); + + return new ResultMessage.Rows(ModificationStatement.buildCasResultSet(casRequest.cfm.ksName, + casRequest.key, + casRequest.cfm.cfName, + result, + columnsWithConditions, + true, + options.forStatement(0))); + } + + private Pair<CQL3CasRequest,Set<ColumnDefinition>> makeCasRequest(BatchQueryOptions options, QueryState state) + throws InvalidRequestException + { long now = state.getTimestamp(); ByteBuffer key = null; - String ksName = null; - String cfName = null; CQL3CasRequest casRequest = null; Set<ColumnDefinition> columnsWithConditions = new LinkedHashSet<>(); @@ -336,8 +359,6 @@ public class BatchStatement implements CQLStatement if (key == null) { key = pks.get(0); - ksName = statement.cfm.ksName; - cfName = statement.cfm.cfName; casRequest = new CQL3CasRequest(statement.cfm, key, true); } else if (!key.equals(pks.get(0))) @@ -358,23 +379,49 @@ public class BatchStatement implements CQLStatement casRequest.addRowUpdate(clusteringPrefix, statement, statementOptions, timestamp); } - ColumnFamily result = StorageProxy.cas(ksName, cfName, key, casRequest, options.getSerialConsistency(), options.getConsistency(), state.getClientState()); - - return new ResultMessage.Rows(ModificationStatement.buildCasResultSet(ksName, key, cfName, result, columnsWithConditions, true, options.forStatement(0))); + return Pair.create(casRequest, columnsWithConditions); } public ResultMessage executeInternal(QueryState queryState, QueryOptions options) throws RequestValidationException, RequestExecutionException { - assert !hasConditions; + if (hasConditions) + return executeInternalWithConditions(BatchQueryOptions.withoutPerStatementVariables(options), queryState); + + executeInternalWithoutCondition(queryState, options); + return new ResultMessage.Void(); + } + + private ResultMessage executeInternalWithoutCondition(QueryState queryState, QueryOptions options) throws RequestValidationException, RequestExecutionException + { for (IMutation mutation : getMutations(BatchQueryOptions.withoutPerStatementVariables(options), true, queryState.getTimestamp())) { - // We don't use counters internally. - assert mutation instanceof Mutation; - ((Mutation) mutation).apply(); + assert mutation instanceof Mutation || mutation instanceof CounterMutation; + + if (mutation instanceof Mutation) + ((Mutation) mutation).apply(); + else if (mutation instanceof CounterMutation) + ((CounterMutation) mutation).apply(); } return null; } + private ResultMessage executeInternalWithConditions(BatchQueryOptions options, QueryState state) throws RequestExecutionException, RequestValidationException + { + Pair<CQL3CasRequest, Set<ColumnDefinition>> p = makeCasRequest(options, state); + CQL3CasRequest request = p.left; + Set<ColumnDefinition> columnsWithConditions = p.right; + + ColumnFamily result = ModificationStatement.casInternal(request, state); + + return new ResultMessage.Rows(ModificationStatement.buildCasResultSet(request.cfm.ksName, + request.key, + request.cfm.cfName, + result, + columnsWithConditions, + true, + options.forStatement(0))); + } + public interface BatchVariables { public List<ByteBuffer> getVariablesForStatement(int statementInBatch); http://git-wip-us.apache.org/repos/asf/cassandra/blob/f797bfa4/src/java/org/apache/cassandra/cql3/statements/CQL3CasRequest.java ---------------------------------------------------------------------- diff --git a/src/java/org/apache/cassandra/cql3/statements/CQL3CasRequest.java b/src/java/org/apache/cassandra/cql3/statements/CQL3CasRequest.java index eb29012..3ea7cd8 100644 --- a/src/java/org/apache/cassandra/cql3/statements/CQL3CasRequest.java +++ b/src/java/org/apache/cassandra/cql3/statements/CQL3CasRequest.java @@ -37,10 +37,10 @@ import org.apache.cassandra.utils.Pair; */ public class CQL3CasRequest implements CASRequest { - private final CFMetaData cfm; - private final ByteBuffer key; - private final long now; - private final boolean isBatch; + final CFMetaData cfm; + final ByteBuffer key; + final long now; + final boolean isBatch; // We index RowCondition by the prefix of the row they applied to for 2 reasons: // 1) this allows to keep things sorted to build the ColumnSlice array below http://git-wip-us.apache.org/repos/asf/cassandra/blob/f797bfa4/src/java/org/apache/cassandra/cql3/statements/ModificationStatement.java ---------------------------------------------------------------------- diff --git a/src/java/org/apache/cassandra/cql3/statements/ModificationStatement.java b/src/java/org/apache/cassandra/cql3/statements/ModificationStatement.java index 60558b4..6b0901a 100644 --- a/src/java/org/apache/cassandra/cql3/statements/ModificationStatement.java +++ b/src/java/org/apache/cassandra/cql3/statements/ModificationStatement.java @@ -26,6 +26,7 @@ import com.google.common.collect.Iterables; import org.apache.cassandra.auth.Permission; import org.apache.cassandra.config.CFMetaData; import org.apache.cassandra.config.ColumnDefinition; +import org.apache.cassandra.config.Schema; import org.apache.cassandra.cql3.*; import org.apache.cassandra.db.*; import org.apache.cassandra.db.composites.CBuilder; @@ -37,9 +38,12 @@ import org.apache.cassandra.exceptions.*; import org.apache.cassandra.service.ClientState; import org.apache.cassandra.service.QueryState; import org.apache.cassandra.service.StorageProxy; +import org.apache.cassandra.service.paxos.Commit; import org.apache.cassandra.thrift.ThriftValidation; import org.apache.cassandra.transport.messages.ResultMessage; +import org.apache.cassandra.triggers.TriggerExecutor; import org.apache.cassandra.utils.Pair; +import org.apache.cassandra.utils.UUIDGen; /* * Abstract parent class of individual modifications, i.e. INSERT, UPDATE and DELETE. @@ -494,6 +498,21 @@ public abstract class ModificationStatement implements CQLStatement public ResultMessage executeWithCondition(QueryState queryState, QueryOptions options) throws RequestExecutionException, RequestValidationException { + CQL3CasRequest request = makeCasRequest(queryState, options); + + ColumnFamily result = StorageProxy.cas(keyspace(), + columnFamily(), + request.key, + request, + options.getSerialConsistency(), + options.getConsistency(), + queryState.getClientState()); + return new ResultMessage.Rows(buildCasResultSet(request.key, result, options)); + } + + private CQL3CasRequest makeCasRequest(QueryState queryState, QueryOptions options) + throws InvalidRequestException + { List<ByteBuffer> keys = buildPartitionKeyNames(options); // We don't support IN for CAS operation so far if (keys.size() > 1) @@ -506,15 +525,7 @@ public abstract class ModificationStatement implements CQLStatement CQL3CasRequest request = new CQL3CasRequest(cfm, key, false); addConditions(prefix, request, options); request.addRowUpdate(prefix, this, options, now); - - ColumnFamily result = StorageProxy.cas(keyspace(), - columnFamily(), - key, - request, - options.getSerialConsistency(), - options.getConsistency(), - queryState.getClientState()); - return new ResultMessage.Rows(buildCasResultSet(key, result, options)); + return request; } public void addConditions(Composite clusteringPrefix, CQL3CasRequest request, QueryOptions options) throws InvalidRequestException @@ -615,16 +626,57 @@ public abstract class ModificationStatement implements CQLStatement public ResultMessage executeInternal(QueryState queryState, QueryOptions options) throws RequestValidationException, RequestExecutionException { - if (hasConditions()) - throw new UnsupportedOperationException(); + return hasConditions() + ? executeInternalWithCondition(queryState, options) + : executeInternalWithoutCondition(queryState, options); + } + public ResultMessage executeInternalWithoutCondition(QueryState queryState, QueryOptions options) throws RequestValidationException, RequestExecutionException + { for (IMutation mutation : getMutations(options, true, queryState.getTimestamp())) { - // We don't use counters internally. - assert mutation instanceof Mutation; + assert mutation instanceof Mutation || mutation instanceof CounterMutation; + + if (mutation instanceof Mutation) + ((Mutation) mutation).apply(); + else if (mutation instanceof CounterMutation) + ((CounterMutation) mutation).apply(); + } + return null; + } + + public ResultMessage executeInternalWithCondition(QueryState state, QueryOptions options) throws RequestValidationException, RequestExecutionException + { + CQL3CasRequest request = makeCasRequest(state, options); + ColumnFamily result = casInternal(request, state); + return new ResultMessage.Rows(buildCasResultSet(request.key, result, options)); + } + + static ColumnFamily casInternal(CQL3CasRequest request, QueryState state) + throws InvalidRequestException + { + long millis = state.getTimestamp() / 1000; + long nanos = ((state.getTimestamp() - (millis * 1000)) + 1) * 10; + UUID ballot = UUIDGen.getTimeUUID(millis, nanos); + CFMetaData metadata = Schema.instance.getCFMetaData(request.cfm.ksName, request.cfm.cfName); - ((Mutation) mutation).apply(); + ReadCommand readCommand = ReadCommand.create(request.cfm.ksName, request.key, request.cfm.cfName, request.now, request.readFilter()); + Keyspace keyspace = Keyspace.open(request.cfm.ksName); + + Row row = readCommand.getRow(keyspace); + ColumnFamily current = row.cf; + if (!request.appliesTo(current)) + { + if (current == null) + current = ArrayBackedSortedColumns.factory.create(metadata); + return current; } + + ColumnFamily updates = request.makeUpdates(current); + updates = TriggerExecutor.instance.execute(request.key, updates); + + Commit proposal = Commit.newProposal(request.key, ballot, updates); + proposal.makeMutation().apply(); return null; } http://git-wip-us.apache.org/repos/asf/cassandra/blob/f797bfa4/src/java/org/apache/cassandra/cql3/statements/SelectStatement.java ---------------------------------------------------------------------- diff --git a/src/java/org/apache/cassandra/cql3/statements/SelectStatement.java b/src/java/org/apache/cassandra/cql3/statements/SelectStatement.java index d0566eb..540cb8c 100644 --- a/src/java/org/apache/cassandra/cql3/statements/SelectStatement.java +++ b/src/java/org/apache/cassandra/cql3/statements/SelectStatement.java @@ -315,13 +315,40 @@ public class SelectStatement implements CQLStatement int limit = getLimit(options); long now = System.currentTimeMillis(); Pageable command = getPageableCommand(options, limit, now); - List<Row> rows = command == null - ? Collections.<Row>emptyList() - : (command instanceof Pageable.ReadCommands - ? readLocally(keyspace(), ((Pageable.ReadCommands)command).commands) - : ((RangeSliceCommand)command).executeLocally()); - return processResults(rows, options, limit, now); + int pageSize = options.getPageSize(); + if (parameters.isCount && pageSize <= 0) + pageSize = DEFAULT_COUNT_PAGE_SIZE; + + if (pageSize <= 0 || command == null || !QueryPagers.mayNeedPaging(command, pageSize)) + { + List<Row> rows = command == null + ? Collections.<Row>emptyList() + : (command instanceof Pageable.ReadCommands + ? readLocally(keyspace(), ((Pageable.ReadCommands)command).commands) + : ((RangeSliceCommand)command).executeLocally()); + + return processResults(rows, options, limit, now); + } + else + { + QueryPager pager = QueryPagers.localPager(command); + if (parameters.isCount) + return pageCountQuery(pager, options, pageSize, now, limit); + + // We can't properly do post-query ordering if we page (see #6722) + if (needsPostQueryOrdering()) + throw new InvalidRequestException("Cannot page queries with both ORDER BY and a IN restriction on the partition key; you must either remove the " + + "ORDER BY or the IN and sort client side, or disable paging for this query"); + + List<Row> page = pager.fetchPage(pageSize); + ResultMessage.Rows msg = processResults(page, options, limit, now); + + if (!pager.isExhausted()) + msg.result.metadata.setHasMorePages(pager.state()); + + return msg; + } } public ResultSet process(List<Row> rows) throws InvalidRequestException http://git-wip-us.apache.org/repos/asf/cassandra/blob/f797bfa4/src/java/org/apache/cassandra/cql3/statements/TruncateStatement.java ---------------------------------------------------------------------- diff --git a/src/java/org/apache/cassandra/cql3/statements/TruncateStatement.java b/src/java/org/apache/cassandra/cql3/statements/TruncateStatement.java index ef1c4a4..09275e8 100644 --- a/src/java/org/apache/cassandra/cql3/statements/TruncateStatement.java +++ b/src/java/org/apache/cassandra/cql3/statements/TruncateStatement.java @@ -22,6 +22,8 @@ import java.util.concurrent.TimeoutException; import org.apache.cassandra.auth.Permission; import org.apache.cassandra.cql3.*; +import org.apache.cassandra.db.ColumnFamilyStore; +import org.apache.cassandra.db.Keyspace; import org.apache.cassandra.exceptions.*; import org.apache.cassandra.transport.messages.ResultMessage; import org.apache.cassandra.service.ClientState; @@ -56,7 +58,8 @@ public class TruncateStatement extends CFStatement implements CQLStatement ThriftValidation.validateColumnFamily(keyspace(), columnFamily()); } - public ResultMessage execute(QueryState state, QueryOptions options) throws InvalidRequestException, TruncateException + public ResultMessage execute(QueryState state, QueryOptions options) + throws InvalidRequestException, TruncateException { try { @@ -78,7 +81,17 @@ public class TruncateStatement extends CFStatement implements CQLStatement } public ResultMessage executeInternal(QueryState state, QueryOptions options) + throws TruncateException { - throw new UnsupportedOperationException(); + try + { + ColumnFamilyStore cfs = Keyspace.open(keyspace()).getColumnFamilyStore(columnFamily()); + cfs.truncateBlocking(); + } + catch (Exception e) + { + throw new TruncateException(e); + } + return null; } } http://git-wip-us.apache.org/repos/asf/cassandra/blob/f797bfa4/src/java/org/apache/cassandra/db/ColumnFamilyStore.java ---------------------------------------------------------------------- diff --git a/src/java/org/apache/cassandra/db/ColumnFamilyStore.java b/src/java/org/apache/cassandra/db/ColumnFamilyStore.java index a01bb49..65bf289 100644 --- a/src/java/org/apache/cassandra/db/ColumnFamilyStore.java +++ b/src/java/org/apache/cassandra/db/ColumnFamilyStore.java @@ -806,6 +806,11 @@ public class ColumnFamilyStore implements ColumnFamilyStoreMBean logger.info("Done loading load new SSTables for {}/{}", keyspace.getName(), name); } + public void rebuildSecondaryIndex(String idxName) + { + rebuildSecondaryIndex(keyspace.getName(), metadata.cfName, idxName); + } + public static void rebuildSecondaryIndex(String ksName, String cfName, String... idxNames) { ColumnFamilyStore cfs = Keyspace.open(ksName).getColumnFamilyStore(cfName); http://git-wip-us.apache.org/repos/asf/cassandra/blob/f797bfa4/src/java/org/apache/cassandra/service/StorageProxy.java ---------------------------------------------------------------------- diff --git a/src/java/org/apache/cassandra/service/StorageProxy.java b/src/java/org/apache/cassandra/service/StorageProxy.java index 1536e46..b76c231 100644 --- a/src/java/org/apache/cassandra/service/StorageProxy.java +++ b/src/java/org/apache/cassandra/service/StorageProxy.java @@ -229,7 +229,9 @@ public class StorageProxy implements StorageProxyMBean Tracing.trace("Reading existing values for CAS precondition"); long timestamp = System.currentTimeMillis(); ReadCommand readCommand = ReadCommand.create(keyspaceName, key, cfName, timestamp, request.readFilter()); - List<Row> rows = read(Arrays.asList(readCommand), consistencyForPaxos == ConsistencyLevel.LOCAL_SERIAL ? ConsistencyLevel.LOCAL_QUORUM : ConsistencyLevel.QUORUM); + List<Row> rows = read(Arrays.asList(readCommand), consistencyForPaxos == ConsistencyLevel.LOCAL_SERIAL + ? ConsistencyLevel.LOCAL_QUORUM + : ConsistencyLevel.QUORUM); ColumnFamily current = rows.get(0).cf; if (!request.appliesTo(current)) { http://git-wip-us.apache.org/repos/asf/cassandra/blob/f797bfa4/src/java/org/apache/cassandra/utils/UUIDGen.java ---------------------------------------------------------------------- diff --git a/src/java/org/apache/cassandra/utils/UUIDGen.java b/src/java/org/apache/cassandra/utils/UUIDGen.java index 53293b2..54347ff 100644 --- a/src/java/org/apache/cassandra/utils/UUIDGen.java +++ b/src/java/org/apache/cassandra/utils/UUIDGen.java @@ -82,10 +82,15 @@ public class UUIDGen return new UUID(createTime(fromUnixTimestamp(when)), clockSeqAndNode); } + public static UUID getTimeUUID(long when, long nanos) + { + return new UUID(createTime(fromUnixTimestamp(when, nanos)), clockSeqAndNode); + } + @VisibleForTesting - public static UUID getTimeUUID(long when, long clockSeqAndNode) + public static UUID getTimeUUID(long when, long nanos, long clockSeqAndNode) { - return new UUID(createTime(fromUnixTimestamp(when)), clockSeqAndNode); + return new UUID(createTime(fromUnixTimestamp(when, nanos)), clockSeqAndNode); } /** creates a type 1 uuid from raw bytes. */ @@ -169,7 +174,12 @@ public class UUIDGen * @return */ private static long fromUnixTimestamp(long timestamp) { - return (timestamp - START_EPOCH) * 10000; + return fromUnixTimestamp(timestamp, 0L); + } + + private static long fromUnixTimestamp(long timestamp, long nanos) + { + return ((timestamp - START_EPOCH) * 10000) + nanos; } /** http://git-wip-us.apache.org/repos/asf/cassandra/blob/f797bfa4/test/long/org/apache/cassandra/cql3/ManyRowsTest.java ---------------------------------------------------------------------- diff --git a/test/long/org/apache/cassandra/cql3/ManyRowsTest.java b/test/long/org/apache/cassandra/cql3/ManyRowsTest.java new file mode 100644 index 0000000..82eeabd --- /dev/null +++ b/test/long/org/apache/cassandra/cql3/ManyRowsTest.java @@ -0,0 +1,92 @@ +/* + * 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 java.util.ArrayList; +import java.util.HashSet; +import java.util.List; +import java.util.Random; +import java.util.Set; + +import org.junit.Test; + +public class ManyRowsTest extends CQLTester +{ + /** + * Migrated from cql_tests.py:TestCQL.large_count_test() + */ + @Test + public void testLargeCount() throws Throwable + { + createTable("CREATE TABLE %s (k int, v int, PRIMARY KEY (k))"); + + // We know we page at 10K, so test counting just before, at 10K, just after and + // a bit after that. + for (int k = 1; k < 10000; k++) + execute("INSERT INTO %s (k) VALUES (?)", k); + + assertRows(execute("SELECT COUNT(*) FROM %s"), row(9999L)); + + execute("INSERT INTO %s (k) VALUES (?)", 10000); + + assertRows(execute("SELECT COUNT(*) FROM %s"), row(10000L)); + + execute("INSERT INTO %s (k) VALUES (?)", 10001); + + assertRows(execute("SELECT COUNT(*) FROM %s"), row(10001L)); + + for (int k = 10002; k < 15001; k++) + execute("INSERT INTO %s (k) VALUES (?)", k); + + assertRows(execute("SELECT COUNT(*) FROM %s"), row(15000L)); + } + + /** + * Test for CASSANDRA-8410, + * migrated from cql_tests.py:TestCQL.large_clustering_in_test() + */ + @Test + public void testLargeClustering() throws Throwable + { + createTable("CREATE TABLE %s (k int, c int, v int, PRIMARY KEY (k, c) )"); + + execute("INSERT INTO %s (k, c, v) VALUES (?, ?, ?)", 0, 0, 0); + + // try to fetch one existing row and 9999 non-existing rows + List<Integer> inValues = new ArrayList(10000); + for (int i = 0; i < 10000; i++) + inValues.add(i); + + assertRows(execute("SELECT * FROM %s WHERE k=? AND c IN ?", 0, inValues), + row(0, 0, 0)); + + // insert approximately 1000 random rows between 0 and 10k + Random rnd = new Random(); + Set<Integer> clusteringValues = new HashSet<>(); + for (int i = 0; i < 1000; i++) + clusteringValues.add(rnd.nextInt(10000)); + + clusteringValues.add(0); + + for (int i : clusteringValues) // TODO - this was done in parallel by dtests + execute("INSERT INTO %s (k, c, v) VALUES (?, ?, ?)", 0, i, i); + + assertRowCount(execute("SELECT * FROM %s WHERE k=? AND c IN ?", 0, inValues), clusteringValues.size()); + } +} http://git-wip-us.apache.org/repos/asf/cassandra/blob/f797bfa4/test/unit/org/apache/cassandra/cql3/AlterTableTest.java ---------------------------------------------------------------------- diff --git a/test/unit/org/apache/cassandra/cql3/AlterTableTest.java b/test/unit/org/apache/cassandra/cql3/AlterTableTest.java deleted file mode 100644 index 4540f3c..0000000 --- a/test/unit/org/apache/cassandra/cql3/AlterTableTest.java +++ /dev/null @@ -1,113 +0,0 @@ -/* - * 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 org.apache.cassandra.db.ColumnFamilyStore; -import org.apache.cassandra.db.Keyspace; -import org.junit.Test; - -import static org.junit.Assert.assertEquals; - -public class AlterTableTest extends CQLTester -{ - @Test - public void testAddList() throws Throwable - { - createTable("CREATE TABLE %s (id text PRIMARY KEY, content text);"); - execute("ALTER TABLE %s ADD myCollection list<text>;"); - execute("INSERT INTO %s (id, content , myCollection) VALUES ('test', 'first test', ['first element']);"); - - assertRows(execute("SELECT * FROM %s;"), row("test", "first test", list("first element"))); - } - - @Test - public void testDropList() throws Throwable - { - createTable("CREATE TABLE %s (id text PRIMARY KEY, content text, myCollection list<text>);"); - execute("INSERT INTO %s (id, content , myCollection) VALUES ('test', 'first test', ['first element']);"); - execute("ALTER TABLE %s DROP myCollection;"); - - assertRows(execute("SELECT * FROM %s;"), row("test", "first test")); - } - @Test - public void testAddMap() throws Throwable - { - createTable("CREATE TABLE %s (id text PRIMARY KEY, content text);"); - execute("ALTER TABLE %s ADD myCollection map<text, text>;"); - execute("INSERT INTO %s (id, content , myCollection) VALUES ('test', 'first test', { '1' : 'first element'});"); - - assertRows(execute("SELECT * FROM %s;"), row("test", "first test", map("1", "first element"))); - } - - @Test - public void testDropMap() throws Throwable - { - createTable("CREATE TABLE %s (id text PRIMARY KEY, content text, myCollection map<text, text>);"); - execute("INSERT INTO %s (id, content , myCollection) VALUES ('test', 'first test', { '1' : 'first element'});"); - execute("ALTER TABLE %s DROP myCollection;"); - - assertRows(execute("SELECT * FROM %s;"), row("test", "first test")); - } - - @Test - public void testDropListAndAddListWithSameName() throws Throwable - { - createTable("CREATE TABLE %s (id text PRIMARY KEY, content text, myCollection list<text>);"); - execute("INSERT INTO %s (id, content , myCollection) VALUES ('test', 'first test', ['first element']);"); - execute("ALTER TABLE %s DROP myCollection;"); - execute("ALTER TABLE %s ADD myCollection list<text>;"); - - assertRows(execute("SELECT * FROM %s;"), row("test", "first test", null)); - execute("UPDATE %s set myCollection = ['second element'] WHERE id = 'test';"); - assertRows(execute("SELECT * FROM %s;"), row("test", "first test", list("second element"))); - } - @Test - public void testDropListAndAddMapWithSameName() throws Throwable - { - createTable("CREATE TABLE %s (id text PRIMARY KEY, content text, myCollection list<text>);"); - execute("INSERT INTO %s (id, content , myCollection) VALUES ('test', 'first test', ['first element']);"); - execute("ALTER TABLE %s DROP myCollection;"); - - assertInvalid("ALTER TABLE %s ADD myCollection map<int, int>;"); - } - - @Test - public void testChangeStrategyWithUnquotedAgrument() throws Throwable - { - createTable("CREATE TABLE %s (id text PRIMARY KEY);"); - - assertInvalidSyntaxMessage("no viable alternative at input '}'", - "ALTER TABLE %s WITH caching = {'keys' : 'all', 'rows_per_partition' : ALL};"); - } - - @Test - // tests CASSANDRA-7976 - public void testAlterIndexInterval() throws Throwable - { - String tableName = createTable("CREATE TABLE IF NOT EXISTS %s (id uuid, album text, artist text, data blob, PRIMARY KEY (id))"); - ColumnFamilyStore cfs = Keyspace.open(KEYSPACE).getColumnFamilyStore(tableName); - - alterTable("ALTER TABLE %s WITH min_index_interval=256 AND max_index_interval=512"); - assertEquals(256, cfs.metadata.getMinIndexInterval()); - assertEquals(512, cfs.metadata.getMaxIndexInterval()); - - alterTable("ALTER TABLE %s WITH caching = 'none'"); - assertEquals(256, cfs.metadata.getMinIndexInterval()); - assertEquals(512, cfs.metadata.getMaxIndexInterval()); - } -} http://git-wip-us.apache.org/repos/asf/cassandra/blob/f797bfa4/test/unit/org/apache/cassandra/cql3/CQLTester.java ---------------------------------------------------------------------- diff --git a/test/unit/org/apache/cassandra/cql3/CQLTester.java b/test/unit/org/apache/cassandra/cql3/CQLTester.java index 8b24e69..8e620af 100644 --- a/test/unit/org/apache/cassandra/cql3/CQLTester.java +++ b/test/unit/org/apache/cassandra/cql3/CQLTester.java @@ -18,6 +18,9 @@ package org.apache.cassandra.cql3; import java.io.File; +import java.math.BigDecimal; +import java.math.BigInteger; +import java.net.InetAddress; import java.nio.ByteBuffer; import java.util.*; import java.util.concurrent.CountDownLatch; @@ -30,17 +33,21 @@ import com.google.common.collect.ImmutableSet; import org.junit.AfterClass; import org.junit.After; import org.junit.Assert; +import org.junit.Before; import org.junit.BeforeClass; import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import static junit.framework.Assert.assertNotNull; import org.apache.cassandra.SchemaLoader; import org.apache.cassandra.concurrent.ScheduledExecutors; import org.apache.cassandra.config.CFMetaData; +import org.apache.cassandra.config.DatabaseDescriptor; import org.apache.cassandra.config.Schema; import org.apache.cassandra.db.Directories; import org.apache.cassandra.db.Keyspace; import org.apache.cassandra.db.marshal.*; +import org.apache.cassandra.dht.Murmur3Partitioner; import org.apache.cassandra.exceptions.*; import org.apache.cassandra.io.util.FileUtils; import org.apache.cassandra.serializers.TypeSerializer; @@ -54,6 +61,7 @@ public abstract class CQLTester public static final String KEYSPACE = "cql_test_keyspace"; private static final boolean USE_PREPARED_VALUES = Boolean.valueOf(System.getProperty("cassandra.test.use_prepared", "true")); + protected static final long ROW_CACHE_SIZE_IN_MB = Integer.valueOf(System.getProperty("cassandra.test.row_cache_size_in_mb", "0")); private static final AtomicInteger seqNumber = new AtomicInteger(); static @@ -66,9 +74,12 @@ public abstract class CQLTester private List<String> types = new ArrayList<>(); @BeforeClass - public static void setUpClass() throws Throwable + public static void setUpClass() { - schemaChange(String.format("CREATE KEYSPACE IF NOT EXISTS %s WITH replication = {'class': 'SimpleStrategy', 'replication_factor': '1'}", KEYSPACE)); + if (ROW_CACHE_SIZE_IN_MB > 0) + DatabaseDescriptor.setRowCacheSizeInMB(ROW_CACHE_SIZE_IN_MB); + + DatabaseDescriptor.setPartitioner(new Murmur3Partitioner()); } @AfterClass @@ -76,6 +87,12 @@ public abstract class CQLTester { } + @Before + public void beforeTest() throws Throwable + { + schemaChange(String.format("CREATE KEYSPACE IF NOT EXISTS %s WITH replication = {'class': 'SimpleStrategy', 'replication_factor': '1'}", KEYSPACE)); + } + @After public void afterTest() throws Throwable { @@ -91,10 +108,10 @@ public abstract class CQLTester { try { - for (int i = tablesToDrop.size() - 1; i >=0; i--) + for (int i = tablesToDrop.size() - 1; i >= 0; i--) schemaChange(String.format("DROP TABLE IF EXISTS %s.%s", KEYSPACE, tablesToDrop.get(i))); - for (int i = typesToDrop.size() - 1; i >=0; i--) + for (int i = typesToDrop.size() - 1; i >= 0; i--) schemaChange(String.format("DROP TYPE IF EXISTS %s.%s", KEYSPACE, typesToDrop.get(i))); // Dropping doesn't delete the sstables. It's not a huge deal but it's cleaner to cleanup after us @@ -105,10 +122,10 @@ public abstract class CQLTester final CountDownLatch latch = new CountDownLatch(1); ScheduledExecutors.nonPeriodicTasks.execute(new Runnable() { - public void run() - { - latch.countDown(); - } + public void run() + { + latch.countDown(); + } }); latch.await(2, TimeUnit.SECONDS); @@ -149,9 +166,25 @@ public abstract class CQLTester } } - public boolean usePrepared() + public void compact() + { + try + { + String currentTable = currentTable(); + if (currentTable != null) + Keyspace.open(KEYSPACE).getColumnFamilyStore(currentTable).forceMajorCompaction(); + } + catch (InterruptedException | ExecutionException e) + { + throw new RuntimeException(e); + } + } + + public void cleanupCache() { - return USE_PREPARED_VALUES; + String currentTable = currentTable(); + if (currentTable != null) + Keyspace.open(KEYSPACE).getColumnFamilyStore(currentTable).cleanupCache(); } private static void removeAllSSTables(String ks, List<String> tables) @@ -196,14 +229,20 @@ public abstract class CQLTester protected String createTable(String query) { - String currentTable = "table_" + seqNumber.getAndIncrement(); - tables.add(currentTable); + String currentTable = createTableName(); String fullQuery = String.format(query, KEYSPACE + "." + currentTable); logger.info(fullQuery); schemaChange(fullQuery); return currentTable; } + protected String createTableName() + { + String currentTable = "table_" + seqNumber.getAndIncrement(); + tables.add(currentTable); + return currentTable; + } + protected void createTableMayThrow(String query) throws Throwable { String currentTable = "table_" + seqNumber.getAndIncrement(); @@ -255,6 +294,37 @@ public abstract class CQLTester schemaChange(fullQuery); } + /** + * Index creation is asynchronous, this method searches in the system table IndexInfo + * for the specified index and returns true if it finds it, which indicates the + * index was built. If we haven't found it after 5 seconds we give-up. + */ + protected boolean waitForIndex(String keyspace, String table, String index) throws Throwable + { + long start = System.currentTimeMillis(); + boolean indexCreated = false; + String indedName = String.format("%s.%s", table, index); + while (!indexCreated) + { + Object[][] results = getRows(execute("select index_name from system.\"IndexInfo\" where table_name = ?", keyspace)); + for(int i = 0; i < results.length; i++) + { + if (indedName.equals(results[i][0])) + { + indexCreated = true; + break; + } + } + + if (System.currentTimeMillis() - start > 5000) + break; + + Thread.sleep(10); + } + + return indexCreated; + } + protected void createIndexMayThrow(String query) throws Throwable { String fullQuery = String.format(query, KEYSPACE + "." + currentTable()); @@ -276,7 +346,7 @@ public abstract class CQLTester schemaChange(fullQuery); } - private static void schemaChange(String query) + protected static void schemaChange(String query) { try { @@ -341,7 +411,7 @@ public abstract class CQLTester Object[] expected = rows[i]; UntypedResultSet.Row actual = iter.next(); - Assert.assertEquals(String.format("Invalid number of (expected) values provided for row %d", i), meta.size(), expected.length); + Assert.assertEquals(String.format("Invalid number of (expected) values provided for row %d", i), expected.length, meta.size()); for (int j = 0; j < meta.size(); j++) { @@ -351,8 +421,17 @@ public abstract class CQLTester ByteBuffer actualValue = actual.getBytes(column.name.toString()); if (!Objects.equal(expectedByteValue, actualValue)) - Assert.fail(String.format("Invalid value for row %d column %d (%s of type %s), expected <%s> but got <%s>", - i, j, column.name, column.type.asCQL3Type(), formatValue(expectedByteValue, column.type), formatValue(actualValue, column.type))); + { + Object actualValueDecoded = column.type.getSerializer().deserialize(actualValue); + if (!actualValueDecoded.equals(expected[j])) + Assert.fail(String.format("Invalid value for row %d column %d (%s of type %s), expected <%s> but got <%s>", + i, + j, + column.name, + column.type.asCQL3Type(), + formatValue(expectedByteValue, column.type), + formatValue(actualValue, column.type))); + } } i++; } @@ -370,6 +449,65 @@ public abstract class CQLTester Assert.assertTrue(String.format("Got more rows than expected. Expected %d but got %d", rows.length, i), i == rows.length); } + protected void assertRowCount(UntypedResultSet result, int numExpectedRows) + { + if (result == null) + { + if (numExpectedRows > 0) + Assert.fail(String.format("No rows returned by query but %d expected", numExpectedRows)); + return; + } + + List<ColumnSpecification> meta = result.metadata(); + Iterator<UntypedResultSet.Row> iter = result.iterator(); + int i = 0; + while (iter.hasNext() && i < numExpectedRows) + { + UntypedResultSet.Row actual = iter.next(); + assertNotNull(actual); + i++; + } + + if (iter.hasNext()) + { + while (iter.hasNext()) + { + iter.next(); + i++; + } + Assert.fail(String.format("Got less rows than expected. Expected %d but got %d.", numExpectedRows, i)); + } + + Assert.assertTrue(String.format("Got %s rows than expected. Expected %d but got %d", numExpectedRows>i ? "less" : "more", numExpectedRows, i), i == numExpectedRows); + } + + protected Object[][] getRows(UntypedResultSet result) + { + if (result == null) + return new Object[0][]; + + List<Object[]> ret = new ArrayList<>(); + List<ColumnSpecification> meta = result.metadata(); + + Iterator<UntypedResultSet.Row> iter = result.iterator(); + while (iter.hasNext()) + { + UntypedResultSet.Row rowVal = iter.next(); + Object[] row = new Object[meta.size()]; + for (int j = 0; j < meta.size(); j++) + { + ColumnSpecification column = meta.get(j); + ByteBuffer val = rowVal.getBytes(column.name.toString()); + row[j] = val == null ? null : column.type.getSerializer().deserialize(val); + } + + ret.add(row); + } + + Object[][] a = new Object[ret.size()][]; + return ret.toArray(a); + } + protected void assertAllRows(Object[]... rows) throws Throwable { assertRows(execute("SELECT * FROM %s"), rows); @@ -386,6 +524,8 @@ public abstract class CQLTester throw new InvalidRequestException(String.format("Expected empty result but got %d rows", result.size())); } + + protected void assertInvalid(String query, Object... values) throws Throwable { assertInvalidMessage(null, query, values); @@ -393,16 +533,32 @@ public abstract class CQLTester protected void assertInvalidMessage(String errorMessage, String query, Object... values) throws Throwable { + assertInvalidThrowMessage(errorMessage, null, query, values); + } + + protected void assertInvalidThrow(Class<? extends Throwable> exception, String query, Object... values) throws Throwable + { + assertInvalidThrowMessage(null, exception, query, values); + } + + protected void assertInvalidThrowMessage(String errorMessage, Class<? extends Throwable> exception, String query, Object... values) throws Throwable + { try { execute(query, values); String q = USE_PREPARED_VALUES - ? query + " (values: " + formatAllValues(values) + ")" - : replaceValues(query, values); + ? query + " (values: " + formatAllValues(values) + ")" + : replaceValues(query, values); Assert.fail("Query should be invalid but no error was thrown. Query is: " + q); } - catch (InvalidRequestException e) + catch (CassandraException e) { + if (exception != null && !exception.isAssignableFrom(e.getClass())) + { + Assert.fail("Query should be invalid but wrong error was thrown. " + + "Expected: " + exception.getName() + ", got: " + e.getClass().getName() + ". " + + "Query is: " + queryInfo(query, values)); + } if (errorMessage != null) { assertMessageContains(errorMessage, e); @@ -410,6 +566,13 @@ public abstract class CQLTester } } + private static String queryInfo(String query, Object[] values) + { + return USE_PREPARED_VALUES + ? query + " (values: " + formatAllValues(values) + ")" + : replaceValues(query, values); + } + protected void assertInvalidSyntax(String query, Object... values) throws Throwable { assertInvalidSyntaxMessage(null, query, values); @@ -721,12 +884,27 @@ public abstract class CQLTester if (value instanceof Double) return DoubleType.instance; + if (value instanceof BigInteger) + return IntegerType.instance; + + if (value instanceof BigDecimal) + return DecimalType.instance; + if (value instanceof String) return UTF8Type.instance; if (value instanceof Boolean) return BooleanType.instance; + if (value instanceof InetAddress) + return InetAddressType.instance; + + if (value instanceof Date) + return TimestampType.instance; + + if (value instanceof UUID) + return UUIDType.instance; + if (value instanceof List) { List l = (List)value; http://git-wip-us.apache.org/repos/asf/cassandra/blob/f797bfa4/test/unit/org/apache/cassandra/cql3/CollectionsTest.java ---------------------------------------------------------------------- diff --git a/test/unit/org/apache/cassandra/cql3/CollectionsTest.java b/test/unit/org/apache/cassandra/cql3/CollectionsTest.java deleted file mode 100644 index 53895d0..0000000 --- a/test/unit/org/apache/cassandra/cql3/CollectionsTest.java +++ /dev/null @@ -1,240 +0,0 @@ -/* - * 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 org.junit.Test; - -public class CollectionsTest extends CQLTester -{ - @Test - public void testMapBulkRemoval() throws Throwable - { - createTable("CREATE TABLE %s (k int PRIMARY KEY, m map<text, text>)"); - - execute("INSERT INTO %s(k, m) VALUES (?, ?)", 0, map("k1", "v1", "k2", "v2", "k3", "v3")); - - assertRows(execute("SELECT * FROM %s"), - row(0, map("k1", "v1", "k2", "v2", "k3", "v3")) - ); - - execute("UPDATE %s SET m = m - ? WHERE k = ?", set("k2"), 0); - - assertRows(execute("SELECT * FROM %s"), - row(0, map("k1", "v1", "k3", "v3")) - ); - - execute("UPDATE %s SET m = m + ?, m = m - ? WHERE k = ?", map("k4", "v4"), set("k3"), 0); - - assertRows(execute("SELECT * FROM %s"), - row(0, map("k1", "v1", "k4", "v4")) - ); - } - - @Test - public void testInvalidCollectionsMix() throws Throwable - { - createTable("CREATE TABLE %s (k int PRIMARY KEY, l list<text>, s set<text>, m map<text, text>)"); - - // Note: we force the non-prepared form for some of those tests because a list and a set - // have the same serialized format in practice and CQLTester don't validate that the type - // of what's passed as a value in the prepared case, so the queries would work (which is ok, - // CQLTester is just a "dumb" client). - - assertInvalid("UPDATE %s SET l = l + { 'a', 'b' } WHERE k = 0"); - assertInvalid("UPDATE %s SET l = l - { 'a', 'b' } WHERE k = 0"); - assertInvalid("UPDATE %s SET l = l + ? WHERE k = 0", map("a", "b", "c", "d")); - assertInvalid("UPDATE %s SET l = l - ? WHERE k = 0", map("a", "b", "c", "d")); - - assertInvalid("UPDATE %s SET s = s + [ 'a', 'b' ] WHERE k = 0"); - assertInvalid("UPDATE %s SET s = s - [ 'a', 'b' ] WHERE k = 0"); - assertInvalid("UPDATE %s SET s = s + ? WHERE k = 0", map("a", "b", "c", "d")); - assertInvalid("UPDATE %s SET s = s - ? WHERE k = 0", map("a", "b", "c", "d")); - - assertInvalid("UPDATE %s SET m = m + ? WHERE k = 0", list("a", "b")); - assertInvalid("UPDATE %s SET m = m - [ 'a', 'b' ] WHERE k = 0"); - assertInvalid("UPDATE %s SET m = m + ? WHERE k = 0", set("a", "b")); - assertInvalid("UPDATE %s SET m = m - ? WHERE k = 0", map("a", "b", "c", "d")); - } - - @Test - public void testSets() throws Throwable - { - createTable("CREATE TABLE %s (k int PRIMARY KEY, s set<text>)"); - - execute("INSERT INTO %s(k, s) VALUES (0, ?)", set("v1", "v2", "v3", "v4")); - - assertRows(execute("SELECT s FROM %s WHERE k = 0"), - row(set("v1", "v2", "v3", "v4")) - ); - - execute("DELETE s[?] FROM %s WHERE k = 0", "v1"); - - assertRows(execute("SELECT s FROM %s WHERE k = 0"), - row(set("v2", "v3", "v4")) - ); - - // Full overwrite - execute("UPDATE %s SET s = ? WHERE k = 0", set("v6", "v5")); - - assertRows(execute("SELECT s FROM %s WHERE k = 0"), - row(set("v5", "v6")) - ); - - execute("UPDATE %s SET s = s + ? WHERE k = 0", set("v7")); - - assertRows(execute("SELECT s FROM %s WHERE k = 0"), - row(set("v5", "v6", "v7")) - ); - - execute("UPDATE %s SET s = s - ? WHERE k = 0", set("v6", "v5")); - - assertRows(execute("SELECT s FROM %s WHERE k = 0"), - row(set("v7")) - ); - - execute("DELETE s[?] FROM %s WHERE k = 0", set("v7")); - - // Deleting an element that does not exist will succeed - execute("DELETE s[?] FROM %s WHERE k = 0", set("v7")); - - execute("DELETE s FROM %s WHERE k = 0"); - - assertRows(execute("SELECT s FROM %s WHERE k = 0"), - row((Object)null) - ); - } - - @Test - public void testMaps() throws Throwable - { - createTable("CREATE TABLE %s (k int PRIMARY KEY, m map<text, int>)"); - - execute("INSERT INTO %s(k, m) VALUES (0, ?)", map("v1", 1, "v2", 2)); - - assertRows(execute("SELECT m FROM %s WHERE k = 0"), - row(map("v1", 1, "v2", 2)) - ); - - execute("UPDATE %s SET m[?] = ?, m[?] = ? WHERE k = 0", "v3", 3, "v4", 4); - - assertRows(execute("SELECT m FROM %s WHERE k = 0"), - row(map("v1", 1, "v2", 2, "v3", 3, "v4", 4)) - ); - - execute("DELETE m[?] FROM %s WHERE k = 0", "v1"); - - assertRows(execute("SELECT m FROM %s WHERE k = 0"), - row(map("v2", 2, "v3", 3, "v4", 4)) - ); - - // Full overwrite - execute("UPDATE %s SET m = ? WHERE k = 0", map("v6", 6, "v5", 5)); - - assertRows(execute("SELECT m FROM %s WHERE k = 0"), - row(map("v5", 5, "v6", 6)) - ); - - execute("UPDATE %s SET m = m + ? WHERE k = 0", map("v7", 7)); - - assertRows(execute("SELECT m FROM %s WHERE k = 0"), - row(map("v5", 5, "v6", 6, "v7", 7)) - ); - - execute("DELETE m[?] FROM %s WHERE k = 0", "v7"); - - assertRows(execute("SELECT m FROM %s WHERE k = 0"), - row(map("v5", 5, "v6", 6)) - ); - - execute("DELETE m[?] FROM %s WHERE k = 0", "v6"); - - assertRows(execute("SELECT m FROM %s WHERE k = 0"), - row(map("v5", 5)) - ); - - execute("DELETE m[?] FROM %s WHERE k = 0", "v5"); - - assertRows(execute("SELECT m FROM %s WHERE k = 0"), - row((Object)null) - ); - - // Deleting a non-existing key should succeed - execute("DELETE m[?] FROM %s WHERE k = 0", "v5"); - - assertRows(execute("SELECT m FROM %s WHERE k = 0"), - row((Object) null) - ); - - // The empty map is parsed as an empty set (because we don't have enough info at parsing - // time when we see a {}) and special cased later. This test checks this work properly - execute("UPDATE %s SET m = {} WHERE k = 0"); - - assertRows(execute("SELECT m FROM %s WHERE k = 0"), - row((Object)null) - ); - } - - @Test - public void testLists() throws Throwable - { - createTable("CREATE TABLE %s (k int PRIMARY KEY, l list<text>)"); - - execute("INSERT INTO %s(k, l) VALUES (0, ?)", list("v1", "v2", "v3")); - - assertRows(execute("SELECT l FROM %s WHERE k = 0"), row(list("v1", "v2", "v3"))); - - execute("DELETE l[?] FROM %s WHERE k = 0", 1); - - assertRows(execute("SELECT l FROM %s WHERE k = 0"), row(list("v1", "v3"))); - - execute("UPDATE %s SET l[?] = ? WHERE k = 0", 1, "v4"); - - assertRows(execute("SELECT l FROM %s WHERE k = 0"), row(list("v1", "v4"))); - - // Full overwrite - execute("UPDATE %s SET l = ? WHERE k = 0", list("v6", "v5")); - - assertRows(execute("SELECT l FROM %s WHERE k = 0"), row(list("v6", "v5"))); - - execute("UPDATE %s SET l = l + ? WHERE k = 0", list("v7", "v8")); - - assertRows(execute("SELECT l FROM %s WHERE k = 0"), row(list("v6", "v5", "v7", "v8"))); - - execute("UPDATE %s SET l = ? + l WHERE k = 0", list("v9")); - - assertRows(execute("SELECT l FROM %s WHERE k = 0"), row(list("v9", "v6", "v5", "v7", "v8"))); - - execute("UPDATE %s SET l = l - ? WHERE k = 0", list("v5", "v8")); - - assertRows(execute("SELECT l FROM %s WHERE k = 0"), row(list("v9", "v6", "v7"))); - - execute("DELETE l FROM %s WHERE k = 0"); - - assertRows(execute("SELECT l FROM %s WHERE k = 0"), row((Object) null)); - - assertInvalidMessage("Attempted to delete an element from a list which is null", - "DELETE l[0] FROM %s WHERE k=0 "); - - assertInvalidMessage("Attempted to set an element on a list which is null", - "UPDATE %s SET l[0] = ? WHERE k=0", list("v10")); - - execute("UPDATE %s SET l = l - ? WHERE k=0 ", list("v11")); - - assertRows(execute("SELECT l FROM %s WHERE k = 0"), row((Object) null)); - } -} http://git-wip-us.apache.org/repos/asf/cassandra/blob/f797bfa4/test/unit/org/apache/cassandra/cql3/ContainsRelationTest.java ---------------------------------------------------------------------- diff --git a/test/unit/org/apache/cassandra/cql3/ContainsRelationTest.java b/test/unit/org/apache/cassandra/cql3/ContainsRelationTest.java deleted file mode 100644 index b51a639..0000000 --- a/test/unit/org/apache/cassandra/cql3/ContainsRelationTest.java +++ /dev/null @@ -1,269 +0,0 @@ -/* - * 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 org.junit.Test; - -public class ContainsRelationTest extends CQLTester -{ - @Test - public void testSetContains() throws Throwable - { - createTable("CREATE TABLE %s (account text, id int, categories set<text>, PRIMARY KEY (account, id))"); - createIndex("CREATE INDEX ON %s(categories)"); - - execute("INSERT INTO %s (account, id , categories) VALUES (?, ?, ?)", "test", 5, set("lmn")); - - assertEmpty(execute("SELECT * FROM %s WHERE account = ? AND categories CONTAINS ?", "xyz", "lmn")); - - assertRows(execute("SELECT * FROM %s WHERE categories CONTAINS ?", "lmn"), - row("test", 5, set("lmn")) - ); - - assertRows(execute("SELECT * FROM %s WHERE account = ? AND categories CONTAINS ?", "test", "lmn"), - row("test", 5, set("lmn")) - ); - - assertRows(execute("SELECT * FROM %s WHERE account = ? AND id = ? AND categories CONTAINS ?", "test", 5, "lmn"), - row("test", 5, set("lmn")) - ); - - assertInvalid("SELECT * FROM %s WHERE account = ? AND categories CONTAINS ? AND categories CONTAINS ?", "xyz", "lmn", "notPresent"); - assertEmpty(execute("SELECT * FROM %s WHERE account = ? AND categories CONTAINS ? AND categories CONTAINS ? ALLOW FILTERING", "xyz", "lmn", "notPresent")); - } - - @Test - public void testListContains() throws Throwable - { - createTable("CREATE TABLE %s (account text, id int, categories list<text>, PRIMARY KEY (account, id))"); - createIndex("CREATE INDEX ON %s(categories)"); - - execute("INSERT INTO %s (account, id , categories) VALUES (?, ?, ?)", "test", 5, list("lmn")); - - assertEmpty(execute("SELECT * FROM %s WHERE account = ? AND categories CONTAINS ?", "xyz", "lmn")); - - assertRows(execute("SELECT * FROM %s WHERE account = ? AND categories CONTAINS ?;", "test", "lmn"), - row("test", 5, list("lmn")) - ); - - assertRows(execute("SELECT * FROM %s WHERE categories CONTAINS ?", "lmn"), - row("test", 5, list("lmn")) - ); - - assertRows(execute("SELECT * FROM %s WHERE account = ? AND id = ? AND categories CONTAINS ?;", "test", 5, "lmn"), - row("test", 5, list("lmn")) - ); - - assertInvalid("SELECT * FROM %s WHERE account = ? AND id = ? AND categories CONTAINS ? AND categories CONTAINS ?", - "test", 5, "lmn", "notPresent"); - assertEmpty(execute("SELECT * FROM %s WHERE account = ? AND id = ? AND categories CONTAINS ? AND categories CONTAINS ? ALLOW FILTERING", - "test", 5, "lmn", "notPresent")); - } - - @Test - public void testListContainsWithFiltering() throws Throwable - { - createTable("CREATE TABLE %s (e int PRIMARY KEY, f list<text>, s int)"); - createIndex("CREATE INDEX ON %s(f)"); - for(int i = 0; i < 3; i++) - { - execute("INSERT INTO %s (e, f, s) VALUES (?, ?, ?)", i, list("Dubai"), 4); - } - for(int i = 3; i < 5; i++) - { - execute("INSERT INTO %s (e, f, s) VALUES (?, ?, ?)", i, list("Dubai"), 3); - } - assertRows(execute("SELECT * FROM %s WHERE f CONTAINS ? AND s=? allow filtering", "Dubai", 3), - row(3, list("Dubai"), 3), - row(4, list("Dubai"), 3)); - } - - @Test - public void testMapKeyContains() throws Throwable - { - createTable("CREATE TABLE %s (account text, id int, categories map<text,text>, PRIMARY KEY (account, id))"); - createIndex("CREATE INDEX ON %s(keys(categories))"); - - execute("INSERT INTO %s (account, id , categories) VALUES (?, ?, ?)", "test", 5, map("lmn", "foo")); - - assertEmpty(execute("SELECT * FROM %s WHERE account = ? AND categories CONTAINS KEY ?", "xyz", "lmn")); - - assertRows(execute("SELECT * FROM %s WHERE account = ? AND categories CONTAINS KEY ?", "test", "lmn"), - row("test", 5, map("lmn", "foo")) - ); - assertRows(execute("SELECT * FROM %s WHERE categories CONTAINS KEY ?", "lmn"), - row("test", 5, map("lmn", "foo")) - ); - - assertRows(execute("SELECT * FROM %s WHERE account = ? AND id = ? AND categories CONTAINS KEY ?", "test", 5, "lmn"), - row("test", 5, map("lmn", "foo")) - ); - - assertInvalid("SELECT * FROM %s WHERE account = ? AND id = ? AND categories CONTAINS KEY ? AND categories CONTAINS KEY ?", - "test", 5, "lmn", "notPresent"); - assertEmpty(execute("SELECT * FROM %s WHERE account = ? AND id = ? AND categories CONTAINS KEY ? AND categories CONTAINS KEY ? ALLOW FILTERING", - "test", 5, "lmn", "notPresent")); - - assertInvalid("SELECT * FROM %s WHERE account = ? AND id = ? AND categories CONTAINS KEY ? AND categories CONTAINS ?", - "test", 5, "lmn", "foo"); - } - - @Test - public void testMapValueContains() throws Throwable - { - createTable("CREATE TABLE %s (account text, id int, categories map<text,text>, PRIMARY KEY (account, id))"); - createIndex("CREATE INDEX ON %s(categories)"); - - execute("INSERT INTO %s (account, id , categories) VALUES (?, ?, ?)", "test", 5, map("lmn", "foo")); - - assertEmpty(execute("SELECT * FROM %s WHERE account = ? AND categories CONTAINS ?", "xyz", "foo")); - - assertRows(execute("SELECT * FROM %s WHERE account = ? AND categories CONTAINS ?", "test", "foo"), - row("test", 5, map("lmn", "foo")) - ); - - assertRows(execute("SELECT * FROM %s WHERE categories CONTAINS ?", "foo"), - row("test", 5, map("lmn", "foo")) - ); - - assertRows(execute("SELECT * FROM %s WHERE account = ? AND id = ? AND categories CONTAINS ?", "test", 5, "foo"), - row("test", 5, map("lmn", "foo")) - ); - - assertInvalid("SELECT * FROM %s WHERE account = ? AND id = ? AND categories CONTAINS ? AND categories CONTAINS ?" - , "test", 5, "foo", "notPresent"); - - assertEmpty(execute("SELECT * FROM %s WHERE account = ? AND id = ? AND categories CONTAINS ? AND categories CONTAINS ? ALLOW FILTERING" - , "test", 5, "foo", "notPresent")); - } - - // See CASSANDRA-7525 - @Test - public void testQueryMultipleIndexTypes() throws Throwable - { - createTable("CREATE TABLE %s (account text, id int, categories map<text,text>, PRIMARY KEY (account, id))"); - - // create an index on - createIndex("CREATE INDEX id_index ON %s(id)"); - createIndex("CREATE INDEX categories_values_index ON %s(categories)"); - - execute("INSERT INTO %s (account, id , categories) VALUES (?, ?, ?)", "test", 5, map("lmn", "foo")); - - assertRows(execute("SELECT * FROM %s WHERE categories CONTAINS ? AND id = ? ALLOW FILTERING", "foo", 5), - row("test", 5, map("lmn", "foo")) - ); - - assertRows( - execute("SELECT * FROM %s WHERE account = ? AND categories CONTAINS ? AND id = ? ALLOW FILTERING", "test", "foo", 5), - row("test", 5, map("lmn", "foo")) - ); - } - - // See CASSANDRA-8033 - @Test - public void testFilterForContains() throws Throwable - { - createTable("CREATE TABLE %s (k1 int, k2 int, v set<int>, PRIMARY KEY ((k1, k2)))"); - createIndex("CREATE INDEX ON %s(k2)"); - - execute("INSERT INTO %s (k1, k2, v) VALUES (?, ?, ?)", 0, 0, set(1, 2, 3)); - execute("INSERT INTO %s (k1, k2, v) VALUES (?, ?, ?)", 0, 1, set(2, 3, 4)); - execute("INSERT INTO %s (k1, k2, v) VALUES (?, ?, ?)", 1, 0, set(3, 4, 5)); - execute("INSERT INTO %s (k1, k2, v) VALUES (?, ?, ?)", 1, 1, set(4, 5, 6)); - - assertRows(execute("SELECT * FROM %s WHERE k2 = ?", 1), - row(0, 1, set(2, 3, 4)), - row(1, 1, set(4, 5, 6)) - ); - - assertRows(execute("SELECT * FROM %s WHERE k2 = ? AND v CONTAINS ? ALLOW FILTERING", 1, 6), - row(1, 1, set(4, 5, 6)) - ); - - assertEmpty(execute("SELECT * FROM %s WHERE k2 = ? AND v CONTAINS ? ALLOW FILTERING", 1, 7)); - } - - // See CASSANDRA-8073 - @Test - public void testIndexLookupWithClusteringPrefix() throws Throwable - { - createTable("CREATE TABLE %s (a int, b int, c int, d set<int>, PRIMARY KEY (a, b, c))"); - createIndex("CREATE INDEX ON %s(d)"); - execute("INSERT INTO %s (a, b, c, d) VALUES (?, ?, ?, ?)", 0, 0, 0, set(1, 2, 3)); - execute("INSERT INTO %s (a, b, c, d) VALUES (?, ?, ?, ?)", 0, 0, 1, set(3, 4, 5)); - execute("INSERT INTO %s (a, b, c, d) VALUES (?, ?, ?, ?)", 0, 1, 0, set(1, 2, 3)); - execute("INSERT INTO %s (a, b, c, d) VALUES (?, ?, ?, ?)", 0, 1, 1, set(3, 4, 5)); - - assertRows(execute("SELECT * FROM %s WHERE a=? AND b=? AND d CONTAINS ?", 0, 1, 3), - row(0, 1, 0, set(1, 2, 3)), - row(0, 1, 1, set(3, 4, 5)) - ); - - assertRows(execute("SELECT * FROM %s WHERE a=? AND b=? AND d CONTAINS ?", 0, 1, 2), - row(0, 1, 0, set(1, 2, 3)) - ); - - assertRows(execute("SELECT * FROM %s WHERE a=? AND b=? AND d CONTAINS ?", 0, 1, 5), - row(0, 1, 1, set(3, 4, 5)) - ); - } - - @Test - public void testContainsKeyAndContainsWithIndexOnMapKey() throws Throwable - { - createTable("CREATE TABLE %s (account text, id int, categories map<text,text>, PRIMARY KEY (account, id))"); - createIndex("CREATE INDEX ON %s(keys(categories))"); - - execute("INSERT INTO %s (account, id , categories) VALUES (?, ?, ?)", "test", 5, map("lmn", "foo")); - execute("INSERT INTO %s (account, id , categories) VALUES (?, ?, ?)", "test", 6, map("lmn", "foo2")); - - assertInvalid("SELECT * FROM %s WHERE account = ? AND categories CONTAINS ?", "test", "foo"); - - assertRows(execute("SELECT * FROM %s WHERE account = ? AND categories CONTAINS KEY ?", "test", "lmn"), - row("test", 5, map("lmn", "foo")), - row("test", 6, map("lmn", "foo2"))); - assertRows(execute("SELECT * FROM %s WHERE account = ? AND categories CONTAINS KEY ? AND categories CONTAINS ? ALLOW FILTERING", - "test", "lmn", "foo"), - row("test", 5, map("lmn", "foo"))); - assertRows(execute("SELECT * FROM %s WHERE account = ? AND categories CONTAINS ? AND categories CONTAINS KEY ? ALLOW FILTERING", - "test", "foo", "lmn"), - row("test", 5, map("lmn", "foo"))); - } - - @Test - public void testContainsKeyAndContainsWithIndexOnMapValue() throws Throwable - { - createTable("CREATE TABLE %s (account text, id int, categories map<text,text>, PRIMARY KEY (account, id))"); - createIndex("CREATE INDEX ON %s(categories)"); - - execute("INSERT INTO %s (account, id , categories) VALUES (?, ?, ?)", "test", 5, map("lmn", "foo")); - execute("INSERT INTO %s (account, id , categories) VALUES (?, ?, ?)", "test", 6, map("lmn2", "foo")); - - assertInvalid("SELECT * FROM %s WHERE account = ? AND categories CONTAINS KEY ?", "test", "lmn"); - - assertRows(execute("SELECT * FROM %s WHERE account = ? AND categories CONTAINS ?", "test", "foo"), - row("test", 5, map("lmn", "foo")), - row("test", 6, map("lmn2", "foo"))); - assertRows(execute("SELECT * FROM %s WHERE account = ? AND categories CONTAINS KEY ? AND categories CONTAINS ? ALLOW FILTERING", - "test", "lmn", "foo"), - row("test", 5, map("lmn", "foo"))); - assertRows(execute("SELECT * FROM %s WHERE account = ? AND categories CONTAINS ? AND categories CONTAINS KEY ? ALLOW FILTERING", - "test", "foo", "lmn"), - row("test", 5, map("lmn", "foo"))); - } -} http://git-wip-us.apache.org/repos/asf/cassandra/blob/f797bfa4/test/unit/org/apache/cassandra/cql3/CrcCheckChanceTest.java ---------------------------------------------------------------------- diff --git a/test/unit/org/apache/cassandra/cql3/CrcCheckChanceTest.java b/test/unit/org/apache/cassandra/cql3/CrcCheckChanceTest.java deleted file mode 100644 index bed3cdd..0000000 --- a/test/unit/org/apache/cassandra/cql3/CrcCheckChanceTest.java +++ /dev/null @@ -1,159 +0,0 @@ -/* - * 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 java.util.List; -import java.util.concurrent.ExecutionException; -import java.util.concurrent.Future; - -import junit.framework.Assert; -import org.apache.cassandra.config.DatabaseDescriptor; -import org.apache.cassandra.db.ColumnFamilyStore; -import org.apache.cassandra.db.Keyspace; -import org.apache.cassandra.db.compaction.CompactionInterruptedException; -import org.apache.cassandra.db.compaction.CompactionManager; -import org.apache.cassandra.utils.FBUtilities; - -import org.junit.Test; - - -public class CrcCheckChanceTest extends CQLTester -{ - @Test - public void testChangingCrcCheckChance() throws Throwable - { - //Start with crc_check_chance of 99% - createTable("CREATE TABLE %s (p text, c text, v text, s text static, PRIMARY KEY (p, c)) WITH compression = {'sstable_compression': 'LZ4Compressor', 'crc_check_chance' : 0.99}"); - - execute("CREATE INDEX foo ON %s(v)"); - - execute("INSERT INTO %s(p, c, v, s) values (?, ?, ?, ?)", "p1", "k1", "v1", "sv1"); - execute("INSERT INTO %s(p, c, v) values (?, ?, ?)", "p1", "k2", "v2"); - execute("INSERT INTO %s(p, s) values (?, ?)", "p2", "sv2"); - - - ColumnFamilyStore cfs = Keyspace.open(CQLTester.KEYSPACE).getColumnFamilyStore(currentTable()); - ColumnFamilyStore indexCfs = cfs.indexManager.getIndexesBackedByCfs().iterator().next(); - cfs.forceBlockingFlush(); - - Assert.assertEquals(0.99, cfs.metadata.compressionParameters.getCrcCheckChance()); - Assert.assertEquals(0.99, cfs.getSSTables().iterator().next().getCompressionMetadata().parameters.getCrcCheckChance()); - Assert.assertEquals(0.99, indexCfs.metadata.compressionParameters.getCrcCheckChance()); - Assert.assertEquals(0.99, indexCfs.getSSTables().iterator().next().getCompressionMetadata().parameters.getCrcCheckChance()); - - //Test for stack overflow - cfs.setCrcCheckChance(0.99); - - assertRows(execute("SELECT * FROM %s WHERE p=?", "p1"), - row("p1", "k1", "sv1", "v1"), - row("p1", "k2", "sv1", "v2") - ); - - assertRows(execute("SELECT * FROM %s WHERE v=?", "v1"), - row("p1", "k1", "sv1", "v1") - ); - - - - //Write a few SSTables then Compact - - execute("INSERT INTO %s(p, c, v, s) values (?, ?, ?, ?)", "p1", "k1", "v1", "sv1"); - execute("INSERT INTO %s(p, c, v) values (?, ?, ?)", "p1", "k2", "v2"); - execute("INSERT INTO %s(p, s) values (?, ?)", "p2", "sv2"); - - cfs.forceBlockingFlush(); - - - execute("INSERT INTO %s(p, c, v, s) values (?, ?, ?, ?)", "p1", "k1", "v1", "sv1"); - execute("INSERT INTO %s(p, c, v) values (?, ?, ?)", "p1", "k2", "v2"); - execute("INSERT INTO %s(p, s) values (?, ?)", "p2", "sv2"); - - cfs.forceBlockingFlush(); - - execute("INSERT INTO %s(p, c, v, s) values (?, ?, ?, ?)", "p1", "k1", "v1", "sv1"); - execute("INSERT INTO %s(p, c, v) values (?, ?, ?)", "p1", "k2", "v2"); - execute("INSERT INTO %s(p, s) values (?, ?)", "p2", "sv2"); - - cfs.forceBlockingFlush(); - - cfs.forceMajorCompaction(); - - //Verify when we alter the value the live sstable readers hold the new one - alterTable("ALTER TABLE %s WITH compression = {'sstable_compression': 'LZ4Compressor', 'crc_check_chance': 0.01}"); - - Assert.assertEquals( 0.01, cfs.metadata.compressionParameters.getCrcCheckChance()); - Assert.assertEquals( 0.01, cfs.getSSTables().iterator().next().getCompressionMetadata().parameters.getCrcCheckChance()); - Assert.assertEquals( 0.01, indexCfs.metadata.compressionParameters.getCrcCheckChance()); - Assert.assertEquals( 0.01, indexCfs.getSSTables().iterator().next().getCompressionMetadata().parameters.getCrcCheckChance()); - - assertRows(execute("SELECT * FROM %s WHERE p=?", "p1"), - row("p1", "k1", "sv1", "v1"), - row("p1", "k2", "sv1", "v2") - ); - - assertRows(execute("SELECT * FROM %s WHERE v=?", "v1"), - row("p1", "k1", "sv1", "v1") - ); - - - //Verify the call used by JMX still works - cfs.setCrcCheckChance(0.03); - Assert.assertEquals( 0.03, cfs.metadata.compressionParameters.getCrcCheckChance()); - Assert.assertEquals( 0.03, cfs.getSSTables().iterator().next().getCompressionMetadata().parameters.getCrcCheckChance()); - Assert.assertEquals( 0.03, indexCfs.metadata.compressionParameters.getCrcCheckChance()); - Assert.assertEquals( 0.03, indexCfs.getSSTables().iterator().next().getCompressionMetadata().parameters.getCrcCheckChance()); - - } - - - @Test - public void testDropDuringCompaction() throws Throwable - { - CompactionManager.instance.disableAutoCompaction(); - - //Start with crc_check_chance of 99% - createTable("CREATE TABLE %s (p text, c text, v text, s text static, PRIMARY KEY (p, c)) WITH compression = {'sstable_compression': 'LZ4Compressor', 'crc_check_chance' : 0.99}"); - - ColumnFamilyStore cfs = Keyspace.open(CQLTester.KEYSPACE).getColumnFamilyStore(currentTable()); - - //Write a few SSTables then Compact, and drop - for (int i = 0; i < 100; i++) - { - execute("INSERT INTO %s(p, c, v, s) values (?, ?, ?, ?)", "p1", "k1", "v1", "sv1"); - execute("INSERT INTO %s(p, c, v) values (?, ?, ?)", "p1", "k2", "v2"); - execute("INSERT INTO %s(p, s) values (?, ?)", "p2", "sv2"); - - cfs.forceBlockingFlush(); - } - - DatabaseDescriptor.setCompactionThroughputMbPerSec(1); - List<Future<?>> futures = CompactionManager.instance.submitMaximal(cfs, CompactionManager.GC_ALL); - execute("DROP TABLE %s"); - - try - { - FBUtilities.waitOnFutures(futures); - } - catch (Throwable t) - { - if (!(t.getCause() instanceof ExecutionException) || !(t.getCause().getCause() instanceof CompactionInterruptedException)) - throw t; - } - } -} - http://git-wip-us.apache.org/repos/asf/cassandra/blob/f797bfa4/test/unit/org/apache/cassandra/cql3/CreateAndAlterKeyspaceTest.java ---------------------------------------------------------------------- diff --git a/test/unit/org/apache/cassandra/cql3/CreateAndAlterKeyspaceTest.java b/test/unit/org/apache/cassandra/cql3/CreateAndAlterKeyspaceTest.java deleted file mode 100644 index 9e0ca21..0000000 --- a/test/unit/org/apache/cassandra/cql3/CreateAndAlterKeyspaceTest.java +++ /dev/null @@ -1,37 +0,0 @@ -/* - * 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 org.junit.Test; - -public class CreateAndAlterKeyspaceTest extends CQLTester -{ - @Test - // tests CASSANDRA-9565 - public void testCreateAndAlterWithDoubleWith() throws Throwable - { - String[] stmts = new String[] {"ALTER KEYSPACE WITH WITH DURABLE_WRITES = true", - "ALTER KEYSPACE ks WITH WITH DURABLE_WRITES = true", - "CREATE KEYSPACE WITH WITH DURABLE_WRITES = true", - "CREATE KEYSPACE ks WITH WITH DURABLE_WRITES = true"}; - - for (String stmt : stmts) { - assertInvalidSyntaxMessage("no viable alternative at input 'WITH'", stmt); - } - } -}
