frankgh commented on code in PR #351: URL: https://github.com/apache/cassandra-sidecar/pull/351#discussion_r3269969461
########## server/src/main/java/org/apache/cassandra/sidecar/configmanagement/CassandraConfigurationOverlay.java: ########## @@ -0,0 +1,175 @@ +/* + * 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(); Review Comment: ideally we should try to use the framework's JSON functionality to avoid allocating additional resources (for JSON processing) etc. ########## server/src/main/java/org/apache/cassandra/sidecar/configmanagement/InMemoryConfigurationProvider.java: ########## @@ -0,0 +1,69 @@ +/* + * 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.Objects; +import java.util.concurrent.ConcurrentHashMap; + +import org.apache.cassandra.sidecar.cluster.instance.InstanceMetadata; +import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; + +/** + * In-memory implementation of {@link ConfigurationProvider} for testing and as a reference implementation. + * Stores configuration overlays in a {@link ConcurrentHashMap} keyed by instance ID. + */ +public class InMemoryConfigurationProvider implements ConfigurationProvider +{ + private final ConcurrentHashMap<Integer, ConfigurationOverlaySnapshot> overlays = new ConcurrentHashMap<>(); + private final ConcurrentHashMap<Integer, Object> locks = new ConcurrentHashMap<>(); + + @Override + @Nullable + public ConfigurationOverlaySnapshot getOverlay(InstanceMetadata instance) + { + return overlays.get(instance.id()); + } + + @Override + public boolean storeOverlay(InstanceMetadata instance, + @Nullable String originalHash, + @NotNull ConfigurationOverlaySnapshot newSnapshot) + { + Objects.requireNonNull(newSnapshot, "newSnapshot must not be null"); + Object lock = locks.computeIfAbsent(instance.id(), k -> new Object()); + synchronized (lock) + { + ConfigurationOverlaySnapshot current = overlays.get(instance.id()); + + if (current == null && originalHash != null) + { + return false; + } + + if (current != null && (originalHash == null || !current.hash().equals(originalHash))) + { + return false; + } + + overlays.put(instance.id(), newSnapshot); + return true; + } Review Comment: do we really need locks here? Isn't the concurrent hashmap already a DS that supports this natively? ```suggestion return overlays.compute(instance.id(), (k, current) -> { if (current == null && originalHash != null) { return null; } if (current != null && (originalHash == null || !current.hash().equals(originalHash))) { return current; } return newSnapshot; }) == newSnapshot; ``` -- 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]

