Skip to content

Dynamic sharding implementation #174

Closed
wants to merge 10 commits into from

5 participants

@yaourt
yaourt commented Jun 29, 2011

Please could you review the following proposal and tell me what do you think about it ?

Currently, Jedis doesn't support dynamic sharding.

If we want to add / remove a Redis server from the available shards, then the only way to achieve this is to stop the application, change the sharding configuration and then restart it.

This implementation allows various things like :

  • to add / remove some servers without downtime for the application
  • to externalize the Redis connectivity monitoring and adapt the sharding configuration in pseudo-realtime
  • to centralize the sharding configuration for a cluster in a DB or something similar
  • etc

This proposal delegates the sharding configuration to a component outside of Jedis and allow to dynamically update the sharding configuration without down time.

JedisDynamicShardsProvider class is doing the bridge between this component and Jedis internals.

The external component manages the sharding configuration the way it wants and when an update is required, it only has to call the relevant method add/removeShard( ) or setShards( ) on JedisDynamicShardsProvider.

Upon such a call, JedisDynamicShardsProvider then notify (when absolutely required) Sharded instances about such a change (inspired from the Observable / Observer pattern)

When Sharded handles such a notification, then all the read access on the shard configuration are blocked until it has finished to change the sharding configuration (ReentrantReadWriteLock usage).

yaourt added some commits Jun 28, 2011
@yaourt yaourt Introduce DynamicShardProvider. 68b9795
@yaourt yaourt DynamicShardProvider with R/W locking mechanism 7a4b47b
@yaourt yaourt Push new constructors in BinaryShardedJedis & ShardedJedis that accep…
…t a JedisDynamicShardsProvider instead of a static shards list
8b790fc
@yaourt yaourt Various fixes and integration tests for dynamic shards updates :
- Use ReentrantReadWriteLock in AbstractDynamicShardsProvider
- Fix read/write locks in Sharded
- Add integration tests with concurrency
2d87bd4
@ib84
ib84 commented Jun 30, 2011

nice!

@ib84
ib84 commented Sep 9, 2011

Hi yaourt. Did you test this already?

@yaourt
yaourt commented Sep 9, 2011

Mmmmh, to be honest, I added several tests for this but I don't remember if I ran all the Jedis test suite ...

So please, gimme some time to confirm, so you will not loose your time ...

@ib84
ib84 commented Sep 9, 2011

yes that would be a big help thanks. I'd like your dynamic sharding!

@yaourt
yaourt commented Sep 9, 2011

I've your answer :

Results :

Tests run: 215, Failures: 0, Errors: 0, Skipped: 0

[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------

@ib84
ib84 commented Sep 9, 2011

nice! I assume it also passed your own custom tests for add/removing shards. forwarding to jonathan

@yaourt
yaourt commented Sep 9, 2011

Real life sample usage code .... (DAO impl NOT provided)

https://gist.github.com/1206366

@ib84
ib84 commented Sep 9, 2011

ah ok thanks, I just wrote you a personal mail asking just those two things. Have a read anyway for another thing

@yaourt
yaourt commented Sep 16, 2011

Up ;-)

Getting started sample available here : https://gist.github.com/1211016

@goldfishy

Will this be merged into Jedis? :)

@allen-servedio allen-servedio commented on the diff Jun 19, 2014
src/main/java/redis/clients/util/Sharded.java
- nodes = new TreeMap<Long, S>();
-
- for (int i = 0; i != shards.size(); ++i) {
- final S shardInfo = shards.get(i);
- if (shardInfo.getName() == null)
- for (int n = 0; n < 160 * shardInfo.getWeight(); n++) {
- nodes.put(this.algo.hash("SHARD-" + i + "-NODE-" + n), shardInfo);
- }
- else
- for (int n = 0; n < 160 * shardInfo.getWeight(); n++) {
- nodes.put(this.algo.hash(shardInfo.getName() + "*" + shardInfo.getWeight() + n), shardInfo);
- }
- resources.put(shardInfo, shardInfo.createResource());
- }
+ nodes.clear();
+ resources.clear();

Isn't this leaking resources? Don't you have to close them all rather than just clearing references to them here?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
@itang itang referenced this pull request in itang/todo.itang.me Aug 19, 2014
Open

Redis, SSDB、Hazelcast 熟悉 [进行中] #48

0 of 12 tasks complete
@HeartSaVioR
Collaborator

We agree that we don't treat ShardedJedis as core of Jedis, and for now we don't improve it.
If you think that we really need this feature, please leave an issue and link this so that we don't forget it.
Closing.

@afredlyj afredlyj referenced this pull request in afredlyj/mynote Jan 1, 2016
Open

零散知识收集 #1

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.