Skip to content

Commit 6dc3ad6

Browse files
committed
Fix RedisArray::_rehash to support closures
* Add "f" parameter in _rehash() * Call object with new method * Add rehash test with closure
1 parent 5855cfc commit 6dc3ad6

5 files changed

+47
-20
lines changed

arrays.markdown

+1-1
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ For instance, the keys “{user:1}:name” and “{user:1}:email” will be stor
7373

7474
## Migrating keys
7575

76-
When a node is added or removed from a ring, RedisArray instances must be instanciated with a “previous” list of nodes. A single call to `$ra->_rehash()` causes all the keys to be redistributed according to the new list of nodes. Passing a callback function to `_rehash()` makes it possible to track the progress of that operation: the function is called with a node name and a number of keys that will be examined.
76+
When a node is added or removed from a ring, RedisArray instances must be instanciated with a “previous” list of nodes. A single call to `$ra->_rehash()` causes all the keys to be redistributed according to the new list of nodes. Passing a callback function to `_rehash()` makes it possible to track the progress of that operation: the function is called with a node name and a number of keys that will be examined, e.g. `_rehash(function ($host, $count){ ... });`.
7777

7878
It is possible to automate this process, by setting `'autorehash' => TRUE` in the constructor options. This will cause keys to be migrated when they need to be read from the previous array.
7979

redis_array.c

+10-4
Original file line numberDiff line numberDiff line change
@@ -499,19 +499,25 @@ PHP_METHOD(RedisArray, _distributor)
499499

500500
PHP_METHOD(RedisArray, _rehash)
501501
{
502-
zval *object, *z_cb = NULL;
502+
zval *object;
503503
RedisArray *ra;
504+
zend_fcall_info z_cb;
505+
zend_fcall_info_cache z_cb_cache;
504506

505-
if (zend_parse_method_parameters(ZEND_NUM_ARGS() TSRMLS_CC, getThis(), "O|z",
506-
&object, redis_array_ce, &z_cb) == FAILURE) {
507+
if (zend_parse_method_parameters(ZEND_NUM_ARGS() TSRMLS_CC, getThis(), "O|f",
508+
&object, redis_array_ce, &z_cb, &z_cb_cache) == FAILURE) {
507509
RETURN_FALSE;
508510
}
509511

510512
if (redis_array_get(object, &ra TSRMLS_CC) < 0) {
511513
RETURN_FALSE;
512514
}
513515

514-
ra_rehash(ra, z_cb TSRMLS_CC);
516+
if (ZEND_NUM_ARGS() == 0) {
517+
ra_rehash(ra, NULL, NULL TSRMLS_CC);
518+
} else {
519+
ra_rehash(ra, &z_cb, &z_cb_cache TSRMLS_CC);
520+
}
515521
}
516522

517523
static void multihost_distribute(INTERNAL_FUNCTION_PARAMETERS, const char *method_name)

redis_array_impl.c

+27-14
Original file line numberDiff line numberDiff line change
@@ -1105,24 +1105,37 @@ ra_move_key(const char *key, int key_len, zval *z_from, zval *z_to TSRMLS_DC) {
11051105
}
11061106

11071107
/* callback with the current progress, with hostname and count */
1108-
static void zval_rehash_callback(zval *z_cb, const char *hostname, long count TSRMLS_DC) {
1108+
static void zval_rehash_callback(zend_fcall_info *z_cb, zend_fcall_info_cache *z_cb_cache,
1109+
const char *hostname, long count TSRMLS_DC) {
11091110

1110-
zval z_ret, *z_args[2];
1111+
zval *z_ret = NULL, **z_args[2];
1112+
zval *z_host, *z_count;
1113+
1114+
z_cb->retval_ptr_ptr = &z_ret;
1115+
z_cb->params = &z_args;
1116+
z_cb->param_count = 2;
1117+
z_cb->no_separation = 0;
11111118

11121119
/* run cb(hostname, count) */
1113-
MAKE_STD_ZVAL(z_args[0]);
1114-
ZVAL_STRING(z_args[0], hostname, 0);
1115-
MAKE_STD_ZVAL(z_args[1]);
1116-
ZVAL_LONG(z_args[1], count);
1117-
call_user_function(EG(function_table), NULL, z_cb, &z_ret, 2, z_args TSRMLS_CC);
1120+
MAKE_STD_ZVAL(z_host);
1121+
ZVAL_STRING(z_host, hostname, 0);
1122+
z_args[0] = &z_host;
1123+
MAKE_STD_ZVAL(z_count);
1124+
ZVAL_LONG(z_count, count);
1125+
z_args[1] = &z_count;
1126+
1127+
zend_call_function(z_cb, z_cb_cache TSRMLS_CC);
11181128

11191129
/* cleanup */
1120-
efree(z_args[0]);
1121-
efree(z_args[1]);
1130+
efree(z_host);
1131+
efree(z_count);
1132+
if(z_ret)
1133+
efree(z_ret);
11221134
}
11231135

11241136
static void
1125-
ra_rehash_server(RedisArray *ra, zval *z_redis, const char *hostname, zend_bool b_index, zval *z_cb TSRMLS_DC) {
1137+
ra_rehash_server(RedisArray *ra, zval *z_redis, const char *hostname, zend_bool b_index,
1138+
zend_fcall_info *z_cb, zend_fcall_info_cache *z_cb_cache TSRMLS_DC) {
11261139

11271140
char **keys;
11281141
int *key_lens;
@@ -1138,8 +1151,8 @@ ra_rehash_server(RedisArray *ra, zval *z_redis, const char *hostname, zend_bool
11381151
}
11391152

11401153
/* callback */
1141-
if(z_cb) {
1142-
zval_rehash_callback(z_cb, hostname, count TSRMLS_CC);
1154+
if(z_cb && z_cb_cache) {
1155+
zval_rehash_callback(z_cb, z_cb_cache, hostname, count TSRMLS_CC);
11431156
}
11441157

11451158
/* for each key, redistribute */
@@ -1163,7 +1176,7 @@ ra_rehash_server(RedisArray *ra, zval *z_redis, const char *hostname, zend_bool
11631176
}
11641177

11651178
void
1166-
ra_rehash(RedisArray *ra, zval *z_cb TSRMLS_DC) {
1179+
ra_rehash(RedisArray *ra, zend_fcall_info *z_cb, zend_fcall_info_cache *z_cb_cache TSRMLS_DC) {
11671180

11681181
int i;
11691182

@@ -1172,7 +1185,7 @@ ra_rehash(RedisArray *ra, zval *z_cb TSRMLS_DC) {
11721185
return; /* TODO: compare the two rings for equality */
11731186

11741187
for(i = 0; i < ra->prev->count; ++i) {
1175-
ra_rehash_server(ra, ra->prev->redis[i], ra->prev->hosts[i], ra->index, z_cb TSRMLS_CC);
1188+
ra_rehash_server(ra, ra->prev->redis[i], ra->prev->hosts[i], ra->index, z_cb, z_cb_cache TSRMLS_CC);
11761189
}
11771190
}
11781191

redis_array_impl.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,6 @@ void ra_index_discard(zval *z_redis, zval *return_value TSRMLS_DC);
2424
void ra_index_unwatch(zval *z_redis, zval *return_value TSRMLS_DC);
2525
zend_bool ra_is_write_cmd(RedisArray *ra, const char *cmd, int cmd_len);
2626

27-
void ra_rehash(RedisArray *ra, zval *z_cb TSRMLS_DC);
27+
void ra_rehash(RedisArray *ra, zend_fcall_info *z_cb, zend_fcall_info_cache *z_cb_cache TSRMLS_DC);
2828

2929
#endif

tests/array-tests.php

+8
Original file line numberDiff line numberDiff line change
@@ -289,6 +289,14 @@ public function testRehash() {
289289
$this->ra->_rehash(); // this will redistribute the keys
290290
}
291291

292+
public function testRehashWithCallback() {
293+
$total = 0;
294+
$this->ra->_rehash(function ($host, $count) use (&$total) {
295+
$total += $count;
296+
});
297+
$this->assertTrue($total > 0);
298+
}
299+
292300
public function testReadRedistributedKeys() {
293301
$this->readAllvalues(); // we shouldn't have any missed reads now.
294302
}

0 commit comments

Comments
 (0)