This repository was archived by the owner on May 30, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 56
Use a thread-safe Jedis pool #37
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,10 +5,12 @@ | |
import com.google.common.cache.LoadingCache; | ||
import com.google.gson.Gson; | ||
import com.google.gson.reflect.TypeToken; | ||
import org.apache.http.util.EntityUtils; | ||
import redis.clients.jedis.Jedis; | ||
import redis.clients.jedis.JedisPool; | ||
import redis.clients.jedis.JedisPoolConfig; | ||
import redis.clients.jedis.Transaction; | ||
|
||
import java.io.IOException; | ||
import java.lang.reflect.Type; | ||
import java.net.URI; | ||
import java.util.HashMap; | ||
|
@@ -22,7 +24,7 @@ | |
*/ | ||
public class RedisFeatureStore implements FeatureStore { | ||
private static final String DEFAULT_PREFIX = "launchdarkly"; | ||
private final Jedis jedis; | ||
private final JedisPool pool; | ||
private LoadingCache<String, FeatureRep<?>> cache; | ||
private LoadingCache<String, Boolean> initCache; | ||
private String prefix; | ||
|
@@ -36,7 +38,7 @@ public class RedisFeatureStore implements FeatureStore { | |
* @param cacheTimeSecs an optional timeout for the in-memory cache. If set to 0, no in-memory caching will be performed | ||
*/ | ||
public RedisFeatureStore(String host, int port, String prefix, long cacheTimeSecs) { | ||
jedis = new Jedis(host, port); | ||
pool = new JedisPool(getPoolConfig(), host, port); | ||
setPrefix(prefix); | ||
createCache(cacheTimeSecs); | ||
createInitCache(cacheTimeSecs); | ||
|
@@ -50,7 +52,7 @@ public RedisFeatureStore(String host, int port, String prefix, long cacheTimeSec | |
* @param cacheTimeSecs an optional timeout for the in-memory cache. If set to 0, no in-memory caching will be performed | ||
*/ | ||
public RedisFeatureStore(URI uri, String prefix, long cacheTimeSecs) { | ||
jedis = new Jedis(uri); | ||
pool = new JedisPool(getPoolConfig(), uri); | ||
setPrefix(prefix); | ||
createCache(cacheTimeSecs); | ||
createInitCache(cacheTimeSecs); | ||
|
@@ -61,7 +63,7 @@ public RedisFeatureStore(URI uri, String prefix, long cacheTimeSecs) { | |
* | ||
*/ | ||
public RedisFeatureStore() { | ||
jedis = new Jedis("localhost"); | ||
pool = new JedisPool(getPoolConfig(), "localhost"); | ||
this.prefix = DEFAULT_PREFIX; | ||
} | ||
|
||
|
@@ -128,18 +130,26 @@ public FeatureRep<?> get(String key) { | |
*/ | ||
@Override | ||
public Map<String, FeatureRep<?>> all() { | ||
Map<String,String> featuresJson = jedis.hgetAll(featuresKey()); | ||
Map<String, FeatureRep<?>> result = new HashMap<String, FeatureRep<?>>(); | ||
Gson gson = new Gson(); | ||
Jedis jedis = null; | ||
try { | ||
jedis = getJedis(); | ||
Map<String,String> featuresJson = getJedis().hgetAll(featuresKey()); | ||
Map<String, FeatureRep<?>> result = new HashMap<String, FeatureRep<?>>(); | ||
Gson gson = new Gson(); | ||
|
||
Type type = new TypeToken<FeatureRep<?>>() {}.getType(); | ||
Type type = new TypeToken<FeatureRep<?>>() {}.getType(); | ||
|
||
for (Map.Entry<String, String> entry : featuresJson.entrySet()) { | ||
FeatureRep<?> rep = gson.fromJson(entry.getValue(), type); | ||
result.put(entry.getKey(), rep); | ||
for (Map.Entry<String, String> entry : featuresJson.entrySet()) { | ||
FeatureRep<?> rep = gson.fromJson(entry.getValue(), type); | ||
result.put(entry.getKey(), rep); | ||
} | ||
return result; | ||
} finally { | ||
if (jedis != null) { | ||
jedis.close(); | ||
} | ||
} | ||
|
||
return result; | ||
} | ||
/** | ||
* Initializes (or re-initializes) the store with the specified set of features. Any existing entries | ||
|
@@ -149,16 +159,25 @@ public Map<String, FeatureRep<?>> all() { | |
*/ | ||
@Override | ||
public void init(Map<String, FeatureRep<?>> features) { | ||
Gson gson = new Gson(); | ||
Transaction t = jedis.multi(); | ||
Jedis jedis = null; | ||
|
||
t.del(featuresKey()); | ||
try { | ||
jedis = getJedis(); | ||
Gson gson = new Gson(); | ||
Transaction t = jedis.multi(); | ||
|
||
for (FeatureRep<?> f: features.values()) { | ||
t.hset(featuresKey(), f.key, gson.toJson(f)); | ||
} | ||
t.del(featuresKey()); | ||
|
||
t.exec(); | ||
for (FeatureRep<?> f: features.values()) { | ||
t.hset(featuresKey(), f.key, gson.toJson(f)); | ||
} | ||
|
||
t.exec(); | ||
} finally { | ||
if (jedis != null) { | ||
jedis.close(); | ||
} | ||
} | ||
} | ||
|
||
|
||
|
@@ -172,7 +191,9 @@ public void init(Map<String, FeatureRep<?>> features) { | |
*/ | ||
@Override | ||
public void delete(String key, int version) { | ||
Jedis jedis = null; | ||
try { | ||
jedis = getJedis(); | ||
Gson gson = new Gson(); | ||
jedis.watch(featuresKey()); | ||
|
||
|
@@ -192,7 +213,10 @@ public void delete(String key, int version) { | |
} | ||
} | ||
finally { | ||
jedis.unwatch(); | ||
if (jedis != null) { | ||
jedis.unwatch(); | ||
jedis.close(); | ||
} | ||
} | ||
|
||
} | ||
|
@@ -206,7 +230,9 @@ public void delete(String key, int version) { | |
*/ | ||
@Override | ||
public void upsert(String key, FeatureRep<?> feature) { | ||
Jedis jedis = null; | ||
try { | ||
jedis = getJedis(); | ||
Gson gson = new Gson(); | ||
jedis.watch(featuresKey()); | ||
|
||
|
@@ -223,7 +249,10 @@ public void upsert(String key, FeatureRep<?> feature) { | |
} | ||
} | ||
finally { | ||
jedis.unwatch(); | ||
if (jedis != null) { | ||
jedis.unwatch(); | ||
jedis.close(); | ||
} | ||
} | ||
} | ||
|
||
|
@@ -245,26 +274,64 @@ public boolean initialized() { | |
return getInit(); | ||
} | ||
|
||
/** | ||
* Releases all resources associated with the store. The store must no longer be used once closed. | ||
* @throws IOException | ||
*/ | ||
public void close() throws IOException | ||
{ | ||
pool.destroy(); | ||
} | ||
|
||
|
||
|
||
private String featuresKey() { | ||
return prefix + ":features"; | ||
} | ||
|
||
private Boolean getInit() { | ||
return jedis.exists(featuresKey()); | ||
Jedis jedis = null; | ||
|
||
try { | ||
jedis = getJedis(); | ||
return jedis.exists(featuresKey()); | ||
} finally { | ||
if (jedis != null) { | ||
jedis.close(); | ||
} | ||
} | ||
} | ||
|
||
private FeatureRep<?> getRedis(String key) { | ||
Gson gson = new Gson(); | ||
String featureJson = jedis.hget(featuresKey(), key); | ||
Jedis jedis = null; | ||
try { | ||
jedis = getJedis(); | ||
Gson gson = new Gson(); | ||
String featureJson = getJedis().hget(featuresKey(), key); | ||
|
||
if (featureJson == null) { | ||
return null; | ||
} | ||
|
||
Type type = new TypeToken<FeatureRep<?>>() {}.getType(); | ||
FeatureRep<?> f = gson.fromJson(featureJson, type); | ||
|
||
if (featureJson == null) { | ||
return null; | ||
return f.deleted ? null : f; | ||
} finally { | ||
if (jedis != null) { | ||
jedis.close(); | ||
} | ||
} | ||
} | ||
|
||
Type type = new TypeToken<FeatureRep<?>>() {}.getType(); | ||
FeatureRep<?> f = gson.fromJson(featureJson, type); | ||
private final Jedis getJedis() { | ||
return pool.getResource(); | ||
} | ||
|
||
return f.deleted ? null : f; | ||
private final JedisPoolConfig getPoolConfig() { | ||
JedisPoolConfig config = new JedisPoolConfig(); | ||
config.setMaxTotal(256); | ||
config.setBlockWhenExhausted(false); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Due to the nature of the store abstraction, blocking when exhausted can cause a deadlock. This can happen because a single toggle call can require multiple jedis pool connections to complete-- one to check whether the store is initialized, and another to fetch the value from the store. If multiple threads attempt to do this concurrently, and the pool is near exhaustion, they could all acquire connections to check initialization, and then block waiting for another connection to fetch the store value. |
||
return config; | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not a big priority, but if we switch to java 7 or 8 this can eliminate a lot of boilerplate in this changeset:
http://docs.oracle.com/javase/7/docs/technotes/guides/language/try-with-resources.html