yifan-c commented on code in PR #4629:
URL: https://github.com/apache/cassandra/pull/4629#discussion_r2850993539


##########
test/unit/org/apache/cassandra/db/compression/CompressionDictionaryTrainingFrequencyTest.java:
##########
@@ -0,0 +1,229 @@
+/*
+ * 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.db.compression;
+
+import java.time.Instant;
+import java.time.temporal.ChronoUnit;
+import java.util.Arrays;
+import java.util.List;
+import java.util.concurrent.TimeUnit;
+import java.util.regex.Matcher;
+import java.util.regex.Pattern;
+
+import com.google.common.util.concurrent.Uninterruptibles;
+
+import org.junit.Assert;
+import org.junit.BeforeClass;
+import org.junit.Test;
+
+import org.apache.cassandra.config.DurationSpec;
+import org.apache.cassandra.cql3.CQLTester;
+import org.apache.cassandra.db.ColumnFamilyStore;
+import 
org.apache.cassandra.db.compression.CompressionDictionaryDetailsTabularData.CompressionDictionaryDataObject;
+import org.apache.cassandra.io.compress.IDictionaryCompressor;
+import org.apache.cassandra.io.util.File;
+import org.apache.cassandra.io.util.FileUtils;
+import org.apache.cassandra.schema.SystemDistributedKeyspace;
+import org.apache.cassandra.tools.ToolRunner;
+import org.apache.cassandra.utils.JsonUtils;
+import org.apache.cassandra.utils.Pair;
+
+import static java.lang.String.format;
+import static org.apache.cassandra.tools.ToolRunner.invokeNodetool;
+import static org.assertj.core.api.Assertions.assertThat;
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertFalse;
+import static org.junit.Assert.assertNotNull;
+import static org.junit.Assert.assertTrue;
+
+public class CompressionDictionaryTrainingFrequencyTest extends CQLTester
+{
+    private static final String tableName = "mytable";
+
+    @BeforeClass
+    public static void setup() throws Throwable
+    {
+        requireNetwork();
+        startJMXServer();
+    }
+
+    @Test
+    public void testTrainingFrequency() throws Throwable
+    {
+        // we can train twice when no limit is imposed
+        String tableId = 
createDictTable(IDictionaryCompressor.DEFAULT_TRAINING_MIN_FREQUENCY);
+        trainDict();
+        trainDict();
+
+        assertDicts(2, tableId);
+
+        alterDictTable("5m");
+
+        assertDictTrainingFails("5m");
+
+        alterDictTable("1m");
+
+        // we can train again as 1 minute from the last training has passed
+        Uninterruptibles.sleepUninterruptibly(1, TimeUnit.MINUTES);
+        trainDict();
+        assertDicts(3, tableId);
+
+        // resetting back to 0, so we can train whenever we want
+        alterDictTable("0m");
+        trainDict();
+        assertDicts(4, tableId);
+
+        alterDictTable("10m");
+
+        Pair<CompressionDictionaryDataObject, File> export = export();
+        assertFailingImport(export.right);
+
+        alterDictTable("0m");
+        assertSuccessfulImport(export.right);
+    }
+
+    private String getTableId()
+    {
+        ColumnFamilyStore cfs = ColumnFamilyStore.getIfExists(keyspace(), 
tableName);
+        Assert.assertNotNull(cfs);
+        return cfs.metadata.id.toLongString();
+    }
+
+    private String createDictTable(String frequency)
+    {
+        schemaChange(format("CREATE TABLE %s.%s (id int PRIMARY KEY, data 
text)" +
+                            " WITH compression = {'class': 
'ZstdDictionaryCompressor', '%s': %s}",
+                            keyspace(),
+                            tableName,
+                            
IDictionaryCompressor.TRAINING_MIN_FREQUENCY_PARAMETER_NAME,
+                            frequency));
+
+        return getTableId();
+    }
+
+    private void alterDictTable(String trainingFrequency)
+    {
+        schemaChange(format("ALTER TABLE %s.%s WITH compression = {'class': 
'ZstdDictionaryCompressor', '%s': %s}",
+                            keyspace(),
+                            tableName,
+                            
IDictionaryCompressor.TRAINING_MIN_FREQUENCY_PARAMETER_NAME,
+                            trainingFrequency));
+    }
+
+    private void assertDicts(int expectedDicts, String tableId)
+    {
+        List<CompressionDictionary.LightweightCompressionDictionary> dicts = 
SystemDistributedKeyspace.retrieveLightweightCompressionDictionaries();
+        assertNotNull(dicts);
+        assertEquals(expectedDicts, dicts.size());
+        for (int i = 0; i < expectedDicts; i++)
+            assertEquals(tableId, dicts.get(i).tableId);
+    }
+
+    private void trainDict()
+    {
+        createSSTables();
+
+        // Test training command with --force since we have limited test data
+        ToolRunner.ToolResult result = invokeNodetool("compressiondictionary", 
"train", "--force", keyspace(), tableName);
+        result.assertOnCleanExit();
+
+        assertThat(result.getStdout())
+        .as("Should indicate training completed")
+        .contains("Training completed successfully")
+        .contains(keyspace())
+        .contains(tableName);
+    }
+
+    private void assertDictTrainingFails(String frequency)
+    {
+        createSSTables();
+
+        // Test training command with --force since we have limited test data
+        ToolRunner.ToolResult result = invokeNodetool("compressiondictionary", 
"train", "--force", keyspace(), tableName);
+        Assert.assertEquals(1, result.getExitCode());

Review Comment:
   Nit: can we use assertJ API for consistency 



##########
doc/modules/cassandra/pages/managing/operating/compression.adoc:
##########
@@ -312,13 +313,13 @@ ALTER TABLE keyspace.table
        'class': 'ZstdDictionaryCompressor',
        'compression_level': '3',
        'training_max_total_sample_size': '20MiB',
-       'training_max_dictionary_size': '128KiB'
+       'training_max_dictionary_size': '128KiB',
+       'min_training_frequency': '1d'
    };
 ----
 
-It is possible to override these training parameters by `nodetool 
compressiondictionary train` command as
-explained in the section futher down below. If `train` subcommand do not 
override them, CQL parameters are
-taken into account.
+It is possible to override these training parameters by `nodetool 
compressiondictionary train` command (except `min_training_frequency` which is 
configurable via CQL only) as
+explained in the section futher down below. If `train` subcommand do not 
override them, CQL parameters are taken into account.

Review Comment:
   An pre-existing typo. `futher` -> `further`



##########
test/unit/org/apache/cassandra/db/compression/CompressionDictionaryTrainingFrequencyTest.java:
##########
@@ -0,0 +1,229 @@
+/*
+ * 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.db.compression;
+
+import java.time.Instant;
+import java.time.temporal.ChronoUnit;
+import java.util.Arrays;
+import java.util.List;
+import java.util.concurrent.TimeUnit;
+import java.util.regex.Matcher;
+import java.util.regex.Pattern;
+
+import com.google.common.util.concurrent.Uninterruptibles;
+
+import org.junit.Assert;
+import org.junit.BeforeClass;
+import org.junit.Test;
+
+import org.apache.cassandra.config.DurationSpec;
+import org.apache.cassandra.cql3.CQLTester;
+import org.apache.cassandra.db.ColumnFamilyStore;
+import 
org.apache.cassandra.db.compression.CompressionDictionaryDetailsTabularData.CompressionDictionaryDataObject;
+import org.apache.cassandra.io.compress.IDictionaryCompressor;
+import org.apache.cassandra.io.util.File;
+import org.apache.cassandra.io.util.FileUtils;
+import org.apache.cassandra.schema.SystemDistributedKeyspace;
+import org.apache.cassandra.tools.ToolRunner;
+import org.apache.cassandra.utils.JsonUtils;
+import org.apache.cassandra.utils.Pair;
+
+import static java.lang.String.format;
+import static org.apache.cassandra.tools.ToolRunner.invokeNodetool;
+import static org.assertj.core.api.Assertions.assertThat;
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertFalse;
+import static org.junit.Assert.assertNotNull;
+import static org.junit.Assert.assertTrue;
+
+public class CompressionDictionaryTrainingFrequencyTest extends CQLTester
+{
+    private static final String tableName = "mytable";
+
+    @BeforeClass
+    public static void setup() throws Throwable
+    {
+        requireNetwork();
+        startJMXServer();
+    }
+
+    @Test
+    public void testTrainingFrequency() throws Throwable
+    {
+        // we can train twice when no limit is imposed
+        String tableId = 
createDictTable(IDictionaryCompressor.DEFAULT_TRAINING_MIN_FREQUENCY);
+        trainDict();
+        trainDict();
+
+        assertDicts(2, tableId);
+
+        alterDictTable("5m");
+
+        assertDictTrainingFails("5m");
+
+        alterDictTable("1m");
+
+        // we can train again as 1 minute from the last training has passed
+        Uninterruptibles.sleepUninterruptibly(1, TimeUnit.MINUTES);

Review Comment:
   nit: avoid sleep ideally. Maybe we can mock the time



##########
doc/modules/cassandra/pages/managing/operating/compression.adoc:
##########
@@ -302,6 +302,7 @@ These parameters are meant to be configured via CQL for 
each respective table if
 * `training_max_total_sample_size` (default: `10MiB`): Maximum total size of 
sample data to collect for training, approximately 10MB. This parameter is 
configured in the 
 table's compression options for `ZstdDictionaryCompressor`.
 * `training_max_dictionary_size` (default: `64KiB`): Maximum size of trained 
dictionaries in bytes. Larger dictionaries can  capture more patterns but 
increase memory overhead. This is a parameter of `ZstdDictionaryCompressor` of 
a table, in `compression` section.
+* `min_training_frequency` (default: `0m`): Minimum time which needs to pass 
until we can train another compression dictionary. For example, if this 
property is set to `1h`, then we can train another dictionary no earlier than 1 
hour after the last training was conducted. `0m`, default, means we can train 
as frequently as we want. The purpose of this parameter is to prevent excessive 
training which might not make sense from operational and performance 
perspective. If an operator wants to prevent training altogether, they can set 
this property to overly big value, like 36500d which means that next training 
can occur at least after 100 years from the last one, effectively banning any 
training. The minimum resolution of `min_training_frequency` is in minutes. 
Accepts minutes (`m`), hours (`h`) and days (`d`).

Review Comment:
   nit: `banning` -> `disabling`



-- 
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]

Reply via email to