smiklosovic commented on code in PR #4399: URL: https://github.com/apache/cassandra/pull/4399#discussion_r2417128551
########## src/java/org/apache/cassandra/db/compression/ZstdDictionaryTrainer.java: ########## @@ -0,0 +1,341 @@ +/* + * 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.nio.ByteBuffer; +import java.util.concurrent.ThreadLocalRandom; +import java.util.concurrent.atomic.AtomicLong; +import java.util.function.Consumer; + +import com.google.common.annotations.VisibleForTesting; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import com.github.luben.zstd.Zstd; +import com.github.luben.zstd.ZstdDictTrainer; +import org.apache.cassandra.config.DatabaseDescriptor; +import org.apache.cassandra.db.compression.CompressionDictionary.DictId; +import org.apache.cassandra.db.compression.CompressionDictionary.Kind; +import org.apache.cassandra.io.compress.IDictionaryCompressor; +import org.apache.cassandra.io.compress.ZstdDictionaryCompressor; +import org.apache.cassandra.schema.CompressionParams; +import org.apache.cassandra.utils.Clock; + +/** + * Zstd implementation of dictionary trainer with lifecycle management. + */ +public class ZstdDictionaryTrainer implements ICompressionDictionaryTrainer +{ + private static final Logger logger = LoggerFactory.getLogger(ZstdDictionaryTrainer.class); + + private final String keyspaceName; + private final String tableName; + private final CompressionDictionaryTrainingConfig config; + private final AtomicLong totalSampleSize; + private final AtomicLong sampleCount; + private final int compressionLevel; // optimal if using the same level for training as when compressing. + + // Sampling rate can be updated during training + private volatile int samplingRate; + + // Minimum number of samples required by ZSTD library + private static final int MIN_SAMPLES_REQUIRED = 10; + + private volatile Consumer<CompressionDictionary> dictionaryTrainedListener; + // TODO: manage the samples in this class for auto-train (follow-up). The ZstdDictTrainer cannot be re-used for multiple training runs. + private ZstdDictTrainer zstdTrainer; + private volatile boolean closed = false; + private volatile TrainingStatus currentTrainingStatus; + + public ZstdDictionaryTrainer(String keyspaceName, String tableName, + CompressionDictionaryTrainingConfig config, + int compressionLevel) + { + this.keyspaceName = keyspaceName; + this.tableName = tableName; + this.config = config; + this.totalSampleSize = new AtomicLong(0); + this.sampleCount = new AtomicLong(0); + this.compressionLevel = compressionLevel; + this.samplingRate = config.samplingRate; + this.currentTrainingStatus = TrainingStatus.NOT_STARTED; + } + + @Override + public boolean shouldSample() + { + return zstdTrainer != null && ThreadLocalRandom.current().nextInt(samplingRate) == 0; + } + + @Override + public void addSample(ByteBuffer sample) + { + if (closed || sample == null || !sample.hasRemaining() || zstdTrainer == null) + return; + + byte[] sampleBytes = new byte[sample.remaining()]; + sample.duplicate().get(sampleBytes); + + if (zstdTrainer.addSample(sampleBytes)) + { + // Update the totalSampleSize and sampleCount if the sample is added + totalSampleSize.addAndGet(sampleBytes.length); + sampleCount.incrementAndGet(); + } + } + + @Override + public CompressionDictionary trainDictionary(boolean force) + { + boolean isReady = isReady(); + if (!force && !isReady) + { + currentTrainingStatus = TrainingStatus.FAILED; + throw new IllegalStateException("Trainer is not ready"); + } + + long currentSampleCount = sampleCount.get(); + if (currentSampleCount < MIN_SAMPLES_REQUIRED) // minimum samples should be required even if force training + { + currentTrainingStatus = TrainingStatus.FAILED; + String errorMsg = String.format("Insufficient samples for training: %d (minimum required: %d)", + currentSampleCount, MIN_SAMPLES_REQUIRED); + throw new IllegalStateException(errorMsg); Review Comment: This will be visible in server's logs: java.lang.IllegalStateException: Insufficient samples for training: 0 (minimum required: 10) at org.apache.cassandra.db.compression.ZstdDictionaryTrainer.trainDictionary(ZstdDictionaryTrainer.java:119) at org.apache.cassandra.db.compression.ICompressionDictionaryTrainer.lambda$trainDictionaryAsync$0(ICompressionDictionaryTrainer.java:80) at org.apache.cassandra.concurrent.FutureTask.call(FutureTask.java:61) at org.apache.cassandra.concurrent.FutureTask.run(FutureTask.java:71) at java.base/java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:515) at java.base/java.util.concurrent.FutureTask.run(FutureTask.java:264) at java.base/java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.run(ScheduledThreadPoolExecutor.java:304) at java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1128) at java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:628) at io.netty.util.concurrent.FastThreadLocalRunnable.run(FastThreadLocalRunnable.java:30) at java.base/java.lang.Thread.run(Thread.java:829) I am not sure we want to have this visible like that, we should log it only. -- 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]

