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

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


The following commit(s) were added to refs/heads/3.3 by this push:
     new 1d1671ef80 [3.3] jedis from 3.10.0 to 5.1.0 and metadata implementing 
TTL functionality in redis (#14248)
1d1671ef80 is described below

commit 1d1671ef80edaf6222b7290fc0d91cc2c7647e02
Author: 一 <[email protected]>
AuthorDate: Thu May 30 18:05:28 2024 +0800

    [3.3] jedis from 3.10.0 to 5.1.0 and metadata implementing TTL 
functionality in redis (#14248)
    
    * Try fix redis server in windows env
    
    * feat:
    1.bump jedis from 3.10.0 to 5.1.0
    2.metadata implementing TTL functionality in redis
    
    * feat: guaranteed publish command exec
    
    ---------
    
    Co-authored-by: Albumen Kevin <[email protected]>
    Co-authored-by: 其一 <[email protected]>
---
 .../report/support/AbstractMetadataReport.java      |  2 +-
 dubbo-metadata/dubbo-metadata-report-redis/pom.xml  |  2 +-
 .../metadata/store/redis/RedisMetadataReport.java   | 21 ++++++++++++++++-----
 .../store/redis/RedisMetadataReportTest.java        |  1 -
 4 files changed, 18 insertions(+), 8 deletions(-)

diff --git 
a/dubbo-metadata/dubbo-metadata-api/src/main/java/org/apache/dubbo/metadata/report/support/AbstractMetadataReport.java
 
b/dubbo-metadata/dubbo-metadata-api/src/main/java/org/apache/dubbo/metadata/report/support/AbstractMetadataReport.java
index 6d2dcb7a5b..f21e99b3df 100644
--- 
a/dubbo-metadata/dubbo-metadata-api/src/main/java/org/apache/dubbo/metadata/report/support/AbstractMetadataReport.java
+++ 
b/dubbo-metadata/dubbo-metadata-api/src/main/java/org/apache/dubbo/metadata/report/support/AbstractMetadataReport.java
@@ -85,7 +85,7 @@ public abstract class AbstractMetadataReport implements 
MetadataReport {
 
     protected static final String DEFAULT_ROOT = "dubbo";
 
-    private static final int ONE_DAY_IN_MILLISECONDS = 60 * 24 * 60 * 1000;
+    protected static final int ONE_DAY_IN_MILLISECONDS = 60 * 24 * 60 * 1000;
     private static final int FOUR_HOURS_IN_MILLISECONDS = 60 * 4 * 60 * 1000;
     // Log output
     protected final ErrorTypeAwareLogger logger = 
LoggerFactory.getErrorTypeAwareLogger(getClass());
diff --git a/dubbo-metadata/dubbo-metadata-report-redis/pom.xml 
b/dubbo-metadata/dubbo-metadata-report-redis/pom.xml
index bdeb867c57..2cc33a888c 100644
--- a/dubbo-metadata/dubbo-metadata-report-redis/pom.xml
+++ b/dubbo-metadata/dubbo-metadata-report-redis/pom.xml
@@ -25,7 +25,7 @@
 
   <artifactId>dubbo-metadata-report-redis</artifactId>
   <properties>
-    <jedis.version>3.10.0</jedis.version>
+    <jedis.version>5.1.0</jedis.version>
   </properties>
 
   <dependencies>
diff --git 
a/dubbo-metadata/dubbo-metadata-report-redis/src/main/java/org/apache/dubbo/metadata/store/redis/RedisMetadataReport.java
 
b/dubbo-metadata/dubbo-metadata-report-redis/src/main/java/org/apache/dubbo/metadata/store/redis/RedisMetadataReport.java
index 7c64220faa..6c2493ccff 100644
--- 
a/dubbo-metadata/dubbo-metadata-report-redis/src/main/java/org/apache/dubbo/metadata/store/redis/RedisMetadataReport.java
+++ 
b/dubbo-metadata/dubbo-metadata-report-redis/src/main/java/org/apache/dubbo/metadata/store/redis/RedisMetadataReport.java
@@ -53,9 +53,11 @@ import redis.clients.jedis.JedisPool;
 import redis.clients.jedis.JedisPoolConfig;
 import redis.clients.jedis.JedisPubSub;
 import redis.clients.jedis.Transaction;
+import redis.clients.jedis.params.SetParams;
 import redis.clients.jedis.util.JedisClusterCRC16;
 
 import static org.apache.dubbo.common.constants.CommonConstants.CLUSTER_KEY;
+import static 
org.apache.dubbo.common.constants.CommonConstants.CYCLE_REPORT_KEY;
 import static 
org.apache.dubbo.common.constants.CommonConstants.DEFAULT_TIMEOUT;
 import static 
org.apache.dubbo.common.constants.CommonConstants.GROUP_CHAR_SEPARATOR;
 import static org.apache.dubbo.common.constants.CommonConstants.QUEUES_KEY;
@@ -64,6 +66,7 @@ import static 
org.apache.dubbo.common.constants.LoggerCodeConstants.TRANSPORT_FA
 import static org.apache.dubbo.metadata.MetadataConstants.META_DATA_STORE_TAG;
 import static 
org.apache.dubbo.metadata.ServiceNameMapping.DEFAULT_MAPPING_GROUP;
 import static org.apache.dubbo.metadata.ServiceNameMapping.getAppNames;
+import static 
org.apache.dubbo.metadata.report.support.Constants.DEFAULT_METADATA_REPORT_CYCLE_REPORT;
 
 /**
  * RedisMetadataReport
@@ -80,12 +83,17 @@ public class RedisMetadataReport extends 
AbstractMetadataReport {
     private String password;
     private final String root;
     private final ConcurrentHashMap<String, MappingDataListener> 
mappingDataListenerMap = new ConcurrentHashMap<>();
+    private SetParams jedisParams = SetParams.setParams();
 
     public RedisMetadataReport(URL url) {
         super(url);
         timeout = url.getParameter(TIMEOUT_KEY, DEFAULT_TIMEOUT);
         password = url.getPassword();
         this.root = url.getGroup(DEFAULT_ROOT);
+        if (url.getParameter(CYCLE_REPORT_KEY, 
DEFAULT_METADATA_REPORT_CYCLE_REPORT)) {
+            // ttl default is twice the cycle-report time
+            jedisParams.ex(ONE_DAY_IN_MILLISECONDS * 2);
+        }
         if (url.getParameter(CLUSTER_KEY, false)) {
             jedisClusterNodes = new HashSet<>();
             List<URL> urls = url.getBackupUrls();
@@ -153,7 +161,7 @@ public class RedisMetadataReport extends 
AbstractMetadataReport {
     private void storeMetadataInCluster(BaseMetadataIdentifier 
metadataIdentifier, String v) {
         try (JedisCluster jedisCluster =
                 new JedisCluster(jedisClusterNodes, timeout, timeout, 2, 
password, new GenericObjectPoolConfig<>())) {
-            jedisCluster.set(metadataIdentifier.getIdentifierKey() + 
META_DATA_STORE_TAG, v);
+            jedisCluster.set(metadataIdentifier.getIdentifierKey() + 
META_DATA_STORE_TAG, v, jedisParams);
         } catch (Throwable e) {
             String msg =
                     "Failed to put " + metadataIdentifier + " to redis cluster 
" + v + ", cause: " + e.getMessage();
@@ -164,7 +172,7 @@ public class RedisMetadataReport extends 
AbstractMetadataReport {
 
     private void storeMetadataStandalone(BaseMetadataIdentifier 
metadataIdentifier, String v) {
         try (Jedis jedis = pool.getResource()) {
-            jedis.set(metadataIdentifier.getUniqueKey(KeyTypeEnum.UNIQUE_KEY), 
v);
+            jedis.set(metadataIdentifier.getUniqueKey(KeyTypeEnum.UNIQUE_KEY), 
v, jedisParams);
         } catch (Throwable e) {
             String msg = "Failed to put " + metadataIdentifier + " to redis " 
+ v + ", cause: " + e.getMessage();
             logger.error(TRANSPORT_FAILED_RESPONSE, "", "", msg, e);
@@ -272,7 +280,7 @@ public class RedisMetadataReport extends 
AbstractMetadataReport {
     private boolean storeMappingInCluster(String key, String field, String 
value, String ticket) {
         try (JedisCluster jedisCluster =
                 new JedisCluster(jedisClusterNodes, timeout, timeout, 2, 
password, new GenericObjectPoolConfig<>())) {
-            Jedis jedis = 
jedisCluster.getConnectionFromSlot(JedisClusterCRC16.getSlot(key));
+            Jedis jedis = new 
Jedis(jedisCluster.getConnectionFromSlot(JedisClusterCRC16.getSlot(key)));
             jedis.watch(key);
             String oldValue = jedis.hget(key, field);
             if (null == oldValue || null == ticket || oldValue.equals(ticket)) 
{
@@ -286,6 +294,7 @@ public class RedisMetadataReport extends 
AbstractMetadataReport {
             } else {
                 jedis.unwatch();
             }
+            jedis.close();
         } catch (Throwable e) {
             String msg = "Failed to put " + key + ":" + field + " to redis " + 
value + ", cause: " + e.getMessage();
             logger.error(TRANSPORT_FAILED_RESPONSE, "", "", msg, e);
@@ -305,9 +314,11 @@ public class RedisMetadataReport extends 
AbstractMetadataReport {
             if (null == oldValue || null == ticket || oldValue.equals(ticket)) 
{
                 Transaction transaction = jedis.multi();
                 transaction.hset(key, field, value);
-                transaction.publish(buildPubSubKey(), field);
                 List<Object> result = transaction.exec();
-                return null != result;
+                if (null != result) {
+                    jedis.publish(buildPubSubKey(), field);
+                    return true;
+                }
             }
             jedis.unwatch();
         } catch (Throwable e) {
diff --git 
a/dubbo-metadata/dubbo-metadata-report-redis/src/test/java/org/apache/dubbo/metadata/store/redis/RedisMetadataReportTest.java
 
b/dubbo-metadata/dubbo-metadata-report-redis/src/test/java/org/apache/dubbo/metadata/store/redis/RedisMetadataReportTest.java
index 1b0e01709a..85569d49c9 100644
--- 
a/dubbo-metadata/dubbo-metadata-report-redis/src/test/java/org/apache/dubbo/metadata/store/redis/RedisMetadataReportTest.java
+++ 
b/dubbo-metadata/dubbo-metadata-report-redis/src/test/java/org/apache/dubbo/metadata/store/redis/RedisMetadataReportTest.java
@@ -253,7 +253,6 @@ class RedisMetadataReportTest {
 
     @Test
     void testWrongAuthRedisMetadata() throws ClassNotFoundException {
-        registryUrl = registryUrl.setPassword("123456");
         redisMetadataReport = (RedisMetadataReport) new 
RedisMetadataReportFactory().createMetadataReport(registryUrl);
         try {
             testStoreProvider(redisMetadataReport, "1.0.0.redis.md.p1", 3000);

Reply via email to