brooklyn-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From henev...@apache.org
Subject [02/21] incubator-brooklyn git commit: clean up copyPersistedState code
Date Sat, 15 Nov 2014 00:05:11 GMT
clean up copyPersistedState code

move shared code to new `BrooklynPersistenceUtils`,
make a new `LocationWithPersistenceStore.newPersistenceObjectStore` interface,
and add documentation for `copy-state` command


Project: http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/repo
Commit: http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/commit/0773c64f
Tree: http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/tree/0773c64f
Diff: http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/diff/0773c64f

Branch: refs/heads/master
Commit: 0773c64fabe89e8af0a48d570a461d2e30ce133e
Parents: 442a769
Author: Alex Heneveld <alex.heneveld@cloudsoftcorp.com>
Authored: Fri Nov 7 08:38:24 2014 +0000
Committer: Alex Heneveld <alex.heneveld@cloudsoftcorp.com>
Committed: Thu Nov 13 23:38:51 2014 +0000

----------------------------------------------------------------------
 .../brooklyn/entity/rebind/RebindManager.java   |   2 +-
 .../brooklyn/config/BrooklynServerConfig.java   |   2 +-
 .../entity/rebind/RebindManagerImpl.java        |   5 +-
 .../persister/BrooklynPersistenceUtils.java     | 103 +++++++++++++
 .../persister/LocationWithObjectStore.java      |  27 ++++
 .../LocalhostMachineProvisioningLocation.java   |  13 +-
 docs/use/guide/persistence/index.md             |   8 +-
 .../location/jclouds/JcloudsLocation.java       |  11 +-
 usage/cli/src/main/java/brooklyn/cli/Main.java  |  11 +-
 .../brooklyn/launcher/BrooklynLauncher.java     | 148 ++++++-------------
 10 files changed, 211 insertions(+), 119 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/blob/0773c64f/api/src/main/java/brooklyn/entity/rebind/RebindManager.java
----------------------------------------------------------------------
diff --git a/api/src/main/java/brooklyn/entity/rebind/RebindManager.java b/api/src/main/java/brooklyn/entity/rebind/RebindManager.java
index 5f9ccf8..8995aae 100644
--- a/api/src/main/java/brooklyn/entity/rebind/RebindManager.java
+++ b/api/src/main/java/brooklyn/entity/rebind/RebindManager.java
@@ -72,7 +72,7 @@ public interface RebindManager {
     @Beta
     public List<Application> rebind(ClassLoader classLoader, RebindExceptionHandler
exceptionHandler, ManagementNodeState mode);
 
-    public BrooklynMementoRawData retrieveMementoRawData() throws IOException;
+    public BrooklynMementoRawData retrieveMementoRawData();
 
     public ChangeListener getChangeListener();
 

http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/blob/0773c64f/core/src/main/java/brooklyn/config/BrooklynServerConfig.java
----------------------------------------------------------------------
diff --git a/core/src/main/java/brooklyn/config/BrooklynServerConfig.java b/core/src/main/java/brooklyn/config/BrooklynServerConfig.java
index ac4a380..a5be8ed 100644
--- a/core/src/main/java/brooklyn/config/BrooklynServerConfig.java
+++ b/core/src/main/java/brooklyn/config/BrooklynServerConfig.java
@@ -152,7 +152,7 @@ public class BrooklynServerConfig {
     public static String resolvePersistencePath(String optionalSuppliedValue, StringConfigMap
brooklynProperties, String optionalObjectStoreLocationSpec) {
         String path = optionalSuppliedValue;
         if (path==null) path = brooklynProperties.getConfig(PERSISTENCE_DIR);
-        if (optionalObjectStoreLocationSpec==null) {
+        if (optionalObjectStoreLocationSpec==null || "localhost".equals(optionalObjectStoreLocationSpec))
{
             // file system
             if (path==null) path=DEFAULT_PERSISTENCE_DIR_FOR_FILESYSTEM;
             return resolveAgainstBaseDir(brooklynProperties, path);

http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/blob/0773c64f/core/src/main/java/brooklyn/entity/rebind/RebindManagerImpl.java
----------------------------------------------------------------------
diff --git a/core/src/main/java/brooklyn/entity/rebind/RebindManagerImpl.java b/core/src/main/java/brooklyn/entity/rebind/RebindManagerImpl.java
index 5ad0f7d..9c85e66 100644
--- a/core/src/main/java/brooklyn/entity/rebind/RebindManagerImpl.java
+++ b/core/src/main/java/brooklyn/entity/rebind/RebindManagerImpl.java
@@ -20,7 +20,6 @@ package brooklyn.entity.rebind;
 
 import static com.google.common.base.Preconditions.checkNotNull;
 
-import java.io.IOException;
 import java.util.Collection;
 import java.util.List;
 import java.util.Map;
@@ -454,7 +453,7 @@ public class RebindManagerImpl implements RebindManager {
     }
     
     @Override
-    public BrooklynMementoRawData retrieveMementoRawData() throws IOException {
+    public BrooklynMementoRawData retrieveMementoRawData() {
         RebindExceptionHandler exceptionHandler = RebindExceptionHandlerImpl.builder()
                 .danglingRefFailureMode(danglingRefFailureMode)
                 .rebindFailureMode(rebindFailureMode)
@@ -470,7 +469,7 @@ public class RebindManagerImpl implements RebindManager {
      * 
      * In so doing, it instantiates the entities + locations, registering them with the rebindContext.
      */
-    protected BrooklynMementoRawData loadMementoRawData(final RebindExceptionHandler exceptionHandler)
throws IOException {
+    protected BrooklynMementoRawData loadMementoRawData(final RebindExceptionHandler exceptionHandler)
{
         try {
             if (!(persistenceStoreAccess instanceof BrooklynMementoPersisterToObjectStore))
{
                 throw new IllegalStateException("Cannot load raw memento with persister "+persistenceStoreAccess);

http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/blob/0773c64f/core/src/main/java/brooklyn/entity/rebind/persister/BrooklynPersistenceUtils.java
----------------------------------------------------------------------
diff --git a/core/src/main/java/brooklyn/entity/rebind/persister/BrooklynPersistenceUtils.java
b/core/src/main/java/brooklyn/entity/rebind/persister/BrooklynPersistenceUtils.java
new file mode 100644
index 0000000..1308d62
--- /dev/null
+++ b/core/src/main/java/brooklyn/entity/rebind/persister/BrooklynPersistenceUtils.java
@@ -0,0 +1,103 @@
+/*
+ * 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 brooklyn.entity.rebind.persister;
+
+import brooklyn.config.BrooklynServerConfig;
+import brooklyn.entity.rebind.PersistenceExceptionHandler;
+import brooklyn.entity.rebind.PersistenceExceptionHandlerImpl;
+import brooklyn.entity.rebind.transformer.CompoundTransformer;
+import brooklyn.entity.rebind.transformer.CompoundTransformerLoader;
+import brooklyn.location.Location;
+import brooklyn.location.LocationSpec;
+import brooklyn.location.basic.LocalhostMachineProvisioningLocation;
+import brooklyn.management.ManagementContext;
+import brooklyn.management.ha.HighAvailabilityMode;
+import brooklyn.management.ha.ManagementPlaneSyncRecord;
+import brooklyn.management.ha.ManagementPlaneSyncRecordPersisterToObjectStore;
+import brooklyn.management.internal.ManagementContextInternal;
+import brooklyn.mementos.BrooklynMementoRawData;
+import brooklyn.util.ResourceUtils;
+import brooklyn.util.text.Strings;
+
+public class BrooklynPersistenceUtils {
+
+    /** Creates a {@link PersistenceObjectStore} for general-purpose use. */
+    public static PersistenceObjectStore newPersistenceObjectStore(ManagementContext managementContext,
+            String locationSpec, String locationContainer) {
+        
+        return newPersistenceObjectStore(managementContext, locationSpec, locationContainer,
+            PersistMode.AUTO, HighAvailabilityMode.STANDBY);
+    }
+    
+    /** Creates a {@link PersistenceObjectStore} for use with a specified set of modes. */
+    public static PersistenceObjectStore newPersistenceObjectStore(ManagementContext managementContext,
+            String locationSpec, String locationContainer, PersistMode persistMode, HighAvailabilityMode
highAvailabilityMode) {
+        PersistenceObjectStore destinationObjectStore;
+        locationContainer = BrooklynServerConfig.resolvePersistencePath(locationContainer,
managementContext.getConfig(), locationSpec);
+
+        Location location = null;
+        try {
+            if (Strings.isBlank(locationSpec)) {
+                location = managementContext.getLocationManager().createLocation(LocationSpec.create(LocalhostMachineProvisioningLocation.class));
+            } else {
+                location = managementContext.getLocationRegistry().resolve(locationSpec);
+                if (!(location instanceof LocationWithObjectStore)) {
+                    throw new IllegalArgumentException("Destination location "+location+"
does not offer a persistent store");
+                }
+            }
+        } finally {
+            if (location!=null) managementContext.getLocationManager().unmanage(location);
+        }
+        destinationObjectStore = ((LocationWithObjectStore)location).newPersistenceObjectStore(locationContainer);
+        
+        destinationObjectStore.injectManagementContext(managementContext);
+        destinationObjectStore.prepareForSharedUse(persistMode, highAvailabilityMode);
+        return destinationObjectStore;
+    }
+
+    public static void writeMemento(ManagementContext managementContext, BrooklynMementoRawData
memento,
+            PersistenceObjectStore destinationObjectStore) {
+        BrooklynMementoPersisterToObjectStore persister = new BrooklynMementoPersisterToObjectStore(
+            destinationObjectStore,
+            ((ManagementContextInternal)managementContext).getBrooklynProperties(),
+            managementContext.getCatalog().getRootClassLoader());
+        PersistenceExceptionHandler exceptionHandler = PersistenceExceptionHandlerImpl.builder().build();
+        persister.enableWriteAccess();
+        persister.checkpoint(memento, exceptionHandler);
+    }
+
+    public static void writeManagerMemento(ManagementContext managementContext, ManagementPlaneSyncRecord
optionalPlaneRecord,
+            PersistenceObjectStore destinationObjectStore) {
+        if (optionalPlaneRecord != null) {
+            ManagementPlaneSyncRecordPersisterToObjectStore managementPersister = new ManagementPlaneSyncRecordPersisterToObjectStore(
+                    managementContext, destinationObjectStore, managementContext.getCatalog().getRootClassLoader());
+            managementPersister.checkpoint(optionalPlaneRecord);
+        }
+    }
+
+    public static CompoundTransformer loadTransformer(ResourceUtils resources, String transformationsFileUrl)
{
+        if (Strings.isBlank(transformationsFileUrl)) {
+            return CompoundTransformer.NOOP; 
+        } else {
+            String contents = resources.getResourceAsString(transformationsFileUrl);
+            return CompoundTransformerLoader.load(contents);
+        }
+    }
+
+}

http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/blob/0773c64f/core/src/main/java/brooklyn/entity/rebind/persister/LocationWithObjectStore.java
----------------------------------------------------------------------
diff --git a/core/src/main/java/brooklyn/entity/rebind/persister/LocationWithObjectStore.java
b/core/src/main/java/brooklyn/entity/rebind/persister/LocationWithObjectStore.java
new file mode 100644
index 0000000..81af047
--- /dev/null
+++ b/core/src/main/java/brooklyn/entity/rebind/persister/LocationWithObjectStore.java
@@ -0,0 +1,27 @@
+/*
+ * 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 brooklyn.entity.rebind.persister;
+
+/** Marker interface for locations which can create a {@link PersistenceObjectStore} */
+public interface LocationWithObjectStore {
+
+    /** Creates a {@link PersistenceObjectStore} pointed at the given container/directory.
*/
+    public PersistenceObjectStore newPersistenceObjectStore(String container);
+    
+}

http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/blob/0773c64f/core/src/main/java/brooklyn/location/basic/LocalhostMachineProvisioningLocation.java
----------------------------------------------------------------------
diff --git a/core/src/main/java/brooklyn/location/basic/LocalhostMachineProvisioningLocation.java
b/core/src/main/java/brooklyn/location/basic/LocalhostMachineProvisioningLocation.java
index 113fb67..324f5a0 100644
--- a/core/src/main/java/brooklyn/location/basic/LocalhostMachineProvisioningLocation.java
+++ b/core/src/main/java/brooklyn/location/basic/LocalhostMachineProvisioningLocation.java
@@ -21,6 +21,7 @@ package brooklyn.location.basic;
 import static brooklyn.util.GroovyJavaMethods.elvis;
 import static brooklyn.util.GroovyJavaMethods.truth;
 
+import java.io.File;
 import java.net.InetAddress;
 import java.util.Arrays;
 import java.util.Map;
@@ -33,6 +34,9 @@ import brooklyn.config.ConfigKey;
 import brooklyn.config.ConfigKey.HasConfigKey;
 import brooklyn.entity.basic.BrooklynConfigKeys;
 import brooklyn.entity.basic.ConfigKeys;
+import brooklyn.entity.rebind.persister.FileBasedObjectStore;
+import brooklyn.entity.rebind.persister.LocationWithObjectStore;
+import brooklyn.entity.rebind.persister.PersistenceObjectStore;
 import brooklyn.location.AddressableLocation;
 import brooklyn.location.LocationSpec;
 import brooklyn.location.OsDetails;
@@ -61,7 +65,7 @@ import com.google.common.collect.Sets;
  * By default you can only obtain a single SshMachineLocation for the localhost. Optionally,
you can "overload"
  * and choose to allow localhost to be provisioned multiple times, which may be useful in
some testing scenarios.
  */
-public class LocalhostMachineProvisioningLocation extends FixedListMachineProvisioningLocation<SshMachineLocation>
implements AddressableLocation {
+public class LocalhostMachineProvisioningLocation extends FixedListMachineProvisioningLocation<SshMachineLocation>
implements AddressableLocation, LocationWithObjectStore {
 
     public static final Logger LOG = LoggerFactory.getLogger(LocalhostMachineProvisioningLocation.class);
     
@@ -324,4 +328,11 @@ public class LocalhostMachineProvisioningLocation extends FixedListMachineProvis
         return SudoChecker.isSudoAllowed();
     }
 
+    @Override
+    public PersistenceObjectStore newPersistenceObjectStore(String container) {
+        File basedir = new File(container);
+        if (basedir.isFile()) throw new IllegalArgumentException("Destination directory must
not be a file");
+        return new FileBasedObjectStore(basedir);
+    }
+    
 }

http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/blob/0773c64f/docs/use/guide/persistence/index.md
----------------------------------------------------------------------
diff --git a/docs/use/guide/persistence/index.md b/docs/use/guide/persistence/index.md
index 22ba5ce..a59a042 100644
--- a/docs/use/guide/persistence/index.md
+++ b/docs/use/guide/persistence/index.md
@@ -16,7 +16,7 @@ of your choice.
 Command Line Options
 --------------------
 
-To configure brooklyn, the relevant command line options are:
+To configure brooklyn, the relevant command line options for the `launch` commands are:
 
 * `--persist` <persistence mode>
   The persistence mode.
@@ -34,6 +34,12 @@ For the persistence mode, the possible values are:
 * `auto` means Brooklyn will rebind if there is any existing state, or will start afresh
if 
   there is no state.
 
+The persistence directory and location can instead be specified from `brooklyn.properties`
using
+the following config keys:
+
+* `brooklyn.persistence.dir`
+* `brooklyn.persistence.location.spec`
+
 
 <a name="file-based-persistence"></a>
 File-based Persistence

http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/blob/0773c64f/locations/jclouds/src/main/java/brooklyn/location/jclouds/JcloudsLocation.java
----------------------------------------------------------------------
diff --git a/locations/jclouds/src/main/java/brooklyn/location/jclouds/JcloudsLocation.java
b/locations/jclouds/src/main/java/brooklyn/location/jclouds/JcloudsLocation.java
index e77fb81..2a86361 100644
--- a/locations/jclouds/src/main/java/brooklyn/location/jclouds/JcloudsLocation.java
+++ b/locations/jclouds/src/main/java/brooklyn/location/jclouds/JcloudsLocation.java
@@ -87,6 +87,9 @@ import brooklyn.config.ConfigKey;
 import brooklyn.config.ConfigKey.HasConfigKey;
 import brooklyn.config.ConfigUtils;
 import brooklyn.entity.basic.Entities;
+import brooklyn.entity.rebind.persister.LocationWithObjectStore;
+import brooklyn.entity.rebind.persister.PersistenceObjectStore;
+import brooklyn.entity.rebind.persister.jclouds.JcloudsBlobStoreBasedObjectStore;
 import brooklyn.location.LocationSpec;
 import brooklyn.location.MachineLocation;
 import brooklyn.location.MachineManagementMixins.MachineMetadata;
@@ -166,7 +169,7 @@ import com.google.common.primitives.Ints;
  * Configuration flags are defined in {@link JcloudsLocationConfig}.
  */
 @SuppressWarnings("serial")
-public class JcloudsLocation extends AbstractCloudMachineProvisioningLocation implements
JcloudsLocationConfig, RichMachineProvisioningLocation<SshMachineLocation> {
+public class JcloudsLocation extends AbstractCloudMachineProvisioningLocation implements
JcloudsLocationConfig, RichMachineProvisioningLocation<SshMachineLocation>, LocationWithObjectStore
{
 
     // TODO After converting from Groovy to Java, this is now very bad code! It relies entirely
on putting 
     // things into and taking them out of maps; it's not type-safe, and it's thus very error-prone.
@@ -2040,4 +2043,10 @@ public class JcloudsLocation extends AbstractCloudMachineProvisioningLocation
im
        }
        return iptablesRules;
     }
+    
+    @Override
+    public PersistenceObjectStore newPersistenceObjectStore(String container) {
+        return new JcloudsBlobStoreBasedObjectStore(this, container);
+    }
+    
 }

http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/blob/0773c64f/usage/cli/src/main/java/brooklyn/cli/Main.java
----------------------------------------------------------------------
diff --git a/usage/cli/src/main/java/brooklyn/cli/Main.java b/usage/cli/src/main/java/brooklyn/cli/Main.java
index 00bf407..b1b933c 100644
--- a/usage/cli/src/main/java/brooklyn/cli/Main.java
+++ b/usage/cli/src/main/java/brooklyn/cli/Main.java
@@ -55,9 +55,9 @@ import brooklyn.entity.basic.ApplicationBuilder;
 import brooklyn.entity.basic.Entities;
 import brooklyn.entity.basic.StartableApplication;
 import brooklyn.entity.proxying.EntitySpec;
+import brooklyn.entity.rebind.persister.BrooklynPersistenceUtils;
 import brooklyn.entity.rebind.persister.PersistMode;
 import brooklyn.entity.rebind.transformer.CompoundTransformer;
-import brooklyn.entity.rebind.transformer.CompoundTransformerLoader;
 import brooklyn.entity.trait.Startable;
 import brooklyn.launcher.BrooklynLauncher;
 import brooklyn.launcher.BrooklynServerDetails;
@@ -784,13 +784,8 @@ public class Main extends AbstractMain {
             return null;
         }
 
-        protected CompoundTransformer loadTransformer(String transformations) {
-            if (transformations == null) {
-                return CompoundTransformer.NOOP; 
-            } else {
-                String contents = ResourceUtils.create(this).getResourceAsString(transformations);
-                return CompoundTransformerLoader.load(contents);
-            }
+        protected CompoundTransformer loadTransformer(String transformationsFileUrl) {
+            return BrooklynPersistenceUtils.loadTransformer(ResourceUtils.create(this), transformationsFileUrl);
         }
         
         @Override

http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/blob/0773c64f/usage/launcher/src/main/java/brooklyn/launcher/BrooklynLauncher.java
----------------------------------------------------------------------
diff --git a/usage/launcher/src/main/java/brooklyn/launcher/BrooklynLauncher.java b/usage/launcher/src/main/java/brooklyn/launcher/BrooklynLauncher.java
index d9cde0c..f28b15f 100644
--- a/usage/launcher/src/main/java/brooklyn/launcher/BrooklynLauncher.java
+++ b/usage/launcher/src/main/java/brooklyn/launcher/BrooklynLauncher.java
@@ -61,10 +61,9 @@ import brooklyn.entity.rebind.PersistenceExceptionHandlerImpl;
 import brooklyn.entity.rebind.RebindManager;
 import brooklyn.entity.rebind.RebindManagerImpl;
 import brooklyn.entity.rebind.persister.BrooklynMementoPersisterToObjectStore;
-import brooklyn.entity.rebind.persister.FileBasedObjectStore;
+import brooklyn.entity.rebind.persister.BrooklynPersistenceUtils;
 import brooklyn.entity.rebind.persister.PersistMode;
 import brooklyn.entity.rebind.persister.PersistenceObjectStore;
-import brooklyn.entity.rebind.persister.jclouds.JcloudsBlobStoreBasedObjectStore;
 import brooklyn.entity.rebind.transformer.CompoundTransformer;
 import brooklyn.entity.trait.Startable;
 import brooklyn.internal.BrooklynFeatureEnablement;
@@ -467,10 +466,10 @@ public class BrooklynLauncher {
 
     /**
      * @param destinationDir Directory for state to be copied to
-     * @param destinationLocation Optional location if target for copied state is a blob
store.
+     * @param destinationLocationSpec Optional location if target for copied state is a blob
store.
      * @param transformer Optional transformations to apply to retrieved state before it
is copied.
      */
-    public void copyPersistedState(String destinationDir, @Nullable String destinationLocation,
@Nullable CompoundTransformer transformer) {
+    public void copyPersistedState(String destinationDir, @Nullable String destinationLocationSpec,
@Nullable CompoundTransformer transformer) {
         initManagementContext();
         try {
             highAvailabilityMode = HighAvailabilityMode.HOT_STANDBY;
@@ -478,45 +477,34 @@ public class BrooklynLauncher {
         } catch (Exception e) {
             handleSubsystemStartupError(ignorePersistenceErrors, "persistence", e);
         }
-        ManagementPlaneSyncRecord planeState = managementContext.getHighAvailabilityManager().getManagementPlaneSyncState();
-        BrooklynMementoRawData memento = retrieveState(false);
-        BrooklynMementoRawData newMemento = memento;
-        if (transformer != null) {
-            try {
-                newMemento = transformer.transform(memento);
-            } catch (Exception e) {
-                throw Exceptions.propagate(e);
-            }
-        }
-        copyPersistedState(newMemento, planeState, destinationDir, destinationLocation);
+        
+        try {
+            BrooklynMementoRawData memento = managementContext.getRebindManager().retrieveMementoRawData();
+            if (transformer != null) memento = transformer.transform(memento);
+            
+            ManagementPlaneSyncRecord planeState = managementContext.getHighAvailabilityManager().getManagementPlaneSyncState();
+            
+            LOG.info("Persisting state to "+destinationDir+(destinationLocationSpec!=null
? " @ "+destinationLocationSpec : ""));
+            PersistenceObjectStore destinationObjectStore = BrooklynPersistenceUtils.newPersistenceObjectStore(
+                managementContext, destinationLocationSpec, destinationDir);
+            BrooklynPersistenceUtils.writeMemento(managementContext, memento, destinationObjectStore);
+            BrooklynPersistenceUtils.writeManagerMemento(managementContext, planeState, destinationObjectStore);
 
+        } catch (Exception e) {
+            Exceptions.propagateIfFatal(e);
+            LOG.debug("Error copying persisted state (rethrowing): " + e, e);
+            throw new FatalRuntimeException("Error copying persisted state: " +
+                Exceptions.collapseText(e), e);
+        }
     }
 
     /** @deprecated since 0.7.0 use {@link #copyPersistedState} instead */
     // Make private after deprecation
     @Deprecated
     public BrooklynMementoRawData retrieveState() {
-        return retrieveState(true);
-    }
-
-    /** To be removed when {@link #retrieveState()} is made private. */
-    private BrooklynMementoRawData retrieveState(boolean initPersistence) {
         initManagementContext();
-        if (initPersistence) {
-            try {
-                initPersistence();
-            } catch (Exception e) {
-                handleSubsystemStartupError(ignorePersistenceErrors, "persistence", e);
-            }
-        }
-        try {
-            return managementContext.getRebindManager().retrieveMementoRawData();
-        } catch (Exception e) {
-            Exceptions.propagateIfFatal(e);
-            LOG.debug("Error rebinding to persisted state (rethrowing): " + e, e);
-            throw new FatalRuntimeException("Error rebinding to persisted state: " +
-                    Exceptions.collapseText(e), e);
-        }
+        initPersistence();
+        return managementContext.getRebindManager().retrieveMementoRawData();
     }
 
     /**
@@ -527,49 +515,11 @@ public class BrooklynLauncher {
      */
     // Make private after deprecation
     @Deprecated
-    public void persistState(BrooklynMementoRawData memento, String destinationDir, @Nullable
String destinationLocation) {
-        copyPersistedState(memento, null, destinationDir, destinationLocation);
-    }
-
-    private void copyPersistedState(BrooklynMementoRawData memento, ManagementPlaneSyncRecord
planeRecord,
-            String destinationDir, String destinationLocation) {
-        LOG.info("Persisting state to "+destinationDir+(Strings.isBlank(destinationLocation)
? "" : " @ "+destinationLocation));
-
+    public void persistState(BrooklynMementoRawData memento, String destinationDir, @Nullable
String destinationLocationSpec) {
         initManagementContext();
-        try {
-            destinationDir = BrooklynServerConfig.resolvePersistencePath(destinationDir,
brooklynProperties, destinationLocation);
-            PersistenceObjectStore destinationObjectStore;
-
-            if (Strings.isBlank(destinationLocation)) {
-                File persistenceDirF = new File(destinationDir);
-                if (persistenceDirF.isFile()) throw new FatalConfigurationRuntimeException("Destination
directory must not be a file");
-                destinationObjectStore = new FileBasedObjectStore(persistenceDirF);
-            } else {
-                destinationObjectStore = new JcloudsBlobStoreBasedObjectStore(destinationLocation,
destinationDir);
-            }
-            destinationObjectStore.injectManagementContext(managementContext);
-            destinationObjectStore.prepareForSharedUse(persistMode, highAvailabilityMode);
-
-            BrooklynMementoPersisterToObjectStore persister = new BrooklynMementoPersisterToObjectStore(
-                    destinationObjectStore,
-                    ((ManagementContextInternal)managementContext).getBrooklynProperties(),
-                    managementContext.getCatalog().getRootClassLoader());
-
-            PersistenceExceptionHandler exceptionHandler = PersistenceExceptionHandlerImpl.builder().build();
-            persister.enableWriteAccess();
-            persister.checkpoint(memento, exceptionHandler);
-
-            if (planeRecord != null) {
-                ManagementPlaneSyncRecordPersisterToObjectStore managementPersister = new
ManagementPlaneSyncRecordPersisterToObjectStore(
-                        managementContext, destinationObjectStore, managementContext.getCatalog().getRootClassLoader());
-                managementPersister.checkpoint(planeRecord);
-            }
-        } catch (Exception e) {
-            Exceptions.propagateIfFatal(e);
-            LOG.debug("Error rebinding to persisted state (rethrowing): "+e, e);
-            throw new FatalRuntimeException("Error rebinding to persisted state: "+
-                Exceptions.collapseText(e), e);
-        }
+        PersistenceObjectStore destinationObjectStore = BrooklynPersistenceUtils.newPersistenceObjectStore(
+            managementContext, destinationLocationSpec, destinationDir);
+        BrooklynPersistenceUtils.writeMemento(managementContext, memento, destinationObjectStore);
     }
 
     /**
@@ -585,7 +535,7 @@ public class BrooklynLauncher {
         // Create the management context
         initManagementContext();
 
-        // Add a CAMP platform (TODO include a flag for this?)
+        // Add a CAMP platform
         campPlatform = new BrooklynCampPlatformLauncherNoServer()
                 .useManagementContext(managementContext)
                 .launch()
@@ -781,22 +731,23 @@ public class BrooklynLauncher {
             objectStore = null;
             
         } else {
-            if (persistenceLocation == null) {
-                persistenceLocation = brooklynProperties.getConfig(BrooklynServerConfig.PERSISTENCE_LOCATION_SPEC);
-            }
-            
-            persistenceDir = BrooklynServerConfig.resolvePersistencePath(persistenceDir,
brooklynProperties, persistenceLocation);
-
-            if (Strings.isBlank(persistenceLocation)) {
-                File persistenceDirF = new File(persistenceDir);
-                if (persistenceDirF.isFile()) throw new FatalConfigurationRuntimeException("Persistence
directory must not be a file");
-                objectStore = new FileBasedObjectStore(persistenceDirF);
-            } else {
-                objectStore = new JcloudsBlobStoreBasedObjectStore(persistenceLocation, persistenceDir);
-            }
             try {
-                objectStore.injectManagementContext(managementContext);
-                objectStore.prepareForSharedUse(persistMode, highAvailabilityMode);
+                if (persistenceLocation == null) {
+                    persistenceLocation = brooklynProperties.getConfig(BrooklynServerConfig.PERSISTENCE_LOCATION_SPEC);
+                }
+                persistenceDir = BrooklynServerConfig.resolvePersistencePath(persistenceDir,
brooklynProperties, persistenceLocation);
+                objectStore = BrooklynPersistenceUtils.newPersistenceObjectStore(managementContext,
persistenceLocation, persistenceDir, 
+                    persistMode, highAvailabilityMode);
+                    
+                RebindManager rebindManager = managementContext.getRebindManager();
+                
+                BrooklynMementoPersisterToObjectStore persister = new BrooklynMementoPersisterToObjectStore(
+                    objectStore,
+                    ((ManagementContextInternal)managementContext).getBrooklynProperties(),
+                    managementContext.getCatalog().getRootClassLoader());
+                PersistenceExceptionHandler persistenceExceptionHandler = PersistenceExceptionHandlerImpl.builder().build();
+                ((RebindManagerImpl) rebindManager).setPeriodicPersistPeriod(persistPeriod);
+                rebindManager.setPersister(persister, persistenceExceptionHandler);
             } catch (FatalConfigurationRuntimeException e) {
                 throw e;
             } catch (Exception e) {
@@ -805,16 +756,6 @@ public class BrooklynLauncher {
                 throw new FatalRuntimeException("Error initializing persistence subsystem:
"+
                     Exceptions.collapseText(e), e);
             }
-
-            RebindManager rebindManager = managementContext.getRebindManager();
-
-            BrooklynMementoPersisterToObjectStore persister = new BrooklynMementoPersisterToObjectStore(
-                    objectStore,
-                    ((ManagementContextInternal)managementContext).getBrooklynProperties(),
-                    managementContext.getCatalog().getRootClassLoader());
-            PersistenceExceptionHandler persistenceExceptionHandler = PersistenceExceptionHandlerImpl.builder().build();
-            ((RebindManagerImpl) rebindManager).setPeriodicPersistPeriod(persistPeriod);
-            rebindManager.setPersister(persister, persistenceExceptionHandler);
         }
         
         // Initialise the HA manager as required
@@ -915,6 +856,7 @@ public class BrooklynLauncher {
             return;
         }
         ApplicationBuilder brooklyn = new ApplicationBuilder() {
+            @SuppressWarnings("deprecation")
             @Override
             protected void doBuild() {
                 addChild(EntitySpec.create(LocalBrooklynNode.class)


Mime
View raw message