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


##########
src/java/org/apache/cassandra/config/registry/DatabaseConfigurationSource.java:
##########
@@ -0,0 +1,308 @@
+/*
+ * 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.ArrayList;
+import java.util.Iterator;
+import java.util.List;
+import java.util.Map;
+import java.util.concurrent.CopyOnWriteArrayList;
+import java.util.concurrent.locks.ReadWriteLock;
+import java.util.concurrent.locks.ReentrantReadWriteLock;
+import java.util.function.BiConsumer;
+import java.util.function.Consumer;
+import java.util.function.Supplier;
+import java.util.stream.Collectors;
+import javax.annotation.Nullable;
+
+import com.google.common.collect.ImmutableMap;
+import com.google.common.collect.Iterators;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import org.apache.cassandra.config.Config;
+import org.apache.cassandra.config.DatabaseDescriptor;
+import org.apache.cassandra.config.Mutable;
+import org.apache.cassandra.exceptions.ConfigurationException;
+import org.apache.cassandra.exceptions.PropertyNotFoundException;
+import org.apache.cassandra.utils.Pair;
+import org.yaml.snakeyaml.introspector.Property;
+
+import static java.util.Optional.ofNullable;
+import static org.apache.cassandra.config.Properties.defaultLoader;
+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 DatabaseConfigurationSource implements ConfigurationSource
+{
+    private static final Logger logger = 
LoggerFactory.getLogger(DatabaseConfigurationSource.class);
+    private final ReadWriteLock lock = new ReentrantReadWriteLock();
+    private final List<ConfigurationSourceListener> changeListeners = new 
CopyOnWriteArrayList<>();
+    private final List<ConfigurationSourceValidator> validators = new 
ArrayList<>();
+    private final TypeConverterRegistry converters;
+    private final Config source;
+    private final Map<String, Property> properties;
+
+    public DatabaseConfigurationSource(Config source)
+    {
+        this.source = source;
+        properties = ImmutableMap.copyOf(defaultLoader()
+                                         .flatten(Config.class)
+                                         .entrySet()
+                                         .stream()
+                                         
.collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue)));

Review Comment:
   why are you converting to a stream if you are taking the map and creating an 
identical map?



##########
src/java/org/apache/cassandra/config/registry/DatabaseConfigurationSource.java:
##########
@@ -0,0 +1,308 @@
+/*
+ * 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.ArrayList;
+import java.util.Iterator;
+import java.util.List;
+import java.util.Map;
+import java.util.concurrent.CopyOnWriteArrayList;
+import java.util.concurrent.locks.ReadWriteLock;
+import java.util.concurrent.locks.ReentrantReadWriteLock;
+import java.util.function.BiConsumer;
+import java.util.function.Consumer;
+import java.util.function.Supplier;
+import java.util.stream.Collectors;
+import javax.annotation.Nullable;
+
+import com.google.common.collect.ImmutableMap;
+import com.google.common.collect.Iterators;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import org.apache.cassandra.config.Config;
+import org.apache.cassandra.config.DatabaseDescriptor;
+import org.apache.cassandra.config.Mutable;
+import org.apache.cassandra.exceptions.ConfigurationException;
+import org.apache.cassandra.exceptions.PropertyNotFoundException;
+import org.apache.cassandra.utils.Pair;
+import org.yaml.snakeyaml.introspector.Property;
+
+import static java.util.Optional.ofNullable;
+import static org.apache.cassandra.config.Properties.defaultLoader;
+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 DatabaseConfigurationSource implements ConfigurationSource
+{
+    private static final Logger logger = 
LoggerFactory.getLogger(DatabaseConfigurationSource.class);
+    private final ReadWriteLock lock = new ReentrantReadWriteLock();
+    private final List<ConfigurationSourceListener> changeListeners = new 
CopyOnWriteArrayList<>();
+    private final List<ConfigurationSourceValidator> validators = new 
ArrayList<>();
+    private final TypeConverterRegistry converters;
+    private final Config source;
+    private final Map<String, Property> properties;
+
+    public DatabaseConfigurationSource(Config source)
+    {
+        this.source = source;
+        properties = ImmutableMap.copyOf(defaultLoader()
+                                         .flatten(Config.class)
+                                         .entrySet()
+                                         .stream()
+                                         
.collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue)));
+        converters = TypeConverterRegistry.instance;
+        // Initialize the configuration handlers.
+        registerConfigurationValidators(this::addConfigurationValidator);
+    }
+
+    private static void 
registerConfigurationValidators(Consumer<ConfigurationSourceValidator> adder)
+    {
+        adder.accept(DatabaseDescriptor::validateUpperBoundStreamingConfig);
+        
adder.accept(createLoggingValidator(DatabaseDescriptor::validateRepairSessionSpace));
+        adder.accept(DatabaseDescriptor::validateConcurrentCompactors);
+    }
+
+    @Override
+    public <T> void set(String name, T value)
+    {
+        Property property = ofNullable(properties.get(name)).orElseThrow(() -> 
notFound(name));
+        lock.writeLock().lock();
+        try
+        {
+            Class<?> originalType = property.getType();
+            // Do conversion if the value is not null and the type is not the 
same as the property type.
+            Object convertedValue = ofNullable(value)
+                                    .map(Object::getClass)
+                                    .map(from -> converters.get(from, 
originalType)
+                                                           .convert(value))
+                                    .orElse(null);
+            // Use given converted value if it is not null, otherwise use the 
default value.
+            ConfigurationSource validationSource = 
PropertyValidationSource.create(this, name, convertedValue);
+            for (ConfigurationSourceValidator validator : validators)
+                validator.validate(validationSource);

Review Comment:
   this should be scoped to the field and not a global set of validators...



##########
src/java/org/apache/cassandra/config/registry/TypeConverterRegistry.java:
##########
@@ -0,0 +1,131 @@
+/*
+ * 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.HashMap;
+import java.util.Map;
+import java.util.Objects;
+
+import org.apache.cassandra.config.DataRateSpec;
+import org.apache.cassandra.config.DataStorageSpec;
+import org.apache.cassandra.config.DurationSpec;
+import org.apache.cassandra.db.ConsistencyLevel;
+import org.apache.cassandra.exceptions.ConfigurationException;
+
+import static org.apache.commons.lang3.ClassUtils.primitiveToWrapper;
+
+/**
+ * A registry for {@link TypeConverter} instances.
+ */
+public class TypeConverterRegistry
+{
+    public static final TypeConverterRegistry instance = new 
TypeConverterRegistry();
+    private final Map<ConverterKey, TypeConverter<?>> converters = new 
HashMap<>();
+
+    private TypeConverterRegistry()
+    {
+        registerConverters(converters);
+    }
+
+    public <V> TypeConverter<V> get(Class<?> from, Class<V> to)
+    {
+        Class<?> fromShaded = primitiveToWrapper(from);
+        TypeConverter<V> converter = get(fromShaded, to, null);
+        if (converter == null)
+            throw new ConfigurationException(String.format("No converter found 
from '%s' to '%s'", fromShaded.getCanonicalName(), to.getCanonicalName()));
+        return converter;
+    }
+
+    @SuppressWarnings("unchecked")
+    public <V> TypeConverter<V> get(Class<?> from, Class<V> to, 
TypeConverter<V> defaultConverter)
+    {
+        Class<?> fromShaded = primitiveToWrapper(from);
+        if (fromShaded.equals(to))
+            return to::cast;
+        if (converters.get(key(fromShaded, to)) == null)
+            return defaultConverter;
+        return (TypeConverter<V>) converters.get(key(fromShaded, to));
+    }
+
+    private static void registerConverters(Map<ConverterKey, TypeConverter<?>> 
converters)

Review Comment:
   we have `org.apache.cassandra.config.YamlConfigurationLoader#updateFromMap` 
which makes sure we always reuse the same input set that was supported in 
YAML... is there a reason to reinvent this?



##########
src/java/org/apache/cassandra/config/registry/DatabaseConfigurationSource.java:
##########
@@ -0,0 +1,308 @@
+/*
+ * 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.ArrayList;
+import java.util.Iterator;
+import java.util.List;
+import java.util.Map;
+import java.util.concurrent.CopyOnWriteArrayList;
+import java.util.concurrent.locks.ReadWriteLock;
+import java.util.concurrent.locks.ReentrantReadWriteLock;
+import java.util.function.BiConsumer;
+import java.util.function.Consumer;
+import java.util.function.Supplier;
+import java.util.stream.Collectors;
+import javax.annotation.Nullable;
+
+import com.google.common.collect.ImmutableMap;
+import com.google.common.collect.Iterators;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import org.apache.cassandra.config.Config;
+import org.apache.cassandra.config.DatabaseDescriptor;
+import org.apache.cassandra.config.Mutable;
+import org.apache.cassandra.exceptions.ConfigurationException;
+import org.apache.cassandra.exceptions.PropertyNotFoundException;
+import org.apache.cassandra.utils.Pair;
+import org.yaml.snakeyaml.introspector.Property;
+
+import static java.util.Optional.ofNullable;
+import static org.apache.cassandra.config.Properties.defaultLoader;
+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 DatabaseConfigurationSource implements ConfigurationSource
+{
+    private static final Logger logger = 
LoggerFactory.getLogger(DatabaseConfigurationSource.class);
+    private final ReadWriteLock lock = new ReentrantReadWriteLock();

Review Comment:
   the locking in this class isn't thread safe, but may also cause performance 
regressions... the current model does not need locking, so not sure why we need 
to add a new set of locks (and global at that)



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