forked from php-zookeeper/php-zookeeper
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathphp_zookeeper_session.c
More file actions
388 lines (323 loc) · 9.57 KB
/
php_zookeeper_session.c
File metadata and controls
388 lines (323 loc) · 9.57 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
/*
+----------------------------------------------------------------------+
| Copyright (c) 2010 The PHP Group |
+----------------------------------------------------------------------+
| This source file is subject to version 3.01 of the PHP license, |
| that is bundled with this package in the file LICENSE, and is |
| available through the world-wide-web at the following url: |
| http://www.php.net/license/3_01.txt. |
| If you did not receive a copy of the PHP license and are unable to |
| obtain it through the world-wide-web, please send a note to |
| license@php.net so we can mail you a copy immediately. |
+----------------------------------------------------------------------+
| Authors: Andrei Zmievski <andrei@php.net> |
| Mikko Koppanen <mkoppanen@php.net> |
+----------------------------------------------------------------------+
*/
#include "SAPI.h"
#include "php5to7.h"
#include "php_zookeeper.h"
#include "php_zookeeper_private.h"
#include "php_zookeeper_session.h"
#ifdef HAVE_ZOOKEEPER_SESSION
#define ZK_SESS_DATA php_zookeeper_session *session = PS_GET_MOD_DATA();
#define PHP_ZK_PARENT_NODE "/php-sessid"
#define PHP_ZK_SESS_DEFAULT_LOCK_WAIT 150000
#define PHP_ZK_SESS_LOCK_EXPIRATION 30
#ifndef ZEND_ENGINE_3
typedef zend_rsrc_list_entry zend_resource;
static inline void *zend_hash_str_update_mem(HashTable *ht, const char *str, size_t len, void *pData, size_t size)
{
void *pDest;
if (zend_hash_update(ht, str, len, pData, size, &pDest) == FAILURE) {
return NULL;
}
return pDest;
}
static inline void *zend_hash_str_find_ptr(const HashTable *ht, const char *str, size_t len)
{
void *pData;
if (zend_hash_find(ht, str, len, &pData) == FAILURE) {
return NULL;
}
return pData;
}
#endif
ps_module ps_mod_zookeeper = {
PS_MOD(zookeeper)
};
/* {{{ static php_zookeeper_session *php_zookeeper_session_init(char *save_path TSRMLS_DC)
Initialize the session
*/
static php_zookeeper_session *php_zookeeper_session_init(char *save_path TSRMLS_DC)
{
struct Stat stat;
int status, recv_timeout = ZK_G(recv_timeout);
php_zookeeper_session *session;
session = pecalloc(1, sizeof(php_zookeeper_session), 1);
session->zk = zookeeper_init(save_path, NULL, recv_timeout, 0, NULL, 0);
if (!session->zk) {
efree(session);
return NULL;
}
/* Create parent node if it does not exist */
if (zoo_exists(session->zk, PHP_ZK_PARENT_NODE, 1, &stat) == ZNONODE) {
int retry_count = 3;
do {
status = zoo_create(session->zk, PHP_ZK_PARENT_NODE, 0, 0, &ZOO_OPEN_ACL_UNSAFE, 0, 0, 0);
retry_count++;
} while (status == ZCONNECTIONLOSS && retry_count--);
if (status != ZOK) {
zookeeper_close(session->zk);
efree(session);
php_error_docref(NULL TSRMLS_CC, E_ERROR, "Failed to create parent node for sessions");
}
}
return session;
}
/* }}} */
/* {{{ static php_zookeeper_session *php_zookeeper_session_get(char *save_path TSRMLS_DC)
Get a connection. If connection does not exist in persistent list allocates a new one
*/
static php_zookeeper_session *php_zookeeper_session_get(char *save_path TSRMLS_DC)
{
php_zookeeper_session *session = NULL;
char *plist_key;
int plist_key_len;
zend_resource le, *le_p = NULL;
plist_key_len = spprintf(&plist_key, 0, "zk-conn:[%s]", save_path);
#ifndef ZEND_ENGINE_3
plist_key_len += 1;
#endif
le_p = zend_hash_str_find_ptr(&EG(persistent_list), plist_key, plist_key_len);
if (le_p) {
if (le_p->type == php_zookeeper_get_connection_le()) {
efree(plist_key);
return (php_zookeeper_session *) le_p->ptr;
}
}
session = php_zookeeper_session_init(save_path TSRMLS_CC);
le.type = php_zookeeper_get_connection_le();
le.ptr = session;
if (!zend_hash_str_update_mem(&EG(persistent_list), (char *)plist_key, plist_key_len, (void *)&le, sizeof(le))) {
php_error_docref(NULL TSRMLS_CC, E_ERROR, "Could not register persistent entry for the zk handle");
}
efree(plist_key);
session->is_locked = 0;
return session;
}
/* }}} */
/* {{{ PS_OPEN_FUNC(zookeeper)
*/
PS_OPEN_FUNC(zookeeper)
{
php_zookeeper_session *session = php_zookeeper_session_get(PS(save_path) TSRMLS_CC);
if (!session) {
PS_SET_MOD_DATA(NULL);
return FAILURE;
}
PS_SET_MOD_DATA(session);
return SUCCESS;
}
/* }}} */
/* {{{ static int php_zookeeper_sess_lock(php_zookeeper_session *session, const char *key TSRMLS_DC)
Locking functionality. Adapted algo from memcached extension
*/
static zend_bool php_zookeeper_sess_lock(php_zookeeper_session *session, const char *key TSRMLS_DC)
{
char *lock_path;
int lock_path_len;
long attempts, lock_maxwait;
long lock_wait = ZK_G(sess_lock_wait);
time_t expiration;
/* lock_path if freed when the lock is freed */
lock_path_len = spprintf(&lock_path, 512 + 5, "%s/%s-lock", PHP_ZK_PARENT_NODE, key);
if (zkr_lock_init(&(session->lock), session->zk, lock_path, &ZOO_OPEN_ACL_UNSAFE) != 0) {
efree(lock_path);
return 0;
}
/* set max timeout for session_start = max_execution_time. (c) Andrei Darashenka, Richter & Poweleit GmbH */
lock_maxwait = zend_ini_long(ZEND_STRS("max_execution_time"), 0);
if (lock_maxwait <= 0) {
lock_maxwait = PHP_ZK_SESS_LOCK_EXPIRATION;
}
if (lock_wait == 0) {
lock_wait = PHP_ZK_SESS_DEFAULT_LOCK_WAIT;
}
expiration = SG(global_request_time) + lock_maxwait + 1;
attempts = lock_maxwait * 1000000 / lock_wait;
do {
if (zkr_lock_lock(&(session->lock))) {
session->is_locked = 1;
return 1;
}
if (lock_wait > 0) {
usleep(lock_wait);
}
} while(--attempts > 0);
return 0;
}
/* {{{ PS_READ_FUNC(zookeeper)
*/
PS_READ_FUNC(zookeeper)
{
#ifdef ZEND_ENGINE_3
#define SESSKEY ZSTR_VAL(key)
#else
#define SESSKEY key
#endif
ZK_SESS_DATA;
int status, path_len;
struct Stat stat;
int retry_count;
int64_t expiration_time;
char * rbuf;
int rbuf_len;
if (ZK_G(session_lock)) {
if (!php_zookeeper_sess_lock(session, SESSKEY TSRMLS_CC)) {
php_error_docref(NULL TSRMLS_CC, E_ERROR, "Failed to create session mutex");
return FAILURE;
}
}
path_len = snprintf(session->path, 512, "%s/%s", PHP_ZK_PARENT_NODE, SESSKEY);
retry_count = 3;
do {
status = zoo_exists(session->zk, session->path, 1, &stat);
retry_count++;
} while (status == ZCONNECTIONLOSS && retry_count--);
if (status == ZNONODE) {
goto error;
} else if (status != ZOK) {
return FAILURE;
}
expiration_time = (int64_t) (SG(global_request_time) - PS(gc_maxlifetime)) * 1000;
/* The session has expired */
if (stat.mtime < expiration_time) {
retry_count = 3;
do {
status = zoo_delete(session->zk, session->path, -1);
retry_count++;
} while (status == ZCONNECTIONLOSS && retry_count--);
goto error;
}
rbuf = emalloc(stat.dataLength);
rbuf_len = stat.dataLength;
retry_count = 3;
do {
status = zoo_get(session->zk, session->path, 0, rbuf, &rbuf_len, &stat);
retry_count++;
} while (status == ZCONNECTIONLOSS && retry_count--);
if (status != ZOK) {
efree(rbuf);
return FAILURE;
}
#ifdef ZEND_ENGINE_3
*val = zend_string_init(rbuf, rbuf_len, 0);
efree(rbuf);
#else
*val = rbuf;
*vallen = rbuf_len;
#endif
return SUCCESS;
error:
#ifdef ZEND_ENGINE_3
*val = ZSTR_EMPTY_ALLOC();
#else
*val = NULL;
*vallen = 0;
#endif
return SUCCESS; //FAILURE;
#undef SESSKEY
}
/* }}} */
/* {{{ PS_WRITE_FUNC(zookeeper)
*/
PS_WRITE_FUNC(zookeeper)
{
#ifdef ZEND_ENGINE_3
#define WBUF ZSTR_VAL(val)
#define WBUFLEN ZSTR_LEN(val)
#else
#define WBUF val
#define WBUFLEN vallen
#endif
ZK_SESS_DATA;
int status, retry_count;
struct Stat stat;
retry_count = 3;
do {
status = zoo_exists(session->zk, session->path, 1, &stat);
retry_count++;
} while (status == ZCONNECTIONLOSS && retry_count--);
retry_count = 3;
do {
if (status != ZOK) {
status = zoo_create(session->zk, session->path, WBUF, WBUFLEN, &ZOO_OPEN_ACL_UNSAFE, 0, 0, 0);
} else {
status = zoo_set(session->zk, session->path, WBUF, WBUFLEN, -1);
}
retry_count++;
} while (status == ZCONNECTIONLOSS && retry_count--);
return (status == ZOK) ? SUCCESS : FAILURE;
#undef WBUF
#undef WBUFLEN
}
/* }}} */
PS_DESTROY_FUNC(zookeeper)
{
ZK_SESS_DATA;
int status, retry_count;
retry_count = 3;
do {
status = zoo_delete(session->zk, session->path, -1);
retry_count++;
} while (status == ZCONNECTIONLOSS && retry_count--);
return (status == ZOK) ? SUCCESS : FAILURE;
}
PS_GC_FUNC(zookeeper)
{
struct Stat stat;
struct String_vector nodes;
int i, status;
int64_t expiration_time;
ZK_SESS_DATA;
expiration_time = (int64_t) (SG(global_request_time) - PS(gc_maxlifetime)) * 1000;
status = zoo_get_children(session->zk, PHP_ZK_PARENT_NODE, 0, &nodes);
if (status == ZOK) {
for (i = 0; i < nodes.count; i++) {
char path[512];
int path_len;
path_len = snprintf(path, 512, "%s/%s", PHP_ZK_PARENT_NODE, nodes.data[i]);
if (zoo_exists(session->zk, path, 1, &stat) == ZOK) {
/* TODO: should lock here? */
if (stat.mtime < expiration_time) {
(void) zoo_delete(session->zk, path, -1);
}
}
}
}
return SUCCESS;
}
static void php_zk_sync_completion(int rc, const char *value, const void *data) {}
PS_CLOSE_FUNC(zookeeper)
{
ZK_SESS_DATA;
if (session->is_locked) {
(void) zkr_lock_unlock(&(session->lock));
efree(session->lock.path);
zkr_lock_destroy(&(session->lock));
session->is_locked = 0;
}
/* TODO: is this needed? */
// zoo_async(session->zk, session->path, php_zk_sync_completion, (const void *) session);
PS_SET_MOD_DATA(NULL);
return SUCCESS;
}
#endif /* HAVE_ZOOKEEPER_SESSION */
/*
* Local variables:
* tab-width: 4
* c-basic-offset: 4
* End:
* vim: noet sw=4 ts=4 fdm=marker:
*/