You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@whirr.apache.org by to...@apache.org on 2011/05/24 07:10:39 UTC

svn commit: r1126869 - in /incubator/whirr/trunk: CHANGES.txt core/src/main/java/org/apache/whirr/Cluster.java core/src/main/java/org/apache/whirr/ClusterController.java core/src/test/java/org/apache/whirr/ClusterTest.java

Author: tomwhite
Date: Tue May 24 05:10:39 2011
New Revision: 1126869

URL: http://svn.apache.org/viewvc?rev=1126869&view=rev
Log:
WHIRR-312. Destroy instance removes all entries from the instances file except the one that is being terminated. Contributed by Andrei Savu.

Added:
    incubator/whirr/trunk/core/src/test/java/org/apache/whirr/ClusterTest.java   (with props)
Modified:
    incubator/whirr/trunk/CHANGES.txt
    incubator/whirr/trunk/core/src/main/java/org/apache/whirr/Cluster.java
    incubator/whirr/trunk/core/src/main/java/org/apache/whirr/ClusterController.java

Modified: incubator/whirr/trunk/CHANGES.txt
URL: http://svn.apache.org/viewvc/incubator/whirr/trunk/CHANGES.txt?rev=1126869&r1=1126868&r2=1126869&view=diff
==============================================================================
--- incubator/whirr/trunk/CHANGES.txt (original)
+++ incubator/whirr/trunk/CHANGES.txt Tue May 24 05:10:39 2011
@@ -92,6 +92,9 @@ Release 0.5.0 - 2011-05-16
 
     WHIRR-310. Improve Configuration Guide. (asavu via tomwhite)
 
+    WHIRR-312. Destroy instance removes all entries from the instances file
+    except the one that is being terminated. (asavu
+
   BUG FIXES
 
     WHIRR-253. ZooKeeper service should only authorize ingress to ZooKeeper 

Modified: incubator/whirr/trunk/core/src/main/java/org/apache/whirr/Cluster.java
URL: http://svn.apache.org/viewvc/incubator/whirr/trunk/core/src/main/java/org/apache/whirr/Cluster.java?rev=1126869&r1=1126868&r2=1126869&view=diff
==============================================================================
--- incubator/whirr/trunk/core/src/main/java/org/apache/whirr/Cluster.java (original)
+++ incubator/whirr/trunk/core/src/main/java/org/apache/whirr/Cluster.java Tue May 24 05:10:39 2011
@@ -23,6 +23,7 @@ import static com.google.common.base.Pre
 
 import com.google.common.base.Objects;
 import com.google.common.base.Predicate;
+import com.google.common.base.Predicates;
 import com.google.common.collect.Iterables;
 import com.google.common.collect.Sets;
 import com.google.common.net.InetAddresses;
@@ -161,7 +162,7 @@ public class Cluster {
   }
 
   public void removeInstancesMatching(Predicate<Instance> predicate) {
-    instances = Sets.filter(instances, predicate);
+    instances = Sets.filter(instances, Predicates.not(predicate));
   }
 
   public String toString() {

Modified: incubator/whirr/trunk/core/src/main/java/org/apache/whirr/ClusterController.java
URL: http://svn.apache.org/viewvc/incubator/whirr/trunk/core/src/main/java/org/apache/whirr/ClusterController.java?rev=1126869&r1=1126868&r2=1126869&view=diff
==============================================================================
--- incubator/whirr/trunk/core/src/main/java/org/apache/whirr/ClusterController.java (original)
+++ incubator/whirr/trunk/core/src/main/java/org/apache/whirr/ClusterController.java Tue May 24 05:10:39 2011
@@ -24,6 +24,7 @@ import com.google.common.base.Predicates
 
 import java.io.IOException;
 import java.util.Map;
+import java.util.NoSuchElementException;
 import java.util.Set;
 
 import com.google.common.collect.Iterables;
@@ -112,6 +113,7 @@ public class ClusterController {
     return cluster;
   }
 
+
   /**
    * Stop the cluster and destroy all resources associated with it.
    *
@@ -169,30 +171,31 @@ public class ClusterController {
 
   public Set<Cluster.Instance> getInstances(ClusterSpec spec, ClusterStateStore stateStore)
       throws IOException, InterruptedException {
+
     Set<Cluster.Instance> instances = Sets.newLinkedHashSet();
     Cluster cluster = (stateStore != null) ? stateStore.load() : null;
-    if (cluster != null) {
-      /* enrich the instance information with node metadata */
 
-      for(NodeMetadata node : getNodes(spec)) {
-        Cluster.Instance instance = cluster.getInstanceMatching(withIds(node.getId()));
-        instances.add(new Cluster.Instance(instance.getLoginCredentials(), instance.getRoles(),
-          instance.getPublicIp(), instance.getPrivateIp(), node.getId(), node)
-        );
-      }
-    } else {
-      /* return a list of instances with no roles attached */
+    for(NodeMetadata node : getNodes(spec)) {
+      instances.add(toInstance(node, cluster, spec));
+    }
 
-      Credentials credentials = new Credentials(spec.getClusterUser(), spec.getPrivateKey());
-      for(NodeMetadata node : getNodes(spec)) {
-        instances.add(new Cluster.Instance(credentials, Sets.<String>newHashSet(),
-            Iterables.getFirst(node.getPublicAddresses(), null),
-            Iterables.getFirst(node.getPrivateAddresses(), null),
-            node.getId(), node));
+    return instances;
+  }
+
+  private Cluster.Instance toInstance(NodeMetadata metadata, Cluster cluster, ClusterSpec spec) {
+    Credentials credentials = new Credentials(spec.getClusterUser(), spec.getPrivateKey());
+
+    Set<String> roles = Sets.newHashSet();
+    try {
+      if (cluster != null) {
+        roles = cluster.getInstanceMatching(withIds(metadata.getId())).getRoles();
       }
+    } catch(NoSuchElementException e) {}
 
-    }
-    return instances;
+    return new Cluster.Instance(credentials, roles,
+      Iterables.getFirst(metadata.getPublicAddresses(), null),
+      Iterables.getFirst(metadata.getPrivateAddresses(), null),
+      metadata.getId(), metadata);
   }
   
   public static Predicate<ComputeMetadata> runningInGroup(final String group) {

Added: incubator/whirr/trunk/core/src/test/java/org/apache/whirr/ClusterTest.java
URL: http://svn.apache.org/viewvc/incubator/whirr/trunk/core/src/test/java/org/apache/whirr/ClusterTest.java?rev=1126869&view=auto
==============================================================================
--- incubator/whirr/trunk/core/src/test/java/org/apache/whirr/ClusterTest.java (added)
+++ incubator/whirr/trunk/core/src/test/java/org/apache/whirr/ClusterTest.java Tue May 24 05:10:39 2011
@@ -0,0 +1,78 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.whirr;
+
+import com.google.common.collect.Sets;
+import org.jclouds.domain.Credentials;
+import org.junit.Before;
+import org.junit.Test;
+
+import java.util.NoSuchElementException;
+import java.util.Set;
+
+import static org.apache.whirr.RolePredicates.role;
+import static org.apache.whirr.RolePredicates.withIds;
+import static org.hamcrest.CoreMatchers.is;
+import static org.hamcrest.MatcherAssert.assertThat;
+import static org.junit.Assert.fail;
+
+public class ClusterTest {
+
+  private Cluster cluster;
+  private final int NUMBER_OF_INSTANCES = 5;
+
+  @Before
+  public void setUp() {
+    Credentials credentials = new Credentials("dummy", "dummy");
+    Set<Cluster.Instance> instances = Sets.newHashSet();
+
+    for(int i = 0; i < NUMBER_OF_INSTANCES; i++) {
+      String ip = "127.0.0." + (i + 1);
+      instances.add(new Cluster.Instance(credentials,
+        Sets.newHashSet("role-" + i), ip, ip, "id-" + i, null));
+    }
+
+    this.cluster = new Cluster(instances);
+  }
+
+  @Test
+  public void remoteInstanceById() {
+    cluster.removeInstancesMatching(withIds("id-0"));
+    try {
+      cluster.getInstanceMatching(withIds("id-0"));
+      fail("Element not remnoved");
+    } catch(NoSuchElementException e) {
+      /* exception thrown as expected */
+    }
+    assertThat(cluster.getInstances().size(), is(NUMBER_OF_INSTANCES - 1));
+  }
+
+  @Test
+  public void getInstanceById() {
+    Cluster.Instance instance = cluster.getInstanceMatching(withIds("id-0"));
+    assertThat(instance.getRoles().contains("role-0"), is(true));
+  }
+
+  @Test
+  public void getInstanceByRole() {
+    Cluster.Instance instance = cluster.getInstanceMatching(role("role-0"));
+    assertThat(instance.getId(), is("id-0"));
+  }
+
+}

Propchange: incubator/whirr/trunk/core/src/test/java/org/apache/whirr/ClusterTest.java
------------------------------------------------------------------------------
    svn:eol-style = native