Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support RedisConnectionStateListener registration for individual connections #1838

Closed
btthorstenwitt opened this issue Sep 1, 2021 · 2 comments
Labels
type: enhancement A general enhancement
Milestone

Comments

@btthorstenwitt
Copy link

Hello,

we develop a springboot application with lettuce-core 6.1.4 in combination with the azure service: Azure Cache for Redis 6.

There is a simple class that manages the connection to the redis server:

@Component
public class SimpleRedisCache {


    private final Map<String,String> clientCache = new ConcurrentHashMap<>();
    private CacheFrontend<String, String> frontendCache;
    private StatefulRedisConnection<String, String> redisConnection;

    public String get(String key) {
        return frontendCache.get(key);
    }

    public String get(String key, Callable<String> valueLoader) {
        return frontendCache.get(key, valueLoader);
    }

    public void close() {
        frontendCache.close();
    }

    @PreDestroy
    public void preDestroy() {
        close();
    }

    @PostConstruct
    public void postConstruct()  {
        RedisClient client = RedisClient.create(getRedisURI());
        this.redisConnection = client.connect();
        this.frontendCache = ClientSideCaching.enable(
                CacheAccessor.forMap(clientCache),
                redisConnection,
                TrackingArgs.Builder.enabled());
    }

    private RedisURI getRedisURI(){
        return RedisURI.builder()
                 ...
                .build();
    }

As you can see we have activated the ClientTracking with:

this.frontendCache = ClientSideCaching.enable(
                CacheAccessor.forMap(clientCache),
                redisConnection,
                TrackingArgs.Builder.enabled());

This works fine at the startup of the application. But after 5 minutes the connection between Azure Redis Cache and the application gets lost and the ConnectionWatchdog does the reconnect:

2021-09-01 11:32:29.098  INFO 1 --- [xecutorLoop-8-1] i.l.core.protocol.ConnectionWatchdog    : Reconnecting, last destination was demo-redis.redis.cache.windows.net/20.101.116.187:6380
2021-09-01 11:32:29.170  INFO 1 --- [lEventLoop-10-2] i.l.core.protocol.ReconnectionHandler    : Reconnected to demo-redis.redis.cache.windows.net:6380

This works very nice and I can see in the logs that the connection is open and the client has access to the keys in the cache. But after Watchdog does the Reconnect, the client tracking seems to be not active anymore.

Is this a bug or could you please tell me how to reactivate the client tracking?

@mp911de
Copy link
Collaborator

mp911de commented Sep 1, 2021

This is a known limitation and I currently don't have a good idea how to approach it. You could approach it through a RedisConnectionStateListener but that doesn't work well with multiple connections being used for different purposes.

Maybe some per-connection listener registration could work.

@mp911de mp911de added the type: enhancement A general enhancement label Sep 3, 2021
@mp911de mp911de added this to the 6.2.0 milestone Sep 9, 2021
@mp911de
Copy link
Collaborator

mp911de commented Sep 9, 2021

For a first step, I added addListener(RedisConnectionStateListener) to StatefulConnection so client code can register listeners for particular connections and react to connects/disconnects. You can call connection.async().clientTracking(tracking) within the onRedisConnected method to re-enable client-side tracking of keys.

However, due to the disconnect, Redis doesn't have the information for which particular keys you want to get notified. There haven't been any keys touched in the context of the new connection.

mp911de added a commit that referenced this issue Sep 9, 2021
…onnection #1838

We now allow registering a RedisConnectionStateListener for a StatefulConnection to allow notifications within the context of a specific connection.
@mp911de mp911de removed this from the 6.2.0 milestone Jul 11, 2022
@mp911de mp911de changed the title Lettuce loosing clientTracking after Reconnect Support RedisConnectionStateListener registration for individual connections Jul 12, 2022
@mp911de mp911de added this to the 6.2.0.RELEASE milestone Jul 12, 2022
@mp911de mp911de closed this as completed Jul 12, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type: enhancement A general enhancement
Projects
None yet
Development

No branches or pull requests

2 participants