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

morningman pushed a commit to branch branch-2.0
in repository https://gitbox.apache.org/repos/asf/doris.git


The following commit(s) were added to refs/heads/branch-2.0 by this push:
     new 79de860900e [opt](fe) Optimize calculate load job num metric in FE 
(#39145)
79de860900e is described below

commit 79de860900e255b18223f7dfa39a8ddd5e67a311
Author: htyoung <hty551...@hotmail.com>
AuthorDate: Tue Aug 13 15:50:12 2024 +0800

    [opt](fe) Optimize calculate load job num metric in FE (#39145)
    
    bp #31952 #34020
    
    ---------
    
    Co-authored-by: Lei Zhang <27994433+swjtu-zhang...@users.noreply.github.com>
---
 .../org/apache/doris/load/loadv2/LoadManager.java  | 24 ++++++++----
 .../java/org/apache/doris/metric/MetricRepo.java   | 45 ++++++++++++++--------
 2 files changed, 46 insertions(+), 23 deletions(-)

diff --git 
a/fe/fe-core/src/main/java/org/apache/doris/load/loadv2/LoadManager.java 
b/fe/fe-core/src/main/java/org/apache/doris/load/loadv2/LoadManager.java
index b31d040e679..e6cd4e77149 100644
--- a/fe/fe-core/src/main/java/org/apache/doris/load/loadv2/LoadManager.java
+++ b/fe/fe-core/src/main/java/org/apache/doris/load/loadv2/LoadManager.java
@@ -58,6 +58,7 @@ import com.google.common.collect.Lists;
 import com.google.common.collect.Maps;
 import com.google.common.collect.Sets;
 import org.apache.commons.lang3.StringUtils;
+import org.apache.commons.lang3.time.StopWatch;
 import org.apache.logging.log4j.LogManager;
 import org.apache.logging.log4j.Logger;
 
@@ -331,6 +332,11 @@ public class LoadManager implements Writable {
         job.unprotectReadEndOperation(operation);
         LOG.info(new LogBuilder(LogKey.LOAD_JOB, 
operation.getId()).add("operation", operation)
                 .add("msg", "replay end load job").build());
+
+        // When idToLoadJob size increase 10000 roughly, we run 
removeOldLoadJob to reduce mem used
+        if ((!idToLoadJob.isEmpty()) && (idToLoadJob.size() % 10000 == 0)) {
+            removeOldLoadJob();
+        }
     }
 
     /**
@@ -393,14 +399,10 @@ public class LoadManager implements Writable {
     /**
      * Get load job num, used by metric.
      **/
-    public long getLoadJobNum(JobState jobState, EtlJobType jobType) {
-        readLock();
-        try {
-            return idToLoadJob.values().stream().filter(j -> j.getState() == 
jobState && j.getJobType() == jobType)
-                    .count();
-        } finally {
-            readUnlock();
-        }
+    public Map<Pair<EtlJobType, JobState>, Long> getLoadJobNum() {
+        return idToLoadJob.values().stream().collect(Collectors.groupingBy(
+                loadJob -> Pair.of(loadJob.getJobType(), loadJob.getState()),
+                Collectors.counting()));
     }
 
     /**
@@ -453,6 +455,8 @@ public class LoadManager implements Writable {
     }
 
     private void removeLoadJobIf(Predicate<LoadJob> pred) {
+        long removeJobNum = 0;
+        StopWatch stopWatch = StopWatch.createStarted();
         writeLock();
         try {
             Iterator<Map.Entry<Long, LoadJob>> iter = 
idToLoadJob.entrySet().iterator();
@@ -461,10 +465,14 @@ public class LoadManager implements Writable {
                 if (pred.test(job)) {
                     iter.remove();
                     jobRemovedTrigger(job);
+                    removeJobNum++;
                 }
             }
         } finally {
             writeUnlock();
+            stopWatch.stop();
+            LOG.info("end to removeOldLoadJob, removeJobNum:{} cost:{} ms",
+                    removeJobNum, stopWatch.getTime());
         }
     }
 
diff --git a/fe/fe-core/src/main/java/org/apache/doris/metric/MetricRepo.java 
b/fe/fe-core/src/main/java/org/apache/doris/metric/MetricRepo.java
index f1d186e5a45..7b18a363ddd 100644
--- a/fe/fe-core/src/main/java/org/apache/doris/metric/MetricRepo.java
+++ b/fe/fe-core/src/main/java/org/apache/doris/metric/MetricRepo.java
@@ -23,6 +23,7 @@ import org.apache.doris.alter.AlterJobV2.JobType;
 import org.apache.doris.catalog.Env;
 import org.apache.doris.catalog.TabletInvertedIndex;
 import org.apache.doris.common.Config;
+import org.apache.doris.common.Pair;
 import org.apache.doris.common.ThreadPoolManager;
 import org.apache.doris.common.util.NetUtils;
 import org.apache.doris.load.EtlJobType;
@@ -42,6 +43,7 @@ import org.apache.doris.transaction.TransactionStatus;
 
 import com.codahale.metrics.Histogram;
 import com.codahale.metrics.MetricRegistry;
+import com.google.common.collect.Maps;
 import com.google.common.collect.Sets;
 import org.apache.logging.log4j.LogManager;
 import org.apache.logging.log4j.Logger;
@@ -126,19 +128,19 @@ public final class MetricRepo {
     public static GaugeMetricImpl<Double> GAUGE_REQUEST_PER_SECOND;
     public static GaugeMetricImpl<Double> GAUGE_QUERY_ERR_RATE;
     public static GaugeMetricImpl<Long> GAUGE_MAX_TABLET_COMPACTION_SCORE;
+    private static Map<Pair<EtlJobType, JobState>, Long> loadJobNum = 
Maps.newHashMap();
 
     private static ScheduledThreadPoolExecutor metricTimer = 
ThreadPoolManager.newDaemonScheduledThreadPool(1,
             "metric-timer-pool", true);
     private static MetricCalculator metricCalculator = new MetricCalculator();
 
-    // init() should only be called after catalog is contructed.
+    // init() should only be called after catalog is constructed.
     public static synchronized void init() {
         if (isInit) {
             return;
         }
 
         // load jobs
-        LoadManager loadManger = Env.getCurrentEnv().getLoadManager();
         for (EtlJobType jobType : EtlJobType.values()) {
             if (jobType == EtlJobType.UNKNOWN) {
                 continue;
@@ -150,7 +152,7 @@ public final class MetricRepo {
                         if (!Env.getCurrentEnv().isMaster()) {
                             return 0L;
                         }
-                        return loadManger.getLoadJobNum(state, jobType);
+                        return MetricRepo.getLoadJobNum(jobType, state);
                     }
                 };
                 gauge.addLabel(new MetricLabel("job", "load")).addLabel(new 
MetricLabel("type", jobType.name()))
@@ -171,7 +173,7 @@ public final class MetricRepo {
                     Set<RoutineLoadJob.JobState> states = Sets.newHashSet();
                     states.add(jobState);
                     List<RoutineLoadJob> jobs = 
routineLoadManager.getRoutineLoadJobByState(states);
-                    return Long.valueOf(jobs.size());
+                    return (long) jobs.size();
                 }
             };
             gauge.addLabel(new MetricLabel("job", "load")).addLabel(new 
MetricLabel("type", "ROUTINE_LOAD"))
@@ -491,7 +493,7 @@ public final class MetricRepo {
 
     private static void initSystemMetrics() {
         // TCP retransSegs
-        GaugeMetric<Long> tcpRetransSegs = (GaugeMetric<Long>) new 
GaugeMetric<Long>(
+        GaugeMetric<Long> tcpRetransSegs = new GaugeMetric<Long>(
                 "snmp", MetricUnit.NOUNIT, "All TCP packets retransmitted") {
             @Override
             public Long getValue() {
@@ -502,7 +504,7 @@ public final class MetricRepo {
         DORIS_METRIC_REGISTER.addSystemMetrics(tcpRetransSegs);
 
         // TCP inErrs
-        GaugeMetric<Long> tpcInErrs = (GaugeMetric<Long>) new 
GaugeMetric<Long>(
+        GaugeMetric<Long> tpcInErrs = new GaugeMetric<Long>(
                 "snmp", MetricUnit.NOUNIT, "The number of all problematic TCP 
packets received") {
             @Override
             public Long getValue() {
@@ -513,7 +515,7 @@ public final class MetricRepo {
         DORIS_METRIC_REGISTER.addSystemMetrics(tpcInErrs);
 
         // TCP inSegs
-        GaugeMetric<Long> tpcInSegs = (GaugeMetric<Long>) new 
GaugeMetric<Long>(
+        GaugeMetric<Long> tpcInSegs = new GaugeMetric<Long>(
                 "snmp", MetricUnit.NOUNIT, "The number of all TCP packets 
received") {
             @Override
             public Long getValue() {
@@ -524,7 +526,7 @@ public final class MetricRepo {
         DORIS_METRIC_REGISTER.addSystemMetrics(tpcInSegs);
 
         // TCP outSegs
-        GaugeMetric<Long> tpcOutSegs = (GaugeMetric<Long>) new 
GaugeMetric<Long>(
+        GaugeMetric<Long> tpcOutSegs = new GaugeMetric<Long>(
                 "snmp", MetricUnit.NOUNIT, "The number of all TCP packets send 
with RST") {
             @Override
             public Long getValue() {
@@ -535,7 +537,7 @@ public final class MetricRepo {
         DORIS_METRIC_REGISTER.addSystemMetrics(tpcOutSegs);
 
         // Memory Total
-        GaugeMetric<Long> memTotal = (GaugeMetric<Long>) new GaugeMetric<Long>(
+        GaugeMetric<Long> memTotal = new GaugeMetric<Long>(
                 "meminfo", MetricUnit.BYTES, "Total usable memory") {
             @Override
             public Long getValue() {
@@ -546,7 +548,7 @@ public final class MetricRepo {
         DORIS_METRIC_REGISTER.addSystemMetrics(memTotal);
 
         // Memory Free
-        GaugeMetric<Long> memFree = (GaugeMetric<Long>) new GaugeMetric<Long>(
+        GaugeMetric<Long> memFree = new GaugeMetric<Long>(
                 "meminfo", MetricUnit.BYTES, "The amount of physical memory 
not used by the system") {
             @Override
             public Long getValue() {
@@ -557,7 +559,7 @@ public final class MetricRepo {
         DORIS_METRIC_REGISTER.addSystemMetrics(memFree);
 
         // Memory Total
-        GaugeMetric<Long> memAvailable = (GaugeMetric<Long>) new 
GaugeMetric<Long>("meminfo", MetricUnit.BYTES,
+        GaugeMetric<Long> memAvailable = new GaugeMetric<Long>("meminfo", 
MetricUnit.BYTES,
                 "An estimate of how much memory is available for starting new 
applications, without swapping") {
             @Override
             public Long getValue() {
@@ -568,7 +570,7 @@ public final class MetricRepo {
         DORIS_METRIC_REGISTER.addSystemMetrics(memAvailable);
 
         // Buffers
-        GaugeMetric<Long> buffers = (GaugeMetric<Long>) new 
GaugeMetric<Long>("meminfo", MetricUnit.BYTES,
+        GaugeMetric<Long> buffers = new GaugeMetric<Long>("meminfo", 
MetricUnit.BYTES,
                 "Memory in buffer cache, so relatively temporary storage for 
raw disk blocks") {
             @Override
             public Long getValue() {
@@ -579,7 +581,7 @@ public final class MetricRepo {
         DORIS_METRIC_REGISTER.addSystemMetrics(buffers);
 
         // Cached
-        GaugeMetric<Long> cached = (GaugeMetric<Long>) new GaugeMetric<Long>(
+        GaugeMetric<Long> cached = new GaugeMetric<Long>(
                 "meminfo", MetricUnit.BYTES, "Memory in the pagecache 
(Diskcache and Shared Memory)") {
             @Override
             public Long getValue() {
@@ -647,7 +649,11 @@ public final class MetricRepo {
         // update the metrics first
         updateMetrics();
 
+        // update load job metrics
+        updateLoadJobMetrics();
+
         StringBuilder sb = new StringBuilder();
+
         // jvm
         JvmService jvmService = new JvmService();
         JvmStats jvmStats = jvmService.stats();
@@ -655,11 +661,11 @@ public final class MetricRepo {
 
         visitor.setMetricNumber(DORIS_METRIC_REGISTER.getAllMetricSize());
         // doris metrics
-        for (Metric metric : DORIS_METRIC_REGISTER.getMetrics()) {
+        for (Metric<?> metric : DORIS_METRIC_REGISTER.getMetrics()) {
             visitor.visit(sb, MetricVisitor.FE_PREFIX, metric);
         }
         // system metric
-        for (Metric metric : DORIS_METRIC_REGISTER.getSystemMetrics()) {
+        for (Metric<?> metric : DORIS_METRIC_REGISTER.getSystemMetrics()) {
             visitor.visit(sb, MetricVisitor.SYS_PREFIX, metric);
         }
 
@@ -692,4 +698,13 @@ public final class MetricRepo {
     public static synchronized List<Metric> getMetricsByName(String name) {
         return DORIS_METRIC_REGISTER.getMetricsByName(name);
     }
+
+    private static void updateLoadJobMetrics() {
+        LoadManager loadManager = Env.getCurrentEnv().getLoadManager();
+        MetricRepo.loadJobNum = loadManager.getLoadJobNum();
+    }
+
+    private static long getLoadJobNum(EtlJobType jobType, JobState jobState) {
+        return MetricRepo.loadJobNum.getOrDefault(Pair.of(jobType, jobState), 
0L);
+    }
 }


---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscr...@doris.apache.org
For additional commands, e-mail: commits-h...@doris.apache.org

Reply via email to