Repository: incubator-atlas Updated Branches: refs/heads/master c9ee6d3f7 -> 86dd72aff
ATLAS-911 Get entity by unique attribute doesn't enforce type (shwethags) Project: http://git-wip-us.apache.org/repos/asf/incubator-atlas/repo Commit: http://git-wip-us.apache.org/repos/asf/incubator-atlas/commit/86dd72af Tree: http://git-wip-us.apache.org/repos/asf/incubator-atlas/tree/86dd72af Diff: http://git-wip-us.apache.org/repos/asf/incubator-atlas/diff/86dd72af Branch: refs/heads/master Commit: 86dd72aff5eabb8b73722388bce82ad713181400 Parents: c9ee6d3 Author: Shwetha GS <[email protected]> Authored: Fri Jun 17 11:08:29 2016 +0530 Committer: Shwetha GS <[email protected]> Committed: Fri Jun 17 11:08:29 2016 +0530 ---------------------------------------------------------------------- release-log.txt | 1 + .../graph/GraphBackedMetadataRepository.java | 5 +- .../atlas/repository/graph/GraphHelper.java | 10 ++-- .../test/java/org/apache/atlas/TestUtils.java | 1 - .../GraphBackedDiscoveryServiceTest.java | 2 +- ...hBackedMetadataRepositoryDeleteTestBase.java | 2 +- .../GraphBackedMetadataRepositoryTest.java | 61 +++++++++++++++++--- 7 files changed, 65 insertions(+), 17 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/incubator-atlas/blob/86dd72af/release-log.txt ---------------------------------------------------------------------- diff --git a/release-log.txt b/release-log.txt index ad3ceaa..b243037 100644 --- a/release-log.txt +++ b/release-log.txt @@ -23,6 +23,7 @@ ATLAS-409 Atlas will not import avro tables with schema read from a file (dosset ATLAS-379 Create sqoop and falcon metadata addons (venkatnrangan,bvellanki,sowmyaramesh via shwethags) ALL CHANGES: +ATLAS-911 Get entity by unique attribute doesn't enforce type (shwethags) ATLAS-899 Fix Hive Hook documentation (sumasai via yhemanth) ATLAS-890 Log received messages in case of error (sumasai via yhemanth) ATLAS-888 NPE in NotificationHookConsumer (sumasai via shwethags) http://git-wip-us.apache.org/repos/asf/incubator-atlas/blob/86dd72af/repository/src/main/java/org/apache/atlas/repository/graph/GraphBackedMetadataRepository.java ---------------------------------------------------------------------- diff --git a/repository/src/main/java/org/apache/atlas/repository/graph/GraphBackedMetadataRepository.java b/repository/src/main/java/org/apache/atlas/repository/graph/GraphBackedMetadataRepository.java index 0d82d90..dd7dd74 100755 --- a/repository/src/main/java/org/apache/atlas/repository/graph/GraphBackedMetadataRepository.java +++ b/repository/src/main/java/org/apache/atlas/repository/graph/GraphBackedMetadataRepository.java @@ -37,6 +37,7 @@ import org.apache.atlas.typesystem.ITypedStruct; import org.apache.atlas.typesystem.exception.EntityExistsException; import org.apache.atlas.typesystem.exception.EntityNotFoundException; import org.apache.atlas.typesystem.exception.TraitNotFoundException; +import org.apache.atlas.typesystem.persistence.Id; import org.apache.atlas.typesystem.types.AttributeInfo; import org.apache.atlas.typesystem.types.ClassType; import org.apache.atlas.typesystem.types.DataTypes; @@ -158,7 +159,9 @@ public class GraphBackedMetadataRepository implements MetadataRepository { LOG.info("Retrieving entity with type={} and {}={}", entityType, attribute, value); IDataType type = typeSystem.getDataType(IDataType.class, entityType); String propertyKey = getFieldNameInVertex(type, attribute); - Vertex instanceVertex = graphHelper.getVertexForProperty(propertyKey, value); + Vertex instanceVertex = graphHelper.findVertex(propertyKey, value, + Constants.ENTITY_TYPE_PROPERTY_KEY, entityType, + Constants.STATE_PROPERTY_KEY, Id.EntityState.ACTIVE.name()); String guid = instanceVertex.getProperty(Constants.GUID_PROPERTY_KEY); return graphToInstanceMapper.mapGraphToTypedInstance(guid, instanceVertex); http://git-wip-us.apache.org/repos/asf/incubator-atlas/blob/86dd72af/repository/src/main/java/org/apache/atlas/repository/graph/GraphHelper.java ---------------------------------------------------------------------- diff --git a/repository/src/main/java/org/apache/atlas/repository/graph/GraphHelper.java b/repository/src/main/java/org/apache/atlas/repository/graph/GraphHelper.java index 4f6d011..4f531e2 100755 --- a/repository/src/main/java/org/apache/atlas/repository/graph/GraphHelper.java +++ b/repository/src/main/java/org/apache/atlas/repository/graph/GraphHelper.java @@ -164,7 +164,7 @@ public final class GraphHelper { * @return vertex with the given property keys * @throws EntityNotFoundException */ - private Vertex findVertex(Object... args) throws EntityNotFoundException { + public Vertex findVertex(Object... args) throws EntityNotFoundException { StringBuilder condition = new StringBuilder(); GraphQuery query = titanGraph.query(); for (int i = 0 ; i < args.length; i+=2) { @@ -301,10 +301,6 @@ public final class GraphHelper { return findVertex(Constants.GUID_PROPERTY_KEY, guid); } - public Vertex getVertexForProperty(String propertyKey, Object value) throws EntityNotFoundException { - return findVertex(propertyKey, value, Constants.STATE_PROPERTY_KEY, Id.EntityState.ACTIVE.name()); - } - public static String getQualifiedNameForMapKey(String prefix, String key) { return prefix + "." + key; } @@ -381,7 +377,9 @@ public final class GraphHelper { if (attributeInfo.isUnique) { String propertyKey = getQualifiedFieldName(classType, attributeInfo.name); try { - result = getVertexForProperty(propertyKey, instance.get(attributeInfo.name)); + result = findVertex(propertyKey, instance.get(attributeInfo.name), + Constants.ENTITY_TYPE_PROPERTY_KEY, classType.getName(), + Constants.STATE_PROPERTY_KEY, Id.EntityState.ACTIVE.name()); LOG.debug("Found vertex by unique attribute : " + propertyKey + "=" + instance.get(attributeInfo.name)); } catch (EntityNotFoundException e) { //Its ok if there is no entity with the same unique value http://git-wip-us.apache.org/repos/asf/incubator-atlas/blob/86dd72af/repository/src/test/java/org/apache/atlas/TestUtils.java ---------------------------------------------------------------------- diff --git a/repository/src/test/java/org/apache/atlas/TestUtils.java b/repository/src/test/java/org/apache/atlas/TestUtils.java index 345e874..0ce7acb 100755 --- a/repository/src/test/java/org/apache/atlas/TestUtils.java +++ b/repository/src/test/java/org/apache/atlas/TestUtils.java @@ -206,7 +206,6 @@ public final class TestUtils { createOptionalAttrDef("namespace", DataTypes.STRING_TYPE), createOptionalAttrDef("cluster", DataTypes.STRING_TYPE), createOptionalAttrDef("colo", DataTypes.STRING_TYPE)); - HierarchicalTypeDefinition<ClassType> databaseTypeDefinition = createClassTypeDef(DATABASE_TYPE, DATABASE_TYPE + _description,ImmutableSet.of(SUPER_TYPE_NAME), TypesUtil.createUniqueRequiredAttrDef(NAME, DataTypes.STRING_TYPE), http://git-wip-us.apache.org/repos/asf/incubator-atlas/blob/86dd72af/repository/src/test/java/org/apache/atlas/discovery/GraphBackedDiscoveryServiceTest.java ---------------------------------------------------------------------- diff --git a/repository/src/test/java/org/apache/atlas/discovery/GraphBackedDiscoveryServiceTest.java b/repository/src/test/java/org/apache/atlas/discovery/GraphBackedDiscoveryServiceTest.java index 5e7de88..64c8bc7 100755 --- a/repository/src/test/java/org/apache/atlas/discovery/GraphBackedDiscoveryServiceTest.java +++ b/repository/src/test/java/org/apache/atlas/discovery/GraphBackedDiscoveryServiceTest.java @@ -77,7 +77,7 @@ public class GraphBackedDiscoveryServiceTest extends BaseRepositoryTest { ITypedReferenceableInstance hrDept = TestUtils.createDeptEg1(typeSystem); repositoryService.createEntities(hrDept); - ITypedReferenceableInstance jane = repositoryService.getEntityDefinition("Person", "name", "Jane"); + ITypedReferenceableInstance jane = repositoryService.getEntityDefinition("Manager", "name", "Jane"); Id janeGuid = jane.getId(); ClassType personType = typeSystem.getDataType(ClassType.class, "Person"); ITypedReferenceableInstance instance = personType.createInstance(janeGuid); http://git-wip-us.apache.org/repos/asf/incubator-atlas/blob/86dd72af/repository/src/test/java/org/apache/atlas/repository/graph/GraphBackedMetadataRepositoryDeleteTestBase.java ---------------------------------------------------------------------- diff --git a/repository/src/test/java/org/apache/atlas/repository/graph/GraphBackedMetadataRepositoryDeleteTestBase.java b/repository/src/test/java/org/apache/atlas/repository/graph/GraphBackedMetadataRepositoryDeleteTestBase.java index 449e066..34842c3 100644 --- a/repository/src/test/java/org/apache/atlas/repository/graph/GraphBackedMetadataRepositoryDeleteTestBase.java +++ b/repository/src/test/java/org/apache/atlas/repository/graph/GraphBackedMetadataRepositoryDeleteTestBase.java @@ -514,7 +514,7 @@ public abstract class GraphBackedMetadataRepositoryDeleteTestBase { AtlasClient.EntityResult entityResult = deleteEntities(maxGuid); - ITypedReferenceableInstance john = repositoryService.getEntityDefinition("Manager", "name", "John"); + ITypedReferenceableInstance john = repositoryService.getEntityDefinition("Person", "name", "John"); assertEquals(entityResult.getDeletedEntities().size(), 1); assertTrue(entityResult.getDeletedEntities().contains(maxGuid)); http://git-wip-us.apache.org/repos/asf/incubator-atlas/blob/86dd72af/repository/src/test/java/org/apache/atlas/repository/graph/GraphBackedMetadataRepositoryTest.java ---------------------------------------------------------------------- diff --git a/repository/src/test/java/org/apache/atlas/repository/graph/GraphBackedMetadataRepositoryTest.java b/repository/src/test/java/org/apache/atlas/repository/graph/GraphBackedMetadataRepositoryTest.java index 2d1c33a..97dfbcd 100755 --- a/repository/src/test/java/org/apache/atlas/repository/graph/GraphBackedMetadataRepositoryTest.java +++ b/repository/src/test/java/org/apache/atlas/repository/graph/GraphBackedMetadataRepositoryTest.java @@ -25,7 +25,6 @@ import com.thinkaurelius.titan.core.util.TitanCleanup; import com.tinkerpop.blueprints.Compare; import com.tinkerpop.blueprints.GraphQuery; import com.tinkerpop.blueprints.Vertex; - import org.apache.atlas.GraphTransaction; import org.apache.atlas.RepositoryMetadataModule; import org.apache.atlas.RequestContext; @@ -57,11 +56,9 @@ import org.testng.annotations.BeforeClass; import org.testng.annotations.BeforeMethod; import org.testng.annotations.Guice; import org.testng.annotations.Test; - import scala.actors.threadpool.Arrays; import javax.inject.Inject; - import java.util.ArrayList; import java.util.Date; import java.util.HashMap; @@ -69,6 +66,11 @@ import java.util.Iterator; import java.util.List; import java.util.UUID; +import static org.apache.atlas.typesystem.types.utils.TypesUtil.createClassTypeDef; +import static org.apache.atlas.typesystem.types.utils.TypesUtil.createUniqueRequiredAttrDef; +import static org.testng.Assert.assertEquals; +import static org.testng.Assert.assertNotEquals; + /** * GraphBackedMetadataRepository test * @@ -186,7 +188,7 @@ public class GraphBackedMetadataRepositoryTest { //Reuse the same database instance without id, with the same unique attribute ITypedReferenceableInstance table = createHiveTableInstance(databaseInstance); - List<String> guids = repositoryService.createEntities(db, table); + List<String> guids = createEntities(db, table); Assert.assertEquals(guids.size(), 7); //1 db + 5 columns + 1 table. Shouldn't create db again System.out.println("added db = " + guids.get(0)); System.out.println("added table = " + guids.get(6)); @@ -201,6 +203,17 @@ public class GraphBackedMetadataRepositoryTest { System.out.println("*** table = " + table); } + private List<String> createEntities(ITypedReferenceableInstance... instances) throws Exception { + RequestContext.createContext(); + return repositoryService.createEntities(instances); + } + + private List<String> createEntity(Referenceable entity) throws Exception { + ClassType type = typeSystem.getDataType(ClassType.class, entity.getTypeName()); + ITypedReferenceableInstance instance = type.convert(entity, Multiplicity.REQUIRED); + return createEntities(instance); + } + @GraphTransaction String getGUID() { Vertex tableVertex = getTableEntityVertex(); @@ -245,6 +258,36 @@ public class GraphBackedMetadataRepositoryTest { Assert.fail(); } + @Test + public void testMultipleTypesWithSameUniqueAttribute() throws Exception { + //Two entities of different types(with same supertype that has the unique attribute) with same qualified name should succeed + HierarchicalTypeDefinition<ClassType> supertype = + createClassTypeDef(randomString(), ImmutableSet.<String>of(), + createUniqueRequiredAttrDef("name", DataTypes.STRING_TYPE)); + HierarchicalTypeDefinition<ClassType> t1 = + createClassTypeDef(randomString(), ImmutableSet.of(supertype.typeName)); + HierarchicalTypeDefinition<ClassType> t2 = + createClassTypeDef(randomString(), ImmutableSet.of(supertype.typeName)); + typeSystem.defineClassTypes(supertype, t1, t2); + + final String name = randomString(); + String id1 = createEntity(new Referenceable(t1.typeName) {{ + set("name", name); + }}).get(0); + String id2 = createEntity(new Referenceable(t2.typeName) {{ + set("name", name); + }}).get(0); + assertNotEquals(id1, id2); + + ITypedReferenceableInstance entity = repositoryService.getEntityDefinition(t1.typeName, "name", name); + assertEquals(entity.getTypeName(), t1.typeName); + assertEquals(entity.getId()._getId(), id1); + + entity = repositoryService.getEntityDefinition(t2.typeName, "name", name); + assertEquals(entity.getTypeName(), t2.typeName); + assertEquals(entity.getId()._getId(), id2); + } + @Test(dependsOnMethods = "testGetTraitNames") public void testAddTrait() throws Exception { final String aGUID = getGUID(); @@ -574,18 +617,22 @@ public class GraphBackedMetadataRepositoryTest { return tableType.convert(tableInstance, Multiplicity.REQUIRED); } - private String random() { + private String randomUTF() { return RandomStringUtils.random(10); } + private String randomString() { + return RandomStringUtils.randomAlphanumeric(10); + } + @Test public void testUTFValues() throws Exception { Referenceable hrDept = new Referenceable("Department"); Referenceable john = new Referenceable("Person"); - john.set("name", random()); + john.set("name", randomUTF()); john.set("department", hrDept); - hrDept.set("name", random()); + hrDept.set("name", randomUTF()); hrDept.set("employees", ImmutableList.of(john)); ClassType deptType = typeSystem.getDataType(ClassType.class, "Department");
