You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@ambari.apache.org by hi...@apache.org on 2012/09/13 00:22:12 UTC

svn commit: r1384149 [3/3] - in /incubator/ambari/branches/AMBARI-666: ./ ambari-project/ ambari-server/ ambari-server/src/main/java/org/apache/ambari/server/orm/ ambari-server/src/main/java/org/apache/ambari/server/orm/dao/ ambari-server/src/main/java...

Added: incubator/ambari/branches/AMBARI-666/ambari-server/src/main/resources/META-INF/persistence.xml
URL: http://svn.apache.org/viewvc/incubator/ambari/branches/AMBARI-666/ambari-server/src/main/resources/META-INF/persistence.xml?rev=1384149&view=auto
==============================================================================
--- incubator/ambari/branches/AMBARI-666/ambari-server/src/main/resources/META-INF/persistence.xml (added)
+++ incubator/ambari/branches/AMBARI-666/ambari-server/src/main/resources/META-INF/persistence.xml Wed Sep 12 22:22:10 2012
@@ -0,0 +1,72 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!--
+   Licensed to the Apache Software Foundation (ASF) under one or more
+   contributor license agreements.  See the NOTICE file distributed with
+   this work for additional information regarding copyright ownership.
+   The ASF licenses this file to You under the Apache License, Version 2.0
+   (the "License"); you may not use this file except in compliance with
+   the License.  You may obtain a copy of the License at
+
+       http://www.apache.org/licenses/LICENSE-2.0
+
+   Unless required by applicable law or agreed to in writing, software
+   distributed under the License is distributed on an "AS IS" BASIS,
+   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+   See the License for the specific language governing permissions and
+   limitations under the License.
+-->
+<persistence xmlns="http://java.sun.com/xml/ns/persistence" version="2.0">
+
+  <persistence-unit name="ambari-postgres">
+    <class>org.apache.ambari.server.orm.entities.ActionStatusEntity</class>
+    <class>org.apache.ambari.server.orm.entities.ClusterEntity</class>
+    <class>org.apache.ambari.server.orm.entities.ClusterServiceEntity</class>
+    <class>org.apache.ambari.server.orm.entities.ClusterStateEntity</class>
+    <class>org.apache.ambari.server.orm.entities.ComponentHostDesiredStateEntity</class>
+    <class>org.apache.ambari.server.orm.entities.HostComponentStateEntity</class>
+    <class>org.apache.ambari.server.orm.entities.HostComponentMappingEntity</class>
+    <class>org.apache.ambari.server.orm.entities.HostEntity</class>
+    <class>org.apache.ambari.server.orm.entities.HostStateEntity</class>
+    <class>org.apache.ambari.server.orm.entities.ServiceComponentConfigEntity</class>
+    <class>org.apache.ambari.server.orm.entities.ServiceComponentHostConfigEntity</class>
+    <class>org.apache.ambari.server.orm.entities.ServiceComponentStateEntity</class>
+    <class>org.apache.ambari.server.orm.entities.ServiceConfigEntity</class>
+    <class>org.apache.ambari.server.orm.entities.ServiceDesiredStateEntity</class>
+    <class>org.apache.ambari.server.orm.entities.ServiceStateEntity</class>
+
+    <properties>
+      <property name="javax.persistence.jdbc.url" value="jdbc:postgresql://localhost/ambari"/>
+      <property name="javax.persistence.jdbc.driver" value="org.postgresql.Driver"/>
+      <property name="javax.persistence.jdbc.user" value="ambari-server"/>
+      <property name="javax.persistence.jdbc.password" value="bigdata"/>
+    </properties>
+  </persistence-unit>
+
+  <persistence-unit name="ambari-javadb">
+    <class>org.apache.ambari.server.orm.entities.ActionStatusEntity</class>
+    <class>org.apache.ambari.server.orm.entities.ClusterEntity</class>
+    <class>org.apache.ambari.server.orm.entities.ClusterServiceEntity</class>
+    <class>org.apache.ambari.server.orm.entities.ClusterStateEntity</class>
+    <class>org.apache.ambari.server.orm.entities.ComponentHostDesiredStateEntity</class>
+    <class>org.apache.ambari.server.orm.entities.HostComponentStateEntity</class>
+    <class>org.apache.ambari.server.orm.entities.HostComponentMappingEntity</class>
+    <class>org.apache.ambari.server.orm.entities.HostEntity</class>
+    <class>org.apache.ambari.server.orm.entities.HostStateEntity</class>
+    <class>org.apache.ambari.server.orm.entities.ServiceComponentConfigEntity</class>
+    <class>org.apache.ambari.server.orm.entities.ServiceComponentHostConfigEntity</class>
+    <class>org.apache.ambari.server.orm.entities.ServiceComponentStateEntity</class>
+    <class>org.apache.ambari.server.orm.entities.ServiceConfigEntity</class>
+    <class>org.apache.ambari.server.orm.entities.ServiceDesiredStateEntity</class>
+    <class>org.apache.ambari.server.orm.entities.ServiceStateEntity</class>
+
+    <properties>
+      <property name="javax.persistence.jdbc.url" value="jdbc:derby:memory:myDB;create=true"/>
+      <property name="javax.persistence.jdbc.driver" value="org.apache.derby.jdbc.EmbeddedDriver"/>
+      <property name="eclipselink.ddl-generation" value="drop-and-create-tables"/>
+      <!--<property name="javax.persistence.jdbc.user" value="ambari-server"/>-->
+      <!--<property name="javax.persistence.jdbc.password" value="bigdata"/>-->
+    </properties>
+  </persistence-unit>
+
+
+</persistence>

Propchange: incubator/ambari/branches/AMBARI-666/ambari-server/src/main/resources/META-INF/persistence.xml
------------------------------------------------------------------------------
    svn:eol-style = native

Added: incubator/ambari/branches/AMBARI-666/ambari-server/src/test/java/org/apache/ambari/server/orm/OrmTestHelper.java
URL: http://svn.apache.org/viewvc/incubator/ambari/branches/AMBARI-666/ambari-server/src/test/java/org/apache/ambari/server/orm/OrmTestHelper.java?rev=1384149&view=auto
==============================================================================
--- incubator/ambari/branches/AMBARI-666/ambari-server/src/test/java/org/apache/ambari/server/orm/OrmTestHelper.java (added)
+++ incubator/ambari/branches/AMBARI-666/ambari-server/src/test/java/org/apache/ambari/server/orm/OrmTestHelper.java Wed Sep 12 22:22:10 2012
@@ -0,0 +1,122 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.ambari.server.orm;
+
+import com.google.inject.Inject;
+import com.google.inject.Injector;
+import com.google.inject.persist.Transactional;
+import org.apache.ambari.server.orm.dao.ClusterDAO;
+import org.apache.ambari.server.orm.entities.ClusterEntity;
+import org.apache.ambari.server.orm.entities.ClusterServiceEntity;
+import org.apache.ambari.server.orm.entities.HostEntity;
+import org.apache.ambari.server.orm.entities.HostStateEntity;
+
+import javax.persistence.EntityManager;
+import javax.persistence.Query;
+import java.util.ArrayList;
+import java.util.Collection;
+import java.util.List;
+
+public class OrmTestHelper {
+
+  @Inject
+  EntityManager entityManager;
+  @Inject
+  Injector injector;
+
+  public EntityManager getEntityManager() {
+    return entityManager;
+  }
+
+  /**
+   * creates some test data
+    */
+  @Transactional
+  public void createDefaultData() {
+
+    ClusterEntity clusterEntity = new ClusterEntity();
+    clusterEntity.setClusterName("test_cluster1");
+    clusterEntity.setClusterInfo("test_cluster_info1");
+
+    HostEntity host1 = new HostEntity();
+    HostEntity host2 = new HostEntity();
+    HostEntity host3 = new HostEntity();
+
+    host1.setHostName("test_host1");
+    host2.setHostName("test_host2");
+    host3.setHostName("test_host3");
+    host1.setIp("192.168.0.1");
+    host2.setIp("192.168.0.2");
+    host3.setIp("192.168.0.3");
+
+    List<HostEntity> hostEntities = new ArrayList<HostEntity>();
+    hostEntities.add(host1);
+    hostEntities.add(host2);
+
+    clusterEntity.setHostEntities(hostEntities);
+
+    //both sides of relation should be set when modifying in runtime
+    host1.setClusterEntity(clusterEntity);
+    host2.setClusterEntity(clusterEntity);
+
+    HostStateEntity hostStateEntity1 = new HostStateEntity();
+    hostStateEntity1.setCurrentState("TEST_STATE1");
+    hostStateEntity1.setHostEntity(host1);
+    HostStateEntity hostStateEntity2 = new HostStateEntity();
+    hostStateEntity2.setCurrentState("TEST_STATE2");
+    hostStateEntity2.setHostEntity(host2);
+    host1.setHostStateEntity(hostStateEntity1);
+    host2.setHostStateEntity(hostStateEntity2);
+
+    ClusterServiceEntity clusterServiceEntity = new ClusterServiceEntity();
+    clusterServiceEntity.setServiceName("HDFS");
+    clusterServiceEntity.setClusterEntity(clusterEntity);
+    List<ClusterServiceEntity> clusterServiceEntities = new ArrayList<ClusterServiceEntity>();
+    clusterServiceEntities.add(clusterServiceEntity);
+    clusterEntity.setClusterServiceEntities(clusterServiceEntities);
+
+
+    entityManager.persist(host1);
+    entityManager.persist(host2);
+    entityManager.persist(clusterEntity);
+    entityManager.persist(hostStateEntity1);
+    entityManager.persist(hostStateEntity2);
+    entityManager.persist(clusterServiceEntity);
+
+  }
+
+  @Transactional
+  public void performTransactionMarkedForRollback() {
+    ClusterDAO clusterDAO = injector.getInstance(ClusterDAO.class);
+    clusterDAO.removeByName("test_cluster1");
+    entityManager.getTransaction().setRollbackOnly();
+  }
+
+  public int getClusterSizeByHostName(String hostName) {
+
+    Query query = entityManager.createQuery(
+            "SELECT host2 from HostEntity host join host.clusterEntity cluster join cluster.hostEntities host2 where host.hostName=:hostName");
+    query.setParameter("hostName", hostName);
+
+    Collection hosts = query.getResultList();
+
+    return hosts.size();
+  }
+
+}

Propchange: incubator/ambari/branches/AMBARI-666/ambari-server/src/test/java/org/apache/ambari/server/orm/OrmTestHelper.java
------------------------------------------------------------------------------
    svn:eol-style = native

Added: incubator/ambari/branches/AMBARI-666/ambari-server/src/test/java/org/apache/ambari/server/orm/TestOrmImpl.java
URL: http://svn.apache.org/viewvc/incubator/ambari/branches/AMBARI-666/ambari-server/src/test/java/org/apache/ambari/server/orm/TestOrmImpl.java?rev=1384149&view=auto
==============================================================================
--- incubator/ambari/branches/AMBARI-666/ambari-server/src/test/java/org/apache/ambari/server/orm/TestOrmImpl.java (added)
+++ incubator/ambari/branches/AMBARI-666/ambari-server/src/test/java/org/apache/ambari/server/orm/TestOrmImpl.java Wed Sep 12 22:22:10 2012
@@ -0,0 +1,188 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.ambari.server.orm;
+
+import com.google.inject.Guice;
+import com.google.inject.Injector;
+import com.google.inject.persist.jpa.JpaPersistModule;
+import org.apache.ambari.server.orm.dao.ClusterDAO;
+import org.apache.ambari.server.orm.dao.ClusterServiceDAO;
+import org.apache.ambari.server.orm.dao.ServiceConfigDAO;
+import org.apache.ambari.server.orm.entities.ClusterEntity;
+import org.apache.ambari.server.orm.entities.ClusterServiceEntity;
+import org.apache.ambari.server.orm.entities.ServiceConfigEntity;
+import org.apache.commons.logging.Log;
+import org.apache.commons.logging.LogFactory;
+import org.junit.Assert;
+import org.junit.BeforeClass;
+import org.junit.Test;
+
+import javax.persistence.EntityManager;
+import javax.persistence.RollbackException;
+import java.util.Date;
+import java.util.List;
+
+public class TestOrmImpl extends Assert {
+  private final static Log log = LogFactory.getLog(TestOrmImpl.class);
+
+  private static Injector injector;
+
+  @BeforeClass
+  public static void setUpClass() throws Exception {
+    injector = Guice.createInjector(new JpaPersistModule("ambari-javadb")); //used for injecting in-memory DB EntityManager
+//    injector = Guice.createInjector(new JpaPersistModule("ambari-postgres")); //for injecting
+    injector.getInstance(GuiceJpaInitializer.class); //needed by Guice-persist to work
+    injector.getInstance(OrmTestHelper.class).createDefaultData();
+  }
+
+  /**
+   * persistence provider is responsible for returning empty collection if relation doesn't exists
+   */
+  @Test
+  public void testEmptyPersistentCollection() {
+    String testClusterName = "test_cluster2";
+
+    ClusterEntity clusterEntity = new ClusterEntity();
+    clusterEntity.setClusterName(testClusterName);
+    ClusterDAO clusterDAO = injector.getInstance(ClusterDAO.class);
+    clusterDAO.create(clusterEntity);
+    clusterEntity = clusterDAO.findByName(clusterEntity.getClusterName());
+
+    assertTrue("empty relation wasn't instantiated", clusterEntity.getHostEntities() != null);
+  }
+
+  /**
+   * Transaction marked for rollback should not be allowed for commit
+   * @throws Throwable
+   */
+  @Test(expected = RollbackException.class)
+  public void testRollbackException() throws Throwable{
+    injector.getInstance(OrmTestHelper.class).performTransactionMarkedForRollback();
+  }
+
+  /**
+   * Rollback test
+   */
+  @Test
+  public void testSafeRollback() {
+    String testClusterName = "don't save";
+
+    EntityManager entityManager = injector.getInstance(OrmTestHelper.class).getEntityManager();
+    entityManager.getTransaction().begin();
+    ClusterEntity clusterEntity = new ClusterEntity();
+    clusterEntity.setClusterName(testClusterName);
+    entityManager.persist(clusterEntity);
+    entityManager.getTransaction().rollback();
+
+    assertNull("transaction was not rolled back", entityManager.find(ClusterEntity.class, testClusterName));
+  }
+
+  /**
+   * Test auto incremented field and custom query example
+   */
+  @Test
+  public void testAutoIncrementedField() {
+    ClusterServiceDAO clusterServiceDAO = injector.getInstance(ClusterServiceDAO.class);
+    ServiceConfigDAO serviceConfigDAO = injector.getInstance(ServiceConfigDAO.class);
+    Date currentTime = new Date();
+    String serviceName = "MapReduce1";
+    String clusterName = "test_cluster1";
+
+    createServiceAndConfig(currentTime, serviceName, clusterName);
+
+
+
+    List<ServiceConfigEntity> result = serviceConfigDAO.findAll();
+    assertNotNull("inserted config not found", result);
+    assertFalse(result.isEmpty());
+    ClusterServiceEntity clusterServiceEntity = clusterServiceDAO.findByClusterAndServiceNames(clusterName, serviceName);
+
+    result = serviceConfigDAO.findByClusterService(clusterServiceEntity);
+
+    assertNotNull("config by ClusterService not found", result);
+    assertEquals("wrong number of configs", 1, result.size());
+
+    ServiceConfigEntity serviceConfigEntity = result.iterator().next();
+
+    log.info("config version = " + serviceConfigEntity.getConfigVersion());
+    assertNotNull("config version is null", serviceConfigEntity.getConfigVersion());
+
+    serviceConfigDAO.remove(serviceConfigEntity);
+
+    result = serviceConfigDAO.findAll();
+
+
+    assertTrue(result.isEmpty());
+
+    clusterServiceDAO.remove(clusterServiceEntity);
+
+    assertNull(clusterServiceDAO
+            .findByClusterAndServiceNames(clusterName, serviceName));
+
+  }
+
+  private void createServiceAndConfig(Date currentTime, String serviceName, String clusterName) {
+    ClusterServiceDAO clusterServiceDAO = injector.getInstance(ClusterServiceDAO.class);
+    ClusterDAO clusterDAO = injector.getInstance(ClusterDAO.class);
+    ClusterEntity cluster = clusterDAO.findByName(clusterName);
+
+    ClusterServiceEntity clusterServiceEntity = new ClusterServiceEntity();
+    clusterServiceEntity.setClusterEntity(cluster);
+    clusterServiceEntity.setServiceName(serviceName);
+
+    cluster.getClusterServiceEntities().add(clusterServiceEntity);
+
+    clusterServiceDAO.create(clusterServiceEntity);
+    clusterDAO.merge(cluster);
+
+    clusterServiceEntity = clusterServiceDAO
+            .findByClusterAndServiceNames(clusterName, serviceName);
+    assertNotNull(clusterServiceEntity);
+
+    ServiceConfigEntity serviceConfigEntity = new ServiceConfigEntity();
+    serviceConfigEntity.setConfigSnapshotTime(currentTime);
+    serviceConfigEntity.setClusterServiceEntity(clusterServiceEntity);
+
+    List<ServiceConfigEntity> list = clusterServiceEntity.getServiceConfigEntities();
+    list.add(serviceConfigEntity);
+    clusterServiceEntity.setServiceConfigEntities(list);
+
+    ServiceConfigDAO serviceConfigDAO = injector.getInstance(ServiceConfigDAO.class);
+
+    serviceConfigDAO.create(serviceConfigEntity);
+    clusterServiceDAO.merge(clusterServiceEntity);
+  }
+
+  /**
+   * to clarify: are cascade operations allowed?
+   */
+  @Test(expected = RollbackException.class)
+  public void testCascadeRemoveFail() {
+    ClusterServiceDAO clusterServiceDAO = injector.getInstance(ClusterServiceDAO.class);
+    Date currentTime = new Date();
+    String serviceName = "MapReduce2";
+    String clusterName = "test_cluster1";
+
+    createServiceAndConfig(currentTime, serviceName, clusterName);
+
+    ClusterServiceEntity clusterServiceEntity = clusterServiceDAO.findByClusterAndServiceNames(clusterName, serviceName);
+    clusterServiceDAO.remove(clusterServiceEntity);
+  }
+
+}

Propchange: incubator/ambari/branches/AMBARI-666/ambari-server/src/test/java/org/apache/ambari/server/orm/TestOrmImpl.java
------------------------------------------------------------------------------
    svn:eol-style = native