dcapwell commented on code in PR #2133:
URL: https://github.com/apache/cassandra/pull/2133#discussion_r1146470588


##########
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:
   if you listen to a property that is `@Replaced` what *should* happen?  The 
common case would be a bug (we forgot to update), but there *might* be a case 
where you want to see if this is happening (don't know of one)...
   
   Maybe we should detect this and fail fast, force the caller to say "I know 
what I am doing, I want the old name!"?



##########
src/java/org/apache/cassandra/config/DataStorageSpec.java:
##########
@@ -207,6 +207,14 @@ public long toBytes()
         {
             return unit().toBytes(quantity());
         }
+
+        /**
+         * @return the amount of data storage in mebibytes.
+         */
+        public long toMebibytes()
+        {
+            return unit().toMebibytes(quantity());
+        }

Review Comment:
   unrelated changes must be removed as per our style guide



##########
src/java/org/apache/cassandra/config/registry/Registry.java:
##########
@@ -0,0 +1,95 @@
+/*
+ * 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 javax.annotation.Nullable;
+
+/**
+ * A registry of Cassandra's configuration properties that can be updated at 
runtime. The {@link org.apache.cassandra.config.Config}
+ * class is the source of configuration fields, types and other metadata 
available to the registry. The registry is used to
+ * handle configuration properties that are loaded from the configuration 
file, and are set via JMX or updated through
+ * the settings virtual table.
+ * <p>
+ * You can use {@link #set(String, Object)} to update a property, in case the 
property is not present in the registry,
+ * an exception will be thrown. If the property is present, the registry will 
try to convert given value to the property's
+ * type, and if the conversion fails, an exception will be thrown. You can use 
the {@code String} as a value to be converted,
+ * or you can use the property's type as a value. In the latter case, no 
conversion will be performed.
+ * <p>
+ * You can use {@link #get(Class, String)} to get a property's value, to read 
the value, the registry will try to convert the
+ * property's value if the {@link #getString(String)} to String type (the 
converter is called to convert the value to String).
+ */
+public interface Registry
+{
+    /**
+     * Update configuration property with the given name to the given value. 
The value may be the same
+     * as the property's value, or it may be represented as a string. In the 
latter case a corresponding
+     * will be called to get the property's value matching type.
+     *
+     * @param name Property name.
+     * @param value Value to set.
+     */
+    void set(String name, @Nullable Object value);
+
+    /**
+     * Get property's value by name, The exception will be thrown if the 
property is not present in the registry or
+     * the property's value cannot be converted to given generic type.
+     *
+     * @param <T>  Type to convert to.
+     * @param cls Class to convert to.
+     * @param name Property name.
+     * @return Property's value matching the property's type in the Config.
+     */
+    <T> T get(Class<T> cls, String name);
+
+    /**
+     * Get property's value by name and convert it to the String type. The 
exception will be thrown if the property
+     * is not present in the registry.
+     *
+     * @param name Property name.
+     * @return Property's value converted to String.
+     */
+    String getString(String name);

Review Comment:
   we should remove this API as it is 100% only for SettingsTable, leave this 
problem for the SettingsTable



-- 
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]

Reply via email to