brooklyn-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From rich...@apache.org
Subject [15/26] git commit: Persistence: expunge policy when removed from entity
Date Tue, 10 Jun 2014 02:25:36 GMT
Persistence: expunge policy when removed from entity


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

Branch: refs/heads/master
Commit: 86b35c52ddd026298ff8c10d0510765f35481a3d
Parents: 0824428
Author: Aled Sage <aled.sage@gmail.com>
Authored: Thu Jun 5 22:45:14 2014 +0200
Committer: Aled Sage <aled.sage@gmail.com>
Committed: Fri Jun 6 16:32:23 2014 +0200

----------------------------------------------------------------------
 .../brooklyn/entity/rebind/ChangeListener.java  | 12 +++++++
 .../enricher/basic/AbstractEnricher.java        |  5 ++-
 .../brooklyn/entity/basic/AbstractEntity.java   | 13 +++----
 .../rebind/ImmediateDeltaChangeListener.java    | 36 +++++++++++++++----
 .../rebind/PeriodicDeltaChangeListener.java     | 38 ++++++++++++++++----
 .../entity/rebind/RebindManagerImpl.java        | 36 +++++++++++++++++++
 .../internal/EntityChangeListener.java          | 18 ++++++----
 .../internal/EntityManagementSupport.java       | 18 ++++++++--
 .../brooklyn/policy/basic/AbstractPolicy.java   |  5 ++-
 .../entity/rebind/RebindPolicyTest.java         | 17 +++++++++
 10 files changed, 167 insertions(+), 31 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/blob/86b35c52/api/src/main/java/brooklyn/entity/rebind/ChangeListener.java
----------------------------------------------------------------------
diff --git a/api/src/main/java/brooklyn/entity/rebind/ChangeListener.java b/api/src/main/java/brooklyn/entity/rebind/ChangeListener.java
index ee17ec8..559c82b 100644
--- a/api/src/main/java/brooklyn/entity/rebind/ChangeListener.java
+++ b/api/src/main/java/brooklyn/entity/rebind/ChangeListener.java
@@ -22,7 +22,11 @@ public interface ChangeListener {
         @Override public void onManaged(Location location) {}
         @Override public void onUnmanaged(Location location) {}
         @Override public void onChanged(Location location) {}
+        @Override public void onManaged(Policy policy) {}
+        @Override public void onUnmanaged(Policy policy) {}
         @Override public void onChanged(Policy policy) {}
+        @Override public void onManaged(Enricher enricher) {}
+        @Override public void onUnmanaged(Enricher enricher) {}
         @Override public void onChanged(Enricher enricher) {}
     };
     
@@ -38,7 +42,15 @@ public interface ChangeListener {
 
     void onChanged(Location location);
     
+    void onManaged(Policy policy);
+
+    void onUnmanaged(Policy policy);
+
     void onChanged(Policy policy);
     
+    void onManaged(Enricher enricher);
+
+    void onUnmanaged(Enricher enricher);
+
     void onChanged(Enricher enricher);
 }

http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/blob/86b35c52/core/src/main/java/brooklyn/enricher/basic/AbstractEnricher.java
----------------------------------------------------------------------
diff --git a/core/src/main/java/brooklyn/enricher/basic/AbstractEnricher.java b/core/src/main/java/brooklyn/enricher/basic/AbstractEnricher.java
index 7fe9811..b5c5015 100644
--- a/core/src/main/java/brooklyn/enricher/basic/AbstractEnricher.java
+++ b/core/src/main/java/brooklyn/enricher/basic/AbstractEnricher.java
@@ -44,6 +44,9 @@ public abstract class AbstractEnricher extends AbstractEntityAdjunct implements
 
     @Override
     protected void onChanged() {
-        getManagementContext().getRebindManager().getChangeListener().onChanged(this);
+        // TODO Could add EnricherChangeListener, similar to EntityChangeListener; should
we do that?
+        if (getManagementContext() != null) {
+            getManagementContext().getRebindManager().getChangeListener().onChanged(this);
+        }
     }
 }

http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/blob/86b35c52/core/src/main/java/brooklyn/entity/basic/AbstractEntity.java
----------------------------------------------------------------------
diff --git a/core/src/main/java/brooklyn/entity/basic/AbstractEntity.java b/core/src/main/java/brooklyn/entity/basic/AbstractEntity.java
index 074fbd7..b3c40a2 100644
--- a/core/src/main/java/brooklyn/entity/basic/AbstractEntity.java
+++ b/core/src/main/java/brooklyn/entity/basic/AbstractEntity.java
@@ -1065,7 +1065,7 @@ public abstract class AbstractEntity implements EntityLocal, EntityInternal
{
         policies.add((AbstractPolicy)policy);
         ((AbstractPolicy)policy).setEntity(this);
         
-        getManagementSupport().getEntityChangeListener().onPoliciesChanged();
+        getManagementSupport().getEntityChangeListener().onPolicyAdded(policy);
         emit(AbstractEntity.POLICY_ADDED, new PolicyDescriptor(policy));
     }
 
@@ -1089,7 +1089,7 @@ public abstract class AbstractEntity implements EntityLocal, EntityInternal
{
         boolean changed = policies.remove(policy);
         
         if (changed) {
-            getManagementSupport().getEntityChangeListener().onPoliciesChanged();
+            getManagementSupport().getEntityChangeListener().onPolicyRemoved(policy);
             emit(AbstractEntity.POLICY_REMOVED, new PolicyDescriptor(policy));
         }
         return changed;
@@ -1102,11 +1102,6 @@ public abstract class AbstractEntity implements EntityLocal, EntityInternal
{
             removePolicy(policy);
             changed = true;
         }
-        
-        if (changed) {
-            getManagementSupport().getEntityChangeListener().onPoliciesChanged();
-        }
-        
         return changed;
     }
     
@@ -1120,7 +1115,7 @@ public abstract class AbstractEntity implements EntityLocal, EntityInternal
{
         enrichers.add((AbstractEnricher) enricher);
         ((AbstractEnricher)enricher).setEntity(this);
         
-        getManagementSupport().getEntityChangeListener().onEnrichersChanged();
+        getManagementSupport().getEntityChangeListener().onEnricherAdded(enricher);
         // TODO Could add equivalent of AbstractEntity.POLICY_ADDED for enrichers; no use-case
for that yet
     }
 
@@ -1130,7 +1125,7 @@ public abstract class AbstractEntity implements EntityLocal, EntityInternal
{
         boolean changed = enrichers.remove(enricher);
         
         if (changed) {
-            getManagementSupport().getEntityChangeListener().onEnrichersChanged();
+            getManagementSupport().getEntityChangeListener().onEnricherRemoved(enricher);
         }
         return changed;
 

http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/blob/86b35c52/core/src/main/java/brooklyn/entity/rebind/ImmediateDeltaChangeListener.java
----------------------------------------------------------------------
diff --git a/core/src/main/java/brooklyn/entity/rebind/ImmediateDeltaChangeListener.java b/core/src/main/java/brooklyn/entity/rebind/ImmediateDeltaChangeListener.java
index a33bb12..36df495 100644
--- a/core/src/main/java/brooklyn/entity/rebind/ImmediateDeltaChangeListener.java
+++ b/core/src/main/java/brooklyn/entity/rebind/ImmediateDeltaChangeListener.java
@@ -32,16 +32,22 @@ public class ImmediateDeltaChangeListener implements ChangeListener {
     
     @Override
     public void onManaged(Entity entity) {
-        if (running && persister != null) {
-            onChanged(entity);
-        }
+        onChanged(entity);
     }
 
     @Override
     public void onManaged(Location location) {
-        if (running && persister != null) {
-            onChanged(location);
-        }
+        onChanged(location);
+    }
+    
+    @Override
+    public void onManaged(Policy policy) {
+        onChanged(policy);
+    }
+    
+    @Override
+    public void onManaged(Enricher enricher) {
+        onChanged(enricher);
     }
     
     @Override
@@ -111,6 +117,24 @@ public class ImmediateDeltaChangeListener implements ChangeListener {
     }
 
     @Override
+    public void onUnmanaged(Policy policy) {
+        if (running && persister != null) {
+            PersisterDeltaImpl delta = new PersisterDeltaImpl();
+            delta.removedPolicyIds.add(policy.getId());
+            persister.delta(delta);
+        }
+    }
+
+    @Override
+    public void onUnmanaged(Enricher enricher) {
+        if (running && persister != null) {
+            PersisterDeltaImpl delta = new PersisterDeltaImpl();
+            delta.removedEnricherIds.add(enricher.getId());
+            persister.delta(delta);
+        }
+    }
+
+    @Override
     public void onChanged(Location location) {
         if (running && persister != null) {
             PersisterDeltaImpl delta = new PersisterDeltaImpl();

http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/blob/86b35c52/core/src/main/java/brooklyn/entity/rebind/PeriodicDeltaChangeListener.java
----------------------------------------------------------------------
diff --git a/core/src/main/java/brooklyn/entity/rebind/PeriodicDeltaChangeListener.java b/core/src/main/java/brooklyn/entity/rebind/PeriodicDeltaChangeListener.java
index 2905493..6978e39 100644
--- a/core/src/main/java/brooklyn/entity/rebind/PeriodicDeltaChangeListener.java
+++ b/core/src/main/java/brooklyn/entity/rebind/PeriodicDeltaChangeListener.java
@@ -258,17 +258,25 @@ public class PeriodicDeltaChangeListener implements ChangeListener {
     @Override
     public synchronized void onManaged(Entity entity) {
         if (LOG.isTraceEnabled()) LOG.trace("onManaged: {}", entity);
-        if (!isStopped()) {
-            onChanged(entity);
-        }
+        onChanged(entity);
     }
 
     @Override
     public synchronized void onManaged(Location location) {
         if (LOG.isTraceEnabled()) LOG.trace("onManaged: {}", location);
-        if (!isStopped()) {
-            onChanged(location);
-        }
+        onChanged(location);
+    }
+    
+    @Override
+    public synchronized void onManaged(Policy policy) {
+        if (LOG.isTraceEnabled()) LOG.trace("onManaged: {}", policy);
+        onChanged(policy);
+    }
+    
+    @Override
+    public synchronized void onManaged(Enricher enricher) {
+        if (LOG.isTraceEnabled()) LOG.trace("onManaged: {}", enricher);
+        onChanged(enricher);
     }
     
     @Override
@@ -324,6 +332,24 @@ public class PeriodicDeltaChangeListener implements ChangeListener {
     }
 
     @Override
+    public synchronized void onUnmanaged(Policy policy) {
+        if (LOG.isTraceEnabled()) LOG.trace("onUnmanaged: {}", policy);
+        if (!isStopped()) {
+            deltaCollector.removedPolicyIds.add(policy.getId());
+            deltaCollector.policies.remove(policy);
+        }
+    }
+
+    @Override
+    public synchronized void onUnmanaged(Enricher enricher) {
+        if (LOG.isTraceEnabled()) LOG.trace("onUnmanaged: {}", enricher);
+        if (!isStopped()) {
+            deltaCollector.removedEnricherIds.add(enricher.getId());
+            deltaCollector.enrichers.remove(enricher);
+        }
+    }
+
+    @Override
     public synchronized void onChanged(Location location) {
         if (LOG.isTraceEnabled()) LOG.trace("onChanged: {}", location);
         if (!isStopped()) {

http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/blob/86b35c52/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 ae38083..c0c45ca 100644
--- a/core/src/main/java/brooklyn/entity/rebind/RebindManagerImpl.java
+++ b/core/src/main/java/brooklyn/entity/rebind/RebindManagerImpl.java
@@ -715,6 +715,24 @@ public class RebindManagerImpl implements RebindManager {
         }
         
         @Override
+        public void onManaged(Policy policy) {
+            try {
+                delegate.onManaged(policy);
+            } catch (Throwable t) {
+                LOG.error("Error persisting mememento onManaged("+policy+"); continuing.",
t);
+            }
+        }
+
+        @Override
+        public void onManaged(Enricher enricher) {
+            try {
+                delegate.onManaged(enricher);
+            } catch (Throwable t) {
+                LOG.error("Error persisting mememento onManaged("+enricher+"); continuing.",
t);
+            }
+        }
+
+        @Override
         public void onChanged(Entity entity) {
             try {
                 delegate.onChanged(entity);
@@ -742,6 +760,24 @@ public class RebindManagerImpl implements RebindManager {
         }
 
         @Override
+        public void onUnmanaged(Policy policy) {
+            try {
+                delegate.onUnmanaged(policy);
+            } catch (Throwable t) {
+                LOG.error("Error persisting mememento onUnmanaged("+policy+"); continuing.",
t);
+            }
+        }
+
+        @Override
+        public void onUnmanaged(Enricher enricher) {
+            try {
+                delegate.onUnmanaged(enricher);
+            } catch (Throwable t) {
+                LOG.error("Error persisting mememento onUnmanaged("+enricher+"); continuing.",
t);
+            }
+        }
+
+        @Override
         public void onChanged(Location location) {
             try {
                 delegate.onChanged(location);

http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/blob/86b35c52/core/src/main/java/brooklyn/management/internal/EntityChangeListener.java
----------------------------------------------------------------------
diff --git a/core/src/main/java/brooklyn/management/internal/EntityChangeListener.java b/core/src/main/java/brooklyn/management/internal/EntityChangeListener.java
index 31805e6..39e9290 100644
--- a/core/src/main/java/brooklyn/management/internal/EntityChangeListener.java
+++ b/core/src/main/java/brooklyn/management/internal/EntityChangeListener.java
@@ -3,6 +3,8 @@ package brooklyn.management.internal;
 import brooklyn.config.ConfigKey;
 import brooklyn.entity.Effector;
 import brooklyn.event.AttributeSensor;
+import brooklyn.policy.Enricher;
+import brooklyn.policy.Policy;
 
 public interface EntityChangeListener {
 
@@ -13,8 +15,10 @@ public interface EntityChangeListener {
         @Override public void onLocationsChanged() {}
         @Override public void onMembersChanged() {}
         @Override public void onChildrenChanged() {}
-        @Override public void onPoliciesChanged() {}
-        @Override public void onEnrichersChanged() {}
+        @Override public void onPolicyAdded(Policy policy) {}
+        @Override public void onPolicyRemoved(Policy policy) {}
+        @Override public void onEnricherAdded(Enricher enricher) {}
+        @Override public void onEnricherRemoved(Enricher enricher) {}
         @Override public void onEffectorStarting(Effector<?> effector) {}
         @Override public void onEffectorCompleted(Effector<?> effector) {}
     };
@@ -31,11 +35,13 @@ public interface EntityChangeListener {
 
     void onChildrenChanged();
 
-    // FIXME Also want something to be notified when policy's state/config changes.
-    //       Do we want a separate PolicyChangeListener instead? Or everything through EntityChangeListener?
-    void onPoliciesChanged();
+    void onPolicyAdded(Policy policy);
 
-    void onEnrichersChanged();
+    void onPolicyRemoved(Policy policy);
+
+    void onEnricherAdded(Enricher enricher);
+
+    void onEnricherRemoved(Enricher enricher);
 
     void onEffectorStarting(Effector<?> effector);
     

http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/blob/86b35c52/core/src/main/java/brooklyn/management/internal/EntityManagementSupport.java
----------------------------------------------------------------------
diff --git a/core/src/main/java/brooklyn/management/internal/EntityManagementSupport.java
b/core/src/main/java/brooklyn/management/internal/EntityManagementSupport.java
index 83b42e1..bad412a 100644
--- a/core/src/main/java/brooklyn/management/internal/EntityManagementSupport.java
+++ b/core/src/main/java/brooklyn/management/internal/EntityManagementSupport.java
@@ -20,6 +20,8 @@ import brooklyn.management.ExecutionContext;
 import brooklyn.management.ManagementContext;
 import brooklyn.management.SubscriptionContext;
 import brooklyn.management.internal.NonDeploymentManagementContext.NonDeploymentManagementContextMode;
+import brooklyn.policy.Enricher;
+import brooklyn.policy.Policy;
 import brooklyn.util.exceptions.Exceptions;
 
 import com.google.common.annotations.Beta;
@@ -339,12 +341,24 @@ public class EntityManagementSupport {
             getManagementContext().getRebindManager().getChangeListener().onChanged(entity);
         }
         @Override
-        public void onPoliciesChanged() {
+        public void onPolicyAdded(Policy policy) {
             getManagementContext().getRebindManager().getChangeListener().onChanged(entity);
+            getManagementContext().getRebindManager().getChangeListener().onManaged(policy);
         }
         @Override
-        public void onEnrichersChanged() {
+        public void onEnricherAdded(Enricher enricher) {
             getManagementContext().getRebindManager().getChangeListener().onChanged(entity);
+            getManagementContext().getRebindManager().getChangeListener().onManaged(enricher);
+        }
+        @Override
+        public void onPolicyRemoved(Policy policy) {
+            getManagementContext().getRebindManager().getChangeListener().onChanged(entity);
+            getManagementContext().getRebindManager().getChangeListener().onUnmanaged(policy);
+        }
+        @Override
+        public void onEnricherRemoved(Enricher enricher) {
+            getManagementContext().getRebindManager().getChangeListener().onChanged(entity);
+            getManagementContext().getRebindManager().getChangeListener().onUnmanaged(enricher);
         }
         @Override
         public void onAttributeChanged(AttributeSensor<?> attribute) {

http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/blob/86b35c52/core/src/main/java/brooklyn/policy/basic/AbstractPolicy.java
----------------------------------------------------------------------
diff --git a/core/src/main/java/brooklyn/policy/basic/AbstractPolicy.java b/core/src/main/java/brooklyn/policy/basic/AbstractPolicy.java
index 5bd8843..da2ea57 100644
--- a/core/src/main/java/brooklyn/policy/basic/AbstractPolicy.java
+++ b/core/src/main/java/brooklyn/policy/basic/AbstractPolicy.java
@@ -78,7 +78,10 @@ public abstract class AbstractPolicy extends AbstractEntityAdjunct implements
Po
 
     @Override
     protected void onChanged() {
-        getManagementContext().getRebindManager().getChangeListener().onChanged(this);
+        // TODO Could add PolicyChangeListener, similar to EntityChangeListener; should we
do that?
+        if (getManagementContext() != null) {
+            getManagementContext().getRebindManager().getChangeListener().onChanged(this);
+        }
     }
     
     @Override

http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/blob/86b35c52/core/src/test/java/brooklyn/entity/rebind/RebindPolicyTest.java
----------------------------------------------------------------------
diff --git a/core/src/test/java/brooklyn/entity/rebind/RebindPolicyTest.java b/core/src/test/java/brooklyn/entity/rebind/RebindPolicyTest.java
index 552db17..b136665 100644
--- a/core/src/test/java/brooklyn/entity/rebind/RebindPolicyTest.java
+++ b/core/src/test/java/brooklyn/entity/rebind/RebindPolicyTest.java
@@ -113,6 +113,23 @@ public class RebindPolicyTest extends RebindTestFixtureWithApp {
     }
 
     @Test
+    public void testExpungesOnPolicyRemoved() throws Exception {
+        TestEntity entity = origApp.createAndManageChild(EntitySpec.create(TestEntity.class));
+        MyPolicy policy = entity.addPolicy(PolicySpec.create(MyPolicy.class));
+        MyEnricher enricher = entity.addEnricher(EnricherSpec.create(MyEnricher.class));
+
+        RebindTestUtils.waitForPersisted(origApp);
+
+        entity.removePolicy(policy);
+        entity.removeEnricher(enricher);
+        RebindTestUtils.waitForPersisted(origApp);
+        
+        BrooklynMementoManifest manifest = loadMementoManifest();
+        assertFalse(manifest.getPolicyIdToType().containsKey(policy.getId()));
+        assertFalse(manifest.getEnricherIdToType().containsKey(enricher.getId()));
+    }
+
+    @Test
     public void testReboundConfigDoesNotContainId() throws Exception {
         MyPolicy policy = origApp.addPolicy(PolicySpec.create(MyPolicy.class));
         


Mime
View raw message