brooklyn-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From aleds...@apache.org
Subject [2/4] git commit: BROOKLYN-78: copy-state command copies nodes directory
Date Fri, 17 Oct 2014 20:41:37 GMT
BROOKLYN-78: copy-state command copies nodes directory


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

Branch: refs/heads/master
Commit: 53121a3804a50183f768df61ed40b19cb90b2df6
Parents: cdc0b3f
Author: Sam Corbett <sam.corbett@cloudsoftcorp.com>
Authored: Wed Oct 15 23:11:08 2014 +0100
Committer: Sam Corbett <sam.corbett@cloudsoftcorp.com>
Committed: Fri Oct 17 17:23:39 2014 +0100

----------------------------------------------------------------------
 ...ntPlaneSyncRecordPersisterToObjectStore.java | 11 +++
 usage/cli/src/main/java/brooklyn/cli/Main.java  |  6 +-
 .../brooklyn/launcher/BrooklynLauncher.java     | 90 ++++++++++++++++++--
 .../BrooklynLauncherRebindTestFixture.java      |  3 +-
 .../BrooklynLauncherRebindTestToFiles.java      | 23 +++--
 5 files changed, 113 insertions(+), 20 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/blob/53121a38/core/src/main/java/brooklyn/management/ha/ManagementPlaneSyncRecordPersisterToObjectStore.java
----------------------------------------------------------------------
diff --git a/core/src/main/java/brooklyn/management/ha/ManagementPlaneSyncRecordPersisterToObjectStore.java
b/core/src/main/java/brooklyn/management/ha/ManagementPlaneSyncRecordPersisterToObjectStore.java
index a00f197..950f2b8 100644
--- a/core/src/main/java/brooklyn/management/ha/ManagementPlaneSyncRecordPersisterToObjectStore.java
+++ b/core/src/main/java/brooklyn/management/ha/ManagementPlaneSyncRecordPersisterToObjectStore.java
@@ -291,6 +291,17 @@ public class ManagementPlaneSyncRecordPersisterToObjectStore implements
Manageme
         masterWriter.waitForCurrentWrites(timeout);
     }
 
+    public void checkpoint(ManagementPlaneSyncRecord record) {
+        init();
+        for (ManagementNodeSyncRecord node : record.getManagementNodes().values()) {
+            // Check included in case the node in the memento is the one being initialised
by
+            // BrooklynLauncher in the copy state command.
+            if (!ManagementNodeState.INITIALIZING.equals(node.getStatus()) && node.getNodeId()
!= null) {
+                persist(node);
+            }
+        }
+    }
+
     private void persist(ManagementNodeSyncRecord node) {
         StoreObjectAccessorWithLock writer = getOrCreateNodeWriter(node.getNodeId());
         boolean fileExists = writer.exists();

http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/blob/53121a38/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 91c57b5..f3b9de1 100644
--- a/usage/cli/src/main/java/brooklyn/cli/Main.java
+++ b/usage/cli/src/main/java/brooklyn/cli/Main.java
@@ -765,11 +765,7 @@ public class Main extends AbstractMain {
             }
             
             try {
-                BrooklynMementoRawData memento = launcher.retrieveState();
-                CompoundTransformer transformer = loadTransformer(transformations);
-                BrooklynMementoRawData newMemento = transformer.transform(memento);
-                launcher.persistState(newMemento, destinationDir, destinationLocation);
-
+                launcher.copyPersistedState(destinationDir, destinationLocation, loadTransformer(transformations));
             } catch (FatalRuntimeException e) {
                 // rely on caller logging this propagated exception
                 throw e;

http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/blob/53121a38/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 cba2d28..29a951f 100644
--- a/usage/launcher/src/main/java/brooklyn/launcher/BrooklynLauncher.java
+++ b/usage/launcher/src/main/java/brooklyn/launcher/BrooklynLauncher.java
@@ -20,7 +20,8 @@ package brooklyn.launcher;
 
 import static com.google.common.base.Preconditions.checkNotNull;
 
-import brooklyn.management.ha.OsgiManager;
+import brooklyn.entity.rebind.transformer.CompoundTransformer;
+import brooklyn.management.ha.ManagementPlaneSyncRecord;
 import io.brooklyn.camp.CampPlatform;
 import io.brooklyn.camp.brooklyn.BrooklynCampPlatformLauncherNoServer;
 import io.brooklyn.camp.brooklyn.spi.creation.BrooklynAssemblyTemplateInstantiator;
@@ -102,6 +103,7 @@ import brooklyn.util.time.Time;
 
 import com.google.common.base.Splitter;
 import com.google.common.base.Stopwatch;
+import com.google.common.base.Throwables;
 import com.google.common.collect.ImmutableList;
 import com.google.common.collect.Lists;
 import com.google.common.collect.Maps;
@@ -451,18 +453,67 @@ public class BrooklynLauncher {
         return this;
     }
 
-    public BrooklynMementoRawData retrieveState() {
-        initManagementContext();
+    /**
+     * @param destinationDir Directory for state to be copied to
+     */
+    public void copyPersistedState(String destinationDir) {
+        copyPersistedState(destinationDir, null, null);
+    }
 
+    /**
+     * @param destinationDir Directory for state to be copied to
+     * @param destinationLocation Optional location if target for copied state is a blob
store.
+     */
+    public void copyPersistedState(String destinationDir, @Nullable String destinationLocation)
{
+        copyPersistedState(destinationDir, destinationLocation, null);
+    }
+
+    /**
+     * @param destinationDir Directory for state to be copied to
+     * @param destinationLocation 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) {
+        initManagementContext();
         try {
+            highAvailabilityMode = HighAvailabilityMode.HOT_STANDBY;
             initPersistence();
         } 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);
+
+    }
 
+    /** @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);
@@ -471,14 +522,27 @@ public class BrooklynLauncher {
         }
     }
 
-    public void persistState(BrooklynMementoRawData memento, String destinationDir, String
destinationLocation) {
+    /**
+     * @param memento The state to copy
+     * @param destinationDir Directory for state to be copied to
+     * @param destinationLocation Optional location if target for copied state is a blob
store.
+     * @deprecated since 0.7.0 use {@link #copyPersistedState} instead
+     */
+    // 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));
 
         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");
@@ -497,7 +561,12 @@ public class BrooklynLauncher {
             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);
@@ -752,9 +821,9 @@ public class BrooklynLauncher {
         } else {
             if (objectStore==null)
                 throw new FatalConfigurationRuntimeException("Cannot run in HA mode when
no persistence configured.");
-            
+
             HighAvailabilityManager haManager = managementContext.getHighAvailabilityManager();
-            ManagementPlaneSyncRecordPersister persister = 
+            ManagementPlaneSyncRecordPersister persister =
                 new ManagementPlaneSyncRecordPersisterToObjectStore(managementContext,
                     objectStore, managementContext.getCatalog().getRootClassLoader());
             ((HighAvailabilityManagerImpl)haManager).setHeartbeatTimeout(haHeartbeatTimeout);
@@ -943,6 +1012,9 @@ public class BrooklynLauncher {
         if (persistMode != PersistMode.DISABLED) {
             try {
                 Stopwatch stopwatch = Stopwatch.createStarted();
+                if (managementContext.getHighAvailabilityManager().getPersister() != null)
{
+                    managementContext.getHighAvailabilityManager().getPersister().waitForWritesCompleted(Duration.TEN_SECONDS);
+                }
                 managementContext.getRebindManager().waitForPendingComplete(Duration.TEN_SECONDS);
                 LOG.info("Finished waiting for persist; took "+Time.makeTimeStringRounded(stopwatch));
             } catch (RuntimeInterruptedException e) {

http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/blob/53121a38/usage/launcher/src/test/java/brooklyn/launcher/BrooklynLauncherRebindTestFixture.java
----------------------------------------------------------------------
diff --git a/usage/launcher/src/test/java/brooklyn/launcher/BrooklynLauncherRebindTestFixture.java
b/usage/launcher/src/test/java/brooklyn/launcher/BrooklynLauncherRebindTestFixture.java
index 9c5c15b..426f939 100644
--- a/usage/launcher/src/test/java/brooklyn/launcher/BrooklynLauncherRebindTestFixture.java
+++ b/usage/launcher/src/test/java/brooklyn/launcher/BrooklynLauncherRebindTestFixture.java
@@ -41,6 +41,7 @@ import brooklyn.entity.rebind.persister.PersistMode;
 import brooklyn.entity.rebind.persister.PersistenceObjectStore;
 import brooklyn.location.Location;
 import brooklyn.management.ManagementContext;
+import brooklyn.management.ha.HighAvailabilityMode;
 import brooklyn.test.Asserts;
 import brooklyn.test.entity.LocalManagementContextForTests;
 import brooklyn.test.entity.TestApplication;
@@ -225,11 +226,11 @@ public abstract class BrooklynLauncherRebindTestFixture {
 
     protected void populatePersistenceDir(String dir, EntitySpec<? extends StartableApplication>
appSpec) throws Exception {
         BrooklynLauncher launcher = newLauncherDefault(PersistMode.CLEAN)
+                .highAvailabilityMode(HighAvailabilityMode.MASTER)
                 .persistenceDir(dir)
                 .application(appSpec)
                 .start();
         launcher.terminate();
-        launcher = null;
         assertMementoContainerNonEmptyForTypeEventually("entities");
     }
     

http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/blob/53121a38/usage/launcher/src/test/java/brooklyn/launcher/BrooklynLauncherRebindTestToFiles.java
----------------------------------------------------------------------
diff --git a/usage/launcher/src/test/java/brooklyn/launcher/BrooklynLauncherRebindTestToFiles.java
b/usage/launcher/src/test/java/brooklyn/launcher/BrooklynLauncherRebindTestToFiles.java
index 6142185..1dce905 100644
--- a/usage/launcher/src/test/java/brooklyn/launcher/BrooklynLauncherRebindTestToFiles.java
+++ b/usage/launcher/src/test/java/brooklyn/launcher/BrooklynLauncherRebindTestToFiles.java
@@ -19,6 +19,9 @@
 package brooklyn.launcher;
 
 import static org.testng.Assert.assertEquals;
+import static org.testng.Assert.assertFalse;
+import static org.testng.Assert.assertNotEquals;
+import static org.testng.Assert.assertTrue;
 
 import java.io.File;
 
@@ -31,12 +34,14 @@ import brooklyn.entity.rebind.persister.BrooklynMementoPersisterToObjectStore;
 import brooklyn.entity.rebind.persister.FileBasedObjectStore;
 import brooklyn.entity.rebind.persister.PersistMode;
 import brooklyn.management.ManagementContext;
+import brooklyn.management.ha.HighAvailabilityMode;
 import brooklyn.mementos.BrooklynMementoRawData;
 import brooklyn.test.entity.TestApplication;
 import brooklyn.util.javalang.JavaClassNames;
 import brooklyn.util.os.Os;
 import brooklyn.util.text.Identifiers;
 
+import com.google.common.base.Joiner;
 import com.google.common.io.Files;
 
 public class BrooklynLauncherRebindTestToFiles extends BrooklynLauncherRebindTestFixture
{
@@ -114,19 +119,27 @@ public class BrooklynLauncherRebindTestToFiles extends BrooklynLauncherRebindTes
     public void testCopyPersistedState() throws Exception {
         EntitySpec<TestApplication> appSpec = EntitySpec.create(TestApplication.class);
         populatePersistenceDir(persistenceDir, appSpec);
-        
+
         File destinationDir = Files.createTempDir();
+        String destination = destinationDir.getAbsolutePath();
         String destinationLocation = null; // i.e. file system, rather than object store
         try {
             // Auto will rebind if the dir exists
             BrooklynLauncher launcher = newLauncherDefault(PersistMode.AUTO)
+                    .highAvailabilityMode(HighAvailabilityMode.MASTER)
                     .webconsole(false);
-            BrooklynMementoRawData memento = launcher.retrieveState();
-            launcher.persistState(memento, destinationDir.getAbsolutePath(), destinationLocation);
+            launcher.copyPersistedState(destination, destinationLocation);
             launcher.terminate();
             
-            assertEquals(memento.getEntities().size(), 1, "entityMementos="+memento.getEntities().keySet());
-            
+            File entities = new File(Os.mergePaths(destination), "entities");
+            assertTrue(entities.isDirectory(), "entities directory should exist");
+            assertEquals(entities.listFiles().length, 1, "entities directory should contain
one file (contained: "+
+                    Joiner.on(", ").join(entities.listFiles()) +")");
+
+            File nodes = new File(Os.mergePaths(destination, "nodes"));
+            assertTrue(nodes.isDirectory(), "nodes directory should exist");
+            assertNotEquals(nodes.listFiles().length, 0, "nodes directory should not be empty");
+
             // Should now have a usable copy in the destinationDir
             // Auto will rebind if the dir exists
             newLauncherDefault(PersistMode.AUTO)


Mime
View raw message