Mmuzaf commented on code in PR #2133: URL: https://github.com/apache/cassandra/pull/2133#discussion_r1149864738
########## src/java/org/apache/cassandra/config/registry/ConfigurationRegistry.java: ########## @@ -0,0 +1,401 @@ +/* + * 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.config.registry; + +import java.util.AbstractMap; +import java.util.ArrayList; +import java.util.Collection; +import java.util.Collections; +import java.util.EnumMap; +import java.util.HashMap; +import java.util.HashSet; +import java.util.List; +import java.util.Map; +import java.util.Set; +import java.util.concurrent.locks.ReadWriteLock; +import java.util.concurrent.locks.ReentrantReadWriteLock; +import java.util.function.Supplier; +import java.util.stream.Collectors; +import javax.annotation.Nullable; + +import com.google.common.collect.ImmutableMap; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import org.apache.cassandra.config.Config; +import org.apache.cassandra.config.Mutable; +import org.apache.cassandra.config.StringConverters; +import org.apache.cassandra.exceptions.ConfigurationException; +import org.yaml.snakeyaml.introspector.Property; + +import static org.apache.cassandra.config.Properties.defaultLoader; +import static org.apache.cassandra.config.registry.PrimitiveUnaryConverter.convertSafe; +import static org.apache.commons.lang3.ClassUtils.primitiveToWrapper; + + +/** + * This is a simple configuration property registry that stores all the {@link Config} settings, it doesn't + * take into account any configuration changes that might happen during properties replacement between releases. + */ +public class ConfigurationRegistry implements Registry +{ + private static final Logger logger = LoggerFactory.getLogger(ConfigurationRegistry.class); + private final ReadWriteLock rwLock = new ReentrantReadWriteLock(); + private final Supplier<Config> configProvider; + private final Map<ConfigurationListener.ChangeType, ConfigurationListenerList> propertyChangeListeners = new EnumMap<>(ConfigurationListener.ChangeType.class); + private final Map<String, List<TypedConstraintAdapter<?>>> constraints = new HashMap<>(); + private volatile boolean initialized; + private Map<String, PropertyAdapter> properties = Collections.emptyMap(); + + public ConfigurationRegistry(Supplier<Config> configProvider) + { + this.configProvider = configProvider; + // Initialize the property change listeners. + for (ConfigurationListener.ChangeType type : ConfigurationListener.ChangeType.values()) + propertyChangeListeners.put(type, new ConfigurationListenerList()); + } + + private void lazyInit() + { + if (initialized) + return; + + rwLock.writeLock().lock(); + try + { + if (initialized) + return; + properties = ImmutableMap.copyOf(defaultLoader() + .flatten(Config.class) + .entrySet() + .stream() + .map(e -> new AbstractMap.SimpleEntry<>(e.getKey(), new PropertyAdapter(configProvider, e.getValue()))) + .collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue))); + Set<String> leftConstraints = new HashSet<>(constraints.keySet()); + leftConstraints.removeAll(properties.keySet()); + if (!leftConstraints.isEmpty()) + throw new ConfigurationException("Constraints are defined for non-existing properties:" + leftConstraints); + Set<String> leftListeners = propertyChangeListeners.values().stream() + .map(l -> l.wrappers.keySet()) + .flatMap(Collection::stream) + .collect(Collectors.toSet()); + leftListeners.removeAll(properties.keySet()); + if (!leftListeners.isEmpty()) + throw new ConfigurationException("Listeners are defined for non-existing properties:" + leftListeners); + initialized = true; + } + finally + { + rwLock.writeLock().unlock(); + } + } + + @Override public void set(String name, @Nullable Object value) + { + lazyInit(); + PropertyAdapter property = properties.get(name); + validatePropertyExists(property, name); + setInternal(property, value); + } + + /** + * Setter for the property with the given name. Can accept {@code null} value. + * @param property The property. + * @param value The value to set. + */ + private void setInternal(PropertyAdapter property, @Nullable Object value) + { + rwLock.writeLock().lock(); + try + { + Class<?> originalType = property.getType(); + Class<?> sourceType = value == null ? null : value.getClass(); + Object convertedValue = value; + // Do conversion if the value is not null and the type is not the same as the property type. + if (sourceType != null && !primitiveToWrapper(originalType).equals(sourceType)) + { + StringConverters converter; + if (sourceType.equals(String.class) && (converter = StringConverters.fromType(originalType)) != null) + convertedValue = converter.fromString((String) value, originalType); + else + throw new IllegalArgumentException(String.format("No converter found for type '%s'", originalType.getName())); + } + // Do validation first for converted new value. + List<TypedConstraintAdapter<?>> constraintsList = constraints.getOrDefault(property.getName(), Collections.emptyList()); + for (TypedConstraintAdapter<?> typed : constraintsList) + typed.validateTypeCast(convertedValue); + // Do set the value only if the validation passes. + Object oldValue = property.getValue(); + propertyChangeListeners.get(ConfigurationListener.ChangeType.BEFORE).fireTypeCast(property.getName(), oldValue, convertedValue); + property.setValue(convertedValue); + propertyChangeListeners.get(ConfigurationListener.ChangeType.AFTER).fireTypeCast(property.getName(), oldValue, convertedValue); + // This potentially may expose the values that are not safe to see in logs on production. + logger.info("Property '{}' updated from '{}' to '{}'.", property.getName(), oldValue, convertedValue); + } + catch (Exception e) + { + if (e instanceof ConfigurationException) + throw (ConfigurationException) e; + else + throw new ConfigurationException(String.format("Error updating property '%s'; cause: %s", property.getName(), e.getMessage()), e); + } + finally + { + rwLock.writeLock().unlock(); + } + } + + /** + * @param cls Class to cast the property value to. + * @param name the property name to get. + * @return The value of the property with the given name. + */ + public <T> T get(Class<T> cls, String name) + { + lazyInit(); + rwLock.readLock().lock(); + try + { + validatePropertyExists(properties.get(name), name); + Class<?> propertyType = type(name); + Object value = properties.get(name).getValue(); + if (cls.equals(propertyType)) + return convertSafe(cls, value); + else if (cls.equals(String.class)) + { + StringConverters converter = StringConverters.fromType(propertyType); + return cls.cast(converter == null ? TypeConverter.DEFAULT.convertNullable(value) : converter.toString(value)); + } + else + throw new ConfigurationException(String.format("Property '%s' is of type '%s' and cannot be cast to '%s'", + name, propertyType.getCanonicalName(), cls.getCanonicalName())); + } + finally + { + rwLock.readLock().unlock(); + } + } + + @Override public String getString(String name) + { + return get(String.class, name); + } + + /** + * @param name the property name to check. + * @return {@code true} if the property with the given name is available, {@code false} otherwise. + */ + @Override public boolean contains(String name) + { + lazyInit(); + return properties.containsKey(name); + } + + /** + * Returns a set of all the property names. + * @return set of all the property names. + */ + @Override public Iterable<String> keys() + { + lazyInit(); + return properties.keySet(); + } + + /** + * @param name The property name to get the type for. + * @return Property type for the property with the given name. + */ + @Override public Class<?> type(String name) + { + lazyInit(); + validatePropertyExists(properties.get(name), name); + return properties.get(name).getType(); + } + + /** + * @return The number of properties. + */ + @Override public int size() + { + lazyInit(); + return properties.size(); + } + + /** + * @param name The property name to get the type for. + * @return Property type for the property with the given name. + */ + public boolean isWritable(String name) + { + validatePropertyExists(properties.get(name), name); + return properties.get(name).isWritable(); + } + + /** + * Adds a listener for the property with the given name. + * @param name property name to listen to. + * @param listener listener to add. + * @param <T> type of the property. + */ + public <T> void addPropertyChangeListener(String name, ConfigurationListener.ChangeType type, ConfigurationListener<T> listener, Class<T> listenerType) Review Comment: In my view, if you change the name of a property, you must also update the corresponding listener usage. I propose to enforce this rule and avoid supporting different names for the same properties in internal code, so only limited subsystems will work with the `Replaced` annotation and know the old names. The `fail fast` rule will work once the `addPropertyChangeListener` is fired with an incorrect property name. -- 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]

