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
This commit is contained in:
parent
0ad987168e
commit
ab90479642
|
@ -990,35 +990,83 @@ static int cli_filter_contacts(void *obj, void *arg, int flags)
|
|||
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) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
wrapper->contact = ao2_bump(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(®exbuf, 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, ®exbuf);
|
||||
ao2_callback(contacts_container, OBJ_UNLINK | OBJ_MULTIPLE | OBJ_NODATA, cli_filter_contacts, ®exbuf);
|
||||
regfree(®exbuf);
|
||||
}
|
||||
|
||||
return child_container;
|
||||
return contacts_container;
|
||||
}
|
||||
|
||||
static void *cli_contact_retrieve_by_id(const char *id)
|
||||
|
|
Loading…
Reference in New Issue