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 3eae397dba fix(features): follow up on the atomic saves from #3001
(#3079)
3eae397dba is described below
commit 3eae397dba7c274d0b3f531ff356b349349fc9b7
Author: JB Onofré <[email protected]>
AuthorDate: Wed Nov 11 05:46:25 2026 +0000
fix(features): follow up on the atomic saves from #3001 (#3079)
- FileUtils.atomicSave falls back to a plain move when the file system
does not support an atomic move, as SimpleDownloadTask and
FeatureConfigInstaller already do, instead of failing every save.
- The resolve request (FeaturesServiceImpl.writeResolve) is saved
through FileUtils.atomicSave too. It was still written in place and
is read back at startup, so a crash while writing it could leave it
truncated.
- The Activator's StateStorage.getOutputStream() is no longer used now
that save() is overridden; it throws instead of opening a truncating
stream on state.json.
- ActivatorStateStorageTest: fix the comment on the interrupted save,
which fails before anything reaches the temporary file, and check
that no temporary file is left behind.
---
.../karaf/features/internal/osgi/Activator.java | 4 +--
.../internal/service/FeaturesServiceImpl.java | 9 ++----
.../internal/osgi/ActivatorStateStorageTest.java | 4 ++-
.../internal/service/FeaturesServiceImplTest.java | 33 ++++++++++++++++++++++
.../main/java/org/apache/karaf/util/FileUtils.java | 10 +++++--
5 files changed, 48 insertions(+), 12 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 3cc22c38af..9b213dc7d4 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
@@ -18,7 +18,6 @@ package org.apache.karaf.features.internal.osgi;
import java.io.File;
import java.io.FileInputStream;
-import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStream;
@@ -343,8 +342,7 @@ public class Activator extends BaseActivator {
@Override
protected OutputStream getOutputStream() throws IOException {
- File file = bundleContext.getDataFile(STATE_FILE);
- return new FileOutputStream(file);
+ throw new UnsupportedOperationException("The state is saved
atomically by save(State)");
}
@Override
diff --git
a/features/core/src/main/java/org/apache/karaf/features/internal/service/FeaturesServiceImpl.java
b/features/core/src/main/java/org/apache/karaf/features/internal/service/FeaturesServiceImpl.java
index e75831fdcd..8d8f4e570d 100644
---
a/features/core/src/main/java/org/apache/karaf/features/internal/service/FeaturesServiceImpl.java
+++
b/features/core/src/main/java/org/apache/karaf/features/internal/service/FeaturesServiceImpl.java
@@ -19,7 +19,6 @@ package org.apache.karaf.features.internal.service;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
-import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.StringWriter;
@@ -72,6 +71,7 @@ import org.apache.karaf.features.internal.model.JacksonUtil;
import org.apache.karaf.features.internal.model.JaxbUtil;
import org.apache.karaf.features.internal.region.DigraphHelper;
import
org.apache.karaf.features.internal.service.BundleInstallSupport.FrameworkInfo;
+import org.apache.karaf.util.FileUtils;
import org.apache.karaf.util.ThreadUtils;
import org.apache.karaf.util.json.JsonReader;
import org.apache.karaf.util.json.JsonWriter;
@@ -210,11 +210,8 @@ public class FeaturesServiceImpl implements
FeaturesService, BootManaged, Deploy
}
request.put("features", requestedFeatures);
request.put("options", opts);
- try (
- FileOutputStream fos = new FileOutputStream(resolveFile)
- ) {
- JsonWriter.write(fos, request);
- }
+ // A failure or a crash while saving never leaves a truncated request
+ FileUtils.atomicSave(resolveFile, out -> JsonWriter.write(out,
request));
}
//
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
index 01c1e15946..3d092b1e27 100644
---
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
@@ -84,7 +84,8 @@ public class ActivatorStateStorageTest {
// 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.
+ // cause. JsonWriter buffers its output, so this save fails before
anything reaches
+ // the temporary file; FileUtilsTest covers a save that fails part way
through.
State unwritable = state.copy();
((Map) unwritable.requirements).put("unwritable", new Object());
try {
@@ -95,6 +96,7 @@ public class ActivatorStateStorageTest {
}
assertArrayEquals(saved, Files.readAllBytes(file.toPath()));
+ assertArrayEquals(new String[] { STATE_FILE },
folder.getRoot().list());
State loaded = new State();
storage.load(loaded);
assertEquals(state.requirements, loaded.requirements);
diff --git
a/features/core/src/test/java/org/apache/karaf/features/internal/service/FeaturesServiceImplTest.java
b/features/core/src/test/java/org/apache/karaf/features/internal/service/FeaturesServiceImplTest.java
index 53944e470e..f3c2b200ce 100644
---
a/features/core/src/test/java/org/apache/karaf/features/internal/service/FeaturesServiceImplTest.java
+++
b/features/core/src/test/java/org/apache/karaf/features/internal/service/FeaturesServiceImplTest.java
@@ -17,7 +17,9 @@
package org.apache.karaf.features.internal.service;
import java.io.File;
+import java.io.FileInputStream;
import java.io.IOException;
+import java.io.InputStream;
import java.lang.reflect.Field;
import java.net.*;
import java.util.Collections;
@@ -35,11 +37,14 @@ import
org.apache.karaf.features.internal.download.DownloadManager;
import org.apache.karaf.features.internal.resolver.Slf4jResolverLog;
import
org.apache.karaf.features.internal.service.BundleInstallSupport.FrameworkInfo;
import org.apache.karaf.features.internal.support.TestDownloadManager;
+import org.apache.karaf.util.json.JsonReader;
import org.easymock.Capture;
import org.easymock.EasyMock;
import org.junit.After;
import org.junit.Before;
+import org.junit.Rule;
import org.junit.Test;
+import org.junit.rules.TemporaryFolder;
import org.osgi.framework.Bundle;
import org.osgi.framework.Version;
import org.osgi.framework.startlevel.BundleStartLevel;
@@ -51,6 +56,7 @@ import org.slf4j.LoggerFactory;
import static org.easymock.EasyMock.anyObject;
import static org.easymock.EasyMock.expect;
import static org.easymock.EasyMock.expectLastCall;
+import static org.junit.Assert.assertArrayEquals;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;
@@ -67,6 +73,9 @@ public class FeaturesServiceImplTest extends
AbstractFeaturesServiceTest {
Resolver resolver = new ResolverImpl(new Slf4jResolverLog(logger));
File dataFile;
+ @Rule
+ public TemporaryFolder folder = new TemporaryFolder();
+
@Before
public void setUp() throws IOException {
dataFile = File.createTempFile("features", null, null);
@@ -104,6 +113,30 @@ public class FeaturesServiceImplTest extends
AbstractFeaturesServiceTest {
impl.addRepository(URI.create("custom:cycle/a-references-b.xml"));
impl.getFeatureCache();
}
+
+ @Test
+ @SuppressWarnings("unchecked")
+ public void testPersistResolveRequest() throws Exception {
+ File resolveFile = new File(folder.getRoot(), "resolve");
+ BundleInstallSupport installSupport =
EasyMock.niceMock(BundleInstallSupport.class);
+
expect(installSupport.getDataFile("resolve")).andReturn(resolveFile).anyTimes();
+ EasyMock.replay(installSupport);
+ FeaturesServiceImpl impl = new FeaturesServiceImpl(new Storage(),
null, null, this.resolver,
+ installSupport, null, new FeaturesServiceConfig());
+
+ Deployer.DeploymentRequest request = new Deployer.DeploymentRequest();
+ request.requirements = Collections.singletonMap("root",
Collections.singleton("feature:example"));
+ request.options = EnumSet.of(Option.NoAutoRefreshBundles);
+ impl.persistResolveRequest(request);
+
+ Map<String, Object> saved;
+ try (InputStream is = new FileInputStream(resolveFile)) {
+ saved = (Map<String, Object>) JsonReader.read(is);
+ }
+ assertEquals(Collections.singletonMap("root",
Collections.singletonList("feature:example")), saved.get("features"));
+ assertEquals(Collections.singletonList("NoAutoRefreshBundles"),
saved.get("options"));
+ assertArrayEquals(new String[] { "resolve" }, folder.getRoot().list());
+ }
@Test
public void testRemoveRepo1() throws Exception {
diff --git a/util/src/main/java/org/apache/karaf/util/FileUtils.java
b/util/src/main/java/org/apache/karaf/util/FileUtils.java
index ef91976373..b1cb632bef 100644
--- a/util/src/main/java/org/apache/karaf/util/FileUtils.java
+++ b/util/src/main/java/org/apache/karaf/util/FileUtils.java
@@ -20,6 +20,7 @@ import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
+import java.nio.file.AtomicMoveNotSupportedException;
import java.nio.file.Files;
import java.nio.file.StandardCopyOption;
@@ -32,7 +33,8 @@ public final class 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.
+ * temporary file. On a file system that does not support an atomic move,
the temporary
+ * file is moved over the file with a plain move instead, which gives no
such guarantee.
*
* @param file the file to write
* @param writer writes the new content to the stream it is given
@@ -45,7 +47,11 @@ public final class FileUtils {
writer.write(out);
out.getFD().sync();
}
- Files.move(tmpFile.toPath(), file.toPath(),
StandardCopyOption.ATOMIC_MOVE, StandardCopyOption.REPLACE_EXISTING);
+ try {
+ Files.move(tmpFile.toPath(), file.toPath(),
StandardCopyOption.ATOMIC_MOVE, StandardCopyOption.REPLACE_EXISTING);
+ } catch (AtomicMoveNotSupportedException e) {
+ Files.move(tmpFile.toPath(), file.toPath(),
StandardCopyOption.REPLACE_EXISTING);
+ }
} catch (Throwable t) {
tmpFile.delete();
throw t;