Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Merge remote-tracking branch 'upstream/master'

  • Loading branch information...
commit 6bf39fcb4374fc5c74b867df18be346f241cdd47 2 parents b703aa0 + ec78393
@jtrosby authored
View
4 config.m4
@@ -58,7 +58,7 @@ if test "$PHP_ZOOKEEPER" != "no"; then
if test "$PHP_ZOOKEEPER_SESSION" != "no"; then
AC_MSG_RESULT([enabled])
-
+
AC_MSG_CHECKING([for session includes])
session_inc_path=""
@@ -87,7 +87,7 @@ if test "$PHP_ZOOKEEPER" != "no"; then
[
PHP_ADD_EXTENSION_DEP(zookeeper, session)
])
-
+
AC_DEFINE(HAVE_ZOOKEEPER_SESSION,1,[Whether zookeeper session handler is enabled])
SESSION_EXTRA_FILES="php_zookeeper_session.c"
else
View
34 package.xml
@@ -15,10 +15,10 @@ http://pear.php.net/dtd/package-2.0.xsd">
<email>andrei@php.net</email>
<active>yes</active>
</lead>
- <date>2009-10-15</date>
+ <date>2012-01-13</date>
<version>
- <release>0.1.0</release>
- <api>0.1.0</api>
+ <release>0.2.1</release>
+ <api>0.2.0</api>
</version>
<stability>
<release>beta</release>
@@ -26,8 +26,9 @@ http://pear.php.net/dtd/package-2.0.xsd">
</stability>
<license uri="http://www.php.net/license">PHP</license>
<notes>
-- Initial PECL release
+- Package up missed source files
</notes>
+
<contents>
<dir name="/">
<file role='doc' name='README.markdown'/>
@@ -38,6 +39,11 @@ http://pear.php.net/dtd/package-2.0.xsd">
<file role='src' name='config.m4'/>
<file role='src' name='php_zookeeper.c'/>
<file role='src' name='php_zookeeper.h'/>
+ <file role='src' name='php_zookeeper_private.h'/>
+ <file role='src' name='php_zookeeper_session.c'/>
+ <file role='src' name='php_zookeeper_session.h'/>
+ <file role='src' name='zoo_lock.c'/>
+ <file role='src' name='zoo_lock.h'/>
</dir> <!-- / -->
</contents>
<dependencies>
@@ -57,6 +63,26 @@ http://pear.php.net/dtd/package-2.0.xsd">
<changelog>
<release>
<stability><release>beta</release><api>beta</api></stability>
+ <version><release>0.2.1</release><api>0.2.0</api></version>
+ <date>2012-01-13</date>
+ <notes>
+- Package up missed source files
+ </notes>
+ </release>
+
+ <release>
+ <stability><release>beta</release><api>beta</api></stability>
+ <version><release>0.2.0</release><api>0.2.0</api></version>
+ <date>2012-01-12</date>
+ <notes>
+- Added session handler support
+- Added connect() and delete methods
+- Bug fixes
+ </notes>
+ </release>
+
+ <release>
+ <stability><release>beta</release><api>beta</api></stability>
<version><release>0.1.0</release><api>0.1.0</api></version>
<date>2009-10-15</date>
<notes>
View
39 php_zookeeper.c
@@ -255,7 +255,7 @@ static PHP_METHOD(Zookeeper, getChildren)
int i, status = ZOK;
ZK_METHOD_INIT_VARS;
- if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s|f", &path, &path_len, &fci,
+ if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s|f!", &path, &path_len, &fci,
&fcc) == FAILURE) {
return;
}
@@ -291,14 +291,15 @@ static PHP_METHOD(Zookeeper, get)
zend_fcall_info_cache fcc = empty_fcall_info_cache;
zval *stat_info = NULL;
php_cb_data_t *cb_data = NULL;
- char buffer[512];
- int buffer_len = 512;
+ char *buffer;
+ long max_size = 0;
struct Stat stat;
int status = ZOK;
+ int length;
ZK_METHOD_INIT_VARS;
- if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s|f!z", &path, &path_len, &fci,
- &fcc, &stat_info) == FAILURE) {
+ if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s|f!zl", &path, &path_len, &fci,
+ &fcc, &stat_info, &max_size) == FAILURE) {
return;
}
@@ -307,11 +308,33 @@ static PHP_METHOD(Zookeeper, get)
if (fci.size != 0) {
cb_data = php_cb_data_new(&fci, &fcc, 1 TSRMLS_CC);
}
+
+ if (max_size <= 0) {
+ status = zoo_exists(i_obj->zk, path, 1, &stat);
+
+ if (status != ZOK) {
+ php_cb_data_destroy(&cb_data);
+ php_error_docref(NULL TSRMLS_CC, E_WARNING, "error: %s", zerror(status));
+ return;
+ }
+ length = stat.dataLength;
+ } else {
+ length = max_size;
+ }
+
+ buffer = emalloc (length);
status = zoo_wget(i_obj->zk, path, (fci.size != 0) ? php_zk_watcher_marshal : NULL,
- cb_data, buffer, &buffer_len, &stat);
+ cb_data, buffer, &length, &stat);
+
if (status != ZOK) {
+ efree (buffer);
php_cb_data_destroy(&cb_data);
php_error_docref(NULL TSRMLS_CC, E_WARNING, "error: %s", zerror(status));
+
+ /* Indicate data marshalling failure with boolean false so that user can retry */
+ if (status == ZMARSHALLINGERROR) {
+ RETURN_FALSE;
+ }
return;
}
@@ -319,7 +342,7 @@ static PHP_METHOD(Zookeeper, get)
zval_dtor(stat_info);
php_stat_to_array(&stat, stat_info);
}
- RETURN_STRINGL(buffer, buffer_len, 1);
+ RETURN_STRINGL(buffer, length, 0);
}
/* }}} */
@@ -336,7 +359,7 @@ static PHP_METHOD(Zookeeper, exists)
int status = ZOK;
ZK_METHOD_INIT_VARS;
- if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s|f", &path, &path_len, &fci,
+ if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s|f!", &path, &path_len, &fci,
&fcc) == FAILURE) {
return;
}
View
4 php_zookeeper.h
@@ -14,7 +14,7 @@
+----------------------------------------------------------------------+
*/
-/* $ Id: $ */
+/* $ Id: $ */
#ifndef PHP_ZOOKEEPER_H
#define PHP_ZOOKEEPER_H
@@ -67,7 +67,7 @@ extern ps_module ps_mod_zookeeper;
PS_FUNCS(zookeeper);
-#endif /* HAVE_ZOOKEEPER_SESSION */
+#endif /* HAVE_ZOOKEEPER_SESSION */
#endif /* PHP_ZOOKEEPER_H */
View
44 php_zookeeper_session.c
@@ -40,18 +40,18 @@ ps_module ps_mod_zookeeper = {
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;
@@ -59,7 +59,7 @@ static php_zookeeper_session *php_zookeeper_session_init(char *save_path TSRMLS_
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);
@@ -90,7 +90,7 @@ static php_zookeeper_session *php_zookeeper_session_get(char *save_path TSRMLS_D
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;
@@ -98,19 +98,19 @@ static php_zookeeper_session *php_zookeeper_session_get(char *save_path TSRMLS_D
if (zend_hash_update(&EG(persistent_list), (char *)plist_key, plist_key_len, (void *)&le, sizeof(le), NULL) == FAILURE) {
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)
*/
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;
@@ -138,7 +138,7 @@ static zend_bool php_zookeeper_sess_lock(php_zookeeper_session *session, const c
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) {
@@ -181,7 +181,7 @@ PS_READ_FUNC(zookeeper)
}
path_len = snprintf(session->path, 512, "%s/%s", PHP_ZK_PARENT_NODE, key);
-
+
retry_count = 3;
do {
status = zoo_exists(session->zk, session->path, 1, &stat);
@@ -208,10 +208,10 @@ PS_READ_FUNC(zookeeper)
*vallen = 0;
return FAILURE;
}
-
+
*val = emalloc(stat.dataLength);
*vallen = stat.dataLength;
-
+
retry_count = 3;
do {
status = zoo_get(session->zk, session->path, 0, *val, vallen, &stat);
@@ -241,9 +241,9 @@ PS_WRITE_FUNC(zookeeper)
status = zoo_exists(session->zk, session->path, 1, &stat);
retry_count++;
} while (status == ZCONNECTIONLOSS && retry_count--);
-
- retry_count = 3;
- do {
+
+ retry_count = 3;
+ do {
if (status != ZOK) {
status = zoo_create(session->zk, session->path, val, vallen, &ZOO_OPEN_ACL_UNSAFE, 0, 0, 0);
} else {
@@ -277,7 +277,7 @@ PS_GC_FUNC(zookeeper)
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);
@@ -287,7 +287,7 @@ PS_GC_FUNC(zookeeper)
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) {
@@ -304,15 +304,15 @@ 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) {
+
+ 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);
View
6 php_zookeeper_session.h
@@ -27,13 +27,13 @@
typedef struct _php_zookeeper_session {
/* Connection to zookeeper */
zhandle_t *zk;
-
+
/* Lock for the session */
zkr_lock_mutex_t lock;
-
+
/* Whether the session is locked */
zend_bool is_locked;
-
+
/* Current session path */
char path[512];
} php_zookeeper_session;
View
58 zoo_lock.c
@@ -44,7 +44,7 @@
#define IF_DEBUG(x) if (logLevel==ZOO_LOG_LEVEL_DEBUG) {x;}
-
+
ZOOAPI int zkr_lock_init(zkr_lock_mutex_t* mutex, zhandle_t* zh,
char* path, struct ACL_vector *acl) {
mutex->zh = zh;
@@ -131,13 +131,13 @@ static void free_String_vector(struct String_vector *v) {
static int vstrcmp(const void* str1, const void* str2) {
const char **a = (const char**)str1;
const char **b = (const char**) str2;
- return strcmp(strrchr(*a, '-')+1, strrchr(*b, '-')+1);
-}
+ return strcmp(strrchr(*a, '-')+1, strrchr(*b, '-')+1);
+}
static void sort_children(struct String_vector *vector) {
qsort( vector->data, vector->count, sizeof(char*), &vstrcmp);
}
-
+
static char* child_floor(char **sorted_data, int len, char *element) {
char* ret = NULL;
int i =0;
@@ -151,7 +151,7 @@ static char* child_floor(char **sorted_data, int len, char *element) {
static void lock_watcher_fn(zhandle_t* zh, int type, int state,
const char* path, void *watcherCtx) {
- //callback that we registered
+ //callback that we registered
//should be called
zkr_lock_lock((zkr_lock_mutex_t*) watcherCtx);
}
@@ -161,7 +161,7 @@ static void lock_watcher_fn(zhandle_t* zh, int type, int state,
*/
static char* getName(char* str) {
char* name = strrchr(str, '/');
- if (name == NULL)
+ if (name == NULL)
return NULL;
return strdup(name + 1);
}
@@ -169,7 +169,7 @@ static char* getName(char* str) {
/**
* just a method to retry get children
*/
-static int retry_getchildren(zhandle_t *zh, char* path, struct String_vector *vector,
+static int retry_getchildren(zhandle_t *zh, char* path, struct String_vector *vector,
struct timespec *ts, int retry) {
int ret = ZCONNECTIONLOSS;
int count = 0;
@@ -185,7 +185,7 @@ static int retry_getchildren(zhandle_t *zh, char* path, struct String_vector *ve
}
/** see if our node already exists
- * if it does then we dup the name and
+ * if it does then we dup the name and
* return it
*/
static char* lookupnode(struct String_vector *vector, char *prefix) {
@@ -219,10 +219,10 @@ static int retry_zoowexists(zhandle_t *zh, char* path, watcher_fn watcher, void*
}
return ret;
}
-
+
/**
- * the main code that does the zookeeper leader
- * election. this code creates its own ephemeral
+ * the main code that does the zookeeper leader
+ * election. this code creates its own ephemeral
* node on the given path and sees if its the first
* one on the list and claims to be a leader if and only
* if its the first one of children in the paretn path
@@ -242,7 +242,7 @@ static int zkr_lock_operation(zkr_lock_mutex_t *mutex, struct timespec *ts) {
int ret = 0;
#if defined(__x86_64__)
snprintf(prefix, 30, "x-%016lx-", session);
-#else
+#else
snprintf(prefix, 30, "x-%016llx-", session);
#endif
struct String_vector vectorst;
@@ -261,10 +261,10 @@ static int zkr_lock_operation(zkr_lock_mutex_t *mutex, struct timespec *ts) {
char retbuf[len+20];
snprintf(buf, len, "%s/%s", path, prefix);
ret = ZCONNECTIONLOSS;
- ret = zoo_create(zh, buf, NULL, 0, mutex->acl,
+ ret = zoo_create(zh, buf, NULL, 0, mutex->acl,
ZOO_EPHEMERAL|ZOO_SEQUENCE, retbuf, (len+20));
-
- // do not want to retry the create since
+
+ // do not want to retry the create since
// we would end up creating more than one child
if (ret != ZOK) {
LOG_WARN(("could not create zoo node %s", buf));
@@ -272,7 +272,7 @@ static int zkr_lock_operation(zkr_lock_mutex_t *mutex, struct timespec *ts) {
}
mutex->id = getName(retbuf);
}
-
+
if (mutex->id != NULL) {
ret = ZCONNECTIONLOSS;
ret = retry_getchildren(zh, path, vector, ts, retry);
@@ -291,11 +291,11 @@ static int zkr_lock_operation(zkr_lock_mutex_t *mutex, struct timespec *ts) {
char last_child[flen];
sprintf(last_child, "%s/%s",mutex->path, lessthanme);
ret = ZCONNECTIONLOSS;
- ret = retry_zoowexists(zh, last_child, &lock_watcher_fn, mutex,
+ ret = retry_zoowexists(zh, last_child, &lock_watcher_fn, mutex,
&stat, ts, retry);
// cannot watch my predecessor i am giving up
- // we need to be able to watch the predecessor
- // since if we do not become a leader the others
+ // we need to be able to watch the predecessor
+ // since if we do not become a leader the others
// will keep waiting
if (ret != ZOK) {
free_String_vector(vector);
@@ -312,7 +312,7 @@ static int zkr_lock_operation(zkr_lock_mutex_t *mutex, struct timespec *ts) {
mutex->isOwner = 0;
}
else {
- // this is the case when we are the owner
+ // this is the case when we are the owner
// of the lock
if (strcmp(mutex->id, owner_id) == 0) {
LOG_DEBUG(("got the zoo lock owner - %s", mutex->id));
@@ -340,20 +340,20 @@ ZOOAPI int zkr_lock_lock(zkr_lock_mutex_t *mutex) {
struct timespec ts;
ts.tv_sec = 0;
ts.tv_nsec = (.5)*1000000;
- // retry to see if the path exists and
+ // retry to see if the path exists and
// and create if the path does not exist
while ((exists == ZCONNECTIONLOSS || exists == ZNONODE) && (count <4)) {
count++;
// retry the operation
- if (exists == ZCONNECTIONLOSS)
+ if (exists == ZCONNECTIONLOSS)
exists = zoo_exists(zh, path, 0, &stat);
- else if (exists == ZNONODE)
+ else if (exists == ZNONODE)
exists = zoo_create(zh, path, NULL, 0, mutex->acl, 0, NULL, 0);
nanosleep(&ts, 0);
-
+
}
- // need to check if we cannot still access the server
+ // need to check if we cannot still access the server
int check_retry = ZCONNECTIONLOSS;
count = 0;
while (check_retry != ZOK && count <4) {
@@ -367,13 +367,13 @@ ZOOAPI int zkr_lock_lock(zkr_lock_mutex_t *mutex) {
return zkr_lock_isowner(mutex);
}
-
+
ZOOAPI char* zkr_lock_getpath(zkr_lock_mutex_t *mutex) {
return mutex->path;
}
ZOOAPI int zkr_lock_isowner(zkr_lock_mutex_t *mutex) {
- return (mutex->id != NULL && mutex->ownerid != NULL
+ return (mutex->id != NULL && mutex->ownerid != NULL
&& (strcmp(mutex->id, mutex->ownerid) == 0));
}
@@ -382,14 +382,14 @@ ZOOAPI char* zkr_lock_getid(zkr_lock_mutex_t *mutex) {
}
ZOOAPI int zkr_lock_destroy(zkr_lock_mutex_t* mutex) {
- if (mutex->id)
+ if (mutex->id)
free(mutex->id);
mutex->path = NULL;
mutex->acl = NULL;
mutex->completion = NULL;
pthread_mutex_destroy(&(mutex->pmutex));
mutex->isOwner = 0;
- if (mutex->ownerid)
+ if (mutex->ownerid)
free(mutex->ownerid);
return 0;
}
View
26 zoo_lock.h
@@ -26,17 +26,17 @@ extern "C" {
#endif
/**
* \brief the call back function called on status change of lock
- *
- * the call back funtion is called with a rc of 0 if lock is acquired and
+ *
+ * the call back funtion is called with a rc of 0 if lock is acquired and
* with an rc of 1 if the lock is released
* \param rc the value to let us know if its locked or unlocked
- * \param cbdata the callback data that we passed when initializing
+ * \param cbdata the callback data that we passed when initializing
* the zookeeper lock.
*/
typedef void (* zkr_lock_completion) (int rc, void* cbdata);
-/**
+/**
* \file zoo_lock.h
* \brief zookeeper recipe for locking and leader election.
* this api implements a writelock on a given path in zookeeper.
@@ -68,7 +68,7 @@ typedef struct zkr_lock_mutex zkr_lock_mutex_t;
* \param acl the acls to use in zookeeper.
* \return return 0 if successful.
*/
-ZOOAPI int zkr_lock_init(zkr_lock_mutex_t *mutex, zhandle_t* zh,
+ZOOAPI int zkr_lock_init(zkr_lock_mutex_t *mutex, zhandle_t* zh,
char* path, struct ACL_vector *acl);
/**
@@ -77,18 +77,18 @@ ZOOAPI int zkr_lock_init(zkr_lock_mutex_t *mutex, zhandle_t* zh,
*
* this method instantiates the zookeeper mutex lock with
* a completion function.
- *
+ *
* \param mutex the mutex to initialize
* \param zh the zookeeper handle to use
* \param path the path in zookeeper to use for locking
* \param acl the acls to use in zookeeper.
- * \param completion the callback thats called when lock
+ * \param completion the callback thats called when lock
* is acquired and released.
* \param cbdata the callback method is called with data
* \return return 0 if successful.
*/
ZOOAPI int zkr_lock_init_cb(zkr_lock_mutex_t *mutex, zhandle_t* zh,
- char* path, struct ACL_vector *acl,
+ char* path, struct ACL_vector *acl,
zkr_lock_completion completion, void* cbdata);
/**
@@ -96,7 +96,7 @@ ZOOAPI int zkr_lock_init_cb(zkr_lock_mutex_t *mutex, zhandle_t* zh,
*
* this method tries locking the mutex
* \param mutex the zookeeper mutex
- * \return return 0 if there is no error. check
+ * \return return 0 if there is no error. check
* with zkr_lock_isowner() if you have the lock
*/
ZOOAPI int zkr_lock_lock(zkr_lock_mutex_t *mutex);
@@ -113,12 +113,12 @@ ZOOAPI int zkr_lock_unlock(zkr_lock_mutex_t *mutex);
/**
* \brief set the callback function for zookeeper mutex
- *
+ *
* this method sets the callback for zookeeper mutex
* \param mutex the zookeeper mutex
* \param callback the call back completion function
*/
-ZOOAPI void zkr_lock_setcallback(zkr_lock_mutex_t *mutex,
+ZOOAPI void zkr_lock_setcallback(zkr_lock_mutex_t *mutex,
zkr_lock_completion completion);
/**
@@ -131,7 +131,7 @@ ZOOAPI void zkr_lock_setcallback(zkr_lock_mutex_t *mutex,
ZOOAPI zkr_lock_completion zkr_lock_getcallback(zkr_lock_mutex_t *mutex);
/**
- * \brief destroy the mutex
+ * \brief destroy the mutex
* this method free the mutex
* \param mutex destroy the zookepeer lock.
* \return return 0 if destroyed.
@@ -156,7 +156,7 @@ ZOOAPI int zkr_lock_isowner(zkr_lock_mutex_t *mutex);
/**
* \brief return the id for this mutex
- * this mutex retunrns the id string
+ * this mutex retunrns the id string
* \param mutex the mutex
* \return the id for this mutex
*/
View
2  zookeeper-api.php
@@ -19,7 +19,7 @@ public function delete( $path, $version = -1 ) {}
public function set( $path, $data, $version = -1, &$stat = null ) {}
- public function get( $path, $watcher_cb = null, &$stat = null) {}
+ public function get( $path, $watcher_cb = null, &$stat = null, $max_size = 0) {}
public function getChildren( $path, $watcher_cb = null ) {}
Please sign in to comment.
Something went wrong with that request. Please try again.