You are viewing a plain text version of this content. The canonical link for it is here.
Posted to issues@bookkeeper.apache.org by GitBox <gi...@apache.org> on 2021/05/07 16:23:49 UTC

[GitHub] [bookkeeper] merlimat commented on a change in pull request #2710: Impose a memory limit on the bookie journal

merlimat commented on a change in pull request #2710:
URL: https://github.com/apache/bookkeeper/pull/2710#discussion_r628351476



##########
File path: bookkeeper-common/src/main/java/org/apache/bookkeeper/common/util/MemoryLimitController.java
##########
@@ -0,0 +1,84 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.bookkeeper.common.util;
+
+import java.util.concurrent.atomic.AtomicLong;
+import java.util.concurrent.locks.Condition;
+import java.util.concurrent.locks.ReentrantLock;
+
+public class MemoryLimitController {
+
+    private final long memoryLimit;
+    private final AtomicLong currentUsage = new AtomicLong();
+    private final ReentrantLock mutex = new ReentrantLock(false);
+    private final Condition condition = mutex.newCondition();
+
+    public MemoryLimitController(long memoryLimitBytes) {
+        this.memoryLimit = memoryLimitBytes;
+    }
+
+    public boolean tryReserveMemory(long size) {
+        while (true) {
+            long current = currentUsage.get();
+            long newUsage = current + size;
+
+            // We allow one request to go over the limit, to make the notification
+            // path simpler and more efficient
+            if (current > memoryLimit && memoryLimit > 0) {

Review comment:
       The reason is mostly to leave open the possibility to track how much memory is being used, even if a limit is not being enforced.




-- 
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.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org