From 1731329e3bc1806993bf6fc642372155e23a10ca Mon Sep 17 00:00:00 2001 From: Jaroslaw Date: Tue, 10 Dec 2019 09:08:47 +0100 Subject: [PATCH] DSP-19407 after upgrade review remarks - Reason for reverting incrementMemoryCounter implementation (from Sergio's comment): "..moving from a CAS loop to the double addAndGet() might be faster, but by doing so, a single big-ish allocation which goes over the limit could cause concurrent small allocations to fail as well before the memory limit is brought back below threshold via DIRECT_MEMORY_COUNTER.addAndGet(-capacity)" - scheduledTaskQueue is now protected to simplify DB-3884 "we might need to peek at the scheduled queue ourselves" --- .../concurrent/AbstractScheduledEventExecutor.java | 2 +- .../io/netty/util/internal/PlatformDependent.java | 12 +++++++----- 2 files changed, 8 insertions(+), 6 deletions(-) diff --git a/common/src/main/java/io/netty/util/concurrent/AbstractScheduledEventExecutor.java b/common/src/main/java/io/netty/util/concurrent/AbstractScheduledEventExecutor.java index b2421609492e..f563ddcb8437 100644 --- a/common/src/main/java/io/netty/util/concurrent/AbstractScheduledEventExecutor.java +++ b/common/src/main/java/io/netty/util/concurrent/AbstractScheduledEventExecutor.java @@ -38,7 +38,7 @@ public int compare(ScheduledFutureTask o1, ScheduledFutureTask o2) { } }; - PriorityQueue> scheduledTaskQueue; + protected PriorityQueue> scheduledTaskQueue; protected AbstractScheduledEventExecutor() { } diff --git a/common/src/main/java/io/netty/util/internal/PlatformDependent.java b/common/src/main/java/io/netty/util/internal/PlatformDependent.java index b7d69868b1d7..a43a534424a3 100644 --- a/common/src/main/java/io/netty/util/internal/PlatformDependent.java +++ b/common/src/main/java/io/netty/util/internal/PlatformDependent.java @@ -673,13 +673,15 @@ public static void freeDirectNoCleaner(ByteBuffer buffer) { } private static void incrementMemoryCounter(int capacity) { - if (DIRECT_MEMORY_COUNTER != null) { - long newUsedMemory = DIRECT_MEMORY_COUNTER.addAndGet(capacity); + for (;;) { + long usedMemory = DIRECT_MEMORY_COUNTER.get(); + long newUsedMemory = usedMemory + capacity; if (DIRECT_MEMORY_LIMIT > 0 && newUsedMemory > DIRECT_MEMORY_LIMIT) { - DIRECT_MEMORY_COUNTER.addAndGet(-capacity); throw new OutOfDirectMemoryError("failed to allocate " + capacity - + " byte(s) of direct memory (used: " + (newUsedMemory - capacity) - + ", max: " + DIRECT_MEMORY_LIMIT + ')'); + + " byte(s) of direct memory (used: " + usedMemory + ", max: " + DIRECT_MEMORY_LIMIT + ')'); + } + if (DIRECT_MEMORY_COUNTER.compareAndSet(usedMemory, newUsedMemory)) { + break; } } }