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

zhangxiaowei pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/ozhera.git


The following commit(s) were added to refs/heads/master by this push:
     new c402b4ad feat: add configuration parameter support and sending 
frequency limit function (#643)
c402b4ad is described below

commit c402b4ad594b26d6f577b34f3240ac817f68558f
Author: wtt <[email protected]>
AuthorDate: Thu Mar 19 16:34:56 2026 +0800

    feat: add configuration parameter support and sending frequency limit 
function (#643)
    
    * fix: solve the blocking problem caused by serverless startup
    
    * fix: fix pipeline execution logic error
    
    * feat: add configuration parameter support and sending frequency limit 
function
---
 .../org/apache/ozhera/log/server/common/Utils.java | 39 +++++++++++
 .../server/service/DefaultLogProcessCollector.java | 20 ++++--
 .../service/DefaultPublishConfigService.java       | 76 ++++++++++++++++++----
 3 files changed, 116 insertions(+), 19 deletions(-)

diff --git 
a/ozhera-log/log-agent-server/src/main/java/org/apache/ozhera/log/server/common/Utils.java
 
b/ozhera-log/log-agent-server/src/main/java/org/apache/ozhera/log/server/common/Utils.java
new file mode 100644
index 00000000..187a17dd
--- /dev/null
+++ 
b/ozhera-log/log-agent-server/src/main/java/org/apache/ozhera/log/server/common/Utils.java
@@ -0,0 +1,39 @@
+/*
+ * 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.ozhera.log.server.common;
+
+import org.apache.commons.lang3.StringUtils;
+
+/**
+ * @author wtt
+ * @date 2026/3/17 10:13
+ * @version 1.0
+ */
+public class Utils {
+    private Utils() {
+    }
+
+    public static String getConfig(String key) {
+        String raw = System.getenv(key);
+        if (StringUtils.isBlank(raw)) {
+            raw = System.getProperty(key);
+        }
+        return raw;
+    }
+}
diff --git 
a/ozhera-log/log-agent-server/src/main/java/org/apache/ozhera/log/server/service/DefaultLogProcessCollector.java
 
b/ozhera-log/log-agent-server/src/main/java/org/apache/ozhera/log/server/service/DefaultLogProcessCollector.java
index 079c6b56..ca47630b 100644
--- 
a/ozhera-log/log-agent-server/src/main/java/org/apache/ozhera/log/server/service/DefaultLogProcessCollector.java
+++ 
b/ozhera-log/log-agent-server/src/main/java/org/apache/ozhera/log/server/service/DefaultLogProcessCollector.java
@@ -49,6 +49,7 @@ import java.util.stream.Collectors;
 
 import static org.apache.ozhera.log.common.Constant.GSON;
 import static org.apache.ozhera.log.common.Constant.SYMBOL_COLON;
+import static org.apache.ozhera.log.server.common.Utils.getConfig;
 
 /**
  * @author wtt
@@ -63,15 +64,17 @@ public class DefaultLogProcessCollector implements 
LogProcessCollector {
 
     private final Map<String, List<UpdateLogProcessCmd.CollectDetail>> 
tailProgressMap = new ConcurrentHashMap<>(256);
 
+    private static final String CONFIG_COUNT_KEY = "CONFIG_MAX_IP_COUNT";
+
     // Maximum number of stored IPs to prevent unlimited memory growth
-    private static final int MAX_IP_COUNT = 200000;
+    private volatile int maxIpCount = 80000;
 
     // The maximum number of CollectDetails under each IP to prevent excessive 
data for a single IP
     private static final int MAX_COLLECT_DETAIL_PER_IP = 5000;
 
     private static final Integer MAX_INTERRUPT_TIME = 10;
 
-    private static final Integer MAX_STATIC_INTERRUPT_TIME_HOUR = 4;
+    private static final Integer MAX_STATIC_INTERRUPT_TIME_HOUR = 1;
 
     private static final String PROCESS_SEPARATOR = "%";
 
@@ -86,6 +89,11 @@ public class DefaultLogProcessCollector implements 
LogProcessCollector {
             return t;
         });
 
+        String maxIpCountStr = getConfig(CONFIG_COUNT_KEY);
+        if (StringUtils.isNotEmpty(maxIpCountStr)) {
+            maxIpCount = NumberUtil.parseInt(maxIpCountStr);
+        }
+
         // Every 2 minutes, perform cleanup of expired data
         
scheduledExecutorService.scheduleWithFixedDelay(this::cleanupExpiredData, 2, 2, 
TimeUnit.MINUTES);
         log.info("Initialized scheduled cleanup for LogProcessCollector");
@@ -103,8 +111,8 @@ public class DefaultLogProcessCollector implements 
LogProcessCollector {
         List<UpdateLogProcessCmd.CollectDetail> deduplicatedCollectList = 
deduplicateCollectList(cmd.getCollectList());
 
         // Control the size of the map to prevent memory overflow
-        if (tailProgressMap.size() >= MAX_IP_COUNT) {
-            log.warn("tailProgressMap size reaches limit {}, removing oldest 
entries", MAX_IP_COUNT);
+        if (tailProgressMap.size() >= maxIpCount) {
+            log.warn("tailProgressMap size reaches limit {}, removing oldest 
entries", maxIpCount);
             // Remove some old entries, keep the latest
             removeOldestEntries();
         }
@@ -236,7 +244,7 @@ public class DefaultLogProcessCollector implements 
LogProcessCollector {
     private void removeOldestEntries() {
         // Get all keys and remove a part of them according to some strategy
         List<String> allKeys = new ArrayList<>(tailProgressMap.keySet());
-        if (allKeys.size() <= MAX_IP_COUNT / 2) {
+        if (allKeys.size() <= maxIpCount / 2) {
             return; // If the number doesn't exceed half the limit, no removal 
is needed
         }
 
@@ -308,7 +316,7 @@ public class DefaultLogProcessCollector implements 
LogProcessCollector {
                 log.info("Cleaned up {} expired entries from tailProgressMap", 
removedCount);
             }
 
-            if (tailProgressMap.size() > MAX_IP_COUNT) {
+            if (tailProgressMap.size() > maxIpCount) {
                 removeOldestEntries();
             }
         } catch (Exception e) {
diff --git 
a/ozhera-log/log-agent-server/src/main/java/org/apache/ozhera/log/server/service/DefaultPublishConfigService.java
 
b/ozhera-log/log-agent-server/src/main/java/org/apache/ozhera/log/server/service/DefaultPublishConfigService.java
index e0d3adc6..9c29d2b0 100644
--- 
a/ozhera-log/log-agent-server/src/main/java/org/apache/ozhera/log/server/service/DefaultPublishConfigService.java
+++ 
b/ozhera-log/log-agent-server/src/main/java/org/apache/ozhera/log/server/service/DefaultPublishConfigService.java
@@ -19,6 +19,9 @@
 package org.apache.ozhera.log.server.service;
 
 import com.google.common.base.Stopwatch;
+import com.google.common.cache.CacheBuilder;
+import com.google.common.cache.CacheLoader;
+import com.google.common.cache.LoadingCache;
 import com.google.common.collect.Lists;
 import com.xiaomi.data.push.context.AgentContext;
 import com.xiaomi.data.push.rpc.RpcServer;
@@ -34,12 +37,15 @@ import 
org.apache.ozhera.log.api.service.PublishConfigService;
 import org.apache.ozhera.log.utils.NetUtil;
 
 import javax.annotation.Resource;
+import java.nio.charset.StandardCharsets;
+import java.security.MessageDigest;
 import java.util.*;
 import java.util.concurrent.*;
 import java.util.concurrent.atomic.AtomicInteger;
 
 import static org.apache.ozhera.log.common.Constant.GSON;
 import static org.apache.ozhera.log.common.Constant.SYMBOL_COLON;
+import static org.apache.ozhera.log.server.common.Utils.getConfig;
 
 /**
  * @author wtt
@@ -66,6 +72,23 @@ public class DefaultPublishConfigService implements 
PublishConfigService {
 
     private static final ExecutorService SEND_CONFIG_EXECUTOR;
 
+    /**
+     * Rate limit cache using Guava: key=agentIp:configMd5, value=lastSendTime
+     * Only send once within 2 minutes for the same configuration
+     * Configured with max size and expire time to prevent memory explosion
+     */
+    private static final LoadingCache<String, Long> CONFIG_SEND_CACHE = 
CacheBuilder.newBuilder()
+            .maximumSize(20000) // Maximum 10k entries to prevent memory 
explosion
+            .expireAfterWrite(3, TimeUnit.MINUTES) // Auto expire after 3 
minutes (longer than rate limit window)
+            .build(new CacheLoader<String, Long>() {
+                @Override
+                public Long load(String key) {
+                    return 0L; // Default value if not present
+                }
+            });
+
+    private static final long RATE_LIMIT_WINDOW_MS = 2 * 60 * 1000L; // 2 
minutes
+
     static {
         int corePoolSize = Math.max(2, 
Runtime.getRuntime().availableProcessors() / 2);
         int maximumPoolSize = Runtime.getRuntime().availableProcessors();
@@ -101,15 +124,6 @@ public class DefaultPublishConfigService implements 
PublishConfigService {
         }
     }
 
-    private String getConfig(String key) {
-        String raw = System.getenv(key);
-        if (StringUtils.isBlank(raw)) {
-            raw = System.getProperty(key);
-        }
-        return raw;
-    }
-
-
     /**
      * dubbo interface, the timeout period cannot be too long
      *
@@ -180,7 +194,7 @@ public class DefaultPublishConfigService implements 
PublishConfigService {
     }
 
     /**
-     * Send configuration to agent asynchronously
+     * Send configuration to agent asynchronously with rate limiting
      *
      * @param agentIp agent IP address
      * @param meta    log collection metadata
@@ -190,15 +204,51 @@ public class DefaultPublishConfigService implements 
PublishConfigService {
             return;
         }
 
+        // Generate config MD5 as rate limit key
+        String configMd5 = generateConfigMd5(meta);
+        String rateLimitKey = agentIp + ":" + configMd5;
+
+        // Check rate limit: only send once within 2 minutes for same config
+        long currentTime = System.currentTimeMillis();
+        Long lastSendTime = CONFIG_SEND_CACHE.getUnchecked(rateLimitKey);
+
+        if (null != lastSendTime && lastSendTime > 0 && currentTime - 
lastSendTime < RATE_LIMIT_WINDOW_MS) {
+            log.info("Rate limit hit, skip sending config. agentIp:{}, 
configMd5:{}, lastSendTime:{}ms ago,config:{}",
+                    agentIp, configMd5, currentTime - lastSendTime, 
GSON.toJson(meta));
+            return;
+        }
+
+        // Update cache and send config
+        CONFIG_SEND_CACHE.put(rateLimitKey, currentTime);
+
         sendWithRetry(agentIp, meta, 1)
                 .exceptionally(ex -> {
                     log.error("send config failed after retry, agentIp:{}", 
agentIp, ex);
                     return false;
                 });
+//        try {
+//            Thread.sleep(random.nextInt(800, 1000));
+//        } catch (InterruptedException e) {
+//            log.error("sleep interrupted, agentIp:{}", agentIp, e);
+//        }
+    }
+
+    /**
+     * Generate MD5 hash of configuration content
+     */
+    private String generateConfigMd5(LogCollectMeta meta) {
         try {
-            Thread.sleep(random.nextInt(800, 1000));
-        } catch (InterruptedException e) {
-            log.error("sleep interrupted, agentIp:{}", agentIp, e);
+            String configJson = GSON.toJson(meta);
+            MessageDigest md = MessageDigest.getInstance("MD5");
+            byte[] digest = 
md.digest(configJson.getBytes(StandardCharsets.UTF_8));
+            StringBuilder sb = new StringBuilder();
+            for (byte b : digest) {
+                sb.append(String.format("%02x", b));
+            }
+            return sb.toString();
+        } catch (Exception e) {
+            log.error("generate config md5 failed, use hashCode instead", e);
+            return String.valueOf(meta.hashCode());
         }
     }
 


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to