Skip to content

Commit

Permalink
DATAREDIS-369 - Avoid resource leak in RedisCache.
Browse files Browse the repository at this point in the history
We now check the presence of a prefix which allows to identify the keys associated with the cache to either keep track of keys or use the prefix to remove them on clear.

This is a small fix intended to be back ported to the Evans (1.4.x) release train. Additional refactoring will be done for the Fowler release.

Original Pull Request: #122
  • Loading branch information
christophstrobl committed Feb 5, 2015
1 parent 0ea0c4b commit b242d4a
Show file tree
Hide file tree
Showing 3 changed files with 187 additions and 17 deletions.
2 changes: 2 additions & 0 deletions src/asciidoc/reference/redis.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -399,6 +399,8 @@ NOTE: By default `RedisCacheManager` will lazily initialize `RedisCache` wheneve

NOTE: By default `RedisCacheManager` will not participate in any ongoing transaction. Use `setTransactionAware` to enable transaction support.

NOTE: By default `RedisCacheManager` does not prefix keys for cache regions, which can lead to an unexpected growth of a `ZSET` used to maintain known keys. It's highly recommended to enable the usage of prefixes in order to avoid this unexpected growth and potential key clashes using more than one cache region.

[[redis:future]]
== Roadmap ahead

Expand Down
70 changes: 53 additions & 17 deletions src/main/java/org/springframework/data/redis/cache/RedisCache.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import org.springframework.cache.support.SimpleValueWrapper;
import org.springframework.dao.DataAccessException;
import org.springframework.data.redis.connection.RedisConnection;
import org.springframework.data.redis.connection.ReturnType;
import org.springframework.data.redis.core.RedisCallback;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.RedisSerializer;
Expand All @@ -39,6 +40,8 @@
@SuppressWarnings("unchecked")
public class RedisCache implements Cache {

private static final byte[] REMOVE_KEYS_BY_PATTERN_LUA = "local keys = redis.call('KEYS', ARGV[1]); local keysCount = table.getn(keys); if(keysCount > 0) then for _, key in ipairs(keys) do redis.call('del', key); end; end; return keysCount;"
.getBytes();
private static final int PAGE_SIZE = 128;
private final String name;
@SuppressWarnings("rawtypes") private final RedisTemplate template;
Expand Down Expand Up @@ -124,12 +127,17 @@ public Object doInRedis(RedisConnection connection) throws DataAccessException {
connection.multi();

connection.set(keyBytes, valueBytes);
connection.zAdd(setName, 0, keyBytes);

if (shouldTrackKeys()) {
connection.zAdd(setName, 0, keyBytes);
}
if (expiration > 0) {
connection.expire(keyBytes, expiration);
// update the expiration of the set of keys as well
connection.expire(setName, expiration);

if (shouldTrackKeys()) {
connection.expire(setName, expiration);
}
}
connection.exec();

Expand All @@ -151,11 +159,18 @@ public Object doInRedis(RedisConnection connection) throws DataAccessException {
Object resultValue = value;
boolean valueWasSet = connection.setNX(keyBytes, valueBytes);
if (valueWasSet) {
connection.zAdd(setName, 0, keyBytes);

if (shouldTrackKeys()) {
connection.zAdd(setName, 0, keyBytes);
}
if (expiration > 0) {

connection.expire(keyBytes, expiration);
// update the expiration of the set of keys as well
connection.expire(setName, expiration);

if (shouldTrackKeys()) {
connection.expire(setName, expiration);
}
}
} else {
resultValue = deserializeIfNecessary(template.getValueSerializer(), connection.get(keyBytes));
Expand All @@ -177,7 +192,9 @@ public void evict(Object key) {
public Object doInRedis(RedisConnection connection) throws DataAccessException {
connection.del(k);
// remove key from set
connection.zRem(setName, k);
if (shouldTrackKeys()) {
connection.zRem(setName, k);
}
return null;
}
}, true);
Expand All @@ -198,19 +215,27 @@ public Object doInRedis(RedisConnection connection) throws DataAccessException {
int offset = 0;
boolean finished = false;

do {
// need to paginate the keys
Set<byte[]> keys = connection.zRange(setName, (offset) * PAGE_SIZE, (offset + 1) * PAGE_SIZE - 1);
finished = keys.size() < PAGE_SIZE;
offset++;
if (!keys.isEmpty()) {
connection.del(keys.toArray(new byte[keys.size()][]));
}
} while (!finished);

connection.del(setName);
if (shouldTrackKeys()) {
do {
// need to paginate the keys
Set<byte[]> keys = connection.zRange(setName, (offset) * PAGE_SIZE, (offset + 1) * PAGE_SIZE - 1);
finished = keys.size() < PAGE_SIZE;
offset++;
if (!keys.isEmpty()) {
connection.del(keys.toArray(new byte[keys.size()][]));
}
} while (!finished);

connection.del(setName);
} else {

byte[] wildCard = "*".getBytes();
byte[] prefixToUse = Arrays.copyOf(prefix, prefix.length + wildCard.length);
System.arraycopy(wildCard, 0, prefixToUse, prefix.length, wildCard.length);

connection.eval(REMOVE_KEYS_BY_PATTERN_LUA, ReturnType.INTEGER, 0, prefixToUse);
}
return null;

} finally {
connection.del(cacheLockName);
}
Expand Down Expand Up @@ -270,4 +295,15 @@ private Object deserializeIfNecessary(RedisSerializer<byte[]> serializer, byte[]
return value;
}

/**
* A set of keys needs to be maintained in case no prefix is provided. Clear cache depends on either a pattern to
* identify keys to remove or a maintained set of known keys, otherwise the entire db would be flushed potentially
* removing non cache entry values.
*
* @return
*/
private boolean shouldTrackKeys() {
return (prefix == null || prefix.length == 0);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,132 @@
/*
* Copyright 2015 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.data.redis.cache;

import static org.mockito.Matchers.*;
import static org.mockito.Mockito.*;

import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.runners.MockitoJUnitRunner;
import org.springframework.data.redis.connection.RedisConnection;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.connection.ReturnType;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.RedisSerializer;

/**
* @author Christoph Strobl
*/
@SuppressWarnings("rawtypes")
@RunWith(MockitoJUnitRunner.class)
public class RedisCacheUnitTests {

private static final String CACHE_NAME = "foo";
private static final String PREFIX = "prefix:";
private static final byte[] PREFIX_BYTES = "prefix:".getBytes();
private static final byte[] KNOWN_KEYS_SET_NAME_BYTES = (CACHE_NAME + "~keys").getBytes();

private static final String KEY = "key";
private static final byte[] KEY_BYTES = KEY.getBytes();
private static final byte[] KEY_WITH_PREFIX_BYTES = (PREFIX + KEY).getBytes();

private static final String VALUE = "value";
private static final byte[] VALUE_BYTES = VALUE.getBytes();

private static final byte[] NO_PREFIX_BYTES = new byte[] {};
private static final long EXPIRATION = 1000;

RedisTemplate<?, ?> templateSpy;
@Mock RedisSerializer keySerializerMock;
@Mock RedisSerializer valueSerializerMock;
@Mock RedisConnectionFactory connectionFactoryMock;
@Mock RedisConnection connectionMock;

RedisCache cache;

@SuppressWarnings("unchecked")
@Before
public void setUp() {

RedisTemplate template = new RedisTemplate();
template.setConnectionFactory(connectionFactoryMock);
template.setKeySerializer(keySerializerMock);
template.setValueSerializer(valueSerializerMock);
template.afterPropertiesSet();

templateSpy = spy(template);

when(connectionFactoryMock.getConnection()).thenReturn(connectionMock);

when(keySerializerMock.serialize(any(byte[].class))).thenReturn(KEY_BYTES);
when(valueSerializerMock.serialize(any(byte[].class))).thenReturn(VALUE_BYTES);
}

/**
* @see DATAREDIS-369
*/
@Test
public void putShouldNotKeepTrackOfKnownKeysWhenPrefixIsSet() {

cache = new RedisCache(CACHE_NAME, PREFIX_BYTES, templateSpy, EXPIRATION);
cache.put(KEY, VALUE);

verify(connectionMock, times(1)).set(eq(KEY_WITH_PREFIX_BYTES), eq(VALUE_BYTES));
verify(connectionMock, times(1)).expire(eq(KEY_WITH_PREFIX_BYTES), eq(EXPIRATION));
verify(connectionMock, never()).zAdd(eq(KNOWN_KEYS_SET_NAME_BYTES), eq(0D), any(byte[].class));
}

/**
* @see DATAREDIS-369
*/
@Test
public void putShouldKeepTrackOfKnownKeysWhenNoPrefixIsSet() {

cache = new RedisCache(CACHE_NAME, NO_PREFIX_BYTES, templateSpy, EXPIRATION);
cache.put(KEY, VALUE);

verify(connectionMock, times(1)).set(eq(KEY_BYTES), eq(VALUE_BYTES));
verify(connectionMock, times(1)).expire(eq(KEY_BYTES), eq(EXPIRATION));
verify(connectionMock, times(1)).zAdd(eq(KNOWN_KEYS_SET_NAME_BYTES), eq(0D), eq(KEY_BYTES));
}

/**
* @see DATAREDIS-369
*/
@Test
public void clearShouldRemoveKeysUsingKnownKeysWhenNoPrefixIsSet() {

cache = new RedisCache(CACHE_NAME, NO_PREFIX_BYTES, templateSpy, EXPIRATION);
cache.clear();

verify(connectionMock, times(1)).zRange(eq(KNOWN_KEYS_SET_NAME_BYTES), eq(0L), eq(127L));
}

/**
* @see DATAREDIS-369
*/
@Test
public void clearShouldCallLuaScritpToRemoveKeysWhenPrefixIsSet() {

cache = new RedisCache(CACHE_NAME, PREFIX_BYTES, templateSpy, EXPIRATION);
cache.clear();

verify(connectionMock, times(1)).eval(any(byte[].class), eq(ReturnType.INTEGER), eq(0),
eq((PREFIX + "*").getBytes()));
}
}

0 comments on commit b242d4a

Please sign in to comment.