Linchen-Xu commented on code in PR #13238:
URL: https://github.com/apache/dubbo/pull/13238#discussion_r1371554351


##########
dubbo-metadata/dubbo-metadata-report-redis/src/main/java/org/apache/dubbo/metadata/store/redis/RedisMetadataReport.java:
##########
@@ -207,4 +226,258 @@ private String 
getMetadataStandalone(BaseMetadataIdentifier metadataIdentifier)
         }
     }
 
+    @Override
+    public boolean registerServiceAppMapping(String serviceInterface, String 
defaultMappingGroup, String newConfigContent, Object ticket) {
+        try {
+            if (null!= ticket && !(ticket instanceof String)) {
+                throw new IllegalArgumentException("zookeeper publishConfigCas 
requires stat type ticket");
+            }
+            String pathKey = buildMappingKey(defaultMappingGroup);
+
+            return storeMapping(pathKey, serviceInterface, 
newConfigContent,(String)ticket);
+        } catch (Exception e) {
+            logger.warn(REGISTRY_ZOOKEEPER_EXCEPTION, "", "", "redis 
publishConfigCas failed.", e);
+            return false;
+        }
+    }
+
+
+    private boolean storeMapping(String key, String field, String value,String 
ticket) {
+        if (pool != null) {
+            return storeMappingStandalone(key, field, value, ticket);
+        } else {
+            return storeMappingInCluster(key, field, value, ticket);
+        }
+    }
+
+    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.watch(key);
+            String oldValue = jedis.get(key);
+            if (null==oldValue||null==ticket||oldValue.equals(ticket)) {
+                Transaction transaction = jedis.multi();
+                transaction.hset(key,field,value);
+                List<Object> result=transaction.exec();
+                if(null!=result){
+                    jedisCluster.publish(buildPubSubKey(field),value);
+                    return true;
+                }
+            }
+        } catch (Throwable e) {
+            String msg = "Failed to put " + key + ":" + field + " to redis " + 
value + ", cause: " + e.getMessage();
+            logger.error(TRANSPORT_FAILED_RESPONSE, "", "", msg, e);
+            throw new RpcException(msg, e);
+        }
+        return false;
+    }
+
+    private boolean storeMappingStandalone(String key, String field, String 
value,String ticket) {
+        try (Jedis jedis = pool.getResource()) {
+            jedis.watch(key);

Review Comment:
   
watch可以考虑移到transaction里。一是因为watch的目的是为了保证在transaction期间key不被更改,二是watch之后需要手动执行[unwatch](https://redis.io/commands/unwatch/)(除非执行过discard或exec)。
   建议改成:
   ```suggestion
               Transaction transaction = jedis.multi();
               transaction.watch(key);
               ...
   ```
   或:
   ```suggestion
               jedis.watch(key);
               ...
                   List<Object> result=transaction.exec();
                   return null!=result;
               }
               jedis.unwatch();
   ```



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: notifications-unsubscr...@dubbo.apache.org

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org


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

Reply via email to