[Asterisk-code-review] res pjsip: improve realtime performance on CLI 'pjsip show c... (asterisk[13])

Alexei Gradinari asteriskteam at digium.com
Tue Sep 25 17:45:01 CDT 2018


Alexei Gradinari has uploaded this change for review. ( https://gerrit.asterisk.org/10270


Change subject: res_pjsip: improve realtime performance on CLI 'pjsip show contacts'
......................................................................

res_pjsip: improve realtime performance on CLI 'pjsip show contacts'

CLI command 'pjsip show contacts' inefficiently make a lot of DB requests.

For example if there are 10k aors then asterisk requests these 10k records
of aor and then does 10k requests of contact - one request per aor.

Even if use 'like <pattern>' the asterisk requests all aor's and contact's
records and then filters them by itself.

This patch gathers contact's container by
- retrieving all dynamic contacts by regex (filtered by reg_server)
- retrieving all aors with permanent contacts
- finally filters container by regex

ASTERISK-28077 #close

Change-Id: Id0ad65d14952a02fb213273a90f3f680a8149618
---
M res/res_pjsip/location.c
1 file changed, 60 insertions(+), 10 deletions(-)



  git pull ssh://gerrit.asterisk.org:29418/asterisk refs/changes/70/10270/1

diff --git a/res/res_pjsip/location.c b/res/res_pjsip/location.c
index eadc115..0ec5edf 100644
--- a/res/res_pjsip/location.c
+++ b/res/res_pjsip/location.c
@@ -1001,35 +1001,85 @@
 	return CMP_MATCH;
 }
 
+static int cli_gather_contact(void *obj, void *arg, int flags)
+{
+	struct ast_sip_contact *contact = obj;
+	RAII_VAR(struct ast_sip_contact_wrapper *, wrapper, NULL, ao2_cleanup);
+
+	if (strcmp(contact->reg_server, ast_config_AST_SYSTEM_NAME ?: "")) {
+		return 0;
+	}
+
+	wrapper = ao2_alloc_options(sizeof(struct ast_sip_contact_wrapper),
+		contact_wrapper_destroy, AO2_ALLOC_OPT_LOCK_NOLOCK);
+	if (!wrapper) {
+		return -1;
+	}
+
+	wrapper->contact_id = ast_malloc(strlen(contact->aor) + strlen(contact->uri) + 2);
+	if (!wrapper->contact_id) {
+		return -1;
+	}
+	sprintf(wrapper->contact_id, "%s/%s", contact->aor, contact->uri);
+
+	wrapper->aor_id = ast_strdup(contact->aor);
+	if (!wrapper->aor_id) {
+		ast_free(wrapper->contact_id);
+		return -1;
+	}
+
+	wrapper->contact = contact;
+	ao2_bump(wrapper->contact);
+
+	ao2_link(arg, wrapper);
+
+        return 0;
+}
+
 static struct ao2_container *cli_contact_get_container(const char *regex)
 {
-	RAII_VAR(struct ao2_container *, parent_container, NULL, ao2_cleanup);
-	struct ao2_container *child_container;
+	RAII_VAR(struct ao2_container *, aors, NULL, ao2_cleanup);
+	RAII_VAR(struct ao2_container *, contacts, NULL, ao2_cleanup);
+	RAII_VAR(struct ast_variable *, var_aor, NULL, ast_variables_destroy);
+	struct ao2_container *contacts_container;
 	regex_t regexbuf;
 
-	parent_container = cli_aor_get_container("");
-	if (!parent_container) {
+	if (!(var_aor = ast_variable_new("contact !=", "", ""))) {
 		return NULL;
 	}
 
-	child_container = ao2_container_alloc_list(AO2_ALLOC_OPT_LOCK_NOLOCK, 0,
+	contacts_container = ao2_container_alloc_list(AO2_ALLOC_OPT_LOCK_NOLOCK, 0,
 		cli_contact_sort, cli_contact_compare);
-	if (!child_container) {
+	if (!contacts_container) {
 		return NULL;
 	}
 
-	ao2_callback(parent_container, OBJ_NODATA, cli_aor_gather_contacts, child_container);
+	contacts = ast_sorcery_retrieve_by_regex(ast_sip_get_sorcery(), "contact", regex);
+	if (!contacts) {
+		ao2_ref(contacts_container, -1);
+		return NULL;
+	}
+	ao2_callback(contacts, OBJ_NODATA, cli_gather_contact, contacts_container);
+
+	aors = ast_sorcery_retrieve_by_fields(ast_sip_get_sorcery(),
+		"aor", AST_RETRIEVE_FLAG_MULTIPLE, var_aor);
+	if (!aors) {
+		ao2_ref(contacts_container, -1);
+		return NULL;
+	}
+
+	ao2_callback(aors, OBJ_NODATA, cli_aor_gather_contacts, contacts_container);
 
 	if (!ast_strlen_zero(regex)) {
 		if (regcomp(&regexbuf, regex, REG_EXTENDED | REG_NOSUB)) {
-			ao2_ref(child_container, -1);
+			ao2_ref(contacts_container, -1);
 			return NULL;
 		}
-		ao2_callback(child_container, OBJ_UNLINK | OBJ_MULTIPLE | OBJ_NODATA, cli_filter_contacts, &regexbuf);
+		ao2_callback(contacts_container, OBJ_UNLINK | OBJ_MULTIPLE | OBJ_NODATA, cli_filter_contacts, &regexbuf);
 		regfree(&regexbuf);
 	}
 
-	return child_container;
+	return contacts_container;
 }
 
 static void *cli_contact_retrieve_by_id(const char *id)

-- 
To view, visit https://gerrit.asterisk.org/10270
To unsubscribe, or for help writing mail filters, visit https://gerrit.asterisk.org/settings

Gerrit-Project: asterisk
Gerrit-Branch: 13
Gerrit-MessageType: newchange
Gerrit-Change-Id: Id0ad65d14952a02fb213273a90f3f680a8149618
Gerrit-Change-Number: 10270
Gerrit-PatchSet: 1
Gerrit-Owner: Alexei Gradinari <alex2grad at gmail.com>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.digium.com/pipermail/asterisk-code-review/attachments/20180925/a5d0da6a/attachment.html>


More information about the asterisk-code-review mailing list