Skip to content

Commit 68abdf4

Browse files
committed
Auto rehash keys on read miss + config.ini
1 parent 2b9a7d9 commit 68abdf4

File tree

5 files changed

+27
-6
lines changed

5 files changed

+27
-6
lines changed

redis.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ PHP_INI_BEGIN()
6363
PHP_INI_ENTRY("redis.arrays.previous", "", PHP_INI_ALL, NULL)
6464
PHP_INI_ENTRY("redis.arrays.functions", "", PHP_INI_ALL, NULL)
6565
PHP_INI_ENTRY("redis.arrays.index", "", PHP_INI_ALL, NULL)
66+
PHP_INI_ENTRY("redis.arrays.autorehash", "", PHP_INI_ALL, NULL)
6667
PHP_INI_END()
6768

6869
ZEND_DECLARE_MODULE_GLOBALS(redis)

redis_array.c

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -229,7 +229,7 @@ PHP_METHOD(RedisArray, __construct)
229229
}
230230

231231
static void
232-
ra_forward_call(INTERNAL_FUNCTION_PARAMETERS, RedisArray *ra, const char *cmd, int cmd_len, zval *z_args) {
232+
ra_forward_call(INTERNAL_FUNCTION_PARAMETERS, RedisArray *ra, const char *cmd, int cmd_len, zval *z_args, zval *z_new_target) {
233233

234234
zval **zp_tmp, z_tmp;
235235
char *key;
@@ -301,8 +301,12 @@ ra_forward_call(INTERNAL_FUNCTION_PARAMETERS, RedisArray *ra, const char *cmd, i
301301

302302
// check if we have an error.
303303
if(failed && ra->prev && !b_write_cmd) { // there was an error reading, try with prev ring.
304-
/* php_printf("ERROR, FALLBACK TO PREVIOUS RING.\n"); */
305-
ra_forward_call(INTERNAL_FUNCTION_PARAM_PASSTHRU, ra->prev, cmd, cmd_len, z_args);
304+
/* ERROR, FALLBACK TO PREVIOUS RING and forward a reference to the first redis instance we were looking at. */
305+
ra_forward_call(INTERNAL_FUNCTION_PARAM_PASSTHRU, ra->prev, cmd, cmd_len, z_args, z_new_target?z_new_target:redis_inst);
306+
}
307+
308+
if(!failed && !b_write_cmd && z_new_target && ra->auto_rehash) { /* move key from old ring to new ring */
309+
ra_move_key(key, key_len, redis_inst, z_new_target TSRMLS_CC);
306310
}
307311
}
308312

@@ -328,7 +332,7 @@ PHP_METHOD(RedisArray, __call)
328332
RETURN_FALSE;
329333
}
330334

331-
ra_forward_call(INTERNAL_FUNCTION_PARAM_PASSTHRU, ra, cmd, cmd_len, z_args);
335+
ra_forward_call(INTERNAL_FUNCTION_PARAM_PASSTHRU, ra, cmd, cmd_len, z_args, NULL);
332336
}
333337

334338
PHP_METHOD(RedisArray, _hosts)

redis_array.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ typedef struct RedisArray_ {
3232
zval *z_fun; /* key extractor */
3333
zval *z_pure_cmds; /* hash table */
3434

35+
int auto_rehash; /* migrate keys on read operations */
3536
struct RedisArray_ *prev;
3637
} RedisArray;
3738

redis_array_impl.c

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -152,9 +152,10 @@ RedisArray *ra_load_array(const char *name TSRMLS_DC) {
152152
zval *z_params_prev, **z_prev;
153153
zval *z_params_funs, **z_data_pp, *z_fun = NULL;
154154
zval *z_params_index;
155+
zval *z_params_autorehash;
155156
RedisArray *ra = NULL;
156157

157-
zend_bool b_index = 0;
158+
zend_bool b_index = 0, b_autorehash = 0;
158159
HashTable *hHosts = NULL, *hPrev = NULL;
159160

160161
/* find entry */
@@ -197,8 +198,19 @@ RedisArray *ra_load_array(const char *name TSRMLS_DC) {
197198
}
198199
}
199200

201+
/* find autorehash option */
202+
MAKE_STD_ZVAL(z_params_autorehash);
203+
array_init(z_params_autorehash);
204+
sapi_module.treat_data(PARSE_STRING, estrdup(INI_STR("redis.arrays.autorehash")), z_params_autorehash TSRMLS_CC);
205+
if (zend_hash_find(Z_ARRVAL_P(z_params_autorehash), name, strlen(name) + 1, (void **) &z_data_pp) != FAILURE) {
206+
if(Z_TYPE_PP(z_data_pp) == IS_STRING && strncmp(Z_STRVAL_PP(z_data_pp), "1", 1) == 0) {
207+
b_autorehash = 1;
208+
}
209+
}
210+
200211
/* create RedisArray object */
201212
ra = ra_make_array(hHosts, z_fun, hPrev, b_index TSRMLS_CC);
213+
ra->auto_rehash = b_autorehash;
202214

203215
/* cleanup */
204216
zval_dtor(z_params_hosts);
@@ -209,6 +221,8 @@ RedisArray *ra_load_array(const char *name TSRMLS_DC) {
209221
efree(z_params_funs);
210222
zval_dtor(z_params_index);
211223
efree(z_params_index);
224+
zval_dtor(z_params_autorehash);
225+
efree(z_params_autorehash);
212226

213227
return ra;
214228
}
@@ -760,7 +774,7 @@ ra_move_list(const char *key, int key_len, zval *z_from, zval *z_to TSRMLS_DC) {
760774
return ra_move_collection(key, key_len, z_from, z_to, 3, cmd_list, 1, cmd_add TSRMLS_CC);
761775
}
762776

763-
static void
777+
void
764778
ra_move_key(const char *key, int key_len, zval *z_from, zval *z_to TSRMLS_DC) {
765779

766780
long type = ra_get_key_type(z_from, key, key_len TSRMLS_CC);

redis_array_impl.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ RedisArray *ra_make_array(HashTable *hosts, zval *z_fun, HashTable *hosts_prev,
1111
zval *ra_find_node(RedisArray *ra, const char *key, int key_len, int *out_pos TSRMLS_DC);
1212
void ra_init_function_table(RedisArray *ra);
1313

14+
void ra_move_key(const char *key, int key_len, zval *z_from, zval *z_to TSRMLS_DC);
1415
char * ra_find_key(RedisArray *ra, zval *z_args, const char *cmd, int *key_len);
1516
void ra_index_multi(zval *z_redis TSRMLS_DC);
1617

0 commit comments

Comments
 (0)