brooklyn-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From henev...@apache.org
Subject [01/11] git commit: Manually add existing machines to server pools
Date Fri, 25 Jul 2014 17:56:57 GMT
Repository: incubator-brooklyn
Updated Branches:
  refs/heads/master 2a4de4d72 -> 35f5ff2db


Manually add existing machines to server pools


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

Branch: refs/heads/master
Commit: 01f70a796d0deaa9d3d2ff68c526912573b9aaa7
Parents: fb4c4dc
Author: Sam Corbett <sam.corbett@cloudsoftcorp.com>
Authored: Fri Jul 18 16:47:37 2014 +0100
Committer: Sam Corbett <sam.corbett@cloudsoftcorp.com>
Committed: Mon Jul 21 10:18:00 2014 +0100

----------------------------------------------------------------------
 .../entity/group/DynamicClusterImpl.java        |  2 +-
 .../java/brooklyn/entity/pool/ServerPool.java   | 25 ++++++++++
 .../brooklyn/entity/pool/ServerPoolImpl.java    | 50 +++++++++++++++++++-
 .../brooklyn/entity/pool/ServerPoolTest.java    | 30 ++++++++++--
 4 files changed, 101 insertions(+), 6 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/blob/01f70a79/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 a80c092..410b01e 100644
--- a/core/src/main/java/brooklyn/entity/group/DynamicClusterImpl.java
+++ b/core/src/main/java/brooklyn/entity/group/DynamicClusterImpl.java
@@ -574,7 +574,7 @@ public class DynamicClusterImpl extends AbstractGroupImpl implements DynamicClus
             chosenLocations = Collections.nCopies(delta, getLocation());
         }
 
-        // create the entities and start themo
+        // create and start the entities
         return addInEachLocation(chosenLocations, ImmutableMap.of());
     }
 

http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/blob/01f70a79/software/base/src/main/java/brooklyn/entity/pool/ServerPool.java
----------------------------------------------------------------------
diff --git a/software/base/src/main/java/brooklyn/entity/pool/ServerPool.java b/software/base/src/main/java/brooklyn/entity/pool/ServerPool.java
index f61c49f..4212aab 100644
--- a/software/base/src/main/java/brooklyn/entity/pool/ServerPool.java
+++ b/software/base/src/main/java/brooklyn/entity/pool/ServerPool.java
@@ -18,10 +18,15 @@
  */
 package brooklyn.entity.pool;
 
+import java.util.Collection;
 import java.util.Map;
 
 import brooklyn.config.ConfigKey;
+import brooklyn.entity.Entity;
+import brooklyn.entity.annotation.Effector;
+import brooklyn.entity.annotation.EffectorParam;
 import brooklyn.entity.basic.ConfigKeys;
+import brooklyn.entity.basic.MethodEffector;
 import brooklyn.entity.machine.MachineEntity;
 import brooklyn.entity.group.DynamicCluster;
 import brooklyn.entity.proxying.EntitySpec;
@@ -71,8 +76,28 @@ public interface ServerPool extends DynamicCluster, LocationOwner<ServerPoolLoca
     ConfigKey<EntitySpec<?>> MEMBER_SPEC = ConfigKeys.newConfigKeyWithDefault(DynamicCluster.MEMBER_SPEC,
             EntitySpec.create(MachineEntity.class));
 
+    MethodEffector<Collection<Entity>> ADD_MACHINES_FROM_SPEC = new MethodEffector<Collection<Entity>>(ServerPool.class,
"addExistingMachinesFromSpec");
+
     public MachineLocation claimMachine(Map<?, ?> flags) throws NoMachinesAvailableException;
 
     public void releaseMachine(MachineLocation machine);
 
+    /**
+     * Sets the pool to use an existing {@link MachineLocation} as a member. Existing locations
+     * will count towards the capacity of the pool but will not be terminated when the pool
is
+     * stopped.
+     * @param machine An existing machine.
+     * @return the new member of the pool, created with the configured {@link #MEMBER_SPEC}.
+     */
+    public Entity addExistingMachine(MachineLocation machine);
+
+    /**
+     * Adds additional machines to the pool by resolving the given spec.
+     * @param spec
+     *          A location spec, e.g. <code>byon:(hosts="user@10.9.1.1,user@10.9.1.2,user@10.9.1.3")</code>
+     * @return the new members of the pool, created with the configured {@link #MEMBER_SPEC}.
+     */
+    @Effector(description = "Adds additional machines to the pool by resolving the given
spec.")
+    public Collection<Entity> addExistingMachinesFromSpec(
+            @EffectorParam(name = "spec", description = "Spec") String spec);
 }

http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/blob/01f70a79/software/base/src/main/java/brooklyn/entity/pool/ServerPoolImpl.java
----------------------------------------------------------------------
diff --git a/software/base/src/main/java/brooklyn/entity/pool/ServerPoolImpl.java b/software/base/src/main/java/brooklyn/entity/pool/ServerPoolImpl.java
index 5ca8646..1ef26e3 100644
--- a/software/base/src/main/java/brooklyn/entity/pool/ServerPoolImpl.java
+++ b/software/base/src/main/java/brooklyn/entity/pool/ServerPoolImpl.java
@@ -19,18 +19,23 @@
 package brooklyn.entity.pool;
 
 import java.util.Collection;
+import java.util.List;
 import java.util.Map;
 
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
+import brooklyn.config.ConfigKey;
 import brooklyn.entity.Entity;
 import brooklyn.entity.basic.Attributes;
+import brooklyn.entity.basic.ConfigKeys;
 import brooklyn.entity.basic.EntityInternal;
 import brooklyn.entity.basic.EntityPredicates;
 import brooklyn.entity.basic.Lifecycle;
+import brooklyn.entity.effector.Effectors;
 import brooklyn.entity.group.AbstractMembershipTrackingPolicy;
 import brooklyn.entity.group.DynamicClusterImpl;
+import brooklyn.entity.trait.Startable;
 import brooklyn.event.AttributeSensor;
 import brooklyn.event.basic.Sensors;
 import brooklyn.location.Location;
@@ -41,14 +46,20 @@ import brooklyn.location.basic.BasicLocationDefinition;
 import brooklyn.location.basic.Machines;
 import brooklyn.location.dynamic.DynamicLocation;
 import brooklyn.management.LocationManager;
+import brooklyn.management.Task;
 import brooklyn.policy.PolicySpec;
 import brooklyn.util.collections.MutableMap;
 import brooklyn.util.guava.Maybe;
+import brooklyn.util.task.DynamicTasks;
 
 import com.google.common.base.Function;
 import com.google.common.base.Joiner;
 import com.google.common.base.Optional;
+import com.google.common.collect.FluentIterable;
+import com.google.common.collect.ImmutableList;
+import com.google.common.collect.ImmutableMap;
 import com.google.common.collect.Iterables;
+import com.google.common.collect.Lists;
 import com.google.common.collect.Maps;
 import com.google.common.reflect.TypeToken;
 
@@ -72,14 +83,19 @@ public class ServerPoolImpl extends DynamicClusterImpl implements ServerPool
{
     // Would use BiMap but persisting them tends to throw ConcurrentModificationExceptions.
     @SuppressWarnings("serial")
     public static final AttributeSensor<Map<Entity, MachineLocation>> ENTITY_MACHINE
= Sensors.newSensor(new TypeToken<Map<Entity, MachineLocation>>() {},
-                "pool.entityMachineMap", "A mapping of entities and their machine locations");
+            "pool.entityMachineMap", "A mapping of entities and their machine locations");
+
     @SuppressWarnings("serial")
     public static final AttributeSensor<Map<MachineLocation, Entity>> MACHINE_ENTITY
= Sensors.newSensor(new TypeToken<Map<MachineLocation, Entity>>() {},
-                "pool.machineEntityMap", "A mapping of machine locations and their entities");
+            "pool.machineEntityMap", "A mapping of machine locations and their entities");
 
     public static final AttributeSensor<LocationDefinition> DYNAMIC_LOCATION_DEFINITION
= Sensors.newSensor(LocationDefinition.class,
             "pool.locationDefinition", "The location definition used to create the pool's
dynamic location");
 
+    public static final ConfigKey<Boolean> REMOVABLE = ConfigKeys.newBooleanConfigKey(
+            "pool.member.removable", "Whether a pool member is removable from the cluster.
Used to denote additional " +
+                    "existing machines that were manually added to the pool", true);
+
     @SuppressWarnings("unused")
     private MemberTrackingPolicy membershipTracker;
 
@@ -211,6 +227,36 @@ public class ServerPoolImpl extends DynamicClusterImpl implements ServerPool
{
         }
     }
 
+    @Override
+    public Entity addExistingMachine(MachineLocation machine) {
+        LOG.info("Adding additional machine to {}: {}", this, machine);
+        Entity added = addNode(machine, MutableMap.of(REMOVABLE, false));
+        Map<String, ?> args = ImmutableMap.of("locations", ImmutableList.of(machine));
+        Task<Void> task = Effectors.invocation(added, Startable.START, args).asTask();
+        DynamicTasks.queueIfPossible(task).orSubmitAsync(this);
+        return added;
+    }
+
+    @Override
+    public Collection<Entity> addExistingMachinesFromSpec(String spec) {
+        Location location = getManagementContext().getLocationRegistry().resolveIfPossible(spec);
+        List<Entity> additions = Lists.newLinkedList();
+        if (location == null) {
+            LOG.warn("Spec was unresolvable: {}", spec);
+        } else {
+            Iterable<MachineLocation> machines = FluentIterable.from(location.getChildren())
+                    .filter(MachineLocation.class);
+            LOG.info("{} adding additional machines: {}", this, machines);
+            synchronized (mutex) {
+                for (MachineLocation machine : machines) {
+                    additions.add(addExistingMachine(machine));
+                }
+            }
+            LOG.debug("{} added additional machines", this);
+        }
+        return additions;
+    }
+
     /**
      * Overrides to restrict delta to the number of machines that can be <em>safely</em>
      * removed (i.e. those that are {@link MachinePoolMemberStatus#UNUSABLE unusable} or

http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/blob/01f70a79/software/base/src/test/java/brooklyn/entity/pool/ServerPoolTest.java
----------------------------------------------------------------------
diff --git a/software/base/src/test/java/brooklyn/entity/pool/ServerPoolTest.java b/software/base/src/test/java/brooklyn/entity/pool/ServerPoolTest.java
index e9f616e..88317ea 100644
--- a/software/base/src/test/java/brooklyn/entity/pool/ServerPoolTest.java
+++ b/software/base/src/test/java/brooklyn/entity/pool/ServerPoolTest.java
@@ -26,13 +26,16 @@ import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 import org.testng.annotations.Test;
 
+import com.google.common.collect.ImmutableList;
+import com.google.common.collect.Iterables;
+
 import brooklyn.entity.Entity;
 import brooklyn.entity.basic.Attributes;
+import brooklyn.location.LocationSpec;
+import brooklyn.location.basic.LocalhostMachineProvisioningLocation.LocalhostMachine;
+import brooklyn.test.Asserts;
 import brooklyn.test.entity.TestApplication;
 
-import com.google.common.collect.ImmutableList;
-import com.google.common.collect.Iterables;
-
 public class ServerPoolTest extends AbstractServerPoolTest {
 
     private static final Logger LOG = LoggerFactory.getLogger(ServerPoolTest.class);
@@ -122,4 +125,25 @@ public class ServerPoolTest extends AbstractServerPoolTest {
         assertClaimedCountEquals(getInitialPoolSize() - 1);
     }
 
+    @Test
+    public void testCanAddExistingMachinesToPool() {
+        TestApplication app = createAppWithChildren(getInitialPoolSize());
+        app.start(ImmutableList.of(pool.getDynamicLocation()));
+        assertAvailableCountEquals(0);
+
+        LocalhostMachine loc = mgmt.getLocationManager().createLocation(LocationSpec.create(LocalhostMachine.class));
+        Entity added = pool.addExistingMachine(loc);
+        assertFalse(added.getConfig(ServerPoolImpl.REMOVABLE));
+        Asserts.succeedsEventually(new Runnable() {
+            @Override
+            public void run() {
+                assertAvailableCountEquals(1);
+            }
+        });
+
+        TestApplication app2 = createAppWithChildren(1);
+        app2.start(ImmutableList.of(pool.getDynamicLocation()));
+        assertAvailableCountEquals(0);
+    }
+
 }


Mime
View raw message