<p>Alexei Gradinari has uploaded this change for <strong>review</strong>.</p><p><a href="https://gerrit.asterisk.org/10270">View Change</a></p><pre style="font-family: monospace,monospace; white-space: pre-wrap;">res_pjsip: improve realtime performance on CLI 'pjsip show contacts'<br><br>CLI command 'pjsip show contacts' inefficiently make a lot of DB requests.<br><br>For example if there are 10k aors then asterisk requests these 10k records<br>of aor and then does 10k requests of contact - one request per aor.<br><br>Even if use 'like <pattern>' the asterisk requests all aor's and contact's<br>records and then filters them by itself.<br><br>This patch gathers contact's container by<br>- retrieving all dynamic contacts by regex (filtered by reg_server)<br>- retrieving all aors with permanent contacts<br>- finally filters container by regex<br><br>ASTERISK-28077 #close<br><br>Change-Id: Id0ad65d14952a02fb213273a90f3f680a8149618<br>---<br>M res/res_pjsip/location.c<br>1 file changed, 60 insertions(+), 10 deletions(-)<br><br></pre><pre style="font-family: monospace,monospace; white-space: pre-wrap;">git pull ssh://gerrit.asterisk.org:29418/asterisk refs/changes/70/10270/1</pre><pre style="font-family: monospace,monospace; white-space: pre-wrap;"><span>diff --git a/res/res_pjsip/location.c b/res/res_pjsip/location.c</span><br><span>index eadc115..0ec5edf 100644</span><br><span>--- a/res/res_pjsip/location.c</span><br><span>+++ b/res/res_pjsip/location.c</span><br><span>@@ -1001,35 +1001,85 @@</span><br><span> return CMP_MATCH;</span><br><span> }</span><br><span> </span><br><span style="color: hsl(120, 100%, 40%);">+static int cli_gather_contact(void *obj, void *arg, int flags)</span><br><span style="color: hsl(120, 100%, 40%);">+{</span><br><span style="color: hsl(120, 100%, 40%);">+ struct ast_sip_contact *contact = obj;</span><br><span style="color: hsl(120, 100%, 40%);">+ RAII_VAR(struct ast_sip_contact_wrapper *, wrapper, NULL, ao2_cleanup);</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+ if (strcmp(contact->reg_server, ast_config_AST_SYSTEM_NAME ?: "")) {</span><br><span style="color: hsl(120, 100%, 40%);">+ return 0;</span><br><span style="color: hsl(120, 100%, 40%);">+ }</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+ wrapper = ao2_alloc_options(sizeof(struct ast_sip_contact_wrapper),</span><br><span style="color: hsl(120, 100%, 40%);">+ contact_wrapper_destroy, AO2_ALLOC_OPT_LOCK_NOLOCK);</span><br><span style="color: hsl(120, 100%, 40%);">+ if (!wrapper) {</span><br><span style="color: hsl(120, 100%, 40%);">+ return -1;</span><br><span style="color: hsl(120, 100%, 40%);">+ }</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+ wrapper->contact_id = ast_malloc(strlen(contact->aor) + strlen(contact->uri) + 2);</span><br><span style="color: hsl(120, 100%, 40%);">+ if (!wrapper->contact_id) {</span><br><span style="color: hsl(120, 100%, 40%);">+ return -1;</span><br><span style="color: hsl(120, 100%, 40%);">+ }</span><br><span style="color: hsl(120, 100%, 40%);">+ sprintf(wrapper->contact_id, "%s/%s", contact->aor, contact->uri);</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+ wrapper->aor_id = ast_strdup(contact->aor);</span><br><span style="color: hsl(120, 100%, 40%);">+ if (!wrapper->aor_id) {</span><br><span style="color: hsl(120, 100%, 40%);">+ ast_free(wrapper->contact_id);</span><br><span style="color: hsl(120, 100%, 40%);">+ return -1;</span><br><span style="color: hsl(120, 100%, 40%);">+ }</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+ wrapper->contact = contact;</span><br><span style="color: hsl(120, 100%, 40%);">+ ao2_bump(wrapper->contact);</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+ ao2_link(arg, wrapper);</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+ return 0;</span><br><span style="color: hsl(120, 100%, 40%);">+}</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span> static struct ao2_container *cli_contact_get_container(const char *regex)</span><br><span> {</span><br><span style="color: hsl(0, 100%, 40%);">- RAII_VAR(struct ao2_container *, parent_container, NULL, ao2_cleanup);</span><br><span style="color: hsl(0, 100%, 40%);">- struct ao2_container *child_container;</span><br><span style="color: hsl(120, 100%, 40%);">+ RAII_VAR(struct ao2_container *, aors, NULL, ao2_cleanup);</span><br><span style="color: hsl(120, 100%, 40%);">+ RAII_VAR(struct ao2_container *, contacts, NULL, ao2_cleanup);</span><br><span style="color: hsl(120, 100%, 40%);">+ RAII_VAR(struct ast_variable *, var_aor, NULL, ast_variables_destroy);</span><br><span style="color: hsl(120, 100%, 40%);">+ struct ao2_container *contacts_container;</span><br><span> regex_t regexbuf;</span><br><span> </span><br><span style="color: hsl(0, 100%, 40%);">- parent_container = cli_aor_get_container("");</span><br><span style="color: hsl(0, 100%, 40%);">- if (!parent_container) {</span><br><span style="color: hsl(120, 100%, 40%);">+ if (!(var_aor = ast_variable_new("contact !=", "", ""))) {</span><br><span> return NULL;</span><br><span> }</span><br><span> </span><br><span style="color: hsl(0, 100%, 40%);">- child_container = ao2_container_alloc_list(AO2_ALLOC_OPT_LOCK_NOLOCK, 0,</span><br><span style="color: hsl(120, 100%, 40%);">+ contacts_container = ao2_container_alloc_list(AO2_ALLOC_OPT_LOCK_NOLOCK, 0,</span><br><span> cli_contact_sort, cli_contact_compare);</span><br><span style="color: hsl(0, 100%, 40%);">- if (!child_container) {</span><br><span style="color: hsl(120, 100%, 40%);">+ if (!contacts_container) {</span><br><span> return NULL;</span><br><span> }</span><br><span> </span><br><span style="color: hsl(0, 100%, 40%);">- ao2_callback(parent_container, OBJ_NODATA, cli_aor_gather_contacts, child_container);</span><br><span style="color: hsl(120, 100%, 40%);">+ contacts = ast_sorcery_retrieve_by_regex(ast_sip_get_sorcery(), "contact", regex);</span><br><span style="color: hsl(120, 100%, 40%);">+ if (!contacts) {</span><br><span style="color: hsl(120, 100%, 40%);">+ ao2_ref(contacts_container, -1);</span><br><span style="color: hsl(120, 100%, 40%);">+ return NULL;</span><br><span style="color: hsl(120, 100%, 40%);">+ }</span><br><span style="color: hsl(120, 100%, 40%);">+ ao2_callback(contacts, OBJ_NODATA, cli_gather_contact, contacts_container);</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+ aors = ast_sorcery_retrieve_by_fields(ast_sip_get_sorcery(),</span><br><span style="color: hsl(120, 100%, 40%);">+ "aor", AST_RETRIEVE_FLAG_MULTIPLE, var_aor);</span><br><span style="color: hsl(120, 100%, 40%);">+ if (!aors) {</span><br><span style="color: hsl(120, 100%, 40%);">+ ao2_ref(contacts_container, -1);</span><br><span style="color: hsl(120, 100%, 40%);">+ return NULL;</span><br><span style="color: hsl(120, 100%, 40%);">+ }</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+ ao2_callback(aors, OBJ_NODATA, cli_aor_gather_contacts, contacts_container);</span><br><span> </span><br><span> if (!ast_strlen_zero(regex)) {</span><br><span> if (regcomp(®exbuf, regex, REG_EXTENDED | REG_NOSUB)) {</span><br><span style="color: hsl(0, 100%, 40%);">- ao2_ref(child_container, -1);</span><br><span style="color: hsl(120, 100%, 40%);">+ ao2_ref(contacts_container, -1);</span><br><span> return NULL;</span><br><span> }</span><br><span style="color: hsl(0, 100%, 40%);">- ao2_callback(child_container, OBJ_UNLINK | OBJ_MULTIPLE | OBJ_NODATA, cli_filter_contacts, ®exbuf);</span><br><span style="color: hsl(120, 100%, 40%);">+ ao2_callback(contacts_container, OBJ_UNLINK | OBJ_MULTIPLE | OBJ_NODATA, cli_filter_contacts, ®exbuf);</span><br><span> regfree(®exbuf);</span><br><span> }</span><br><span> </span><br><span style="color: hsl(0, 100%, 40%);">- return child_container;</span><br><span style="color: hsl(120, 100%, 40%);">+ return contacts_container;</span><br><span> }</span><br><span> </span><br><span> static void *cli_contact_retrieve_by_id(const char *id)</span><br><span></span><br></pre><p>To view, visit <a href="https://gerrit.asterisk.org/10270">change 10270</a>. To unsubscribe, or for help writing mail filters, visit <a href="https://gerrit.asterisk.org/settings">settings</a>.</p><div itemscope itemtype="http://schema.org/EmailMessage"><div itemscope itemprop="action" itemtype="http://schema.org/ViewAction"><link itemprop="url" href="https://gerrit.asterisk.org/10270"/><meta itemprop="name" content="View Change"/></div></div>
<div style="display:none"> Gerrit-Project: asterisk </div>
<div style="display:none"> Gerrit-Branch: 13 </div>
<div style="display:none"> Gerrit-MessageType: newchange </div>
<div style="display:none"> Gerrit-Change-Id: Id0ad65d14952a02fb213273a90f3f680a8149618 </div>
<div style="display:none"> Gerrit-Change-Number: 10270 </div>
<div style="display:none"> Gerrit-PatchSet: 1 </div>
<div style="display:none"> Gerrit-Owner: Alexei Gradinari <alex2grad@gmail.com> </div>