This is an automated email from the ASF dual-hosted git repository. krisztiankasa pushed a commit to branch branch-2.7 in repository https://gitbox.apache.org/repos/asf/ambari.git
The following commit(s) were added to refs/heads/branch-2.7 by this push: new 0cc4ef0 AMBARI-24254 - Refactor renaming service ambari infra to ambari infra solr when upgrading to ambari 2.7.0 (#1695) 0cc4ef0 is described below commit 0cc4ef0c49681e8c61adce99c7cadec1ccd83550 Author: kasakrisz <33458261+kasakr...@users.noreply.github.com> AuthorDate: Fri Jul 6 18:26:52 2018 +0200 AMBARI-24254 - Refactor renaming service ambari infra to ambari infra solr when upgrading to ambari 2.7.0 (#1695) --- .../ambari/server/upgrade/UpgradeCatalog270.java | 109 +++++++++++++++------ 1 file changed, 81 insertions(+), 28 deletions(-) diff --git a/ambari-server/src/main/java/org/apache/ambari/server/upgrade/UpgradeCatalog270.java b/ambari-server/src/main/java/org/apache/ambari/server/upgrade/UpgradeCatalog270.java index 7982b8e..43bbc87 100644 --- a/ambari-server/src/main/java/org/apache/ambari/server/upgrade/UpgradeCatalog270.java +++ b/ambari-server/src/main/java/org/apache/ambari/server/upgrade/UpgradeCatalog270.java @@ -56,16 +56,25 @@ import org.apache.ambari.server.controller.internal.CalculatedStatus; import org.apache.ambari.server.orm.DBAccessor; import org.apache.ambari.server.orm.dao.AmbariConfigurationDAO; import org.apache.ambari.server.orm.dao.ArtifactDAO; +import org.apache.ambari.server.orm.dao.ClusterServiceDAO; import org.apache.ambari.server.orm.dao.DaoUtils; +import org.apache.ambari.server.orm.dao.HostComponentDesiredStateDAO; import org.apache.ambari.server.orm.dao.HostComponentStateDAO; import org.apache.ambari.server.orm.dao.RequestDAO; +import org.apache.ambari.server.orm.dao.ServiceComponentDesiredStateDAO; +import org.apache.ambari.server.orm.dao.ServiceDesiredStateDAO; import org.apache.ambari.server.orm.entities.AlertDefinitionEntity; import org.apache.ambari.server.orm.entities.AlertGroupEntity; import org.apache.ambari.server.orm.entities.AlertHistoryEntity; import org.apache.ambari.server.orm.entities.ArtifactEntity; +import org.apache.ambari.server.orm.entities.ClusterServiceEntity; +import org.apache.ambari.server.orm.entities.ClusterServiceEntityPK; +import org.apache.ambari.server.orm.entities.HostComponentDesiredStateEntity; import org.apache.ambari.server.orm.entities.HostComponentStateEntity; import org.apache.ambari.server.orm.entities.RequestEntity; +import org.apache.ambari.server.orm.entities.ServiceComponentDesiredStateEntity; import org.apache.ambari.server.orm.entities.ServiceConfigEntity; +import org.apache.ambari.server.orm.entities.ServiceDesiredStateEntity; import org.apache.ambari.server.orm.entities.StageEntity; import org.apache.ambari.server.security.authorization.UserAuthenticationType; import org.apache.ambari.server.serveraction.kerberos.KerberosServerAction; @@ -1055,30 +1064,8 @@ public class UpgradeCatalog270 extends AbstractUpgradeCatalog { updateStormConfigs(); } - protected void renameAmbariInfra() throws SQLException { + protected void renameAmbariInfra() { LOG.info("Renaming service AMBARI_INFRA to AMBARI_INFRA_SOLR"); - dbAccessor.dropFKConstraint(SERVICE_COMPONENT_DESIRED_STATE_TABLE, SERVICE_COMPONENT_DESIRED_STATES_CLUSTER_SERVICES_FK); - dbAccessor.dropFKConstraint(SERVICE_DESIRED_STATE_TABLE, SERVICE_DESIRED_STATE_CLUSTER_SERVICES_FK); - dbAccessor.dropFKConstraint(COMPONENT_DESIRED_STATE_TABLE, COMPONENT_DESIRED_STATE_SERVICE_COMPONENT_DESIRED_STATE_FK); - dbAccessor.dropFKConstraint(COMPONENT_STATE_TABLE, COMPONENT_STATE_SERVICE_COMPONENT_DESIRED_STATE_FK); - try { - dbAccessor.updateTable(SERVICE_COMPONENT_DESIRED_STATE_TABLE, SERVICE_NAME_COLUMN, AMBARI_INFRA_NEW_NAME, String.format("WHERE %s = '%s'", SERVICE_NAME_COLUMN, AMBARI_INFRA_OLD_NAME)); - dbAccessor.updateTable(COMPONENT_DESIRED_STATE_TABLE, SERVICE_NAME_COLUMN, AMBARI_INFRA_NEW_NAME, String.format("WHERE %s = '%s'", SERVICE_NAME_COLUMN, AMBARI_INFRA_OLD_NAME)); - dbAccessor.updateTable(COMPONENT_STATE_TABLE, SERVICE_NAME_COLUMN, AMBARI_INFRA_NEW_NAME, String.format("WHERE %s = '%s'", SERVICE_NAME_COLUMN, AMBARI_INFRA_OLD_NAME)); - dbAccessor.updateTable(SERVICE_DESIRED_STATE_TABLE, SERVICE_NAME_COLUMN, AMBARI_INFRA_NEW_NAME, String.format("WHERE %s = '%s'", SERVICE_NAME_COLUMN, AMBARI_INFRA_OLD_NAME)); - dbAccessor.updateTable(CLUSTER_SERVICES_TABLE, SERVICE_NAME_COLUMN, AMBARI_INFRA_NEW_NAME, String.format("WHERE %s = '%s'", SERVICE_NAME_COLUMN, AMBARI_INFRA_OLD_NAME)); - } finally { - dbAccessor.addFKConstraint(SERVICE_COMPONENT_DESIRED_STATE_TABLE, SERVICE_COMPONENT_DESIRED_STATES_CLUSTER_SERVICES_FK, - SERVICE_NAME_CLUSTER_ID_KEY_COLUMNS, CLUSTER_SERVICES_TABLE, SERVICE_NAME_CLUSTER_ID_KEY_COLUMNS, false); - dbAccessor.addFKConstraint(SERVICE_DESIRED_STATE_TABLE, SERVICE_DESIRED_STATE_CLUSTER_SERVICES_FK, - SERVICE_NAME_CLUSTER_ID_KEY_COLUMNS, CLUSTER_SERVICES_TABLE, SERVICE_NAME_CLUSTER_ID_KEY_COLUMNS, false); - dbAccessor.addFKConstraint(COMPONENT_DESIRED_STATE_TABLE, COMPONENT_DESIRED_STATE_SERVICE_COMPONENT_DESIRED_STATE_FK, - COMPONENT_NAME_SERVICE_NAME_CLUSTER_ID_KEY_COLUMNS, SERVICE_COMPONENT_DESIRED_STATE_TABLE, COMPONENT_NAME_SERVICE_NAME_CLUSTER_ID_KEY_COLUMNS, false); - dbAccessor.addFKConstraint(COMPONENT_STATE_TABLE, COMPONENT_STATE_SERVICE_COMPONENT_DESIRED_STATE_FK, - COMPONENT_NAME_SERVICE_NAME_CLUSTER_ID_KEY_COLUMNS, SERVICE_COMPONENT_DESIRED_STATE_TABLE, COMPONENT_NAME_SERVICE_NAME_CLUSTER_ID_KEY_COLUMNS, false); - } - - AmbariManagementController ambariManagementController = injector.getInstance(AmbariManagementController.class); Clusters clusters = ambariManagementController.getClusters(); if (clusters == null) @@ -1088,8 +1075,74 @@ public class UpgradeCatalog270 extends AbstractUpgradeCatalog { if (MapUtils.isEmpty(clusterMap)) return; + EntityManager entityManager = getEntityManagerProvider().get(); + ClusterServiceDAO clusterServiceDAO = injector.getInstance(ClusterServiceDAO.class); + HostComponentStateDAO hostComponentStateDAO = injector.getInstance(HostComponentStateDAO.class); + HostComponentDesiredStateDAO hostComponentDesiredStateDAO = injector.getInstance(HostComponentDesiredStateDAO.class); + ServiceDesiredStateDAO serviceDesiredStateDAO = injector.getInstance(ServiceDesiredStateDAO.class); + ServiceComponentDesiredStateDAO serviceComponentDesiredStateDAO = injector.getInstance(ServiceComponentDesiredStateDAO.class); + + for (final Cluster cluster : clusterMap.values()) { + ClusterServiceEntityPK clusterServiceEntityPK = new ClusterServiceEntityPK(); + clusterServiceEntityPK.setClusterId(cluster.getClusterId()); + clusterServiceEntityPK.setServiceName(AMBARI_INFRA_OLD_NAME); + ClusterServiceEntity clusterServiceEntity = clusterServiceDAO.findByPK(clusterServiceEntityPK); + if (clusterServiceEntity == null) + continue; + + List<ServiceComponentDesiredStateEntity> serviceComponentDesiredStateEntities = + new ArrayList<>(clusterServiceEntity.getServiceComponentDesiredStateEntities()); + ServiceDesiredStateEntity serviceDesiredStateEntity = clusterServiceEntity.getServiceDesiredStateEntity(); + List<HostComponentStateEntity> hostComponentStateEntities = hostComponentStateDAO.findByService(AMBARI_INFRA_OLD_NAME); + List<HostComponentDesiredStateEntity> hostComponentDesiredStateEntities = new ArrayList<>(); + for (ServiceComponentDesiredStateEntity serviceComponentDesiredStateEntity : clusterServiceEntity.getServiceComponentDesiredStateEntities()) { + hostComponentDesiredStateEntities.addAll( + hostComponentDesiredStateDAO.findByIndex(cluster.getClusterId(), AMBARI_INFRA_OLD_NAME, serviceComponentDesiredStateEntity.getComponentName())); + } + + for (HostComponentStateEntity hostComponentStateEntity : hostComponentStateEntities) { + hostComponentStateDAO.remove(hostComponentStateEntity); + entityManager.detach(hostComponentStateEntity); + hostComponentStateEntity.setServiceName(AMBARI_INFRA_NEW_NAME); + } + + for (HostComponentDesiredStateEntity hostComponentDesiredStateEntity : hostComponentDesiredStateEntities) { + hostComponentDesiredStateDAO.remove(hostComponentDesiredStateEntity); + entityManager.detach(hostComponentDesiredStateEntity); + hostComponentDesiredStateEntity.setServiceName(AMBARI_INFRA_NEW_NAME); + } + + clusterServiceEntity.getServiceComponentDesiredStateEntities().clear(); + for (ServiceComponentDesiredStateEntity serviceComponentDesiredStateEntity : serviceComponentDesiredStateEntities) { + serviceComponentDesiredStateDAO.remove(serviceComponentDesiredStateEntity); + entityManager.detach(serviceComponentDesiredStateEntity); + serviceComponentDesiredStateEntity.setServiceName(AMBARI_INFRA_NEW_NAME); + } + + if (serviceDesiredStateEntity != null) { + clusterServiceEntity.setServiceDesiredStateEntity(null); + serviceDesiredStateDAO.remove(serviceDesiredStateEntity); + entityManager.detach(serviceDesiredStateEntity); + serviceDesiredStateEntity.setServiceName(AMBARI_INFRA_NEW_NAME); + } + + clusterServiceDAO.remove(clusterServiceEntity); + entityManager.detach(clusterServiceEntity); + + clusterServiceEntity.setServiceName(AMBARI_INFRA_NEW_NAME); + clusterServiceEntity.setServiceDesiredStateEntity(serviceDesiredStateEntity); + clusterServiceDAO.create(clusterServiceEntity); + + for (ServiceComponentDesiredStateEntity serviceComponentDesiredStateEntity : serviceComponentDesiredStateEntities) + serviceComponentDesiredStateDAO.create(serviceComponentDesiredStateEntity); + for (HostComponentStateEntity hostComponentStateEntity : hostComponentStateEntities) + hostComponentStateDAO.create(hostComponentStateEntity); + for (HostComponentDesiredStateEntity hostComponentDesiredStateEntity : hostComponentDesiredStateEntities) + hostComponentDesiredStateDAO.create(hostComponentDesiredStateEntity); + } + executeInTransaction(() -> { - TypedQuery<ServiceConfigEntity> serviceConfigUpdate = getEntityManagerProvider().get().createQuery( + TypedQuery<ServiceConfigEntity> serviceConfigUpdate = entityManager.createQuery( "UPDATE ServiceConfigEntity SET serviceName = :newServiceName WHERE serviceName = :oldServiceName", ServiceConfigEntity.class); serviceConfigUpdate.setParameter("newServiceName", AMBARI_INFRA_NEW_NAME); serviceConfigUpdate.setParameter("oldServiceName", AMBARI_INFRA_OLD_NAME); @@ -1098,7 +1151,7 @@ public class UpgradeCatalog270 extends AbstractUpgradeCatalog { executeInTransaction(() -> { for (final Cluster cluster : clusterMap.values()) { - TypedQuery<AlertDefinitionEntity> alertDefinitionUpdate = getEntityManagerProvider().get().createQuery( + TypedQuery<AlertDefinitionEntity> alertDefinitionUpdate = entityManager.createQuery( "UPDATE AlertDefinitionEntity SET serviceName = :newServiceName WHERE serviceName = :oldServiceName AND clusterId = :clusterId", AlertDefinitionEntity.class); alertDefinitionUpdate.setParameter("clusterId", cluster.getClusterId()); alertDefinitionUpdate.setParameter("newServiceName", AMBARI_INFRA_NEW_NAME); @@ -1108,21 +1161,21 @@ public class UpgradeCatalog270 extends AbstractUpgradeCatalog { }); executeInTransaction(() -> { - TypedQuery<AlertGroupEntity> alertGroupUpdate = getEntityManagerProvider().get().createQuery("UPDATE AlertGroupEntity SET serviceName = :newServiceName, groupName = :newServiceName WHERE serviceName = :oldServiceName", AlertGroupEntity.class); + TypedQuery<AlertGroupEntity> alertGroupUpdate = entityManager.createQuery("UPDATE AlertGroupEntity SET serviceName = :newServiceName, groupName = :newServiceName WHERE serviceName = :oldServiceName", AlertGroupEntity.class); alertGroupUpdate.setParameter("newServiceName", AMBARI_INFRA_NEW_NAME); alertGroupUpdate.setParameter("oldServiceName", AMBARI_INFRA_OLD_NAME); alertGroupUpdate.executeUpdate(); }); executeInTransaction(() -> { - TypedQuery<AlertHistoryEntity> alertHistoryUpdate = getEntityManagerProvider().get().createQuery("UPDATE AlertHistoryEntity SET serviceName = :newServiceName WHERE serviceName = :oldServiceName", AlertHistoryEntity.class); + TypedQuery<AlertHistoryEntity> alertHistoryUpdate = entityManager.createQuery("UPDATE AlertHistoryEntity SET serviceName = :newServiceName WHERE serviceName = :oldServiceName", AlertHistoryEntity.class); alertHistoryUpdate.setParameter("newServiceName", AMBARI_INFRA_NEW_NAME); alertHistoryUpdate.setParameter("oldServiceName", AMBARI_INFRA_OLD_NAME); alertHistoryUpdate.executeUpdate(); }); // Force the clusters object to reload to ensure the renamed service is accounted for - getEntityManagerProvider().get().getEntityManagerFactory().getCache().evictAll(); + entityManager.getEntityManagerFactory().getCache().evictAll(); clusters.invalidateAllClusters(); }