jt2594838 commented on code in PR #18812:
URL: https://github.com/apache/iotdb/pull/18812#discussion_r4236167728
##########
iotdb-core/datanode/src/main/java/org/apache/iotdb/db/subscription/resource/SubscriptionMemoryManager.java:
##########
@@ -46,44 +64,321 @@ public SubscriptionMemoryManager(final long
totalMemorySizeInBytes) {
MEMORY_BLOCK_NAME, null, totalMemorySizeInBytes,
MemoryBlockType.DYNAMIC);
}
- /**
- * Reserves memory for materialized subscription data.
- *
- * <p>A single entry larger than the whole budget is allowed only while the
budget is otherwise
- * empty. This avoids permanently blocking progress while keeping the
overrun bounded by one
- * consensus entry.
- */
+ /** Registers a queue for fair-share accounting. */
+ public synchronized MemoryHandle registerQueue() {
+ final MemoryHandle handle = new MemoryHandle(++nextHandleId);
+ handles.put(handle.id, handle);
+ return handle;
+ }
+
+ /** Reserves memory through the compatibility owner. New queues should use a
handle. */
public synchronized boolean tryAllocate(final long sizeInBytes) {
+ return tryAllocateLegacy(sizeInBytes).isAccepted();
+ }
+
+ private AllocationResult tryAllocateLegacy(final long sizeInBytes) {
if (sizeInBytes <= 0L) {
- return true;
+ return AllocationResult.accepted(getTotalMemorySizeInBytes(),
getFreeMemorySizeInBytes());
}
- if (memoryBlock.allocate(sizeInBytes)) {
- return true;
+ final long total = getTotalMemorySizeInBytes();
+ final long used = getUsedMemorySizeInBytes();
+ if (total > 0L && used <= total && sizeInBytes <= total - used) {
+ if (!memoryBlock.allocate(sizeInBytes)) {
+ return AllocationResult.rejected(
+ AllocationRejectionReason.MEMORY_LIMIT, total,
getFreeMemorySizeInBytes());
+ }
+ legacyUsedMemoryInBytes += sizeInBytes;
+ return AllocationResult.accepted(total, total - used - sizeInBytes);
}
- if (memoryBlock.getTotalMemorySizeInBytes() > 0L
- && memoryBlock.getUsedMemoryInBytes() == 0L
- && sizeInBytes > memoryBlock.getTotalMemorySizeInBytes()) {
+ if (total > 0L && used == 0L && sizeInBytes > total) {
memoryBlock.forceAllocateWithoutLimitation(sizeInBytes);
- return true;
+ legacyUsedMemoryInBytes += sizeInBytes;
+ return AllocationResult.accepted(total, total - sizeInBytes);
}
- return false;
+ return AllocationResult.rejected(
+ sizeInBytes > total
+ ? AllocationRejectionReason.OVERSIZED_ENTRY
+ : AllocationRejectionReason.MEMORY_LIMIT,
+ total,
+ total - used);
}
public synchronized void release(final long sizeInBytes) {
- if (sizeInBytes > 0L) {
- memoryBlock.release(sizeInBytes);
+ if (sizeInBytes <= 0L) {
+ return;
+ }
+ final long released = Math.min(sizeInBytes, legacyUsedMemoryInBytes);
+ if (released > 0L) {
+ legacyUsedMemoryInBytes -= released;
+ memoryBlock.release(released);
}
}
- public long getTotalMemorySizeInBytes() {
+ public synchronized long getTotalMemorySizeInBytes() {
return memoryBlock.getTotalMemorySizeInBytes();
}
- public long getUsedMemorySizeInBytes() {
+ public synchronized long getUsedMemorySizeInBytes() {
return memoryBlock.getUsedMemoryInBytes();
}
- public long getFreeMemorySizeInBytes() {
+ public synchronized long getFreeMemorySizeInBytes() {
return memoryBlock.getFreeMemoryInBytes();
}
+
+ private synchronized AllocationResult tryAllocate(
+ final MemoryHandle handle, final long sizeInBytes) {
+ final AllocationResult decision = inspectAllocationUnderLock(handle,
sizeInBytes);
+ if (!decision.isAccepted() || sizeInBytes <= 0L) {
+ return decision;
+ }
+
+ if (!memoryBlock.allocate(sizeInBytes)) {
+ return AllocationResult.rejected(
+ AllocationRejectionReason.MEMORY_LIMIT,
+ handle.getMemoryQuotaInBytesUnderLock(),
+ getFreeMemorySizeInBytes());
+ }
+ handle.usedMemoryInBytes += sizeInBytes;
+ return AllocationResult.accepted(
+ handle.getMemoryQuotaInBytesUnderLock(),
handle.getFreeMemorySizeInBytes());
+ }
+
+ private AllocationResult inspectAllocationUnderLock(
+ final MemoryHandle handle, final long sizeInBytes) {
+ if (sizeInBytes <= 0L) {
+ return AllocationResult.accepted(
+ handle.getMemoryQuotaInBytes(), handle.getFreeMemorySizeInBytes());
+ }
+ if (!handle.active || handle.closed || !handles.containsKey(handle.id)) {
+ return AllocationResult.rejected(
+ AllocationRejectionReason.MEMORY_LIMIT, getTotalMemorySizeInBytes(),
0L);
+ }
Review Comment:
Why is this called MEMORY_LIMIT?
--
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]