[DATAREDIS-877] Destroy LettuceConnectionFactory block too long Created: 15/Oct/18  Updated: 16/Oct/18  Resolved: 15/Oct/18

Status: Closed
Project: Spring Data Redis
Component/s: Lettuce Driver
Affects Version/s: 2.1 GA (Lovelace)
Fix Version/s: None

Type: Improvement Priority: Minor
Reporter: Yanming Zhou Assignee: Mark Paluch
Resolution: Works as Designed Votes: 0
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified

Issue Links:
Relate
relates to DATAREDIS-881 Add config parameter shutdownQuietPer... Closed
Last updater: Mark Paluch

 Description   

After switch redis driver from jedis to lettuce, closing ApplicationContext take more about 1.1s.

import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
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.LettuceConnectionFactory;
import org.springframework.data.redis.core.StringRedisTemplate;

import io.lettuce.core.ClientOptions;

public class Main {

	public static void main(String[] args) {
		test(false);
		test(true);
	}

	private static void test(boolean interaction) {
		AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext(Config.class);
		if (interaction) {
			StringRedisTemplate srt = ctx.getBean(StringRedisTemplate.class);
			srt.delete("anything"); // any operation
		}
		long time = System.currentTimeMillis();
		ctx.close();
		System.out.println(
				(System.currentTimeMillis() - time) + " ms " + (interaction ? "with" : "without") + " interaction");
	}

	@Configuration
	public static class Config {

		@Bean
		public RedisConnectionFactory redisConnectionFactory() {
			LettuceClientConfiguration clientConfiguration = LettuceClientConfiguration.builder()
					.clientOptions(ClientOptions.builder().build()).build();
			RedisStandaloneConfiguration standaloneConfiguration = new RedisStandaloneConfiguration("localhost", 6379);
			return new LettuceConnectionFactory(standaloneConfiguration, clientConfiguration);
		}

		@Bean
		public StringRedisTemplate stringRedisTemplate(RedisConnectionFactory redisConnectionFactory) {
			return new StringRedisTemplate(redisConnectionFactory);
		}

	}

}

output result:
113 ms without interaction
1113 ms with interaction
 

 



 Comments   
Comment by Mark Paluch [ 15/Oct/18 ]

The observed delay between closing the context and the actual termination originates in Lettuce and is not related to Spring Data Redis. Lettuce is trying to dispose its threading infrastructure and uses a timeout to cleanup resources. You can configure this timeout with LettuceClientConfigurationBuilder#shutdownTimeout(Duration.ZERO) to suit your needs.

Comment by Yanming Zhou [ 15/Oct/18 ]

Is there any side effect to configure ZERO duration? why not default ZERO?

Comment by Mark Paluch [ 15/Oct/18 ]

A zero timeout bears the risk of I/O tasks being rejected if they are not completed before you initiate shutdown. See  https://stackoverflow.com/a/29116343/2067527 for further reference.

Comment by Yanming Zhou [ 15/Oct/18 ]

I think timeout means max time to wait, shutdownTimeout seems to be fixed time to wait, should it rename to shutdownDelay?

Comment by Mark Paluch [ 15/Oct/18 ]

We will stick with the current name as this configuration parameter isn't a delay. It's used as synchronization timeout and as quiet period timeout.

Comment by Yanming Zhou [ 15/Oct/18 ]

https://github.com/spring-projects/spring-data-redis/blob/master/src/main/java/org/springframework/data/redis/connection/lettuce/LettuceConnectionFactory.java#L306

client.shutdown(timeout.toMillis(), timeout.toMillis(), TimeUnit.MILLISECONDS);

quietPeriod should be 0 instead of timeout.toMillis(), what do you think ?

Comment by Yanming Zhou [ 16/Oct/18 ]

Refer to https://github.com/netty/netty/blob/4.1/common/src/main/java/io/netty/util/concurrent/EventExecutorGroup.java#L52 , shutdownQuietPeriod should be extra option separated from shutdownTimeout.

Comment by Mark Paluch [ 16/Oct/18 ]

The second option, adding an additional config parameter, is something that we could implement. Do you want to create a new ticket and submit a pull request?

Comment by Yanming Zhou [ 16/Oct/18 ]

I will try it.

Generated at Fri Jan 24 05:50:58 UTC 2020 using Jira 7.13.8#713008-sha1:1606a5c1e7006e1ab135aac81f7a9566b2dbc3a6.