Repository: ambari Updated Branches: refs/heads/branch-2.1 c679234d3 -> 21cef44b3
AMBARI-12319 - Views : S020 Data storage error for Hive view (Mysql db) (tbeerbower) Project: http://git-wip-us.apache.org/repos/asf/ambari/repo Commit: http://git-wip-us.apache.org/repos/asf/ambari/commit/21cef44b Tree: http://git-wip-us.apache.org/repos/asf/ambari/tree/21cef44b Diff: http://git-wip-us.apache.org/repos/asf/ambari/diff/21cef44b Branch: refs/heads/branch-2.1 Commit: 21cef44b3e49f6d6ee4eb15fbf567c9fcf43eb5d Parents: c679234 Author: tbeerbower <tbeerbo...@hortonworks.com> Authored: Wed Jul 8 08:34:41 2015 -0400 Committer: tbeerbower <tbeerbo...@hortonworks.com> Committed: Wed Jul 8 08:35:57 2015 -0400 ---------------------------------------------------------------------- .../server/view/persistence/DataStoreImpl.java | 21 ++++- .../view/persistence/DataStoreImplTest.java | 81 ++++++++++++++++++++ 2 files changed, 100 insertions(+), 2 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/ambari/blob/21cef44b/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 6d31a08..333ff19 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,6 +25,7 @@ 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.internal.helper.DatabaseField; import org.eclipse.persistence.jpa.dynamic.JPADynamicHelper; import org.eclipse.persistence.jpa.dynamic.JPADynamicTypeBuilder; import org.eclipse.persistence.mappings.DirectToFieldMapping; @@ -117,7 +118,12 @@ public class DataStoreImpl implements DataStore { /** * Max length of entity string field. */ - protected static final int MAX_ENTITY_STRING_FIELD_LENGTH = 4000; + protected static final int MAX_ENTITY_STRING_FIELD_LENGTH = 3200; + + /** + * Max total length of all the fields of an entity. + */ + protected static final int MAX_ENTITY_FIELD_LENGTH_TOTAL = 65000; /** * Table / column name prefix. @@ -286,6 +292,8 @@ public class DataStoreImpl implements DataStore { Map<String, PropertyDescriptor> descriptorMap = getDescriptorMap(clazz); + long totalLength = 0L; + for (Map.Entry<String, PropertyDescriptor> descriptorEntry : descriptorMap.entrySet()) { String fieldName = descriptorEntry.getKey(); @@ -302,9 +310,18 @@ public class DataStoreImpl implements DataStore { if (isDirectMappingType(propertyType)) { DirectToFieldMapping mapping = typeBuilder.addDirectMapping(attributeName, propertyType, attributeName); + DatabaseField field = mapping.getField(); + // explicitly set the length of string fields if (String.class.isAssignableFrom(propertyType)) { - mapping.getField().setLength(MAX_ENTITY_STRING_FIELD_LENGTH); + field.setLength(MAX_ENTITY_STRING_FIELD_LENGTH); + } + totalLength += field.getLength(); + if (totalLength > MAX_ENTITY_FIELD_LENGTH_TOTAL) { + String msg = String.format("The total length of the fields of the %s entity can not exceed %d characters.", + clazz.getSimpleName(), MAX_ENTITY_FIELD_LENGTH_TOTAL); + LOG.error(msg); + throw new IllegalStateException(msg); } } } http://git-wip-us.apache.org/repos/asf/ambari/blob/21cef44b/ambari-server/src/test/java/org/apache/ambari/server/view/persistence/DataStoreImplTest.java ---------------------------------------------------------------------- diff --git a/ambari-server/src/test/java/org/apache/ambari/server/view/persistence/DataStoreImplTest.java b/ambari-server/src/test/java/org/apache/ambari/server/view/persistence/DataStoreImplTest.java index 1b4758d..9be6e4f 100644 --- a/ambari-server/src/test/java/org/apache/ambari/server/view/persistence/DataStoreImplTest.java +++ b/ambari-server/src/test/java/org/apache/ambari/server/view/persistence/DataStoreImplTest.java @@ -189,6 +189,46 @@ public class DataStoreImplTest { } @Test + public void testStore_create_largeEntity() throws Exception { + DynamicClassLoader classLoader = new DynamicClassLoader(DataStoreImplTest.class.getClassLoader()); + + // create mocks + EntityManagerFactory entityManagerFactory = createMock(EntityManagerFactory.class); + EntityManager entityManager = createMock(EntityManager.class); + JPADynamicHelper jpaDynamicHelper = createNiceMock(JPADynamicHelper.class); + SchemaManager schemaManager = createNiceMock(SchemaManager.class); + EntityTransaction transaction = createMock(EntityTransaction.class); + + // set expectations + Capture<DynamicType> typeCapture = new Capture<DynamicType>(); + Capture<DynamicType> typeCapture2 = new Capture<DynamicType>(); + jpaDynamicHelper.addTypes(eq(true), eq(true), capture(typeCapture), capture(typeCapture2)); + + expect(entityManagerFactory.createEntityManager()).andReturn(entityManager); + expect(entityManager.getTransaction()).andReturn(transaction).anyTimes(); + + entityManager.close(); + + transaction.begin(); + expect(transaction.isActive()).andReturn(true); + transaction.rollback(); + + // replay mocks + replay(entityManagerFactory, entityManager, jpaDynamicHelper, transaction, schemaManager); + + DataStoreImpl dataStore = getDataStore(entityManagerFactory, jpaDynamicHelper, classLoader, schemaManager); + + try { + dataStore.store(new TestLargeEntity(99)); + Assert.fail("Expected PersistenceException."); + } catch (PersistenceException e) { + // expected + } + // verify mocks + verify(entityManagerFactory, entityManager, jpaDynamicHelper, transaction, schemaManager); + } + + @Test public void testStore_update() throws Exception { DynamicClassLoader classLoader = new DynamicClassLoader(DataStoreImplTest.class.getClassLoader()); @@ -589,6 +629,47 @@ public class DataStoreImplTest { } } + public static class TestLargeEntity { + + public TestLargeEntity() { + } + + public TestLargeEntity(int id) { + this.id = id; + } + + int id; + String f1; + String f2; + String f3; + String f4; + String f5; + String f6; + String f7; + String f8; + String f9; + String f10; + String f11; + String f12; + String f13; + String f14; + String f15; + String f16; + String f17; + String f18; + String f19; + String f20; + String f21; + + public int getId() { + return id; + } + + public void setId(int id) { + this.id = id; + } + } + private static class TestModule implements Module, SchemaManagerFactory { private final ViewInstanceEntity viewInstanceEntity; private final EntityManagerFactory entityManagerFactory;