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

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


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

commit c470a5cb454c9da46570479f960b3dd79a00b944
Author: sidmishra <sidmis...@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);

Reply via email to