Skip to content
This repository has been archived by the owner on Apr 20, 2022. It is now read-only.

Commit

Permalink
PCBC-134 Improving error reporting on connection failure
Browse files Browse the repository at this point in the history
This is a backport of the master branch changes for PCBC-134 to the
1.0.x branch.  Primary changes have to do with making it such that the
OO version of the api can have getResultCode/Message() called on an
object even if the object's connection handle is not connected to the
server, and throwing warning messages a bit more comprehensively on
issues in both functional/oo approaches.  (Pure functional
couchbase_connect still returns false on connection error.)

Change-Id: I93ca6b59d72fe170a309dbe67470a7819cca3bc1
Reviewed-on: http://review.couchbase.org/21487
Reviewed-by: Matt Ingenthron <matt@couchbase.com>
Reviewed-by: Mordechai Nunberg <mnunberg@haskalah.org>
Tested-by: Mordechai Nunberg <mnunberg@haskalah.org>
  • Loading branch information
mjackson-omniti committed Oct 26, 2012
1 parent 1c1ebb5 commit 5f6204c
Show file tree
Hide file tree
Showing 3 changed files with 77 additions and 16 deletions.
82 changes: 67 additions & 15 deletions couchbase.c
Original file line number Diff line number Diff line change
Expand Up @@ -1542,11 +1542,11 @@ static void php_couchbase_create_impl(INTERNAL_FUNCTION_PARAMETERS, int oo) /* {

php_ignore_value(libcouchbase_set_error_callback(handle, php_couchbase_error_callback));

if (LIBCOUCHBASE_SUCCESS != (retval = libcouchbase_connect(handle))) {
php_couchbase_free_connparams(&cparams);
retval = libcouchbase_connect(handle);

if (LIBCOUCHBASE_SUCCESS != retval) {
php_error_docref(NULL TSRMLS_CC, E_WARNING,
"Failed to connect libcouchbase to server: %s", libcouchbase_strerror(handle, retval));
RETURN_FALSE;
}

php_ignore_value(libcouchbase_set_get_callback(handle, php_couchbase_get_callback));
Expand All @@ -1570,21 +1570,20 @@ static void php_couchbase_create_impl(INTERNAL_FUNCTION_PARAMETERS, int oo) /* {
libcouchbase_set_cookie(handle, (const void *)ctx);

/* wait for the connection established */
libcouchbase_wait(handle);
if (LIBCOUCHBASE_SUCCESS == retval) { /* earlier libcouchbase_connect's retval */
libcouchbase_wait(handle);
}

couchbase_res->seqno = 0;
if (LIBCOUCHBASE_SUCCESS != (retval = libcouchbase_get_last_error(handle))) {
php_couchbase_free_connparams(&cparams);
php_error_docref(NULL TSRMLS_CC, E_WARNING,
"Failed to connect libcouchbase to server: %s", libcouchbase_strerror(handle, retval));
libcouchbase_destroy(handle);
pefree(couchbase_res, persistent);
efree(ctx);
ctx = NULL;
RETURN_FALSE;
couchbase_res->rc = retval;
couchbase_res->is_connected = 0;
php_error(E_WARNING, "Failed to establish libcouchbase connection to server: %s", libcouchbase_strerror(handle, retval));
} else {
couchbase_res->is_connected = 1;
}

if (persistent) {
if (persistent && couchbase_res->is_connected) {
zend_rsrc_list_entry le;
Z_TYPE(le) = le_pcouchbase;
le.ptr = couchbase_res;
Expand All @@ -1602,7 +1601,12 @@ static void php_couchbase_create_impl(INTERNAL_FUNCTION_PARAMETERS, int oo) /* {
if (oo) {
zval *self = getThis();
zend_update_property(couchbase_ce, self, ZEND_STRL(COUCHBASE_PROPERTY_HANDLE), return_value TSRMLS_CC);
} else if (!couchbase_res->is_connected) { /* !oo && !connected */
php_couchbase_free_connparams(&cparams);
efree(ctx);
RETURN_FALSE;
}

php_couchbase_free_connparams(&cparams);
if (ctx != NULL) {
efree(ctx);
Expand Down Expand Up @@ -1647,6 +1651,10 @@ static void php_couchbase_get_impl(INTERNAL_FUNCTION_PARAMETERS, int multi, int
}

ZEND_FETCH_RESOURCE2(couchbase_res, php_couchbase_res *, &res, -1, PHP_COUCHBASE_RESOURCE, le_couchbase, le_pcouchbase);
if (!couchbase_res->is_connected) {
php_error(E_WARNING, "There is no active connection to couchbase.");
RETURN_FALSE;
}
if (couchbase_res->async) {
php_error_docref(NULL TSRMLS_CC, E_WARNING, "there are some results should be fetched before do any sync request");
RETURN_FALSE;
Expand Down Expand Up @@ -1734,6 +1742,10 @@ static void php_couchbase_get_impl(INTERNAL_FUNCTION_PARAMETERS, int multi, int
}

ZEND_FETCH_RESOURCE2(couchbase_res, php_couchbase_res *, &res, -1, PHP_COUCHBASE_RESOURCE, le_couchbase, le_pcouchbase);
if (!couchbase_res->is_connected) {
php_error(E_WARNING, "There is no active connection to couchbase.");
RETURN_FALSE;
}
if (couchbase_res->async) {
php_error_docref(NULL TSRMLS_CC, E_WARNING, "there are some results should be fetched before do any sync request");
RETURN_FALSE;
Expand Down Expand Up @@ -1906,12 +1918,16 @@ static void php_couchbase_get_delayed_impl(INTERNAL_FUNCTION_PARAMETERS, int oo)
char **keys;
long nkey, *klens, i;

ZEND_FETCH_RESOURCE2(couchbase_res, php_couchbase_res *, &res, -1, PHP_COUCHBASE_RESOURCE, le_couchbase, le_pcouchbase);
if (!couchbase_res->is_connected) {
php_error(E_WARNING, "There is no active connection to couchbase.");
RETURN_FALSE;
}

nkey = zend_hash_num_elements(Z_ARRVAL_P(akeys));
keys = ecalloc(nkey, sizeof(char *));
klens = ecalloc(nkey, sizeof(long));

ZEND_FETCH_RESOURCE2(couchbase_res, php_couchbase_res *, &res, -1, PHP_COUCHBASE_RESOURCE, le_couchbase, le_pcouchbase);

for(i=0, zend_hash_internal_pointer_reset(Z_ARRVAL_P(akeys));
zend_hash_has_more_elements(Z_ARRVAL_P(akeys)) == SUCCESS;
zend_hash_move_forward(Z_ARRVAL_P(akeys)), i++) {
Expand Down Expand Up @@ -2038,6 +2054,10 @@ static void php_couchbase_fetch_impl(INTERNAL_FUNCTION_PARAMETERS, int multi, in
php_couchbase_ctx *ctx;

ZEND_FETCH_RESOURCE2(couchbase_res, php_couchbase_res *, &res, -1, PHP_COUCHBASE_RESOURCE, le_couchbase, le_pcouchbase);
if (!couchbase_res->is_connected) {
php_error(E_WARNING, "There is no active connection to couchbase.");
RETURN_FALSE;
}
if (!couchbase_res->async) {
RETURN_FALSE;
}
Expand Down Expand Up @@ -2119,6 +2139,10 @@ static void php_couchbase_store_impl(INTERNAL_FUNCTION_PARAMETERS, libcouchbase_
}

ZEND_FETCH_RESOURCE2(couchbase_res, php_couchbase_res *, &res, -1, PHP_COUCHBASE_RESOURCE, le_couchbase, le_pcouchbase);
if (!couchbase_res->is_connected) {
php_error(E_WARNING, "There is no active connection to couchbase.");
RETURN_FALSE;
}
if (couchbase_res->async) {
php_error_docref(NULL TSRMLS_CC, E_WARNING, "there are some results should be fetched before do any sync request");
RETURN_FALSE;
Expand Down Expand Up @@ -2187,6 +2211,10 @@ static void php_couchbase_store_impl(INTERNAL_FUNCTION_PARAMETERS, libcouchbase_
}

ZEND_FETCH_RESOURCE2(couchbase_res, php_couchbase_res *, &res, -1, PHP_COUCHBASE_RESOURCE, le_couchbase, le_pcouchbase);
if (!couchbase_res->is_connected) {
php_error(E_WARNING, "There is no active connection to couchbase.");
RETURN_FALSE;
}
if (couchbase_res->async) {
php_error_docref(NULL TSRMLS_CC, E_WARNING, "there are some results should be fetched before do any sync request");
RETURN_FALSE;
Expand Down Expand Up @@ -2317,6 +2345,10 @@ static void php_couchbase_remove_impl(INTERNAL_FUNCTION_PARAMETERS, int oo) /* {
php_couchbase_ctx *ctx;

ZEND_FETCH_RESOURCE2(couchbase_res, php_couchbase_res *, &res, -1, PHP_COUCHBASE_RESOURCE, le_couchbase, le_pcouchbase);
if (!couchbase_res->is_connected) {
php_error(E_WARNING, "There is no active connection to couchbase.");
RETURN_FALSE;
}
if (couchbase_res->async) {
php_error_docref(NULL TSRMLS_CC, E_WARNING, "there are some results should be fetched before do any sync request");
RETURN_FALSE;
Expand Down Expand Up @@ -2379,6 +2411,10 @@ static void php_couchbase_flush_impl(INTERNAL_FUNCTION_PARAMETERS, int oo) /* {{
php_couchbase_ctx *ctx;

ZEND_FETCH_RESOURCE2(couchbase_res, php_couchbase_res *, &res, -1, PHP_COUCHBASE_RESOURCE, le_couchbase, le_pcouchbase);
if (!couchbase_res->is_connected) {
php_error(E_WARNING, "There is no active connection to couchbase.");
RETURN_FALSE;
}
if (couchbase_res->async) {
php_error_docref(NULL TSRMLS_CC, E_WARNING, "there are some results should be fetched before do any sync request");
RETURN_FALSE;
Expand Down Expand Up @@ -2452,6 +2488,10 @@ static void php_couchbase_arithmetic_impl(INTERNAL_FUNCTION_PARAMETERS, char op,
long delta = (op == '+')? offset : -offset;

ZEND_FETCH_RESOURCE2(couchbase_res, php_couchbase_res *, &res, -1, PHP_COUCHBASE_RESOURCE, le_couchbase, le_pcouchbase);
if (!couchbase_res->is_connected) {
php_error(E_WARNING, "There is no active connection to couchbase.");
RETURN_FALSE;
}
if (couchbase_res->async) {
php_error_docref(NULL TSRMLS_CC, E_WARNING, "there are some results should be fetched before do any sync request");
RETURN_FALSE;
Expand Down Expand Up @@ -2510,6 +2550,10 @@ static void php_couchbase_stats_impl(INTERNAL_FUNCTION_PARAMETERS, int oo) /* {{
php_couchbase_ctx *ctx;

ZEND_FETCH_RESOURCE2(couchbase_res, php_couchbase_res *, &res, -1, PHP_COUCHBASE_RESOURCE, le_couchbase, le_pcouchbase);
if (!couchbase_res->is_connected) {
php_error(E_WARNING, "There is no active connection to couchbase.");
RETURN_FALSE;
}
if (couchbase_res->async) {
php_error_docref(NULL TSRMLS_CC, E_WARNING, "there are some results should be fetched before do any sync request");
RETURN_FALSE;
Expand Down Expand Up @@ -2561,6 +2605,10 @@ static void php_couchbase_version_impl(INTERNAL_FUNCTION_PARAMETERS, int oo) /*
php_couchbase_ctx *ctx;

ZEND_FETCH_RESOURCE2(couchbase_res, php_couchbase_res *, &res, -1, PHP_COUCHBASE_RESOURCE, le_couchbase, le_pcouchbase);
if (!couchbase_res->is_connected) {
php_error(E_WARNING, "There is no active connection to couchbase.");
RETURN_FALSE;
}
if (couchbase_res->async) {
php_error_docref(NULL TSRMLS_CC, E_WARNING, "there are some results should be fetched before do any sync request");
RETURN_FALSE;
Expand Down Expand Up @@ -2621,6 +2669,10 @@ static void php_couchbase_cas_impl(INTERNAL_FUNCTION_PARAMETERS, int oo) /* {{{
php_couchbase_ctx *ctx;

ZEND_FETCH_RESOURCE2(couchbase_res, php_couchbase_res *, &res, -1, PHP_COUCHBASE_RESOURCE, le_couchbase, le_pcouchbase);
if (!couchbase_res->is_connected) {
php_error(E_WARNING, "There is no active connection to couchbase.");
RETURN_FALSE;
}
if (couchbase_res->async) {
php_error_docref(NULL TSRMLS_CC, E_WARNING, "there are some results should be fetched before do any sync request");
RETURN_FALSE;
Expand Down
1 change: 1 addition & 0 deletions php_couchbase.h
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,7 @@ typedef struct _php_couchbase_res {
char *prefix_key;
int prefix_key_len;
libcouchbase_error_t rc; /* returned code */
unsigned char is_connected;
} php_couchbase_res;

typedef struct _php_couchbase_ctx {
Expand Down
10 changes: 9 additions & 1 deletion tests/002.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,20 @@ $handle = couchbase_connect(COUCHBASE_CONFIG_HOST, '80', COUCHBASE_CONFIG_PASSWD

$handle = new Couchbase(COUCHBASE_CONFIG_HOST, COUCHBASE_CONFIG_USER, COUCHBASE_CONFIG_PASSWD, COUCHBASE_CONFIG_BUCKET);
print_r($handle);

$oo = new Couchbase('127.0.0.1:1','bad-user','bad-password','does-not-exist-bucket');
print($oo->getResultCode() . "\n");
print($oo->getResultMessage() . "\n");
?>
--EXPECTF--
resource(%d) of type (Couchbase)

Warning: couchbase_connect(): Failed to connect libcouchbase to server: Authentication error in %s002.php on line %d
Warning: Failed to establish libcouchbase connection to server: Authentication error in %s002.php on line %d
Couchbase Object
(
[%s] => Resource id #%d
)

Warning: Failed to establish libcouchbase connection to server: Connection failure in %s002.php on line %d
23
Connection failure

0 comments on commit 5f6204c

Please sign in to comment.