You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@atlas.apache.org by am...@apache.org on 2021/05/03 23:34:20 UTC

[atlas] branch master updated: ATLAS-4272: Changed the cache to thread local and clearing after import

This is an automated email from the ASF dual-hosted git repository.

amestry pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/atlas.git


The following commit(s) were added to refs/heads/master by this push:
     new 77914a3  ATLAS-4272: Changed the cache to thread local and clearing after import
77914a3 is described below

commit 77914a3a0dafc0975ed5654ea5394d4215e2d426
Author: sidmishra <si...@cloudera.com>
AuthorDate: Mon May 3 14:59:10 2021 -0700

    ATLAS-4272: Changed the cache to thread local and clearing after import
---
 .../java/org/apache/atlas/glossary/GlossaryService.java    | 14 +++++++++-----
 .../java/org/apache/atlas/glossary/GlossaryTermUtils.java  | 13 +++++++++----
 2 files changed, 18 insertions(+), 9 deletions(-)

diff --git a/repository/src/main/java/org/apache/atlas/glossary/GlossaryService.java b/repository/src/main/java/org/apache/atlas/glossary/GlossaryService.java
index be757ad..73217de 100644
--- a/repository/src/main/java/org/apache/atlas/glossary/GlossaryService.java
+++ b/repository/src/main/java/org/apache/atlas/glossary/GlossaryService.java
@@ -1125,13 +1125,17 @@ public class GlossaryService {
             throw new AtlasBaseException(AtlasErrorCode.INVALID_FILE_TYPE, fileName);
         }
 
-        List<String[]> fileData = FileUtils.readFileData(fileName, inputStream);
+        try {
+            List<String[]> fileData = FileUtils.readFileData(fileName, inputStream);
 
-        List<AtlasGlossaryTerm> glossaryTermsWithoutRelations = glossaryTermUtils.getGlossaryTermDataWithoutRelations(fileData, ret);
-        createGlossaryTerms(glossaryTermsWithoutRelations, ret);
+            List<AtlasGlossaryTerm> glossaryTermsWithoutRelations = glossaryTermUtils.getGlossaryTermDataWithoutRelations(fileData, ret);
+            createGlossaryTerms(glossaryTermsWithoutRelations, ret);
 
-        List<AtlasGlossaryTerm> glossaryTermsWithRelations = glossaryTermUtils.getGlossaryTermDataWithRelations(fileData, ret);
-        updateGlossaryTermsRelation(glossaryTermsWithRelations, ret);
+            List<AtlasGlossaryTerm> glossaryTermsWithRelations = glossaryTermUtils.getGlossaryTermDataWithRelations(fileData, ret);
+            updateGlossaryTermsRelation(glossaryTermsWithRelations, ret);
+        } finally {
+            glossaryTermUtils.clearImportCache();
+        }
 
         return ret;
     }
diff --git a/repository/src/main/java/org/apache/atlas/glossary/GlossaryTermUtils.java b/repository/src/main/java/org/apache/atlas/glossary/GlossaryTermUtils.java
index dc39fd2..553d3d0 100644
--- a/repository/src/main/java/org/apache/atlas/glossary/GlossaryTermUtils.java
+++ b/repository/src/main/java/org/apache/atlas/glossary/GlossaryTermUtils.java
@@ -49,6 +49,7 @@ import java.util.Arrays;
 import java.util.Collection;
 import java.util.Collections;
 import java.util.HashMap;
+import java.util.LinkedHashMap;
 import java.util.HashSet;
 import java.util.List;
 import java.util.Map;
@@ -65,7 +66,7 @@ public class GlossaryTermUtils extends GlossaryUtils {
     private static final int INDEX_FOR_GLOSSARY_AT_RECORD = 0;
     private static final int INDEX_FOR_TERM_AT_RECORD     = 1;
 
-    Map<String, String> glossaryNameGuidCacheForImport = new HashMap<>();
+    private static final ThreadLocal<Map<String, String>> glossaryNameGuidCacheForImport = ThreadLocal.withInitial(() -> new LinkedHashMap<>());
 
     protected GlossaryTermUtils(AtlasRelationshipStore relationshipStore, AtlasTypeRegistry typeRegistry, DataAccess dataAccess) {
         super(relationshipStore, typeRegistry, dataAccess);
@@ -148,6 +149,10 @@ public class GlossaryTermUtils extends GlossaryUtils {
         }
     }
 
+    public void clearImportCache() {
+        glossaryNameGuidCacheForImport.get().clear();
+    }
+
     private boolean isRelationshipGuidSame(AtlasRelatedObjectId storeObject, AtlasRelatedObjectId relatedObjectId) {
         return StringUtils.equals(relatedObjectId.getRelationshipGuid(), storeObject.getRelationshipGuid());
     }
@@ -555,7 +560,7 @@ public class GlossaryTermUtils extends GlossaryUtils {
             } else {
                 glossaryName = record[INDEX_FOR_GLOSSARY_AT_RECORD];
 
-                String glossaryGuid = glossaryNameGuidCacheForImport.get(glossaryName);
+                String glossaryGuid = glossaryNameGuidCacheForImport.get().get(glossaryName);
 
                 if (StringUtils.isEmpty(glossaryGuid)) {
                     glossaryGuid = getGlossaryGUIDFromGraphDB(glossaryName);
@@ -564,7 +569,7 @@ public class GlossaryTermUtils extends GlossaryUtils {
                         glossaryGuid = createGlossary(glossaryName, failedTermMsgs);
                     }
 
-                    glossaryNameGuidCacheForImport.put(glossaryName, glossaryGuid);
+                    glossaryNameGuidCacheForImport.get().put(glossaryName, glossaryGuid);
                 }
 
                 if (StringUtils.isNotEmpty(glossaryGuid)) {
@@ -599,7 +604,7 @@ public class GlossaryTermUtils extends GlossaryUtils {
             if (ArrayUtils.isNotEmpty(record) && StringUtils.isNotBlank(record[INDEX_FOR_GLOSSARY_AT_RECORD])) {
                 AtlasGlossaryTerm glossaryTerm = new AtlasGlossaryTerm();
                 String            glossaryName = record[INDEX_FOR_GLOSSARY_AT_RECORD];
-                String            glossaryGuid = glossaryNameGuidCacheForImport.get(glossaryName);
+                String            glossaryGuid = glossaryNameGuidCacheForImport.get().get(glossaryName);
 
                 if (StringUtils.isNotEmpty(glossaryGuid)) {
                     glossaryTerm = populateGlossaryTermObject(failedTermMsgs, record, glossaryGuid, true);