skoppu22 commented on code in PR #358:
URL: https://github.com/apache/cassandra-sidecar/pull/358#discussion_r3374686528


##########
server/src/main/java/org/apache/cassandra/sidecar/configmanagement/ConfigUtils.java:
##########
@@ -0,0 +1,157 @@
+/*
+ * 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.io.IOException;
+import java.io.UncheckedIOException;
+import java.nio.file.Files;
+import java.nio.file.Path;
+import java.time.Instant;
+import java.util.Collections;
+import java.util.Map;
+
+import com.fasterxml.jackson.core.JsonParser;
+import com.fasterxml.jackson.dataformat.yaml.YAMLFactory;
+import com.fasterxml.jackson.dataformat.yaml.YAMLGenerator;
+import io.vertx.core.json.JsonObject;
+import io.vertx.core.json.jackson.DatabindCodec;
+
+/**
+ * Utility methods for configuration operations: YAML loading and deep merge.
+ */
+public final class ConfigUtils
+{
+    static final YAMLFactory YAML_FACTORY = new YAMLFactory()
+            .disable(YAMLGenerator.Feature.WRITE_DOC_START_MARKER);
+
+    private ConfigUtils()
+    {
+        throw new UnsupportedOperationException();
+    }
+
+    /**
+     * Loads a YAML configuration file and returns it as a {@link 
ConfigurationOverlaySnapshot}.
+     * The snapshot's {@code lastModified} is set to the file's modification 
time and
+     * {@code extraJvmOpts} is empty.
+     *
+     * <p>If {@code yamlPath} is {@code null}, returns an {@linkplain 
ConfigurationOverlaySnapshot#emptySnapshot()
+     * empty snapshot}.
+     *
+     * @param yamlPath path to the YAML configuration file, or {@code null} 
for an empty snapshot
+     * @return a snapshot representing the file contents
+     */
+    public static ConfigurationOverlaySnapshot loadConfiguration(Path yamlPath)
+    {
+        if (yamlPath == null)
+        {
+            return ConfigurationOverlaySnapshot.emptySnapshot();
+        }
+        JsonObject yaml = loadYaml(yamlPath);
+        Instant lastModified;
+        try
+        {
+            lastModified = Files.getLastModifiedTime(yamlPath).toInstant();

Review Comment:
   There is no synchronization to avoid race condition between loading yaml at 
line 65 and retrieving last modified time at line 69. There is a possibility of 
associating different modified time for older yaml if the file was modified in 
between.
   
   As we are reading from disk, 
   - either we need to read both contents and timestamp in a single call if 
such an API exists
   - or do optimistic synchronization
   t1 = read modified time
   data = read contents
   t2 = read modified time 
   if t2 != t1, file was modified and repeat the action.
   
   



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