You are viewing a plain text version of this content. The canonical link for it is here.
Posted to gitbox@hive.apache.org by GitBox <gi...@apache.org> on 2021/05/24 15:13:10 UTC

[GitHub] [hive] belugabehr opened a new pull request #2312: HIVE-25157: Clean up QueryResultsCache Code

belugabehr opened a new pull request #2312:
URL: https://github.com/apache/hive/pull/2312


   <!--
   Thanks for sending a pull request!  Here are some tips for you:
     1. If this is your first time, please read our contributor guidelines: https://cwiki.apache.org/confluence/display/Hive/HowToContribute
     2. Ensure that you have created an issue on the Hive project JIRA: https://issues.apache.org/jira/projects/HIVE/summary
     3. Ensure you have added or run the appropriate tests for your PR: 
     4. If the PR is unfinished, add '[WIP]' in your PR title, e.g., '[WIP]HIVE-XXXXX:  Your PR title ...'.
     5. Be sure to keep the PR description updated to reflect all changes.
     6. Please write your PR title to summarize what this PR proposes.
     7. If possible, provide a concise example to reproduce the issue for a faster review.
   
   -->
   
   ### What changes were proposed in this pull request?
   <!--
   Please clarify what changes you are proposing. The purpose of this section is to outline the changes and how this PR fixes the issue. 
   If possible, please consider writing useful notes for better and faster reviews in your PR. See the examples below.
     1. If you refactor some codes with changing classes, showing the class hierarchy will help reviewers.
     2. If you fix some SQL features, you can provide some references of other DBMSes.
     3. If there is design documentation, please add the link.
     4. If there is a discussion in the mailing list, please add the link.
   -->
   
   
   ### Why are the changes needed?
   <!--
   Please clarify why the changes are needed. For instance,
     1. If you propose a new API, clarify the use case for a new API.
     2. If you fix a bug, you can clarify why it is a bug.
   -->
   
   
   ### Does this PR introduce _any_ user-facing change?
   <!--
   Note that it means *any* user-facing change including all aspects such as the documentation fix.
   If yes, please clarify the previous behavior and the change this PR proposes - provide the console output, description, screenshot and/or a reproducable example to show the behavior difference if possible.
   If possible, please also clarify if this is a user-facing change compared to the released Hive versions or within the unreleased branches such as master.
   If no, write 'No'.
   -->
   
   
   ### How was this patch tested?
   <!--
   If tests were added, say they were added here. Please make sure to add some test cases that check the changes thoroughly including negative and positive cases if possible.
   If it was tested in a way different from regular unit tests, please clarify how you tested step by step, ideally copy and paste-able, so that other reviewers can test and check, and descendants can verify in the future.
   If tests were not added, please describe why they were not added and/or why it was difficult to add.
   -->
   


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



---------------------------------------------------------------------
To unsubscribe, e-mail: gitbox-unsubscribe@hive.apache.org
For additional commands, e-mail: gitbox-help@hive.apache.org


[GitHub] [hive] belugabehr commented on a change in pull request #2312: HIVE-25157: Clean up QueryResultsCache Code

Posted by GitBox <gi...@apache.org>.
belugabehr commented on a change in pull request #2312:
URL: https://github.com/apache/hive/pull/2312#discussion_r639709076



##########
File path: ql/src/java/org/apache/hadoop/hive/ql/cache/results/QueryResultsCache.java
##########
@@ -814,37 +784,11 @@ private boolean clearSpaceForCacheEntry(CacheEntry entry, long size) {
       }
     }
 
-    LOG.info("Could not free enough space for cache entry for query: [{}] withe size {}",
+    LOG.info("Could not free enough space for cache entry for query: [{}] with size {}",
         entry.getQueryText(), size);
     return false;
   }
 
-  private static void addToEntryMap(Map<String, Set<CacheEntry>> entryMap,
-      String key, CacheEntry entry) {
-    Set<CacheEntry> entriesForKey = entryMap.get(key);
-    if (entriesForKey == null) {
-      entriesForKey = new HashSet<CacheEntry>();
-      entryMap.put(key, entriesForKey);
-    }
-    entriesForKey.add(entry);
-  }
-
-  private static boolean removeFromEntryMap(Map<String, Set<CacheEntry>> entryMap,
-      String key, CacheEntry entry) {
-    Set<CacheEntry> entries = entryMap.get(key);
-    if (entries == null) {
-      return false;
-    }
-    boolean deleted = entries.remove(entry);
-    if (!deleted) {
-      return false;
-    }
-    if (entries.isEmpty()) {
-      entryMap.remove(key);
-    }
-    return true;
-  }
-

Review comment:
       Using Guava `Multimap` makes this dead code




-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



---------------------------------------------------------------------
To unsubscribe, e-mail: gitbox-unsubscribe@hive.apache.org
For additional commands, e-mail: gitbox-help@hive.apache.org


[GitHub] [hive] belugabehr commented on a change in pull request #2312: HIVE-25157: Clean up QueryResultsCache Code

Posted by GitBox <gi...@apache.org>.
belugabehr commented on a change in pull request #2312:
URL: https://github.com/apache/hive/pull/2312#discussion_r639711891



##########
File path: ql/src/java/org/apache/hadoop/hive/ql/cache/results/QueryResultsCache.java
##########
@@ -319,9 +312,8 @@ public boolean waitForValidStatus() {
             // Status has not changed, continue waiting.
             break;
           }
-
           synchronized (this) {
-            this.wait(timeout);
+            this.wait();

Review comment:
       This thread is waiting on the state change.  Adding a timeout doesn't cause the loop to exit, instead the thread will just loop and see that there was no change and `wait` again.  This timeout only adds churn.




-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



---------------------------------------------------------------------
To unsubscribe, e-mail: gitbox-unsubscribe@hive.apache.org
For additional commands, e-mail: gitbox-help@hive.apache.org


[GitHub] [hive] belugabehr commented on a change in pull request #2312: HIVE-25157: Clean up QueryResultsCache Code

Posted by GitBox <gi...@apache.org>.
belugabehr commented on a change in pull request #2312:
URL: https://github.com/apache/hive/pull/2312#discussion_r639715093



##########
File path: ql/src/java/org/apache/hadoop/hive/ql/cache/results/QueryResultsCache.java
##########
@@ -726,35 +709,24 @@ private boolean entryMatches(LookupInfo lookupInfo, CacheEntry entry, Set<CacheE
 
   public void removeEntry(CacheEntry entry) {
     entry.invalidate();
-    rwLock.writeLock().lock();
+    cacheWriteLock.lock();
     try {
-      removeFromLookup(entry);
+      String queryString = entry.getQueryText();
+      if (!queryMap.remove(queryString, entry)) {
+        LOG.warn("Attempted to remove entry but it was not in the cache: {}", entry);
+      }
+
+      // Remove this entry from the table usage mappings.
+      entry.getTableNames().forEach(tableName -> tableToEntryMap.remove(tableName, entry));
+

Review comment:
       Moved out from having its own method




-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



---------------------------------------------------------------------
To unsubscribe, e-mail: gitbox-unsubscribe@hive.apache.org
For additional commands, e-mail: gitbox-help@hive.apache.org


[GitHub] [hive] klcopp commented on pull request #2312: HIVE-25157: Clean up QueryResultsCache Code

Posted by GitBox <gi...@apache.org>.
klcopp commented on pull request #2312:
URL: https://github.com/apache/hive/pull/2312#issuecomment-851459610


   @belugabehr  I don't in the next couple weeks, sorry :/ 


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



---------------------------------------------------------------------
To unsubscribe, e-mail: gitbox-unsubscribe@hive.apache.org
For additional commands, e-mail: gitbox-help@hive.apache.org


[GitHub] [hive] belugabehr commented on a change in pull request #2312: HIVE-25157: Clean up QueryResultsCache Code

Posted by GitBox <gi...@apache.org>.
belugabehr commented on a change in pull request #2312:
URL: https://github.com/apache/hive/pull/2312#discussion_r639710071



##########
File path: ql/src/java/org/apache/hadoop/hive/ql/cache/results/QueryResultsCache.java
##########
@@ -726,35 +709,24 @@ private boolean entryMatches(LookupInfo lookupInfo, CacheEntry entry, Set<CacheE
 
   public void removeEntry(CacheEntry entry) {
     entry.invalidate();
-    rwLock.writeLock().lock();
+    cacheWriteLock.lock();
     try {
-      removeFromLookup(entry);
+      String queryString = entry.getQueryText();
+      if (!queryMap.remove(queryString, entry)) {
+        LOG.warn("Attempted to remove entry but it was not in the cache: {}", entry);
+      }
+
+      // Remove this entry from the table usage mappings.
+      entry.getTableNames().forEach(tableName -> tableToEntryMap.remove(tableName, entry));
+
       lru.remove(entry);
       // Should the cache size be updated here, or after the result data has actually been deleted?
       cacheSize -= entry.size;
     } finally {
-      rwLock.writeLock().unlock();
+      cacheWriteLock.unlock();
     }
   }
 
-  private void removeFromLookup(CacheEntry entry) {
-    String queryString = entry.getQueryText();
-    if (!removeFromEntryMap(queryMap, queryString, entry)) {
-      LOG.warn("Attempted to remove entry but it was not in the cache: {}", entry);
-    }
-
-    // Remove this entry from the table usage mappings.
-    entry.getTableNames()
-        .forEach(tableName -> removeFromEntryMap(tableToEntryMap, tableName, entry));
-  }
-
-  private void calculateEntrySize(CacheEntry entry, FetchWork fetchWork) throws IOException {
-    Path queryResultsPath = fetchWork.getTblDir();
-    FileSystem resultsFs = queryResultsPath.getFileSystem(conf);
-    ContentSummary cs = resultsFs.getContentSummary(queryResultsPath);
-    entry.size = cs.getLength();
-  }
-

Review comment:
       Unused code.




-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



---------------------------------------------------------------------
To unsubscribe, e-mail: gitbox-unsubscribe@hive.apache.org
For additional commands, e-mail: gitbox-help@hive.apache.org


[GitHub] [hive] belugabehr commented on a change in pull request #2312: HIVE-25157: Clean up QueryResultsCache Code

Posted by GitBox <gi...@apache.org>.
belugabehr commented on a change in pull request #2312:
URL: https://github.com/apache/hive/pull/2312#discussion_r639708841



##########
File path: ql/src/java/org/apache/hadoop/hive/ql/cache/results/QueryResultsCache.java
##########
@@ -885,8 +824,6 @@ public void run() {
 
   private static void cleanupEntry(final CacheEntry entry) {
     Preconditions.checkState(entry.getStatus() == CacheEntryStatus.INVALID);
-    final HiveConf conf = getInstance().conf;
-

Review comment:
       Unused code.




-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



---------------------------------------------------------------------
To unsubscribe, e-mail: gitbox-unsubscribe@hive.apache.org
For additional commands, e-mail: gitbox-help@hive.apache.org


[GitHub] [hive] belugabehr commented on a change in pull request #2312: HIVE-25157: Clean up QueryResultsCache Code

Posted by GitBox <gi...@apache.org>.
belugabehr commented on a change in pull request #2312:
URL: https://github.com/apache/hive/pull/2312#discussion_r639714420



##########
File path: ql/src/java/org/apache/hadoop/hive/ql/cache/results/QueryResultsCache.java
##########
@@ -601,66 +591,59 @@ public boolean setEntryValid(CacheEntry cacheEntry, FetchWork fetchWork) {
   }
 
   public void clear() {
-    Lock writeLock = rwLock.writeLock();
+    cacheWriteLock.lock();
     try {
-      writeLock.lock();
       LOG.info("Clearing the results cache");
-      CacheEntry[] allEntries = null;
-      synchronized (lru) {
-        allEntries = lru.keySet().toArray(EMPTY_CACHEENTRY_ARRAY);
-      }
-      for (CacheEntry entry : allEntries) {

Review comment:
       The `cacheWrite` lock is held here so no need to synchronize on `lru`




-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



---------------------------------------------------------------------
To unsubscribe, e-mail: gitbox-unsubscribe@hive.apache.org
For additional commands, e-mail: gitbox-help@hive.apache.org


[GitHub] [hive] belugabehr commented on a change in pull request #2312: HIVE-25157: Clean up QueryResultsCache Code

Posted by GitBox <gi...@apache.org>.
belugabehr commented on a change in pull request #2312:
URL: https://github.com/apache/hive/pull/2312#discussion_r639711031



##########
File path: ql/src/java/org/apache/hadoop/hive/ql/cache/results/QueryResultsCache.java
##########
@@ -260,6 +252,7 @@ private CacheEntryStatus setStatus(CacheEntryStatus newStatus) {
       synchronized (this) {
         CacheEntryStatus oldStatus = status;
         status = newStatus;
+        this.notifyAll();
         return oldStatus;

Review comment:
       Notify waiting threads when the status changes.  This is the correct way to do this instead of putting `notifyAll` in a bunch of different places.




-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



---------------------------------------------------------------------
To unsubscribe, e-mail: gitbox-unsubscribe@hive.apache.org
For additional commands, e-mail: gitbox-help@hive.apache.org


[GitHub] [hive] github-actions[bot] closed pull request #2312: HIVE-25157: Clean up QueryResultsCache Code

Posted by GitBox <gi...@apache.org>.
github-actions[bot] closed pull request #2312:
URL: https://github.com/apache/hive/pull/2312


   


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: gitbox-unsubscribe@hive.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



---------------------------------------------------------------------
To unsubscribe, e-mail: gitbox-unsubscribe@hive.apache.org
For additional commands, e-mail: gitbox-help@hive.apache.org


[GitHub] [hive] belugabehr commented on a change in pull request #2312: HIVE-25157: Clean up QueryResultsCache Code

Posted by GitBox <gi...@apache.org>.
belugabehr commented on a change in pull request #2312:
URL: https://github.com/apache/hive/pull/2312#discussion_r639710520



##########
File path: ql/src/java/org/apache/hadoop/hive/ql/cache/results/QueryResultsCache.java
##########
@@ -338,15 +330,17 @@ public boolean waitForValidStatus() {
   }
 
   // Allow lookup by query string
-  private final Map<String, Set<CacheEntry>> queryMap = new HashMap<String, Set<CacheEntry>>();
+  @GuardedBy("cacheLock")
+  private final Multimap<String, CacheEntry> queryMap = HashMultimap.create();
 
   // LRU. Could also implement LRU as a doubly linked list if CacheEntry keeps its node.
   // Use synchronized map since even read actions cause the lru to get updated.
-  private final Map<CacheEntry, CacheEntry> lru = Collections.synchronizedMap(
-      new LinkedHashMap<CacheEntry, CacheEntry>(INITIAL_LRU_SIZE, LRU_LOAD_FACTOR, true));
+  private final Set<CacheEntry> lru =
+      Collections.synchronizedSet(Collections.newSetFromMap(new LinkedHashMap<>(16, 0.75f, true)));

Review comment:
       Use `Set` instead of Map with the same key/value




-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



---------------------------------------------------------------------
To unsubscribe, e-mail: gitbox-unsubscribe@hive.apache.org
For additional commands, e-mail: gitbox-help@hive.apache.org


[GitHub] [hive] belugabehr commented on pull request #2312: HIVE-25157: Clean up QueryResultsCache Code

Posted by GitBox <gi...@apache.org>.
belugabehr commented on pull request #2312:
URL: https://github.com/apache/hive/pull/2312#issuecomment-849271893


   @klcopp @miklosgergely If you have time. :)


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



---------------------------------------------------------------------
To unsubscribe, e-mail: gitbox-unsubscribe@hive.apache.org
For additional commands, e-mail: gitbox-help@hive.apache.org


[GitHub] [hive] belugabehr closed pull request #2312: HIVE-25157: Clean up QueryResultsCache Code

Posted by GitBox <gi...@apache.org>.
belugabehr closed pull request #2312:
URL: https://github.com/apache/hive/pull/2312


   


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



---------------------------------------------------------------------
To unsubscribe, e-mail: gitbox-unsubscribe@hive.apache.org
For additional commands, e-mail: gitbox-help@hive.apache.org


[GitHub] [hive] belugabehr closed pull request #2312: HIVE-25157: Clean up QueryResultsCache Code

Posted by GitBox <gi...@apache.org>.
belugabehr closed pull request #2312:
URL: https://github.com/apache/hive/pull/2312


   


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



---------------------------------------------------------------------
To unsubscribe, e-mail: gitbox-unsubscribe@hive.apache.org
For additional commands, e-mail: gitbox-help@hive.apache.org


[GitHub] [hive] belugabehr commented on a change in pull request #2312: HIVE-25157: Clean up QueryResultsCache Code

Posted by GitBox <gi...@apache.org>.
belugabehr commented on a change in pull request #2312:
URL: https://github.com/apache/hive/pull/2312#discussion_r639713660



##########
File path: ql/src/java/org/apache/hadoop/hive/ql/cache/results/QueryResultsCache.java
##########
@@ -493,20 +491,17 @@ public CacheEntry addToCache(QueryInfo queryInfo, ValidTxnWriteIdList txnWriteId
     addedEntry.queryInfo = queryInfo;
     addedEntry.txnWriteIdList = txnWriteIdList;
 
-    Lock writeLock = rwLock.writeLock();
+    cacheWriteLock.lock();
     try {
-      writeLock.lock();
-

Review comment:
       Move locks outside the try block as is the official Java Trails way.




-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



---------------------------------------------------------------------
To unsubscribe, e-mail: gitbox-unsubscribe@hive.apache.org
For additional commands, e-mail: gitbox-help@hive.apache.org


[GitHub] [hive] belugabehr commented on a change in pull request #2312: HIVE-25157: Clean up QueryResultsCache Code

Posted by GitBox <gi...@apache.org>.
belugabehr commented on a change in pull request #2312:
URL: https://github.com/apache/hive/pull/2312#discussion_r639715427



##########
File path: ql/src/java/org/apache/hadoop/hive/ql/cache/results/QueryResultsCache.java
##########
@@ -860,15 +804,10 @@ public static void cleanupInstance() {
     }
   }
 
-  private static ScheduledExecutorService invalidationExecutor = null;
-  private static ExecutorService deletionExecutor = null;
-
-  static {
-    ThreadFactory threadFactory =
-        new ThreadFactoryBuilder().setDaemon(true).setNameFormat("QueryResultsCache %d").build();
-    invalidationExecutor = Executors.newSingleThreadScheduledExecutor(threadFactory);
-    deletionExecutor = Executors.newSingleThreadExecutor(threadFactory);
-  }
+  private static ScheduledExecutorService invalidationExecutor = Executors.newSingleThreadScheduledExecutor(
+      new ThreadFactoryBuilder().setDaemon(true).setNameFormat("QueryCacheInvalidator %d").build());
+  private static ExecutorService deletionExecutor = Executors.newSingleThreadScheduledExecutor(
+      new ThreadFactoryBuilder().setDaemon(true).setNameFormat("QueryCacheDeletor %d").build());

Review comment:
       Re-factor and give each pool its own name.




-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



---------------------------------------------------------------------
To unsubscribe, e-mail: gitbox-unsubscribe@hive.apache.org
For additional commands, e-mail: gitbox-help@hive.apache.org


[GitHub] [hive] github-actions[bot] commented on pull request #2312: HIVE-25157: Clean up QueryResultsCache Code

Posted by GitBox <gi...@apache.org>.
github-actions[bot] commented on pull request #2312:
URL: https://github.com/apache/hive/pull/2312#issuecomment-890260563


   This pull request has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs.
   Feel free to reach out on the dev@hive.apache.org list if the patch is in need of reviews.


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: gitbox-unsubscribe@hive.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



---------------------------------------------------------------------
To unsubscribe, e-mail: gitbox-unsubscribe@hive.apache.org
For additional commands, e-mail: gitbox-help@hive.apache.org


[GitHub] [hive] belugabehr commented on a change in pull request #2312: HIVE-25157: Clean up QueryResultsCache Code

Posted by GitBox <gi...@apache.org>.
belugabehr commented on a change in pull request #2312:
URL: https://github.com/apache/hive/pull/2312#discussion_r639713247



##########
File path: ql/src/java/org/apache/hadoop/hive/ql/cache/results/QueryResultsCache.java
##########
@@ -416,58 +412,60 @@ public Path getCacheDirPath() {
 
   /**
    * Check if the cache contains an entry for the requested LookupInfo.
+   *
    * @param request
-   * @return  The cached result if there is a match in the cache, or null if no match is found.
+   * @return The cached result if there is a match in the cache, or null if no
+   *         match is found.
+   * @throws NullPointerException if request is {@code null}
    */
-  public CacheEntry lookup(LookupInfo request) {
-    CacheEntry result = null;
+  public CacheEntry lookup(final LookupInfo request) {
+    Objects.requireNonNull(request);
 
-    LOG.debug("QueryResultsCache lookup for query: {}", request.queryText);
+    LOG.debug("Cache lookup for query: {}", request.queryText);
 
+    CacheEntry result = null;
     boolean foundPending = false;
-    // Cannot entries while we currently hold read lock, so keep track of them to delete later.
-    Set<CacheEntry> entriesToRemove = new HashSet<CacheEntry>();
-    Lock readLock = rwLock.readLock();
+    // Cannot modify entries while we currently hold read lock, so keep track of
+    // them to delete later.
+    Set<CacheEntry> entriesToRemove = new HashSet<>();
+    cacheReadLock.lock();
     try {
-      // Note: ReentrantReadWriteLock deos not allow upgrading a read lock to a write lock.
+      // Note: ReentrantReadWriteLock does not allow upgrading a read lock to a write lock.
       // Care must be taken while under read lock, to make sure we do not perform any actions
       // which attempt to take a write lock.
-      readLock.lock();
-      Set<CacheEntry> candidates = queryMap.get(request.queryText);
-      if (candidates != null) {
-        CacheEntry pendingResult = null;
-        for (CacheEntry candidate : candidates) {
-          if (entryMatches(request, candidate, entriesToRemove)) {
-            CacheEntryStatus entryStatus = candidate.status;
-            if (entryStatus == CacheEntryStatus.VALID) {
-              result = candidate;
-              break;
-            } else if (entryStatus == CacheEntryStatus.PENDING && pendingResult == null) {
-              pendingResult = candidate;
-            }
+      Collection<CacheEntry> candidates = queryMap.get(request.queryText);
+      // Try to find valid entry, but settle for pending entry if that is all
+      // there is available
+      for (CacheEntry candidate : candidates) {
+        if (entryMatches(request, candidate, entriesToRemove)) {
+          CacheEntryStatus entryStatus = candidate.status;
+          if (entryStatus == CacheEntryStatus.VALID) {
+            result = candidate;
+            break;
+          }
+          if (entryStatus == CacheEntryStatus.PENDING) {
+            // Only accept first pending result
+            result = (result == null) ? candidate : result;

Review comment:
       `queryMap` is now a Guava `Multimap` and therefore does not return null values (it returns empty `Collection`).  Otherwise just simplifying the code.




-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



---------------------------------------------------------------------
To unsubscribe, e-mail: gitbox-unsubscribe@hive.apache.org
For additional commands, e-mail: gitbox-help@hive.apache.org


[GitHub] [hive] belugabehr commented on a change in pull request #2312: HIVE-25157: Clean up QueryResultsCache Code

Posted by GitBox <gi...@apache.org>.
belugabehr commented on a change in pull request #2312:
URL: https://github.com/apache/hive/pull/2312#discussion_r639712397



##########
File path: ql/src/java/org/apache/hadoop/hive/ql/cache/results/QueryResultsCache.java
##########
@@ -355,7 +349,9 @@ public boolean waitForValidStatus() {
   private long maxCacheSize;
   private long maxEntrySize;
   private long maxEntryLifetime;
-  private ReadWriteLock rwLock = new ReentrantReadWriteLock();
+  private final ReadWriteLock cacheLock = new ReentrantReadWriteLock();
+  private final Lock cacheReadLock = cacheLock.readLock();
+  private final Lock cacheWriteLock = cacheLock.writeLock();

Review comment:
       Breakout the `cacheLock` into its parts.  Simplifies code that relies on them.




-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



---------------------------------------------------------------------
To unsubscribe, e-mail: gitbox-unsubscribe@hive.apache.org
For additional commands, e-mail: gitbox-help@hive.apache.org


[GitHub] [hive] belugabehr commented on a change in pull request #2312: HIVE-25157: Clean up QueryResultsCache Code

Posted by GitBox <gi...@apache.org>.
belugabehr commented on a change in pull request #2312:
URL: https://github.com/apache/hive/pull/2312#discussion_r639709847



##########
File path: ql/src/java/org/apache/hadoop/hive/ql/cache/results/QueryResultsCache.java
##########
@@ -783,13 +755,11 @@ private boolean hasSpaceForCacheEntry(CacheEntry entry, long size) {
 
   private CacheEntry findEntryToRemove() {
     // Entries should be in LRU order in the keyset iterator.
-    Set<CacheEntry> entries = lru.keySet();
     synchronized (lru) {
-      for (CacheEntry removalCandidate : entries) {
-        if (removalCandidate.getStatus() != CacheEntryStatus.VALID) {
-          continue;
+      for (CacheEntry removalCandidate : lru) {
+        if (removalCandidate.getStatus() == CacheEntryStatus.VALID) {
+          return removalCandidate;
         }
-        return removalCandidate;

Review comment:
       Grab the `keySet` _inside_ of the synchronized block to avoid ConcurrentModification. Remove use of 'continue' keyword.




-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



---------------------------------------------------------------------
To unsubscribe, e-mail: gitbox-unsubscribe@hive.apache.org
For additional commands, e-mail: gitbox-help@hive.apache.org