dcapwell commented on code in PR #3785: URL: https://github.com/apache/cassandra/pull/3785#discussion_r1930935605
########## test/distributed/org/apache/cassandra/distributed/test/cql3/StatefulASTBase.java: ########## @@ -0,0 +1,502 @@ +/* + * 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.distributed.test.cql3; + +import java.io.IOException; +import java.net.InetSocketAddress; +import java.nio.ByteBuffer; +import java.util.ArrayList; +import java.util.EnumSet; +import java.util.List; +import java.util.Objects; +import java.util.Set; +import java.util.concurrent.atomic.AtomicInteger; +import java.util.function.Consumer; +import java.util.stream.Collectors; +import java.util.stream.Stream; +import javax.annotation.Nullable; + +import com.google.common.collect.ImmutableSet; +import org.slf4j.Logger; + +import accord.utils.Gen; +import accord.utils.Gens; +import accord.utils.Property; +import accord.utils.RandomSource; +import com.datastax.driver.core.ColumnDefinitions; +import com.datastax.driver.core.ResultSet; +import com.datastax.driver.core.Row; +import com.datastax.driver.core.Session; +import com.datastax.driver.core.SimpleStatement; +import org.apache.cassandra.config.CassandraRelevantProperties; +import org.apache.cassandra.config.DatabaseDescriptor; +import org.apache.cassandra.cql3.ast.Bind; +import org.apache.cassandra.cql3.ast.CQLFormatter; +import org.apache.cassandra.cql3.ast.Conditional; +import org.apache.cassandra.cql3.ast.Literal; +import org.apache.cassandra.cql3.ast.Mutation; +import org.apache.cassandra.cql3.ast.Select; +import org.apache.cassandra.cql3.ast.StandardVisitors; +import org.apache.cassandra.cql3.ast.Statement; +import org.apache.cassandra.cql3.ast.TableReference; +import org.apache.cassandra.cql3.ast.Value; +import org.apache.cassandra.cql3.ast.Visitor; +import org.apache.cassandra.db.marshal.AbstractType; +import org.apache.cassandra.db.marshal.AsciiType; +import org.apache.cassandra.db.marshal.BytesType; +import org.apache.cassandra.db.marshal.DecimalType; +import org.apache.cassandra.db.marshal.InetAddressType; +import org.apache.cassandra.db.marshal.IntegerType; +import org.apache.cassandra.db.marshal.LongType; +import org.apache.cassandra.db.marshal.StringType; +import org.apache.cassandra.db.marshal.UTF8Type; +import org.apache.cassandra.distributed.Cluster; +import org.apache.cassandra.distributed.api.ConsistencyLevel; +import org.apache.cassandra.distributed.api.Feature; +import org.apache.cassandra.distributed.api.IInstance; +import org.apache.cassandra.distributed.api.IInstanceConfig; +import org.apache.cassandra.distributed.test.JavaDriverUtils; +import org.apache.cassandra.distributed.test.TestBaseImpl; +import org.apache.cassandra.harry.gen.BijectionCache; +import org.apache.cassandra.harry.model.ASTSingleTableModel; +import org.apache.cassandra.harry.util.StringUtils; +import org.apache.cassandra.schema.TableMetadata; +import org.apache.cassandra.utils.AbstractTypeGenerators; +import org.apache.cassandra.utils.CassandraGenerators; +import org.apache.cassandra.utils.FastByteOperations; +import org.apache.cassandra.utils.Generators; +import org.quicktheories.generators.SourceDSL; + +import static org.apache.cassandra.utils.AbstractTypeGenerators.overridePrimitiveTypeSupport; +import static org.apache.cassandra.utils.AbstractTypeGenerators.stringComparator; + +public class StatefulASTBase extends TestBaseImpl +{ + protected enum KnownIssue + { + BETWEEN_START_LARGER_THAN_END("https://issues.apache.org/jira/browse/CASSANDRA-20154", + "BETWEEN is matching values when start > end, which should never return anything"), + VECTOR_AND_UDT_LITERAL("https://issues.apache.org/jira/browse/CASSANDRA-19889", + "UDT and Vector type don't unwrap the type, which leads to literal validation failing as it thinks the type isn't a UDT... its a reverse UDT!"), + ALLOW_FILTER_WITH_DELETED_COLUMNS("https://issues.apache.org/jira/browse/CASSANDRA-20108", + "When doing an ALLOW FILTER query, if column deletes or TTL are found this will lead to a failure to take an empty buffer and return the data from the expected type"), + SAI_INET_MIXED("https://issues.apache.org/jira/browse/CASSANDRA-19492", + "SAI converts ipv4 to ipv6 to simplify the index, this causes issues with range search as it starts to mix the values, which isn't always desirable or intuative"), + SAI_REVERSE_OF_FIXED_LENGTH("https://issues.apache.org/jira/browse/CASSANDRA-20100", + "Fixed length values have their bytes reversed when storing to disk, which leads to finding incorrect values when doing a normal index search"), + CUSTOM_INDEX_MAX_COLUMN_48("https://issues.apache.org/jira/browse/CASSANDRA-19897", + "Columns can be up to 50 chars, but CREATE CUSTOM INDEX only allows up to 48"), + AF_MULTI_NODE_AND_NODE_LOCAL_WRITES("https://issues.apache.org/jira/browse/CASSANDRA-20243", + "When writes are done at NODE_LOCAL and the select is ALL, AF should be able to return the correct data but it doesn't") + ; + + KnownIssue(String url, String description) + { + // don't actually care to save the values, just there to force documentation + } + } + protected static final EnumSet<KnownIssue> IGNORED_ISSUES = EnumSet.allOf(KnownIssue.class); + protected static final Set<AbstractType<?>> SAI_REVERSE_OF_FIXED_LENGTH_TYPES = ImmutableSet.of(InetAddressType.instance, + LongType.instance, + IntegerType.instance, + DecimalType.instance + ); + /** + * Sometimes only add noise and make the history hard to read, so by default a Harry descriptor is used for problamtic types (such as text/blob). + * If this behavior isn't desired while debugging flipping this to true will show the CQL literal. + * + * NOTE: Sometimes text literals may not look the same when comparing, but they actually are... this can get tricky + * to spot as text editors (such as Intellij) may show different strings! This is the main this flag was added... + * to avoid this confussion while debugging! + */ + protected static boolean SHOW_REAL_VALUES = false; + + protected static final Gen<Gen<Boolean>> BIND_OR_LITERAL_DISTRO = Gens.bools().mixedDistribution(); + protected static final Gen<Gen<Boolean>> BETWEEN_EQ_DISTRO = Gens.bools().mixedDistribution(); + protected static final Gen<Gen<Conditional.Where.Inequality>> LESS_THAN_DISTRO = Gens.mixedDistribution(Stream.of(Conditional.Where.Inequality.values()) + .filter(i -> i == Conditional.Where.Inequality.LESS_THAN || i == Conditional.Where.Inequality.LESS_THAN_EQ) + .collect(Collectors.toList())); + protected static final Gen<Gen<Conditional.Where.Inequality>> GREATER_THAN_DISTRO = Gens.mixedDistribution(Stream.of(Conditional.Where.Inequality.values()) + .filter(i -> i == Conditional.Where.Inequality.GREATER_THAN || i == Conditional.Where.Inequality.GREATER_THAN_EQ) + .collect(Collectors.toList())); + protected static final Gen<Gen<Conditional.Where.Inequality>> RANGE_INEQUALITY_DISTRO = Gens.mixedDistribution(Stream.of(Conditional.Where.Inequality.values()) + .filter(i -> i != Conditional.Where.Inequality.EQUAL && i != Conditional.Where.Inequality.NOT_EQUAL) + .collect(Collectors.toList())); + protected static final Gen<Gen.IntGen> FETCH_SIZE_DISTRO = Gens.mixedDistribution(new int[] {1, 10, 100, 1000, 5000}); + + static + { + // since this test does frequent truncates, the info table gets updated and forced flushed... which is 90% of the cost of this test... + // this flag disables that flush + CassandraRelevantProperties.UNSAFE_SYSTEM.setBoolean(true); + // queries maybe dumb which could lead to performance issues causing timeouts... don't timeout! + CassandraRelevantProperties.SAI_TEST_DISABLE_TIMEOUT.setBoolean(true); + + overridePrimitiveTypeSupport(AsciiType.instance, AbstractTypeGenerators.TypeSupport.of(AsciiType.instance, SourceDSL.strings().ascii().ofLengthBetween(1, 10), stringComparator(AsciiType.instance))); + overridePrimitiveTypeSupport(UTF8Type.instance, AbstractTypeGenerators.TypeSupport.of(UTF8Type.instance, Generators.utf8(1, 10), stringComparator(UTF8Type.instance))); + overridePrimitiveTypeSupport(BytesType.instance, AbstractTypeGenerators.TypeSupport.of(BytesType.instance, Generators.bytes(1, 10), FastByteOperations::compareUnsigned)); + } + + /** + * There is an assumption that keyspace name doesn't impact this test, so to get simpler names use this counter... + * if this assumption doesn't hold, then need to switch to random or rely on DROP KEYSPACE. + */ + private static final AtomicInteger COUNTER = new AtomicInteger(); + + protected static String nextKeyspace() + { + return "ks" + COUNTER.incrementAndGet(); + } + + protected static Cluster createCluster(int nodeCount, Consumer<IInstanceConfig> config) throws IOException + { + Cluster cluster = Cluster.build(nodeCount) + .withConfig(c -> { + c.with(Feature.NATIVE_PROTOCOL, Feature.GOSSIP) + .set("incremental_backups", false); + config.accept(c); + }) + .start(); + // we don't allow setting null in yaml... but these configs support null! + cluster.forEach(i -> i.runOnInstance(() -> { + // When values are large SAI will drop them... soooo... disable that... this test does not care about perf but correctness + DatabaseDescriptor.getRawConfig().sai_frozen_term_size_warn_threshold = null; + DatabaseDescriptor.getRawConfig().sai_frozen_term_size_fail_threshold = null; + })); + return cluster; + } + + protected <S extends BaseState> Property.StatefulSuccess<S, Void> onSuccess(Logger logger) + { + return (state, sut, history) -> logger.info("Successful for the following:\nState {}\nHistory:\n{}", state, Property.formatList("\t\t", history)); + } + + protected static <S extends BaseState> Property.Command<S, Void, ?> flushTable(RandomSource rs, S state) + { + return new Property.SimpleCommand<>("nodetool flush " + state.metadata.keyspace + " " + state.metadata.name, s2 -> { + s2.cluster.forEach(i -> i.nodetoolResult("flush", s2.metadata.keyspace, s2.metadata.name).asserts().success()); + s2.flush(); + }); + } + + protected static <S extends BaseState> Property.Command<S, Void, ?> compactTable(RandomSource rs, S state) + { + return new Property.SimpleCommand<>("nodetool compact " + state.metadata.keyspace + " " + state.metadata.name, s2 -> { + state.cluster.forEach(i -> i.nodetoolResult("compact", s2.metadata.keyspace, s2.metadata.name).asserts().success()); + s2.compact(); + }); + } + + protected static abstract class BaseState implements AutoCloseable + { + protected final RandomSource rs; + protected final Cluster cluster; + protected final com.datastax.driver.core.Cluster client; + protected final Session session; + protected final Gen<Boolean> bindOrLiteralGen; + protected final Gen<Boolean> betweenEqGen; + protected final Gen<Conditional.Where.Inequality> lessThanGen; + protected final Gen<Conditional.Where.Inequality> greaterThanGen; + protected final Gen<Conditional.Where.Inequality> rangeInequalityGen; + protected final Gen.IntGen fetchSizeGen; + protected final TableMetadata metadata; + protected final TableReference tableRef; + protected final ASTSingleTableModel model; + private final @Nullable BijectionCache<ValueWithType> hardForHumans = SHOW_REAL_VALUES ? null : new BijectionCache<>(null); Review Comment: removed this logic -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected] --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]

