@@ -110,10 +110,11 @@ void SharedArbitrator::reserveMemory(MemoryPool* pool, uint64_t /*unused*/) {
110110 pool->grow (reserveBytes);
111111}
112112
113- void SharedArbitrator::releaseMemory (MemoryPool* pool) {
113+ uint64_t SharedArbitrator::releaseMemory (MemoryPool* pool, uint64_t bytes ) {
114114 std::lock_guard<std::mutex> l (mutex_);
115- const uint64_t freedBytes = pool->shrink (0 );
115+ const uint64_t freedBytes = pool->shrink (bytes );
116116 incrementFreeCapacityLocked (freedBytes);
117+ return freedBytes;
117118}
118119
119120std::vector<SharedArbitrator::Candidate> SharedArbitrator::getCandidateStats (
@@ -196,52 +197,57 @@ bool SharedArbitrator::arbitrateMemory(
196197
197198 const uint64_t growTarget =
198199 std::max (minMemoryPoolCapacityTransferSize_, targetBytes);
199- uint64_t freedBytes = decrementFreeCapacity (growTarget);
200- if (freedBytes >= targetBytes) {
201- requestor->grow (freedBytes);
200+ uint64_t unusedFreedBytes = decrementFreeCapacity (growTarget);
201+
202+ auto freeGuard = folly::makeGuard ([&]() {
203+ // Returns the unused freed memory capacity back to the arbitrator.
204+ if (unusedFreedBytes > 0 ) {
205+ incrementFreeCapacity (unusedFreedBytes);
206+ }
207+ });
208+
209+ if (unusedFreedBytes >= targetBytes) {
210+ requestor->grow (unusedFreedBytes);
211+ unusedFreedBytes = 0 ;
202212 return true ;
203213 }
204- VELOX_CHECK_LT (freedBytes , growTarget);
214+ VELOX_CHECK_LT (unusedFreedBytes , growTarget);
205215
206- freedBytes +=
207- reclaimFreeMemoryFromCandidates (candidates, growTarget - freedBytes);
208- if (freedBytes >= targetBytes) {
209- requestor->grow (freedBytes);
216+ reclaimFreeMemoryFromCandidates (candidates, growTarget - unusedFreedBytes);
217+ unusedFreedBytes += decrementFreeCapacity (growTarget - unusedFreedBytes);
218+ if (unusedFreedBytes >= targetBytes) {
219+ requestor->grow (unusedFreedBytes);
220+ unusedFreedBytes = 0 ;
210221 return true ;
211222 }
212223
213- VELOX_CHECK_LT (freedBytes, growTarget);
214- freedBytes += reclaimUsedMemoryFromCandidates (
215- requestor, candidates, growTarget - freedBytes);
224+ VELOX_CHECK_LT (unusedFreedBytes, growTarget);
225+ reclaimUsedMemoryFromCandidates (
226+ requestor, candidates, growTarget - unusedFreedBytes);
227+ unusedFreedBytes += decrementFreeCapacity (growTarget - unusedFreedBytes);
216228 if (requestor->aborted ()) {
217- // Returns the unused freed memory capacity back to the arbitrator if the
218- // requestor has been aborted.
219- incrementFreeCapacity (freedBytes);
220229 ++numFailures_;
221230 VELOX_MEM_POOL_ABORTED (requestor);
222231 }
223232
224233 VELOX_CHECK (!requestor->aborted ());
225234
226- auto freeGuard = folly::makeGuard ([&]() {
227- // Returns the unused freed memory capacity back to the arbitrator.
228- if (freedBytes > 0 ) {
229- incrementFreeCapacity (freedBytes);
230- }
231- });
232-
233- if (freedBytes < targetBytes) {
235+ if (unusedFreedBytes < targetBytes) {
234236 VELOX_MEM_LOG (WARNING)
235237 << " Failed to arbitrate sufficient memory for memory pool "
236238 << requestor->name () << " , request " << succinctBytes (targetBytes)
237- << " , only " << succinctBytes (freedBytes )
239+ << " , only " << succinctBytes (unusedFreedBytes )
238240 << " has been freed, Arbitrator state: " << toString ();
239241 return false ;
240242 }
241243
242- const uint64_t growBytes = std::min (freedBytes, growTarget);
243- freedBytes -= growBytes;
244- requestor->grow (growBytes);
244+ if (unusedFreedBytes > growTarget) {
245+ requestor->grow (growTarget);
246+ unusedFreedBytes -= growTarget;
247+ return true ;
248+ }
249+ requestor->grow (unusedFreedBytes);
250+ unusedFreedBytes = 0 ;
245251 return true ;
246252}
247253
@@ -262,7 +268,9 @@ uint64_t SharedArbitrator::reclaimFreeMemoryFromCandidates(
262268 if (bytesToShrink <= 0 ) {
263269 break ;
264270 }
265- freedBytes += candidate.pool ->shrink (bytesToShrink);
271+ uint64_t shrunk = candidate.pool ->shrink (bytesToShrink);
272+ incrementFreeCapacity (shrunk);
273+ freedBytes += shrunk;
266274 if (freedBytes >= targetBytes) {
267275 break ;
268276 }
@@ -302,6 +310,7 @@ uint64_t SharedArbitrator::reclaim(
302310 uint64_t freedBytes{0 };
303311 try {
304312 freedBytes = pool->shrink (targetBytes);
313+ incrementFreeCapacity (freedBytes);
305314 if (freedBytes < targetBytes) {
306315 pool->reclaim (targetBytes - freedBytes);
307316 }
@@ -311,7 +320,7 @@ uint64_t SharedArbitrator::reclaim(
311320 abort (pool);
312321 // Free up all the free capacity from the aborted pool as the associated
313322 // query has failed at this point.
314- pool->shrink ();
323+ incrementFreeCapacity ( pool->shrink () );
315324 }
316325 const uint64_t newCapacity = pool->capacity ();
317326 VELOX_CHECK_GE (oldCapacity, newCapacity);
0 commit comments