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:13 UTC

[6/8] brooklyn-server git commit: Use AtomicIntegers for internal sequencer state

Use AtomicIntegers for internal sequencer state


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

Branch: refs/heads/master
Commit: ff6e7a376a135ffd2e8e672d25bacefdb662b594
Parents: 68a6f14
Author: Andrew Donald Kennedy <an...@cloudsoftcorp.com>
Authored: Wed Feb 8 19:47:11 2017 +0000
Committer: Andrew Donald Kennedy <an...@cloudsoftcorp.com>
Committed: Tue Apr 11 14:34:27 2017 +0100

----------------------------------------------------------------------
 .../brooklyn/entity/group/SequenceGroup.java    |  5 +++--
 .../entity/group/SequenceGroupImpl.java         | 16 ++++++++--------
 .../brooklyn/entity/stock/SequenceEntity.java   |  4 ++++
 .../entity/stock/SequenceEntityImpl.java        | 20 ++++++++++++--------
 .../entity/group/SequenceGroupTest.java         | 19 +++++++++++++------
 .../entity/stock/SequenceEntityTest.java        |  2 +-
 6 files changed, 41 insertions(+), 25 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/brooklyn-server/blob/ff6e7a37/core/src/main/java/org/apache/brooklyn/entity/group/SequenceGroup.java
----------------------------------------------------------------------
diff --git a/core/src/main/java/org/apache/brooklyn/entity/group/SequenceGroup.java b/core/src/main/java/org/apache/brooklyn/entity/group/SequenceGroup.java
index 4d7d082..170e90d 100644
--- a/core/src/main/java/org/apache/brooklyn/entity/group/SequenceGroup.java
+++ b/core/src/main/java/org/apache/brooklyn/entity/group/SequenceGroup.java
@@ -19,6 +19,7 @@
 package org.apache.brooklyn.entity.group;
 
 import java.util.Map;
+import java.util.concurrent.atomic.AtomicInteger;
 
 import org.apache.brooklyn.api.entity.Entity;
 import org.apache.brooklyn.api.entity.ImplementedBy;
@@ -123,8 +124,8 @@ public interface SequenceGroup extends DynamicGroup {
             .description("The current entity in the sequence")
             .build();
 
-    AttributeSensor<Integer> SEQUENCE_NEXT = Sensors.builder(Integer.class, "sequence.next")
-            .description("The next value of the sequence")
+    AttributeSensor<AtomicInteger> SEQUENCE_STATE = Sensors.builder(AtomicInteger.class, "sequence.state")
+            .description("The current state of the sequence")
             .build();
 
     AttributeSensor<Map<String, Integer>> SEQUENCE_CACHE = Sensors.builder(new TypeToken<Map<String, Integer>>() { }, "sequence.cache")

http://git-wip-us.apache.org/repos/asf/brooklyn-server/blob/ff6e7a37/core/src/main/java/org/apache/brooklyn/entity/group/SequenceGroupImpl.java
----------------------------------------------------------------------
diff --git a/core/src/main/java/org/apache/brooklyn/entity/group/SequenceGroupImpl.java b/core/src/main/java/org/apache/brooklyn/entity/group/SequenceGroupImpl.java
index 0e8c39c..6aca7af 100644
--- a/core/src/main/java/org/apache/brooklyn/entity/group/SequenceGroupImpl.java
+++ b/core/src/main/java/org/apache/brooklyn/entity/group/SequenceGroupImpl.java
@@ -19,6 +19,7 @@
 package org.apache.brooklyn.entity.group;
 
 import java.util.Map;
+import java.util.concurrent.atomic.AtomicInteger;
 
 import org.apache.brooklyn.api.entity.Entity;
 import org.apache.brooklyn.api.sensor.AttributeSensor;
@@ -44,7 +45,8 @@ public class SequenceGroupImpl extends DynamicGroupImpl implements SequenceGroup
             sensors().set(SEQUENCE_CACHE, Maps.<String, Integer>newConcurrentMap());
             sensors().set(SEQUENCE_CURRENT, null);
             Integer initial = config().get(SEQUENCE_START);
-            sensors().set(SEQUENCE_NEXT, initial);
+            AtomicInteger state = new AtomicInteger(initial);
+            sensors().set(SEQUENCE_STATE, state);
             return null;
         }
     }
@@ -86,7 +88,10 @@ public class SequenceGroupImpl extends DynamicGroupImpl implements SequenceGroup
 
     private Integer sequence(Entity entity) {
         String format = config().get(SEQUENCE_FORMAT);
-        Integer current = sensors().get(SEQUENCE_NEXT);
+        Integer increment = config().get(SEQUENCE_INCREMENT);
+        AtomicInteger state = sensors().get(SEQUENCE_STATE);
+        Integer current = state.getAndAdd(increment);
+
         String string = String.format(format, current);
         AttributeSensor<Integer> valueSensor = config().get(SEQUENCE_VALUE_SENSOR);
         AttributeSensor<String> stringSensor = config().get(SEQUENCE_STRING_SENSOR);
@@ -96,12 +101,7 @@ public class SequenceGroupImpl extends DynamicGroupImpl implements SequenceGroup
         LOG.debug("Sequence on {} set to to {}", entity, current);
 
         sensors().set(SEQUENCE_CURRENT, entity);
-
-        Integer increment = config().get(SEQUENCE_INCREMENT);
-        Integer next = current + increment;
-        LOG.debug("Sequence for {} incremented to {}", this, next);
-
-        sensors().set(SEQUENCE_NEXT, next);
+        LOG.debug("Sequence for {} incremented to {}", this, state.get());
 
         return current;
     }

http://git-wip-us.apache.org/repos/asf/brooklyn-server/blob/ff6e7a37/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 6b73d8f..bc58f47 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
@@ -18,6 +18,8 @@
  */
 package org.apache.brooklyn.entity.stock;
 
+import java.util.concurrent.atomic.AtomicInteger;
+
 import org.apache.brooklyn.api.entity.Entity;
 import org.apache.brooklyn.api.entity.ImplementedBy;
 import org.apache.brooklyn.api.sensor.AttributeSensor;
@@ -47,6 +49,8 @@ public interface SequenceEntity extends Entity, Startable, Supplier<Integer> {
 
     AttributeSensor<Integer> SEQUENCE_VALUE = SequenceGroup.SEQUENCE_VALUE;
 
+    AttributeSensor<AtomicInteger> SEQUENCE_STATE = SequenceGroup.SEQUENCE_STATE;
+
     @SetFromFlag("sequenceStart")
     ConfigKey<Integer> SEQUENCE_START = SequenceGroup.SEQUENCE_START;
 

http://git-wip-us.apache.org/repos/asf/brooklyn-server/blob/ff6e7a37/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 b731dfe..06fee53 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
@@ -19,6 +19,7 @@
 package org.apache.brooklyn.entity.stock;
 
 import java.util.Collection;
+import java.util.concurrent.atomic.AtomicInteger;
 
 import org.apache.brooklyn.api.location.Location;
 import org.apache.brooklyn.core.entity.AbstractEntity;
@@ -61,7 +62,7 @@ public class SequenceEntityImpl extends AbstractEntity implements SequenceEntity
     @Override
     public Integer incrementAndGet() {
         synchronized (mutex) {
-            increment();
+            sequence();
             return get();
         }
     }
@@ -69,9 +70,7 @@ public class SequenceEntityImpl extends AbstractEntity implements SequenceEntity
     @Override
     public Void increment() {
         synchronized (mutex) {
-            Integer increment = config().get(SEQUENCE_INCREMENT);
-            Integer current = get();
-            sequence(current + increment);
+            sequence();
             return null;
         }
     }
@@ -80,14 +79,19 @@ public class SequenceEntityImpl extends AbstractEntity implements SequenceEntity
     public Void reset() {
         synchronized (mutex) {
             Integer start = config().get(SEQUENCE_START);
-            sequence(start);
+            AtomicInteger state =  new AtomicInteger(start);
+            sensors().set(SEQUENCE_STATE, state);
+            sensors().set(SEQUENCE_VALUE, state.get());
             return null;
         }
     }
 
-    private void sequence(Integer value) {
-        sensors().set(SEQUENCE_VALUE, value);
-        LOG.debug("Sequence for {} set to {}", this, value);
+    private void sequence() {
+        Integer increment = config().get(SEQUENCE_INCREMENT);
+        AtomicInteger state = sensors().get(SEQUENCE_STATE);
+        Integer current = state.addAndGet(increment);
+        sensors().set(SEQUENCE_VALUE, current);
+        LOG.debug("Sequence for {} set to {}", this, current);
     }
 
 }

http://git-wip-us.apache.org/repos/asf/brooklyn-server/blob/ff6e7a37/core/src/test/java/org/apache/brooklyn/entity/group/SequenceGroupTest.java
----------------------------------------------------------------------
diff --git a/core/src/test/java/org/apache/brooklyn/entity/group/SequenceGroupTest.java b/core/src/test/java/org/apache/brooklyn/entity/group/SequenceGroupTest.java
index d835c7d..f1d85c5 100644
--- a/core/src/test/java/org/apache/brooklyn/entity/group/SequenceGroupTest.java
+++ b/core/src/test/java/org/apache/brooklyn/entity/group/SequenceGroupTest.java
@@ -22,9 +22,11 @@ import static org.apache.brooklyn.core.entity.EntityAsserts.assertAttribute;
 import static org.apache.brooklyn.core.entity.EntityAsserts.assertAttributeEquals;
 import static org.apache.brooklyn.core.entity.EntityAsserts.assertAttributeEqualsEventually;
 import static org.apache.brooklyn.test.Asserts.assertEqualsIgnoringOrder;
-import static org.apache.brooklyn.test.Asserts.assertTrue;
+import static org.apache.brooklyn.test.Asserts.*;
 import static org.apache.brooklyn.test.Asserts.succeedsEventually;
 
+import java.util.concurrent.atomic.AtomicInteger;
+
 import org.apache.brooklyn.api.entity.Entity;
 import org.apache.brooklyn.api.entity.EntitySpec;
 import org.apache.brooklyn.api.location.LocationSpec;
@@ -83,7 +85,8 @@ public class SequenceGroupTest extends BrooklynAppUnitTestSupport {
 
         assertEqualsIgnoringOrder(group.getMembers(), ImmutableList.of(e1));
         assertAttributeEquals(e1, SequenceGroup.SEQUENCE_VALUE, 1);
-        assertAttributeEquals(group, SequenceGroup.SEQUENCE_NEXT, 2);
+        AtomicInteger state = group.sensors().get(SequenceGroup.SEQUENCE_STATE);
+        assertEquals(state.get(), 2);
     }
 
     @Test
@@ -143,8 +146,9 @@ public class SequenceGroupTest extends BrooklynAppUnitTestSupport {
         assertAttributeEquals(e1, SequenceGroup.SEQUENCE_VALUE, 3);
         assertAttributeEquals(e2, SequenceGroup.SEQUENCE_VALUE, 4);
         assertAttributeEquals(e3, SequenceGroup.SEQUENCE_VALUE, 5);
-        assertAttributeEquals(group, SequenceGroup.SEQUENCE_NEXT, 6);
         assertAttributeEquals(group, SequenceGroup.SEQUENCE_CURRENT, e3);
+        AtomicInteger state = group.sensors().get(SequenceGroup.SEQUENCE_STATE);
+        assertEquals(state.get(), 6);
     }
 
     @Test
@@ -162,7 +166,8 @@ public class SequenceGroupTest extends BrooklynAppUnitTestSupport {
             public void run() {
                 assertEqualsIgnoringOrder(group.getMembers(), ImmutableSet.of(e));
                 assertAttributeEquals(e, SequenceGroup.SEQUENCE_VALUE, 1);
-                assertAttributeEquals(group, SequenceGroup.SEQUENCE_NEXT, 2);
+                AtomicInteger state = group.sensors().get(SequenceGroup.SEQUENCE_STATE);
+                assertEquals(state.get(), 2);
             }});
     }
 
@@ -179,8 +184,9 @@ public class SequenceGroupTest extends BrooklynAppUnitTestSupport {
         assertAttributeEquals(e1, SequenceGroup.SEQUENCE_VALUE, 1);
         assertAttributeEquals(e2, SequenceGroup.SEQUENCE_VALUE, 2);
         assertAttributeEquals(e3, SequenceGroup.SEQUENCE_VALUE, 3);
-        assertAttributeEquals(group, SequenceGroup.SEQUENCE_NEXT, 4);
         assertAttributeEquals(group, SequenceGroup.SEQUENCE_CURRENT, e3);
+        AtomicInteger state = group.sensors().get(SequenceGroup.SEQUENCE_STATE);
+        assertEquals(state.get(), 4);
 
         final Entity e = app.addChild(EntitySpec.create(TestEntity.class));
 
@@ -188,8 +194,9 @@ public class SequenceGroupTest extends BrooklynAppUnitTestSupport {
             public void run() {
                 assertEqualsIgnoringOrder(group.getMembers(), ImmutableSet.of(e1, e2, e3, e));
                 assertAttributeEquals(e, SequenceGroup.SEQUENCE_VALUE, 4);
-                assertAttributeEquals(group, SequenceGroup.SEQUENCE_NEXT, 5);
                 assertAttributeEquals(group, SequenceGroup.SEQUENCE_CURRENT, e);
+                AtomicInteger state = group.sensors().get(SequenceGroup.SEQUENCE_STATE);
+                assertEquals(state.get(), 5);
             }});
     }
 }

http://git-wip-us.apache.org/repos/asf/brooklyn-server/blob/ff6e7a37/core/src/test/java/org/apache/brooklyn/entity/stock/SequenceEntityTest.java
----------------------------------------------------------------------
diff --git a/core/src/test/java/org/apache/brooklyn/entity/stock/SequenceEntityTest.java b/core/src/test/java/org/apache/brooklyn/entity/stock/SequenceEntityTest.java
index c4b3b86..0acafaf 100644
--- a/core/src/test/java/org/apache/brooklyn/entity/stock/SequenceEntityTest.java
+++ b/core/src/test/java/org/apache/brooklyn/entity/stock/SequenceEntityTest.java
@@ -113,7 +113,7 @@ public class SequenceEntityTest extends BrooklynAppUnitTestSupport {
     }
 
     @Test
-    public void testSequenceNextEffectors() throws Exception {
+    public void testSequenceIncrementAndGetEffector() throws Exception {
         sequence = app.addChild(EntitySpec.create(SequenceEntity.class));
         app.start(ImmutableList.of(loc1));