From 32eb1c5f7da6ab2008c3f0a0ccbd5bb83cea8f38 Mon Sep 17 00:00:00 2001 From: Vitaliy Stepanyuk Date: Wed, 30 Mar 2016 11:22:02 +0200 Subject: [PATCH] Reduce number of connections to one cluster node by shuffling nodes. --- cluster_library.c | 19 ++++++++++++++----- cluster_library.h | 2 ++ 2 files changed, 16 insertions(+), 5 deletions(-) diff --git a/cluster_library.c b/cluster_library.c index f996642182..e15e351431 100644 --- a/cluster_library.c +++ b/cluster_library.c @@ -856,14 +856,21 @@ cluster_init_seeds(redisCluster *cluster, HashTable *ht_seeds) { char *str, *psep, key[1024]; int key_len; zval **z_seed; + int *seeds; + size_t i, count; + + count = zend_hash_num_elements(ht_seeds); + seeds = emalloc(sizeof(int) * count); + + for (i = 0; i < count; i++) seeds[i] = i; + fyshuffle(seeds, count); // Iterate our seeds array - for(zend_hash_internal_pointer_reset(ht_seeds); - zend_hash_has_more_elements(ht_seeds)==SUCCESS; - zend_hash_move_forward(ht_seeds)) - { + for (i = 0; i < count; i++) { // Grab seed string - zend_hash_get_current_data(ht_seeds, (void**)&z_seed); + if (zend_hash_index_find(ht_seeds, seeds[i], (void**)&z_seed) != SUCCESS) { + continue; + } // Skip anything that isn't a string if(Z_TYPE_PP(z_seed)!=IS_STRING) @@ -890,6 +897,8 @@ cluster_init_seeds(redisCluster *cluster, HashTable *ht_seeds) { sizeof(RedisSock*),NULL); } + efree(seeds); + // Success if at least one seed seems valid return zend_hash_num_elements(cluster->seeds) > 0 ? 0 : -1; } diff --git a/cluster_library.h b/cluster_library.h index f82667579d..a265396ee9 100644 --- a/cluster_library.h +++ b/cluster_library.h @@ -458,6 +458,8 @@ int mbulk_resp_loop_zipdbl(RedisSock *redis_sock, zval *z_result, int mbulk_resp_loop_assoc(RedisSock *redis_sock, zval *z_result, long long count, void *ctx TSRMLS_DC); +static void fyshuffle(int *array, size_t len); + #endif /* vim: set tabstop=4 softtabstops=4 noexpandtab shiftwidth=4: */