ambari-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From pallav...@apache.org
Subject ambari git commit: AMBARI-16242. Hive View and Pig View : one user overriding job details of other user in database (Nitiraj Rathore via pallavkul)
Date Fri, 20 May 2016 11:56:33 GMT
Repository: ambari
Updated Branches:
  refs/heads/branch-2.4 c96eb05f3 -> e37391903


AMBARI-16242. Hive View and Pig View : one user overriding job details of other user in database
(Nitiraj Rathore via pallavkul)


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

Branch: refs/heads/branch-2.4
Commit: e37391903bd8478720cce3287dc10dfe0c79ff65
Parents: c96eb05
Author: Pallav Kulshreshtha <pallav.kul@gmail.com>
Authored: Fri May 20 17:24:54 2016 +0530
Committer: Pallav Kulshreshtha <pallav.kul@gmail.com>
Committed: Fri May 20 17:26:03 2016 +0530

----------------------------------------------------------------------
 .../server/upgrade/UpgradeCatalog240.java       | 71 ++++++++++++++++++++
 .../server/view/persistence/DataStoreImpl.java  | 42 +++++++-----
 .../view/hive/persistence/DataStoreStorage.java | 40 ++---------
 .../view/hive/resources/jobs/JobService.java    |  3 +
 .../view/pig/persistence/DataStoreStorage.java  | 24 ++-----
 5 files changed, 109 insertions(+), 71 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/ambari/blob/e3739190/ambari-server/src/main/java/org/apache/ambari/server/upgrade/UpgradeCatalog240.java
----------------------------------------------------------------------
diff --git a/ambari-server/src/main/java/org/apache/ambari/server/upgrade/UpgradeCatalog240.java
b/ambari-server/src/main/java/org/apache/ambari/server/upgrade/UpgradeCatalog240.java
index 8a2ccc6..51b3292 100644
--- a/ambari-server/src/main/java/org/apache/ambari/server/upgrade/UpgradeCatalog240.java
+++ b/ambari-server/src/main/java/org/apache/ambari/server/upgrade/UpgradeCatalog240.java
@@ -61,6 +61,7 @@ import org.apache.ambari.server.orm.entities.ResourceEntity;
 import org.apache.ambari.server.orm.entities.ResourceTypeEntity;
 import org.apache.ambari.server.orm.entities.RoleAuthorizationEntity;
 import org.apache.ambari.server.orm.entities.UserEntity;
+import org.apache.ambari.server.orm.entities.ViewEntityEntity;
 import org.apache.ambari.server.orm.entities.WidgetEntity;
 import org.apache.ambari.server.security.authorization.ResourceType;
 import org.apache.ambari.server.state.AlertFirmness;
@@ -93,6 +94,10 @@ import com.google.inject.Inject;
 import com.google.inject.Injector;
 import com.google.inject.persist.Transactional;
 
+import javax.persistence.EntityManager;
+import javax.persistence.Query;
+import javax.persistence.TypedQuery;
+
 /**
  * Upgrade catalog for version 2.4.0.
  */
@@ -318,6 +323,7 @@ public class UpgradeCatalog240 extends AbstractUpgradeCatalog {
     updateHIVEConfigs();
     updateAMSConfigs();
     updateClusterEnv();
+    updateSequenceForView();
     updateHostRoleCommandTableDML();
     updateKerberosConfigs();
     updateYarnEnv();
@@ -348,6 +354,71 @@ public class UpgradeCatalog240 extends AbstractUpgradeCatalog {
     principalEntity.setPrincipalType(principalTypeEntity);
     principalDAO.create(principalEntity);
   }
+  private static final String NAME_PREFIX = "DS_";
+
+  private String getEntityName(ViewEntityEntity entity) {
+    String className = entity.getClassName();
+    String[] parts = className.split("\\.");
+    String simpleClassName = parts[parts.length - 1];
+
+    if (entity.getViewInstance().alterNames()) {
+      return NAME_PREFIX + simpleClassName + "_" + entity.getId();
+    }
+    return simpleClassName + entity.getId();
+  }
+
+  /**
+   * get all entries of viewentity
+   * find all the table names by parsing class_name
+   * create all the sequence names by appending _id_seq
+   * query each dynamic table to find the max of id
+   * insert into ambari_sequence name and counter for each item
+   */
+  private void updateSequenceForView() {
+    LOG.info("updateSequenceForView called.");
+    EntityManager entityManager = getEntityManagerProvider().get();
+    TypedQuery<ViewEntityEntity> viewEntityQuery = entityManager.createQuery("SELECT
vee FROM ViewEntityEntity vee", ViewEntityEntity.class);
+    List<ViewEntityEntity> viewEntities = viewEntityQuery.getResultList();
+    LOG.info("Received view Entities : {}, length : {}", viewEntities, viewEntities.size());
+
+    // as the id fields are string in these entities we will have to get all ids and convert
to int and find max.
+    String selectIdsFormat = "select %s from %s";
+    String insertQuery = "insert into ambari_sequences values ('%s',%d)";
+    for (ViewEntityEntity viewEntity : viewEntities) {
+      LOG.info("Working with viewEntity : {} : {} ", viewEntity, viewEntity.getViewName()
+ viewEntity.getViewInstance());
+      String tableName = getEntityName(viewEntity);
+      String seqName = tableName.toLowerCase() + "_id_seq";
+      try {
+        entityManager.getTransaction().begin();
+        String selectIdsQueryString = String.format(selectIdsFormat, NAME_PREFIX + viewEntity.getIdProperty(),
tableName).toLowerCase();
+        LOG.info("executing max query string {}", selectIdsQueryString);
+        Query selectIdsQuery = entityManager.createNativeQuery(selectIdsQueryString);
+        List<String> ids = selectIdsQuery.getResultList();
+        LOG.info("Received ids : {}", ids);
+        int maxId = 0;
+        if (null != ids && ids.size() != 0) {
+          for (String id : ids) {
+            try {
+              Integer intId = Integer.parseInt(id);
+              maxId = Math.max(intId, maxId);
+            } catch (NumberFormatException e) {
+              LOG.error("the id was non integer : id : {}. So ignoring.", id);
+            }
+          }
+        }
+
+        String insertQueryString = String.format(insertQuery, seqName, maxId).toLowerCase();
+        LOG.info("Executing insert query : {}", insertQueryString);
+        Query insertQ = entityManager.createNativeQuery(insertQueryString);
+        int rowsChanged = insertQ.executeUpdate();
+        entityManager.getTransaction().commit();
+        LOG.info("executing insert resulted in {} row changes.", rowsChanged);
+      } catch (Exception e) { // when the entity table is not yet created or other exception.
+        entityManager.getTransaction().rollback();
+        LOG.error("Error --> can be ignored.", e);
+      }
+    }
+  }
 
   private void createSettingTable() throws SQLException {
     List<DBColumnInfo> columns = new ArrayList<>();

http://git-wip-us.apache.org/repos/asf/ambari/blob/e3739190/ambari-server/src/main/java/org/apache/ambari/server/view/persistence/DataStoreImpl.java
----------------------------------------------------------------------
diff --git a/ambari-server/src/main/java/org/apache/ambari/server/view/persistence/DataStoreImpl.java
b/ambari-server/src/main/java/org/apache/ambari/server/view/persistence/DataStoreImpl.java
index 0ed260d..039fd6f 100644
--- a/ambari-server/src/main/java/org/apache/ambari/server/view/persistence/DataStoreImpl.java
+++ b/ambari-server/src/main/java/org/apache/ambari/server/view/persistence/DataStoreImpl.java
@@ -25,10 +25,14 @@ import org.apache.ambari.view.PersistenceException;
 import org.eclipse.persistence.dynamic.DynamicClassLoader;
 import org.eclipse.persistence.dynamic.DynamicEntity;
 import org.eclipse.persistence.dynamic.DynamicType;
+import org.eclipse.persistence.exceptions.DynamicException;
 import org.eclipse.persistence.internal.helper.DatabaseField;
+import org.eclipse.persistence.jpa.JpaHelper;
 import org.eclipse.persistence.jpa.dynamic.JPADynamicHelper;
 import org.eclipse.persistence.jpa.dynamic.JPADynamicTypeBuilder;
 import org.eclipse.persistence.mappings.DirectToFieldMapping;
+import org.eclipse.persistence.sequencing.TableSequence;
+import org.eclipse.persistence.sessions.Session;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
@@ -44,14 +48,7 @@ import java.lang.reflect.Field;
 import java.lang.reflect.InvocationTargetException;
 import java.lang.reflect.Method;
 import java.lang.reflect.ParameterizedType;
-import java.util.Collection;
-import java.util.HashMap;
-import java.util.HashSet;
-import java.util.LinkedHashMap;
-import java.util.List;
-import java.util.Map;
-import java.util.Set;
-import java.util.StringTokenizer;
+import java.util.*;
 
 
 /**
@@ -141,8 +138,20 @@ public class DataStoreImpl implements DataStore {
     try {
       em.getTransaction().begin();
       try {
-        persistEntity(entity, em, new HashSet<DynamicEntity>());
+        DynamicEntity dynamicEntity = persistEntity(entity, em, new HashSet<DynamicEntity>());
         em.getTransaction().commit();
+        Map<String, Object> props = getEntityProperties(entity);
+        List<String> keys = new ArrayList<String>(props.keySet());
+        for( String key : keys){
+          String attribute = getAttributeName(key);
+          try {
+            props.put(key, dynamicEntity.get(attribute));
+          }catch(DynamicException de){
+            LOG.debug("Error occurred while copying entity property : {} : {}", key, de);
+            // ignore - the property was not found in Dynamic entity.
+          }
+        }
+        setEntityProperties(entity,props);
       } catch (Exception e) {
         rollbackTransaction(em.getTransaction());
         throwPersistenceException("Caught exception trying to store view entity " + entity,
e);
@@ -283,19 +292,22 @@ public class DataStoreImpl implements DataStore {
       typeBuilderMap.put(entityName, typeBuilder);
     }
 
+    Session session = JpaHelper.getEntityManager(getEntityManager()).getServerSession();
     // add the direct mapped properties to the dynamic type builders
     for (Map.Entry<Class, String> entry: entityClassMap.entrySet()) {
 
       Class                 clazz       = entry.getKey();
       String                entityName  = entry.getValue();
       JPADynamicTypeBuilder typeBuilder = typeBuilderMap.get(entityName);
+      String seqName = new String(entityName + "_id_seq").toLowerCase();
+      TableSequence tableSequence = new TableSequence(seqName,50, "ambari_sequences", "sequence_name","sequence_value");
+      session.getLogin().addSequence(tableSequence);
 
       Map<String, PropertyDescriptor> descriptorMap = getDescriptorMap(clazz);
 
       long totalLength = 0L;
 
       for (Map.Entry<String, PropertyDescriptor> descriptorEntry : descriptorMap.entrySet())
{
-
         String fieldName     = descriptorEntry.getKey();
         String attributeName = getAttributeName(fieldName);
 
@@ -303,6 +315,7 @@ public class DataStoreImpl implements DataStore {
 
         if (fieldName.equals(entityMap.get(entityName).getIdProperty())) {
           typeBuilder.setPrimaryKeyFields(attributeName);
+          typeBuilder.configureSequencing(tableSequence,seqName,attributeName);
         }
 
         Class<?> propertyType = descriptor.getPropertyType();
@@ -340,12 +353,7 @@ public class DataStoreImpl implements DataStore {
         String fieldName     = descriptorEntry.getKey();
         String attributeName = getAttributeName(fieldName);
 
-
         PropertyDescriptor descriptor = descriptorEntry.getValue();
-        if (fieldName.equals(entityMap.get(entityName).getIdProperty())) {
-          typeBuilder.setPrimaryKeyFields(attributeName);
-        }
-
         Class<?> propertyType = descriptor.getPropertyType();
         String refEntityName = entityClassMap.get(propertyType);
 
@@ -398,7 +406,9 @@ public class DataStoreImpl implements DataStore {
     DynamicType type = getDynamicEntityType(clazz);
 
     if (type != null) {
-      dynamicEntity  = em.find(type.getJavaClass(), properties.get(id));
+      if (null != properties.get(id)) {
+        dynamicEntity = em.find(type.getJavaClass(), properties.get(id));
+      }
 
       boolean create = dynamicEntity == null;
 

http://git-wip-us.apache.org/repos/asf/ambari/blob/e3739190/contrib/views/hive/src/main/java/org/apache/ambari/view/hive/persistence/DataStoreStorage.java
----------------------------------------------------------------------
diff --git a/contrib/views/hive/src/main/java/org/apache/ambari/view/hive/persistence/DataStoreStorage.java
b/contrib/views/hive/src/main/java/org/apache/ambari/view/hive/persistence/DataStoreStorage.java
index 1e8f07f..5457a5c 100644
--- a/contrib/views/hive/src/main/java/org/apache/ambari/view/hive/persistence/DataStoreStorage.java
+++ b/contrib/views/hive/src/main/java/org/apache/ambari/view/hive/persistence/DataStoreStorage.java
@@ -56,36 +56,17 @@ public class DataStoreStorage implements Storage {
 
   @Override
   public synchronized void store(Class model, Indexed obj) {
-    assignId(model, obj);
-
-    Indexed newBean;
-    try {
-      newBean = (Indexed) BeanUtils.cloneBean(obj);
-    } catch (IllegalAccessException e) {
-      throw new ServiceFormattedException("S010 Data storage error", e);
-    } catch (InstantiationException e) {
-      throw new ServiceFormattedException("S010 Data storage error", e);
-    } catch (InvocationTargetException e) {
-      throw new ServiceFormattedException("S010 Data storage error", e);
-    } catch (NoSuchMethodException e) {
-      throw new ServiceFormattedException("S010 Data storage error", e);
-    }
-    preprocessEntity(newBean);
 
     try {
+      Indexed newBean = (Indexed) BeanUtils.cloneBean(obj);
+      preprocessEntity(newBean);
       context.getDataStore().store(newBean);
-    } catch (PersistenceException e) {
+      obj.setId(newBean.getId());
+    } catch (Exception e) {
       throw new ServiceFormattedException("S020 Data storage error", e);
     }
   }
 
-  public void assignId(Class model, Indexed obj) {
-    if (obj.getId() == null) {
-      String id = nextIdForEntity(context, model);
-      obj.setId(id);
-    }
-  }
-
   private void preprocessEntity(Indexed obj) {
     cleanTransientFields(obj);
   }
@@ -105,19 +86,6 @@ public class DataStoreStorage implements Storage {
     }
   }
 
-  private static synchronized String nextIdForEntity(ViewContext context, Class aClass) {
-    // auto increment id implementation
-    String lastId = context.getInstanceData(aClass.getName());
-    int newId;
-    if (lastId == null) {
-      newId = 1;
-    } else {
-      newId = Integer.parseInt(lastId) + 1;
-    }
-    context.putInstanceData(aClass.getName(), String.valueOf(newId));
-    return String.valueOf(newId);
-  }
-
   @Override
   public synchronized <T extends Indexed> T load(Class<T> model, Object id) throws
ItemNotFound {
     LOG.debug(String.format("Loading %s #%s", model.getName(), id));

http://git-wip-us.apache.org/repos/asf/ambari/blob/e3739190/contrib/views/hive/src/main/java/org/apache/ambari/view/hive/resources/jobs/JobService.java
----------------------------------------------------------------------
diff --git a/contrib/views/hive/src/main/java/org/apache/ambari/view/hive/resources/jobs/JobService.java
b/contrib/views/hive/src/main/java/org/apache/ambari/view/hive/resources/jobs/JobService.java
index f7f883b..34f060d 100644
--- a/contrib/views/hive/src/main/java/org/apache/ambari/view/hive/resources/jobs/JobService.java
+++ b/contrib/views/hive/src/main/java/org/apache/ambari/view/hive/resources/jobs/JobService.java
@@ -438,10 +438,13 @@ public class JobService extends BaseService {
 
       return Response.ok(jobObject).status(201).build();
     } catch (WebApplicationException ex) {
+      LOG.error("Error occurred while creating job : ",ex);
       throw ex;
     } catch (ItemNotFound itemNotFound) {
+      LOG.error("Error occurred while creating job : ",itemNotFound);
       throw new NotFoundFormattedException(itemNotFound.getMessage(), itemNotFound);
     } catch (Exception ex) {
+      LOG.error("Error occurred while creating job : ",ex);
       throw new ServiceFormattedException(ex.getMessage(), ex);
     }
   }

http://git-wip-us.apache.org/repos/asf/ambari/blob/e3739190/contrib/views/pig/src/main/java/org/apache/ambari/view/pig/persistence/DataStoreStorage.java
----------------------------------------------------------------------
diff --git a/contrib/views/pig/src/main/java/org/apache/ambari/view/pig/persistence/DataStoreStorage.java
b/contrib/views/pig/src/main/java/org/apache/ambari/view/pig/persistence/DataStoreStorage.java
index 7ae7721..d3cc676 100644
--- a/contrib/views/pig/src/main/java/org/apache/ambari/view/pig/persistence/DataStoreStorage.java
+++ b/contrib/views/pig/src/main/java/org/apache/ambari/view/pig/persistence/DataStoreStorage.java
@@ -22,6 +22,7 @@ import org.apache.ambari.view.PersistenceException;
 import org.apache.ambari.view.ViewContext;
 import org.apache.ambari.view.pig.persistence.utils.*;
 import org.apache.ambari.view.pig.utils.ServiceFormattedException;
+import org.apache.commons.beanutils.BeanUtils;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
@@ -48,29 +49,14 @@ public class DataStoreStorage implements Storage {
   @Override
   public synchronized void store(Indexed obj) {
     try {
-      if (obj.getId() == null) {
-        int id = nextIdForEntity(context, obj.getClass());
-        obj.setId(String.valueOf(id));
-      }
-      context.getDataStore().store(obj);
-    } catch (PersistenceException e) {
+      Indexed newBean = (Indexed) BeanUtils.cloneBean(obj);
+      context.getDataStore().store(newBean);
+      obj.setId(newBean.getId());
+    } catch (Exception e) {
       throw new ServiceFormattedException("Error while saving object to DataStorage", e);
     }
   }
 
-  private static synchronized int nextIdForEntity(ViewContext context, Class aClass) {
-    // auto increment id implementation
-    String lastId = context.getInstanceData(aClass.getName());
-    int newId;
-    if (lastId == null) {
-      newId = 1;
-    } else {
-      newId = Integer.parseInt(lastId) + 1;
-    }
-    context.putInstanceData(aClass.getName(), String.valueOf(newId));
-    return newId;
-  }
-
   @Override
   public synchronized <T extends Indexed> T load(Class<T> model, int id) throws
ItemNotFound {
     LOG.debug(String.format("Loading %s #%d", model.getName(), id));


Mime
View raw message