This is an automated email from the ASF dual-hosted git repository.

jbonofre pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/karaf.git


The following commit(s) were added to refs/heads/main by this push:
     new a217caf65c Save the features state and the region digraph atomically. 
(#3001)
a217caf65c is described below

commit a217caf65c4363e3b61cb2455a352b448289fb61
Author: James Roome <[email protected]>
AuthorDate: Tue Nov 10 22:32:58 2026 +0530

    Save the features state and the region digraph atomically. (#3001)
    
    * Save the features state and the region digraph atomically.
    
    * Code review fixes
---
 .../karaf/features/internal/osgi/Activator.java    |   8 ++
 .../features/internal/region/DigraphHelper.java    |  15 +--
 .../features/internal/service/StateStorage.java    |  22 ++--
 .../internal/osgi/ActivatorStateStorageTest.java   | 116 +++++++++++++++++++++
 .../internal/region/DigraphHelperTest.java         |  71 +++++++++++++
 .../main/java/org/apache/karaf/util/FileUtils.java |  63 +++++++++++
 .../java/org/apache/karaf/util/FileUtilsTest.java  |  85 +++++++++++++++
 7 files changed, 365 insertions(+), 15 deletions(-)

diff --git 
a/features/core/src/main/java/org/apache/karaf/features/internal/osgi/Activator.java
 
b/features/core/src/main/java/org/apache/karaf/features/internal/osgi/Activator.java
index 3daaaf6153..3cc22c38af 100644
--- 
a/features/core/src/main/java/org/apache/karaf/features/internal/osgi/Activator.java
+++ 
b/features/core/src/main/java/org/apache/karaf/features/internal/osgi/Activator.java
@@ -54,8 +54,10 @@ import 
org.apache.karaf.features.internal.service.FeaturesServiceImpl;
 import org.apache.karaf.features.internal.service.SimpleFeaturesServiceImpl;
 import org.apache.karaf.features.internal.service.BundleInstallSupport;
 import org.apache.karaf.features.internal.service.BundleInstallSupportImpl;
+import org.apache.karaf.features.internal.service.State;
 import org.apache.karaf.features.internal.service.StateStorage;
 import org.apache.karaf.features.internal.util.SystemExitManager;
+import org.apache.karaf.util.FileUtils;
 import org.apache.karaf.util.ThreadUtils;
 import org.apache.karaf.features.spi.MavenResolverFactory;
 import org.apache.karaf.util.tracker.BaseActivator;
@@ -344,6 +346,12 @@ public class Activator extends BaseActivator {
                 File file = bundleContext.getDataFile(STATE_FILE);
                 return new FileOutputStream(file);
             }
+
+            @Override
+            public void save(State state) throws IOException {
+                // A failure or a crash while saving never leaves a truncated 
state
+                FileUtils.atomicSave(bundleContext.getDataFile(STATE_FILE), 
out -> write(state, out));
+            }
         };
         return stateStorage;
     }
diff --git 
a/features/core/src/main/java/org/apache/karaf/features/internal/region/DigraphHelper.java
 
b/features/core/src/main/java/org/apache/karaf/features/internal/region/DigraphHelper.java
index 5897415c65..7bf2c45a45 100644
--- 
a/features/core/src/main/java/org/apache/karaf/features/internal/region/DigraphHelper.java
+++ 
b/features/core/src/main/java/org/apache/karaf/features/internal/region/DigraphHelper.java
@@ -21,7 +21,6 @@ import static 
org.apache.karaf.features.internal.util.MapUtils.addToMapSet;
 import java.io.DataInputStream;
 import java.io.File;
 import java.io.FileInputStream;
-import java.io.FileOutputStream;
 import java.io.IOException;
 import java.io.InputStream;
 import java.io.OutputStream;
@@ -35,6 +34,7 @@ import java.util.Map;
 import java.util.Set;
 
 import org.apache.karaf.features.internal.service.FeaturesServiceImpl;
+import org.apache.karaf.util.FileUtils;
 import org.apache.karaf.util.json.JsonReader;
 import org.apache.karaf.util.json.JsonWriter;
 import org.eclipse.equinox.internal.region.StandardRegionDigraph;
@@ -45,11 +45,15 @@ import org.osgi.framework.Bundle;
 import org.osgi.framework.BundleContext;
 import org.osgi.framework.BundleException;
 import org.osgi.framework.InvalidSyntaxException;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
 
 public final class DigraphHelper {
 
     public static final String DIGRAPH_FILE = "digraph.json";
 
+    private static final Logger LOGGER = 
LoggerFactory.getLogger(DigraphHelper.class);
+
     private static final String REGIONS = "regions";
     private static final String EDGES = "edges";
     private static final String TAIL = "tail";
@@ -76,12 +80,11 @@ public final class DigraphHelper {
     }
 
     public static void saveDigraph(File outFile, RegionDigraph digraph) {
-        try (
-            FileOutputStream out = new FileOutputStream(outFile)
-        ) {
-            saveDigraph(digraph, out);
+        // A failure or a crash while saving never leaves a truncated digraph
+        try {
+            FileUtils.atomicSave(outFile, out -> saveDigraph(digraph, out));
         } catch (Exception e) {
-            // Ignore
+            LOGGER.warn("Error saving the region digraph to {}", outFile, e);
         }
     }
 
diff --git 
a/features/core/src/main/java/org/apache/karaf/features/internal/service/StateStorage.java
 
b/features/core/src/main/java/org/apache/karaf/features/internal/service/StateStorage.java
index 0d1fa980f0..a7548cfc8c 100644
--- 
a/features/core/src/main/java/org/apache/karaf/features/internal/service/StateStorage.java
+++ 
b/features/core/src/main/java/org/apache/karaf/features/internal/service/StateStorage.java
@@ -59,19 +59,23 @@ public abstract class StateStorage {
                 OutputStream os = getOutputStream()
         ) {
             if (os != null) {
-                Map<String, Object> json = new HashMap<>();
-                json.put("bootDone", state.bootDone.get());
-                json.put("repositories", state.repositories);
-                json.put("features", state.requirements);
-                json.put("installed", state.installedFeatures);
-                json.put("state", state.stateFeatures);
-                json.put("managed", state.managedBundles);
-                json.put("checksums", toStringLongMap(state.bundleChecksums));
-                JsonWriter.write(os, json);
+                write(state, os);
             }
         }
     }
 
+    protected void write(State state, OutputStream os) throws IOException {
+        Map<String, Object> json = new HashMap<>();
+        json.put("bootDone", state.bootDone.get());
+        json.put("repositories", state.repositories);
+        json.put("features", state.requirements);
+        json.put("installed", state.installedFeatures);
+        json.put("state", state.stateFeatures);
+        json.put("managed", state.managedBundles);
+        json.put("checksums", toStringLongMap(state.bundleChecksums));
+        JsonWriter.write(os, json);
+    }
+
     protected abstract InputStream getInputStream() throws IOException;
 
     protected abstract OutputStream getOutputStream() throws IOException;
diff --git 
a/features/core/src/test/java/org/apache/karaf/features/internal/osgi/ActivatorStateStorageTest.java
 
b/features/core/src/test/java/org/apache/karaf/features/internal/osgi/ActivatorStateStorageTest.java
new file mode 100644
index 0000000000..01c1e15946
--- /dev/null
+++ 
b/features/core/src/test/java/org/apache/karaf/features/internal/osgi/ActivatorStateStorageTest.java
@@ -0,0 +1,116 @@
+/*
+ * 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.karaf.features.internal.osgi;
+
+import static org.easymock.EasyMock.anyString;
+import static org.easymock.EasyMock.expect;
+import static org.easymock.EasyMock.getCurrentArguments;
+import static org.easymock.EasyMock.mock;
+import static org.easymock.EasyMock.replay;
+import static org.junit.Assert.assertArrayEquals;
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.fail;
+
+import java.io.File;
+import java.lang.reflect.Method;
+import java.nio.file.Files;
+import java.util.Collections;
+import java.util.Map;
+
+import org.apache.karaf.features.internal.service.State;
+import org.apache.karaf.features.internal.service.StateStorage;
+import org.junit.Before;
+import org.junit.Rule;
+import org.junit.Test;
+import org.junit.rules.TemporaryFolder;
+import org.osgi.framework.BundleContext;
+
+public class ActivatorStateStorageTest {
+
+    private static final String STATE_FILE = "state.json";
+
+    @Rule
+    public TemporaryFolder folder = new TemporaryFolder();
+
+    private StateStorage storage;
+
+    @Before
+    public void setUp() throws Exception {
+        BundleContext bundleContext = mock(BundleContext.class);
+        expect(bundleContext.getDataFile(anyString()))
+                .andAnswer(() -> new File(folder.getRoot(), (String) 
getCurrentArguments()[0]))
+                .anyTimes();
+        replay(bundleContext);
+
+        Method createStateStorage = 
Activator.class.getDeclaredMethod("createStateStorage");
+        createStateStorage.setAccessible(true);
+        storage = (StateStorage) createStateStorage.invoke(new 
StorageActivator(bundleContext));
+    }
+
+    @Test
+    public void testSaveState() throws Exception {
+        State state = state();
+
+        storage.save(state);
+
+        State loaded = new State();
+        storage.load(loaded);
+        assertEquals(state.requirements, loaded.requirements);
+        assertEquals(state.installedFeatures, loaded.installedFeatures);
+        assertArrayEquals(new String[] { STATE_FILE }, 
folder.getRoot().list());
+    }
+
+    @Test
+    @SuppressWarnings({ "rawtypes", "unchecked" })
+    public void testInterruptedSaveKeepsPreviousState() throws Exception {
+        State state = state();
+        storage.save(state);
+        File file = new File(folder.getRoot(), STATE_FILE);
+        byte[] saved = Files.readAllBytes(file.toPath());
+
+        // A value JSON cannot write stands in for what interrupts a save in 
practice, the
+        // process dying or the disk filling up part way through, which a unit 
test cannot
+        // cause: the save fails after it has started writing.
+        State unwritable = state.copy();
+        ((Map) unwritable.requirements).put("unwritable", new Object());
+        try {
+            storage.save(unwritable);
+            fail("A state holding a value JSON cannot write should fail to 
save");
+        } catch (IllegalArgumentException e) {
+            // Expected
+        }
+
+        assertArrayEquals(saved, Files.readAllBytes(file.toPath()));
+        State loaded = new State();
+        storage.load(loaded);
+        assertEquals(state.requirements, loaded.requirements);
+        assertEquals(state.installedFeatures, loaded.installedFeatures);
+    }
+
+    private static State state() {
+        State state = new State();
+        state.requirements.put("root", 
Collections.singleton("feature:example/[1.0.0,1.0.0]"));
+        state.installedFeatures.put("root", 
Collections.singleton("example/1.0.0"));
+        return state;
+    }
+
+    private static class StorageActivator extends Activator {
+        StorageActivator(BundleContext bundleContext) {
+            this.bundleContext = bundleContext;
+        }
+    }
+}
diff --git 
a/features/core/src/test/java/org/apache/karaf/features/internal/region/DigraphHelperTest.java
 
b/features/core/src/test/java/org/apache/karaf/features/internal/region/DigraphHelperTest.java
new file mode 100644
index 0000000000..ecab32fbd6
--- /dev/null
+++ 
b/features/core/src/test/java/org/apache/karaf/features/internal/region/DigraphHelperTest.java
@@ -0,0 +1,71 @@
+/*
+ * 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.karaf.features.internal.region;
+
+import static org.junit.Assert.assertArrayEquals;
+import static org.junit.Assert.assertEquals;
+
+import java.io.File;
+import java.nio.charset.StandardCharsets;
+import java.nio.file.Files;
+import java.util.Collections;
+
+import org.easymock.EasyMock;
+import org.eclipse.equinox.region.RegionDigraph;
+import org.junit.Rule;
+import org.junit.Test;
+import org.junit.rules.TemporaryFolder;
+
+public class DigraphHelperTest {
+
+    private static final String EMPTY_DIGRAPH = 
"{\"regions\":{},\"edges\":[]}";
+
+    @Rule
+    public TemporaryFolder folder = new TemporaryFolder();
+
+    @Test
+    public void testSaveDigraph() throws Exception {
+        File file = new File(folder.getRoot(), DigraphHelper.DIGRAPH_FILE);
+        Files.write(file.toPath(), 
"previous".getBytes(StandardCharsets.UTF_8));
+
+        RegionDigraph digraph = EasyMock.createMock(RegionDigraph.class);
+        
EasyMock.expect(digraph.getRegions()).andReturn(Collections.emptySet()).anyTimes();
+        EasyMock.replay(digraph);
+
+        DigraphHelper.saveDigraph(file, digraph);
+
+        assertEquals(EMPTY_DIGRAPH, Files.readString(file.toPath()));
+        assertArrayEquals(new String[] { DigraphHelper.DIGRAPH_FILE }, 
folder.getRoot().list());
+    }
+
+    @Test
+    public void testInterruptedSaveKeepsPreviousDigraph() throws Exception {
+        File file = new File(folder.getRoot(), DigraphHelper.DIGRAPH_FILE);
+        Files.write(file.toPath(), 
EMPTY_DIGRAPH.getBytes(StandardCharsets.UTF_8));
+
+        // A digraph that fails while it is read stands in for what interrupts 
a save in
+        // practice, the process dying or the disk filling up part way 
through, which a unit
+        // test cannot cause: the save fails after it has opened the file it 
writes.
+        RegionDigraph digraph = EasyMock.createMock(RegionDigraph.class);
+        EasyMock.expect(digraph.getRegions()).andThrow(new 
IllegalStateException("digraph changed while saving")).anyTimes();
+        EasyMock.replay(digraph);
+
+        DigraphHelper.saveDigraph(file, digraph);
+
+        assertEquals(EMPTY_DIGRAPH, Files.readString(file.toPath()));
+    }
+}
diff --git a/util/src/main/java/org/apache/karaf/util/FileUtils.java 
b/util/src/main/java/org/apache/karaf/util/FileUtils.java
new file mode 100644
index 0000000000..ef91976373
--- /dev/null
+++ b/util/src/main/java/org/apache/karaf/util/FileUtils.java
@@ -0,0 +1,63 @@
+/*
+ * 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.karaf.util;
+
+import java.io.File;
+import java.io.FileOutputStream;
+import java.io.IOException;
+import java.io.OutputStream;
+import java.nio.file.Files;
+import java.nio.file.StandardCopyOption;
+
+public final class FileUtils {
+
+    private FileUtils() {
+    }
+
+    /**
+     * Saves a file through a temporary file of its own, synced to disk and 
then moved over
+     * the file atomically. The file always holds its previous or its new 
content, even after
+     * a crash or a power loss, and concurrent saves never clash. A failed 
write deletes the
+     * temporary file.
+     *
+     * @param file the file to write
+     * @param writer writes the new content to the stream it is given
+     * @throws IOException if the content cannot be written or moved over the 
file
+     */
+    public static void atomicSave(File file, StreamWriter writer) throws 
IOException {
+        File tmpFile = File.createTempFile(file.getName(), ".tmp", 
file.getAbsoluteFile().getParentFile());
+        try {
+            try (FileOutputStream out = new FileOutputStream(tmpFile)) {
+                writer.write(out);
+                out.getFD().sync();
+            }
+            Files.move(tmpFile.toPath(), file.toPath(), 
StandardCopyOption.ATOMIC_MOVE, StandardCopyOption.REPLACE_EXISTING);
+        } catch (Throwable t) {
+            tmpFile.delete();
+            throw t;
+        }
+    }
+
+    /**
+     * Writes content to a stream. It must flush everything, wrappers 
included, before it
+     * returns, as the stream is then synced to disk.
+     */
+    @FunctionalInterface
+    public interface StreamWriter {
+        void write(OutputStream out) throws IOException;
+    }
+}
diff --git a/util/src/test/java/org/apache/karaf/util/FileUtilsTest.java 
b/util/src/test/java/org/apache/karaf/util/FileUtilsTest.java
new file mode 100644
index 0000000000..8df7373828
--- /dev/null
+++ b/util/src/test/java/org/apache/karaf/util/FileUtilsTest.java
@@ -0,0 +1,85 @@
+/*
+ * 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.karaf.util;
+
+import static org.junit.Assert.assertArrayEquals;
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.fail;
+
+import java.io.File;
+import java.io.IOException;
+import java.nio.charset.StandardCharsets;
+import java.nio.file.Files;
+
+import org.junit.Rule;
+import org.junit.Test;
+import org.junit.rules.TemporaryFolder;
+
+public class FileUtilsTest {
+
+    @Rule
+    public TemporaryFolder folder = new TemporaryFolder();
+
+    @Test
+    public void testAtomicSave() throws Exception {
+        File file = new File(folder.getRoot(), "state.json");
+        Files.write(file.toPath(), 
"previous".getBytes(StandardCharsets.UTF_8));
+
+        FileUtils.atomicSave(file, out -> 
out.write("new".getBytes(StandardCharsets.UTF_8)));
+
+        assertEquals("new", Files.readString(file.toPath()));
+        assertArrayEquals(new String[] { "state.json" }, 
folder.getRoot().list());
+    }
+
+    @Test
+    public void testInterruptedAtomicSaveKeepsPreviousContent() throws 
Exception {
+        File file = new File(folder.getRoot(), "state.json");
+        Files.write(file.toPath(), 
"previous".getBytes(StandardCharsets.UTF_8));
+
+        // A writer that fails part way through stands in for what interrupts 
a save in
+        // practice, the process dying or the disk filling up, which a unit 
test cannot cause
+        try {
+            FileUtils.atomicSave(file, out -> {
+                out.write("partial".getBytes(StandardCharsets.UTF_8));
+                throw new IOException("disk full");
+            });
+            fail("A write that fails should fail the save");
+        } catch (IOException e) {
+            // Expected
+        }
+
+        assertEquals("previous", Files.readString(file.toPath()));
+        assertArrayEquals(new String[] { "state.json" }, 
folder.getRoot().list());
+    }
+
+    @Test
+    public void testOverlappingAtomicSavesKeepTheirOwnContent() throws 
Exception {
+        File file = new File(folder.getRoot(), "state.json");
+
+        // A save made while another save of the same file is still writing 
stands in for two
+        // threads saving it at once, whose timing a unit test cannot control
+        FileUtils.atomicSave(file, out -> {
+            out.write("first".getBytes(StandardCharsets.UTF_8));
+            FileUtils.atomicSave(file, other -> 
other.write("second".getBytes(StandardCharsets.UTF_8)));
+            assertEquals("second", Files.readString(file.toPath()));
+            out.write(" save".getBytes(StandardCharsets.UTF_8));
+        });
+
+        assertEquals("first save", Files.readString(file.toPath()));
+        assertArrayEquals(new String[] { "state.json" }, 
folder.getRoot().list());
+    }
+}

Reply via email to