Browse code

ndb_redis: replaced printf with dbg, formatted sentinel code and long lines

Daniel-Constantin Mierla authored on 24/09/2018 17:49:38
Showing 1 changed files
... ...
@@ -39,7 +39,8 @@
39 39
 
40 40
 #include "redis_client.h"
41 41
 
42
-#define redisCommandNR(a...) (int)({ void *__tmp; __tmp = redisCommand(a); if (__tmp) freeReplyObject(__tmp); __tmp ? 0 : -1;})
42
+#define redisCommandNR(a...) (int)({ void *__tmp; __tmp = redisCommand(a); \
43
+		if (__tmp) freeReplyObject(__tmp); __tmp ? 0 : -1;})
43 44
 
44 45
 static redisc_server_t * _redisc_srv_list=NULL;
45 46
 
... ...
@@ -93,7 +94,7 @@ int redisc_init(void)
93 94
 	{
94 95
 		char sentinels[MAXIMUM_SENTINELS][256];
95 96
 		uint8_t sentinels_count = 0;
96
-		
97
+
97 98
 		port = 6379;
98 99
 		db = 0;
99 100
 		haspass = 0;
... ...
@@ -106,10 +107,12 @@ int redisc_init(void)
106 107
 		for (pit = rsrv->attrs; pit; pit=pit->next)
107 108
 		{
108 109
 			if(pit->name.len==4 && strncmp(pit->name.s, "unix", 4)==0) {
109
-				snprintf(unix_sock_path, sizeof(unix_sock_path)-1, "%.*s", pit->body.len, pit->body.s);
110
+				snprintf(unix_sock_path, sizeof(unix_sock_path)-1, "%.*s",
111
+						pit->body.len, pit->body.s);
110 112
 				sock = 1;
111 113
 			} else if(pit->name.len==4 && strncmp(pit->name.s, "addr", 4)==0) {
112
-				snprintf(addr, sizeof(addr)-1, "%.*s", pit->body.len, pit->body.s);
114
+				snprintf(addr, sizeof(addr)-1, "%.*s",
115
+						pit->body.len, pit->body.s);
113 116
 			} else if(pit->name.len==4 && strncmp(pit->name.s, "port", 4)==0) {
114 117
 				if(str2int(&pit->body, &port) < 0)
115 118
 					port = 6379;
... ...
@@ -117,25 +120,33 @@ int redisc_init(void)
117 120
 				if(str2int(&pit->body, &db) < 0)
118 121
 					db = 0;
119 122
 			} else if(pit->name.len==4 && strncmp(pit->name.s, "pass", 4)==0) {
120
-				snprintf(pass, sizeof(pass)-1, "%.*s", pit->body.len, pit->body.s);
123
+				snprintf(pass, sizeof(pass)-1, "%.*s",
124
+						pit->body.len, pit->body.s);
121 125
 				haspass = 1;
122
-			} else if(pit->name.len==14 && strncmp(pit->name.s, "sentinel_group", 14)==0) {
123
-				snprintf(sentinel_group, sizeof(sentinel_group)-1, "%.*s", pit->body.len, pit->body.s);
124
-			} else if(pit->name.len==15 && strncmp(pit->name.s, "sentinel_master", 15)==0) {
126
+			} else if(pit->name.len==14 && strncmp(pit->name.s,
127
+						"sentinel_group", 14)==0) {
128
+				snprintf(sentinel_group, sizeof(sentinel_group)-1, "%.*s",
129
+						pit->body.len, pit->body.s);
130
+			} else if(pit->name.len==15 && strncmp(pit->name.s,
131
+						"sentinel_master", 15)==0) {
125 132
 				if(str2int(&pit->body, &sentinel_master) < 0)
126 133
 					sentinel_master = 1;
127
-			} else if(pit->name.len==8 && strncmp(pit->name.s, "sentinel", 8)==0) {
134
+			} else if(pit->name.len==8 && strncmp(pit->name.s,
135
+						"sentinel", 8)==0) {
128 136
 				if( sentinels_count < MAXIMUM_SENTINELS ){
129
-					snprintf(sentinels[sentinels_count], sizeof(sentinels[sentinels_count])-1, "%.*s", pit->body.len, pit->body.s);
137
+					snprintf(sentinels[sentinels_count],
138
+							sizeof(sentinels[sentinels_count])-1, "%.*s",
139
+							pit->body.len, pit->body.s);
130 140
 					sentinels_count++;
131 141
 				}
132 142
 				else {
133
-					LM_ERR("too many sentinels, maximum %d supported.\n", MAXIMUM_SENTINELS);
143
+					LM_ERR("too many sentinels, maximum %d supported.\n",
144
+							MAXIMUM_SENTINELS);
134 145
 					return -1;
135 146
 				}
136 147
 			}
137 148
 		}
138
-		
149
+
139 150
 		// if sentinels are provided, we need to connect to them and retrieve the redis server
140 151
 		// address / port
141 152
 		if(sentinels_count > 0) {
... ...
@@ -144,46 +155,49 @@ int redisc_init(void)
144 155
 				char *pos;
145 156
 				redisContext *redis;
146 157
 				redisReply *res, *res2;
147
-				
158
+
148 159
 				port = 6379;
149 160
 				if( (pos = strchr(sentinelAddr, ':')) != NULL ) {
150 161
 					port = atoi(pos+1);
151 162
 					pos[i] = '\0';
152 163
 				}
153
-				
164
+
154 165
 				redis = redisConnectWithTimeout(sentinelAddr, port, tv_conn);
155 166
 				if( redis ) {
156 167
 					if(sentinel_master != 0) {
157
-						res = redisCommand(redis, "SENTINEL get-master-addr-by-name %s", sentinel_group);
158
-						if( res && (res->type == REDIS_REPLY_ARRAY) && (res->elements == 2) ) {
159
-							strncpy(addr, res->element[0]->str, res->element[0]->len + 1);
168
+						res = redisCommand(redis,
169
+								"SENTINEL get-master-addr-by-name %s",
170
+								sentinel_group);
171
+						if( res && (res->type == REDIS_REPLY_ARRAY)
172
+								&& (res->elements == 2) ) {
173
+							strncpy(addr, res->element[0]->str,
174
+									res->element[0]->len + 1);
160 175
 							port = atoi(res->element[1]->str);
161
-							
162
-							
163
-							printf("sentinel replied: %s:%d\n", addr, port);
176
+							LM_DBG("sentinel replied: %s:%d\n", addr, port);
164 177
 						}
165 178
 					}
166 179
 					else {
167
-						res = redisCommand(redis, "SENTINEL slaves %s", sentinel_group);
180
+						res = redisCommand(redis, "SENTINEL slaves %s",
181
+								sentinel_group);
168 182
 						if( res && (res->type == REDIS_REPLY_ARRAY) ) {
169
-							
170 183
 							for(row = 0; row< res->elements; row++){
171 184
 								res2 = res->element[row];
172
-								
173 185
 								for(i= 0; i< res2->elements; i+= 2) {
174
-									if( strncmp(res2->element[i]->str, "ip", 2) == 0 ) {
175
-										strncpy(addr, res2->element[i+1]->str, res2->element[i+1]->len);
186
+									if( strncmp(res2->element[i]->str,
187
+												"ip", 2) == 0 ) {
188
+										strncpy(addr, res2->element[i+1]->str,
189
+												res2->element[i+1]->len);
176 190
 										addr[res2->element[i+1]->len] = '\0';
177 191
 									}
178
-									else if( strncmp(res2->element[i]->str, "port", 4) == 0) {
192
+									else if( strncmp(res2->element[i]->str,
193
+												"port", 4) == 0) {
179 194
 										port = atoi(res2->element[i+1]->str);
180 195
 										break;
181 196
 									}
182 197
 								}
183
-								
184 198
 							}
185
-							
186
-							printf("slave for %s: %s:%d\n", sentinel_group, addr, port);
199
+							LM_DBG("slave for %s: %s:%d\n", sentinel_group,
200
+									addr, port);
187 201
 						}
188 202
 					}
189 203
 				}
... ...
@@ -206,7 +220,8 @@ int redisc_init(void)
206 220
 			goto err;
207 221
 		}
208 222
 		if (rsrv->ctxRedis->err) {
209
-			LM_ERR("Failed to create REDIS returned an error: %s\n", rsrv->ctxRedis->errstr);
223
+			LM_ERR("Failed to create REDIS returned an error: %s\n",
224
+					rsrv->ctxRedis->errstr);
210 225
 			goto err2;
211 226
 		}
212 227
 		if ((haspass != 0) && redisc_check_auth(rsrv, pass)) {
... ...
@@ -218,11 +233,14 @@ int redisc_init(void)
218 233
 			goto err2;
219 234
 		}
220 235
 		if (redisCommandNR(rsrv->ctxRedis, "PING")) {
221
-			LM_ERR("Failed to send PING (REDIS returned %s).\n", rsrv->ctxRedis->errstr);
236
+			LM_ERR("Failed to send PING (REDIS returned %s).\n",
237
+					rsrv->ctxRedis->errstr);
222 238
 			goto err2;
223 239
 		}
224
-		if ((redis_cluster_param == 0) && redisCommandNR(rsrv->ctxRedis, "SELECT %i", db)) {
225
-			LM_ERR("Failed to send \"SELECT %i\" (REDIS returned \"%s\", and not in cluster mode).\n", db, rsrv->ctxRedis->errstr);
240
+		if ((redis_cluster_param == 0) && redisCommandNR(rsrv->ctxRedis,
241
+					"SELECT %i", db)) {
242
+			LM_ERR("Failed to send \"SELECT %i\" (REDIS returned \"%s\","
243
+					" and not in cluster mode).\n", db, rsrv->ctxRedis->errstr);
226 244
 			goto err2;
227 245
 		}
228 246
 	}
... ...
@@ -374,7 +392,8 @@ redisc_server_t *redisc_get_server(str *name)
374 392
 	rsrv=_redisc_srv_list;
375 393
 	while(rsrv!=NULL)
376 394
 	{
377
-		LM_DBG("Entry %u (%.*s)\n", rsrv->hname, rsrv->sname->len, rsrv->sname->s);
395
+		LM_DBG("Entry %u (%.*s)\n", rsrv->hname,
396
+				rsrv->sname->len, rsrv->sname->s);
378 397
 		if(rsrv->hname==hname && rsrv->sname->len==name->len
379 398
 				&& strncmp(rsrv->sname->s, name->s, name->len)==0)
380 399
 			return rsrv;
... ...
@@ -409,7 +428,8 @@ int redisc_reconnect_server(redisc_server_t *rsrv)
409 428
 	for (pit = rsrv->attrs; pit; pit=pit->next)
410 429
 	{
411 430
 		if(pit->name.len==4 && strncmp(pit->name.s, "unix", 4)==0) {
412
-			snprintf(unix_sock_path, sizeof(unix_sock_path)-1, "%.*s", pit->body.len, pit->body.s);
431
+			snprintf(unix_sock_path, sizeof(unix_sock_path)-1, "%.*s",
432
+					pit->body.len, pit->body.s);
413 433
 			sock = 1;
414 434
 		} else if(pit->name.len==4 && strncmp(pit->name.s, "addr", 4)==0) {
415 435
 			snprintf(addr, sizeof(addr)-1, "%.*s", pit->body.len, pit->body.s);
... ...
@@ -447,7 +467,8 @@ int redisc_reconnect_server(redisc_server_t *rsrv)
447 467
 		goto err2;
448 468
 	if (redisCommandNR(rsrv->ctxRedis, "PING"))
449 469
 		goto err2;
450
-	if ((redis_cluster_param == 0) && redisCommandNR(rsrv->ctxRedis, "SELECT %i", db))
470
+	if ((redis_cluster_param == 0) && redisCommandNR(rsrv->ctxRedis,
471
+				"SELECT %i", db))
451 472
 		goto err2;
452 473
 	if (redis_flush_on_reconnect_param)
453 474
 		if (redisCommandNR(rsrv->ctxRedis, "FLUSHALL"))
... ...
@@ -511,7 +532,8 @@ int redisc_append_cmd(str *srv, str *res, str *cmd, ...)
511 532
 	}
512 533
 	if (rsrv->piped.pending_commands >= MAXIMUM_PIPELINED_COMMANDS)
513 534
 	{
514
-		LM_ERR("Too many pipelined commands, maximum is %d\n",MAXIMUM_PIPELINED_COMMANDS);
535
+		LM_ERR("Too many pipelined commands, maximum is %d\n",
536
+				MAXIMUM_PIPELINED_COMMANDS);
515 537
 		goto error_cmd;
516 538
 	}
517 539
 	rpl = redisc_get_reply(res);
... ...
@@ -586,17 +608,21 @@ int redisc_create_pipelined_message(redisc_server_t *rsrv)
586 608
 
587 609
 	if (rsrv->ctxRedis->err)
588 610
 	{
589
-		LM_DBG("Reconnecting server because of error %d: \"%s\"",rsrv->ctxRedis->err,rsrv->ctxRedis->errstr);
611
+		LM_DBG("Reconnecting server because of error %d: \"%s\"",
612
+				rsrv->ctxRedis->err,rsrv->ctxRedis->errstr);
590 613
 		if (redisc_reconnect_server(rsrv))
591 614
 		{
592
-			LM_ERR("unable to reconnect to REDIS server: %.*s\n", rsrv->sname->len,rsrv->sname->s);
615
+			LM_ERR("unable to reconnect to REDIS server: %.*s\n",
616
+					rsrv->sname->len,rsrv->sname->s);
593 617
 			return -1;
594 618
 		}
595 619
 	}
596 620
 
597 621
 	for (i=0;i<rsrv->piped.pending_commands;i++)
598 622
 	{
599
-		if (redis_append_formatted_command(rsrv->ctxRedis,rsrv->piped.commands[i].s,rsrv->piped.commands[i].len) != REDIS_OK)
623
+		if (redis_append_formatted_command(rsrv->ctxRedis,
624
+					rsrv->piped.commands[i].s,rsrv->piped.commands[i].len)
625
+						!= REDIS_OK)
600 626
 		{
601 627
 			LM_ERR("Error while appending command %d",i);
602 628
 			return -1;
... ...
@@ -642,7 +668,8 @@ int redisc_exec_pipelined(redisc_server_t *rsrv)
642 668
 	}
643 669
 	if(rsrv->ctxRedis==NULL)
644 670
 	{
645
-		LM_ERR("no redis context for server: %.*s\n", rsrv->sname->len,rsrv->sname->s);
671
+		LM_ERR("no redis context for server: %.*s\n",
672
+				rsrv->sname->len,rsrv->sname->s);
646 673
 		goto error_exec;
647 674
 	}
648 675
 
... ...
@@ -694,7 +721,8 @@ int redisc_exec_pipelined(redisc_server_t *rsrv)
694 721
 			freeReplyObject(rpl->rplRedis);
695 722
 			rpl->rplRedis = NULL;
696 723
 		}
697
-		if (redisGetReplyFromReader(rsrv->ctxRedis, (void**) &rpl->rplRedis) != REDIS_OK)
724
+		if (redisGetReplyFromReader(rsrv->ctxRedis, (void**) &rpl->rplRedis)
725
+				!= REDIS_OK)
698 726
 		{
699 727
 			LM_ERR("Unable to read reply\n");
700 728
 			continue;
... ...
@@ -1181,7 +1209,8 @@ int redis_check_server(redisc_server_t *rsrv)
1181 1209
 	{
1182 1210
 		if (get_ticks() > rsrv->disable.restore_tick)
1183 1211
 		{
1184
-			LM_NOTICE("REDIS server %.*s re-enabled",rsrv->sname->len,rsrv->sname->s);
1212
+			LM_NOTICE("REDIS server %.*s re-enabled",
1213
+					rsrv->sname->len, rsrv->sname->s);
1185 1214
 			rsrv->disable.disabled = 0;
1186 1215
 			rsrv->disable.consecutive_errors = 0;
1187 1216
 		}
... ...
@@ -1205,7 +1234,8 @@ int redis_count_err_and_disable(redisc_server_t *rsrv)
1205 1234
 	{
1206 1235
 		rsrv->disable.disabled=1;
1207 1236
 		rsrv->disable.restore_tick=get_ticks() + redis_disable_time_param;
1208
-		LM_WARN("REDIS server %.*s disabled for %d seconds",rsrv->sname->len,rsrv->sname->s,redis_disable_time_param);
1237
+		LM_WARN("REDIS server %.*s disabled for %d seconds", rsrv->sname->len,
1238
+				rsrv->sname->s, redis_disable_time_param);
1209 1239
 		return 1;
1210 1240
 	}
1211 1241
 	return 0;
... ...
@@ -1246,7 +1276,8 @@ void print_redis_reply(int log_level, redisReply *rpl,int offset)
1246 1276
 		LOG(log_level,"%sinteger reply: %lld", padding, rpl->integer);
1247 1277
 		break;
1248 1278
 	case REDIS_REPLY_ARRAY:
1249
-		LOG(log_level,"%sarray reply with %d elements", padding, (int)rpl->elements);
1279
+		LOG(log_level,"%sarray reply with %d elements", padding,
1280
+				(int)rpl->elements);
1250 1281
 		for (i=0; i < rpl->elements; i++)
1251 1282
 		{
1252 1283
 			LOG(log_level,"%selement %d:",padding,i);