[ 
https://issues.apache.org/jira/browse/HIVE-21686?focusedWorklogId=241448&page=com.atlassian.jira.plugin.system.issuetabpanels:worklog-tabpanel#worklog-241448
 ]

ASF GitHub Bot logged work on HIVE-21686:
-----------------------------------------

                Author: ASF GitHub Bot
            Created on: 14/May/19 02:01
            Start Date: 14/May/19 02:01
    Worklog Time Spent: 10m 
      Work Description: b-slim commented on pull request #626: [HIVE-21686] 
ensure that memory allocator does not evict using brute …
URL: https://github.com/apache/hive/pull/626#discussion_r283600622
 
 

 ##########
 File path: 
llap-server/src/java/org/apache/hadoop/hive/llap/cache/BuddyAllocator.java
 ##########
 @@ -277,7 +301,74 @@ public void allocateMultiple(MemoryBuffer[] dest, int 
size, BufferObjectFactory
     long threadId = arenaCount > 1 ? Thread.currentThread().getId() : 0;
     int destAllocIx = allocateFast(dest, null, 0, dest.length,
         freeListIx, allocationSize, (int)(threadId % arenaCount), arenaCount);
-    if (destAllocIx == dest.length) return;
+    if (destAllocIx == dest.length) {
+      return;
+    }
+    // Try to allocate memory if we haven't allocated all the way to maxSize 
yet; very rare.
+    destAllocIx = allocateWithExpand(dest, destAllocIx, freeListIx, 
allocationSize, arenaCount);
+    if (destAllocIx == dest.length) {
+      return;
+    }
+
+    int forceEvictAttempt = 0;
+    long totalForceEvictedBytes = 0;
+    long currentEvicted;
+    int startArenaIx;
+    int emptyAttempt = 0;
+
+    // We called reserveMemory so we know that there's memory waiting for us 
somewhere.
+    // But that can mean that the reserved memory is fragmented thus unusable
+    // or we have a common class of rare race conditions related to the order 
of locking/checking of
+    // different allocation areas this is due to the fact that Allocations 
arrive in a burst fashion.
+    // To mitigate that:
+    //  - First we try again (fast path):
+    //  - Second try to push on the memoryManger/evictor to evict more.
+    //  - Third by pass the memory manager and use discards method to brute 
force evict if needed.
+    while (totalForceEvictedBytes < maxForcedEvictionSize && emptyAttempt < 
MAX_DISCARD_ATTEMPTS) {
+      // make sure we hit a different start on each new attempt
+      startArenaIx = (int) ((threadId + forceEvictAttempt) % arenaCount);
+      destAllocIx =
+          allocateFast(dest, null, destAllocIx, dest.length, freeListIx, 
allocationSize, startArenaIx, arenaCount);
+      if (destAllocIx == dest.length) {
+        return;
+      }
+      destAllocIx =
+          allocateWithSplit(dest,
+              null,
+              destAllocIx,
+              dest.length,
+              freeListIx,
+              allocationSize,
+              startArenaIx,
+              arenaCount,
+              -1);
+      if (destAllocIx == dest.length) {
+        return;
+      }
+
+      if (forceEvictAttempt >= MAX_FAST_ATTEMPT) {
+        // Try to evict more starting from how much memory we are missing 
allocLog2 << dest.length - destAllocIx
+        // Exponentially increase the eviction request based on 
forceEvictAttempt counter.
+        // Why Exponentially increase, usually query fragments triggers a 
burst of allocation at the same time
+        // Thus IMO to avoid lock contentions it is worth to over evict thus 
next allocation will get free lunch.
+        currentEvicted =
+            memoryManager.evictMemory(allocLog2 << dest.length - destAllocIx + 
forceEvictAttempt - MAX_FAST_ATTEMPT);
 
 Review comment:
   good catch @odraese 
 
----------------------------------------------------------------
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:
us...@infra.apache.org


Issue Time Tracking
-------------------

    Worklog Id:     (was: 241448)
    Time Spent: 2h 50m  (was: 2h 40m)

> Brute Force eviction can lead to a random uncontrolled eviction pattern.
> ------------------------------------------------------------------------
>
>                 Key: HIVE-21686
>                 URL: https://issues.apache.org/jira/browse/HIVE-21686
>             Project: Hive
>          Issue Type: Bug
>            Reporter: slim bouguerra
>            Assignee: slim bouguerra
>            Priority: Major
>              Labels: pull-request-available
>         Attachments: Cache_hitrate_improvement.csv, HIVE-21686.2.patch, 
> HIVE-21686.patch
>
>          Time Spent: 2h 50m
>  Remaining Estimate: 0h
>
> Current logic used by brute force eviction can lead to a perpetual random 
> eviction pattern.
> For instance if the cache build a small pocket of free memory where the total 
> size is greater than incoming allocation request, the allocator will randomly 
> evict block that fits a particular size.
> This can happen over and over therefore all the eviction will be random.
> In Addition this random eviction will lead a leak in the linked list 
> maintained by the policy since it does not know anymore about what is evicted 
> and what not.
> The improvement of this patch is very substantial  to TPC-DS benchmark. I 
> have tested it with 10TB scale 9 llap nodes and 32GB cache size per node.  
> The patch has showed very noticeable difference in the Hit rate for raw 
> number  [^Cache_hitrate_improvement.csv] 



--
This message was sent by Atlassian JIRA
(v7.6.3#76005)

Reply via email to