Browse code

presence: be more resilient doing clean up of presentity values

previously if an error was found we were bailing out and the value
was kept so at next round the value will be there and no more values
where removed

(cherry picked from commit 444adb0b27d62fbd2af25e4f555b6aab5ca7bc96)

Victor Seva authored on 29/06/2022 07:03:14 • Daniel-Constantin Mierla committed on 06/07/2022 07:23:59
Showing 1 changed files
... ...
@@ -69,6 +69,7 @@ void ps_presentity_db_timer_clean(unsigned int ticks, void *param)
69 69
 	int n_db_cols = 0, n_result_cols = 0;
70 70
 	int event_col, etag_col, user_col, domain_col;
71 71
 	int i = 0, num_watchers = 0;
72
+	pres_ev_t fake;
72 73
 	presentity_t pres;
73 74
 	str uri = {0, 0}, event, *rules_doc = NULL;
74 75
 	static str query_str;
... ...
@@ -121,6 +122,7 @@ void ps_presentity_db_timer_clean(unsigned int ticks, void *param)
121 122
 		rows = RES_ROWS(result);
122 123
 
123 124
 		for(i = 0; i < RES_ROW_N(result); i++) {
125
+			num_watchers = 0;
124 126
 			values = ROW_VALUES(&rows[i]);
125 127
 			memset(&pres, 0, sizeof(presentity_t));
126 128
 
... ...
@@ -134,28 +136,33 @@ void ps_presentity_db_timer_clean(unsigned int ticks, void *param)
134 136
 			event.len = strlen(event.s);
135 137
 			pres.event = contains_event(&event, NULL);
136 138
 			if(pres.event == NULL || pres.event->evp == NULL) {
137
-				LM_ERR("event not found\n");
138
-				goto error;
139
+				LM_ERR("event[%.*s] not found\n", STR_FMT(&event));
140
+				memset(&fake, 0, sizeof(pres_ev_t));
141
+				fake.name = event;
142
+				pres.event = &fake;
143
+				goto simple_error;
139 144
 			}
140 145
 
141 146
 			if(uandd_to_uri(pres.user, pres.domain, &uri) < 0) {
142
-				LM_ERR("constructing uri\n");
143
-				goto error;
147
+				LM_ERR("constructing uri from [user]=%.*s  [domain]=%.*s\n",
148
+					STR_FMT(&pres.user), STR_FMT(&pres.domain));
149
+				goto simple_error;
144 150
 			}
145 151
 
146 152
 			/* delete from hash table */
147 153
 			if(publ_cache_mode==PS_PCACHE_HYBRID
148 154
 					&& delete_phtable(&uri, pres.event->evp->type) < 0) {
149
-				LM_ERR("deleting from presentity hash table\n");
150
-				goto error;
155
+				LM_ERR("deleting uri[%.*s] event[%.*s] from presentity hash table\n",
156
+					STR_FMT(&uri), STR_FMT(&event));
157
+				goto simple_error;
151 158
 			}
152 159
 
153
-			LM_DBG("found expired publish for [user]=%.*s  [domanin]=%.*s\n",
160
+			LM_DBG("found expired publish for [user]=%.*s  [domain]=%.*s\n",
154 161
 					pres.user.len, pres.user.s, pres.domain.len, pres.domain.s);
155 162
 
156 163
 			if(pres_force_delete == 1) {
157 164
 				if(delete_presentity(&pres, NULL) < 0) {
158
-					LM_ERR("Deleting presentity\n");
165
+					LM_ERR("Deleting presentity uri[%.*s]\n", STR_FMT(&uri));
159 166
 					goto error;
160 167
 				}
161 168
 			} else if(pres_notifier_processes > 0) {
... ...
@@ -171,7 +178,7 @@ void ps_presentity_db_timer_clean(unsigned int ticks, void *param)
171 178
 						if(pa_dbf.abort_transaction(pa_db) < 0)
172 179
 							LM_ERR("in abort_transaction\n");
173 180
 					}
174
-					goto error;
181
+					goto next;
175 182
 				}
176 183
 
177 184
 				if(num_watchers > 0) {
... ...
@@ -181,12 +188,12 @@ void ps_presentity_db_timer_clean(unsigned int ticks, void *param)
181 188
 							if(pa_dbf.abort_transaction(pa_db) < 0)
182 189
 								LM_ERR("in abort_transaction\n");
183 190
 						}
184
-						goto error;
191
+						goto next;
185 192
 					}
186 193
 				} else {
187 194
 					if(delete_presentity(&pres, NULL) < 0) {
188
-						LM_ERR("Deleting presentity\n");
189
-						goto error;
195
+						LM_ERR("Deleting presentity uri[%.*s]\n", STR_FMT(&uri));
196
+						goto next;
190 197
 					}
191 198
 				}
192 199
 				if(pa_dbf.end_transaction) {
... ...
@@ -201,22 +208,30 @@ void ps_presentity_db_timer_clean(unsigned int ticks, void *param)
201 208
 								   &pres.user, &pres.domain, &rules_doc)
202 209
 								   < 0) {
203 210
 					LM_ERR("getting rules doc\n");
204
-					goto error;
211
+					goto simple_error;
205 212
 				}
206 213
 				if(publ_notify(&pres, uri, NULL, &pres.etag, rules_doc) < 0) {
207 214
 					LM_ERR("sending Notify request\n");
208
-					goto error;
209
-				}
210
-				if(rules_doc) {
211
-					if(rules_doc->s)
212
-						pkg_free(rules_doc->s);
213
-					pkg_free(rules_doc);
214
-					rules_doc = NULL;
215
+					goto simple_error;
215 216
 				}
216 217
 			}
217 218
 
218
-			pkg_free(uri.s);
219
-			uri.s = NULL;
219
+simple_error:
220
+			if(num_watchers == 0 && delete_presentity(&pres, NULL) < 0) {
221
+				LM_ERR("Deleting presentity\n");
222
+			}
223
+next:
224
+			if(uri.s) {
225
+				pkg_free(uri.s);
226
+				uri.s = NULL;
227
+			}
228
+			if(rules_doc) {
229
+				if(rules_doc->s) {
230
+					pkg_free(rules_doc->s);
231
+				}
232
+				pkg_free(rules_doc);
233
+				rules_doc = NULL;
234
+			}
220 235
 		}
221 236
 	} while(db_fetch_next(&pa_dbf, pres_fetch_rows, pa_db, &result) == 1
222 237
 			&& RES_ROW_N(result) > 0);
... ...
@@ -277,22 +292,23 @@ void ps_ptable_timer_clean(unsigned int ticks, void *param)
277 292
 		pres.etag = ptn->etag;
278 293
 		pres.event = contains_event(&ptn->event, NULL);
279 294
 		if(pres.event == NULL || pres.event->evp == NULL) {
280
-			LM_ERR("event not found\n");
281
-			goto error;
295
+			LM_ERR("event[%.*s] not found\n", STR_FMT(&ptn->event));
296
+			goto next;
282 297
 		}
283 298
 
284 299
 		if(uandd_to_uri(pres.user, pres.domain, &uri) < 0) {
285
-			LM_ERR("constructing uri\n");
286
-			goto error;
300
+			LM_ERR("constructing uri from [user]=%.*s  [domain]=%.*s\n",
301
+				STR_FMT(&pres.user), STR_FMT(&pres.domain));
302
+			goto next;
287 303
 		}
288 304
 
289
-		LM_DBG("found expired publish for [user]=%.*s  [domanin]=%.*s\n",
305
+		LM_DBG("found expired publish for [user]=%.*s  [domain]=%.*s\n",
290 306
 				pres.user.len, pres.user.s, pres.domain.len, pres.domain.s);
291 307
 
292 308
 		if(pres_force_delete == 1) {
293 309
 			if(ps_ptable_remove(ptn) <0) {
294 310
 				LM_ERR("Deleting presentity\n");
295
-				goto error;
311
+				goto next;
296 312
 			}
297 313
 		} else {
298 314
 			if(pres.event->get_rules_doc
... ...
@@ -300,25 +316,28 @@ void ps_ptable_timer_clean(unsigned int ticks, void *param)
300 316
 								&pres.user, &pres.domain, &rules_doc)
301 317
 								< 0) {
302 318
 				LM_ERR("getting rules doc\n");
303
-				goto error;
319
+				goto next;
304 320
 			}
305 321
 			if(publ_notify(&pres, uri, NULL, &pres.etag, rules_doc) < 0) {
306 322
 				LM_ERR("sending Notify request\n");
307
-				goto error;
308
-			}
309
-			if(rules_doc) {
310
-				if(rules_doc->s)
311
-					pkg_free(rules_doc->s);
312
-				pkg_free(rules_doc);
313
-				rules_doc = NULL;
323
+				goto next;
314 324
 			}
315 325
 		}
316 326
 
317
-		pkg_free(uri.s);
318
-		uri.s = NULL;
327
+next:
328
+		if(uri.s) {
329
+			pkg_free(uri.s);
330
+			uri.s = NULL;
331
+		}
332
+		if(rules_doc) {
333
+			if(rules_doc->s) {
334
+				pkg_free(rules_doc->s);
335
+			}
336
+			pkg_free(rules_doc);
337
+			rules_doc = NULL;
338
+		}
319 339
 	}
320 340
 
321
-error:
322 341
 	for(ptn = ptlist; ptn != NULL; ptn = ptn->next) {
323 342
 		if(ps_ptable_remove(ptn) <0) {
324 343
 			LM_ERR("failed deleting presentity item\n");