You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@whirr.apache.org by as...@apache.org on 2012/02/06 00:41:05 UTC

svn commit: r1240863 - in /whirr/trunk: CHANGES.txt core/src/main/java/org/apache/whirr/actions/ByonClusterAction.java

Author: asavu
Date: Sun Feb  5 23:41:05 2012
New Revision: 1240863

URL: http://svn.apache.org/viewvc?rev=1240863&view=rev
Log:
WHIRR-500. Let users control which hardware is used for each instance template (Karel Vervaeke via asavu)

Modified:
    whirr/trunk/CHANGES.txt
    whirr/trunk/core/src/main/java/org/apache/whirr/actions/ByonClusterAction.java

Modified: whirr/trunk/CHANGES.txt
URL: http://svn.apache.org/viewvc/whirr/trunk/CHANGES.txt?rev=1240863&r1=1240862&r2=1240863&view=diff
==============================================================================
--- whirr/trunk/CHANGES.txt (original)
+++ whirr/trunk/CHANGES.txt Sun Feb  5 23:41:05 2012
@@ -2,6 +2,11 @@ Apache Whirr Change Log
 
 Trunk (unreleased changes)
 
+  NEW FEATURES
+
+    WHIRR-500. Let users control which hardware is used for each 
+    instance template (Karel Vervaeke via asavu)
+
   IMPROVEMENTS
 
     WHIRR-421. Handle more role / service lifecycle events as part 

Modified: whirr/trunk/core/src/main/java/org/apache/whirr/actions/ByonClusterAction.java
URL: http://svn.apache.org/viewvc/whirr/trunk/core/src/main/java/org/apache/whirr/actions/ByonClusterAction.java?rev=1240863&r1=1240862&r2=1240863&view=diff
==============================================================================
--- whirr/trunk/core/src/main/java/org/apache/whirr/actions/ByonClusterAction.java (original)
+++ whirr/trunk/core/src/main/java/org/apache/whirr/actions/ByonClusterAction.java Sun Feb  5 23:41:05 2012
@@ -19,12 +19,16 @@
 package org.apache.whirr.actions;
 
 import com.google.common.base.Function;
+import com.google.common.base.Predicate;
+import com.google.common.base.Predicates;
 import com.google.common.collect.Collections2;
 import com.google.common.collect.Iterables;
 import com.google.common.collect.Lists;
 import com.google.common.collect.Sets;
 
 import java.io.IOException;
+import java.util.ArrayList;
+import java.util.Arrays;
 import java.util.Collection;
 import java.util.List;
 import java.util.Map;
@@ -35,7 +39,7 @@ import java.util.concurrent.ExecutionExc
 import java.util.concurrent.ExecutorService;
 import java.util.concurrent.Executors;
 import java.util.concurrent.Future;
-
+import org.apache.commons.lang.StringUtils;
 import org.apache.whirr.Cluster;
 import org.apache.whirr.Cluster.Instance;
 import org.apache.whirr.ClusterSpec;
@@ -80,6 +84,7 @@ public class ByonClusterAction extends S
     Set<Future<Void>> futures = Sets.newHashSet();
 
     List<NodeMetadata> nodes = Lists.newArrayList();
+    List<NodeMetadata> usedNodes = Lists.newArrayList();
     int numberAllocated = 0;
     Set<Instance> allInstances = Sets.newLinkedHashSet();
 
@@ -106,9 +111,16 @@ public class ByonClusterAction extends S
       }
 
       int num = entry.getKey().getNumberOfInstances();
-      final List<NodeMetadata> templateNodes =
-        nodes.subList(numberAllocated, numberAllocated + num);
-      numberAllocated += num;
+      Predicate<NodeMetadata> unused = Predicates.not(Predicates.in(usedNodes));
+      Predicate<NodeMetadata> instancePredicate = new TagsPredicate(StringUtils.split(entry.getKey().getHardwareId()));      
+
+      List<NodeMetadata> templateNodes = new ArrayList(Collections2.filter(nodes, Predicates.and(unused, instancePredicate)));
+      if (templateNodes.size() < num) {
+        LOG.warn("Not enough nodes available for template " + StringUtils.join(entry.getKey().getRoles(), "+"));
+      }
+      templateNodes = templateNodes.subList(0, num);
+      usedNodes.addAll(templateNodes);
+      numberAllocated = usedNodes.size() ;
       
       final Set<Instance> templateInstances = getInstances(
           credentials, entry.getKey().getRoles(), templateNodes
@@ -167,4 +179,21 @@ public class ByonClusterAction extends S
     ));
   }
   
+  private static class TagsPredicate implements Predicate<NodeMetadata>{
+    private String[] tags;
+
+    public TagsPredicate(String[] tags) {
+      this.tags = tags;
+    }
+
+    @Override
+    public boolean apply(NodeMetadata node) {
+      if (tags == null) {
+        return true;
+      } else {
+        return node.getTags().containsAll(Arrays.asList(tags));
+      }
+    }
+
+  }
 }