ambari-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From jonathanhur...@apache.org
Subject [1/2] ambari git commit: AMBARI-17371 - Unable To Delete A Service After Upgrade Due To Foreign Key Violation (jonathanhurley)
Date Thu, 23 Jun 2016 02:10:15 GMT
Repository: ambari
Updated Branches:
  refs/heads/branch-2.4 479614c3b -> ec942a1ac


AMBARI-17371 - Unable To Delete A Service After Upgrade Due To Foreign Key Violation (jonathanhurley)


Project: http://git-wip-us.apache.org/repos/asf/ambari/repo
Commit: http://git-wip-us.apache.org/repos/asf/ambari/commit/4720e51d
Tree: http://git-wip-us.apache.org/repos/asf/ambari/tree/4720e51d
Diff: http://git-wip-us.apache.org/repos/asf/ambari/diff/4720e51d

Branch: refs/heads/branch-2.4
Commit: 4720e51dc68ac39b9842458f54fa17f104b910e9
Parents: 479614c
Author: Jonathan Hurley <jhurley@hortonworks.com>
Authored: Wed Jun 22 11:30:28 2016 -0400
Committer: Jonathan Hurley <jhurley@hortonworks.com>
Committed: Wed Jun 22 22:08:54 2016 -0400

----------------------------------------------------------------------
 .../ServiceComponentDesiredStateEntity.java     |  9 +--
 .../entities/ServiceComponentHistoryEntity.java | 66 +++++---------------
 .../upgrades/FinalizeUpgradeAction.java         |  7 ++-
 .../server/state/ServiceComponentTest.java      | 23 +++++--
 4 files changed, 40 insertions(+), 65 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/ambari/blob/4720e51d/ambari-server/src/main/java/org/apache/ambari/server/orm/entities/ServiceComponentDesiredStateEntity.java
----------------------------------------------------------------------
diff --git a/ambari-server/src/main/java/org/apache/ambari/server/orm/entities/ServiceComponentDesiredStateEntity.java
b/ambari-server/src/main/java/org/apache/ambari/server/orm/entities/ServiceComponentDesiredStateEntity.java
index 7281c01..61c053d 100644
--- a/ambari-server/src/main/java/org/apache/ambari/server/orm/entities/ServiceComponentDesiredStateEntity.java
+++ b/ambari-server/src/main/java/org/apache/ambari/server/orm/entities/ServiceComponentDesiredStateEntity.java
@@ -114,7 +114,7 @@ public class ServiceComponentDesiredStateEntity {
    */
   @OneToMany(
       mappedBy = "m_serviceComponentDesiredStateEntity",
-      cascade = { CascadeType.MERGE, CascadeType.REFRESH, CascadeType.REMOVE })
+      cascade = { CascadeType.ALL })
   private Collection<ServiceComponentHistoryEntity> serviceComponentHistory;
 
   public Long getId() {
@@ -171,7 +171,7 @@ public class ServiceComponentDesiredStateEntity {
 
   /**
    * Adds a historical entry for the version of this service component. New
-   * entries are automatically created when this entities is merged via a
+   * entries are automatically created when this entity is merged via a
    * {@link CascadeType#MERGE}.
    *
    * @param historicalEntry
@@ -183,10 +183,7 @@ public class ServiceComponentDesiredStateEntity {
     }
 
     serviceComponentHistory.add(historicalEntry);
-
-    if (!equals(historicalEntry.getServiceComponentDesiredState())) {
-      historicalEntry.setServiceComponentDesiredState(this);
-    }
+    historicalEntry.setServiceComponentDesiredState(this);
   }
 
   /**

http://git-wip-us.apache.org/repos/asf/ambari/blob/4720e51d/ambari-server/src/main/java/org/apache/ambari/server/orm/entities/ServiceComponentHistoryEntity.java
----------------------------------------------------------------------
diff --git a/ambari-server/src/main/java/org/apache/ambari/server/orm/entities/ServiceComponentHistoryEntity.java
b/ambari-server/src/main/java/org/apache/ambari/server/orm/entities/ServiceComponentHistoryEntity.java
index e7fef71..1521468 100644
--- a/ambari-server/src/main/java/org/apache/ambari/server/orm/entities/ServiceComponentHistoryEntity.java
+++ b/ambari-server/src/main/java/org/apache/ambari/server/orm/entities/ServiceComponentHistoryEntity.java
@@ -30,7 +30,9 @@ import javax.persistence.NamedQuery;
 import javax.persistence.Table;
 import javax.persistence.TableGenerator;
 
-import org.apache.commons.lang.ObjectUtils;
+import org.apache.commons.lang.builder.HashCodeBuilder;
+
+import com.google.common.base.Objects;
 
 /**
  * The {@link ServiceComponentHistoryEntity} class is used to represent an
@@ -86,10 +88,6 @@ public class ServiceComponentHistoryEntity {
    */
   public void setServiceComponentDesiredState(ServiceComponentDesiredStateEntity serviceComponentDesiredStateEntity)
{
     m_serviceComponentDesiredStateEntity = serviceComponentDesiredStateEntity;
-
-    if (!m_serviceComponentDesiredStateEntity.getHistory().contains(this)) {
-      m_serviceComponentDesiredStateEntity.addHistory(this);
-    }
   }
 
   /**
@@ -149,14 +147,12 @@ public class ServiceComponentHistoryEntity {
    */
   @Override
   public int hashCode() {
-    final int prime = 31;
-    int result = 1;
-    result = prime * result + ObjectUtils.hashCode(m_fromStack);
-    result = prime * result + (int) (m_id ^ (m_id >>> 32));
-    result = prime * result + ObjectUtils.hashCode(m_serviceComponentDesiredStateEntity);
-    result = prime * result + ObjectUtils.hashCode(m_toStack);
-    result = prime * result + ObjectUtils.hashCode(m_upgradeEntity);
-    return result;
+    HashCodeBuilder hashCodeBuilder = new HashCodeBuilder();
+    hashCodeBuilder.append(m_fromStack);
+    hashCodeBuilder.append(m_toStack);
+    hashCodeBuilder.append(m_upgradeEntity);
+    hashCodeBuilder.append(m_serviceComponentDesiredStateEntity);
+    return hashCodeBuilder.toHashCode();
   }
 
   /**
@@ -176,44 +172,10 @@ public class ServiceComponentHistoryEntity {
       return false;
     }
 
-    ServiceComponentHistoryEntity other = (ServiceComponentHistoryEntity) obj;
-    if (m_fromStack == null) {
-      if (other.m_fromStack != null) {
-        return false;
-      }
-    } else if (!m_fromStack.equals(other.m_fromStack)) {
-      return false;
-    }
-
-    if (m_id != other.m_id) {
-      return false;
-    }
-
-    if (m_serviceComponentDesiredStateEntity == null) {
-      if (other.m_serviceComponentDesiredStateEntity != null) {
-        return false;
-      }
-    } else if (!m_serviceComponentDesiredStateEntity.equals(
-        other.m_serviceComponentDesiredStateEntity)) {
-      return false;
-    }
-
-    if (m_toStack == null) {
-      if (other.m_toStack != null) {
-        return false;
-      }
-    } else if (!m_toStack.equals(other.m_toStack)) {
-      return false;
-    }
-
-    if (m_upgradeEntity == null) {
-      if (other.m_upgradeEntity != null) {
-        return false;
-      }
-    } else if (!m_upgradeEntity.equals(other.m_upgradeEntity)) {
-      return false;
-    }
-
-    return true;
+    final ServiceComponentHistoryEntity other = (ServiceComponentHistoryEntity) obj;
+    return Objects.equal(m_fromStack, other.m_fromStack)
+        && Objects.equal(m_toStack, other.m_toStack)
+        && Objects.equal(m_upgradeEntity, other.m_upgradeEntity) 
+        && Objects.equal(m_serviceComponentDesiredStateEntity, other.m_serviceComponentDesiredStateEntity);
   }
 }

http://git-wip-us.apache.org/repos/asf/ambari/blob/4720e51d/ambari-server/src/main/java/org/apache/ambari/server/serveraction/upgrades/FinalizeUpgradeAction.java
----------------------------------------------------------------------
diff --git a/ambari-server/src/main/java/org/apache/ambari/server/serveraction/upgrades/FinalizeUpgradeAction.java
b/ambari-server/src/main/java/org/apache/ambari/server/serveraction/upgrades/FinalizeUpgradeAction.java
index 9088c1c..a07d0e6 100644
--- a/ambari-server/src/main/java/org/apache/ambari/server/serveraction/upgrades/FinalizeUpgradeAction.java
+++ b/ambari-server/src/main/java/org/apache/ambari/server/serveraction/upgrades/FinalizeUpgradeAction.java
@@ -513,17 +513,20 @@ public class FinalizeUpgradeAction extends AbstractServerAction {
     for (Service service : cluster.getServices().values()) {
       for (ServiceComponent serviceComponent : service.getServiceComponents().values()) {
         if (serviceComponent.isVersionAdvertised()) {
+          // create the historical entry
           ServiceComponentHistoryEntity historyEntity = new ServiceComponentHistoryEntity();
           historyEntity.setUpgrade(upgradeEntity);
           historyEntity.setFromStack(fromStack);
           historyEntity.setToStack(toStack);
 
+          // get the service component
           ServiceComponentDesiredStateEntity desiredStateEntity = serviceComponentDesiredStateDAO.findByName(
               cluster.getClusterId(), serviceComponent.getServiceName(),
               serviceComponent.getName());
 
-          historyEntity.setServiceComponentDesiredState(desiredStateEntity);
-          serviceComponentDesiredStateDAO.create(historyEntity);
+          // add the history to the component and save
+          desiredStateEntity.addHistory(historyEntity);
+          serviceComponentDesiredStateDAO.merge(desiredStateEntity);
         }
       }
     }

http://git-wip-us.apache.org/repos/asf/ambari/blob/4720e51d/ambari-server/src/test/java/org/apache/ambari/server/state/ServiceComponentTest.java
----------------------------------------------------------------------
diff --git a/ambari-server/src/test/java/org/apache/ambari/server/state/ServiceComponentTest.java
b/ambari-server/src/test/java/org/apache/ambari/server/state/ServiceComponentTest.java
index 6b00616..22cf302 100644
--- a/ambari-server/src/test/java/org/apache/ambari/server/state/ServiceComponentTest.java
+++ b/ambari-server/src/test/java/org/apache/ambari/server/state/ServiceComponentTest.java
@@ -397,13 +397,22 @@ public class ServiceComponentTest {
     history.setFromStack(serviceComponentDesiredStateEntity.getDesiredStack());
     history.setToStack(serviceComponentDesiredStateEntity.getDesiredStack());
     history.setUpgrade(upgradeEntity);
-    history.setServiceComponentDesiredState(serviceComponentDesiredStateEntity);
-    history = serviceComponentDesiredStateDAO.merge(history);
+
+    serviceComponentDesiredStateEntity.addHistory(history);
+
+    serviceComponentDesiredStateEntity = serviceComponentDesiredStateDAO.merge(
+        serviceComponentDesiredStateEntity);
 
     serviceComponentDesiredStateEntity = serviceComponentDesiredStateDAO.findByName(
         cluster.getClusterId(), serviceName, componentName);
 
-    Assert.assertEquals(history, serviceComponentDesiredStateEntity.getHistory().iterator().next());
+    assertEquals(1, serviceComponentDesiredStateEntity.getHistory().size());
+    ServiceComponentHistoryEntity persistedHistory = serviceComponentDesiredStateEntity.getHistory().iterator().next();
+
+    assertEquals(history.getFromStack(), persistedHistory.getFromStack());
+    assertEquals(history.getToStack(), persistedHistory.getFromStack());
+    assertEquals(history.getUpgrade(), persistedHistory.getUpgrade());
+    assertEquals(history.getServiceComponentDesiredState(), persistedHistory.getServiceComponentDesiredState());
   }
 
 
@@ -508,12 +517,16 @@ public class ServiceComponentTest {
     history.setToStack(serviceComponentDesiredStateEntity.getDesiredStack());
     history.setUpgrade(upgradeEntity);
     history.setServiceComponentDesiredState(serviceComponentDesiredStateEntity);
-    history = serviceComponentDesiredStateDAO.merge(history);
+
+    serviceComponentDesiredStateEntity.addHistory(history);
+
+    serviceComponentDesiredStateEntity = serviceComponentDesiredStateDAO.merge(
+        serviceComponentDesiredStateEntity);
 
     serviceComponentDesiredStateEntity = serviceComponentDesiredStateDAO.findByName(
         cluster.getClusterId(), serviceName, componentName);
 
-    Assert.assertEquals(history, serviceComponentDesiredStateEntity.getHistory().iterator().next());
+    assertEquals(1, serviceComponentDesiredStateEntity.getHistory().size());
 
     // verify that we can retrieve the history directly
     List<ServiceComponentHistoryEntity> componentHistoryList = serviceComponentDesiredStateDAO.findHistory(


Mime
View raw message