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

heneveld pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/brooklyn-server.git

commit a6d210f8c2f38ebed6e59c51ccf8fd46b127e629
Author: Alex Heneveld <[email protected]>
AuthorDate: Wed May 26 23:45:15 2021 +0100

    restore bundle metadata on rollback
---
 .../org/apache/brooklyn/camp/brooklyn/RebindOsgiTest.java   |  2 +-
 .../core/mgmt/ha/BrooklynBomOsgiArchiveInstaller.java       | 13 +++++++++----
 .../java/org/apache/brooklyn/core/mgmt/ha/OsgiManager.java  | 12 ++++++++----
 3 files changed, 18 insertions(+), 9 deletions(-)

diff --git 
a/camp/camp-brooklyn/src/test/java/org/apache/brooklyn/camp/brooklyn/RebindOsgiTest.java
 
b/camp/camp-brooklyn/src/test/java/org/apache/brooklyn/camp/brooklyn/RebindOsgiTest.java
index 20cb545..2766942 100644
--- 
a/camp/camp-brooklyn/src/test/java/org/apache/brooklyn/camp/brooklyn/RebindOsgiTest.java
+++ 
b/camp/camp-brooklyn/src/test/java/org/apache/brooklyn/camp/brooklyn/RebindOsgiTest.java
@@ -513,7 +513,7 @@ public class RebindOsgiTest extends AbstractYamlRebindTest {
             Asserts.expectedFailureContains(e, "DeliberatelyMissing", 
appSymbolicName);
         }
         Map<String, ManagedBundle> newBundles = 
origManagementContext.getOsgiManager().get().getManagedBundles();
-        Assert.assertEquals(newBundles, oldBundles, "Bundles: "+newBundles);
+        Assert.assertEquals(newBundles, oldBundles, "Bundles: "+newBundles+" 
!= "+oldBundles);
 
         rebind();
         newBundles = 
origManagementContext.getOsgiManager().get().getManagedBundles();
diff --git 
a/core/src/main/java/org/apache/brooklyn/core/mgmt/ha/BrooklynBomOsgiArchiveInstaller.java
 
b/core/src/main/java/org/apache/brooklyn/core/mgmt/ha/BrooklynBomOsgiArchiveInstaller.java
index 3f66043..4dd0fd3 100644
--- 
a/core/src/main/java/org/apache/brooklyn/core/mgmt/ha/BrooklynBomOsgiArchiveInstaller.java
+++ 
b/core/src/main/java/org/apache/brooklyn/core/mgmt/ha/BrooklynBomOsgiArchiveInstaller.java
@@ -69,6 +69,7 @@ import org.apache.brooklyn.util.stream.Streams;
 import org.apache.brooklyn.util.text.BrooklynVersionSyntax;
 import org.apache.brooklyn.util.text.Strings;
 import org.apache.brooklyn.util.text.VersionComparator;
+import org.apache.commons.lang3.tuple.Pair;
 import org.osgi.framework.Bundle;
 import org.osgi.framework.BundleException;
 import org.osgi.framework.Constants;
@@ -643,10 +644,12 @@ public class BrooklynBomOsgiArchiveInstaller {
             }
             
             osgiManager.checkCorrectlyInstalled(result.getMetadata(), 
result.bundle);
-            final File oldZipFile; 
+            final File oldZipFile;
+            final ManagedBundle oldManagedBundle;
             
             if (!updating) { 
                 oldZipFile = null;
+                oldManagedBundle = null;
                 osgiManager.managedBundlesRecord.addManagedBundle(result, 
zipFile);
                 result.code = 
OsgiBundleInstallationResult.ResultCode.INSTALLED_NEW_BUNDLE;
                 result.message = "Installed Brooklyn catalog bundle 
"+result.getMetadata().getVersionedName()+" with ID 
"+result.getMetadata().getId()+" ["+result.bundle.getBundleId()+"]";
@@ -655,7 +658,9 @@ public class BrooklynBomOsgiArchiveInstaller {
                     
mgmt().getRebindManager().getChangeListener().onManaged(result.getMetadata());
                 }
             } else {
-                oldZipFile = 
osgiManager.managedBundlesRecord.updateManagedBundleFileAndMetadata(result, 
zipFile);
+                Pair<File, ManagedBundle> olds = 
osgiManager.managedBundlesRecord.updateManagedBundleFileAndMetadata(result, 
zipFile);
+                oldZipFile = olds.getLeft();
+                oldManagedBundle = olds.getRight();
 
                 result.code = 
OsgiBundleInstallationResult.ResultCode.UPDATED_EXISTING_BUNDLE;
                 result.message = "Updated Brooklyn catalog bundle 
"+result.getMetadata().getVersionedName()+" as existing ID 
"+result.getMetadata().getId()+" ["+result.bundle.getBundleId()+"]";
@@ -686,9 +691,9 @@ public class BrooklynBomOsgiArchiveInstaller {
                         if (oldZipFile==null) {
                             throw new IllegalStateException("Did not have old 
ZIP file to install");
                         }
-                        log.debug("Rolling back bundle 
"+result.getVersionedName()+" to state from "+oldZipFile);
+                        log.debug("Rolling back bundle 
"+result.getVersionedName()+" to state "+oldManagedBundle+" from "+oldZipFile);
                         try {
-                            File zipFileNow = 
osgiManager.managedBundlesRecord.rollbackManagedBundleFile(result, oldZipFile);
+                            File zipFileNow = 
osgiManager.managedBundlesRecord.rollbackManagedBundleFileAndMetadata(result, 
oldZipFile, oldManagedBundle);
                             result.bundle.update(new 
FileInputStream(Preconditions.checkNotNull(zipFileNow, "Couldn't find contents 
of old version of bundle")));
                         } catch (Exception e) {
                             Exceptions.propagateIfFatal(e);
diff --git 
a/core/src/main/java/org/apache/brooklyn/core/mgmt/ha/OsgiManager.java 
b/core/src/main/java/org/apache/brooklyn/core/mgmt/ha/OsgiManager.java
index 44065ab..a909635 100644
--- a/core/src/main/java/org/apache/brooklyn/core/mgmt/ha/OsgiManager.java
+++ b/core/src/main/java/org/apache/brooklyn/core/mgmt/ha/OsgiManager.java
@@ -66,6 +66,7 @@ import org.apache.brooklyn.util.repeat.Repeater;
 import org.apache.brooklyn.util.stream.Streams;
 import org.apache.brooklyn.util.text.Strings;
 import org.apache.brooklyn.util.time.Duration;
+import org.apache.commons.lang3.tuple.Pair;
 import org.osgi.framework.Bundle;
 import org.osgi.framework.BundleException;
 import org.osgi.framework.Constants;
@@ -173,7 +174,7 @@ public class OsgiManager {
         }
 
         /** Updates the bundle file associated with the given record, creating 
and returning a backup if there was already such a file */ 
-        synchronized File 
updateManagedBundleFileAndMetadata(OsgiBundleInstallationResult result, File 
fNew) {
+        synchronized Pair<File,ManagedBundle> 
updateManagedBundleFileAndMetadata(OsgiBundleInstallationResult result, File 
fNew) {
             File fCached = fileFor(result.getMetadata());
             File fBak = new File(fCached.getAbsolutePath()+".bak");
             if (fBak.equals(fNew)) {
@@ -192,13 +193,13 @@ public class OsgiManager {
                 throw Exceptions.propagate(e);
             }
 
-            managedBundlesByUid.put(result.getMetadata().getId(), 
result.getMetadata());
+            ManagedBundle mbBak = 
managedBundlesByUid.put(result.getMetadata().getId(), result.getMetadata());
 
-            return fBak;
+            return Pair.of(fBak, mbBak);
         }
         
         /** Rolls back the officially installed file to a given backup copy of 
a bundle file, returning the new name of the file */
-        synchronized File 
rollbackManagedBundleFile(OsgiBundleInstallationResult result, File fBak) {
+        synchronized File 
rollbackManagedBundleFileAndMetadata(OsgiBundleInstallationResult result, File 
fBak, ManagedBundle mbBak) {
             log.debug("Rolling back to back Brooklyn local copy of bundle file 
"+fBak);
             if (!fBak.exists()) {
                 throw new IllegalStateException("Cannot rollback to "+fBak+" 
as file does not exist");
@@ -210,6 +211,9 @@ public class OsgiManager {
                 log.warn("No pre-existing bundle file "+fCached+" when rolling 
back; ignoring");
             }
             fBak.renameTo(fCached);
+
+            managedBundlesByUid.put(result.getMetadata().getId(), mbBak);
+
             return fCached;
         }
     }

Reply via email to