Search in sources :

Example 6 with RequestContextV1

use of org.apache.atlas.RequestContextV1 in project atlas by apache.

the class DeleteHandlerV1 method deleteEdgeBetweenVertices.

/**
 * Deletes the edge between outvertex and inVertex. The edge is for attribute attributeName of outVertex
 * @param outVertex
 * @param inVertex
 * @param attribute
 * @throws AtlasException
 */
protected void deleteEdgeBetweenVertices(AtlasVertex outVertex, AtlasVertex inVertex, AtlasAttribute attribute) throws AtlasBaseException {
    if (LOG.isDebugEnabled()) {
        LOG.debug("Removing edge from {} to {} with attribute name {}", string(outVertex), string(inVertex), attribute.getName());
    }
    final String typeName = GraphHelper.getTypeName(outVertex);
    final String outId = GraphHelper.getGuid(outVertex);
    final AtlasEntity.Status state = getState(outVertex);
    if (state == DELETED || (outId != null && RequestContextV1.get().isDeletedEntity(outId))) {
        // If the reference vertex is marked for deletion, skip updating the reference
        return;
    }
    AtlasStructType parentType = (AtlasStructType) typeRegistry.getType(typeName);
    String propertyName = AtlasGraphUtilsV1.getQualifiedAttributePropertyKey(parentType, attribute.getName());
    String edgeLabel = EDGE_LABEL_PREFIX + propertyName;
    AtlasEdge edge = null;
    AtlasAttributeDef attrDef = attribute.getAttributeDef();
    AtlasType attrType = attribute.getAttributeType();
    switch(attrType.getTypeCategory()) {
        case OBJECT_ID_TYPE:
            {
                // If its class attribute, its the only edge between two vertices
                if (attrDef.getIsOptional()) {
                    edge = graphHelper.getEdgeForLabel(outVertex, edgeLabel);
                    if (shouldUpdateInverseReferences) {
                        GraphHelper.setProperty(outVertex, propertyName, null);
                    }
                } else {
                    // Cannot unset a required attribute.
                    throw new AtlasBaseException("Cannot unset required attribute " + propertyName + " on " + GraphHelper.vertexString(outVertex) + " edge = " + edgeLabel);
                }
            }
            break;
        case ARRAY:
            {
                // If its array attribute, find the right edge between the two vertices and update array property
                List<String> elements = GraphHelper.getListProperty(outVertex, propertyName);
                if (elements != null) {
                    // Make a copy, else list.remove reflects on titan.getProperty()
                    elements = new ArrayList<>(elements);
                    for (String elementEdgeId : elements) {
                        AtlasEdge elementEdge = graphHelper.getEdgeByEdgeId(outVertex, edgeLabel, elementEdgeId);
                        if (elementEdge == null) {
                            continue;
                        }
                        AtlasVertex elementVertex = elementEdge.getInVertex();
                        if (elementVertex.equals(inVertex)) {
                            edge = elementEdge;
                            // TODO element.size includes deleted items as well. should exclude
                            if (!attrDef.getIsOptional() && elements.size() <= attrDef.getValuesMinCount()) {
                                // Deleting this edge would violate the attribute's lower bound.
                                throw new AtlasBaseException("Cannot remove array element from required attribute " + propertyName + " on " + GraphHelper.getVertexDetails(outVertex) + " " + GraphHelper.getEdgeDetails(elementEdge));
                            }
                            if (shouldUpdateInverseReferences) {
                                // but when column is deleted, table will not reference the deleted column
                                if (LOG.isDebugEnabled()) {
                                    LOG.debug("Removing edge {} from the array attribute {}", string(elementEdge), attribute.getName());
                                }
                                // Remove all occurrences of the edge ID from the list.
                                // This prevents dangling edge IDs (i.e. edge IDs for deleted edges)
                                // from the remaining in the list if there are duplicates.
                                elements.removeAll(Collections.singletonList(elementEdge.getId().toString()));
                                GraphHelper.setProperty(outVertex, propertyName, elements);
                                break;
                            }
                        }
                    }
                }
            }
            break;
        case MAP:
            {
                // If its map attribute, find the right edge between two vertices and update map property
                List<String> keys = GraphHelper.getListProperty(outVertex, propertyName);
                if (keys != null) {
                    // Make a copy, else list.remove reflects on titan.getProperty()
                    keys = new ArrayList<>(keys);
                    for (String key : keys) {
                        String keyPropertyName = GraphHelper.getQualifiedNameForMapKey(propertyName, key);
                        String mapEdgeId = GraphHelper.getSingleValuedProperty(outVertex, keyPropertyName, String.class);
                        AtlasEdge mapEdge = graphHelper.getEdgeByEdgeId(outVertex, keyPropertyName, mapEdgeId);
                        if (mapEdge != null) {
                            AtlasVertex mapVertex = mapEdge.getInVertex();
                            if (mapVertex.getId().toString().equals(inVertex.getId().toString())) {
                                // TODO keys.size includes deleted items as well. should exclude
                                if (attrDef.getIsOptional() || keys.size() > attrDef.getValuesMinCount()) {
                                    edge = mapEdge;
                                } else {
                                    // Deleting this entry would violate the attribute's lower bound.
                                    throw new AtlasBaseException("Cannot remove map entry " + keyPropertyName + " from required attribute " + propertyName + " on " + GraphHelper.getVertexDetails(outVertex) + " " + GraphHelper.getEdgeDetails(mapEdge));
                                }
                                if (shouldUpdateInverseReferences) {
                                    // remove this key
                                    if (LOG.isDebugEnabled()) {
                                        LOG.debug("Removing edge {}, key {} from the map attribute {}", string(mapEdge), key, attribute.getName());
                                    }
                                    keys.remove(key);
                                    GraphHelper.setProperty(outVertex, propertyName, keys);
                                    GraphHelper.setProperty(outVertex, keyPropertyName, null);
                                }
                                break;
                            }
                        }
                    }
                }
            }
            break;
        case STRUCT:
        case CLASSIFICATION:
            break;
        default:
            throw new IllegalStateException("There can't be an edge from " + GraphHelper.getVertexDetails(outVertex) + " to " + GraphHelper.getVertexDetails(inVertex) + " with attribute name " + attribute.getName() + " which is not class/array/map attribute. found " + attrType.getTypeCategory().name());
    }
    if (edge != null) {
        deleteEdge(edge, false);
        RequestContextV1 requestContext = RequestContextV1.get();
        if (!requestContext.isUpdatedEntity(outId)) {
            GraphHelper.setProperty(outVertex, Constants.MODIFICATION_TIMESTAMP_PROPERTY_KEY, requestContext.getRequestTime());
            GraphHelper.setProperty(outVertex, Constants.MODIFIED_BY_KEY, requestContext.getUser());
            requestContext.recordEntityUpdate(entityRetriever.toAtlasObjectId(outVertex));
        }
    }
}
Also used : RequestContextV1(org.apache.atlas.RequestContextV1) AtlasStructType(org.apache.atlas.type.AtlasStructType) AtlasType(org.apache.atlas.type.AtlasType) AtlasEdge(org.apache.atlas.repository.graphdb.AtlasEdge) AtlasBaseException(org.apache.atlas.exception.AtlasBaseException) AtlasVertex(org.apache.atlas.repository.graphdb.AtlasVertex) AtlasEntity(org.apache.atlas.model.instance.AtlasEntity) AtlasAttributeDef(org.apache.atlas.model.typedef.AtlasStructDef.AtlasAttributeDef)

Example 7 with RequestContextV1

use of org.apache.atlas.RequestContextV1 in project atlas by apache.

the class AtlasInstanceConverter method getAndCacheEntity.

public AtlasEntity.AtlasEntityWithExtInfo getAndCacheEntity(String guid) throws AtlasBaseException {
    RequestContextV1 context = RequestContextV1.get();
    AtlasEntity.AtlasEntityWithExtInfo entityWithExtInfo = context.getInstanceV2(guid);
    if (entityWithExtInfo == null) {
        entityWithExtInfo = entityGraphRetriever.toAtlasEntityWithExtInfo(guid);
        if (entityWithExtInfo != null) {
            context.cache(entityWithExtInfo);
            if (LOG.isDebugEnabled()) {
                LOG.debug("Cache miss -> GUID = {}", guid);
            }
        }
    }
    return entityWithExtInfo;
}
Also used : RequestContextV1(org.apache.atlas.RequestContextV1) AtlasEntity(org.apache.atlas.model.instance.AtlasEntity)

Example 8 with RequestContextV1

use of org.apache.atlas.RequestContextV1 in project atlas by apache.

the class FullTextMapperV2 method getAndCacheEntity.

private AtlasEntityWithExtInfo getAndCacheEntity(String guid) throws AtlasBaseException {
    RequestContextV1 context = RequestContextV1.get();
    AtlasEntityWithExtInfo entityWithExtInfo = context.getInstanceV2(guid);
    if (entityWithExtInfo == null) {
        entityWithExtInfo = entityGraphRetriever.toAtlasEntityWithExtInfo(guid);
        if (entityWithExtInfo != null) {
            context.cache(entityWithExtInfo);
            if (LOG.isDebugEnabled()) {
                LOG.debug("Cache miss -> GUID = {}", guid);
            }
        }
    }
    return entityWithExtInfo;
}
Also used : AtlasEntityWithExtInfo(org.apache.atlas.model.instance.AtlasEntity.AtlasEntityWithExtInfo) RequestContextV1(org.apache.atlas.RequestContextV1)

Example 9 with RequestContextV1

use of org.apache.atlas.RequestContextV1 in project atlas by apache.

the class AuditFilter method doFilter.

@Override
public void doFilter(ServletRequest request, ServletResponse response, FilterChain filterChain) throws IOException, ServletException {
    final long startTime = System.currentTimeMillis();
    final Date requestTime = new Date();
    final HttpServletRequest httpRequest = (HttpServletRequest) request;
    final HttpServletResponse httpResponse = (HttpServletResponse) response;
    final String requestId = UUID.randomUUID().toString();
    final Thread currentThread = Thread.currentThread();
    final String oldName = currentThread.getName();
    final String user = AtlasAuthorizationUtils.getCurrentUserName();
    final Set<String> userGroups = AtlasAuthorizationUtils.getCurrentUserGroups();
    try {
        currentThread.setName(formatName(oldName, requestId));
        RequestContextV1.clear();
        RequestContextV1 requestContext = RequestContextV1.get();
        requestContext.setUser(user, userGroups);
        requestContext.setClientIPAddress(AtlasAuthorizationUtils.getRequestIpAddress(httpRequest));
        filterChain.doFilter(request, response);
    } finally {
        long timeTaken = System.currentTimeMillis() - startTime;
        recordAudit(httpRequest, requestTime, user, httpResponse.getStatus(), timeTaken);
        // put the request id into the response so users can trace logs for this request
        httpResponse.setHeader(AtlasClient.REQUEST_ID, requestId);
        currentThread.setName(oldName);
        RequestContextV1.clear();
    }
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) RequestContextV1(org.apache.atlas.RequestContextV1) HttpServletResponse(javax.servlet.http.HttpServletResponse) Date(java.util.Date)

Example 10 with RequestContextV1

use of org.apache.atlas.RequestContextV1 in project incubator-atlas by apache.

the class DeleteHandlerV1 method deleteEntities.

/**
 * Deletes the specified entity vertices.
 * Deletes any traits, composite entities, and structs owned by each entity.
 * Also deletes all the references from/to the entity.
 *
 * @param instanceVertices
 * @throws AtlasException
 */
public void deleteEntities(Collection<AtlasVertex> instanceVertices) throws AtlasBaseException {
    RequestContextV1 requestContext = RequestContextV1.get();
    Set<AtlasVertex> deletionCandidateVertices = new HashSet<>();
    for (AtlasVertex instanceVertex : instanceVertices) {
        String guid = AtlasGraphUtilsV1.getIdFromVertex(instanceVertex);
        AtlasEntity.Status state = AtlasGraphUtilsV1.getState(instanceVertex);
        if (state == AtlasEntity.Status.DELETED) {
            LOG.debug("Skipping deletion of {} as it is already deleted", guid);
            continue;
        }
        String typeName = AtlasGraphUtilsV1.getTypeName(instanceVertex);
        AtlasObjectId objId = new AtlasObjectId(guid, typeName);
        if (requestContext.getDeletedEntityIds().contains(objId)) {
            LOG.debug("Skipping deletion of {} as it is already deleted", guid);
            continue;
        }
        // Get GUIDs and vertices for all deletion candidates.
        Set<GraphHelper.VertexInfo> compositeVertices = getOwnedVertices(instanceVertex);
        // and gather deletion candidate vertices.
        for (GraphHelper.VertexInfo vertexInfo : compositeVertices) {
            requestContext.recordEntityDelete(new AtlasObjectId(vertexInfo.getGuid(), vertexInfo.getTypeName()));
            deletionCandidateVertices.add(vertexInfo.getVertex());
        }
    }
    // Delete traits and vertices.
    for (AtlasVertex deletionCandidateVertex : deletionCandidateVertices) {
        deleteAllTraits(deletionCandidateVertex);
        deleteTypeVertex(deletionCandidateVertex, false);
    }
}
Also used : AtlasVertex(org.apache.atlas.repository.graphdb.AtlasVertex) RequestContextV1(org.apache.atlas.RequestContextV1) AtlasEntity(org.apache.atlas.model.instance.AtlasEntity) GraphHelper(org.apache.atlas.repository.graph.GraphHelper) AtlasObjectId(org.apache.atlas.model.instance.AtlasObjectId) HashSet(java.util.HashSet) LinkedHashSet(java.util.LinkedHashSet)

Aggregations

RequestContextV1 (org.apache.atlas.RequestContextV1)15 AtlasVertex (org.apache.atlas.repository.graphdb.AtlasVertex)8 AtlasEntity (org.apache.atlas.model.instance.AtlasEntity)7 AtlasObjectId (org.apache.atlas.model.instance.AtlasObjectId)6 EntityMutationResponse (org.apache.atlas.model.instance.EntityMutationResponse)3 AtlasEdge (org.apache.atlas.repository.graphdb.AtlasEdge)3 AtlasStructType (org.apache.atlas.type.AtlasStructType)3 AtlasBaseException (org.apache.atlas.exception.AtlasBaseException)2 AtlasAttributeDef (org.apache.atlas.model.typedef.AtlasStructDef.AtlasAttributeDef)2 GraphHelper (org.apache.atlas.repository.graph.GraphHelper)2 AtlasEntityType (org.apache.atlas.type.AtlasEntityType)2 AtlasType (org.apache.atlas.type.AtlasType)2 Date (java.util.Date)1 HashSet (java.util.HashSet)1 LinkedHashSet (java.util.LinkedHashSet)1 HttpServletRequest (javax.servlet.http.HttpServletRequest)1 HttpServletResponse (javax.servlet.http.HttpServletResponse)1 AtlasEntityWithExtInfo (org.apache.atlas.model.instance.AtlasEntity.AtlasEntityWithExtInfo)1