You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@iotdb.apache.org by ji...@apache.org on 2023/05/22 02:10:19 UTC

[iotdb] branch native_raft updated: add entry allocator statistics and report

This is an automated email from the ASF dual-hosted git repository.

jiangtian pushed a commit to branch native_raft
in repository https://gitbox.apache.org/repos/asf/iotdb.git


The following commit(s) were added to refs/heads/native_raft by this push:
     new f00b483a092 add entry allocator statistics and report
f00b483a092 is described below

commit f00b483a0924380710ba01f25469aaea03455f63
Author: Tian Jiang <jt...@163.com>
AuthorDate: Mon May 22 10:13:10 2023 +0800

    add entry allocator statistics and report
---
 .../consensus/natraft/protocol/RaftMember.java     |  3 +-
 .../protocol/log/recycle/EntryAllocator.java       | 57 +++++++++++++++++++++-
 .../iotdb/consensus/natraft/utils/NodeReport.java  |  7 ++-
 3 files changed, 63 insertions(+), 4 deletions(-)

diff --git a/consensus/src/main/java/org/apache/iotdb/consensus/natraft/protocol/RaftMember.java b/consensus/src/main/java/org/apache/iotdb/consensus/natraft/protocol/RaftMember.java
index 03c45a917f0..9be05b5dcce 100644
--- a/consensus/src/main/java/org/apache/iotdb/consensus/natraft/protocol/RaftMember.java
+++ b/consensus/src/main/java/org/apache/iotdb/consensus/natraft/protocol/RaftMember.java
@@ -1407,6 +1407,7 @@ public class RaftMember {
         readOnly,
         heartbeatThread.getLastHeartbeatReceivedTime(),
         prevLastLogIndex,
-        logManager.getAppliedIndex());
+        logManager.getAppliedIndex(),
+        requestEntryAllocator.toString());
   }
 }
diff --git a/consensus/src/main/java/org/apache/iotdb/consensus/natraft/protocol/log/recycle/EntryAllocator.java b/consensus/src/main/java/org/apache/iotdb/consensus/natraft/protocol/log/recycle/EntryAllocator.java
index 3b92e366aec..b98cdbde9ae 100644
--- a/consensus/src/main/java/org/apache/iotdb/consensus/natraft/protocol/log/recycle/EntryAllocator.java
+++ b/consensus/src/main/java/org/apache/iotdb/consensus/natraft/protocol/log/recycle/EntryAllocator.java
@@ -24,6 +24,7 @@ import org.apache.iotdb.consensus.natraft.protocol.log.Entry;
 
 import java.util.concurrent.ArrayBlockingQueue;
 import java.util.concurrent.BlockingQueue;
+import java.util.concurrent.atomic.AtomicLong;
 import java.util.function.Supplier;
 
 public class EntryAllocator<T extends Entry> {
@@ -31,6 +32,11 @@ public class EntryAllocator<T extends Entry> {
   private Supplier<T> entryFactory;
   private BlockingQueue<T> recyclingEntries;
   private Supplier<Long> safeIndexProvider;
+  private AtomicLong allocatorSize = new AtomicLong();
+  private AtomicLong allocatorGetCnt = new AtomicLong();
+  private AtomicLong allocatorGetMissCnt = new AtomicLong();
+  private AtomicLong allocatorRecycleCnt = new AtomicLong();
+  private AtomicLong allocatorRecycleMissCnt = new AtomicLong();
 
   public EntryAllocator(
       RaftConfig config, Supplier<T> entryFactory, Supplier<Long> safeIndexProvider) {
@@ -41,20 +47,33 @@ public class EntryAllocator<T extends Entry> {
   }
 
   public T Allocate() {
+    allocatorGetCnt.incrementAndGet();
     T entry = entryPool.poll();
     if (entry == null) {
       entry = entryFactory.get();
+      allocatorGetMissCnt.incrementAndGet();
+    } else {
+      allocatorSize.addAndGet(-entry.estimateSize());
     }
     return entry;
   }
 
   public void recycle(T entry) {
     Long safeIndex = safeIndexProvider.get();
+    allocatorRecycleCnt.incrementAndGet();
     if (entry.getCurrLogIndex() <= safeIndex) {
       entry.recycle();
-      entryPool.offer(entry);
+      if (entryPool.offer(entry)) {
+        allocatorSize.addAndGet(entry.estimateSize());
+      } else {
+        allocatorRecycleMissCnt.incrementAndGet();
+      }
     } else {
-      recyclingEntries.offer(entry);
+      if (recyclingEntries.offer(entry)) {
+        allocatorSize.addAndGet(entry.estimateSize());
+      } else {
+        allocatorRecycleMissCnt.incrementAndGet();
+      }
     }
 
     checkRecyclingEntries();
@@ -75,4 +94,38 @@ public class EntryAllocator<T extends Entry> {
       }
     }
   }
+
+  public long getAllocatorSize() {
+    return allocatorSize.get();
+  }
+
+  public int cachedEntryNum() {
+    return entryPool.size() + recyclingEntries.size();
+  }
+
+  public double allocateHitRatio() {
+    return 1.0 - allocatorGetMissCnt.get() * 1.0 / allocatorGetCnt.get();
+  }
+
+  public double recycleHitRatio() {
+    return 1.0 - allocatorRecycleMissCnt.get() * 1.0 / allocatorRecycleCnt.get();
+  }
+
+  @Override
+  public String toString() {
+    return "EntryAllocator{"
+        + "size="
+        + getAllocatorSize()
+        + ","
+        + "entryNum="
+        + cachedEntryNum()
+        + ","
+        + "allocateRatio="
+        + allocateHitRatio()
+        + ","
+        + "recycleRatio="
+        + recycleHitRatio()
+        + ","
+        + "}";
+  }
 }
diff --git a/consensus/src/main/java/org/apache/iotdb/consensus/natraft/utils/NodeReport.java b/consensus/src/main/java/org/apache/iotdb/consensus/natraft/utils/NodeReport.java
index 42393edab6b..3d40f929408 100644
--- a/consensus/src/main/java/org/apache/iotdb/consensus/natraft/utils/NodeReport.java
+++ b/consensus/src/main/java/org/apache/iotdb/consensus/natraft/utils/NodeReport.java
@@ -72,6 +72,7 @@ public class NodeReport {
     long lastHeartbeatReceivedTime;
     long prevLastLogIndex;
     long maxAppliedLogIndex;
+    String allocatorReport;
 
     public RaftMemberReport(
         RaftRole character,
@@ -85,7 +86,8 @@ public class NodeReport {
         boolean isReadOnly,
         long lastHeartbeatReceivedTime,
         long prevLastLogIndex,
-        long maxAppliedLogIndex) {
+        long maxAppliedLogIndex,
+        String allocatorReport) {
       this.character = character;
       this.leader = leader;
       this.term = term;
@@ -98,6 +100,7 @@ public class NodeReport {
       this.lastHeartbeatReceivedTime = lastHeartbeatReceivedTime;
       this.prevLastLogIndex = prevLastLogIndex;
       this.maxAppliedLogIndex = maxAppliedLogIndex;
+      this.allocatorReport = allocatorReport;
     }
 
     @Override
@@ -126,6 +129,8 @@ public class NodeReport {
           + ", lastHeartbeat="
           + (System.currentTimeMillis() - lastHeartbeatReceivedTime)
           + "ms ago"
+          + ", "
+          + allocatorReport
           + ", logIncrement="
           + (lastLogIndex - prevLastLogIndex)
           + '}';