Browse Source

AMBARI-12314. Downgrade fails with 'Cannot create action for Execute HDFS Finalize with no hosts' (ncole)

Nate Cole 10 years ago
parent
commit
cbb80f68fb

+ 9 - 3
ambari-server/src/main/java/org/apache/ambari/server/state/stack/upgrade/ClusterGrouping.java

@@ -203,16 +203,22 @@ public class ClusterGrouping extends Grouping {
       HostsType hosts = ctx.getResolver().getMasterAndHosts(service, component);
       HostsType hosts = ctx.getResolver().getMasterAndHosts(service, component);
 
 
       if (hosts != null) {
       if (hosts != null) {
-        Set<String> realHosts = new LinkedHashSet<String>(hosts.hosts);
 
 
-        if (null != et.hosts && "master".equals(et.hosts) && null != hosts.master) {
+        Set<String> realHosts = new LinkedHashSet<String>(hosts.hosts);
+        if (ExecuteHostType.MASTER == et.hosts && null != hosts.master) {
           realHosts = Collections.singleton(hosts.master);
           realHosts = Collections.singleton(hosts.master);
         }
         }
+
         // Pick a random host.
         // Pick a random host.
-        if (null != et.hosts && "any".equals(et.hosts) && !hosts.hosts.isEmpty()) {
+        if (ExecuteHostType.ANY == et.hosts && !hosts.hosts.isEmpty()) {
           realHosts = Collections.singleton(hosts.hosts.iterator().next());
           realHosts = Collections.singleton(hosts.hosts.iterator().next());
         }
         }
 
 
+        // !!! cannot execute against empty hosts (safety net)
+        if (realHosts.isEmpty()) {
+          return null;
+        }
+
         return new StageWrapper(
         return new StageWrapper(
             StageWrapper.Type.RU_TASKS,
             StageWrapper.Type.RU_TASKS,
             execution.title,
             execution.title,

+ 3 - 2
ambari-server/src/main/java/org/apache/ambari/server/state/stack/upgrade/TaskWrapperBuilder.java

@@ -48,7 +48,8 @@ public class TaskWrapperBuilder {
     List<TaskWrapper> collection = new ArrayList<TaskWrapper>();
     List<TaskWrapper> collection = new ArrayList<TaskWrapper>();
     for (Task t : tasks) {
     for (Task t : tasks) {
       if (t.getType().equals(Task.Type.EXECUTE)) {
       if (t.getType().equals(Task.Type.EXECUTE)) {
-        if (((ExecuteTask) t).hosts != null && ((ExecuteTask) t).hosts == ExecuteHostType.MASTER) {
+        ExecuteTask et = (ExecuteTask) t;
+        if (et.hosts == ExecuteHostType.MASTER) {
           if (hostsType.master != null) {
           if (hostsType.master != null) {
             collection.add(new TaskWrapper(service, component, Collections.singleton(hostsType.master), t));
             collection.add(new TaskWrapper(service, component, Collections.singleton(hostsType.master), t));
             continue;
             continue;
@@ -58,7 +59,7 @@ public class TaskWrapperBuilder {
           }
           }
         }
         }
         // Pick a random host.
         // Pick a random host.
-        if (((ExecuteTask) t).hosts != null && ((ExecuteTask) t).hosts == ExecuteHostType.ANY) {
+        if (et.hosts == ExecuteHostType.ANY) {
           if (hostsType.hosts != null && !hostsType.hosts.isEmpty()) {
           if (hostsType.hosts != null && !hostsType.hosts.isEmpty()) {
             collection.add(new TaskWrapper(service, component, Collections.singleton(hostsType.hosts.iterator().next()), t));
             collection.add(new TaskWrapper(service, component, Collections.singleton(hostsType.hosts.iterator().next()), t));
             continue;
             continue;

+ 73 - 0
ambari-server/src/test/java/org/apache/ambari/server/state/UpgradeHelperTest.java

@@ -879,6 +879,79 @@ public class UpgradeHelperTest {
     return c;
     return c;
   }
   }
 
 
+  @Test
+  public void testDowngradeAfterPartialUpgrade() throws Exception {
+
+    Clusters clusters = injector.getInstance(Clusters.class);
+    ServiceFactory serviceFactory = injector.getInstance(ServiceFactory.class);
+
+    String clusterName = "c1";
+
+    StackId stackId = new StackId("HDP-2.1.1");
+    clusters.addCluster(clusterName, stackId);
+    Cluster c = clusters.getCluster(clusterName);
+
+    helper.getOrCreateRepositoryVersion(stackId,
+        c.getDesiredStackVersion().getStackVersion());
+
+    c.createClusterVersion(stackId,
+        c.getDesiredStackVersion().getStackVersion(), "admin",
+        RepositoryVersionState.UPGRADING);
+
+    for (int i = 0; i < 2; i++) {
+      String hostName = "h" + (i+1);
+      clusters.addHost(hostName);
+      Host host = clusters.getHost(hostName);
+
+      Map<String, String> hostAttributes = new HashMap<String, String>();
+      hostAttributes.put("os_family", "redhat");
+      hostAttributes.put("os_release_version", "6");
+
+      host.setHostAttributes(hostAttributes);
+
+      host.persist();
+      clusters.mapHostToCluster(hostName, clusterName);
+    }
+
+    // !!! add services
+    c.addService(serviceFactory.createNew(c, "HDFS"));
+
+    Service s = c.getService("HDFS");
+    ServiceComponent sc = s.addServiceComponent("NAMENODE");
+    sc.addServiceComponentHost("h1");
+    sc.addServiceComponentHost("h2");
+
+    List<ServiceComponentHost> schs = c.getServiceComponentHosts("HDFS", "NAMENODE");
+    assertEquals(2, schs.size());
+
+    HostsType type = new HostsType();
+    type.master = "h1";
+    type.secondary = "h2";
+
+    expect(m_masterHostResolver.getMasterAndHosts("ZOOKEEPER", "ZOOKEEPER_SERVER")).andReturn(null).anyTimes();
+    expect(m_masterHostResolver.getMasterAndHosts("HDFS", "NAMENODE")).andReturn(type).anyTimes();
+    expect(m_masterHostResolver.getCluster()).andReturn(c).anyTimes();
+    replay(m_masterHostResolver);
+
+    UpgradeContext context = new UpgradeContext(m_masterHostResolver, HDP_21, HDP_21, DOWNGRADE_VERSION, Direction.DOWNGRADE);
+
+    Map<String, UpgradePack> upgrades = ambariMetaInfo.getUpgradePacks("HDP", "2.1.1");
+    UpgradePack upgrade = upgrades.get("upgrade_direction");
+    assertNotNull(upgrade);
+
+    List<UpgradeGroupHolder> groups = m_upgradeHelper.createSequence(upgrade, context);
+    assertEquals(1, groups.size());
+
+    UpgradeGroupHolder group = groups.get(0);
+    assertEquals(3, group.items.size());
+
+    StageWrapper stage = group.items.get(1);
+    assertEquals("NameNode Finalize", stage.getText());
+    assertEquals(1, stage.getTasks().size());
+    TaskWrapper task = stage.getTasks().get(0);
+    assertEquals(1, task.getHosts().size());
+  }
+
 
 
 
 
   /**
   /**

+ 1 - 1
ambari-server/src/test/java/org/apache/ambari/server/state/stack/UpgradePackTest.java

@@ -243,7 +243,7 @@ public class UpgradePackTest {
 
 
     ClusterGrouping cluster_group = (ClusterGrouping) groups.get(2);
     ClusterGrouping cluster_group = (ClusterGrouping) groups.get(2);
     List<ExecuteStage> stages = cluster_group.executionStages;
     List<ExecuteStage> stages = cluster_group.executionStages;
-    assertEquals(2, stages.size());
+    assertEquals(3, stages.size());
     assertNotNull(stages.get(0).intendedDirection);
     assertNotNull(stages.get(0).intendedDirection);
     assertEquals(Direction.DOWNGRADE, stages.get(0).intendedDirection);
     assertEquals(Direction.DOWNGRADE, stages.get(0).intendedDirection);
 
 

+ 8 - 0
ambari-server/src/test/resources/stacks/HDP/2.1.1/upgrades/upgrade_direction.xml

@@ -41,6 +41,14 @@
           <message>this is downgrade message</message>
           <message>this is downgrade message</message>
         </task>
         </task>
       </execute-stage>
       </execute-stage>
+      
+      <execute-stage title="NameNode Finalize" service="HDFS" component="NAMENODE" >
+        <task xsi:type="execute" hosts="master">
+          <script>scripts/namenode.py</script>
+          <function>actionexecute</function>
+        </task>
+      </execute-stage>      
+      
       <execute-stage title="Save Cluster State" service="" component="">
       <execute-stage title="Save Cluster State" service="" component="">
         <task xsi:type="server_action" class="org.apache.ambari.server.serveraction.upgrades.FinalizeUpgradeAction">
         <task xsi:type="server_action" class="org.apache.ambari.server.serveraction.upgrades.FinalizeUpgradeAction">
         </task>
         </task>