Claudenw commented on code in PR #2282: URL: https://github.com/apache/cassandra/pull/2282#discussion_r1265231061
########## test/distributed/org/apache/cassandra/distributed/test/SSTableCompressionTest.java: ########## @@ -0,0 +1,454 @@ +/* + * 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; + +import java.io.IOException; +import java.nio.file.Path; +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import java.util.Set; +import java.util.function.Consumer; +import java.util.stream.Collectors; + +import com.google.common.collect.ImmutableMap; +import com.google.common.collect.ImmutableSet; +import org.junit.Test; + +import org.apache.cassandra.config.DatabaseDescriptor; +import org.apache.cassandra.db.ColumnFamilyStore; +import org.apache.cassandra.distributed.Cluster; +import org.apache.cassandra.distributed.api.IInstanceConfig; +import org.apache.cassandra.distributed.api.IInvokableInstance; +import org.apache.cassandra.io.compress.DeflateCompressor; +import org.apache.cassandra.io.compress.LZ4Compressor; +import org.apache.cassandra.io.compress.NoopCompressor; +import org.apache.cassandra.io.compress.SnappyCompressor; +import org.apache.cassandra.io.compress.ZstdCompressor; +import org.apache.cassandra.io.util.File; +import org.apache.cassandra.service.StorageService; + +import static com.google.common.base.Charsets.UTF_8; +import static java.lang.String.format; +import static org.apache.cassandra.distributed.api.Feature.GOSSIP; +import static org.apache.cassandra.distributed.api.Feature.NATIVE_PROTOCOL; +import static org.apache.cassandra.distributed.api.Feature.NETWORK; +import static org.apache.cassandra.distributed.test.ExecUtil.rethrow; +import static org.apache.commons.io.FileUtils.readFileToString; +import static org.assertj.core.api.Assertions.assertThat; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertTrue; + +public class SSTableCompressionTest +{ + static Cluster CLUSTER; + static String NODES; + static int NATIVE_PORT; + static int STORAGE_PORT; + static int SSL_STORAGE_PORT; + + private static final String KEYSPACE = "sstable_compression_test"; + + private static final Consumer<IInstanceConfig> INITIAL_CONFIG = c -> { + c.with(NATIVE_PROTOCOL, NETWORK, GOSSIP); // need gossip to get hostid for Java driver + c.set("flush_compression", "fast"); // this is default + c.set("sstable_compression", ImmutableMap.builder().put("class_name", "lz4").build()); + }; + + private static final Consumer<IInstanceConfig> SNAPPY_INITIAL_CONFIG = c -> { + c.with(NATIVE_PROTOCOL, NETWORK, GOSSIP); // need gossip to get hostid for Java driver + c.set("flush_compression", "fast"); // this is default + c.set("sstable_compression", ImmutableMap.builder().put("class_name", "snappy").build()); + }; + + private static final Consumer<IInstanceConfig> CHANGED_CONFIG = c -> { + c.with(NATIVE_PROTOCOL, NETWORK, GOSSIP); // need gossip to get hostid for Java driver + c.set("flush_compression", "fast"); // this is default + c.set("sstable_compression", ImmutableMap.builder().put("class_name", "deflate").build()); + }; Review Comment: Let's change the names of the Consumer<IInstanceConfig> to DEFAULT_CONFIG, FAST_CONFIG, and SLOW_CONFIG. Also add string constants ``` FAST_PARAM = "WITH compression = {'class': 'SnappyCompressor'}"; SLOW_PARAM = "WITH compression = {'class': 'DeflateCompressor'}" DEFAULT_PARAM = ""; ``` This should make the code easier to read. ########## src/java/org/apache/cassandra/schema/CompressionParams.java: ########## @@ -43,189 +44,239 @@ import org.apache.cassandra.io.util.DataInputPlus; import org.apache.cassandra.io.util.DataOutputPlus; import org.apache.cassandra.net.MessagingService; +import org.apache.cassandra.utils.FBUtilities; import static java.lang.String.format; +import static java.util.Collections.emptyMap; +import static org.apache.cassandra.config.CassandraRelevantProperties.DETERMINISM_SSTABLE_COMPRESSION_DEFAULT; -@SuppressWarnings("deprecation") public final class CompressionParams { - private static final Logger logger = LoggerFactory.getLogger(CompressionParams.class); - - private static volatile boolean hasLoggedSsTableCompressionWarning; - private static volatile boolean hasLoggedChunkLengthWarning; - private static volatile boolean hasLoggedCrcCheckChanceWarning; - - public static final int DEFAULT_CHUNK_LENGTH = 1024 * 16; + public static final CompressorType DEFAULT_COMPRESSION_TYPE = CompressorType.lz4; + public static final int DEFAULT_CHUNK_LENGTH = 1024 * 16; // in KB public static final double DEFAULT_MIN_COMPRESS_RATIO = 0.0; // Since pre-4.0 versions do not understand the // new compression parameter we can't use a // different default value. public static final IVersionedSerializer<CompressionParams> serializer = new Serializer(); public static final String CLASS = "class"; public static final String CHUNK_LENGTH_IN_KB = "chunk_length_in_kb"; + /** + * Requires a DataStorageSpec suffix + */ + public static final String CHUNK_LENGTH = "chunk_length"; + /** + * Requires a DataStorageSpec suffix + */ + public static final String MAX_COMPRESSED_LENGTH = "max_compressed_length"; public static final String ENABLED = "enabled"; public static final String MIN_COMPRESS_RATIO = "min_compress_ratio"; - public static final CompressionParams DEFAULT = !CassandraRelevantProperties.DETERMINISM_SSTABLE_COMPRESSION_DEFAULT.getBoolean() - ? noCompression() - : new CompressionParams(LZ4Compressor.create(Collections.emptyMap()), - DEFAULT_CHUNK_LENGTH, - calcMaxCompressedLength(DEFAULT_CHUNK_LENGTH, DEFAULT_MIN_COMPRESS_RATIO), - DEFAULT_MIN_COMPRESS_RATIO, - Collections.emptyMap()); - - public static final CompressionParams NOOP = new CompressionParams(NoopCompressor.create(Collections.emptyMap()), + public static final CompressionParams NOOP = new CompressionParams(NoopCompressor.create(emptyMap()), // 4 KiB is often the underlying disk block size 1024 * 4, Integer.MAX_VALUE, DEFAULT_MIN_COMPRESS_RATIO, - Collections.emptyMap()); + emptyMap()); - private static final String CRC_CHECK_CHANCE_WARNING = "The option crc_check_chance was deprecated as a compression option. " + - "You should specify it as a top-level table option instead"; + public static final CompressionParams DEFAULT = new CompressionParams(LZ4Compressor.create(Collections.emptyMap()), Review Comment: Do not change this to public. Make it private and create a method `defaultFastCompression()` that returns DEFAULT, then call that in `DataComponent`. We could also consider renaming DEFAULT to DEFAULT_FAST_COMPRESSOR. ########## test/distributed/org/apache/cassandra/distributed/test/SSTableCompressionTest.java: ########## @@ -0,0 +1,454 @@ +/* + * 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; + +import java.io.IOException; +import java.nio.file.Path; +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import java.util.Set; +import java.util.function.Consumer; +import java.util.stream.Collectors; + +import com.google.common.collect.ImmutableMap; +import com.google.common.collect.ImmutableSet; +import org.junit.Test; + +import org.apache.cassandra.config.DatabaseDescriptor; +import org.apache.cassandra.db.ColumnFamilyStore; +import org.apache.cassandra.distributed.Cluster; +import org.apache.cassandra.distributed.api.IInstanceConfig; +import org.apache.cassandra.distributed.api.IInvokableInstance; +import org.apache.cassandra.io.compress.DeflateCompressor; +import org.apache.cassandra.io.compress.LZ4Compressor; +import org.apache.cassandra.io.compress.NoopCompressor; +import org.apache.cassandra.io.compress.SnappyCompressor; +import org.apache.cassandra.io.compress.ZstdCompressor; +import org.apache.cassandra.io.util.File; +import org.apache.cassandra.service.StorageService; + +import static com.google.common.base.Charsets.UTF_8; +import static java.lang.String.format; +import static org.apache.cassandra.distributed.api.Feature.GOSSIP; +import static org.apache.cassandra.distributed.api.Feature.NATIVE_PROTOCOL; +import static org.apache.cassandra.distributed.api.Feature.NETWORK; +import static org.apache.cassandra.distributed.test.ExecUtil.rethrow; +import static org.apache.commons.io.FileUtils.readFileToString; +import static org.assertj.core.api.Assertions.assertThat; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertTrue; + +public class SSTableCompressionTest +{ + static Cluster CLUSTER; + static String NODES; + static int NATIVE_PORT; + static int STORAGE_PORT; + static int SSL_STORAGE_PORT; + + private static final String KEYSPACE = "sstable_compression_test"; + + private static final Consumer<IInstanceConfig> INITIAL_CONFIG = c -> { + c.with(NATIVE_PROTOCOL, NETWORK, GOSSIP); // need gossip to get hostid for Java driver + c.set("flush_compression", "fast"); // this is default + c.set("sstable_compression", ImmutableMap.builder().put("class_name", "lz4").build()); + }; + + private static final Consumer<IInstanceConfig> SNAPPY_INITIAL_CONFIG = c -> { + c.with(NATIVE_PROTOCOL, NETWORK, GOSSIP); // need gossip to get hostid for Java driver + c.set("flush_compression", "fast"); // this is default + c.set("sstable_compression", ImmutableMap.builder().put("class_name", "snappy").build()); + }; + + private static final Consumer<IInstanceConfig> CHANGED_CONFIG = c -> { + c.with(NATIVE_PROTOCOL, NETWORK, GOSSIP); // need gossip to get hostid for Java driver + c.set("flush_compression", "fast"); // this is default + c.set("sstable_compression", ImmutableMap.builder().put("class_name", "deflate").build()); + }; + + public static Path setupCluster(Consumer<IInstanceConfig> config, Path root) throws IOException + { + Cluster.Builder builder = Cluster.build().withNodes(1).withConfig(config); + + if (root != null) + builder.withRoot(root); + + CLUSTER = builder.start(); + NODES = CLUSTER.get(1).config().broadcastAddress().getHostString(); + NATIVE_PORT = CLUSTER.get(1).callOnInstance(DatabaseDescriptor::getNativeTransportPort); + STORAGE_PORT = CLUSTER.get(1).callOnInstance(DatabaseDescriptor::getStoragePort); + SSL_STORAGE_PORT = CLUSTER.get(1).callOnInstance(DatabaseDescriptor::getSSLStoragePort); + + return builder.getRootPath(); + } + + public static void tearDownCluster() + { + if (CLUSTER != null) + CLUSTER.close(); + } + + @Test + public void testCreation() throws IOException + { + // create clauster with lz4 compression + try + { + setupCluster(INITIAL_CONFIG, null); + CLUSTER.schemaChange(format("CREATE KEYSPACE IF NOT EXISTS %s WITH replication = {'class': 'SimpleStrategy', 'replication_factor': '1'}", KEYSPACE)); + testDisabled(); + testDeflate(); + testLZ4(); + testNoop(); + testSnappy(); + testZstd(); + } + finally + { + tearDownCluster(); + } + } + + /** + * This tests shows that defining a table with a different compressor works and that changing + * the default compressor between starts does not change the compressors associated with the + * saved (snapshotted) systems. + */ + @Test + public void configChangeIsolation() throws Throwable + { + try + { + Path root = setupCluster(INITIAL_CONFIG, null); + CLUSTER.schemaChange(format("CREATE KEYSPACE IF NOT EXISTS %s WITH replication = {'class': 'SimpleStrategy', 'replication_factor': '1'}", KEYSPACE)); + createSSTable("default", ""); + createSSTable("different_compress", "WITH compression = {'class': 'SnappyCompressor'}"); + + populateTable("default"); + populateTable("different_compress"); + flushTables(); + + Map<String, String> parameters = getCompressionParameters(CLUSTER.get(1), "default"); + assertEquals(LZ4Compressor.class.getName(), parameters.get("class")); + parameters = getCompressionParameters(CLUSTER.get(1), "different_compress"); + assertEquals(SnappyCompressor.class.getName(), parameters.get("class")); + + // shutdown but do not delete files. + CLUSTER.close(false); + + // restart with Deflate compression definition + setupCluster(CHANGED_CONFIG, root); + + // create and populate a new table + CLUSTER.schemaChange(format("CREATE TABLE IF NOT EXISTS %s.new_table (pk int, val text, PRIMARY KEY (pk))", KEYSPACE)); + flushTables(); + + parameters = getCompressionParameters(CLUSTER.get(1), "default"); + assertEquals(LZ4Compressor.class.getName(), parameters.get("class")); + + parameters = getCompressionParameters(CLUSTER.get(1), "different_compress"); + assertEquals(SnappyCompressor.class.getName(), parameters.get("class")); + + parameters = getCompressionParameters(CLUSTER.get(1), "new_table"); + assertEquals(DeflateCompressor.class.getName(), parameters.get("class")); + } + finally + { + tearDownCluster(); + } + } + + /** + * This tests shows that defining a table with a different compressor works and that changing + * the default compressor between starts does not change the compressors associated with the + * saved (snapshotted) systems. + */ + @Test + public void compressionNotChangedInSnapshotIO() throws Throwable + { + String tableName = "test_table"; + try + { + Path root = setupCluster(INITIAL_CONFIG, null); + CLUSTER.schemaChange(format("CREATE KEYSPACE IF NOT EXISTS %s WITH replication = {'class': 'SimpleStrategy', 'replication_factor': '1'}", KEYSPACE)); + createSSTable(tableName, "WITH compression = {'class': 'ZstdCompressor'}"); + populateTable(tableName); + flushTables(); + + Map<String, String> parameters = getCompressionParameters(CLUSTER.get(1), tableName); + assertEquals(ZstdCompressor.class.getName(), parameters.get("class")); + + Set<String> snapshot = snapshot(CLUSTER.get(1), tableName, "test"); + + // shutdown but do not delete files + CLUSTER.close(false); + + // restart with Deflate compression definition + setupCluster(CHANGED_CONFIG, root); + + restore(CLUSTER.get(1), snapshot, tableName); + + assertSnapshotCompression(snapshot(CLUSTER.get(1), tableName, "backup1"), ImmutableSet.of(LZ4Compressor.class.getSimpleName())); + parameters = getCompressionParameters(CLUSTER.get(1), tableName); + assertEquals(ZstdCompressor.class.getName(), parameters.get("class")); + } + finally + { + tearDownCluster(); + } + } + + @Test Review Comment: I think this test should have a comment explaining what it is testing and that it should be changed to cycle through the 3 configs (default, fast, slow) and create a table for each of the 3 table parameters (default, fast, slow). For each table add some data, flush, create snapshot, verify snapshot compression type. Reload the data, verify table compression is as expected. This should give us a reasonable expectation that all the possible combinations of defaults and tables should work. -- 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]

