From 2bec0b9e156385f8e86ad7265e7dc9715331a159 Mon Sep 17 00:00:00 2001 From: Holger Friedrich Date: Sat, 17 Oct 2026 07:40:42 +0200 Subject: [PATCH 1/3] Resume interrupted feature deployments on startup If the framework stops after a deployment has installed its bundles and saved the features state, but before or while the bundles are started, those bundles stay installed/resolved after the next start: the framework does not start them (starts during shutdown are not persisted) and the features service considers the deployment done. Persist a deploymentPending flag while the deployment is in progress, keep it if the framework is shutting down at the end of the start phase, and resume pending deployments in BootFeaturesInstaller. --- .../service/BootFeaturesInstaller.java | 29 ++++- .../internal/service/BootManaged.java | 15 +++ .../features/internal/service/Deployer.java | 21 +++- .../internal/service/FeaturesServiceImpl.java | 14 +++ .../features/internal/service/State.java | 7 ++ .../internal/service/StateStorage.java | 3 + .../service/BootFeaturesInstallerTest.java | 26 ++++ .../internal/service/DeployerTest.java | 114 +++++++++++++++++- .../internal/service/StateStorageTest.java | 21 ++++ 9 files changed, 241 insertions(+), 9 deletions(-) diff --git a/features/core/src/main/java/org/apache/karaf/features/internal/service/BootFeaturesInstaller.java b/features/core/src/main/java/org/apache/karaf/features/internal/service/BootFeaturesInstaller.java index 1a9b9d29bb6..78efb486e6a 100644 --- a/features/core/src/main/java/org/apache/karaf/features/internal/service/BootFeaturesInstaller.java +++ b/features/core/src/main/java/org/apache/karaf/features/internal/service/BootFeaturesInstaller.java @@ -91,7 +91,21 @@ public BootFeaturesInstaller(BundleContext bundleContext, */ public void start() { if (bootManaged.isBootDone()) { - publishBootFinished(); + if (bootManaged.isDeploymentPending()) { + if (asynchronous) { + new Thread("Resume Features Provisioning") { + public void run() { + resumeDeployment(); + publishBootFinished(); + } + }.start(); + } else { + resumeDeployment(); + publishBootFinished(); + } + } else { + publishBootFinished(); + } return; } @@ -108,6 +122,19 @@ public void run() { } } + /** + * Completes a deployment that was interrupted (e.g. by stopping the framework) after its bundles were installed, + * but before they were started. Without this, these bundles would stay unstarted until the next deployment. + */ + protected void resumeDeployment() { + try { + LOGGER.info("Resuming interrupted features deployment"); + bootManaged.resumeDeployment(); + } catch (Throwable e) { + LOGGER.error("Error resuming interrupted features deployment", e); + } + } + protected void installBootFeatures(boolean quitIfUnsuccessful) { try { addRepositories(quitIfUnsuccessful); diff --git a/features/core/src/main/java/org/apache/karaf/features/internal/service/BootManaged.java b/features/core/src/main/java/org/apache/karaf/features/internal/service/BootManaged.java index c6a82ca1dc2..63cae23dcfa 100644 --- a/features/core/src/main/java/org/apache/karaf/features/internal/service/BootManaged.java +++ b/features/core/src/main/java/org/apache/karaf/features/internal/service/BootManaged.java @@ -25,4 +25,19 @@ public interface BootManaged { void bootDone(); + /** + * @return {@code true} if a deployment was interrupted after its bundles were installed, but before they were + * started (e.g. by stopping the framework), see {@link #resumeDeployment()} + */ + default boolean isDeploymentPending() { + return false; + } + + /** + * Provisions the current requirements again to complete an interrupted deployment, which in particular starts + * the bundles of started features that were left installed or resolved only. + */ + default void resumeDeployment() throws Exception { + } + } diff --git a/features/core/src/main/java/org/apache/karaf/features/internal/service/Deployer.java b/features/core/src/main/java/org/apache/karaf/features/internal/service/Deployer.java index a88e16abf4c..7e119cfd036 100644 --- a/features/core/src/main/java/org/apache/karaf/features/internal/service/Deployer.java +++ b/features/core/src/main/java/org/apache/karaf/features/internal/service/Deployer.java @@ -950,6 +950,9 @@ && isUpdateable(resource) && !deployment.bundleChecksums.containsKey(bundle.getB newState.installedFeatures.putAll(installedFeatures); newState.stateFeatures.putAll(stateFeatures); newState.managedBundles.putAll(managedBundles); + // the bundles are installed, but not yet refreshed/resolved/started: if the framework stops before this + // deployment is complete, they would stay unstarted, so remember to resume it on the next start + newState.deploymentPending.set(true); callback.saveState(newState); // @@ -1031,9 +1034,9 @@ && isUpdateable(resource) && !deployment.bundleChecksums.containsKey(bundle.getB // Compute bundles to start removeFragmentsAndBundlesInState(toStart, UNINSTALLED | ACTIVE); + List exceptions = new ArrayList<>(); if (!toStart.isEmpty()) { // Compute correct start order - List exceptions = new ArrayList<>(); print("Starting bundles:", verbose); while (!toStart.isEmpty()) { List bs = getBundlesToStart(toStart, serviceBundle); @@ -1047,9 +1050,19 @@ && isUpdateable(resource) && !deployment.bundleChecksums.containsKey(bundle.getB toStart.remove(bundle); } } - if (!exceptions.isEmpty()) { - throw new MultiException("Error restarting bundles", exceptions); - } + } + // All bundles have been started (failures included, resuming would not help with those), unless the framework + // has started to shut down meanwhile: then the starts do not persist and the deployment has to be resumed. + // While the framework is still raising its start level (e.g. boot features), the system bundle is STARTING. + Bundle systemBundle = dstate.bundles.get(0L); + if (systemBundle == null || (systemBundle.getState() & (Bundle.STARTING | Bundle.ACTIVE)) != 0) { + newState.deploymentPending.set(false); + callback.saveState(newState); + } else { + print("Framework is stopping, the deployment will be resumed on next start", verbose); + } + if (!exceptions.isEmpty()) { + throw new MultiException("Error restarting bundles", exceptions); } // If uninstall and delete configurations, actually delete configurations and configuration files 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 fef4588fe5e..f7b488ea098 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 @@ -260,6 +260,20 @@ public void bootDone() { } } + @Override + public boolean isDeploymentPending() { + synchronized (lock) { + return state.deploymentPending.get(); + } + } + + @Override + public void resumeDeployment() throws Exception { + State state = copyState(); + doProvisionInThread(state.requirements, emptyMap(), state, getFeaturesById(), + EnumSet.noneOf(Option.class)); + } + // // Listeners support // diff --git a/features/core/src/main/java/org/apache/karaf/features/internal/service/State.java b/features/core/src/main/java/org/apache/karaf/features/internal/service/State.java index e39e321aafd..faec6c72231 100644 --- a/features/core/src/main/java/org/apache/karaf/features/internal/service/State.java +++ b/features/core/src/main/java/org/apache/karaf/features/internal/service/State.java @@ -41,6 +41,12 @@ public class State { public final AtomicBoolean bootDone = new AtomicBoolean(); + /** + * Set while a deployment has been saved, but its bundles have not been (re)started yet. If it is still set when + * the features service starts, the deployment was interrupted (e.g. by a shutdown) and has to be resumed, as the + * framework does not start bundles which have only been installed. + */ + public final AtomicBoolean deploymentPending = new AtomicBoolean(); public final Set repositories = new TreeSet<>(); /** Map from region name to Set of feature requirements (feature:name/version-range) */ @@ -76,6 +82,7 @@ private static void copy(State from, State to, boolean clear) { to.bundleChecksums.clear(); } to.bootDone.set(from.bootDone.get()); + to.deploymentPending.set(from.deploymentPending.get()); MapUtils.copy(from.repositories, to.repositories); MapUtils.copy(from.requirements, to.requirements); MapUtils.copy(from.installedFeatures, to.installedFeatures); 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 a7548cfc8c4..c88ac7c0be1 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 @@ -44,6 +44,8 @@ public void load(State state) throws IOException { if (is != null) { Map json = (Map) JsonReader.read(is); state.bootDone.set((Boolean) json.get("bootDone")); + // absent in state files written by older versions + state.deploymentPending.set(Boolean.TRUE.equals(json.get("deploymentPending"))); state.repositories.addAll(toStringSet((Collection) json.get("repositories"))); state.requirements.putAll(toStringStringSetMap((Map) json.get("features"))); state.installedFeatures.putAll(toStringStringSetMap((Map) json.get("installed"))); @@ -67,6 +69,7 @@ public void save(State state) throws IOException { protected void write(State state, OutputStream os) throws IOException { Map json = new HashMap<>(); json.put("bootDone", state.bootDone.get()); + json.put("deploymentPending", state.deploymentPending.get()); json.put("repositories", state.repositories); json.put("features", state.requirements); json.put("installed", state.installedFeatures); diff --git a/features/core/src/test/java/org/apache/karaf/features/internal/service/BootFeaturesInstallerTest.java b/features/core/src/test/java/org/apache/karaf/features/internal/service/BootFeaturesInstallerTest.java index 4e249ce83f3..7f4aead23b0 100644 --- a/features/core/src/test/java/org/apache/karaf/features/internal/service/BootFeaturesInstallerTest.java +++ b/features/core/src/test/java/org/apache/karaf/features/internal/service/BootFeaturesInstallerTest.java @@ -40,6 +40,7 @@ import static org.easymock.EasyMock.createMock; import static org.easymock.EasyMock.createStrictMock; import static org.easymock.EasyMock.eq; +import static org.easymock.EasyMock.expect; import static org.easymock.EasyMock.expectLastCall; import static org.easymock.EasyMock.newCapture; import static org.easymock.EasyMock.replay; @@ -190,4 +191,29 @@ public void exit() { exitCalled = true; } } + + @Test + public void testStartResumesInterruptedDeployment() throws Exception { + FeaturesServiceImpl impl = createMock(FeaturesServiceImpl.class); + expect(impl.isBootDone()).andReturn(true); + expect(impl.isDeploymentPending()).andReturn(true); + impl.resumeDeployment(); + expectLastCall(); + + replay(impl); + new BootFeaturesInstaller(null, impl, null, new String[0], "", false).start(); + verify(impl); + } + + @Test + public void testStartDoesNotResumeCompletedDeployment() throws Exception { + FeaturesServiceImpl impl = createMock(FeaturesServiceImpl.class); + expect(impl.isBootDone()).andReturn(true); + expect(impl.isDeploymentPending()).andReturn(false); + + replay(impl); + new BootFeaturesInstaller(null, impl, null, new String[0], "", false).start(); + verify(impl); + } + } diff --git a/features/core/src/test/java/org/apache/karaf/features/internal/service/DeployerTest.java b/features/core/src/test/java/org/apache/karaf/features/internal/service/DeployerTest.java index 85cac4d2b77..b1870e50cee 100644 --- a/features/core/src/test/java/org/apache/karaf/features/internal/service/DeployerTest.java +++ b/features/core/src/test/java/org/apache/karaf/features/internal/service/DeployerTest.java @@ -91,7 +91,108 @@ public void testInstallSimpleFeature() throws Exception { EasyMock.anyObject()); EasyMock.expectLastCall(); callback.saveState(EasyMock.anyObject()); + List pendingOnSave = new ArrayList<>(); + EasyMock.expectLastCall().andAnswer(() -> { + pendingOnSave.add(((State) EasyMock.getCurrentArgument(0)).deploymentPending.get()); + return null; + }).times(2); + callback.installConfigs(f100); + EasyMock.expectLastCall(); + callback.installLibraries(f100); + EasyMock.expectLastCall(); + callback.callListeners(DeploymentEvent.BUNDLES_INSTALLED); + EasyMock.expectLastCall(); + callback.resolveBundles(EasyMock.anyObject(), + EasyMock.anyObject(), + EasyMock.anyObject()); + EasyMock.expectLastCall(); + callback.callListeners(DeploymentEvent.BUNDLES_RESOLVED); + EasyMock.expectLastCall(); + callback.callListeners(EasyMock.anyObject()); + EasyMock.expectLastCall(); + callback.callListeners(DeploymentEvent.DEPLOYMENT_FINISHED); + EasyMock.expectLastCall(); + + Bundle bundleA = createTestBundle(1, Bundle.ACTIVE, dataDir, "a100"); + EasyMock.expect(callback.installBundle(EasyMock.eq(ROOT_REGION), EasyMock.eq("a100"), EasyMock.anyObject())) + .andReturn(bundleA); + + c.replay(); + + Deployer.DeploymentState dstate = new Deployer.DeploymentState(); + dstate.state = new State(); + dstate.bundles = new HashMap<>(); + dstate.bundlesPerRegion = new HashMap<>(); + dstate.partitionFeatures(Arrays.asList(f100, f101)); + dstate.filtersPerRegion = new HashMap<>(); + dstate.filtersPerRegion.put(ROOT_REGION, new HashMap<>()); + + Deployer.DeploymentRequest request = new Deployer.DeploymentRequest(); + request.bundleUpdateRange = DEFAULT_BUNDLE_UPDATE_RANGE; + request.featureResolutionRange = DEFAULT_FEATURE_RESOLUTION_RANGE; + request.globalRepository = null; + request.options = EnumSet.noneOf(Option.class); + request.stateChanges = Collections.emptyMap(); + request.updateSnaphots = SnapshotUpdateBehavior.None; + request.requirements = new HashMap<>(); + addToMapSet(request.requirements, ROOT_REGION, f100.getName() + "/" + new VersionRange(f100.getVersion(), true)); + + deployer.deploy(dstate, request); + + c.verify(); + // saved as pending once the bundles are installed, completed once they are started + assertEquals(Arrays.asList(true, false), pendingOnSave); + } + + @Test + public void testInstallSimpleFeatureWhileFrameworkStarts() throws Exception { + // boot features are deployed while the framework raises its start level: the deployment completes + assertEquals(Arrays.asList(true, false), deploySimpleFeature(Bundle.STARTING)); + } + + @Test + public void testInstallSimpleFeatureWhileFrameworkStops() throws Exception { + // starting bundles does not persist while the framework stops, so the deployment stays pending + assertEquals(Arrays.asList(true), deploySimpleFeature(Bundle.STOPPING)); + } + + @Test + public void testInstallSimpleFeatureAfterFrameworkStopped() throws Exception { + // the deployment thread is not interrupted and may outlive the framework + assertEquals(Arrays.asList(true), deploySimpleFeature(Bundle.RESOLVED)); + } + + /** + * Deploys a simple feature with the system bundle in the given state. + * + * @return the value of {@link State#deploymentPending} at each save of the state + */ + private List deploySimpleFeature(int systemBundleState) throws Exception { + IMocksControl c = EasyMock.createControl(); + String dataDir = "data1"; + + TestDownloadManager manager = new TestDownloadManager(getClass(), dataDir); + + RepositoryImpl repo = new RepositoryImpl(getClass().getResource(dataDir + "/features.xml").toURI()); + Feature f100 = repo.getFeatures()[0]; + Feature f101 = repo.getFeatures()[1]; + + Deployer.DeployCallback callback = c.createMock(Deployer.DeployCallback.class); + Deployer deployer = new Deployer(manager, resolver, callback); + + callback.print(EasyMock.anyString(), EasyMock.anyBoolean()); + EasyMock.expectLastCall().anyTimes(); + callback.callListeners(DeploymentEvent.DEPLOYMENT_STARTED); EasyMock.expectLastCall(); + callback.replaceDigraph(EasyMock.anyObject(), + EasyMock.anyObject()); + EasyMock.expectLastCall(); + callback.saveState(EasyMock.anyObject()); + List pendingOnSave = new ArrayList<>(); + EasyMock.expectLastCall().andAnswer(() -> { + pendingOnSave.add(((State) EasyMock.getCurrentArgument(0)).deploymentPending.get()); + return null; + }).atLeastOnce(); callback.installConfigs(f100); EasyMock.expectLastCall(); callback.installLibraries(f100); @@ -118,6 +219,10 @@ public void testInstallSimpleFeature() throws Exception { Deployer.DeploymentState dstate = new Deployer.DeploymentState(); dstate.state = new State(); dstate.bundles = new HashMap<>(); + Hashtable systemHeaders = new Hashtable<>(); + systemHeaders.put("Bundle-ManifestVersion", "2"); + systemHeaders.put("Bundle-SymbolicName", "org.eclipse.osgi"); + dstate.bundles.put(0L, new TestBundle(0, "org.eclipse.osgi", systemBundleState, systemHeaders)); dstate.bundlesPerRegion = new HashMap<>(); dstate.partitionFeatures(Arrays.asList(f100, f101)); dstate.filtersPerRegion = new HashMap<>(); @@ -136,6 +241,7 @@ public void testInstallSimpleFeature() throws Exception { deployer.deploy(dstate, request); c.verify(); + return pendingOnSave; } @Test @@ -251,7 +357,7 @@ public Object answer() throws Throwable { EasyMock.anyObject()); EasyMock.expectLastCall(); callback.saveState(EasyMock.anyObject()); - EasyMock.expectLastCall(); + EasyMock.expectLastCall().times(2); callback.installConfigs(f101); EasyMock.expectLastCall(); callback.installLibraries(f101); @@ -325,7 +431,7 @@ public void testUpdateServiceBundle() throws Exception { EasyMock.anyObject()); EasyMock.expectLastCall(); callback.saveState(EasyMock.anyObject()); - EasyMock.expectLastCall(); + EasyMock.expectLastCall().times(2); callback.callListeners(DeploymentEvent.BUNDLES_INSTALLED); EasyMock.expectLastCall(); callback.installConfigs(f1); @@ -398,7 +504,7 @@ public void testPrerequisite() throws Exception { EasyMock.anyObject()); EasyMock.expectLastCall(); callback.saveState(EasyMock.anyObject()); - EasyMock.expectLastCall(); + EasyMock.expectLastCall().times(2); callback.installConfigs(EasyMock.anyObject()); EasyMock.expectLastCall(); callback.installLibraries(EasyMock.anyObject()); @@ -457,7 +563,7 @@ public void testPrerequisite() throws Exception { EasyMock.anyObject()); EasyMock.expectLastCall(); callback.saveState(EasyMock.anyObject()); - EasyMock.expectLastCall(); + EasyMock.expectLastCall().times(2); callback.installConfigs(f2); EasyMock.expectLastCall(); callback.installLibraries(f2); diff --git a/features/core/src/test/java/org/apache/karaf/features/internal/service/StateStorageTest.java b/features/core/src/test/java/org/apache/karaf/features/internal/service/StateStorageTest.java index 1786257f180..de821b7529b 100644 --- a/features/core/src/test/java/org/apache/karaf/features/internal/service/StateStorageTest.java +++ b/features/core/src/test/java/org/apache/karaf/features/internal/service/StateStorageTest.java @@ -21,6 +21,7 @@ import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; +import java.nio.charset.StandardCharsets; import java.util.Arrays; import java.util.Collections; import java.util.HashSet; @@ -28,6 +29,8 @@ import org.junit.Test; import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertTrue; public class StateStorageTest { @@ -36,6 +39,7 @@ public void testStoreLoad() throws Exception { State oldState = new State(); oldState.bootDone.set(true); + oldState.deploymentPending.set(true); oldState.bundleChecksums.put(4l, 32794l); oldState.requirements.put("bar", Collections.singleton("f1")); oldState.managedBundles.put("reg", Collections.singleton(32l)); @@ -52,12 +56,29 @@ public void testStoreLoad() throws Exception { storage.load(newState); assertEquals(oldState.bootDone.get(), newState.bootDone.get()); + assertEquals(oldState.deploymentPending.get(), newState.deploymentPending.get()); assertEquals(oldState.bundleChecksums, newState.bundleChecksums); assertEquals(oldState.requirements, newState.requirements); assertEquals(oldState.managedBundles, newState.managedBundles); assertEquals(oldState.repositories, newState.repositories); } + @Test + public void testLoadWithoutDeploymentPending() throws Exception { + TestStorage storage = new TestStorage(); + storage.baos = new ByteArrayOutputStream(); + // state file as written by versions without the deploymentPending flag + storage.baos.write(("{\"bootDone\":true,\"repositories\":[],\"features\":{},\"installed\":{}," + + "\"state\":{},\"managed\":{},\"checksums\":{}}").getBytes(StandardCharsets.UTF_8)); + + State state = new State(); + state.deploymentPending.set(true); + storage.load(state); + + assertTrue(state.bootDone.get()); + assertFalse(state.deploymentPending.get()); + } + static class TestStorage extends StateStorage { ByteArrayOutputStream baos; From a55e400e8a06ed220787a5261374f9ce8f64cd20 Mon Sep 17 00:00:00 2001 From: Holger Friedrich Date: Fri, 20 Nov 2026 17:38:46 +0100 Subject: [PATCH 2/3] Resume interrupted feature deployments on startup If the framework stops while a deployment changes bundles, the bundles it has installed, updated or stopped are not started on the next launch: the framework does not start bundles which have only been installed, and starts during a shutdown are not persisted. As the features service considers the deployment done, they stay unstarted until the next one. Save a pending deployment in the features state, along with the new requirements, before the first bundle operation, and end it once all bundles have been started or the deployment has failed. A pending deployment of an earlier framework launch is resumed by BootFeaturesInstaller: - it records the framework UUID of its launch, so that a deployment that restarts the features service (e.g. by refreshing its bundle) is not resumed while it is still running - it records the options of the deployment, which the resume replays, and the features whose configurations are still to be deleted - once the features service bundle has been refreshed, the deployment no longer saves the state, as the bundle context is no longer valid - a failed deployment is not resumed, it would fail again on each start; if it fails before its new state is saved, the previous state is restored - a restart of Karaf to refresh the system bundle keeps the deployment pending, so that it is completed after the restart Co-Authored-By: Claude Opus 5.5 --- .../internal/service/BootManaged.java | 9 +- .../service/BundleInstallSupport.java | 2 + .../service/BundleInstallSupportImpl.java | 2 + .../features/internal/service/Deployer.java | 148 +++++++- .../internal/service/FeaturesServiceImpl.java | 28 +- .../features/internal/service/State.java | 40 ++- .../internal/service/StateStorage.java | 27 +- .../internal/service/DeployerTest.java | 320 +++++++++++++++++- .../service/FeaturesServiceImplTest.java | 29 ++ .../internal/service/StateStorageTest.java | 32 +- 10 files changed, 585 insertions(+), 52 deletions(-) diff --git a/features/core/src/main/java/org/apache/karaf/features/internal/service/BootManaged.java b/features/core/src/main/java/org/apache/karaf/features/internal/service/BootManaged.java index 63cae23dcfa..784c6cf7f2d 100644 --- a/features/core/src/main/java/org/apache/karaf/features/internal/service/BootManaged.java +++ b/features/core/src/main/java/org/apache/karaf/features/internal/service/BootManaged.java @@ -26,16 +26,17 @@ public interface BootManaged { void bootDone(); /** - * @return {@code true} if a deployment was interrupted after its bundles were installed, but before they were - * started (e.g. by stopping the framework), see {@link #resumeDeployment()} + * @return {@code true} if a deployment of an earlier framework launch was interrupted (e.g. by stopping the + * framework) after it started to change bundles, but before all of them were started, see + * {@link #resumeDeployment()} */ default boolean isDeploymentPending() { return false; } /** - * Provisions the current requirements again to complete an interrupted deployment, which in particular starts - * the bundles of started features that were left installed or resolved only. + * Provisions the requirements of an interrupted deployment again, with its options, to complete it. In particular, + * this starts the bundles of started features that were left installed or resolved only. */ default void resumeDeployment() throws Exception { } diff --git a/features/core/src/main/java/org/apache/karaf/features/internal/service/BundleInstallSupport.java b/features/core/src/main/java/org/apache/karaf/features/internal/service/BundleInstallSupport.java index b37f689356e..d6ec148bb3a 100644 --- a/features/core/src/main/java/org/apache/karaf/features/internal/service/BundleInstallSupport.java +++ b/features/core/src/main/java/org/apache/karaf/features/internal/service/BundleInstallSupport.java @@ -91,6 +91,8 @@ class FrameworkInfo { public int initialBundleStartLevel; public int currentStartLevel; public Map bundles = new HashMap<>(); + /** {@link org.osgi.framework.Constants#FRAMEWORK_UUID} of the current framework launch */ + public String frameworkUuid; } } diff --git a/features/core/src/main/java/org/apache/karaf/features/internal/service/BundleInstallSupportImpl.java b/features/core/src/main/java/org/apache/karaf/features/internal/service/BundleInstallSupportImpl.java index ca893dfc7d1..123c7178e20 100644 --- a/features/core/src/main/java/org/apache/karaf/features/internal/service/BundleInstallSupportImpl.java +++ b/features/core/src/main/java/org/apache/karaf/features/internal/service/BundleInstallSupportImpl.java @@ -40,6 +40,7 @@ import org.osgi.framework.Bundle; import org.osgi.framework.BundleContext; import org.osgi.framework.BundleException; +import org.osgi.framework.Constants; import org.osgi.framework.FrameworkEvent; import org.osgi.framework.FrameworkListener; import org.osgi.framework.InvalidSyntaxException; @@ -332,6 +333,7 @@ public FrameworkInfo getInfo() { } info.cmBundle = cmBundle; info.systemBundle = info.bundles.get(0L); + info.frameworkUuid = systemBundleContext.getProperty(Constants.FRAMEWORK_UUID); return info; } } diff --git a/features/core/src/main/java/org/apache/karaf/features/internal/service/Deployer.java b/features/core/src/main/java/org/apache/karaf/features/internal/service/Deployer.java index 7e119cfd036..50835cb28eb 100644 --- a/features/core/src/main/java/org/apache/karaf/features/internal/service/Deployer.java +++ b/features/core/src/main/java/org/apache/karaf/features/internal/service/Deployer.java @@ -188,6 +188,8 @@ public static class DeploymentState { public int currentStartLevel; /** bundle-id -> bundle for all currently installed bundles */ public Map bundles; + /** {@link org.osgi.framework.Constants#FRAMEWORK_UUID} of the current framework launch */ + public String frameworkUuid; // part of the deployment state related to all available features @@ -354,6 +356,24 @@ public void deployFully(DeploymentState dstate, DeploymentRequest request) throw * @throws Exception in case of deployment failure. */ public void deploy(DeploymentState dstate, DeploymentRequest request) throws Exception { + PendingDeploymentTracker pending = new PendingDeploymentTracker(dstate, + request.options.contains(FeaturesService.Option.Verbose)); + try { + deploy(dstate, request, pending); + } catch (Throwable t) { + // Resuming a failed deployment would fail the same way again, on each start: unless the framework is + // stopping, end it + try { + pending.end(); + } catch (Throwable t2) { + t.addSuppressed(t2); + } + throw t; + } + } + + private void deploy(DeploymentState dstate, DeploymentRequest request, PendingDeploymentTracker pending) + throws Exception { boolean noRefreshUnmanaged = request.options.contains(FeaturesService.Option.NoAutoRefreshUnmanagedBundles); boolean noRefreshManaged = request.options.contains(FeaturesService.Option.NoAutoRefreshManagedBundles); boolean noRefresh = request.options.contains(FeaturesService.Option.NoAutoRefreshBundles); @@ -689,6 +709,7 @@ public void deploy(DeploymentState dstate, DeploymentRequest request) throws Exc // // Execute deployment // + // #0: save the new requirements as a pending deployment // #1: stop bundles that needs to be updated or uninstalled or refreshed in order // #2: uninstall needed bundles // #3: update regions @@ -753,6 +774,18 @@ public void deploy(DeploymentState dstate, DeploymentRequest request) throws Exc callback.callListeners(DeploymentEvent.DEPLOYMENT_STARTED); + // Configurations to delete: the ones of the features uninstalled now, and the ones an interrupted deployment + // has not deleted yet, but never the ones of features that are installed (again) + Map> configsToDelete = deleteConfigurations ? copy(delFeatures) : new HashMap<>(); + if (dstate.state.pendingDeployment != null) { + add(configsToDelete, dstate.state.pendingDeployment.configsToDelete); + } + configsToDelete = diff(configsToDelete, installedFeatures); + + // Save the new requirements before the first bundle operation, so that the deployment is resumed if the + // framework stops before all its bundles are started + pending.start(request, stateFeatures, configsToDelete); + // // Perform bundle operations // @@ -950,10 +983,8 @@ && isUpdateable(resource) && !deployment.bundleChecksums.containsKey(bundle.getB newState.installedFeatures.putAll(installedFeatures); newState.stateFeatures.putAll(stateFeatures); newState.managedBundles.putAll(managedBundles); - // the bundles are installed, but not yet refreshed/resolved/started: if the framework stops before this - // deployment is complete, they would stay unstarted, so remember to resume it on the next start - newState.deploymentPending.set(true); - callback.saveState(newState); + // the bundles are installed, but not yet refreshed/resolved/started: the deployment stays pending + pending.save(newState); // // Install configurations and libraries @@ -977,10 +1008,11 @@ && isUpdateable(resource) && !deployment.bundleChecksums.containsKey(bundle.getB } // Delete configurations - if (deleteConfigurations) { - for (Map.Entry> entry : delFeatures.entrySet()) { - for (String name : entry.getValue()) { - Feature feature = dstate.featuresById.get(name); + for (Map.Entry> entry : configsToDelete.entrySet()) { + for (String name : entry.getValue()) { + Feature feature = dstate.featuresById.get(name); + // the repository may have been removed since an interrupted deployment + if (feature != null) { callback.deleteConfigs(feature); } } @@ -991,6 +1023,7 @@ && isUpdateable(resource) && !deployment.bundleChecksums.containsKey(bundle.getB print("The system bundle needs to be refreshed, restarting Karaf...", verbose); System.setProperty("karaf.restart", "true"); dstate.bundles.get(0l).stop(); + // the deployment stays pending, so that it is completed after the restart return; } @@ -1018,6 +1051,7 @@ && isUpdateable(resource) && !deployment.bundleChecksums.containsKey(bundle.getB // Ensure all classes are loaded in case the bundle will be refreshed if (serviceBundle != null && toRefresh.containsKey(serviceBundle)) { ensureAllClassesLoaded(serviceBundle); + pending.serviceBundleRefreshed(); } callback.refreshPackages(toRefresh.keySet()); @@ -1051,16 +1085,8 @@ && isUpdateable(resource) && !deployment.bundleChecksums.containsKey(bundle.getB } } } - // All bundles have been started (failures included, resuming would not help with those), unless the framework - // has started to shut down meanwhile: then the starts do not persist and the deployment has to be resumed. - // While the framework is still raising its start level (e.g. boot features), the system bundle is STARTING. - Bundle systemBundle = dstate.bundles.get(0L); - if (systemBundle == null || (systemBundle.getState() & (Bundle.STARTING | Bundle.ACTIVE)) != 0) { - newState.deploymentPending.set(false); - callback.saveState(newState); - } else { - print("Framework is stopping, the deployment will be resumed on next start", verbose); - } + // All bundles have been started (failures included, resuming would not help with those) + pending.end(); if (!exceptions.isEmpty()) { throw new MultiException("Error restarting bundles", exceptions); } @@ -1089,6 +1115,92 @@ && isUpdateable(resource) && !deployment.bundleChecksums.containsKey(bundle.getB print("Done.", verbose); } + /** + * Keeps {@link State#pendingDeployment} set while a deployment changes bundles, from before the first bundle + * operation until all bundles have been started, so that the deployment is resumed if the framework stops + * meanwhile. + */ + private class PendingDeploymentTracker { + private final DeploymentState dstate; + private final boolean verbose; + /** null until the deployment changes bundles */ + private State.PendingDeployment pending; + /** The state to save when the deployment ends: the one before it until its new state has been saved */ + private State stateOnEnd; + /** false once the FeaturesService bundle has been refreshed: then its new instance owns the state */ + private boolean ownsState = true; + + PendingDeploymentTracker(DeploymentState dstate, boolean verbose) { + this.dstate = dstate; + this.verbose = verbose; + } + + /** + * Saves the requirements and the features state of the deployment, along with the pending deployment. The + * other parts of the state (e.g. the managed bundles) are saved as they were, they are only up to date once + * the bundles have been installed (see {@link #save(State)}). This is enough to resume the deployment: it + * resolves the requirements again, and the bundles it has already installed are taken over as unmanaged + * bundles required by its features. + */ + void start(DeploymentRequest request, Map> stateFeatures, + Map> configsToDelete) { + pending = new State.PendingDeployment(); + pending.frameworkUuid = dstate.frameworkUuid; + for (FeaturesService.Option option : request.options) { + pending.options.add(option.name()); + } + MapUtils.copy(configsToDelete, pending.configsToDelete); + stateOnEnd = dstate.state.copy(); + State state = dstate.state.copy(); + state.requirements.clear(); + MapUtils.copy(request.requirements, state.requirements); + state.stateFeatures.clear(); + MapUtils.copy(stateFeatures, state.stateFeatures); + state.pendingDeployment = pending.copy(); + callback.saveState(state); + } + + /** + * Saves the new state of the deployment, which stays pending. + */ + void save(State newState) { + newState.pendingDeployment = pending.copy(); + callback.saveState(newState); + stateOnEnd = newState; + } + + void serviceBundleRefreshed() { + ownsState = false; + } + + /** + * Ends the deployment, which is no longer pending: either all its bundles have been started, or it has + * failed. If it has failed before its new state has been saved, the state before the deployment is restored. + */ + void end() { + if (pending == null) { + return; + } + if (!ownsState) { + // Using the bundle context of the refreshed bundle would fail. The new instance does not resume this + // deployment, as it was started in the same framework launch: the next launch will resume it, which + // re-provisions the same requirements. + return; + } + // When the framework stops, the bundles started meanwhile are not started on next launch: the deployment + // has to be resumed then. While the framework is still raising its start level (e.g. boot features), + // the system bundle is STARTING. + Bundle systemBundle = dstate.bundles.get(0L); + if (systemBundle != null && (systemBundle.getState() & (STARTING | ACTIVE)) == 0) { + print("Framework is stopping, the deployment will be resumed on next start", verbose); + return; + } + pending = null; + stateOnEnd.pendingDeployment = null; + callback.saveState(stateOnEnd); + } + } + private void handlePrerequisites(DeploymentState dstate, DeploymentRequest request, SubsystemResolver resolver) throws Exception { Set prereqs = resolver.collectPrerequisites(); 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 f7b488ea098..e06f17f8da8 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 @@ -262,16 +262,37 @@ public void bootDone() { @Override public boolean isDeploymentPending() { + State.PendingDeployment pending; synchronized (lock) { - return state.deploymentPending.get(); + pending = state.pendingDeployment; } + if (pending == null) { + return false; + } + // A deployment of the current framework launch may still be running, e.g. when it has refreshed the bundle + // of this service, or changed its configuration: only one of an earlier launch has been interrupted + FrameworkInfo info = installSupport.getInfo(); + return info != null && info.frameworkUuid != null && !info.frameworkUuid.equals(pending.frameworkUuid); } @Override public void resumeDeployment() throws Exception { State state = copyState(); - doProvisionInThread(state.requirements, emptyMap(), state, getFeaturesById(), - EnumSet.noneOf(Option.class)); + State.PendingDeployment pending = state.pendingDeployment; + if (pending == null) { + return; + } + // with the options of the interrupted deployment (e.g. no auto refresh), but without any console output + EnumSet