You are viewing a plain text version of this content. The canonical link for it is here.
Posted to pr@cassandra.apache.org by "dcapwell (via GitHub)" <gi...@apache.org> on 2024/08/13 21:51:26 UTC

[PR] Improve CQLTester to make it trivial to run the tests with different configs, and to add randomness to the test [cassandra]

dcapwell opened a new pull request, #3474:
URL: https://github.com/apache/cassandra/pull/3474

   (no comment)


-- 
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: pr-unsubscribe@cassandra.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: pr-unsubscribe@cassandra.apache.org
For additional commands, e-mail: pr-help@cassandra.apache.org


Re: [PR] Improve CQLTester to make it trivial to run the tests with different configs, and to add randomness to the test [cassandra]

Posted by "dcapwell (via GitHub)" <gi...@apache.org>.
dcapwell commented on code in PR #3474:
URL: https://github.com/apache/cassandra/pull/3474#discussion_r1715986822


##########
test/unit/org/apache/cassandra/cql3/CQLTester.java:
##########
@@ -420,15 +428,20 @@ public static JMXServiceURL getJMXServiceURL() throws MalformedURLException
      */
     @BeforeClass
     public static void setUpClass()
+    {
+        prePrepareServer();

Review Comment:
   this was pulled out of [CASSANDRA-19830](https://issues.apache.org/jira/browse/CASSANDRA-19830) to avoid conflict once that merges.  This style makes this patch easier, so wanted to match



-- 
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: pr-unsubscribe@cassandra.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: pr-unsubscribe@cassandra.apache.org
For additional commands, e-mail: pr-help@cassandra.apache.org


Re: [PR] Improve CQLTester to make it trivial to run the tests with different configs, and to add randomness to the test [cassandra]

Posted by "dcapwell (via GitHub)" <gi...@apache.org>.
dcapwell commented on code in PR #3474:
URL: https://github.com/apache/cassandra/pull/3474#discussion_r1723579107


##########
test/unit/org/apache/cassandra/utils/CassandraGenerators.java:
##########
@@ -610,6 +628,58 @@ public static Gen<Token> tokensInRange(Range<Token> range)
         throw new UnsupportedOperationException("Unsupported partitioner: " + partitioner.getClass());
     }
 
+    private enum SupportedPartitioners { Murmur, ByteOrdered, Random, Local, OrderPreserving}
+
+    public static Gen<IPartitioner> partitioners()
+    {
+        var pGen = SourceDSL.arbitrary().enumValues(SupportedPartitioners.class);
+        return pGen.flatMap(p -> {
+            switch (p)
+            {
+                case Murmur: return ignore -> Murmur3Partitioner.instance;
+                case ByteOrdered: return ignore -> ByteOrderedPartitioner.instance;
+                case Random: return ignore -> RandomPartitioner.instance;
+                case OrderPreserving: return ignore -> OrderPreservingPartitioner.instance;
+                case Local: return localPartitioner();
+                default: throw new AssertionError("Unknown partition: " + p);
+            }
+        });
+    }
+
+    public static Gen<IPartitioner> nonLocalPartitioners()
+    {
+        var pGen = SourceDSL.arbitrary().enumValues(SupportedPartitioners.class);
+        return pGen.assuming(p -> p != SupportedPartitioners.Local).flatMap(p -> {
+            switch (p)
+            {
+                case Murmur: return ignore -> Murmur3Partitioner.instance;
+                case ByteOrdered: return ignore -> ByteOrderedPartitioner.instance;
+                case Random: return ignore -> RandomPartitioner.instance;
+                case OrderPreserving: return ignore -> OrderPreservingPartitioner.instance;
+                default: throw new AssertionError("Unknown partition: " + p);
+            }
+        });
+    }

Review Comment:
   made the change, good suggestions 



-- 
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: pr-unsubscribe@cassandra.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: pr-unsubscribe@cassandra.apache.org
For additional commands, e-mail: pr-help@cassandra.apache.org


Re: [PR] Improve CQLTester to make it trivial to run the tests with different configs, and to add randomness to the test [cassandra]

Posted by "maedhroz (via GitHub)" <gi...@apache.org>.
maedhroz commented on code in PR #3474:
URL: https://github.com/apache/cassandra/pull/3474#discussion_r1722470137


##########
test/unit/org/apache/cassandra/utils/CassandraGenerators.java:
##########
@@ -610,6 +628,58 @@ public static Gen<Token> tokensInRange(Range<Token> range)
         throw new UnsupportedOperationException("Unsupported partitioner: " + partitioner.getClass());
     }
 
+    private enum SupportedPartitioners { Murmur, ByteOrdered, Random, Local, OrderPreserving}
+
+    public static Gen<IPartitioner> partitioners()
+    {
+        var pGen = SourceDSL.arbitrary().enumValues(SupportedPartitioners.class);
+        return pGen.flatMap(p -> {
+            switch (p)
+            {
+                case Murmur: return ignore -> Murmur3Partitioner.instance;
+                case ByteOrdered: return ignore -> ByteOrderedPartitioner.instance;
+                case Random: return ignore -> RandomPartitioner.instance;
+                case OrderPreserving: return ignore -> OrderPreservingPartitioner.instance;
+                case Local: return localPartitioner();
+                default: throw new AssertionError("Unknown partition: " + p);
+            }
+        });
+    }
+
+    public static Gen<IPartitioner> nonLocalPartitioners()
+    {
+        var pGen = SourceDSL.arbitrary().enumValues(SupportedPartitioners.class);
+        return pGen.assuming(p -> p != SupportedPartitioners.Local).flatMap(p -> {
+            switch (p)
+            {
+                case Murmur: return ignore -> Murmur3Partitioner.instance;
+                case ByteOrdered: return ignore -> ByteOrderedPartitioner.instance;
+                case Random: return ignore -> RandomPartitioner.instance;
+                case OrderPreserving: return ignore -> OrderPreservingPartitioner.instance;
+                default: throw new AssertionError("Unknown partition: " + p);
+            }
+        });
+    }

Review Comment:
   Could always write the above as...
   
   ```
   private enum SupportedPartitioners
   {
       Murmur(ignore -> Murmur3Partitioner.instance),
       ByteOrdered(ignore -> ByteOrderedPartitioner.instance),
       Random(ignore -> RandomPartitioner.instance),
       Local(localPartitioner()),
       OrderPreserving(ignore -> OrderPreservingPartitioner.instance);
   
       private final Gen<IPartitioner> partitioner;
   
       SupportedPartitioners(Gen<IPartitioner> partitioner)
       {
           this.partitioner = partitioner;
       }
       
       public Gen<IPartitioner> partitioner()
       {
           return partitioner;
       }
   }
   
   public static Gen<IPartitioner> partitioners()
   {
       return  SourceDSL.arbitrary().enumValues(SupportedPartitioners.class).flatMap(SupportedPartitioners::partitioner);
   }
   
   public static Gen<IPartitioner> nonLocalPartitioners()
   {
       return SourceDSL.arbitrary().enumValues(SupportedPartitioners.class)
                                   .assuming(p -> p != SupportedPartitioners.Local)
                                   .flatMap(SupportedPartitioners::partitioner);
   }
   ```
   ...to make the `AssertionError`s unnecessary?



-- 
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: pr-unsubscribe@cassandra.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: pr-unsubscribe@cassandra.apache.org
For additional commands, e-mail: pr-help@cassandra.apache.org


Re: [PR] Improve CQLTester to make it trivial to run the tests with different configs, and to add randomness to the test [cassandra]

Posted by "maedhroz (via GitHub)" <gi...@apache.org>.
maedhroz commented on code in PR #3474:
URL: https://github.com/apache/cassandra/pull/3474#discussion_r1722600979


##########
test/unit/org/apache/cassandra/cql3/CQLTester.java:
##########
@@ -74,11 +74,19 @@
 import org.junit.Assert;
 import org.junit.Before;
 import org.junit.BeforeClass;
+import org.junit.FixMethodOrder;

Review Comment:
   unused import?



-- 
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: pr-unsubscribe@cassandra.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: pr-unsubscribe@cassandra.apache.org
For additional commands, e-mail: pr-help@cassandra.apache.org


Re: [PR] Improve CQLTester to make it trivial to run the tests with different configs, and to add randomness to the test [cassandra]

Posted by "maedhroz (via GitHub)" <gi...@apache.org>.
maedhroz commented on code in PR #3474:
URL: https://github.com/apache/cassandra/pull/3474#discussion_r1722444117


##########
test/unit/org/apache/cassandra/cql3/CQLTester.java:
##########
@@ -2990,6 +3002,105 @@ public boolean equals(Object o)
         }
     }
 
+    /**
+     * Enhances {@link CQLTester} to make it easier for tests to leverage randomness.  This class is not the best way to
+     * leverage randomness as it won't run the tests against multiple seeds (so could take a long time to find faults)
+     *
+     * The main use case for this class is to take existing tests or tests patterns people wish to write, and make it easy
+     * and safe to add randomness.  One main advantage is that the node spun up has non-static configs, meaning that each
+     * test run can explore different spaces and avoid having to create a new yaml/CI pipeline to test different configs.
+     *
+     * When possible {@link Property#qt()} should be leveraged as it will rerun the test many times with different seeds.
+     */
+    public static abstract class Fuzzed extends CQLTester
+    {
+        private static RandomSource RANDOM;
+        private static long SEED;
+        private static String CONFIG = null;
+        protected static Gen<Map<String, Object>> CONFIG_GEN = null;
+
+        @Rule
+        public FailureWatcher failureRule = new FailureWatcher();
+
+        @BeforeClass
+        public static void setUpClass()
+        {
+            setupSeed();
+            try
+            {
+                prePrepareServer();
+                updateConfigs();
+
+                // Once per-JVM is enough
+                prepareServer();
+            }
+            catch (Throwable t)
+            {
+                throwPropertyError(t);
+            }
+        }
+
+        protected static RandomSource random()
+        {
+            if (RANDOM == null)
+                setupSeed();
+            return RANDOM;
+        }
+
+        protected static long seed()
+        {
+            return SEED;
+        }
+
+        private static void setupSeed()
+        {
+            if (RANDOM != null) return;
+            SEED = TEST_RANDOM_SEED.getLong(new DefaultRandom().nextLong());
+            RANDOM = new DefaultRandom(SEED);
+        }
+
+        @Before
+        public void resetSeed()
+        {
+            RANDOM.setSeed(SEED);
+        }
+
+        protected static void updateConfigs()
+        {
+            if (CONFIG_GEN == null)
+                CONFIG_GEN = new ConfigGenBuilder().build();
+            Map<String, Object> config = CONFIG_GEN.next(RANDOM);
+            CONFIG = YamlConfigurationLoader.toYaml(config);
+
+            Config c = DatabaseDescriptor.loadConfig();
+            YamlConfigurationLoader.updateFromMap(config, true, c);
+

Review Comment:
   Since the default is `periodic` now, we'd need some way to remove any lingering config incompatible w/ `batch` here:
   
   ```
   if (c.commitlog_sync != Config.CommitLogSync.periodic)
       c.commitlog_sync_period = null;
   ```



-- 
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: pr-unsubscribe@cassandra.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: pr-unsubscribe@cassandra.apache.org
For additional commands, e-mail: pr-help@cassandra.apache.org


Re: [PR] Improve CQLTester to make it trivial to run the tests with different configs, and to add randomness to the test [cassandra]

Posted by "maedhroz (via GitHub)" <gi...@apache.org>.
maedhroz commented on code in PR #3474:
URL: https://github.com/apache/cassandra/pull/3474#discussion_r1722487803


##########
test/unit/org/apache/cassandra/utils/ConfigGenBuilder.java:
##########
@@ -0,0 +1,179 @@
+/*
+ * 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.utils;
+
+import java.util.LinkedHashMap;
+import java.util.Map;
+import java.util.concurrent.TimeUnit;
+
+import com.google.common.collect.ImmutableMap;
+
+import accord.utils.Gen;
+import accord.utils.Gens;
+import accord.utils.RandomSource;
+import org.apache.cassandra.config.Config;
+import org.apache.cassandra.config.DurationSpec;
+import org.apache.cassandra.dht.IPartitioner;
+import org.apache.cassandra.io.sstable.format.SSTableFormat;
+
+public class ConfigGenBuilder
+{
+    public enum Memtable {SkipListMemtable, TrieMemtable, ShardedSkipListMemtable}
+
+    Gen<IPartitioner> partitionerGen = Generators.toGen(CassandraGenerators.nonLocalPartitioners());
+    Gen<Config.DiskAccessMode> commitLogDiskAccessModeGen = Gens.enums().all(Config.DiskAccessMode.class)
+                                                                .filter(m -> m != Config.DiskAccessMode.standard && m != Config.DiskAccessMode.mmap_index_only);

Review Comment:
   I think we need to exclude `direct` mode here too if the filesystem doesn't support it.



-- 
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: pr-unsubscribe@cassandra.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: pr-unsubscribe@cassandra.apache.org
For additional commands, e-mail: pr-help@cassandra.apache.org


Re: [PR] Improve CQLTester to make it trivial to run the tests with different configs, and to add randomness to the test [cassandra]

Posted by "dcapwell (via GitHub)" <gi...@apache.org>.
dcapwell commented on code in PR #3474:
URL: https://github.com/apache/cassandra/pull/3474#discussion_r1723589826


##########
src/java/org/apache/cassandra/config/DatabaseDescriptor.java:
##########
@@ -271,6 +274,42 @@ public static void daemonInitialization(Supplier<Config> config) throws Configur
         AuthConfig.applyAuth();
     }
 
+    public static void unsafeDaemonInitialization(Supplier<Config> config) throws ConfigurationException
+    {
+        if (toolInitialized)
+            throw new AssertionError("toolInitialization() already called");
+        if (clientInitialized)
+            throw new AssertionError("clientInitialization() already called");

Review Comment:
   added `assertNotDaemonInitialized` as well



-- 
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: pr-unsubscribe@cassandra.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: pr-unsubscribe@cassandra.apache.org
For additional commands, e-mail: pr-help@cassandra.apache.org


Re: [PR] Improve CQLTester to make it trivial to run the tests with different configs, and to add randomness to the test [cassandra]

Posted by "dcapwell (via GitHub)" <gi...@apache.org>.
dcapwell commented on code in PR #3474:
URL: https://github.com/apache/cassandra/pull/3474#discussion_r1715998071


##########
test/unit/org/apache/cassandra/cql3/validation/operations/InsertTest.java:
##########
@@ -75,7 +75,7 @@ public void testEmptyTTL() throws Throwable
         createTable("CREATE TABLE %s (k int PRIMARY KEY, v int)");
         execute("INSERT INTO %s (k, v) VALUES (0, 0) USING TTL ?", (Object) null);
         execute("INSERT INTO %s (k, v) VALUES (1, 1) USING TTL ?", ByteBuffer.wrap(new byte[0]));
-        assertRows(execute("SELECT k, v, ttl(v) FROM %s"), row(1, 1, null), row(0, 0, null));
+        assertRowsIgnoringOrder(execute("SELECT k, v, ttl(v) FROM %s"), row(1, 1, null), row(0, 0, null));

Review Comment:
   different partitioners produce different partition key orderings, so `IgnoringOrder` is required



-- 
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: pr-unsubscribe@cassandra.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: pr-unsubscribe@cassandra.apache.org
For additional commands, e-mail: pr-help@cassandra.apache.org


Re: [PR] Improve CQLTester to make it trivial to run the tests with different configs, and to add randomness to the test [cassandra]

Posted by "maedhroz (via GitHub)" <gi...@apache.org>.
maedhroz commented on code in PR #3474:
URL: https://github.com/apache/cassandra/pull/3474#discussion_r1722593563


##########
test/unit/org/apache/cassandra/utils/ConfigGenBuilder.java:
##########
@@ -0,0 +1,179 @@
+/*
+ * 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.utils;
+
+import java.util.LinkedHashMap;
+import java.util.Map;
+import java.util.concurrent.TimeUnit;
+
+import com.google.common.collect.ImmutableMap;
+
+import accord.utils.Gen;
+import accord.utils.Gens;
+import accord.utils.RandomSource;
+import org.apache.cassandra.config.Config;
+import org.apache.cassandra.config.DurationSpec;
+import org.apache.cassandra.dht.IPartitioner;
+import org.apache.cassandra.io.sstable.format.SSTableFormat;
+
+public class ConfigGenBuilder
+{
+    public enum Memtable {SkipListMemtable, TrieMemtable, ShardedSkipListMemtable}
+
+    Gen<IPartitioner> partitionerGen = Generators.toGen(CassandraGenerators.nonLocalPartitioners());
+    Gen<Config.DiskAccessMode> commitLogDiskAccessModeGen = Gens.enums().all(Config.DiskAccessMode.class)
+                                                                .filter(m -> m != Config.DiskAccessMode.standard && m != Config.DiskAccessMode.mmap_index_only);
+    Gen<Config.DiskAccessMode> diskAccessModeGen = Gens.enums().all(Config.DiskAccessMode.class).filter(m -> m != Config.DiskAccessMode.direct);
+    Gen<String> sstableFormatGen = Generators.toGen(CassandraGenerators.sstableFormat()).map(SSTableFormat::name);
+    Gen<Config.MemtableAllocationType> memtableAllocationTypeGen = Gens.enums().all(Config.MemtableAllocationType.class);
+    Gen<Memtable> memtableGen = Gens.enums().all(Memtable.class);
+    Gen<Config.CommitLogSync> commitLogSyncGen = Gens.enums().all(Config.CommitLogSync.class);
+    Gen<DurationSpec.IntMillisecondsBound> commitLogSyncPeriodGen = rs -> {
+        // how long?
+        long periodMillis;
+        switch (rs.nextInt(0, 2))
+        {
+            case 0: // millis
+                periodMillis = rs.nextLong(1, 20);
+                break;
+            case 1: // seconds
+                periodMillis = TimeUnit.SECONDS.toMillis(rs.nextLong(1, 20));
+                break;
+            default:
+                throw new AssertionError();
+        }
+        return new DurationSpec.IntMillisecondsBound(periodMillis);
+    };
+    // group blocks each and every write for X milliseconds which cause tests to take a lot of time,
+    // for this reason the period must be "short"
+    Gen<DurationSpec.IntMillisecondsBound> commitlogSyncGroupWindowGen = Gens.longs().between(1, 20).map(l -> new DurationSpec.IntMillisecondsBound(l));
+
+    public ConfigGenBuilder withPartitioner(IPartitioner instance)
+    {
+        this.partitionerGen = ignore -> instance;
+        return this;
+    }
+
+    public ConfigGenBuilder withCommitLogSync(Config.CommitLogSync commitLogSync)
+    {
+        this.commitLogSyncGen = ignore -> commitLogSync;
+        return this;
+    }
+
+    public Gen<Map<String, Object>> build()
+    {
+        return rs -> {
+            Map<String, Object> config = new LinkedHashMap<>();
+
+            updateConfigPartitioner(rs, config);
+            updateConfigCommitLog(rs, config);
+            updateConfigMemtable(rs, config);
+            updateConfigSSTables(rs, config);
+            updateConfigDisk(rs, config);
+            return config;
+        };
+    }
+
+    private void updateConfigPartitioner(RandomSource rs, Map<String, Object> config)
+    {
+        IPartitioner partitioner = partitionerGen.next(rs);
+        config.put("partitioner", partitioner.getClass().getSimpleName());
+    }
+
+    private void updateConfigCommitLog(RandomSource rs, Map<String, Object> config)
+    {
+        Config.CommitLogSync commitlog_sync = commitLogSyncGen.next(rs);
+        config.put("commitlog_sync", commitlog_sync);
+        switch (commitlog_sync)
+        {
+            case batch:
+                break;
+            case periodic:
+                config.put("commitlog_sync_period", commitLogSyncPeriodGen.next(rs).toString());
+                break;
+            case group:
+                config.put("commitlog_sync_group_window", commitlogSyncGroupWindowGen.next(rs).toString());
+                break;
+            default:
+                throw new AssertionError(commitlog_sync.name());
+        }
+        config.put("commitlog_disk_access_mode", commitLogDiskAccessModeGen.next(rs));
+    }
+
+    private void updateConfigMemtable(RandomSource rs, Map<String, Object> config)
+    {
+        config.put("memtable_allocation_type", memtableAllocationTypeGen.next(rs));
+        //TODO (usability, operability): rather than defining each config, we should have common configs and let you override... this is what we did with sstables...

Review Comment:
   I thought we already had a Jira for this, and it just got dropped...



-- 
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: pr-unsubscribe@cassandra.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: pr-unsubscribe@cassandra.apache.org
For additional commands, e-mail: pr-help@cassandra.apache.org


Re: [PR] Improve CQLTester to make it trivial to run the tests with different configs, and to add randomness to the test [cassandra]

Posted by "maedhroz (via GitHub)" <gi...@apache.org>.
maedhroz commented on code in PR #3474:
URL: https://github.com/apache/cassandra/pull/3474#discussion_r1722594897


##########
test/unit/org/apache/cassandra/utils/ConfigGenBuilder.java:
##########
@@ -0,0 +1,179 @@
+/*
+ * 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.utils;
+
+import java.util.LinkedHashMap;
+import java.util.Map;
+import java.util.concurrent.TimeUnit;
+
+import com.google.common.collect.ImmutableMap;
+
+import accord.utils.Gen;
+import accord.utils.Gens;
+import accord.utils.RandomSource;
+import org.apache.cassandra.config.Config;
+import org.apache.cassandra.config.DurationSpec;
+import org.apache.cassandra.dht.IPartitioner;
+import org.apache.cassandra.io.sstable.format.SSTableFormat;
+
+public class ConfigGenBuilder
+{
+    public enum Memtable {SkipListMemtable, TrieMemtable, ShardedSkipListMemtable}
+
+    Gen<IPartitioner> partitionerGen = Generators.toGen(CassandraGenerators.nonLocalPartitioners());
+    Gen<Config.DiskAccessMode> commitLogDiskAccessModeGen = Gens.enums().all(Config.DiskAccessMode.class)
+                                                                .filter(m -> m != Config.DiskAccessMode.standard && m != Config.DiskAccessMode.mmap_index_only);
+    Gen<Config.DiskAccessMode> diskAccessModeGen = Gens.enums().all(Config.DiskAccessMode.class).filter(m -> m != Config.DiskAccessMode.direct);
+    Gen<String> sstableFormatGen = Generators.toGen(CassandraGenerators.sstableFormat()).map(SSTableFormat::name);
+    Gen<Config.MemtableAllocationType> memtableAllocationTypeGen = Gens.enums().all(Config.MemtableAllocationType.class);
+    Gen<Memtable> memtableGen = Gens.enums().all(Memtable.class);
+    Gen<Config.CommitLogSync> commitLogSyncGen = Gens.enums().all(Config.CommitLogSync.class);
+    Gen<DurationSpec.IntMillisecondsBound> commitLogSyncPeriodGen = rs -> {
+        // how long?
+        long periodMillis;
+        switch (rs.nextInt(0, 2))
+        {
+            case 0: // millis
+                periodMillis = rs.nextLong(1, 20);
+                break;
+            case 1: // seconds
+                periodMillis = TimeUnit.SECONDS.toMillis(rs.nextLong(1, 20));
+                break;
+            default:
+                throw new AssertionError();
+        }
+        return new DurationSpec.IntMillisecondsBound(periodMillis);
+    };
+    // group blocks each and every write for X milliseconds which cause tests to take a lot of time,
+    // for this reason the period must be "short"
+    Gen<DurationSpec.IntMillisecondsBound> commitlogSyncGroupWindowGen = Gens.longs().between(1, 20).map(l -> new DurationSpec.IntMillisecondsBound(l));
+
+    public ConfigGenBuilder withPartitioner(IPartitioner instance)
+    {
+        this.partitionerGen = ignore -> instance;
+        return this;
+    }
+
+    public ConfigGenBuilder withCommitLogSync(Config.CommitLogSync commitLogSync)
+    {
+        this.commitLogSyncGen = ignore -> commitLogSync;
+        return this;
+    }
+
+    public Gen<Map<String, Object>> build()
+    {
+        return rs -> {
+            Map<String, Object> config = new LinkedHashMap<>();
+
+            updateConfigPartitioner(rs, config);
+            updateConfigCommitLog(rs, config);
+            updateConfigMemtable(rs, config);
+            updateConfigSSTables(rs, config);
+            updateConfigDisk(rs, config);
+            return config;
+        };
+    }
+
+    private void updateConfigPartitioner(RandomSource rs, Map<String, Object> config)
+    {
+        IPartitioner partitioner = partitionerGen.next(rs);
+        config.put("partitioner", partitioner.getClass().getSimpleName());
+    }
+
+    private void updateConfigCommitLog(RandomSource rs, Map<String, Object> config)
+    {
+        Config.CommitLogSync commitlog_sync = commitLogSyncGen.next(rs);
+        config.put("commitlog_sync", commitlog_sync);
+        switch (commitlog_sync)
+        {
+            case batch:
+                break;
+            case periodic:
+                config.put("commitlog_sync_period", commitLogSyncPeriodGen.next(rs).toString());
+                break;
+            case group:
+                config.put("commitlog_sync_group_window", commitlogSyncGroupWindowGen.next(rs).toString());
+                break;
+            default:
+                throw new AssertionError(commitlog_sync.name());
+        }
+        config.put("commitlog_disk_access_mode", commitLogDiskAccessModeGen.next(rs));
+    }
+
+    private void updateConfigMemtable(RandomSource rs, Map<String, Object> config)
+    {
+        config.put("memtable_allocation_type", memtableAllocationTypeGen.next(rs));
+        //TODO (usability, operability): rather than defining each config, we should have common configs and let you override... this is what we did with sstables...

Review Comment:
   CASSANDRA-18441 was the SSTable one...



-- 
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: pr-unsubscribe@cassandra.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: pr-unsubscribe@cassandra.apache.org
For additional commands, e-mail: pr-help@cassandra.apache.org


Re: [PR] Improve CQLTester to make it trivial to run the tests with different configs, and to add randomness to the test [cassandra]

Posted by "dcapwell (via GitHub) via pr" <pr...@cassandra.apache.org>.
dcapwell closed pull request #3474: Improve CQLTester to make it trivial to run the tests with different configs, and to add randomness to the test
URL: https://github.com/apache/cassandra/pull/3474


-- 
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: pr-unsubscribe@cassandra.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: pr-unsubscribe@cassandra.apache.org
For additional commands, e-mail: pr-help@cassandra.apache.org


Re: [PR] Improve CQLTester to make it trivial to run the tests with different configs, and to add randomness to the test [cassandra]

Posted by "maedhroz (via GitHub)" <gi...@apache.org>.
maedhroz commented on code in PR #3474:
URL: https://github.com/apache/cassandra/pull/3474#discussion_r1722357973


##########
src/java/org/apache/cassandra/config/DatabaseDescriptor.java:
##########
@@ -271,6 +274,42 @@ public static void daemonInitialization(Supplier<Config> config) throws Configur
         AuthConfig.applyAuth();
     }
 
+    public static void unsafeDaemonInitialization(Supplier<Config> config) throws ConfigurationException
+    {
+        if (toolInitialized)
+            throw new AssertionError("toolInitialization() already called");
+        if (clientInitialized)
+            throw new AssertionError("clientInitialization() already called");

Review Comment:
   nit: We could de-dup the 3 places where these are called w/ something like...
   
   ```
   assertNotToolInitialized();
   assertNotClientInitialized();
   ```
   
   Not much ROI, but up to you.



-- 
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: pr-unsubscribe@cassandra.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: pr-unsubscribe@cassandra.apache.org
For additional commands, e-mail: pr-help@cassandra.apache.org


Re: [PR] Improve CQLTester to make it trivial to run the tests with different configs, and to add randomness to the test [cassandra]

Posted by "dcapwell (via GitHub)" <gi...@apache.org>.
dcapwell commented on code in PR #3474:
URL: https://github.com/apache/cassandra/pull/3474#discussion_r1723569386


##########
test/unit/org/apache/cassandra/cql3/CQLTester.java:
##########
@@ -2990,6 +3002,105 @@ public boolean equals(Object o)
         }
     }
 
+    /**
+     * Enhances {@link CQLTester} to make it easier for tests to leverage randomness.  This class is not the best way to
+     * leverage randomness as it won't run the tests against multiple seeds (so could take a long time to find faults)
+     *
+     * The main use case for this class is to take existing tests or tests patterns people wish to write, and make it easy
+     * and safe to add randomness.  One main advantage is that the node spun up has non-static configs, meaning that each
+     * test run can explore different spaces and avoid having to create a new yaml/CI pipeline to test different configs.
+     *
+     * When possible {@link Property#qt()} should be leveraged as it will rerun the test many times with different seeds.
+     */
+    public static abstract class Fuzzed extends CQLTester
+    {
+        private static RandomSource RANDOM;
+        private static long SEED;
+        private static String CONFIG = null;
+        protected static Gen<Map<String, Object>> CONFIG_GEN = null;
+
+        @Rule
+        public FailureWatcher failureRule = new FailureWatcher();
+
+        @BeforeClass
+        public static void setUpClass()
+        {
+            setupSeed();
+            try
+            {
+                prePrepareServer();
+                updateConfigs();
+
+                // Once per-JVM is enough
+                prepareServer();
+            }
+            catch (Throwable t)
+            {
+                throwPropertyError(t);
+            }
+        }
+
+        protected static RandomSource random()
+        {
+            if (RANDOM == null)
+                setupSeed();
+            return RANDOM;
+        }
+
+        protected static long seed()
+        {
+            return SEED;
+        }
+
+        private static void setupSeed()
+        {
+            if (RANDOM != null) return;
+            SEED = TEST_RANDOM_SEED.getLong(new DefaultRandom().nextLong());
+            RANDOM = new DefaultRandom(SEED);
+        }
+
+        @Before
+        public void resetSeed()
+        {
+            RANDOM.setSeed(SEED);
+        }
+
+        protected static void updateConfigs()
+        {
+            if (CONFIG_GEN == null)
+                CONFIG_GEN = new ConfigGenBuilder().build();
+            Map<String, Object> config = CONFIG_GEN.next(RANDOM);
+            CONFIG = YamlConfigurationLoader.toYaml(config);
+
+            Config c = DatabaseDescriptor.loadConfig();
+            YamlConfigurationLoader.updateFromMap(config, true, c);
+

Review Comment:
   `Config c = ConfigGenBuilder.santize(DatabaseDescriptor.loadConfig());`  this will roll back these configs to their real defaults, which fixes the test



-- 
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: pr-unsubscribe@cassandra.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: pr-unsubscribe@cassandra.apache.org
For additional commands, e-mail: pr-help@cassandra.apache.org


Re: [PR] Improve CQLTester to make it trivial to run the tests with different configs, and to add randomness to the test [cassandra]

Posted by "dcapwell (via GitHub)" <gi...@apache.org>.
dcapwell commented on code in PR #3474:
URL: https://github.com/apache/cassandra/pull/3474#discussion_r1723583119


##########
test/unit/org/apache/cassandra/cql3/CQLTester.java:
##########
@@ -74,11 +74,19 @@
 import org.junit.Assert;
 import org.junit.Before;
 import org.junit.BeforeClass;
+import org.junit.FixMethodOrder;

Review Comment:
   thanks.  I dropped the fixed method in favor of resetting the seed and didn't update the imports



-- 
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: pr-unsubscribe@cassandra.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: pr-unsubscribe@cassandra.apache.org
For additional commands, e-mail: pr-help@cassandra.apache.org


Re: [PR] Improve CQLTester to make it trivial to run the tests with different configs, and to add randomness to the test [cassandra]

Posted by "maedhroz (via GitHub)" <gi...@apache.org>.
maedhroz commented on code in PR #3474:
URL: https://github.com/apache/cassandra/pull/3474#discussion_r1722601113


##########
test/unit/org/apache/cassandra/cql3/CQLTester.java:
##########
@@ -74,11 +74,19 @@
 import org.junit.Assert;
 import org.junit.Before;
 import org.junit.BeforeClass;
+import org.junit.FixMethodOrder;
 import org.junit.Rule;
 import org.junit.rules.TestName;
+import org.junit.rules.TestWatcher;
+import org.junit.runner.Description;
+import org.junit.runners.MethodSorters;

Review Comment:
   unused import?



-- 
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: pr-unsubscribe@cassandra.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: pr-unsubscribe@cassandra.apache.org
For additional commands, e-mail: pr-help@cassandra.apache.org


Re: [PR] Improve CQLTester to make it trivial to run the tests with different configs, and to add randomness to the test [cassandra]

Posted by "dcapwell (via GitHub)" <gi...@apache.org>.
dcapwell commented on code in PR #3474:
URL: https://github.com/apache/cassandra/pull/3474#discussion_r1720400474


##########
test/unit/accord/utils/RandomSource.java:
##########
@@ -301,7 +301,7 @@ default long reset()
 
     default Random asJdkRandom()
     {
-        return new Random()
+        return new Random(nextLong())

Review Comment:
   made this change in accord branch awhile ago, so bringing it forward as this method is touched in `sai` tests



-- 
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: pr-unsubscribe@cassandra.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: pr-unsubscribe@cassandra.apache.org
For additional commands, e-mail: pr-help@cassandra.apache.org


Re: [PR] Improve CQLTester to make it trivial to run the tests with different configs, and to add randomness to the test [cassandra]

Posted by "dcapwell (via GitHub)" <gi...@apache.org>.
dcapwell commented on code in PR #3474:
URL: https://github.com/apache/cassandra/pull/3474#discussion_r1716000362


##########
test/unit/org/apache/cassandra/utils/CassandraGenerators.java:
##########
@@ -597,6 +600,21 @@ public static Gen<Token> localPartitionerToken(LocalPartitioner partitioner)
         return rs -> partitioner.getToken(bytes.generate(rs));
     }
 
+    public static Gen<IPartitioner> localPartitioner()
+    {
+        return AbstractTypeGenerators.safeTypeGen().map(LocalPartitioner::new);
+    }
+
+    public static Gen<Token> localPartitionerToken()

Review Comment:
   dead code... pulled from the accord branch and keeps this consistent with the other methods that exist



-- 
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: pr-unsubscribe@cassandra.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: pr-unsubscribe@cassandra.apache.org
For additional commands, e-mail: pr-help@cassandra.apache.org


Re: [PR] Improve CQLTester to make it trivial to run the tests with different configs, and to add randomness to the test [cassandra]

Posted by "dcapwell (via GitHub)" <gi...@apache.org>.
dcapwell commented on code in PR #3474:
URL: https://github.com/apache/cassandra/pull/3474#discussion_r1716000362


##########
test/unit/org/apache/cassandra/utils/CassandraGenerators.java:
##########
@@ -597,6 +600,21 @@ public static Gen<Token> localPartitionerToken(LocalPartitioner partitioner)
         return rs -> partitioner.getToken(bytes.generate(rs));
     }
 
+    public static Gen<IPartitioner> localPartitioner()
+    {
+        return AbstractTypeGenerators.safeTypeGen().map(LocalPartitioner::new);
+    }
+
+    public static Gen<Token> localPartitionerToken()

Review Comment:
   dead code... pulled from the accord branch and keeps this consistent with the other methods that exist



-- 
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: pr-unsubscribe@cassandra.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: pr-unsubscribe@cassandra.apache.org
For additional commands, e-mail: pr-help@cassandra.apache.org