This is an automated email from the ASF dual-hosted git repository.

dmitriusan pushed a commit to branch trunk
in repository https://gitbox.apache.org/repos/asf/ambari.git


The following commit(s) were added to refs/heads/trunk by this push:
     new 642220e  AMBARI-24996. Hostname update command does not work if the 
new hostna… (#2688)
642220e is described below

commit 642220e874991f41f78f545a12ad85a654e4bd97
Author: Lisnichenko Dmitro <[email protected]>
AuthorDate: Thu Dec 6 15:17:06 2018 +0200

    AMBARI-24996. Hostname update command does not work if the new hostna… 
(#2688)
    
    AMBARI-24996. Hostname update command does not work if the new hostnames 
contain the old hostnames also (dlysnichenko)
---
 .../ambari/server/update/HostUpdateHelper.java     | 52 ++++++++++++----------
 .../ambari/server/update/HostUpdateHelperTest.java | 20 ++++-----
 2 files changed, 39 insertions(+), 33 deletions(-)

diff --git 
a/ambari-server/src/main/java/org/apache/ambari/server/update/HostUpdateHelper.java
 
b/ambari-server/src/main/java/org/apache/ambari/server/update/HostUpdateHelper.java
index 2b7129b..1d9b28f 100644
--- 
a/ambari-server/src/main/java/org/apache/ambari/server/update/HostUpdateHelper.java
+++ 
b/ambari-server/src/main/java/org/apache/ambari/server/update/HostUpdateHelper.java
@@ -90,6 +90,7 @@ public class HostUpdateHelper {
           (HostUpdateHelper.class);
 
   private static final String AUTHENTICATED_USER_NAME = "ambari-host-update";
+  public static final String TMP_PREFIX = "tmpvalue";
 
 
   private PersistService persistService;
@@ -403,20 +404,31 @@ public class HostUpdateHelper {
     for (Map.Entry<String, Map<String,String>> clusterHosts : 
hostChangesFileMap.entrySet()) {
       String clusterName = clusterHosts.getKey();
       Map<String, String> hostMapping = clusterHosts.getValue();
-      ClusterEntity clusterEntity = clusterDAO.findByName(clusterName);
-      List<String> currentHostNames = new ArrayList<>();
-
+      Map<String, String> toTmpHostMapping = new HashMap<>();
+      Map<String, String> fromTmpHostMapping = new HashMap<>();
       for (Map.Entry<String, String> hostPair : hostMapping.entrySet()) {
-        currentHostNames.add(hostPair.getKey());
+        toTmpHostMapping.put(hostPair.getKey(), TMP_PREFIX + 
hostPair.getValue());
+        fromTmpHostMapping.put(TMP_PREFIX + hostPair.getValue(), 
hostPair.getValue());
       }
+      ClusterEntity clusterEntity = clusterDAO.findByName(clusterName);
+      renameHostsInDB(hostDAO, toTmpHostMapping, clusterEntity);
+      renameHostsInDB(hostDAO, fromTmpHostMapping, clusterEntity);
+    }
+  }
 
-      if (clusterEntity != null) {
-        Collection<HostEntity> hostEntities = clusterEntity.getHostEntities();
-        for (HostEntity hostEntity : hostEntities) {
-          if (currentHostNames.contains(hostEntity.getHostName())) {
-            hostEntity.setHostName(hostMapping.get(hostEntity.getHostName()));
-            hostDAO.merge(hostEntity);
-          }
+  private void renameHostsInDB(HostDAO hostDAO, Map<String, String> 
hostMapping, ClusterEntity clusterEntity) {
+    List<String> currentHostNames = new ArrayList<>();
+
+    for (Map.Entry<String, String> hostPair : hostMapping.entrySet()) {
+      currentHostNames.add(hostPair.getKey());
+    }
+
+    if (clusterEntity != null) {
+      Collection<HostEntity> hostEntities = clusterEntity.getHostEntities();
+      for (HostEntity hostEntity : hostEntities) {
+        if (currentHostNames.contains(hostEntity.getHostName())) {
+          hostEntity.setHostName(hostMapping.get(hostEntity.getHostName()));
+          hostDAO.merge(hostEntity);
         }
       }
     }
@@ -534,6 +546,8 @@ public class HostUpdateHelper {
   }
 
   public static void main(String[] args) throws Exception {
+    Injector injector = Guice.createInjector(new UpdateHelperModule(), new 
CheckHelperAuditModule(), new LdapModule());
+    HostUpdateHelper hostUpdateHelper = 
injector.getInstance(HostUpdateHelper.class);
     try {
       LOG.info("Host names update started.");
 
@@ -542,10 +556,6 @@ public class HostUpdateHelper {
       if (hostChangesFile == null || hostChangesFile.isEmpty()) {
         throw new AmbariException("Path to file with host names changes is 
empty or null.");
       }
-
-      Injector injector = Guice.createInjector(new UpdateHelperModule(), new 
CheckHelperAuditModule(), new LdapModule());
-      HostUpdateHelper hostUpdateHelper = 
injector.getInstance(HostUpdateHelper.class);
-
       hostUpdateHelper.setHostChangesFile(hostChangesFile);
 
       hostUpdateHelper.initHostChangesFileMap();
@@ -566,15 +576,11 @@ public class HostUpdateHelper {
 
       LOG.info("Host names update completed successfully.");
 
-      hostUpdateHelper.stopPersistenceService();
-    } catch (Throwable e) {
-      if (e instanceof AmbariException) {
+    } catch (AmbariException e) {
         LOG.error("Exception occurred during host names update, failed", e);
-        throw (AmbariException)e;
-      }else{
-        LOG.error("Unexpected error, host names update failed", e);
-        throw new Exception("Unexpected error, host names update failed", e);
-      }
+        throw e;
+    } finally {
+      hostUpdateHelper.stopPersistenceService();
     }
   }
 }
diff --git 
a/ambari-server/src/test/java/org/apache/ambari/server/update/HostUpdateHelperTest.java
 
b/ambari-server/src/test/java/org/apache/ambari/server/update/HostUpdateHelperTest.java
index b6b34da..1454707 100644
--- 
a/ambari-server/src/test/java/org/apache/ambari/server/update/HostUpdateHelperTest.java
+++ 
b/ambari-server/src/test/java/org/apache/ambari/server/update/HostUpdateHelperTest.java
@@ -411,8 +411,8 @@ public class HostUpdateHelperTest {
     final EntityManager entityManager = createNiceMock(EntityManager.class);
     final DBAccessor dbAccessor = createNiceMock(DBAccessor.class);
     ClusterEntity mockClusterEntity = 
easyMockSupport.createNiceMock(ClusterEntity.class);
-    HostEntity mockHostEntity1 = 
easyMockSupport.createNiceMock(HostEntity.class);
-    HostEntity mockHostEntity2 = 
easyMockSupport.createNiceMock(HostEntity.class);
+    HostEntity mockHostEntity1 = new HostEntity();
+    HostEntity mockHostEntity2 = new HostEntity();
     Map<String, Map<String, String>> clusterHostsToChange = new HashMap<>();
     List<HostEntity> hostEntities = new ArrayList<>();
     Map<String, String> hosts = new HashMap<>();
@@ -437,15 +437,10 @@ public class HostUpdateHelperTest {
     });
 
     
expect(mockClusterDAO.findByName("cl1")).andReturn(mockClusterEntity).once();
-    expect(mockClusterEntity.getHostEntities()).andReturn(hostEntities).once();
-    expect(mockHostEntity1.getHostName()).andReturn("host1").atLeastOnce();
-    expect(mockHostEntity2.getHostName()).andReturn("host2").atLeastOnce();
+    
expect(mockClusterEntity.getHostEntities()).andReturn(hostEntities).times(2);
+    mockHostEntity1.setHostName("host1");
+    mockHostEntity2.setHostName("host2");
 
-    mockHostEntity1.setHostName("host10");
-    expectLastCall();
-
-    mockHostEntity2.setHostName("host11");
-    expectLastCall();
 
     HostUpdateHelper hostUpdateHelper = new HostUpdateHelper(null, null, 
mockInjector);
 
@@ -454,6 +449,11 @@ public class HostUpdateHelperTest {
     easyMockSupport.replayAll();
     hostUpdateHelper.updateHostsInDB();
     easyMockSupport.verifyAll();
+
+
+    Assert.assertEquals(mockHostEntity1.getHostName(),"host10");
+    Assert.assertEquals(mockHostEntity2.getHostName(),"host11");
+
   }
 
   @Test

Reply via email to