Skip to content

Commit 1d11421

Browse files
author
Emmanuel Merali
committed
Added more thogrough error logging
Added more thogrough error logging before throwing exceptions
1 parent 282e9de commit 1d11421

File tree

5 files changed

+42
-32
lines changed

5 files changed

+42
-32
lines changed

config.h

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -37,10 +37,6 @@
3737
/* Define to 1 if you have the <unistd.h> header file. */
3838
#define HAVE_UNISTD_H 1
3939

40-
/* Define to the sub-directory in which libtool stores uninstalled libraries.
41-
*/
42-
#define LT_OBJDIR ".libs/"
43-
4440
/* Define to 1 if your C compiler doesn't accept -c and -o together. */
4541
/* #undef NO_MINUS_C_MINUS_O */
4642

library.c

Lines changed: 37 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,19 @@ PHPAPI void redis_stream_close(RedisSock *redis_sock TSRMLS_DC) {
3333
}
3434
}
3535

36+
PHPAPI void redis_throw_exception(zend_class_entry *exception_ce, const char *file, int line, const char *cmd, const char *cause, RedisSock *redis_sock TSRMLS_DC)
37+
{
38+
char message[1024];
39+
if (!cmd) {
40+
sprintf(message, "%s in %s, line %d (%s:%d)", cause, file, line, redis_sock->host, redis_sock->port);
41+
}
42+
else {
43+
sprintf(message, "%s in %s, line %d, command: %s (%s:%d)", cause, file, line, cmd, redis_sock->host, redis_sock->port);
44+
}
45+
php_log_err(message);
46+
zend_throw_exception(redis_exception_ce, message, 0 TSRMLS_CC);
47+
}
48+
3649
PHPAPI int redis_check_eof(RedisSock *redis_sock TSRMLS_DC)
3750
{
3851
int eof;
@@ -41,32 +54,31 @@ PHPAPI int redis_check_eof(RedisSock *redis_sock TSRMLS_DC)
4154
if (!redis_sock->stream) {
4255
return -1;
4356
}
44-
4557
eof = php_stream_eof(redis_sock->stream);
4658
for (; eof; count++) {
4759
if((MULTI == redis_sock->mode) || redis_sock->watching || count == 10) { /* too many failures */
48-
if(redis_sock->stream) { /* close stream if still here */
60+
if(redis_sock->stream) { /* close stream if still here */
4961
redis_stream_close(redis_sock TSRMLS_CC);
5062
redis_sock->stream = NULL;
5163
redis_sock->mode = ATOMIC;
5264
redis_sock->status = REDIS_SOCK_STATUS_FAILED;
5365
redis_sock->watching = 0;
66+
}
67+
redis_throw_exception(redis_exception_ce, __FILE__, __LINE__, NULL, "Connection lost", redis_sock TSRMLS_CC);
68+
return -1;
5469
}
55-
zend_throw_exception(redis_exception_ce, "Connection lost", 0 TSRMLS_CC);
56-
return -1;
57-
}
58-
if(redis_sock->stream) { /* close existing stream before reconnecting */
70+
if (redis_sock->stream) { /* close existing stream before reconnecting */
5971
redis_stream_close(redis_sock TSRMLS_CC);
6072
redis_sock->stream = NULL;
6173
redis_sock->mode = ATOMIC;
6274
redis_sock->watching = 0;
63-
}
64-
// Wait for a while before trying to reconnect
65-
if (redis_sock->retry_interval) {
66-
// Random factor to avoid having several (or many) concurrent connections trying to reconnect at the same time
67-
long retry_interval = (count ? redis_sock->retry_interval : (random() % redis_sock->retry_interval));
68-
usleep(retry_interval);
69-
}
75+
}
76+
// Wait for a while before trying to reconnect
77+
if (redis_sock->retry_interval) {
78+
// Random factor to avoid having several (or many) concurrent connections trying to reconnect at the same time
79+
long retry_interval = (count ? redis_sock->retry_interval : (random() % redis_sock->retry_interval));
80+
usleep(retry_interval);
81+
}
7082
redis_sock_connect(redis_sock TSRMLS_CC); /* reconnect */
7183
if(redis_sock->stream) { /* check for EOF again. */
7284
eof = php_stream_eof(redis_sock->stream);
@@ -115,7 +127,7 @@ PHPAPI zval *redis_sock_read_multibulk_reply_zval(INTERNAL_FUNCTION_PARAMETERS,
115127
redis_sock->status = REDIS_SOCK_STATUS_FAILED;
116128
redis_sock->mode = ATOMIC;
117129
redis_sock->watching = 0;
118-
zend_throw_exception(redis_exception_ce, "read error on connection", 0 TSRMLS_CC);
130+
redis_throw_exception(redis_exception_ce, __FILE__, __LINE__, NULL, "read error on connection", redis_sock TSRMLS_CC);
119131
return NULL;
120132
}
121133

@@ -158,7 +170,7 @@ PHPAPI char *redis_sock_read_bulk_reply(RedisSock *redis_sock, int bytes TSRMLS_
158170
got = php_stream_read(redis_sock->stream, reply + offset, bytes-offset);
159171
if (got <= 0) {
160172
/* Error or EOF */
161-
zend_throw_exception(redis_exception_ce, "socket error on read socket", 0 TSRMLS_CC);
173+
redis_throw_exception(redis_exception_ce, __FILE__, __LINE__, NULL, "socket error on read socket", redis_sock TSRMLS_CC);
162174
break;
163175
}
164176
offset += got;
@@ -191,7 +203,7 @@ PHPAPI char *redis_sock_read(RedisSock *redis_sock, int *buf_len TSRMLS_DC)
191203
redis_sock->status = REDIS_SOCK_STATUS_FAILED;
192204
redis_sock->mode = ATOMIC;
193205
redis_sock->watching = 0;
194-
zend_throw_exception(redis_exception_ce, "read error on connection", 0 TSRMLS_CC);
206+
redis_throw_exception(redis_exception_ce, __FILE__, __LINE__, NULL, "read error on connection", redis_sock TSRMLS_CC);
195207
return NULL;
196208
}
197209

@@ -201,7 +213,7 @@ PHPAPI char *redis_sock_read(RedisSock *redis_sock, int *buf_len TSRMLS_DC)
201213
redis_sock_set_err(redis_sock, inbuf+1, err_len);
202214
/* stale data */
203215
if(memcmp(inbuf + 1, "-ERR SYNC ", 10) == 0) {
204-
zend_throw_exception(redis_exception_ce, "SYNC with master in progress", 0 TSRMLS_CC);
216+
redis_throw_exception(redis_exception_ce, __FILE__, __LINE__, NULL, "SYNC with master in progress", redis_sock TSRMLS_CC);
205217
}
206218
return NULL;
207219

@@ -895,7 +907,7 @@ PHPAPI int redis_sock_read_multibulk_reply_zipped_with_flag(INTERNAL_FUNCTION_PA
895907
redis_sock->status = REDIS_SOCK_STATUS_FAILED;
896908
redis_sock->mode = ATOMIC;
897909
redis_sock->watching = 0;
898-
zend_throw_exception(redis_exception_ce, "read error on connection", 0 TSRMLS_CC);
910+
redis_throw_exception(redis_exception_ce, __FILE__, __LINE__, NULL, "read error on connection", redis_sock TSRMLS_CC);
899911
return -1;
900912
}
901913

@@ -1271,7 +1283,7 @@ PHPAPI int redis_sock_read_multibulk_reply(INTERNAL_FUNCTION_PARAMETERS, RedisSo
12711283
redis_sock->status = REDIS_SOCK_STATUS_FAILED;
12721284
redis_sock->mode = ATOMIC;
12731285
redis_sock->watching = 0;
1274-
zend_throw_exception(redis_exception_ce, "read error on connection", 0 TSRMLS_CC);
1286+
redis_throw_exception(redis_exception_ce, __FILE__, __LINE__, NULL, "read error on connection", redis_sock TSRMLS_CC);
12751287
return -1;
12761288
}
12771289

@@ -1318,7 +1330,7 @@ PHPAPI int redis_sock_read_multibulk_reply_raw(INTERNAL_FUNCTION_PARAMETERS, Red
13181330
redis_sock->status = REDIS_SOCK_STATUS_FAILED;
13191331
redis_sock->mode = ATOMIC;
13201332
redis_sock->watching = 0;
1321-
zend_throw_exception(redis_exception_ce, "read error on connection", 0 TSRMLS_CC);
1333+
redis_throw_exception(redis_exception_ce, __FILE__, __LINE__, NULL, "read error on connection", redis_sock TSRMLS_CC);
13221334
return -1;
13231335
}
13241336

@@ -1397,7 +1409,7 @@ PHPAPI int redis_sock_read_multibulk_reply_assoc(INTERNAL_FUNCTION_PARAMETERS, R
13971409
redis_sock->status = REDIS_SOCK_STATUS_FAILED;
13981410
redis_sock->mode = ATOMIC;
13991411
redis_sock->watching = 0;
1400-
zend_throw_exception(redis_exception_ce, "read error on connection", 0 TSRMLS_CC);
1412+
redis_throw_exception(redis_exception_ce, __FILE__, __LINE__, NULL, "read error on connection", redis_sock TSRMLS_CC);
14011413
return -1;
14021414
}
14031415

@@ -1449,7 +1461,7 @@ PHPAPI int redis_sock_read_multibulk_reply_assoc(INTERNAL_FUNCTION_PARAMETERS, R
14491461
PHPAPI int redis_sock_write(RedisSock *redis_sock, char *cmd, size_t sz TSRMLS_DC)
14501462
{
14511463
if(redis_sock && redis_sock->status == REDIS_SOCK_STATUS_DISCONNECTED) {
1452-
zend_throw_exception(redis_exception_ce, "Connection closed", 0 TSRMLS_CC);
1464+
redis_throw_exception(redis_exception_ce, __FILE__, __LINE__, cmd, "Connection closed", redis_sock TSRMLS_CC);
14531465
return -1;
14541466
}
14551467
if(-1 == redis_check_eof(redis_sock TSRMLS_CC)) {
@@ -1645,7 +1657,7 @@ redis_sock_gets(RedisSock *redis_sock, char *buf, int buf_size, size_t *line_siz
16451657
redis_sock->watching = 0;
16461658

16471659
// Throw a read error exception
1648-
zend_throw_exception(redis_exception_ce, "read error on connection", 0 TSRMLS_CC);
1660+
redis_throw_exception(redis_exception_ce, __FILE__, __LINE__, NULL, "read error on connection", redis_sock TSRMLS_CC);
16491661
}
16501662

16511663
// We don't need \r\n
@@ -1667,7 +1679,7 @@ redis_read_reply_type(RedisSock *redis_sock, REDIS_REPLY_TYPE *reply_type, int *
16671679

16681680
// Attempt to read the reply-type byte
16691681
if((*reply_type = php_stream_getc(redis_sock->stream)) == EOF) {
1670-
zend_throw_exception(redis_exception_ce, "socket error on read socket", 0 TSRMLS_CC);
1682+
redis_throw_exception(redis_exception_ce, __FILE__, __LINE__, NULL, "socket error on read socket", redis_sock TSRMLS_CC);
16711683
}
16721684

16731685
// If this is a BULK, MULTI BULK, or simply an INTEGER response, we can extract the value or size info here
@@ -1705,7 +1717,7 @@ redis_read_variant_line(RedisSock *redis_sock, REDIS_REPLY_TYPE reply_type, zval
17051717
// If this is an error response, check if it is a SYNC error, and throw in that case
17061718
if(reply_type == TYPE_ERR) {
17071719
if(memcmp(inbuf, "ERR SYNC", 9) == 0) {
1708-
zend_throw_exception(redis_exception_ce, "SYNC with master in progress", 0 TSRMLS_CC);
1720+
redis_throw_exception(redis_exception_ce, __FILE__, __LINE__, NULL, "SYNC with master in progress", redis_sock TSRMLS_CC);
17091721
}
17101722

17111723
// Set our last error

library.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ int redis_cmd_append_sstr_dbl(smart_str *str, double value);
1313

1414
PHPAPI char * redis_sock_read(RedisSock *redis_sock, int *buf_len TSRMLS_DC);
1515

16+
PHPAPI void redis_throw_exception(zend_class_entry *exception_ce, const char *file, int line, const char *cmd, const char *cause, RedisSock *redis_sock TSRMLS_DC);
1617
PHPAPI void redis_1_response(INTERNAL_FUNCTION_PARAMETERS, RedisSock *redis_sock, zval *z_tab, void *ctx);
1718
PHPAPI void redis_long_response(INTERNAL_FUNCTION_PARAMETERS, RedisSock *redis_sock, zval* z_tab, void *ctx);
1819
typedef void (*SuccessCallback)(RedisSock *redis_sock);

redis.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -383,7 +383,7 @@ PHPAPI int redis_sock_get(zval *id, RedisSock **redis_sock TSRMLS_DC, int no_thr
383383
sizeof("socket"), (void **) &socket) == FAILURE) {
384384
// Throw an exception unless we've been requested not to
385385
if(!no_throw) {
386-
zend_throw_exception(redis_exception_ce, "Redis server went away", 0 TSRMLS_CC);
386+
redis_throw_exception(redis_exception_ce, __FILE__, __LINE__, NULL, "Redis server went away", *redis_sock TSRMLS_CC);
387387
}
388388
return -1;
389389
}
@@ -393,7 +393,7 @@ PHPAPI int redis_sock_get(zval *id, RedisSock **redis_sock TSRMLS_DC, int no_thr
393393
if (!*redis_sock || resource_type != le_redis_sock) {
394394
// Throw an exception unless we've been requested not to
395395
if(!no_throw) {
396-
zend_throw_exception(redis_exception_ce, "Redis server went away", 0 TSRMLS_CC);
396+
redis_throw_exception(redis_exception_ce, __FILE__, __LINE__, NULL, "Redis server went away", *redis_sock TSRMLS_CC);
397397
}
398398
return -1;
399399
}

redis_array_impl.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -492,7 +492,8 @@ ra_find_node(RedisArray *ra, const char *key, int key_len, int *out_pos TSRMLS_D
492492
/* easy resharding */
493493
if (num_reshards) {
494494
/* Infer the new position */
495-
for (int i = 0; i < num_reshards; i++) {
495+
int i;
496+
for (i = 0; i < num_reshards; i++) {
496497
int total = num_original * 2;
497498
h64 = hash;
498499
h64 *= total;

0 commit comments

Comments
 (0)