You are viewing a plain text version of this content. The canonical link for it is here.
Posted to reviews@iotdb.apache.org by GitBox <gi...@apache.org> on 2022/07/13 02:29:29 UTC

[GitHub] [iotdb] SzyWilliam opened a new pull request, #6657: [IOTDB-3650] Add memory control for RatisConsensus

SzyWilliam opened a new pull request, #6657:
URL: https://github.com/apache/iotdb/pull/6657

   Control:
   1. queued write requests
   2. heap memory usage
   3. direct memory usage


-- 
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: reviews-unsubscribe@iotdb.apache.org

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


[GitHub] [iotdb] neuyilan commented on a diff in pull request #6657: [IOTDB-3650] Add memory control for RatisConsensus

Posted by GitBox <gi...@apache.org>.
neuyilan commented on code in PR #6657:
URL: https://github.com/apache/iotdb/pull/6657#discussion_r924041816


##########
consensus/src/main/java/org/apache/iotdb/consensus/ratis/MemChecker.java:
##########
@@ -0,0 +1,132 @@
+/*
+ * 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.iotdb.consensus.ratis;
+
+import org.apache.ratis.protocol.Message;
+
+import java.util.concurrent.atomic.AtomicLong;
+
+public class MemChecker {
+  private final long maxAllowedDirectBytes;
+  private final long maxAllowedHeapBytes;
+  private final long maxQueueingRequests;
+
+  private final AtomicLong usedDirectBytes;
+  private final AtomicLong usedHeapBytes;
+  private final AtomicLong queueingRequests;
+
+  public MemChecker(
+      long maxAllowedDirectBytes, long maxAllowedHeapBytes, long maxQueueingRequests) {
+    this.maxAllowedDirectBytes = maxAllowedDirectBytes;
+    this.maxAllowedHeapBytes = maxAllowedHeapBytes;
+    this.maxQueueingRequests = maxQueueingRequests;
+    usedDirectBytes = new AtomicLong(0L);
+    usedHeapBytes = new AtomicLong(0L);
+    queueingRequests = new AtomicLong(0L);
+  }
+
+  public boolean tryAcquire(Message requestMessage) {
+    if (queueingRequests.getAndAdd(1L) >= maxQueueingRequests) {
+      return false;
+    }
+
+    long expectDirectUsage = getEstimatedDirectMemoryUsage((RequestMessage) requestMessage);
+    if (usedDirectBytes.getAndAdd(expectDirectUsage) > maxAllowedDirectBytes) {
+      return false;
+    }
+
+    long expectHeapUsage = getEstimatedHeapMemoryUsage((RequestMessage) requestMessage);
+    if (usedHeapBytes.getAndAdd(expectHeapUsage) > maxAllowedHeapBytes) {
+      return false;
+    }
+
+    // permission acquired
+    queueingRequests.incrementAndGet();
+    usedDirectBytes.addAndGet(expectDirectUsage);
+    usedHeapBytes.addAndGet(expectHeapUsage);
+    return true;
+  }
+
+  public void release(Message requestMessage) {
+    long expectDirectUsage = getEstimatedDirectMemoryUsage((RequestMessage) requestMessage);
+    long expectHeapUsage = getEstimatedHeapMemoryUsage((RequestMessage) requestMessage);
+    queueingRequests.decrementAndGet();
+    usedDirectBytes.addAndGet(expectDirectUsage);
+    usedHeapBytes.addAndGet(expectHeapUsage);

Review Comment:
   why use `addAndGet` in relase method?



##########
consensus/src/main/java/org/apache/iotdb/consensus/ratis/MemChecker.java:
##########
@@ -0,0 +1,132 @@
+/*
+ * 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.iotdb.consensus.ratis;
+
+import org.apache.ratis.protocol.Message;
+
+import java.util.concurrent.atomic.AtomicLong;
+
+public class MemChecker {
+  private final long maxAllowedDirectBytes;
+  private final long maxAllowedHeapBytes;
+  private final long maxQueueingRequests;
+
+  private final AtomicLong usedDirectBytes;
+  private final AtomicLong usedHeapBytes;
+  private final AtomicLong queueingRequests;
+
+  public MemChecker(
+      long maxAllowedDirectBytes, long maxAllowedHeapBytes, long maxQueueingRequests) {
+    this.maxAllowedDirectBytes = maxAllowedDirectBytes;
+    this.maxAllowedHeapBytes = maxAllowedHeapBytes;
+    this.maxQueueingRequests = maxQueueingRequests;
+    usedDirectBytes = new AtomicLong(0L);
+    usedHeapBytes = new AtomicLong(0L);
+    queueingRequests = new AtomicLong(0L);
+  }
+
+  public boolean tryAcquire(Message requestMessage) {
+    if (queueingRequests.getAndAdd(1L) >= maxQueueingRequests) {
+      return false;
+    }
+
+    long expectDirectUsage = getEstimatedDirectMemoryUsage((RequestMessage) requestMessage);
+    if (usedDirectBytes.getAndAdd(expectDirectUsage) > maxAllowedDirectBytes) {
+      return false;
+    }
+
+    long expectHeapUsage = getEstimatedHeapMemoryUsage((RequestMessage) requestMessage);
+    if (usedHeapBytes.getAndAdd(expectHeapUsage) > maxAllowedHeapBytes) {
+      return false;
+    }
+
+    // permission acquired
+    queueingRequests.incrementAndGet();
+    usedDirectBytes.addAndGet(expectDirectUsage);
+    usedHeapBytes.addAndGet(expectHeapUsage);

Review Comment:
   double calculate in line 45 and line 60?
   the same as other.



-- 
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: reviews-unsubscribe@iotdb.apache.org

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