Skip to content

Commit 8326adb

Browse files
committed
Add INI setting to choose session consistent hash (ketama or ketama_weighted)
1 parent 893d7d5 commit 8326adb

File tree

4 files changed

+31
-17
lines changed

4 files changed

+31
-17
lines changed

php_memcached.c

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -323,6 +323,20 @@ PHP_INI_MH(OnUpdateSessionPrefixString)
323323
return OnUpdateString(entry, new_value, mh_arg1, mh_arg2, mh_arg3, stage);
324324
}
325325

326+
static
327+
PHP_INI_MH(OnUpdateConsistentHash)
328+
{
329+
if (!strcmp(ZSTR_VAL(new_value), "ketama")) {
330+
MEMC_SESS_INI(consistent_hash_type) = MEMCACHED_BEHAVIOR_KETAMA;
331+
} else if (!strcmp(ZSTR_VAL(new_value), "ketama_weighted")) {
332+
MEMC_SESS_INI(consistent_hash_type) = MEMCACHED_BEHAVIOR_KETAMA_WEIGHTED;
333+
} else {
334+
return FAILURE;
335+
}
336+
337+
return OnUpdateString(entry, new_value, mh_arg1, mh_arg2, mh_arg3, stage);
338+
}
339+
326340
#define MEMC_INI_ENTRY(key, default_value, update_fn, gkey) \
327341
STD_PHP_INI_ENTRY("memcached."key, default_value, PHP_INI_ALL, update_fn, memc.gkey, zend_php_memcached_globals, php_memcached_globals)
328342

@@ -357,6 +371,7 @@ PHP_INI_BEGIN()
357371
MEMC_SESSION_INI_BOOL ("binary_protocol", "1", OnUpdateBool, binary_protocol_enabled)
358372
#endif
359373
MEMC_SESSION_INI_BOOL ("consistent_hash", "1", OnUpdateBool, consistent_hash_enabled)
374+
MEMC_SESSION_INI_BOOL ("consistent_hash_type", "ketama", OnUpdateConsistentHash, consistent_hash_type)
360375
MEMC_SESSION_INI_ENTRY("number_of_replicas", "0", OnUpdateLongGEZero, number_of_replicas)
361376
MEMC_SESSION_INI_BOOL ("randomize_replica_read", "0", OnUpdateBool, randomize_replica_read_enabled)
362377
MEMC_SESSION_INI_BOOL ("remove_failed_servers", "0", OnUpdateBool, remove_failed_servers_enabled)
@@ -4263,6 +4278,7 @@ PHP_GINIT_FUNCTION(php_memcached)
42634278
php_memcached_globals->session.lock_expiration = 30;
42644279
php_memcached_globals->session.binary_protocol_enabled = 1;
42654280
php_memcached_globals->session.consistent_hash_enabled = 1;
4281+
php_memcached_globals->session.consistent_hash_type = MEMCACHED_BEHAVIOR_KETAMA;
42664282
php_memcached_globals->session.number_of_replicas = 0;
42674283
php_memcached_globals->session.server_failure_limit = 1;
42684284
php_memcached_globals->session.randomize_replica_read_enabled = 1;

php_memcached.h

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -42,14 +42,6 @@ PHP_MEMCACHED_API zend_class_entry *php_memc_get_exception_base(int root);
4242
extern zend_module_entry memcached_module_entry;
4343
#define phpext_memcached_ptr &memcached_module_entry
4444

45-
#ifdef ZTS
46-
#define MEMC_G(v) TSRMG(php_memcached_globals_id, zend_php_memcached_globals *, memc.v)
47-
#define MEMC_SERVER_G(v) TSRMG(php_memcached_globals_id, zend_php_memcached_globals *, server.v)
48-
#else
49-
#define MEMC_G(v) (php_memcached_globals.memc.v)
50-
#define MEMC_SERVER_G(v) (php_memcached_globals.server.v)
51-
#endif
52-
5345
#endif /* PHP_MEMCACHED_H */
5446

5547
/*

php_memcached_private.h

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,19 @@ typedef unsigned long int uint32_t;
7979
# define GC_SET_REFCOUNT(p, rc) GC_REFCOUNT(p) = rc
8080
#endif
8181

82+
/* Globals accessor macros */
83+
#ifdef ZTS
84+
# define MEMC_G(v) TSRMG(php_memcached_globals_id, zend_php_memcached_globals *, memc.v)
85+
# define MEMC_SERVER_G(v) TSRMG(php_memcached_globals_id, zend_php_memcached_globals *, server.v)
86+
# define MEMC_SESS_INI(v) TSRMG(php_memcached_globals_id, zend_php_memcached_globals *, session.v)
87+
#else
88+
# define MEMC_G(v) (php_memcached_globals.memc.v)
89+
# define MEMC_SERVER_G(v) (php_memcached_globals.server.v)
90+
# define MEMC_SESS_INI(v) (php_memcached_globals.session.v)
91+
#endif
92+
93+
#define MEMC_SESS_STR_INI(vv) ((MEMC_SESS_INI(vv) && *MEMC_SESS_INI(vv)) ? MEMC_SESS_INI(vv) : NULL)
94+
8295
/****************************************
8396
Structures and definitions
8497
****************************************/
@@ -156,6 +169,7 @@ ZEND_BEGIN_MODULE_GLOBALS(php_memcached)
156169

157170
zend_bool binary_protocol_enabled;
158171
zend_bool consistent_hash_enabled;
172+
zend_bool consistent_hash_type;
159173

160174
zend_long server_failure_limit;
161175
zend_long number_of_replicas;

php_memcached_session.c

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -43,14 +43,6 @@ typedef struct {
4343
# define MAX(a,b) (((a)>(b))?(a):(b))
4444
#endif
4545

46-
#ifdef ZTS
47-
#define MEMC_SESS_INI(v) TSRMG(php_memcached_globals_id, zend_php_memcached_globals *, session.v)
48-
#else
49-
#define MEMC_SESS_INI(v) (php_memcached_globals.session.v)
50-
#endif
51-
52-
#define MEMC_SESS_STR_INI(vv) ((MEMC_SESS_INI(vv) && *MEMC_SESS_INI(vv)) ? MEMC_SESS_INI(vv) : NULL)
53-
5446
static
5547
int le_memc_sess;
5648

@@ -202,7 +194,7 @@ zend_bool s_configure_from_ini_values(memcached_st *memc, zend_bool silent)
202194
}
203195

204196
if (MEMC_SESS_INI(consistent_hash_enabled)) {
205-
check_set_behavior(MEMCACHED_BEHAVIOR_KETAMA, 1);
197+
check_set_behavior(MEMC_SESS_INI(consistent_hash_type), 1);
206198
}
207199

208200
if (MEMC_SESS_INI(server_failure_limit)) {

0 commit comments

Comments
 (0)