Browse code

presence: No autocommit+rollback if no active watchers

- In case the active_watcher query returns no elements there
is a weird situation where kamailio first runs the select for
active_watcher then rollbacks.
This can happen a lot of times for each second since function
process_dialogs() runs repeatedly.
For this reason trying to avoid the rollback can result
in a quite good performance boost.

Alessio Garzi authored on 07/07/2022 13:22:12
Showing 1 changed files
... ...
@@ -2867,6 +2867,7 @@ int process_dialogs(int round, int presence_winfo)
2867 2867
 	str ev_sname, winfo = str_init("presence.winfo");
2868 2868
 	int now = (int)time(NULL);
2869 2869
 	int updated = 0;
2870
+	int no_active_watchers = 0;
2870 2871
 	db_query_f query_fn = pa_dbf.query_lock ? pa_dbf.query_lock : pa_dbf.query;
2871 2872
 
2872 2873
 	query_cols[n_query_cols] = &str_updated_col;
... ...
@@ -2900,13 +2901,6 @@ int process_dialogs(int round, int presence_winfo)
2900 2901
 		goto error;
2901 2902
 	}
2902 2903
 
2903
-	if(pa_dbf.start_transaction) {
2904
-		if(pa_dbf.start_transaction(pa_db, pres_db_table_lock) < 0) {
2905
-			LM_ERR("in start_transaction\n");
2906
-			goto error;
2907
-		}
2908
-	}
2909
-
2910 2904
 	/* Step 1: Find active_watchers that require notification */
2911 2905
 	if(query_fn(pa_db, query_cols, query_ops, query_vals, result_cols,
2912 2906
 			   n_query_cols, n_result_cols, 0, &dialog_list)
... ...
@@ -2920,7 +2914,17 @@ int process_dialogs(int round, int presence_winfo)
2920 2914
 	}
2921 2915
 
2922 2916
 	if(dialog_list->n <= 0)
2917
+	{
2918
+		no_active_watchers = 1;
2923 2919
 		goto done;
2920
+	}
2921
+
2922
+	if(pa_dbf.start_transaction) {
2923
+		if(pa_dbf.start_transaction(pa_db, pres_db_table_lock) < 0) {
2924
+			LM_ERR("in start_transaction\n");
2925
+			goto error;
2926
+		}
2927
+	}
2924 2928
 
2925 2929
 	/* Step 2: Update the records so they are not notified again */
2926 2930
 	if(pa_dbf.update(pa_db, query_cols, query_ops, query_vals, update_cols,
... ...
@@ -3164,7 +3168,7 @@ error:
3164 3168
 	if(dialog)
3165 3169
 		pa_dbf.free_result(pa_db, dialog);
3166 3170
 
3167
-	if(pa_dbf.abort_transaction) {
3171
+	if(no_active_watchers == 0 && pa_dbf.abort_transaction) {
3168 3172
 		if(pa_dbf.abort_transaction(pa_db) < 0)
3169 3173
 			LM_ERR("in abort_transaction\n");
3170 3174
 	}