smiklosovic commented on code in PR #4557:
URL: https://github.com/apache/cassandra/pull/4557#discussion_r2686400547


##########
src/java/org/apache/cassandra/config/StartupChecksConfiguration.java:
##########
@@ -0,0 +1,173 @@
+/*
+ * 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;
+
+import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+
+import com.google.common.annotations.VisibleForTesting;
+
+import org.apache.cassandra.exceptions.StartupException;
+import org.apache.cassandra.service.StartupCheck;
+import org.apache.cassandra.service.StartupChecks;
+
+import static java.lang.Boolean.FALSE;
+import static java.lang.Boolean.TRUE;
+
+public class StartupChecksConfiguration
+{
+    public static final String ENABLED_PROPERTY = "enabled";
+
+    private final Map<String, Map<String, Object>> options = new HashMap<>();
+    private final StartupChecks startupChecks;
+
+    public StartupChecksConfiguration(StartupChecks startupChecks, final 
Map<String, Map<String, Object>> options)
+    {
+        this.options.putAll(options);
+        this.startupChecks = startupChecks;
+
+        apply(startupChecks, options);
+    }
+
+    @VisibleForTesting
+    public StartupCheck getCheck(String name)
+    {
+        return startupChecks.getCheck(name);
+    }
+
+    private StartupCheck getConfigurableCheck(String name)
+    {
+        StartupCheck check = startupChecks.getCheck(name);
+        if (check == null)
+            return null;
+
+        if (!check.isConfigurable())
+            return null;
+
+        return check;
+    }
+
+    public void set(final String name, final String key, final Object value)
+    {
+        StartupCheck check = getConfigurableCheck(name);
+        if (check == null)
+            return;
+
+        Map<String, Object> checkConfiguration = options.get(name);
+        if (checkConfiguration == null)
+            return;
+
+        checkConfiguration.put(key, value);
+    }
+
+    public void reset(String name)
+    {
+        StartupCheck check = getConfigurableCheck(name);
+        if (check == null)
+            return;
+
+        Map<String, Object> checkConfiguration = options.get(name);
+        if (checkConfiguration == null)
+            return;
+
+        boolean enabled = isEnabled(name);
+
+        checkConfiguration.clear();
+
+        checkConfiguration.put(ENABLED_PROPERTY, enabled);
+    }
+
+    public void enable(final String name)
+    {
+        set(name, ENABLED_PROPERTY, TRUE);
+    }
+
+    public void disable(final String name)
+    {
+        StartupCheck check = getConfigurableCheck(name);
+        if (check == null)
+            return;
+
+        set(name, ENABLED_PROPERTY, FALSE);
+    }
+
+    public boolean isEnabled(final String name)
+    {
+        Map<String, Object> config = getConfig(name);
+        if (config == null)
+            return false;
+
+        Object enabledBoolean = config.get(ENABLED_PROPERTY);
+        if (enabledBoolean == null)
+            return false;
+
+        return Boolean.parseBoolean(enabledBoolean.toString());
+    }
+
+    public boolean isDisabled(final String name)
+    {
+        return !isEnabled(name);
+    }
+
+    public Map<String, Object> getConfig(final String name)
+    {
+        return options.get(name);
+    }
+
+    private void apply(StartupChecks startupChecks, Map<String, Map<String, 
Object>> options)
+    {
+        List<String> notExistingCheckNames = new ArrayList<>();
+        List<String> notConfigurableCheckNames = new ArrayList<>();
+
+        for (Map.Entry<String, Map<String, Object>> userConfigEntry : 
options.entrySet())
+        {
+            String key = userConfigEntry.getKey();
+            StartupCheck check = startupChecks.getCheck(key);
+            if (check == null)
+                notExistingCheckNames.add(key);
+            else if (!check.isConfigurable())
+                notConfigurableCheckNames.add(key);
+        }
+
+        if (!notExistingCheckNames.isEmpty())
+            throw new IllegalStateException("There are configuration entries 
for startup checks which do not exist: " + notExistingCheckNames);
+        if (!notConfigurableCheckNames.isEmpty())
+            throw new IllegalStateException("There are configuration entries 
for startup checks which are not configurable: " + notConfigurableCheckNames);
+
+        for (StartupCheck check : startupChecks.getChecks())
+        {
+            String startupCheckName = check.name();
+            Map<String, Object> configMap = 
this.options.computeIfAbsent(startupCheckName, k -> new HashMap<>());
+            if (configMap.containsKey(ENABLED_PROPERTY))
+                configMap.putIfAbsent(ENABLED_PROPERTY, FALSE);

Review Comment:
   from javadoc:
   
   `If the specified key is not already associated with a value (or is mapped 
to null) associates it with the given value and returns null, else returns the 
current value.`
   
   This covers cases when there is "enabled" as a key in a map but its value is 
null. We do not want that. If it is ever null then we want to explicitly put 
there false.
   
   



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