redis / lettuce

Advanced Java Redis client for thread-safe sync, async, and reactive usage. Supports Cluster, Sentinel, Pipelining, and codecs.
https://lettuce.io
MIT License
5.37k stars 962 forks source link

When using the Master/Slave Sentinel server configuration with Read Replica Preferred, does Lettuce support the execution of RedisScript? #2321

Open banilkumar1606 opened 1 year ago

banilkumar1606 commented 1 year ago

Our objective is to use LettuceConfiguration for Redis Sentinel server to obtain the Redis lock and for caching @Cachable. We are having the following problem.

 : Failed to complete request: org.springframework.dao.CannotAcquireLockException: Failed to lock mutex at DEFAULT_LOCK_REGISTRY:userid; nested exception is org.springframework.data.redis. RedisSystemException: Error in execution; nested exception is io.lettuce.core. RedisCommandExecutionException: ERR Error running script (call to f_8426
c8df41c64d8177dce3ecbbe9146ef3759cd2): @user_script:6: @user_script: 6: -READONLY You can't write against a read only slave.

Configuration looks like below:

 /**
  * Lettuce
  */
 @Bean("redisslaveconnection")
 public RedisConnectionFactory lettuceConnectionFactory() {
  RedisSentinelConfiguration sentinelConfig = new RedisSentinelConfiguration()
          .master("mymaster")
          .sentinel("127.0.0.1", 16380);
  sentinelConfig.setPassword("xxxxx");
  LettuceClientConfiguration clientConfig = LettuceClientConfiguration.builder()
          .commandTimeout(redisCommandTimeout)
          .readFrom(ReadFrom.REPLICA_PREFERRED)
          .clientOptions(clusterClientOptions())
          .build();

  return new LettuceConnectionFactory(sentinelConfig, clientConfig);
 }
@Bean
 public RedisCacheManager cacheManager() {
  return RedisCacheManager.builder(this.lettuceConnectionFactory()).cacheDefaults(this.cacheConfiguration())
    .build();
 }
@Bean
 public RedisLockRegistry lockRegistry(RedisConnectionFactory cf) {
  return new RedisLockRegistry(cf, "Lock_Registery_Name", 120000);
 }

We have a RedisScript to get the lock inside the RedisLockRegistry.

Please let me know whether Lettuce will accept this kind of script or if there are any other options I can use with Read REPLICA PREFERRED settings. Do you think it would be a good idea to create two beans with the properties MASTER PREFERRED and REPLICA PREFERRED in order to gain the lock MASTER PREFERRED bean and for @CacheManager REPLICA PREFERRED bean?

tishun commented 3 months ago

Is this issue still relevant?