maedhroz commented on a change in pull request #1204: URL: https://github.com/apache/cassandra/pull/1204#discussion_r743183902
########## File path: test/unit/org/apache/cassandra/auth/CacheRefresherTest.java ########## @@ -0,0 +1,94 @@ +/* + * 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.auth; + +import java.util.HashMap; +import java.util.Map; +import java.util.concurrent.atomic.AtomicBoolean; +import java.util.function.BooleanSupplier; + +import com.google.common.util.concurrent.MoreExecutors; + +import com.github.benmanes.caffeine.cache.CacheLoader; +import com.github.benmanes.caffeine.cache.Caffeine; +import com.github.benmanes.caffeine.cache.LoadingCache; +import org.junit.Assert; +import org.junit.Test; + +public class CacheRefresherTest +{ + @Test + public void refresh() throws Exception + { + Map<String, String> src = new HashMap<>(); + CacheLoader<String, String> loader = new CacheLoader<String, String>() Review comment: This whole thing could just be... ``` CacheLoader<String, String> loader = src::get; ``` ########## File path: src/java/org/apache/cassandra/config/DatabaseDescriptor.java ########## @@ -1350,6 +1370,16 @@ public static int setCredentialsCacheMaxEntries(int maxEntries) return conf.credentials_cache_max_entries = maxEntries; } + public static boolean getCredentialsCacheActiveUpdate() + { + return conf.credentials_cache_active_update; + } + + public static void setCredentialsCacheActiveUpdate(boolean activeUpdate) Review comment: nitty nit: We call this `activeUpdate` here and `update` in the other two setters. ########## File path: conf/cassandra.yaml ########## @@ -203,16 +225,27 @@ permissions_validity_in_ms: 2000 # underlying table, it may not bring a significant reduction in the # latency of individual authentication attempts. # Defaults to 2000, set to 0 to disable credentials caching. +# For a long-running cache using credentials_cache_active_update, consider +# setting to something longer such as a daily validation: 86400000 credentials_validity_in_ms: 2000 # Refresh interval for credentials cache (if enabled). # After this interval, cache entries become eligible for refresh. Upon next # access, an async reload is scheduled and the old value returned until it # completes. If credentials_validity_in_ms is non-zero, then this must be # also. +# This setting is also used to inform the interval of auto-updating if +# using credentials_cache_active_update. # Defaults to the same value as credentials_validity_in_ms. +# For a longer-running permissions cache, consider setting to update hourly (60000) # credentials_update_interval_in_ms: 2000 +# If true, cache contents are actively updated by a background task at the +# interval set by credentials_update_interval_in_ms. If false, cache entries +# become eligible for refresh after their update interval. Upon next access, +# an async reload is scheduled and the old value returned until it completes. +# credentials_cache_active_update: true Review comment: For all three of these, the commented YAML value is `true`, but the default from `Config` appears to be `false`. I'd either make them both `false` or just clarify that the default is `false` in the comment here. WDYT? ########## File path: src/java/org/apache/cassandra/auth/AuthCache.java ########## @@ -78,6 +94,8 @@ public static void shutdownAllAndWait(long timeout, TimeUnit unit) throws Interr * @param getUpdateIntervalDelegate Getter for update interval * @param setMaxEntriesDelegate Used to set max # entries in cache. See {@link com.github.benmanes.caffeine.cache.Policy.Eviction#setMaximum(long)} * @param getMaxEntriesDelegate Getter for max entries. + * @param setActiveUpdate Actively update the cache before expiry Review comment: nit: Would this make more sense as something like "Set whether or not cache updates actively before expiry" or "Sets whether or not cache updates independent of expiry"? ########## File path: src/java/org/apache/cassandra/auth/CacheRefresher.java ########## @@ -0,0 +1,94 @@ +/* + * 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.auth; + +import java.util.Set; +import java.util.function.BiPredicate; +import java.util.function.BooleanSupplier; + +import com.github.benmanes.caffeine.cache.LoadingCache; +import com.google.common.annotations.VisibleForTesting; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import org.apache.cassandra.service.StorageService; + +public class CacheRefresher<K, V> implements Runnable +{ + private static final Logger logger = LoggerFactory.getLogger(CacheRefresher.class); + + private final String name; + private final LoadingCache<K, V> cache; + private final BiPredicate<K, V> invalidationCondition; + private final BooleanSupplier skipCondition; + + private CacheRefresher(String name, LoadingCache<K, V> cache, BiPredicate<K, V> invalidationCondition, BooleanSupplier skipCondition) + { + this.name = name; + this.cache = cache; + this.invalidationCondition = invalidationCondition; + this.skipCondition = skipCondition; + } + + public void run() + { + try + { + logger.debug("Refreshing {} cache", name); + Set<K> ks = cache.asMap().keySet(); + for (K key : ks) + { + if (skipCondition.getAsBoolean()) + { + logger.debug("Skipping {} cache refresh", name); + return; + } Review comment: Any reason we wouldn't just move this up out of the loop (to be the first thing in `run()`)? ########## File path: src/java/org/apache/cassandra/auth/AuthCache.java ########## @@ -20,19 +20,25 @@ import java.util.HashSet; import java.util.Set; +import java.util.concurrent.ScheduledFuture; import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeoutException; +import java.util.function.BiPredicate; import java.util.function.BooleanSupplier; +import java.util.function.Consumer; import java.util.function.Function; import java.util.function.IntConsumer; import java.util.function.IntSupplier; +import com.google.common.util.concurrent.MoreExecutors; Review comment: nit: unused ########## File path: src/java/org/apache/cassandra/auth/AuthCache.java ########## @@ -212,6 +277,20 @@ public int getMaxEntries() return getMaxEntriesDelegate.getAsInt(); } + public boolean getActiveUpdate() + { + return getActiveUpdate.getAsBoolean(); + } + + public void setActiveUpdate(boolean update) + { + if (Boolean.getBoolean("cassandra.disable_auth_caches_remote_configuration")) + throw new UnsupportedOperationException("Remote configuration of auth caches is disabled"); + + setActiveUpdate.accept(update); + cache = initCache(cache); Review comment: nit: This is also happening above in `setMaxEntries()`, but the assignment and the call to `initCache()` together constitute a check-and-set operation. Throwing a `synchronized` on `setActiveUpdate()` and `setMaxEntries()` would at least make the static analysis tools happy. ########## File path: conf/cassandra.yaml ########## @@ -241,10 +241,10 @@ credentials_validity_in_ms: 2000 # credentials_update_interval_in_ms: 2000 # If true, cache contents are actively updated by a background task at the -# interval set by credentials_update_interval_in_ms. If false, cache entries +# interval set by credentials_update_interval_in_ms. If false (default), cache entries # become eligible for refresh after their update interval. Upon next access, # an async reload is scheduled and the old value returned until it completes. -# credentials_cache_active_update: true +# credentials_cache_active_update: false Review comment: Should we do this for `permissions_cache_active_update` and `roles_cache_active_update` as well? ########## File path: src/java/org/apache/cassandra/auth/AuthCache.java ########## @@ -212,6 +277,20 @@ public int getMaxEntries() return getMaxEntriesDelegate.getAsInt(); } + public boolean getActiveUpdate() + { + return getActiveUpdate.getAsBoolean(); + } + + public void setActiveUpdate(boolean update) + { + if (Boolean.getBoolean("cassandra.disable_auth_caches_remote_configuration")) + throw new UnsupportedOperationException("Remote configuration of auth caches is disabled"); + + setActiveUpdate.accept(update); + cache = initCache(cache); Review comment: > multiple operators calling JMX ops against these methods exactly -- 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]

