brooklyn-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From aleds...@apache.org
Subject [2/8] git commit: Fix concurrency issue stopping cluster
Date Sun, 07 Sep 2014 21:28:51 GMT
Fix concurrency issue stopping cluster

- when resizing to zero, we shrink by currentSize. If someone
  concurrently stops an entity, then don’t worry that we were only
  able to stop (currentSize-1) entities. Just continue.


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

Branch: refs/heads/master
Commit: 13b7304f836bc2c4b69b28940a6e7d94290b0fba
Parents: 94bc892
Author: Aled Sage <aled.sage@gmail.com>
Authored: Fri Sep 5 19:22:32 2014 +0100
Committer: Aled Sage <aled.sage@gmail.com>
Committed: Fri Sep 5 21:52:50 2014 +0100

----------------------------------------------------------------------
 .../entity/group/DynamicClusterImpl.java        | 22 +++++++++++---------
 1 file changed, 12 insertions(+), 10 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/blob/13b7304f/core/src/main/java/brooklyn/entity/group/DynamicClusterImpl.java
----------------------------------------------------------------------
diff --git a/core/src/main/java/brooklyn/entity/group/DynamicClusterImpl.java b/core/src/main/java/brooklyn/entity/group/DynamicClusterImpl.java
index be7724d..3c1bc82 100644
--- a/core/src/main/java/brooklyn/entity/group/DynamicClusterImpl.java
+++ b/core/src/main/java/brooklyn/entity/group/DynamicClusterImpl.java
@@ -786,7 +786,8 @@ public class DynamicClusterImpl extends AbstractGroupImpl implements DynamicClus
             return Lists.newArrayList();
         
         if (delta == 1 && !isAvailabilityZoneEnabled()) {
-            return ImmutableList.of(pickAndRemoveMember()); // for backwards compatibility
in sub-classes
+            Maybe<Entity> member = tryPickAndRemoveMember();
+            return (member.isPresent()) ? ImmutableList.of(member.get()) : ImmutableList.<Entity>of();
         }
 
         // TODO inefficient impl
@@ -807,28 +808,29 @@ public class DynamicClusterImpl extends AbstractGroupImpl implements
DynamicClus
         } else {
             List<Entity> entities = Lists.newArrayList();
             for (int i = 0; i < delta; i++) {
-                entities.add(pickAndRemoveMember());
+                // don't assume we have enough members; e.g. if shrinking to zero and someone
else concurrently stops a member,
+                // then just return what we were able to remove.
+                Maybe<Entity> member = tryPickAndRemoveMember();
+                if (member.isPresent()) entities.add(member.get());
             }
             return entities;
         }
     }
 
-    /**
-     * @deprecated since 0.6.0; subclasses should instead override {@link #pickAndRemoveMembers(int)}
if they really need to!
-     */
-    protected Entity pickAndRemoveMember() {
+    private Maybe<Entity> tryPickAndRemoveMember() {
         assert !isAvailabilityZoneEnabled() : "should instead call pickAndRemoveMembers(int)
if using availability zones";
 
         // TODO inefficient impl
-        Preconditions.checkState(getMembers().size() > 0, "Attempt to remove a node when
members is empty, from cluster "+this);
-        if (LOG.isDebugEnabled()) LOG.debug("Removing a node from {}", this);
+        Collection<Entity> members = getMembers();
+        if (members.isEmpty()) return Maybe.absent();
 
-        Entity entity = getRemovalStrategy().apply(getMembers());
+        if (LOG.isDebugEnabled()) LOG.debug("Removing a node from {}", this);
+        Entity entity = getRemovalStrategy().apply(members);
         Preconditions.checkNotNull(entity, "No entity chosen for removal from "+getId());
         Preconditions.checkState(entity instanceof Startable, "Chosen entity for removal
not stoppable: cluster="+this+"; choice="+entity);
 
         removeMember(entity);
-        return entity;
+        return Maybe.of(entity);
     }
 
     protected void discardNode(Entity entity) {


Mime
View raw message