Search in sources :

Example 16 with RepositoryException

use of org.apache.atlas.repository.RepositoryException in project incubator-atlas by apache.

the class GraphBackedMetadataRepository method getEntityDefinitions.

@Override
@GraphTransaction
public List<ITypedReferenceableInstance> getEntityDefinitions(String... guids) throws RepositoryException, EntityNotFoundException {
    if (LOG.isDebugEnabled()) {
        LOG.debug("Retrieving entities with guids={}", Arrays.toString(guids));
    }
    RequestContext context = RequestContext.get();
    ITypedReferenceableInstance[] result = new ITypedReferenceableInstance[guids.length];
    // Map of the guids of instances not in the cache to their index(es) in the result.
    // This is used to put the loaded instances into the location(s) corresponding
    // to their guid in the result.  Note that a set is needed since guids can
    // appear more than once in the list.
    Map<String, Set<Integer>> uncachedGuids = new HashMap<>();
    for (int i = 0; i < guids.length; i++) {
        String guid = guids[i];
        // First, check the cache.
        ITypedReferenceableInstance cached = context.getInstanceV1(guid);
        if (cached != null) {
            result[i] = cached;
        } else {
            Set<Integer> indices = uncachedGuids.get(guid);
            if (indices == null) {
                indices = new HashSet<>(1);
                uncachedGuids.put(guid, indices);
            }
            indices.add(i);
        }
    }
    List<String> guidsToFetch = new ArrayList<>(uncachedGuids.keySet());
    Map<String, AtlasVertex> instanceVertices = graphHelper.getVerticesForGUIDs(guidsToFetch);
    // search for missing entities
    if (instanceVertices.size() != guidsToFetch.size()) {
        Set<String> missingGuids = new HashSet<String>(guidsToFetch);
        missingGuids.removeAll(instanceVertices.keySet());
        if (!missingGuids.isEmpty()) {
            if (LOG.isDebugEnabled()) {
                LOG.debug("Failed to find guids={}", missingGuids);
            }
            throw new EntityNotFoundException("Could not find entities in the repository with guids: " + missingGuids.toString());
        }
    }
    for (String guid : guidsToFetch) {
        try {
            ITypedReferenceableInstance entity = graphToInstanceMapper.mapGraphToTypedInstance(guid, instanceVertices.get(guid));
            for (int index : uncachedGuids.get(guid)) {
                result[index] = entity;
            }
        } catch (AtlasException e) {
            throw new RepositoryException(e);
        }
    }
    return Arrays.asList(result);
}
Also used : HashSet(java.util.HashSet) Set(java.util.Set) HashMap(java.util.HashMap) ITypedReferenceableInstance(org.apache.atlas.typesystem.ITypedReferenceableInstance) ArrayList(java.util.ArrayList) RepositoryException(org.apache.atlas.repository.RepositoryException) EntityNotFoundException(org.apache.atlas.typesystem.exception.EntityNotFoundException) AtlasException(org.apache.atlas.AtlasException) AtlasVertex(org.apache.atlas.repository.graphdb.AtlasVertex) RequestContext(org.apache.atlas.RequestContext) HashSet(java.util.HashSet) GraphTransaction(org.apache.atlas.annotation.GraphTransaction)

Example 17 with RepositoryException

use of org.apache.atlas.repository.RepositoryException in project incubator-atlas by apache.

the class MemRepository method create.

/**
 * 1. traverse the Object Graph from  i and create idToNewIdMap : Map[Id, Id],
 *    also create old Id to Instance Map: oldIdToInstance : Map[Id, IInstance]
 *   - traverse reference Attributes, List[ClassType], Maps where Key/value is ClassType
 *   - traverse Structs
 *   - traverse Traits.
 * 1b. Ensure that every newId has an associated Instance.
 * 2. Traverse oldIdToInstance map create newInstances : List[ITypedReferenceableInstance]
 *    - create a ITypedReferenceableInstance.
 *      replace any old References ( ids or object references) with new Ids.
 * 3. Traverse over newInstances
 *    - ask ClassStore to assign a position to the Id.
 *      - for Instances with Traits, assign a position for each Trait
 *    - invoke store on the nwInstance.
 *
 * Recovery:
 * - on each newInstance, invoke releaseId and delete on its ClassStore and Traits' Stores.
 *
 * @param i
 * @return
 * @throws org.apache.atlas.repository.RepositoryException
 */
public ITypedReferenceableInstance create(IReferenceableInstance i) throws RepositoryException {
    DiscoverInstances discoverInstances = new DiscoverInstances(this);
    /*
         * Step 1: traverse the Object Graph from  i and create idToNewIdMap : Map[Id, Id],
         *    also create old Id to Instance Map: oldIdToInstance : Map[Id, IInstance]
         *   - traverse reference Attributes, List[ClassType], Maps where Key/value is ClassType
         *   - traverse Structs
         *   - traverse Traits.
         */
    try {
        new ObjectGraphWalker(typeSystem, discoverInstances, i).walk();
    } catch (AtlasException me) {
        throw new RepositoryException("TypeSystem error when walking the ObjectGraph", me);
    }
    /*
         * Step 1b: Ensure that every newId has an associated Instance.
         */
    for (Id oldId : discoverInstances.idToNewIdMap.keySet()) {
        if (!discoverInstances.idToInstanceMap.containsKey(oldId)) {
            throw new RepositoryException(String.format("Invalid Object Graph: " + "Encountered an unassignedId %s that is not associated with an Instance", oldId));
        }
    }
    /* Step 2: Traverse oldIdToInstance map create newInstances :
        List[ITypedReferenceableInstance]
         * - create a ITypedReferenceableInstance.
         *   replace any old References ( ids or object references) with new Ids.
        */
    List<ITypedReferenceableInstance> newInstances = new ArrayList<>();
    ITypedReferenceableInstance retInstance = null;
    Set<ClassType> classTypes = new TreeSet<>();
    Set<TraitType> traitTypes = new TreeSet<>();
    for (IReferenceableInstance transientInstance : discoverInstances.idToInstanceMap.values()) {
        try {
            ClassType cT = typeSystem.getDataType(ClassType.class, transientInstance.getTypeName());
            ITypedReferenceableInstance newInstance = cT.convert(transientInstance, Multiplicity.REQUIRED);
            newInstances.add(newInstance);
            classTypes.add(cT);
            for (String traitName : newInstance.getTraits()) {
                TraitType tT = typeSystem.getDataType(TraitType.class, traitName);
                traitTypes.add(tT);
            }
            if (newInstance.getId() == i.getId()) {
                retInstance = newInstance;
            }
            /*
                 * Now replace old references with new Ids
                 */
            MapIds mapIds = new MapIds(discoverInstances.idToNewIdMap);
            new ObjectGraphWalker(typeSystem, mapIds, newInstances).walk();
        } catch (AtlasException me) {
            throw new RepositoryException(String.format("Failed to create Instance(id = %s", transientInstance.getId()), me);
        }
    }
    /*
         * 3. Acquire Class and Trait Storage locks.
         * - acquire them in a stable order (super before subclass, classes before traits
         */
    for (ClassType cT : classTypes) {
        HierarchicalTypeStore st = typeStores.get(cT.getName());
        st.acquireWriteLock();
    }
    for (TraitType tT : traitTypes) {
        HierarchicalTypeStore st = typeStores.get(tT.getName());
        st.acquireWriteLock();
    }
    /*
         * 4. Traverse over newInstances
         *    - ask ClassStore to assign a position to the Id.
         *      - for Instances with Traits, assign a position for each Trait
         *    - invoke store on the nwInstance.
         */
    try {
        for (ITypedReferenceableInstance instance : newInstances) {
            HierarchicalTypeStore st = typeStores.get(instance.getTypeName());
            st.assignPosition(instance.getId());
            for (String traitName : instance.getTraits()) {
                HierarchicalTypeStore tt = typeStores.get(traitName);
                tt.assignPosition(instance.getId());
            }
        }
        for (ITypedReferenceableInstance instance : newInstances) {
            HierarchicalTypeStore st = typeStores.get(instance.getTypeName());
            st.store((ReferenceableInstance) instance);
            for (String traitName : instance.getTraits()) {
                HierarchicalTypeStore tt = typeStores.get(traitName);
                tt.store((ReferenceableInstance) instance);
            }
        }
    } catch (RepositoryException re) {
        for (ITypedReferenceableInstance instance : newInstances) {
            HierarchicalTypeStore st = typeStores.get(instance.getTypeName());
            st.releaseId(instance.getId());
        }
        throw re;
    } finally {
        for (ClassType cT : classTypes) {
            HierarchicalTypeStore st = typeStores.get(cT.getName());
            st.releaseWriteLock();
        }
        for (TraitType tT : traitTypes) {
            HierarchicalTypeStore st = typeStores.get(tT.getName());
            st.releaseWriteLock();
        }
    }
    return retInstance;
}
Also used : DiscoverInstances(org.apache.atlas.repository.DiscoverInstances) ITypedReferenceableInstance(org.apache.atlas.typesystem.ITypedReferenceableInstance) TraitType(org.apache.atlas.typesystem.types.TraitType) ObjectGraphWalker(org.apache.atlas.typesystem.types.ObjectGraphWalker) ArrayList(java.util.ArrayList) RepositoryException(org.apache.atlas.repository.RepositoryException) AtlasException(org.apache.atlas.AtlasException) ClassType(org.apache.atlas.typesystem.types.ClassType) MapIds(org.apache.atlas.typesystem.persistence.MapIds) IReferenceableInstance(org.apache.atlas.typesystem.IReferenceableInstance) TreeSet(java.util.TreeSet) Id(org.apache.atlas.typesystem.persistence.Id)

Example 18 with RepositoryException

use of org.apache.atlas.repository.RepositoryException in project incubator-atlas by apache.

the class MemRepository method get.

public ITypedReferenceableInstance get(Id id) throws RepositoryException {
    try {
        ReplaceIdWithInstance replacer = new ReplaceIdWithInstance(this);
        ObjectGraphWalker walker = new ObjectGraphWalker(typeSystem, replacer);
        replacer.setWalker(walker);
        ITypedReferenceableInstance r = getDuringWalk(id, walker);
        walker.walk();
        return r;
    } catch (AtlasException me) {
        throw new RepositoryException("TypeSystem error when walking the ObjectGraph", me);
    }
}
Also used : ITypedReferenceableInstance(org.apache.atlas.typesystem.ITypedReferenceableInstance) ObjectGraphWalker(org.apache.atlas.typesystem.types.ObjectGraphWalker) RepositoryException(org.apache.atlas.repository.RepositoryException) AtlasException(org.apache.atlas.AtlasException)

Example 19 with RepositoryException

use of org.apache.atlas.repository.RepositoryException in project incubator-atlas by apache.

the class MemRepository method getDuringWalk.

/*
     * - Id must be valid; Class must be valid.
     * - Ask ClassStore to createInstance.
     * - Ask ClassStore to load instance.
     * - load instance traits
     * - add to GraphWalker
     */
ITypedReferenceableInstance getDuringWalk(Id id, ObjectGraphWalker walker) throws RepositoryException {
    ClassStore cS = getClassStore(id.getTypeName());
    if (cS == null) {
        throw new RepositoryException(String.format("Unknown Class %s", id.getTypeName()));
    }
    cS.validate(this, id);
    ReferenceableInstance r = cS.createInstance(this, id);
    cS.load(r);
    for (String traitName : r.getTraits()) {
        HierarchicalTypeStore tt = typeStores.get(traitName);
        tt.load(r);
    }
    walker.addRoot(r);
    return r;
}
Also used : RepositoryException(org.apache.atlas.repository.RepositoryException) ReferenceableInstance(org.apache.atlas.typesystem.persistence.ReferenceableInstance) IReferenceableInstance(org.apache.atlas.typesystem.IReferenceableInstance) ITypedReferenceableInstance(org.apache.atlas.typesystem.ITypedReferenceableInstance)

Example 20 with RepositoryException

use of org.apache.atlas.repository.RepositoryException in project incubator-atlas by apache.

the class TypedInstanceToGraphMapper method addOrUpdateAttributesAndTraits.

private String addOrUpdateAttributesAndTraits(Operation operation, ITypedReferenceableInstance typedInstance) throws AtlasException {
    if (LOG.isDebugEnabled()) {
        LOG.debug("Adding/Updating typed instance {}", typedInstance.toShortString());
    }
    Id id = typedInstance.getId();
    if (id == null) {
        // oops
        throw new RepositoryException("id cannot be null");
    }
    AtlasVertex instanceVertex = idToVertexMap.get(id);
    // add the attributes for the instance
    ClassType classType = typeSystem.getDataType(ClassType.class, typedInstance.getTypeName());
    final Map<String, AttributeInfo> fields = classType.fieldMapping().fields;
    mapInstanceToVertex(typedInstance, instanceVertex, fields, false, operation);
    if (Operation.CREATE.equals(operation)) {
        // TODO - Handle Trait updates
        addTraits(typedInstance, instanceVertex, classType);
    }
    return getId(typedInstance)._getId();
}
Also used : AtlasVertex(org.apache.atlas.repository.graphdb.AtlasVertex) RepositoryException(org.apache.atlas.repository.RepositoryException) Id(org.apache.atlas.typesystem.persistence.Id)

Aggregations

RepositoryException (org.apache.atlas.repository.RepositoryException)31 AtlasBaseException (org.apache.atlas.exception.AtlasBaseException)12 AtlasVertex (org.apache.atlas.repository.graphdb.AtlasVertex)12 AtlasException (org.apache.atlas.AtlasException)11 AtlasEdge (org.apache.atlas.repository.graphdb.AtlasEdge)10 GraphTransaction (org.apache.atlas.annotation.GraphTransaction)6 AtlasGraphManagement (org.apache.atlas.repository.graphdb.AtlasGraphManagement)5 RequestContext (org.apache.atlas.RequestContext)4 ITypedReferenceableInstance (org.apache.atlas.typesystem.ITypedReferenceableInstance)4 CreateUpdateEntitiesResult (org.apache.atlas.CreateUpdateEntitiesResult)3 AtlasObjectId (org.apache.atlas.model.instance.AtlasObjectId)3 GuidMapping (org.apache.atlas.model.instance.GuidMapping)3 IReferenceableInstance (org.apache.atlas.typesystem.IReferenceableInstance)3 EntityExistsException (org.apache.atlas.typesystem.exception.EntityExistsException)3 EntityNotFoundException (org.apache.atlas.typesystem.exception.EntityNotFoundException)3 Id (org.apache.atlas.typesystem.persistence.Id)3 ArrayList (java.util.ArrayList)2 HashSet (java.util.HashSet)2 EntityResult (org.apache.atlas.model.legacy.EntityResult)2 AtlasBaseTypeDef (org.apache.atlas.model.typedef.AtlasBaseTypeDef)2