michaelsembwever commented on code in PR #236: URL: https://github.com/apache/cassandra-analytics/pull/236#discussion_r3992713630
########## cassandra-six-zero-bridge/src/main/java/org/apache/cassandra/spark/reader/SSTableCache.java: ########## @@ -0,0 +1,240 @@ +/* + * 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.spark.reader; + +import java.io.IOException; +import java.io.InputStream; +import java.util.Map; +import java.util.Optional; +import java.util.concurrent.Callable; +import java.util.concurrent.ExecutionException; +import java.util.concurrent.TimeUnit; +import java.util.function.Function; + +import com.google.common.cache.Cache; +import com.google.common.cache.CacheBuilder; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import org.apache.cassandra.db.DecoratedKey; +import org.apache.cassandra.io.sstable.Descriptor; +import org.apache.cassandra.io.sstable.metadata.MetadataComponent; +import org.apache.cassandra.io.sstable.metadata.MetadataType; +import org.apache.cassandra.schema.TableMetadata; +import org.apache.cassandra.spark.data.FileType; +import org.apache.cassandra.spark.data.SSTable; +import org.apache.cassandra.spark.utils.Pair; +import org.apache.cassandra.spark.utils.ThrowableUtils; +import org.apache.cassandra.utils.BloomFilter; +import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; + +/** + * Basic cache to reduce wasteful requests on the DataLayer for cacheable SSTable metadata, + * useful when running many Spark tasks on the same Spark worker + * + * <p>Cassandra 6.0 needs its own copy of this class, because a {@link CompressionMetadata} of 6.0 holds a + * reference to a compression dictionary and must release it when this class stops holding the metadata. + */ +@SuppressWarnings("UnstableApiUsage") +public class SSTableCache +{ + private static final Logger LOGGER = LoggerFactory.getLogger(SSTableCache.class); + + public static final SSTableCache INSTANCE = new SSTableCache(); + private final Cache<SSTable, SummaryDbUtils.Summary> summary = buildCache(propOrDefault("sbr.cache.summary.maxEntries", 4096), + propOrDefault("sbr.cache.summary.expireAfterMins", 15)); + private final Cache<SSTable, Pair<DecoratedKey, DecoratedKey>> index = buildCache(propOrDefault("sbr.cache.index.maxEntries", 128), + propOrDefault("sbr.cache.index.expireAfterMins", 60)); + private final Cache<SSTable, Map<MetadataType, MetadataComponent>> stats = buildCache(propOrDefault("sbr.cache.stats.maxEntries", 16384), + propOrDefault("sbr.cache.stats.expireAfterMins", 60)); + private final Cache<SSTable, BloomFilter> filter = buildCache(propOrDefault("sbr.cache.filter.maxEntries", 16384), + propOrDefault("sbr.cache.filter.expireAfterMins", 60)); + // if compression is disabled then the CompressionInfo.db file will not exist + // therefore we can cache as Optional to a) avoid null errors in the cache and b) record that the component does not exist + private final Cache<SSTable, Optional<CompressionMetadata>> compressionMetadata = buildCompressionMetadataCache( + propOrDefault("sbr.cache.compressionInfo.maxEntries", 128), + propOrDefault("sbr.cache.compressionInfo.expireAfterMins", 15)); + + static int propOrDefault(String name, int defaultValue) + { + return propOrDefault(name, defaultValue, Integer::parseInt); + } + + private static long propOrDefault(String name, long defaultValue) + { + return propOrDefault(name, defaultValue, Long::parseLong); + } + + private static boolean propOrDefault(String name, boolean defaultValue) + { + return propOrDefault(name, defaultValue, Boolean::parseBoolean); + } + + private static <T> T propOrDefault(String name, T defaultValue, Function<String, T> parser) + { + String str = System.getProperty(name); + if (str != null) + { + try + { + return parser.apply(str); + } + catch (NumberFormatException exception) + { + LOGGER.error("NumberFormatException for prop {} ", name, exception); + } + } + return defaultValue; + } + + private <T> Cache<SSTable, T> buildCache(int size, int expireAfterMins) + { + return CacheBuilder.newBuilder() + .expireAfterAccess(expireAfterMins, TimeUnit.MINUTES) + .maximumSize(size) + .build(); + } + + /** + * Cassandra 6.0 gives a {@link CompressionMetadata} a reference to a compression dictionary, which owns native + * zstd tables. Release that reference here, so the tables go away once every reader of the dictionary has also + * released its own reference. + */ + private Cache<SSTable, Optional<CompressionMetadata>> buildCompressionMetadataCache(int size, int expireAfterMins) + { + return CacheBuilder.newBuilder() + .expireAfterAccess(expireAfterMins, TimeUnit.MINUTES) + .maximumSize(size) + .<SSTable, Optional<CompressionMetadata>>removalListener(notification -> { + Optional<CompressionMetadata> metadata = notification.getValue(); + if (metadata != null) + { + metadata.ifPresent(CompressionMetadata::close); + } + }) Review Comment: added `CompressionDictionaryTests.testCacheEvictionReleasesDictionaryReference()` it needed package-private `evictCompressionMetadata(SSTable)` (the cache field is private and nothing could force an entry out), and `CompressionDictionaryTests.testCacheEvictionReleasesDictionaryReference`. see https://github.com/apache/cassandra-analytics/pull/236/changes/16d6c8fceb6fb3e535a66857e2a10f12175da2c9 -- 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]
