Return-Path: X-Original-To: archive-asf-public-internal@cust-asf2.ponee.io Delivered-To: archive-asf-public-internal@cust-asf2.ponee.io Received: from cust-asf.ponee.io (cust-asf.ponee.io [163.172.22.183]) by cust-asf2.ponee.io (Postfix) with ESMTP id 9A91B200C53 for ; Tue, 11 Apr 2017 16:13:12 +0200 (CEST) Received: by cust-asf.ponee.io (Postfix) id 9922A160B89; Tue, 11 Apr 2017 14:13:12 +0000 (UTC) Delivered-To: archive-asf-public@cust-asf.ponee.io Received: from mail.apache.org (hermes.apache.org [140.211.11.3]) by cust-asf.ponee.io (Postfix) with SMTP id 5FDFB160BAE for ; Tue, 11 Apr 2017 16:13:11 +0200 (CEST) Received: (qmail 92429 invoked by uid 500); 11 Apr 2017 14:13:10 -0000 Mailing-List: contact commits-help@brooklyn.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: dev@brooklyn.apache.org Delivered-To: mailing list commits@brooklyn.apache.org Received: (qmail 92299 invoked by uid 99); 11 Apr 2017 14:13:08 -0000 Received: from git1-us-west.apache.org (HELO git1-us-west.apache.org) (140.211.11.23) by apache.org (qpsmtpd/0.29) with ESMTP; Tue, 11 Apr 2017 14:13:08 +0000 Received: by git1-us-west.apache.org (ASF Mail Server at git1-us-west.apache.org, from userid 33) id A895FE02B4; Tue, 11 Apr 2017 14:13:08 +0000 (UTC) Content-Type: text/plain; charset="us-ascii" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit From: drigodwin@apache.org To: commits@brooklyn.apache.org Date: Tue, 11 Apr 2017 14:13:13 -0000 Message-Id: <9c52e4ec8abf4718ac5ed20d998b4dfb@git.apache.org> In-Reply-To: <413ccc4af65141e898856fd552b65ba6@git.apache.org> References: <413ccc4af65141e898856fd552b65ba6@git.apache.org> X-Mailer: ASF-Git Admin Mailer Subject: [6/8] brooklyn-server git commit: Use AtomicIntegers for internal sequencer state archived-at: Tue, 11 Apr 2017 14:13:12 -0000 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 Authored: Wed Feb 8 19:47:11 2017 +0000 Committer: Andrew Donald Kennedy 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 SEQUENCE_NEXT = Sensors.builder(Integer.class, "sequence.next") - .description("The next value of the sequence") + AttributeSensor SEQUENCE_STATE = Sensors.builder(AtomicInteger.class, "sequence.state") + .description("The current state of the sequence") .build(); AttributeSensor> SEQUENCE_CACHE = Sensors.builder(new TypeToken>() { }, "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.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 valueSensor = config().get(SEQUENCE_VALUE_SENSOR); AttributeSensor 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 { AttributeSensor SEQUENCE_VALUE = SequenceGroup.SEQUENCE_VALUE; + AttributeSensor SEQUENCE_STATE = SequenceGroup.SEQUENCE_STATE; + @SetFromFlag("sequenceStart") ConfigKey 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));