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


##########
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:
   The reason I used an explicit lock is to prevent contention within the CHM 
bucket but I guess this is premature optimization so I'm ok to remove and rely 
only on CHM.



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