You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@dubbo.apache.org by li...@apache.org on 2022/02/21 05:24:00 UTC

[dubbo] branch 3.0 updated: [3.0] Add max entry limit for Profiler (#9703)

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

liujun pushed a commit to branch 3.0
in repository https://gitbox.apache.org/repos/asf/dubbo.git


The following commit(s) were added to refs/heads/3.0 by this push:
     new 82c1b03  [3.0] Add max entry limit for Profiler (#9703)
82c1b03 is described below

commit 82c1b030fae864faf54128b1314d2e748228901e
Author: Albumen Kevin <jh...@gmail.com>
AuthorDate: Mon Feb 21 13:23:27 2022 +0800

    [3.0] Add max entry limit for Profiler (#9703)
---
 .../java/org/apache/dubbo/common/profiler/Profiler.java     | 13 ++++++++++---
 .../org/apache/dubbo/common/profiler/ProfilerEntry.java     |  8 ++++++++
 .../org/apache/dubbo/rpc/proxy/AbstractProxyInvoker.java    |  7 +++++--
 3 files changed, 23 insertions(+), 5 deletions(-)

diff --git a/dubbo-common/src/main/java/org/apache/dubbo/common/profiler/Profiler.java b/dubbo-common/src/main/java/org/apache/dubbo/common/profiler/Profiler.java
index 3d1250f..8bb1920 100644
--- a/dubbo-common/src/main/java/org/apache/dubbo/common/profiler/Profiler.java
+++ b/dubbo-common/src/main/java/org/apache/dubbo/common/profiler/Profiler.java
@@ -23,6 +23,7 @@ import java.util.List;
 
 public class Profiler {
     public static final String PROFILER_KEY = "DUBBO_INVOKE_PROFILER";
+    public static final int MAX_ENTRY_SIZE = 1000;
 
     private final static InternalThreadLocal<ProfilerEntry> bizProfiler = new InternalThreadLocal<>();
 
@@ -32,7 +33,9 @@ public class Profiler {
 
     public static ProfilerEntry enter(ProfilerEntry entry, String message) {
         ProfilerEntry subEntry = new ProfilerEntry(message, entry, entry.getFirst());
-        entry.getSub().add(subEntry);
+        if (subEntry.getRequestCount().incrementAndGet() < MAX_ENTRY_SIZE) {
+            entry.getSub().add(subEntry);
+        } // ignore if sub entry size is exceed
         return subEntry;
     }
 
@@ -46,8 +49,12 @@ public class Profiler {
         }
     }
 
-    public static void setToBizProfiler(ProfilerEntry entry) {
-        bizProfiler.set(entry);
+    public static ProfilerEntry setToBizProfiler(ProfilerEntry entry) {
+        try {
+            return bizProfiler.get();
+        } finally {
+            bizProfiler.set(entry);
+        }
     }
 
     public static ProfilerEntry getBizProfiler() {
diff --git a/dubbo-common/src/main/java/org/apache/dubbo/common/profiler/ProfilerEntry.java b/dubbo-common/src/main/java/org/apache/dubbo/common/profiler/ProfilerEntry.java
index 70d4e67..9a9eb1d 100644
--- a/dubbo-common/src/main/java/org/apache/dubbo/common/profiler/ProfilerEntry.java
+++ b/dubbo-common/src/main/java/org/apache/dubbo/common/profiler/ProfilerEntry.java
@@ -18,6 +18,7 @@ package org.apache.dubbo.common.profiler;
 
 import java.util.ArrayList;
 import java.util.List;
+import java.util.concurrent.atomic.AtomicInteger;
 
 public class ProfilerEntry {
     private final List<ProfilerEntry> sub = new ArrayList<>(4);
@@ -25,6 +26,7 @@ public class ProfilerEntry {
     private final ProfilerEntry parent;
     private final ProfilerEntry first;
     private final long startTime;
+    private final AtomicInteger requestCount;
     private long endTime;
 
     public ProfilerEntry(String message) {
@@ -32,6 +34,7 @@ public class ProfilerEntry {
         this.parent = null;
         this.first = this;
         this.startTime = System.nanoTime();
+        this.requestCount = new AtomicInteger(1);
     }
 
     public ProfilerEntry(String message, ProfilerEntry parentEntry, ProfilerEntry firstEntry) {
@@ -39,6 +42,7 @@ public class ProfilerEntry {
         this.parent = parentEntry;
         this.first = firstEntry;
         this.startTime = System.nanoTime();
+        this.requestCount = parentEntry.getRequestCount();
     }
 
     public List<ProfilerEntry> getSub() {
@@ -68,4 +72,8 @@ public class ProfilerEntry {
     public long getEndTime() {
         return endTime;
     }
+
+    public AtomicInteger getRequestCount() {
+        return requestCount;
+    }
 }
diff --git a/dubbo-rpc/dubbo-rpc-api/src/main/java/org/apache/dubbo/rpc/proxy/AbstractProxyInvoker.java b/dubbo-rpc/dubbo-rpc-api/src/main/java/org/apache/dubbo/rpc/proxy/AbstractProxyInvoker.java
index 1743e69..eb6d4ff 100644
--- a/dubbo-rpc/dubbo-rpc-api/src/main/java/org/apache/dubbo/rpc/proxy/AbstractProxyInvoker.java
+++ b/dubbo-rpc/dubbo-rpc-api/src/main/java/org/apache/dubbo/rpc/proxy/AbstractProxyInvoker.java
@@ -86,13 +86,13 @@ public abstract class AbstractProxyInvoker<T> implements Invoker<T> {
     @Override
     public Result invoke(Invocation invocation) throws RpcException {
         try {
+            ProfilerEntry originEntry = null;
             if (ProfilerSwitch.isEnableSimpleProfiler()) {
                 Object fromInvocation = invocation.get(Profiler.PROFILER_KEY);
                 if (fromInvocation instanceof ProfilerEntry) {
                     ProfilerEntry profiler = Profiler.enter((ProfilerEntry) fromInvocation, "Receive request. Server biz impl invoke begin.");
                     invocation.put(Profiler.PROFILER_KEY, profiler);
-                    // TODO clear after invoke
-                    Profiler.setToBizProfiler(profiler);
+                    originEntry = Profiler.setToBizProfiler(profiler);
                 }
             }
 
@@ -106,6 +106,9 @@ public abstract class AbstractProxyInvoker<T> implements Invoker<T> {
                 }
             }
             Profiler.removeBizProfiler();
+            if (originEntry != null) {
+                Profiler.setToBizProfiler(originEntry);
+            }
 
             CompletableFuture<Object> future = wrapWithFuture(value, invocation);
             CompletableFuture<AppResponse> appResponseFuture = future.handle((obj, t) -> {