You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@brooklyn.apache.org by dr...@apache.org on 2017/04/11 14:13:12 UTC

[5/8] brooklyn-server git commit: Use configuration definitions from SequenceGroup and add local mutex

Use configuration definitions from SequenceGroup and add local mutex


Project: http://git-wip-us.apache.org/repos/asf/brooklyn-server/repo
Commit: http://git-wip-us.apache.org/repos/asf/brooklyn-server/commit/77d0ddb7
Tree: http://git-wip-us.apache.org/repos/asf/brooklyn-server/tree/77d0ddb7
Diff: http://git-wip-us.apache.org/repos/asf/brooklyn-server/diff/77d0ddb7

Branch: refs/heads/master
Commit: 77d0ddb7336e60dcf9ed9b1f3085c422c6811a3f
Parents: a5aff68
Author: Andrew Donald Kennedy <an...@cloudsoftcorp.com>
Authored: Tue Feb 7 09:44:37 2017 +0000
Committer: Andrew Donald Kennedy <an...@cloudsoftcorp.com>
Committed: Tue Apr 11 14:34:26 2017 +0100

----------------------------------------------------------------------
 .../brooklyn/entity/stock/SequenceEntity.java   | 44 +++---------
 .../entity/stock/SequenceEntityImpl.java        | 74 ++++++++++++--------
 2 files changed, 52 insertions(+), 66 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/brooklyn-server/blob/77d0ddb7/core/src/main/java/org/apache/brooklyn/entity/stock/SequenceEntity.java
----------------------------------------------------------------------
diff --git a/core/src/main/java/org/apache/brooklyn/entity/stock/SequenceEntity.java b/core/src/main/java/org/apache/brooklyn/entity/stock/SequenceEntity.java
index 5135f3c..f53ba65 100644
--- a/core/src/main/java/org/apache/brooklyn/entity/stock/SequenceEntity.java
+++ b/core/src/main/java/org/apache/brooklyn/entity/stock/SequenceEntity.java
@@ -23,15 +23,10 @@ import org.apache.brooklyn.api.entity.ImplementedBy;
 import org.apache.brooklyn.api.sensor.AttributeSensor;
 import org.apache.brooklyn.config.ConfigKey;
 import org.apache.brooklyn.core.annotation.Effector;
-import org.apache.brooklyn.core.config.ConfigKeys;
 import org.apache.brooklyn.core.effector.MethodEffector;
 import org.apache.brooklyn.core.entity.trait.Startable;
-import org.apache.brooklyn.core.sensor.AttributeSensorAndConfigKey;
-import org.apache.brooklyn.core.sensor.Sensors;
+import org.apache.brooklyn.entity.group.SequenceGroup;
 import org.apache.brooklyn.util.core.flags.SetFromFlag;
-import org.apache.brooklyn.util.text.StringPredicates;
-
-import com.google.common.base.Predicates;
 
 /**
  * An entity that supplies a sequence of values through an effector.
@@ -44,46 +39,23 @@ import com.google.common.base.Predicates;
  *     sequence.start: 0
  *     sequence.increment: 1
  *     sequence.format: "global-%03d"
- *     sequence.name: "global"
  * }</pre>
  */
 @ImplementedBy(SequenceEntityImpl.class)
 public interface SequenceEntity extends Entity, Startable {
 
+    AttributeSensor<Integer> SEQUENCE_VALUE = SequenceGroup.SEQUENCE_VALUE;
+
+    AttributeSensor<String> SEQUENCE_STRING = SequenceGroup.SEQUENCE_STRING;
+
     @SetFromFlag("sequenceStart")
-    ConfigKey<Integer> SEQUENCE_START = ConfigKeys.builder(Integer.class)
-            .name("sequence.start")
-            .description("The starting point of the sequence")
-            .defaultValue(1)
-            .constraint(Predicates.<Integer>notNull())
-            .build();
+    ConfigKey<Integer> SEQUENCE_START = SequenceGroup.SEQUENCE_START;
 
     @SetFromFlag("sequenceIncrement")
-    ConfigKey<Integer> SEQUENCE_INCREMENT =  ConfigKeys.builder(Integer.class)
-            .name("sequence.increment")
-            .description("The sequence increment for the next value")
-            .defaultValue(1)
-            .constraint(Predicates.<Integer>notNull())
-            .build();
+    ConfigKey<Integer> SEQUENCE_INCREMENT = SequenceGroup.SEQUENCE_INCREMENT;
 
     @SetFromFlag("sequenceFormat")
-    ConfigKey<String> SEQUENCE_FORMAT = ConfigKeys.builder(String.class)
-            .name("sequence.format")
-            .description("A format used to generate a string representation of the sequence")
-            .defaultValue("%d")
-            .constraint(StringPredicates.containsRegex("%[-#+ 0,(]*[0-9]*[doxX]"))
-            .build();
-
-    @SetFromFlag("sequenceName")
-    AttributeSensorAndConfigKey<String, String> SEQUENCE_NAME = ConfigKeys.newStringSensorAndConfigKey("sequence.name", "The name of the sequence", "sequence");
-
-    AttributeSensor<Integer> SEQUENCE_VALUE = Sensors.builder(Integer.class, "sequence.value")
-            .description("The current value of the sequence")
-            .build();
-
-    AttributeSensor<String> SEQUENCE_STRING = Sensors.builder(String.class, "sequence.string")
-            .description("The current value of the sequence formatted as a string")
-            .build();
+    ConfigKey<String> SEQUENCE_FORMAT = SequenceGroup.SEQUENCE_FORMAT;
 
     MethodEffector<Void> RESET = new MethodEffector<Void>(SequenceEntity.class, "reset");
     MethodEffector<Void> INCREMENT = new MethodEffector<Void>(SequenceEntity.class, "increment");

http://git-wip-us.apache.org/repos/asf/brooklyn-server/blob/77d0ddb7/core/src/main/java/org/apache/brooklyn/entity/stock/SequenceEntityImpl.java
----------------------------------------------------------------------
diff --git a/core/src/main/java/org/apache/brooklyn/entity/stock/SequenceEntityImpl.java b/core/src/main/java/org/apache/brooklyn/entity/stock/SequenceEntityImpl.java
index 338b323..0ddb134 100644
--- a/core/src/main/java/org/apache/brooklyn/entity/stock/SequenceEntityImpl.java
+++ b/core/src/main/java/org/apache/brooklyn/entity/stock/SequenceEntityImpl.java
@@ -22,17 +22,16 @@ import java.util.Collection;
 
 import org.apache.brooklyn.api.location.Location;
 import org.apache.brooklyn.core.entity.AbstractEntity;
-import org.apache.brooklyn.core.feed.ConfigToAttributes;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
 
 public class SequenceEntityImpl extends AbstractEntity implements SequenceEntity {
 
-    public SequenceEntityImpl() { }
+    private static final Logger LOG = LoggerFactory.getLogger(SequenceEntity.class);
 
-    public void init() {
-        super.init();
+    private Object mutex = new Object();
 
-        ConfigToAttributes.apply(this, SEQUENCE_NAME);
-    }
+    public SequenceEntityImpl() { }
 
     @Override
     public void start(Collection<? extends Location> locations) {
@@ -52,48 +51,63 @@ public class SequenceEntityImpl extends AbstractEntity implements SequenceEntity
         start(getLocations());
     }
 
-    protected void sequence(Integer value) {
-        String format = config().get(SEQUENCE_FORMAT);
-
-        sensors().set(SEQUENCE_VALUE, value);
-        sensors().set(SEQUENCE_STRING, String.format(format, value));
-    }
-
     @Override
     public Integer currentValue() {
-        return sensors().get(SEQUENCE_VALUE);
+        synchronized (mutex) {
+            return sensors().get(SEQUENCE_VALUE);
+        }
     }
 
     @Override
     public String currentString() {
-        return sensors().get(SEQUENCE_STRING);
+        synchronized (mutex) {
+            return sensors().get(SEQUENCE_STRING);
+        }
     }
 
     @Override
-    public synchronized Integer nextValue() {
-        increment();
-        return currentValue();
+    public Integer nextValue() {
+        synchronized (mutex) {
+            increment();
+            return currentValue();
+        }
     }
 
     @Override
-    public synchronized String nextString() {
-        increment();
-        return currentString();
+    public String nextString() {
+        synchronized (mutex) {
+            increment();
+            return currentString();
+        }
     }
 
     @Override
-    public synchronized Void increment() {
-        Integer increment = config().get(SEQUENCE_INCREMENT);
-        Integer current = currentValue();
-        sequence(current + increment);
-        return null;
+    public Void increment() {
+        synchronized (mutex) {
+            Integer increment = config().get(SEQUENCE_INCREMENT);
+            Integer current = currentValue();
+            sequence(current + increment);
+            return null;
+        }
     }
 
     @Override
-    public synchronized Void reset() {
-        Integer start = config().get(SEQUENCE_START);
-        sequence(start);
-        return null;
+    public Void reset() {
+        synchronized (mutex) {
+            Integer start = config().get(SEQUENCE_START);
+            sequence(start);
+            return null;
+        }
+    }
+
+    private void sequence(Integer value) {
+        String format = config().get(SEQUENCE_FORMAT);
+        String string = String.format(format, value);
+
+        sensors().set(SEQUENCE_VALUE, value);
+        sensors().set(SEQUENCE_STRING, string);
+
+        LOG.debug("Sequence for {} set to {}", this, value);
     }
 
 }