brooklyn-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From henev...@apache.org
Subject [1/3] brooklyn-server git commit: BYON resolver: use LocationSpec for machines
Date Wed, 30 Mar 2016 13:51:39 GMT
Repository: brooklyn-server
Updated Branches:
  refs/heads/master cf305e25c -> c6b4372af


BYON resolver: use LocationSpec for machines

Avoid instantiating the machine locations in the resolver, as that
causes us to manage (and never unmanage) them.

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

Branch: refs/heads/master
Commit: 3170a814d817df20567cf079d081644988291fd7
Parents: 89f9dd7
Author: Aled Sage <aled.sage@gmail.com>
Authored: Wed Mar 30 13:50:25 2016 +0100
Committer: Aled Sage <aled.sage@gmail.com>
Committed: Wed Mar 30 13:50:25 2016 +0100

----------------------------------------------------------------------
 .../location/byon/ByonLocationResolver.java     |  7 ++-
 .../FixedListMachineProvisioningLocation.java   | 51 +++++++++++++++-----
 .../location/byon/ByonLocationResolverTest.java | 27 ++++++++++-
 3 files changed, 69 insertions(+), 16 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/brooklyn-server/blob/3170a814/core/src/main/java/org/apache/brooklyn/location/byon/ByonLocationResolver.java
----------------------------------------------------------------------
diff --git a/core/src/main/java/org/apache/brooklyn/location/byon/ByonLocationResolver.java
b/core/src/main/java/org/apache/brooklyn/location/byon/ByonLocationResolver.java
index 3fa528e..90c9a53 100644
--- a/core/src/main/java/org/apache/brooklyn/location/byon/ByonLocationResolver.java
+++ b/core/src/main/java/org/apache/brooklyn/location/byon/ByonLocationResolver.java
@@ -126,7 +126,7 @@ public class ByonLocationResolver extends AbstractLocationResolver {
             throw new IllegalArgumentException("Invalid location '"+spec+"'; at least one
host must be defined");
         }
         
-        List<MachineLocation> machines = Lists.newArrayList();
+        List<LocationSpec<? extends MachineLocation>> machineSpecs = Lists.newArrayList();
         for (Object host : hostAddresses) {
             LocationSpec<? extends MachineLocation> machineSpec;
             if (host instanceof String) {
@@ -137,11 +137,10 @@ public class ByonLocationResolver extends AbstractLocationResolver {
                 throw new IllegalArgumentException("Expected machine to be String or Map,
but was "+host.getClass().getName()+" ("+host+")");
             }
             machineSpec.configureIfNotNull(LocalLocationManager.CREATE_UNMANAGED, config.get(LocalLocationManager.CREATE_UNMANAGED));
-            MachineLocation machine = managementContext.getLocationManager().createLocation(machineSpec);
-            machines.add(machine);
+            machineSpecs.add(machineSpec);
         }
         
-        config.putStringKey("machines", machines);
+        config.put(FixedListMachineProvisioningLocation.MACHINE_SPECS, machineSpecs);
 
         return config;
     }

http://git-wip-us.apache.org/repos/asf/brooklyn-server/blob/3170a814/core/src/main/java/org/apache/brooklyn/location/byon/FixedListMachineProvisioningLocation.java
----------------------------------------------------------------------
diff --git a/core/src/main/java/org/apache/brooklyn/location/byon/FixedListMachineProvisioningLocation.java
b/core/src/main/java/org/apache/brooklyn/location/byon/FixedListMachineProvisioningLocation.java
index fd83393..6e88f15 100644
--- a/core/src/main/java/org/apache/brooklyn/location/byon/FixedListMachineProvisioningLocation.java
+++ b/core/src/main/java/org/apache/brooklyn/location/byon/FixedListMachineProvisioningLocation.java
@@ -39,9 +39,19 @@ import org.apache.brooklyn.config.ConfigKey;
 import org.apache.brooklyn.core.config.ConfigKeys;
 import org.apache.brooklyn.core.location.AbstractLocation;
 import org.apache.brooklyn.core.location.cloud.CloudLocationConfig;
+import org.apache.brooklyn.location.ssh.SshMachineLocation;
+import org.apache.brooklyn.util.collections.CollectionFunctionals;
+import org.apache.brooklyn.util.collections.MutableMap;
+import org.apache.brooklyn.util.collections.MutableSet;
+import org.apache.brooklyn.util.core.config.ConfigBag;
+import org.apache.brooklyn.util.core.flags.SetFromFlag;
+import org.apache.brooklyn.util.stream.Streams;
+import org.apache.brooklyn.util.text.WildcardGlobs;
+import org.apache.brooklyn.util.text.WildcardGlobs.PhraseTreatment;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
+import com.google.common.annotations.Beta;
 import com.google.common.base.Function;
 import com.google.common.base.Objects;
 import com.google.common.collect.ImmutableList;
@@ -52,16 +62,6 @@ import com.google.common.collect.Maps;
 import com.google.common.collect.Sets;
 import com.google.common.reflect.TypeToken;
 
-import org.apache.brooklyn.location.ssh.SshMachineLocation;
-import org.apache.brooklyn.util.collections.CollectionFunctionals;
-import org.apache.brooklyn.util.collections.MutableMap;
-import org.apache.brooklyn.util.collections.MutableSet;
-import org.apache.brooklyn.util.core.config.ConfigBag;
-import org.apache.brooklyn.util.core.flags.SetFromFlag;
-import org.apache.brooklyn.util.stream.Streams;
-import org.apache.brooklyn.util.text.WildcardGlobs;
-import org.apache.brooklyn.util.text.WildcardGlobs.PhraseTreatment;
-
 /**
  * A provisioner of {@link MachineLocation}s which takes a list of machines it can connect
to.
  * The collection of initial machines should be supplied in the 'machines' flag in the constructor,
@@ -88,7 +88,26 @@ implements MachineProvisioningLocation<T>, Closeable {
                     CollectionFunctionals.<MachineLocation>firstElement());
 
     public static final ConfigKey<Collection<MachineLocationCustomizer>> MACHINE_LOCATION_CUSTOMIZERS
= CloudLocationConfig.MACHINE_LOCATION_CUSTOMIZERS;
-    
+
+    /**
+     * The machineSpecs allows {@link ByonLocationResolver} to work with just LocationSpecs,
rather than 
+     * having to instantiate the machines immediately (which would be bad, because the caller
+     * might not use the spec and thus might not unmanage the machine instances).
+     * 
+     * We clear the machineSpecs in init, so they will never be persisted. This will help
with
+     * backwards compatibility if we change how this is done.
+     * 
+     * By the end of init(), the {@link #machines} will contain the full list of locations;

+     * it will never contain a {@link LocationSpec}.
+     */
+    @Beta
+    @SuppressWarnings("serial")
+    public static final ConfigKey<List<LocationSpec<? extends MachineLocation>>>
MACHINE_SPECS = ConfigKeys.newConfigKey(
+                    new TypeToken<List<LocationSpec<? extends MachineLocation>>>()
{}, 
+                    "byon.machineSpecs",
+                    "Specs of machines that should be immediatly instantiated on init",
+                    ImmutableList.<LocationSpec<? extends MachineLocation>>of());
+
     private final Object lock = new Object();
     
     @SetFromFlag
@@ -118,6 +137,16 @@ implements MachineProvisioningLocation<T>, Closeable {
     public void init() {
         super.init();
         
+        List<LocationSpec<? extends MachineLocation>> machineSpecs = getConfig(MACHINE_SPECS);
+        if (machineSpecs != null) {
+            for (LocationSpec<? extends MachineLocation> spec : machineSpecs) {
+                @SuppressWarnings("unchecked")
+                T machine = (T) getManagementContext().getLocationManager().createLocation(spec);
+                machines.add(machine);
+            }
+        }
+        config().set(MACHINE_SPECS, (List<LocationSpec<? extends MachineLocation>>)
null);
+        
         Set<T> machinesCopy = MutableSet.of();
         for (T location: machines) {
             if (location==null) {

http://git-wip-us.apache.org/repos/asf/brooklyn-server/blob/3170a814/core/src/test/java/org/apache/brooklyn/location/byon/ByonLocationResolverTest.java
----------------------------------------------------------------------
diff --git a/core/src/test/java/org/apache/brooklyn/location/byon/ByonLocationResolverTest.java
b/core/src/test/java/org/apache/brooklyn/location/byon/ByonLocationResolverTest.java
index c03e9b2..7e13fe3 100644
--- a/core/src/test/java/org/apache/brooklyn/location/byon/ByonLocationResolverTest.java
+++ b/core/src/test/java/org/apache/brooklyn/location/byon/ByonLocationResolverTest.java
@@ -23,6 +23,7 @@ import static org.testng.Assert.assertTrue;
 import static org.testng.Assert.fail;
 
 import java.net.InetAddress;
+import java.util.Collection;
 import java.util.List;
 import java.util.Map;
 import java.util.NoSuchElementException;
@@ -66,6 +67,7 @@ import com.google.common.collect.ImmutableList;
 import com.google.common.collect.ImmutableMap;
 import com.google.common.collect.ImmutableSet;
 import com.google.common.collect.Iterables;
+import com.google.common.collect.Sets;
 
 public class ByonLocationResolverTest {
 
@@ -351,6 +353,24 @@ public class ByonLocationResolverTest {
         assertEquals(machine.getConfig(ConfigKeys.newConfigKey(String.class, "mykey")), "myval");
     }
 
+    @Test
+    public void testLocationSpecDoesNotCreateMachines() throws Exception {
+        Collection<Location> before = managementContext.getLocationManager().getLocations();
+        
+        String spec = "byon(hosts=\"1.1.1.1\")";
+        LocationSpec<FixedListMachineProvisioningLocation<MachineLocation>> locationSpec
= getLocationSpec(spec);
+        
+        Collection<Location> after = managementContext.getLocationManager().getLocations();
+        assertEquals(after, before, "after="+after+"; before="+before);
+    }
+
+    @Test
+    public void testMachineSpecsTemporaryConfigWillNotBePersisted() throws Exception {
+        String spec = "byon(hosts=\"1.1.1.1\")";
+        MachineProvisioningLocation<MachineLocation> provisioner = resolve(spec);
+        assertEquals(provisioner.config().get(FixedListMachineProvisioningLocation.MACHINE_SPECS),
null);
+    }
+
     private void assertByonClusterEquals(FixedListMachineProvisioningLocation<? extends
MachineLocation> cluster, Set<String> expectedHosts) {
         assertByonClusterEquals(cluster, expectedHosts, defaultNamePredicate);
     }
@@ -399,7 +419,12 @@ public class ByonLocationResolverTest {
             // success
         }
     }
-    
+
+    @SuppressWarnings("unchecked")
+    private LocationSpec<FixedListMachineProvisioningLocation<MachineLocation>>
getLocationSpec(String val) {
+        return (LocationSpec<FixedListMachineProvisioningLocation<MachineLocation>>)
managementContext.getLocationRegistry().getLocationSpec(val).get();
+    }
+
     @SuppressWarnings("unchecked")
     private FixedListMachineProvisioningLocation<MachineLocation> resolve(String val)
{
         return (FixedListMachineProvisioningLocation<MachineLocation>) managementContext.getLocationRegistry().getLocationManaged(val);


Mime
View raw message