ericpai commented on code in PR #9149:
URL: https://github.com/apache/iotdb/pull/9149#discussion_r1123982379


##########
server/src/main/java/org/apache/iotdb/db/mpp/execution/memory/MemoryPool.java:
##########
@@ -169,32 +178,29 @@ public Pair<ListenableFuture<Void>, Boolean> reserve(
     }
 
     ListenableFuture<Void> result;
-    synchronized (this) {
-      if (maxBytes - reservedBytes < bytesToReserve
-          || maxBytesCanReserve
-                  - queryMemoryReservations
-                      .getOrDefault(queryId, Collections.emptyMap())
-                      .getOrDefault(fragmentInstanceId, Collections.emptyMap())
-                      .getOrDefault(planNodeId, 0L)
-              < bytesToReserve) {
-        LOGGER.debug(
-            "Blocked reserve request: {} bytes memory for planNodeId{}",
-            bytesToReserve,
-            planNodeId);
-        result =
-            MemoryReservationFuture.create(
-                queryId, fragmentInstanceId, planNodeId, bytesToReserve, 
maxBytesCanReserve);
-        memoryReservationFutures.add((MemoryReservationFuture<Void>) result);
-        return new Pair<>(result, Boolean.FALSE);
-      } else {
-        reservedBytes += bytesToReserve;
-        queryMemoryReservations
-            .computeIfAbsent(queryId, x -> new HashMap<>())
-            .computeIfAbsent(fragmentInstanceId, x -> new HashMap<>())
-            .merge(planNodeId, bytesToReserve, Long::sum);
-        result = Futures.immediateFuture(null);
-        return new Pair<>(result, Boolean.TRUE);
-      }
+    long tryRemainingBytes = remainingBytes.addAndGet(-bytesToReserve);

Review Comment:
   It seems that the codes of logic of try-reserving memory and releasing 
memory have been repeated many times, it should be refined to two private 
methods, e.g.
   
   ```java
   private boolean tryReserve() {
     long tryRemainingBytes = remainingBytes.addAndGet(-bytesToReserve);
     long queryRemainingBytes =
           maxBytesCanReserve
               - queryMemoryReservations
                   .computeIfAbsent(queryId, x -> new ConcurrentHashMap<>())
                   .computeIfAbsent(fragmentInstanceId, x -> new 
ConcurrentHashMap<>())
                   .merge(planNodeId, bytesToReserve, Long::sum);
       return tryRemainingBytes >= 0 && queryRemainingBytes >= 0;
   }
   
   private void rollbackReserve() {
         queryMemoryReservations
             .computeIfAbsent(queryId, x -> new ConcurrentHashMap<>())
             .computeIfAbsent(fragmentInstanceId, x -> new 
ConcurrentHashMap<>())
             .merge(planNodeId, -bytesToReserve, Long::sum);   
      remainingBytes.addAndGet(bytesToReserve);
   }
   ```



-- 
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: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to