isaacreath commented on code in PR #351:
URL: https://github.com/apache/cassandra-sidecar/pull/351#discussion_r3259489052


##########
server/src/main/java/org/apache/cassandra/sidecar/configmanagement/CassandraConfigurationOverlay.java:
##########
@@ -0,0 +1,172 @@
+/*
+ * 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.sidecar.configmanagement;
+
+import java.util.ArrayList;
+import java.util.Collections;
+import java.util.List;
+import java.util.Map;
+import java.util.Objects;
+
+import com.fasterxml.jackson.annotation.JsonCreator;
+import com.fasterxml.jackson.annotation.JsonProperty;
+import com.fasterxml.jackson.databind.JsonNode;
+import com.fasterxml.jackson.databind.ObjectMapper;
+import com.fasterxml.jackson.databind.node.ObjectNode;
+import org.jetbrains.annotations.NotNull;
+import org.jetbrains.annotations.Nullable;
+
+/**
+ * Represents a configuration overlay - a sparse set of configuration values 
that overwrite base template
+ * values or add new configuration attributes.
+ *
+ * <p>The {@code cassandraYaml} field is a version-agnostic JSON 
representation of {@code cassandra.yaml}
+ * settings. It may contain settings from any Cassandra version supported by 
Sidecar (4.0, 4.1, 5.0, etc.).
+ * No version-specific validation is performed by this class; validation 
against a version-aware schema is
+ * the responsibility of the Configuration Manager.
+ *
+ * <p>The {@code extraJvmOpts} field contains JVM options that are appended to 
the Cassandra JVM startup
+ * command. These are opaque strings not subject to schema validation.
+ */
+public class CassandraConfigurationOverlay
+{
+    private static final ObjectMapper MAPPER = new ObjectMapper();
+
+    @NotNull
+    private final JsonNode cassandraYaml;
+
+    @NotNull
+    private final List<String> extraJvmOpts;
+
+    @JsonCreator
+    public CassandraConfigurationOverlay(@JsonProperty("cassandraYaml") 
@Nullable JsonNode cassandraYaml,
+                                         @JsonProperty("extraJvmOpts") 
@Nullable List<String> extraJvmOpts)
+    {
+        if (cassandraYaml == null)
+        {
+            this.cassandraYaml = MAPPER.createObjectNode();
+        }
+        else if (!cassandraYaml.isObject())
+        {
+            throw new IllegalArgumentException("cassandraYaml must be a JSON 
object, got " + cassandraYaml.getNodeType());
+        }
+        else
+        {
+            this.cassandraYaml = cassandraYaml;
+        }
+        this.extraJvmOpts = extraJvmOpts != null
+                            ? Collections.unmodifiableList(new 
ArrayList<>(extraJvmOpts))
+                            : Collections.emptyList();
+    }
+
+    /**
+     * @return the cassandra.yaml overlay as a version-agnostic JSON object
+     */
+    @JsonProperty("cassandraYaml")
+    @NotNull
+    public JsonNode cassandraYaml()
+    {
+        return cassandraYaml;

Review Comment:
   It's technically possible to mutate `cassandraYaml` by accessing the value 
from here, casting it to a `ObjectNode`, then using `ObjectNode#put` to update 
a key. 
   
   To preserve the immutable property of this object, we may want to consider 
returning a copy of the `cassandraYaml`.  That said, this could create a lot of 
unnecessary objects if the immutability properties aren't necessary. 



##########
server/src/main/java/org/apache/cassandra/sidecar/configmanagement/CassandraConfigurationOverlay.java:
##########
@@ -0,0 +1,172 @@
+/*
+ * 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.sidecar.configmanagement;
+
+import java.util.ArrayList;
+import java.util.Collections;
+import java.util.List;
+import java.util.Map;
+import java.util.Objects;
+
+import com.fasterxml.jackson.annotation.JsonCreator;
+import com.fasterxml.jackson.annotation.JsonProperty;
+import com.fasterxml.jackson.databind.JsonNode;
+import com.fasterxml.jackson.databind.ObjectMapper;
+import com.fasterxml.jackson.databind.node.ObjectNode;
+import org.jetbrains.annotations.NotNull;
+import org.jetbrains.annotations.Nullable;
+
+/**
+ * Represents a configuration overlay - a sparse set of configuration values 
that overwrite base template
+ * values or add new configuration attributes.
+ *
+ * <p>The {@code cassandraYaml} field is a version-agnostic JSON 
representation of {@code cassandra.yaml}
+ * settings. It may contain settings from any Cassandra version supported by 
Sidecar (4.0, 4.1, 5.0, etc.).
+ * No version-specific validation is performed by this class; validation 
against a version-aware schema is
+ * the responsibility of the Configuration Manager.
+ *
+ * <p>The {@code extraJvmOpts} field contains JVM options that are appended to 
the Cassandra JVM startup
+ * command. These are opaque strings not subject to schema validation.
+ */
+public class CassandraConfigurationOverlay
+{
+    private static final ObjectMapper MAPPER = new ObjectMapper();
+
+    @NotNull
+    private final JsonNode cassandraYaml;
+
+    @NotNull
+    private final List<String> extraJvmOpts;
+
+    @JsonCreator
+    public CassandraConfigurationOverlay(@JsonProperty("cassandraYaml") 
@Nullable JsonNode cassandraYaml,
+                                         @JsonProperty("extraJvmOpts") 
@Nullable List<String> extraJvmOpts)
+    {
+        if (cassandraYaml == null)
+        {
+            this.cassandraYaml = MAPPER.createObjectNode();
+        }
+        else if (!cassandraYaml.isObject())
+        {
+            throw new IllegalArgumentException("cassandraYaml must be a JSON 
object, got " + cassandraYaml.getNodeType());
+        }
+        else
+        {
+            this.cassandraYaml = cassandraYaml;
+        }
+        this.extraJvmOpts = extraJvmOpts != null
+                            ? Collections.unmodifiableList(new 
ArrayList<>(extraJvmOpts))
+                            : Collections.emptyList();
+    }
+
+    /**
+     * @return the cassandra.yaml overlay as a version-agnostic JSON object
+     */
+    @JsonProperty("cassandraYaml")
+    @NotNull
+    public JsonNode cassandraYaml()
+    {
+        return cassandraYaml;
+    }
+
+    /**
+     * @return an unmodifiable list of extra JVM options
+     */
+    @JsonProperty("extraJvmOpts")
+    @NotNull
+    public List<String> extraJvmOpts()
+    {
+        return extraJvmOpts;
+    }
+
+    /**
+     * Returns a new overlay with the given updates applied. The current 
instance is not modified.
+     *
+     * @param cassandraYamlUpdates field-level changes to cassandra.yaml: key 
= field name, value = new value.
+     *                             A null or {@link 
com.fasterxml.jackson.databind.node.NullNode} value removes
+     *                             the field. Pass {@code null} for no yaml 
changes.
+     * @param addJvmOpts           JVM options to append to the current list. 
Pass {@code null} for no additions.
+     * @param removeJvmOpts        JVM options to remove by value. Pass {@code 
null} for no removals.
+     * @return a new overlay with the updates applied
+     */
+    @NotNull
+    public CassandraConfigurationOverlay updated(@Nullable Map<String, 
JsonNode> cassandraYamlUpdates,
+                                                 @Nullable List<String> 
addJvmOpts,
+                                                 @Nullable List<String> 
removeJvmOpts)

Review Comment:
   Right now this function provides differing approaches for updating values 
between `cassandraYaml` and `extraJvmOpts`.  
   
   For `cassandraYaml`, if I want to update an existing override, I pass the 
new value in through the `cassandraYamlUpdates` map.
   
   For `extraJvmpOpts`, I need to add the existing JVM option to 
`removeJvmOpts` and the new value to `addJvmOpts`. 
   
   Is there any way to offer a similar approach for both so that the semantics 
are consistent for callers?



##########
server/src/main/java/org/apache/cassandra/sidecar/configmanagement/ConfigurationOverlaySnapshot.java:
##########
@@ -0,0 +1,138 @@
+/*
+ * 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.sidecar.configmanagement;
+
+import java.security.MessageDigest;
+import java.security.NoSuchAlgorithmException;
+import java.time.Instant;
+import java.util.Objects;
+
+import com.fasterxml.jackson.core.JsonProcessingException;
+import com.fasterxml.jackson.databind.ObjectMapper;
+import com.fasterxml.jackson.databind.node.ObjectNode;
+import org.jetbrains.annotations.NotNull;
+
+/**
+ * Represents a snapshot of a configuration overlay with its metadata.
+ * The SHA-256 hash is dynamically computed from the overlay contents and 
cached.
+ */
+public class ConfigurationOverlaySnapshot
+{
+    private static final ObjectMapper MAPPER = new ObjectMapper();
+
+    @NotNull
+    private final Instant lastModified;
+
+    @NotNull
+    private final CassandraConfigurationOverlay configuration;
+
+    private volatile String hash;
+
+    public ConfigurationOverlaySnapshot(@NotNull Instant lastModified,
+                                        @NotNull CassandraConfigurationOverlay 
configuration)
+    {
+        this.lastModified = Objects.requireNonNull(lastModified, "lastModified 
must not be null");
+        this.configuration = Objects.requireNonNull(configuration, 
"configuration must not be null");
+    }
+
+    /**
+     * Returns the SHA-256 hash of the overlay contents, prefixed with 
"sha256:".
+     * Computed on first access and cached for subsequent calls.
+     *
+     * @return the content hash in the form "sha256:&lt;64 hex chars&gt;"
+     */
+    @NotNull
+    public String hash()
+    {
+        if (hash == null)
+        {
+            hash = computeHash();
+        }
+        return hash;
+    }
+
+    @NotNull
+    public Instant lastModified()
+    {
+        return lastModified;
+    }
+
+    @NotNull
+    public CassandraConfigurationOverlay configuration()
+    {
+        return configuration;
+    }
+
+    private String computeHash()
+    {
+        try
+        {
+            byte[] bytes = MAPPER.writeValueAsBytes(configuration);
+            MessageDigest digest = MessageDigest.getInstance("SHA-256");
+            byte[] hashBytes = digest.digest(bytes);
+            return "sha256:" + bytesToHex(hashBytes);
+        }
+        catch (JsonProcessingException | NoSuchAlgorithmException e)
+        {
+            throw new RuntimeException("Failed to compute configuration hash", 
e);
+        }
+    }
+
+    private static String bytesToHex(byte[] bytes)
+    {
+        StringBuilder sb = new StringBuilder(bytes.length * 2);
+        for (byte b : bytes)
+        {
+            sb.append(String.format("%02x", b));
+        }
+        return sb.toString();
+    }
+
+    @Override
+    public boolean equals(Object o)
+    {
+        if (this == o)
+        {
+            return true;
+        }
+        if (o == null || getClass() != o.getClass())
+        {
+            return false;
+        }
+        ConfigurationOverlaySnapshot that = (ConfigurationOverlaySnapshot) o;
+        return Objects.equals(lastModified, that.lastModified)
+               && Objects.equals(configuration, that.configuration);
+    }
+
+    @Override
+    public int hashCode()
+    {
+        return Objects.hash(lastModified, configuration);
+    }
+
+    @Override
+    public String toString()
+    {
+        ObjectNode node = MAPPER.createObjectNode();
+        node.put("hash", hash());
+        node.put("lastModified", lastModified.toString());
+        node.set("configuration", MAPPER.valueToTree(configuration));
+        return node.toString();

Review Comment:
   Nit: Do we want to consider using a pretty print string here? (i.e. 
`toPrettyString()`). Could make debugging easier. 



##########
server/src/main/java/org/apache/cassandra/sidecar/configmanagement/ConfigurationProvider.java:
##########
@@ -0,0 +1,66 @@
+/*
+ * 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.sidecar.configmanagement;
+
+import org.apache.cassandra.sidecar.cluster.instance.InstanceMetadata;
+import org.jetbrains.annotations.NotNull;
+import org.jetbrains.annotations.Nullable;
+
+/**
+ * Provides storage and retrieval of configuration overlays for Cassandra 
instances.
+ *
+ * <p>The provider is a pluggable abstraction that decouples configuration 
storage from the
+ * Configuration Manager. Implementations may persist overlays locally 
(files), remotely
+ * (etcd, Consul, HTTP APIs), or in-memory (for testing).
+ *
+ * <p>The provider stores version-agnostic overlays and does not perform 
version-specific
+ * validation or merge logic. Validation against a version-aware schema and 
computing updated
+ * overlays (via {@link CassandraConfigurationOverlay#updated}) are the 
responsibility of the
+ * Configuration Manager.
+ */
+public interface ConfigurationProvider
+{
+    /**
+     * Retrieve the configuration overlay for the given Cassandra instance.
+     *
+     * @param instance the Cassandra instance metadata
+     * @return the configuration overlay snapshot, or {@code null} if no 
overlay exists for the instance
+     */
+    @Nullable
+    ConfigurationOverlaySnapshot getConfiguration(InstanceMetadata instance);

Review Comment:
   Did you consider `getOverlay` / `storeOverlay` instead of `getConfiguration` 
/ `storeConfiguration`? The use of overlay can indicate to the reader of 
callers of this interface that we are not getting a fully materialized config, 
but it may not be a big deal. 



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