소스 검색

修改报错

yumeng 6 일 전
부모
커밋
8ca6c9106a

+ 4 - 0
pom.xml

@@ -56,6 +56,10 @@
             <artifactId>spring-boot-starter-data-redis</artifactId>
         </dependency>
         <dependency>
+            <groupId>org.apache.commons</groupId>
+            <artifactId>commons-pool2</artifactId>
+        </dependency>
+        <dependency>
             <groupId>org.mybatis.spring.boot</groupId>
             <artifactId>mybatis-spring-boot-starter</artifactId>
             <version>3.0.3</version>

+ 12 - 1
src/main/java/com/adx/tencent/AppConfiguration.java

@@ -62,6 +62,7 @@ import com.zaxxer.hikari.HikariConfig;
 import com.zaxxer.hikari.HikariDataSource;
 import io.lettuce.core.ClientOptions;
 import io.lettuce.core.SocketOptions;
+import org.apache.commons.pool2.impl.GenericObjectPoolConfig;
 import io.lettuce.core.protocol.ProtocolVersion;
 import org.apache.ibatis.session.SqlSessionFactory;
 import org.mybatis.spring.SqlSessionFactoryBean;
@@ -76,6 +77,7 @@ import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
 import org.springframework.data.redis.connection.RedisConnectionFactory;
 import org.springframework.data.redis.connection.RedisStandaloneConfiguration;
 import org.springframework.data.redis.connection.lettuce.LettuceClientConfiguration;
+import org.springframework.data.redis.connection.lettuce.LettucePoolingClientConfiguration;
 import org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory;
 import org.springframework.data.redis.core.StringRedisTemplate;
 import org.springframework.lang.Nullable;
@@ -121,7 +123,16 @@ public class AppConfiguration {
             cfg.setPassword(props.getRedisPassword());
         }
 
-        LettuceClientConfiguration clientCfg = LettuceClientConfiguration.builder()
+        GenericObjectPoolConfig<?> poolConfig = new GenericObjectPoolConfig<>();
+        poolConfig.setMaxTotal(props.getRedisPoolMaxActive());
+        poolConfig.setMaxIdle(props.getRedisPoolMaxIdle());
+        poolConfig.setMinIdle(props.getRedisPoolMinIdle());
+        poolConfig.setMaxWait(props.getRedisPoolMaxWait());
+        poolConfig.setTestOnBorrow(props.isRedisPoolTestOnBorrow());
+        poolConfig.setTestWhileIdle(props.isRedisPoolTestWhileIdle());
+
+        LettuceClientConfiguration clientCfg = LettucePoolingClientConfiguration.builder()
+                .poolConfig(poolConfig)
                 .clientOptions(ClientOptions.builder()
                         .protocolVersion(ProtocolVersion.RESP2)
                         .socketOptions(SocketOptions.builder()

+ 55 - 0
src/main/java/com/adx/tencent/config/AppProperties.java

@@ -130,6 +130,12 @@ public class AppProperties {
     private Duration redisCommandTimeout = Duration.ofSeconds(5);
     private Duration redisShutdownTimeout = Duration.ofMillis(100);
     private boolean redisValidateConnection = true;
+    private int redisPoolMaxActive = 64;
+    private int redisPoolMaxIdle = 16;
+    private int redisPoolMinIdle = 4;
+    private Duration redisPoolMaxWait = Duration.ofSeconds(2);
+    private boolean redisPoolTestOnBorrow = true;
+    private boolean redisPoolTestWhileIdle = true;
 
     // TiDB
     private String tidbUrl;
@@ -880,6 +886,55 @@ public class AppProperties {
         this.redisValidateConnection = v;
     }
 
+
+    public int getRedisPoolMaxActive() {
+        return redisPoolMaxActive;
+    }
+
+    public void setRedisPoolMaxActive(int v) {
+        this.redisPoolMaxActive = v;
+    }
+
+    public int getRedisPoolMaxIdle() {
+        return redisPoolMaxIdle;
+    }
+
+    public void setRedisPoolMaxIdle(int v) {
+        this.redisPoolMaxIdle = v;
+    }
+
+    public int getRedisPoolMinIdle() {
+        return redisPoolMinIdle;
+    }
+
+    public void setRedisPoolMinIdle(int v) {
+        this.redisPoolMinIdle = v;
+    }
+
+    public Duration getRedisPoolMaxWait() {
+        return redisPoolMaxWait;
+    }
+
+    public void setRedisPoolMaxWait(Duration v) {
+        this.redisPoolMaxWait = v;
+    }
+
+    public boolean isRedisPoolTestOnBorrow() {
+        return redisPoolTestOnBorrow;
+    }
+
+    public void setRedisPoolTestOnBorrow(boolean v) {
+        this.redisPoolTestOnBorrow = v;
+    }
+
+    public boolean isRedisPoolTestWhileIdle() {
+        return redisPoolTestWhileIdle;
+    }
+
+    public void setRedisPoolTestWhileIdle(boolean v) {
+        this.redisPoolTestWhileIdle = v;
+    }
+
     public String getTidbUrl() {
         return tidbUrl;
     }

+ 13 - 2
src/main/java/com/adx/tencent/honor/store/HonorHotStore.java

@@ -283,11 +283,22 @@ public class HonorHotStore {
 
     public void delete(List<String> ids) {
         if (ids == null || ids.isEmpty()) return;
-        redis.opsForStream().delete(stream, ids.toArray(String[]::new));
+        try {
+            redis.opsForStream().delete(stream, ids.toArray(String[]::new));
+        } catch (Exception e) {
+            log.warn("[HonorHotStore] delete acknowledged stream entries failed | stream={} | count={} | error={}",
+                    stream, ids.size(), rootMessage(e));
+        }
     }
 
     public void trim(long maxLen) {
-        if (maxLen > 0) redis.opsForStream().trim(stream, maxLen, false);
+        if (maxLen <= 0) return;
+        try {
+            redis.opsForStream().trim(stream, maxLen, false);
+        } catch (Exception e) {
+            log.warn("[HonorHotStore] trim stream failed | stream={} | maxLen={} | error={}",
+                    stream, maxLen, rootMessage(e));
+        }
     }
 
     private void trimStreamIfNeeded(org.springframework.data.redis.connection.RedisConnection conn) {

+ 13 - 2
src/main/java/com/adx/tencent/kuaishou/store/KuaishouHotStore.java

@@ -291,11 +291,22 @@ public class KuaishouHotStore {
 
     public void delete(List<String> ids) {
         if (ids == null || ids.isEmpty()) return;
-        redis.opsForStream().delete(stream, ids.toArray(String[]::new));
+        try {
+            redis.opsForStream().delete(stream, ids.toArray(String[]::new));
+        } catch (Exception e) {
+            log.warn("[KuaishouHotStore] delete acknowledged stream entries failed | stream={} | count={} | error={}",
+                    stream, ids.size(), rootMessage(e));
+        }
     }
 
     public void trim(long maxLen) {
-        if (maxLen > 0) redis.opsForStream().trim(stream, maxLen, false);
+        if (maxLen <= 0) return;
+        try {
+            redis.opsForStream().trim(stream, maxLen, false);
+        } catch (Exception e) {
+            log.warn("[KuaishouHotStore] trim stream failed | stream={} | maxLen={} | error={}",
+                    stream, maxLen, rootMessage(e));
+        }
     }
 
     private void trimStreamIfNeeded(org.springframework.data.redis.connection.RedisConnection conn) {

+ 13 - 2
src/main/java/com/adx/tencent/oppo/store/OppoHotStore.java

@@ -282,11 +282,22 @@ public class OppoHotStore {
 
     public void delete(List<String> ids) {
         if (ids == null || ids.isEmpty()) return;
-        redis.opsForStream().delete(stream, ids.toArray(String[]::new));
+        try {
+            redis.opsForStream().delete(stream, ids.toArray(String[]::new));
+        } catch (Exception e) {
+            log.warn("[OppoHotStore] delete acknowledged stream entries failed | stream={} | count={} | error={}",
+                    stream, ids.size(), rootMessage(e));
+        }
     }
 
     public void trim(long maxLen) {
-        if (maxLen > 0) redis.opsForStream().trim(stream, maxLen, false);
+        if (maxLen <= 0) return;
+        try {
+            redis.opsForStream().trim(stream, maxLen, false);
+        } catch (Exception e) {
+            log.warn("[OppoHotStore] trim stream failed | stream={} | maxLen={} | error={}",
+                    stream, maxLen, rootMessage(e));
+        }
     }
 
     private void trimStreamIfNeeded(org.springframework.data.redis.connection.RedisConnection conn) {

+ 27 - 2
src/main/java/com/adx/tencent/storage/RedisHotStore.java

@@ -4,6 +4,8 @@ import com.adx.tencent.storage.model.BidRecord;
 import com.adx.tencent.storage.model.QueuedEvent;
 import com.adx.tencent.storage.model.TrackingRecord;
 import com.fasterxml.jackson.databind.ObjectMapper;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
 import org.springframework.data.redis.core.script.DefaultRedisScript;
 import org.springframework.data.redis.connection.stream.*;
 import org.springframework.data.redis.core.StringRedisTemplate;
@@ -23,6 +25,7 @@ import java.util.concurrent.ConcurrentHashMap;
  */
 public class RedisHotStore {
 
+    private static final Logger log = LoggerFactory.getLogger(RedisHotStore.class);
     public static final String BID_NOT_FOUND_MSG = "bid not found";
     private static final Duration IMPRESSION_BID_TTL = Duration.ofHours(3);
     private static final Duration DEFAULT_CONVERSION_SEEN_TTL = Duration.ofDays(7);
@@ -381,7 +384,12 @@ public class RedisHotStore {
 
     public void delete(List<String> ids) {
         if (ids == null || ids.isEmpty()) return;
-        redis.opsForStream().delete(stream, ids.toArray(String[]::new));
+        try {
+            redis.opsForStream().delete(stream, ids.toArray(String[]::new));
+        } catch (Exception e) {
+            log.warn("[RedisHotStore] delete acknowledged stream entries failed | stream={} | count={} | error={}",
+                    stream, ids.size(), rootMessage(e));
+        }
     }
 
     /**
@@ -389,7 +397,12 @@ public class RedisHotStore {
      */
     public void trim(long maxLen) {
         if (maxLen <= 0) return;
-        redis.opsForStream().trim(stream, maxLen, false);
+        try {
+            redis.opsForStream().trim(stream, maxLen, false);
+        } catch (Exception e) {
+            log.warn("[RedisHotStore] trim stream failed | stream={} | maxLen={} | error={}",
+                    stream, maxLen, rootMessage(e));
+        }
     }
 
     private void trimStreamIfNeeded(org.springframework.data.redis.connection.RedisConnection conn) {
@@ -465,6 +478,18 @@ public class RedisHotStore {
         return script;
     }
 
+    private static String rootMessage(Throwable e) {
+        Throwable cur = e;
+        String msg = null;
+        while (cur != null) {
+            if (cur.getMessage() != null && !cur.getMessage().isBlank()) {
+                msg = cur.getMessage();
+            }
+            cur = cur.getCause();
+        }
+        return msg == null ? "" : msg;
+    }
+
     public static class DeductionDecision {
         private final long totalSeen;
         private final long totalDeducted;

+ 13 - 2
src/main/java/com/adx/tencent/vivo/store/VivoHotStore.java

@@ -291,11 +291,22 @@ public class VivoHotStore {
 
     public void delete(List<String> ids) {
         if (ids == null || ids.isEmpty()) return;
-        redis.opsForStream().delete(stream, ids.toArray(String[]::new));
+        try {
+            redis.opsForStream().delete(stream, ids.toArray(String[]::new));
+        } catch (Exception e) {
+            log.warn("[VivoHotStore] delete acknowledged stream entries failed | stream={} | count={} | error={}",
+                    stream, ids.size(), rootMessage(e));
+        }
     }
 
     public void trim(long maxLen) {
-        if (maxLen > 0) redis.opsForStream().trim(stream, maxLen, false);
+        if (maxLen <= 0) return;
+        try {
+            redis.opsForStream().trim(stream, maxLen, false);
+        } catch (Exception e) {
+            log.warn("[VivoHotStore] trim stream failed | stream={} | maxLen={} | error={}",
+                    stream, maxLen, rootMessage(e));
+        }
     }
 
     private void trimStreamIfNeeded(org.springframework.data.redis.connection.RedisConnection conn) {

+ 6 - 0
src/main/resources/application-dev.yml

@@ -51,6 +51,12 @@ adx:
   redis-command-timeout: 5s
   redis-shutdown-timeout: 100ms
   redis-validate-connection: true
+  redis-pool-max-active: 64
+  redis-pool-max-idle: 16
+  redis-pool-min-idle: 4
+  redis-pool-max-wait: 2s
+  redis-pool-test-on-borrow: true
+  redis-pool-test-while-idle: true
 
   # --- TiDB/MySQL ---
   tidb-url: "jdbc:mysql://139.186.172.149:3390/adx_tencent?useSSL=false&useUnicode=true&characterEncoding=UTF-8&serverTimezone=Asia/Shanghai&allowPublicKeyRetrieval=true"

+ 6 - 0
src/main/resources/application-prod.yml

@@ -54,6 +54,12 @@ adx:
   redis-command-timeout: 5s
   redis-shutdown-timeout: 100ms
   redis-validate-connection: true
+  redis-pool-max-active: 64
+  redis-pool-max-idle: 16
+  redis-pool-min-idle: 4
+  redis-pool-max-wait: 2s
+  redis-pool-test-on-borrow: true
+  redis-pool-test-while-idle: true
 
   # --- TiDB/MySQL ---
   tidb-url: "jdbc:mysql://172.30.0.36:3390/adx_tencent?useSSL=false&useUnicode=true&characterEncoding=UTF-8&serverTimezone=Asia/Shanghai&allowPublicKeyRetrieval=true"

+ 6 - 0
src/main/resources/application-test.yml

@@ -52,6 +52,12 @@ adx:
   redis-command-timeout: 5s
   redis-shutdown-timeout: 100ms
   redis-validate-connection: true
+  redis-pool-max-active: 64
+  redis-pool-max-idle: 16
+  redis-pool-min-idle: 4
+  redis-pool-max-wait: 2s
+  redis-pool-test-on-borrow: true
+  redis-pool-test-while-idle: true
 
   # --- TiDB/MySQL ---
   tidb-url: "jdbc:mysql://139.186.172.149:3390/adx_tencent?useSSL=false&useUnicode=true&characterEncoding=UTF-8&serverTimezone=Asia/Shanghai&allowPublicKeyRetrieval=true&connectTimeout=5000&socketTimeout=30000"