Commit a064e092 by Madhan Neethiraj

ATLAS-2878: avoid retrieval of entiyWithExtInfo when extInfo is not needed

(cherry picked from commit 8e7ecf72f32ef6ca282a314d85761742e229a48b)
parent 48e52249
...@@ -180,8 +180,7 @@ public class EntityAuditListenerV2 implements EntityChangeListenerV2 { ...@@ -180,8 +180,7 @@ public class EntityAuditListenerV2 implements EntityChangeListenerV2 {
List<EntityAuditEventV2> events = new ArrayList<>(); List<EntityAuditEventV2> events = new ArrayList<>();
for (AtlasRelatedObjectId relatedObjectId : entities) { for (AtlasRelatedObjectId relatedObjectId : entities) {
AtlasEntityWithExtInfo entityWithExtInfo = instanceConverter.getAndCacheEntity(relatedObjectId.getGuid()); AtlasEntity entity = instanceConverter.getAndCacheEntity(relatedObjectId.getGuid());
AtlasEntity entity = (entityWithExtInfo != null) ? entityWithExtInfo.getEntity() : null;
if (entity != null) { if (entity != null) {
events.add(createEvent(entity, TERM_ADD, "Added term: " + term.toAuditString())); events.add(createEvent(entity, TERM_ADD, "Added term: " + term.toAuditString()));
...@@ -198,8 +197,7 @@ public class EntityAuditListenerV2 implements EntityChangeListenerV2 { ...@@ -198,8 +197,7 @@ public class EntityAuditListenerV2 implements EntityChangeListenerV2 {
List<EntityAuditEventV2> events = new ArrayList<>(); List<EntityAuditEventV2> events = new ArrayList<>();
for (AtlasRelatedObjectId relatedObjectId : entities) { for (AtlasRelatedObjectId relatedObjectId : entities) {
AtlasEntityWithExtInfo entityWithExtInfo = instanceConverter.getAndCacheEntity(relatedObjectId.getGuid()); AtlasEntity entity = instanceConverter.getAndCacheEntity(relatedObjectId.getGuid());
AtlasEntity entity = (entityWithExtInfo != null) ? entityWithExtInfo.getEntity() : null;
if (entity != null) { if (entity != null) {
events.add(createEvent(entity, TERM_DELETE, "Deleted term: " + term.toAuditString())); events.add(createEvent(entity, TERM_DELETE, "Deleted term: " + term.toAuditString()));
......
...@@ -28,6 +28,7 @@ import org.apache.atlas.model.TypeCategory; ...@@ -28,6 +28,7 @@ import org.apache.atlas.model.TypeCategory;
import org.apache.atlas.model.instance.AtlasClassification; import org.apache.atlas.model.instance.AtlasClassification;
import org.apache.atlas.model.instance.AtlasEntity; import org.apache.atlas.model.instance.AtlasEntity;
import org.apache.atlas.model.instance.AtlasEntity.AtlasEntitiesWithExtInfo; import org.apache.atlas.model.instance.AtlasEntity.AtlasEntitiesWithExtInfo;
import org.apache.atlas.model.instance.AtlasEntity.AtlasEntityWithExtInfo;
import org.apache.atlas.model.instance.AtlasEntityHeader; import org.apache.atlas.model.instance.AtlasEntityHeader;
import org.apache.atlas.model.instance.EntityMutationResponse; import org.apache.atlas.model.instance.EntityMutationResponse;
import org.apache.atlas.model.instance.EntityMutations.EntityOperation; import org.apache.atlas.model.instance.EntityMutations.EntityOperation;
...@@ -94,12 +95,12 @@ public class AtlasInstanceConverter { ...@@ -94,12 +95,12 @@ public class AtlasInstanceConverter {
} }
public Referenceable getReferenceable(String guid) throws AtlasBaseException { public Referenceable getReferenceable(String guid) throws AtlasBaseException {
AtlasEntity.AtlasEntityWithExtInfo entity = getAndCacheEntity(guid); AtlasEntityWithExtInfo entity = getAndCacheEntityExtInfo(guid);
return getReferenceable(entity); return getReferenceable(entity);
} }
public Referenceable getReferenceable(AtlasEntity.AtlasEntityWithExtInfo entity) throws AtlasBaseException { public Referenceable getReferenceable(AtlasEntityWithExtInfo entity) throws AtlasBaseException {
AtlasFormatConverter.ConverterContext ctx = new AtlasFormatConverter.ConverterContext(); AtlasFormatConverter.ConverterContext ctx = new AtlasFormatConverter.ConverterContext();
ctx.addEntity(entity.getEntity()); ctx.addEntity(entity.getEntity());
...@@ -291,10 +292,29 @@ public class AtlasInstanceConverter { ...@@ -291,10 +292,29 @@ public class AtlasInstanceConverter {
return ret; return ret;
} }
public AtlasEntity getAndCacheEntity(String guid) throws AtlasBaseException {
RequestContext context = RequestContext.get();
AtlasEntity entity = context.getEntity(guid);
if (entity == null) {
entity = entityGraphRetriever.toAtlasEntity(guid);
if (entity != null) {
context.cache(entity);
if (LOG.isDebugEnabled()) {
LOG.debug("Cache miss -> GUID = {}", guid);
}
}
}
return entity;
}
public AtlasEntity.AtlasEntityWithExtInfo getAndCacheEntity(String guid) throws AtlasBaseException { public AtlasEntityWithExtInfo getAndCacheEntityExtInfo(String guid) throws AtlasBaseException {
RequestContext context = RequestContext.get(); RequestContext context = RequestContext.get();
AtlasEntity.AtlasEntityWithExtInfo entityWithExtInfo = context.getInstanceV2(guid); AtlasEntityWithExtInfo entityWithExtInfo = context.getEntityWithExtInfo(guid);
if (entityWithExtInfo == null) { if (entityWithExtInfo == null) {
entityWithExtInfo = entityGraphRetriever.toAtlasEntityWithExtInfo(guid); entityWithExtInfo = entityGraphRetriever.toAtlasEntityWithExtInfo(guid);
......
...@@ -75,7 +75,15 @@ public class FullTextMapperV2 { ...@@ -75,7 +75,15 @@ public class FullTextMapperV2 {
*/ */
public String getIndexTextForClassifications(String guid, List<AtlasClassification> classifications) throws AtlasBaseException { public String getIndexTextForClassifications(String guid, List<AtlasClassification> classifications) throws AtlasBaseException {
String ret = null; String ret = null;
AtlasEntityWithExtInfo entityWithExtInfo = getAndCacheEntity(guid); final AtlasEntityWithExtInfo entityWithExtInfo;
if (followReferences) {
entityWithExtInfo = getAndCacheEntityWithExtInfo(guid);
} else {
AtlasEntity entity = getAndCacheEntity(guid);
entityWithExtInfo = entity != null ? new AtlasEntityWithExtInfo(entity) : null;
}
if (entityWithExtInfo != null) { if (entityWithExtInfo != null) {
StringBuilder sb = new StringBuilder(); StringBuilder sb = new StringBuilder();
...@@ -102,12 +110,12 @@ public class FullTextMapperV2 { ...@@ -102,12 +110,12 @@ public class FullTextMapperV2 {
public String getIndexTextForEntity(String guid) throws AtlasBaseException { public String getIndexTextForEntity(String guid) throws AtlasBaseException {
String ret = null; String ret = null;
AtlasEntityWithExtInfo entity = getAndCacheEntity(guid); AtlasEntity entity = getAndCacheEntity(guid);
if (entity != null) { if (entity != null) {
StringBuilder sb = new StringBuilder(); StringBuilder sb = new StringBuilder();
map(entity.getEntity(), entity, sb, new HashSet<String>()); map(entity, null, sb, new HashSet<String>());
ret = sb.toString(); ret = sb.toString();
} }
...@@ -166,7 +174,7 @@ public class FullTextMapperV2 { ...@@ -166,7 +174,7 @@ public class FullTextMapperV2 {
private void mapAttribute(Object value, AtlasEntityExtInfo entityExtInfo, StringBuilder sb, Set<String> processedGuids) throws AtlasBaseException { private void mapAttribute(Object value, AtlasEntityExtInfo entityExtInfo, StringBuilder sb, Set<String> processedGuids) throws AtlasBaseException {
if (value instanceof AtlasObjectId) { if (value instanceof AtlasObjectId) {
if (followReferences) { if (followReferences && entityExtInfo != null) {
AtlasObjectId objectId = (AtlasObjectId) value; AtlasObjectId objectId = (AtlasObjectId) value;
AtlasEntity entity = entityExtInfo.getEntity(objectId.getGuid()); AtlasEntity entity = entityExtInfo.getEntity(objectId.getGuid());
...@@ -203,9 +211,28 @@ public class FullTextMapperV2 { ...@@ -203,9 +211,28 @@ public class FullTextMapperV2 {
} }
} }
private AtlasEntityWithExtInfo getAndCacheEntity(String guid) throws AtlasBaseException { private AtlasEntity getAndCacheEntity(String guid) throws AtlasBaseException {
RequestContext context = RequestContext.get();
AtlasEntity entity = context.getEntity(guid);
if (entity == null) {
entity = entityGraphRetriever.toAtlasEntity(guid);
if (entity != null) {
context.cache(entity);
if (LOG.isDebugEnabled()) {
LOG.debug("Cache miss -> GUID = {}", guid);
}
}
}
return entity;
}
private AtlasEntityWithExtInfo getAndCacheEntityWithExtInfo(String guid) throws AtlasBaseException {
RequestContext context = RequestContext.get(); RequestContext context = RequestContext.get();
AtlasEntityWithExtInfo entityWithExtInfo = context.getInstanceV2(guid); AtlasEntityWithExtInfo entityWithExtInfo = context.getEntityWithExtInfo(guid);
if (entityWithExtInfo == null) { if (entityWithExtInfo == null) {
// Only map ownedRef and relationship attr when follow references is set to true // Only map ownedRef and relationship attr when follow references is set to true
......
...@@ -254,8 +254,7 @@ public class AtlasEntityChangeNotifier { ...@@ -254,8 +254,7 @@ public class AtlasEntityChangeNotifier {
continue; continue;
} }
AtlasEntityWithExtInfo entityWithExtInfo = instanceConverter.getAndCacheEntity(guid); AtlasEntity entity = instanceConverter.getAndCacheEntity(guid);
AtlasEntity entity = entityWithExtInfo != null ? entityWithExtInfo.getEntity() : null;
if (entity == null) { if (entity == null) {
continue; continue;
...@@ -413,9 +412,7 @@ public class AtlasEntityChangeNotifier { ...@@ -413,9 +412,7 @@ public class AtlasEntityChangeNotifier {
entity.setGuid(entityGuid); entity.setGuid(entityGuid);
} else { } else {
AtlasEntityWithExtInfo entityWithExtInfo = instanceConverter.getAndCacheEntity(entityGuid); entity = instanceConverter.getAndCacheEntity(entityGuid);
entity = (entityWithExtInfo != null) ? entityWithExtInfo.getEntity() : null;
} }
if (entity != null) { if (entity != null) {
......
...@@ -1422,8 +1422,7 @@ public class EntityGraphMapper { ...@@ -1422,8 +1422,7 @@ public class EntityGraphMapper {
for (AtlasVertex vertex : notificationVertices) { for (AtlasVertex vertex : notificationVertices) {
String entityGuid = GraphHelper.getGuid(vertex); String entityGuid = GraphHelper.getGuid(vertex);
AtlasEntityWithExtInfo entityWithExtInfo = instanceConverter.getAndCacheEntity(entityGuid); AtlasEntity entity = instanceConverter.getAndCacheEntity(entityGuid);
AtlasEntity entity = (entityWithExtInfo != null) ? entityWithExtInfo.getEntity() : null;
List<AtlasClassification> addedClassifications = StringUtils.equals(entityGuid, guid) ? addClassifications : propagations.get(vertex); List<AtlasClassification> addedClassifications = StringUtils.equals(entityGuid, guid) ? addClassifications : propagations.get(vertex);
if (CollectionUtils.isNotEmpty(addedClassifications)) { if (CollectionUtils.isNotEmpty(addedClassifications)) {
...@@ -1529,8 +1528,7 @@ public class EntityGraphMapper { ...@@ -1529,8 +1528,7 @@ public class EntityGraphMapper {
for (Map.Entry<AtlasVertex, List<AtlasClassification>> entry : removedClassifications.entrySet()) { for (Map.Entry<AtlasVertex, List<AtlasClassification>> entry : removedClassifications.entrySet()) {
String guid = GraphHelper.getGuid(entry.getKey()); String guid = GraphHelper.getGuid(entry.getKey());
List<AtlasClassification> deletedClassificationNames = entry.getValue(); List<AtlasClassification> deletedClassificationNames = entry.getValue();
AtlasEntityWithExtInfo entityWithExtInfo = instanceConverter.getAndCacheEntity(guid); AtlasEntity entity = instanceConverter.getAndCacheEntity(guid);
AtlasEntity entity = (entityWithExtInfo != null) ? entityWithExtInfo.getEntity() : null;
entityChangeNotifier.onClassificationDeletedFromEntity(entity, deletedClassificationNames); entityChangeNotifier.onClassificationDeletedFromEntity(entity, deletedClassificationNames);
} }
...@@ -1687,8 +1685,7 @@ public class EntityGraphMapper { ...@@ -1687,8 +1685,7 @@ public class EntityGraphMapper {
for (AtlasVertex vertex : notificationVertices) { for (AtlasVertex vertex : notificationVertices) {
String entityGuid = GraphHelper.getGuid(vertex); String entityGuid = GraphHelper.getGuid(vertex);
AtlasEntityWithExtInfo entityWithExtInfo = instanceConverter.getAndCacheEntity(entityGuid); AtlasEntity entity = instanceConverter.getAndCacheEntity(entityGuid);
AtlasEntity entity = (entityWithExtInfo != null) ? entityWithExtInfo.getEntity() : null;
if (isActive(entity)) { if (isActive(entity)) {
entityChangeNotifier.onClassificationUpdatedToEntity(entity, updatedClassifications); entityChangeNotifier.onClassificationUpdatedToEntity(entity, updatedClassifications);
...@@ -1700,8 +1697,7 @@ public class EntityGraphMapper { ...@@ -1700,8 +1697,7 @@ public class EntityGraphMapper {
AtlasVertex vertex = entry.getKey(); AtlasVertex vertex = entry.getKey();
List<AtlasClassification> removedClassifications = entry.getValue(); List<AtlasClassification> removedClassifications = entry.getValue();
String entityGuid = GraphHelper.getGuid(vertex); String entityGuid = GraphHelper.getGuid(vertex);
AtlasEntityWithExtInfo entityWithExtInfo = instanceConverter.getAndCacheEntity(entityGuid); AtlasEntity entity = instanceConverter.getAndCacheEntity(entityGuid);
AtlasEntity entity = (entityWithExtInfo != null) ? entityWithExtInfo.getEntity() : null;
if (isActive(entity)) { if (isActive(entity)) {
entityChangeNotifier.onClassificationDeletedFromEntity(entity, removedClassifications); entityChangeNotifier.onClassificationDeletedFromEntity(entity, removedClassifications);
......
...@@ -35,7 +35,8 @@ public class RequestContext { ...@@ -35,7 +35,8 @@ public class RequestContext {
private final Map<String, AtlasObjectId> updatedEntities = new HashMap<>(); private final Map<String, AtlasObjectId> updatedEntities = new HashMap<>();
private final Map<String, AtlasObjectId> deletedEntities = new HashMap<>(); private final Map<String, AtlasObjectId> deletedEntities = new HashMap<>();
private final Map<String, AtlasEntityWithExtInfo> entityCacheV2 = new HashMap<>(); private final Map<String, AtlasEntity> entityCache = new HashMap<>();
private final Map<String, AtlasEntityWithExtInfo> entityExtInfoCache = new HashMap<>();
private final Map<String, List<AtlasClassification>> addedPropagations = new HashMap<>(); private final Map<String, List<AtlasClassification>> addedPropagations = new HashMap<>();
private final Map<String, List<AtlasClassification>> removedPropagations = new HashMap<>(); private final Map<String, List<AtlasClassification>> removedPropagations = new HashMap<>();
private final long requestTime = System.currentTimeMillis(); private final long requestTime = System.currentTimeMillis();
...@@ -70,7 +71,8 @@ public class RequestContext { ...@@ -70,7 +71,8 @@ public class RequestContext {
if (instance != null) { if (instance != null) {
instance.updatedEntities.clear(); instance.updatedEntities.clear();
instance.deletedEntities.clear(); instance.deletedEntities.clear();
instance.entityCacheV2.clear(); instance.entityCache.clear();
instance.entityExtInfoCache.clear();
instance.addedPropagations.clear(); instance.addedPropagations.clear();
instance.removedPropagations.clear(); instance.removedPropagations.clear();
...@@ -174,10 +176,18 @@ public class RequestContext { ...@@ -174,10 +176,18 @@ public class RequestContext {
*/ */
public void cache(AtlasEntityWithExtInfo entity) { public void cache(AtlasEntityWithExtInfo entity) {
if (entity != null && entity.getEntity() != null && entity.getEntity().getGuid() != null) { if (entity != null && entity.getEntity() != null && entity.getEntity().getGuid() != null) {
entityCacheV2.put(entity.getEntity().getGuid(), entity); entityExtInfoCache.put(entity.getEntity().getGuid(), entity);
entityCache.put(entity.getEntity().getGuid(), entity.getEntity());
} }
} }
public void cache(AtlasEntity entity) {
if (entity != null && entity.getGuid() != null) {
entityCache.put(entity.getGuid(), entity);
}
}
public Collection<AtlasObjectId> getUpdatedEntities() { public Collection<AtlasObjectId> getUpdatedEntities() {
return updatedEntities.values(); return updatedEntities.values();
} }
...@@ -193,8 +203,12 @@ public class RequestContext { ...@@ -193,8 +203,12 @@ public class RequestContext {
* @param guid the guid to find * @param guid the guid to find
* @return Either the instance or null if it is not in the cache. * @return Either the instance or null if it is not in the cache.
*/ */
public AtlasEntityWithExtInfo getInstanceV2(String guid) { public AtlasEntityWithExtInfo getEntityWithExtInfo(String guid) {
return entityCacheV2.get(guid); return entityExtInfoCache.get(guid);
}
public AtlasEntity getEntity(String guid) {
return entityCache.get(guid);
} }
public long getRequestTime() { public long getRequestTime() {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment