brooklyn-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From aleds...@apache.org
Subject [2/3] incubator-brooklyn git commit: Adds AttributeSensor.persistenceMode
Date Mon, 15 Dec 2014 23:58:17 GMT
Adds AttributeSensor.persistenceMode

- If set to NONE then that attribute’s value will not be persisted, so will
  not be available on rebind.
- Adds Sensors.builder() to avoid too much overloading.


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

Branch: refs/heads/master
Commit: 327c227d4d1fe53d497b6af0640c988e9a068939
Parents: b3cea88
Author: Aled Sage <aled.sage@gmail.com>
Authored: Mon Dec 15 17:36:20 2014 +0000
Committer: Aled Sage <aled.sage@gmail.com>
Committed: Mon Dec 15 21:15:49 2014 +0000

----------------------------------------------------------------------
 .../java/brooklyn/event/AttributeSensor.java    | 28 ++++++++++++-
 .../entity/rebind/dto/MementosGenerators.java   |  8 ++--
 .../event/basic/BasicAttributeSensor.java       | 18 +++++++-
 .../main/java/brooklyn/event/basic/Sensors.java | 43 ++++++++++++++++++++
 .../entity/rebind/RebindEntityTest.java         | 21 ++++++++++
 5 files changed, 112 insertions(+), 6 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/blob/327c227d/api/src/main/java/brooklyn/event/AttributeSensor.java
----------------------------------------------------------------------
diff --git a/api/src/main/java/brooklyn/event/AttributeSensor.java b/api/src/main/java/brooklyn/event/AttributeSensor.java
index b50e4ef..58e7480 100644
--- a/api/src/main/java/brooklyn/event/AttributeSensor.java
+++ b/api/src/main/java/brooklyn/event/AttributeSensor.java
@@ -18,9 +18,35 @@
  */
 package brooklyn.event;
 
+import com.google.common.annotations.Beta;
+
 /**
  * The interface implemented by attribute sensors.
  */
 public interface AttributeSensor<T> extends Sensor<T> {
-    // Marker Interface
+    
+    /**
+     * @since 0.7.0
+     */
+    @Beta
+    public enum SensorPersistenceMode {
+        /**
+         * Indicates that this sensor should be persisted, and its value should be read from
+         * persisted state on rebind.
+         */
+        REQUIRED,
+        
+        /**
+         * Indicates that this sensor should not be persisted; therefore its value for any
entity
+         * will be null immediately after rebind.
+         */
+        NONE;
+    }
+    
+    /**
+     * The persistence mode of this sensor, to determine its behaviour for rebind.
+     * 
+     * @since 0.7.0
+     */
+    SensorPersistenceMode getPersistenceMode();
 }

http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/blob/327c227d/core/src/main/java/brooklyn/entity/rebind/dto/MementosGenerators.java
----------------------------------------------------------------------
diff --git a/core/src/main/java/brooklyn/entity/rebind/dto/MementosGenerators.java b/core/src/main/java/brooklyn/entity/rebind/dto/MementosGenerators.java
index 8ec638a..ca1c434 100644
--- a/core/src/main/java/brooklyn/entity/rebind/dto/MementosGenerators.java
+++ b/core/src/main/java/brooklyn/entity/rebind/dto/MementosGenerators.java
@@ -38,13 +38,13 @@ import brooklyn.entity.basic.EntityDynamicType;
 import brooklyn.entity.basic.EntityInternal;
 import brooklyn.entity.rebind.TreeUtils;
 import brooklyn.event.AttributeSensor;
+import brooklyn.event.AttributeSensor.SensorPersistenceMode;
 import brooklyn.event.feed.AbstractFeed;
 import brooklyn.location.Location;
 import brooklyn.location.basic.AbstractLocation;
 import brooklyn.location.basic.LocationInternal;
 import brooklyn.management.ManagementContext;
 import brooklyn.management.Task;
-import brooklyn.management.internal.NonDeploymentManagementContext;
 import brooklyn.mementos.BrooklynMemento;
 import brooklyn.mementos.CatalogItemMemento;
 import brooklyn.mementos.EnricherMemento;
@@ -173,8 +173,10 @@ public class MementosGenerators {
         Map<AttributeSensor, Object> allAttributes = entity.getAllAttributes();
         for (@SuppressWarnings("rawtypes") Map.Entry<AttributeSensor, Object> entry
: allAttributes.entrySet()) {
             AttributeSensor<?> key = checkNotNull(entry.getKey(), allAttributes);
-            Object value = entry.getValue();
-            builder.attributes.put((AttributeSensor<?>)key, value);
+            if (key.getPersistenceMode() != SensorPersistenceMode.NONE) {
+                Object value = entry.getValue();
+                builder.attributes.put((AttributeSensor<?>)key, value);
+            }
         }
         
         for (Location location : entity.getLocations()) {

http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/blob/327c227d/core/src/main/java/brooklyn/event/basic/BasicAttributeSensor.java
----------------------------------------------------------------------
diff --git a/core/src/main/java/brooklyn/event/basic/BasicAttributeSensor.java b/core/src/main/java/brooklyn/event/basic/BasicAttributeSensor.java
index 318b65b..6333ece 100644
--- a/core/src/main/java/brooklyn/event/basic/BasicAttributeSensor.java
+++ b/core/src/main/java/brooklyn/event/basic/BasicAttributeSensor.java
@@ -18,6 +18,8 @@
  */
 package brooklyn.event.basic;
 
+import static com.google.common.base.Preconditions.checkNotNull;
+
 import brooklyn.event.AttributeSensor;
 import brooklyn.event.Sensor;
 
@@ -28,13 +30,15 @@ import com.google.common.reflect.TypeToken;
  */
 public class BasicAttributeSensor<T> extends BasicSensor<T> implements AttributeSensor<T>
{
     private static final long serialVersionUID = -2493209215974820300L;
+    
+    private final SensorPersistenceMode persistence;
 
     public BasicAttributeSensor(Class<T> type, String name) {
         this(type, name, name);
     }
     
     public BasicAttributeSensor(Class<T> type, String name, String description) {
-        super(type, name, description);
+        this(TypeToken.of(type), name, description);
     }
     
     public BasicAttributeSensor(TypeToken<T> typeToken, String name) {
@@ -42,7 +46,17 @@ public class BasicAttributeSensor<T> extends BasicSensor<T>
implements Attribute
     }
     
     public BasicAttributeSensor(TypeToken<T> typeToken, String name, String description)
{
-        super(typeToken, name, description);
+        this(typeToken, name, description, SensorPersistenceMode.REQUIRED);
     }
     
+    public BasicAttributeSensor(TypeToken<T> typeToken, String name, String description,
SensorPersistenceMode persistence) {
+        super(typeToken, name, description);
+        this.persistence = checkNotNull(persistence, "persistence");
+    }
+
+    @Override
+    public SensorPersistenceMode getPersistenceMode() {
+        // persistence could be null if deserializing state written by an old version; in
which case default to 'required'
+        return (persistence != null) ? persistence : SensorPersistenceMode.REQUIRED;
+    }
 }

http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/blob/327c227d/core/src/main/java/brooklyn/event/basic/Sensors.java
----------------------------------------------------------------------
diff --git a/core/src/main/java/brooklyn/event/basic/Sensors.java b/core/src/main/java/brooklyn/event/basic/Sensors.java
index ac22e2c..033f6b4 100644
--- a/core/src/main/java/brooklyn/event/basic/Sensors.java
+++ b/core/src/main/java/brooklyn/event/basic/Sensors.java
@@ -18,6 +18,8 @@
  */
 package brooklyn.event.basic;
 
+import static com.google.common.base.Preconditions.checkNotNull;
+
 import java.net.InetAddress;
 import java.net.URI;
 import java.net.URL;
@@ -26,17 +28,58 @@ import javax.annotation.Nullable;
 
 import brooklyn.config.render.RendererHints;
 import brooklyn.event.AttributeSensor;
+import brooklyn.event.AttributeSensor.SensorPersistenceMode;
 import brooklyn.util.net.UserAndHostAndPort;
 import brooklyn.util.text.StringFunctions;
 import brooklyn.util.time.Duration;
 import brooklyn.util.time.Time;
 
+import com.google.common.annotations.Beta;
 import com.google.common.base.Function;
 import com.google.common.net.HostAndPort;
 import com.google.common.reflect.TypeToken;
 
 public class Sensors {
 
+    @Beta
+    public static <T> Builder<T> builder(TypeToken<T> type, String name)
{
+        return new Builder<T>().type(type).name(name);
+    }
+
+    @Beta
+    public static <T> Builder<T> builder(Class<T> type, String name) {
+        return new Builder<T>().type(type).name(name);
+    }
+    
+    @Beta
+    public static class Builder<T> {
+        private String name;
+        private TypeToken<T> type;
+        private String description;
+        private SensorPersistenceMode persistence;
+        
+        protected Builder() { // use builder(type, name) instead
+        }
+        public Builder<T> name(String val) {
+            this.name = checkNotNull(val, "name"); return this;
+        }
+        public Builder<T> type(Class<T> val) {
+            return type(TypeToken.of(val));
+        }
+        public Builder<T> type(TypeToken<T> val) {
+            this.type = checkNotNull(val, "type"); return this;
+        }
+        public Builder<T> description(String val) {
+            this.description = val; return this;
+        }
+        public Builder<T> persistence(SensorPersistenceMode val) {
+            this.persistence = val; return this;
+        }
+        public AttributeSensor<T> build() {
+            return new BasicAttributeSensor<T>(type, name, description, persistence);
+        }
+    }
+
     public static <T> AttributeSensor<T> newSensor(Class<T> type, String
name) {
         return new BasicAttributeSensor<T>(type, name);
     }

http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/blob/327c227d/core/src/test/java/brooklyn/entity/rebind/RebindEntityTest.java
----------------------------------------------------------------------
diff --git a/core/src/test/java/brooklyn/entity/rebind/RebindEntityTest.java b/core/src/test/java/brooklyn/entity/rebind/RebindEntityTest.java
index d6faccf..ab55346 100644
--- a/core/src/test/java/brooklyn/entity/rebind/RebindEntityTest.java
+++ b/core/src/test/java/brooklyn/entity/rebind/RebindEntityTest.java
@@ -33,6 +33,7 @@ import java.util.List;
 import java.util.Map;
 import java.util.concurrent.CopyOnWriteArrayList;
 import java.util.concurrent.CountDownLatch;
+import java.util.concurrent.Semaphore;
 
 import org.testng.Assert;
 import org.testng.annotations.Test;
@@ -52,6 +53,7 @@ import brooklyn.entity.proxying.ImplementedBy;
 import brooklyn.entity.trait.Resizable;
 import brooklyn.entity.trait.Startable;
 import brooklyn.event.AttributeSensor;
+import brooklyn.event.AttributeSensor.SensorPersistenceMode;
 import brooklyn.event.SensorEvent;
 import brooklyn.event.SensorEventListener;
 import brooklyn.event.basic.BasicAttributeSensor;
@@ -577,6 +579,25 @@ public class RebindEntityTest extends RebindTestFixtureWithApp {
     }
 
     @Test
+    public void testRebindDoesNotPersistTransientAttribute() throws Exception {
+        final String sensorName = "test.mydynamicsensor";
+        final AttributeSensor<Object> MY_DYNAMIC_SENSOR = Sensors.builder(Object.class,
sensorName)
+                .persistence(SensorPersistenceMode.NONE)
+                .build();
+        
+        // Anonymous inner class: we will not be able to rebind that.
+        @SuppressWarnings("serial")
+        Semaphore unrebindableObject = new Semaphore(1) {
+        };
+        
+        origApp.setAttribute(MY_DYNAMIC_SENSOR, unrebindableObject);
+        assertEquals(origApp.getAttribute(MY_DYNAMIC_SENSOR), unrebindableObject);
+
+        newApp = rebind();
+        assertNull(newApp.getAttribute(MY_DYNAMIC_SENSOR));
+    }
+
+    @Test
     public void testRebindWhenPreviousAppDestroyedHasNoApp() throws Exception {
         origApp.stop();
 


Mime
View raw message