private boolean updateBooking(MemoryConsumer consumer, int requestMB) { lock.lock(); try { ConsumerEntry entry = booking.get(consumer); if (entry == null) { if (requestMB == 0) return true; entry = new ConsumerEntry(consumer); booking.put(consumer, entry); } int delta = requestMB - entry.reservedMB; return updateBookingWithDelta(consumer, delta); } finally { lock.unlock(); } }
private void tryFreeUp(int gap) { // note don't hold lock when calling consumer.freeUp(), that method holding lock for itself and may cause deadlock for (ConsumerEntry entry : booking.values()) { int mb = entry.consumer.freeUp(gap); if (mb > 0) { lock.lock(); try { updateBookingWithDelta(entry.consumer, -mb); } finally { lock.unlock(); } gap -= mb; if (gap <= 0) break; } } if (gap > 0) throw new NotEnoughBudgetException(); if (debug) { if (getSystemAvailMB() < getRemainingBudgetMB()) { logger.debug("Remaining budget is " + getRemainingBudgetMB() + " MB free, but system only has " + getSystemAvailMB() + " MB free. If this persists, some memory calculation must be wrong."); } } }
private boolean updateBooking(MemoryConsumer consumer, int requestMB) { lock.lock(); try { ConsumerEntry entry = booking.get(consumer); if (entry == null) { if (requestMB == 0) return true; entry = new ConsumerEntry(consumer); booking.put(consumer, entry); } int delta = requestMB - entry.reservedMB; return updateBookingWithDelta(consumer, delta); } finally { lock.unlock(); } }
private void tryFreeUp(int gap) { // note don't hold lock when calling consumer.freeUp(), that method holding lock for itself and may cause deadlock for (ConsumerEntry entry : booking.values()) { int mb = entry.consumer.freeUp(gap); if (mb > 0) { lock.lock(); try { updateBookingWithDelta(entry.consumer, -mb); } finally { lock.unlock(); } gap -= mb; if (gap <= 0) break; } } if (gap > 0) throw new NotEnoughBudgetException(); if (debug) { if (getSystemAvailMB() < getRemainingBudgetMB()) { logger.debug("Remaining budget is " + getRemainingBudgetMB() + " MB free, but system only has " + getSystemAvailMB() + " MB free. If this persists, some memory calculation must be wrong."); } } }