[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 <jhq0812@gmail.com>
Co-authored-by: 其一 <yuanhongbo@cai-inc.com>
This commit is contained in:
2024-05-30 18:05:28 +08:00 committed by GitHub
parent bc83ae32c9
commit 1d1671ef80
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 18 additions and 8 deletions

View File

@ -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());

View File

@ -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>

View File

@ -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) {

View File

@ -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);