asterisk/res/res_sorcery_realtime.c
George Joseph c948ce9651 sorcery/res_pjsip: Refactor for realtime performance
There were a number of places in the res_pjsip stack that were getting
all endpoints or all aors, and then filtering them locally.

A good example is pjsip_options which, on startup, retrieves all
endpoints, then the aors for those endpoints, then tests the aors to see
if the qualify_frequency is > 0.  One issue was that it never did
anything with the endpoints other than retrieve the aors so we probably
could have skipped a step and just retrieved all aors. But nevermind.

This worked reasonably well with local config files but with a realtime
backend and thousands of objects, this was a nightmare.  The issue
really boiled down to the fact that while realtime supports predicates
that are passed to the database engine, the non-realtime sorcery
backends didn't.

They do now.

The realtime engines have a scheme for doing simple comparisons. They
take in an ast_variable (or list) for matching, and the name of each
variable can contain an operator.  For instance, a name of
"qualify_frequency >" and a value of "0" would create a SQL predicate
that looks like "where qualify_frequency > '0'".  If there's no operator
after the name, the engines add an '=' so a simple name of
"qualify_frequency" and a value of "10" would return exact matches.

The non-realtime backends decide whether to include an object in a
result set by calling ast_sorcery_changeset_create on every object in
the internal container.  However, ast_sorcery_changeset_create only does
exact string matches though so a name of "qualify_frequency >" and a
value of "0" returns nothing because the literal "qualify_frequency >"
doesn't match any name in the objset set.

So, the real task was to create a generic string matcher that can take a
left value, operator and a right value and perform the match. To that
end, strings.c has a new ast_strings_match(left, operator, right)
function.  Left and right are the strings to operate on and the operator
can be a string containing any of the following: = (or NULL or ""), !=,
>, >=, <, <=, like or regex.  If the operator is like or regex, the
right string should be a %-pattern or a regex expression.  If both left
and right can be converted to float, then a numeric comparison is
performed, otherwise a string comparison is performed.

To use this new function on ast_variables, 2 new functions were added to
config.c.  One that compares 2 ast_variables, and one that compares 2
ast_variable lists.  The former is useful when you want to compare 2
ast_variables that happen to be in a list but don't want to traverse the
list.  The latter will traverse the right list and return true if all
the variables in it match the left list.

Now, the backends' fields_cmp functions call ast_variable_lists_match
instead of ast_sorcery_changeset_create and they can now process the
same syntax as the realtime engines.  The realtime backend just passes
the variable list unaltered to the engine.  The only gotcha is that
there's no common realtime engine support for regex so that's been noted
in the api docs for ast_sorcery_retrieve_by_fields.

Only one more change to sorcery was done...  A new config flag
"allow_unqualified_fetch" was added to reg_sorcery_realtime.
"no": ignore fetches if no predicate fields were supplied.
"error": same as no but emit an error. (good for testing)
"yes": allow (the default);
"warn": allow but emit a warning. (good for testing)

Now on to res_pjsip...

pjsip_options was modified to retrieve aors with qualify_frequency > 0
rather than all endpoints then all aors.  Not only was this a big
improvement in realtime retrieval but even for config files there's an
improvement because we're not going through endpoints anymore.

res_pjsip_mwi was modified to retieve only endpoints with something in
the mailboxes field instead of all endpoints then testing mailboxes.

res_pjsip_registrar_expire was completely refactored.  It was retrieving
all contacts then setting up scheduler entries to check for expiration.
Now, it's a single thread (like keepalive) that periodically retrieves
only contacts whose expiration time is < now and deletes them.  A new
contact_expiration_check_interval was added to global with a default of
30 seconds.

Ross Beer reports that with this patch, his Asterisk startup time dropped
from around an hour to under 30 seconds.

There are still objects that can't be filtered at the database like
identifies, transports, and registrations.  These are not going to be
anywhere near as numerous as endpoints, aors, auths, contacts however.

Back to allow_unqualified_fetch.  If this is set to yes and you have a
very large number of objects in the database, the pjsip CLI commands
will attempt to retrive ALL of them if not qualified with a LIKE.
Worse, if you type "pjsip show endpoint <tab>" guess what's going to
happen? :)  Having a cache helps but all the objects will have to be
retrieved at least once to fill the cache.  Setting
allow_unqualified_fetch=no prevents the mass retrieve and should be used
on endpoints, auths, aors, and contacts.  It should NOT be used for
identifies, registrations and transports since these MUST be
retrieved in bulk.

Example sorcery.conf:

[res_pjsip]
endpoint=config,pjsip.conf,criteria=type=endpoint
endpoint=realtime,ps_endpoints,allow_unqualified_fetch=error

ASTERISK-25826 #close
Reported-by: Ross Beer
Tested-by: Ross Beer

Change-Id: Id2691e447db90892890036e663aaf907b2dc1c67
2016-03-27 22:43:27 -05:00

363 lines
11 KiB
C

/*
* Asterisk -- An open source telephony toolkit.
*
* Copyright (C) 2013, Digium, Inc.
*
* Joshua Colp <jcolp@digium.com>
*
* See http://www.asterisk.org for more information about
* the Asterisk project. Please do not directly contact
* any of the maintainers of this project for assistance;
* the project provides a web site, mailing lists and IRC
* channels for your use.
*
* This program is free software, distributed under the terms of
* the GNU General Public License Version 2. See the LICENSE file
* at the top of the source tree.
*/
/*!
* \file
*
* \brief Sorcery Realtime Object Wizard
*
* \author Joshua Colp <jcolp@digium.com>
*/
/*** MODULEINFO
<support_level>core</support_level>
***/
#include "asterisk.h"
ASTERISK_REGISTER_FILE()
#include <regex.h>
#include "asterisk/module.h"
#include "asterisk/sorcery.h"
/*! \brief They key field used to store the unique identifier for the object */
#define UUID_FIELD "id"
enum unqualified_fetch {
UNQUALIFIED_FETCH_NO,
UNQUALIFIED_FETCH_WARN,
UNQUALIFIED_FETCH_YES,
UNQUALIFIED_FETCH_ERROR,
};
struct sorcery_config {
enum unqualified_fetch fetch;
char family[];
};
static void *sorcery_realtime_open(const char *data);
static int sorcery_realtime_create(const struct ast_sorcery *sorcery, void *data, void *object);
static void *sorcery_realtime_retrieve_id(const struct ast_sorcery *sorcery, void *data, const char *type, const char *id);
static void *sorcery_realtime_retrieve_fields(const struct ast_sorcery *sorcery, void *data, const char *type, const struct ast_variable *fields);
static void sorcery_realtime_retrieve_multiple(const struct ast_sorcery *sorcery, void *data, const char *type, struct ao2_container *objects,
const struct ast_variable *fields);
static void sorcery_realtime_retrieve_regex(const struct ast_sorcery *sorcery, void *data, const char *type, struct ao2_container *objects, const char *regex);
static int sorcery_realtime_update(const struct ast_sorcery *sorcery, void *data, void *object);
static int sorcery_realtime_delete(const struct ast_sorcery *sorcery, void *data, void *object);
static void sorcery_realtime_close(void *data);
static struct ast_sorcery_wizard realtime_object_wizard = {
.name = "realtime",
.open = sorcery_realtime_open,
.create = sorcery_realtime_create,
.retrieve_id = sorcery_realtime_retrieve_id,
.retrieve_fields = sorcery_realtime_retrieve_fields,
.retrieve_multiple = sorcery_realtime_retrieve_multiple,
.retrieve_regex = sorcery_realtime_retrieve_regex,
.update = sorcery_realtime_update,
.delete = sorcery_realtime_delete,
.close = sorcery_realtime_close,
};
static int sorcery_realtime_create(const struct ast_sorcery *sorcery, void *data, void *object)
{
struct sorcery_config *config = data;
RAII_VAR(struct ast_variable *, fields, ast_sorcery_objectset_create(sorcery, object), ast_variables_destroy);
struct ast_variable *id = ast_variable_new(UUID_FIELD, ast_sorcery_object_get_id(object), "");
if (!fields || !id) {
ast_variables_destroy(id);
return -1;
}
/* Place the identifier at the front for sanity sake */
id->next = fields;
fields = id;
return (ast_store_realtime_fields(config->family, fields) <= 0) ? -1 : 0;
}
/*! \brief Internal helper function which returns a filtered objectset.
*
* The following are filtered out of the objectset:
* \li The id field. This is returned to the caller in an out parameter.
* \li Fields that are not registered with sorcery.
*
* \param objectset Objectset to filter.
* \param[out] id The ID of the sorcery object, as found in the objectset.
* \param sorcery The sorcery instance that is requesting an objectset.
* \param type The object type
*
* \return The filtered objectset
*/
static struct ast_variable *sorcery_realtime_filter_objectset(struct ast_variable *objectset, struct ast_variable **id,
const struct ast_sorcery *sorcery, const char *type)
{
struct ast_variable *previous = NULL, *field = objectset;
struct ast_sorcery_object_type *object_type;
object_type = ast_sorcery_get_object_type(sorcery, type);
if (!object_type) {
ast_log(LOG_WARNING, "Unknown sorcery object type %s. Expect errors\n", type);
/* Continue since we still want to filter out the id */
}
while (field) {
int remove_field = 0;
int delete_field = 0;
if (!strcmp(field->name, UUID_FIELD)) {
*id = field;
remove_field = 1;
} else if (object_type &&
!ast_sorcery_is_object_field_registered(object_type, field->name)) {
ast_debug(1, "Filtering out realtime field '%s' from retrieval\n", field->name);
remove_field = 1;
delete_field = 1;
}
if (remove_field) {
struct ast_variable *removed;
if (previous) {
previous->next = field->next;
} else {
objectset = field->next;
}
removed = field;
field = field->next;
removed->next = NULL;
if (delete_field) {
ast_variables_destroy(removed);
}
} else {
previous = field;
field = field->next;
}
}
ao2_cleanup(object_type);
return objectset;
}
static void *sorcery_realtime_retrieve_fields(const struct ast_sorcery *sorcery, void *data, const char *type, const struct ast_variable *fields)
{
struct sorcery_config *config = data;
RAII_VAR(struct ast_variable *, objectset, NULL, ast_variables_destroy);
RAII_VAR(struct ast_variable *, id, NULL, ast_variables_destroy);
void *object = NULL;
if (!(objectset = ast_load_realtime_fields(config->family, fields))) {
return NULL;
}
objectset = sorcery_realtime_filter_objectset(objectset, &id, sorcery, type);
if (!id
|| !(object = ast_sorcery_alloc(sorcery, type, id->value))
|| ast_sorcery_objectset_apply(sorcery, object, objectset)) {
return NULL;
}
return object;
}
static void *sorcery_realtime_retrieve_id(const struct ast_sorcery *sorcery, void *data, const char *type, const char *id)
{
RAII_VAR(struct ast_variable *, fields, ast_variable_new(UUID_FIELD, id, ""), ast_variables_destroy);
return sorcery_realtime_retrieve_fields(sorcery, data, type, fields);
}
static void sorcery_realtime_retrieve_multiple(const struct ast_sorcery *sorcery, void *data, const char *type, struct ao2_container *objects, const struct ast_variable *fields)
{
struct sorcery_config *config = data;
RAII_VAR(struct ast_config *, rows, NULL, ast_config_destroy);
RAII_VAR(struct ast_variable *, all, NULL, ast_variables_destroy);
struct ast_category *row = NULL;
if (!fields) {
char field[strlen(UUID_FIELD) + 6], value[2];
if (config->fetch == UNQUALIFIED_FETCH_NO) {
return;
}
if (config->fetch == UNQUALIFIED_FETCH_ERROR) {
ast_log(LOG_ERROR, "Unqualified fetch prevented on %s\n", config->family);
return;
}
if (config->fetch == UNQUALIFIED_FETCH_WARN) {
ast_log(LOG_WARNING, "Unqualified fetch attempted on %s\n", config->family);
return;
}
/* If no fields have been specified we want all rows, so trick realtime into doing it */
snprintf(field, sizeof(field), "%s LIKE", UUID_FIELD);
snprintf(value, sizeof(value), "%%");
if (!(all = ast_variable_new(field, value, ""))) {
return;
}
fields = all;
}
if (!(rows = ast_load_realtime_multientry_fields(config->family, fields))) {
return;
}
while ((row = ast_category_browse_filtered(rows, NULL, row, NULL))) {
struct ast_variable *objectset = ast_category_detach_variables(row);
RAII_VAR(struct ast_variable *, id, NULL, ast_variables_destroy);
RAII_VAR(void *, object, NULL, ao2_cleanup);
objectset = sorcery_realtime_filter_objectset(objectset, &id, sorcery, type);
if (id && (object = ast_sorcery_alloc(sorcery, type, id->value)) && !ast_sorcery_objectset_apply(sorcery, object, objectset)) {
ao2_link(objects, object);
}
ast_variables_destroy(objectset);
}
}
static void sorcery_realtime_retrieve_regex(const struct ast_sorcery *sorcery, void *data, const char *type, struct ao2_container *objects, const char *regex)
{
char field[strlen(UUID_FIELD) + 6], value[strlen(regex) + 3];
RAII_VAR(struct ast_variable *, fields, NULL, ast_variables_destroy);
if (!ast_strlen_zero(regex)) {
/* The realtime API provides no direct ability to do regex so for now we support a limited subset using pattern matching */
snprintf(field, sizeof(field), "%s LIKE", UUID_FIELD);
if (regex[0] == '^') {
snprintf(value, sizeof(value), "%s%%", regex + 1);
} else {
snprintf(value, sizeof(value), "%%%s%%", regex);
}
if (!(fields = ast_variable_new(field, value, ""))) {
return;
}
}
sorcery_realtime_retrieve_multiple(sorcery, data, type, objects, fields);
}
static int sorcery_realtime_update(const struct ast_sorcery *sorcery, void *data, void *object)
{
struct sorcery_config *config = data;
RAII_VAR(struct ast_variable *, fields, ast_sorcery_objectset_create(sorcery, object), ast_variables_destroy);
if (!fields) {
return -1;
}
return (ast_update_realtime_fields(config->family, UUID_FIELD, ast_sorcery_object_get_id(object), fields) <= 0) ? -1 : 0;
}
static int sorcery_realtime_delete(const struct ast_sorcery *sorcery, void *data, void *object)
{
struct sorcery_config *config = data;
return (ast_destroy_realtime_fields(config->family, UUID_FIELD, ast_sorcery_object_get_id(object), NULL) <= 0) ? -1 : 0;
}
static void *sorcery_realtime_open(const char *data)
{
struct sorcery_config *config;
char *tmp;
char *family;
char *option;
/* We require a prefix for family string generation, or else stuff could mix together */
if (ast_strlen_zero(data)) {
return NULL;
}
tmp = ast_strdupa(data);
family = strsep(&tmp, ",");
if (!ast_realtime_is_mapping_defined(family)) {
return NULL;
}
config = ast_calloc(1, sizeof(*config) + strlen(family) + 1);
if (!config) {
return NULL;
}
strcpy(config->family, family); /* Safe */
config->fetch = UNQUALIFIED_FETCH_YES;
while ((option = strsep(&tmp, ","))) {
char *name = strsep(&option, "=");
char *value = option;
if (!strcasecmp(name, "allow_unqualified_fetch")) {
if (ast_strlen_zero(value) || !strcasecmp(value, "yes")) {
config->fetch = UNQUALIFIED_FETCH_YES;
} else if (!strcasecmp(value, "no")) {
config->fetch = UNQUALIFIED_FETCH_NO;
} else if (!strcasecmp(value, "warn")) {
config->fetch = UNQUALIFIED_FETCH_WARN;
} else if (!strcasecmp(value, "error")) {
config->fetch = UNQUALIFIED_FETCH_ERROR;
} else {
ast_log(LOG_ERROR, "Unrecognized value in %s:%s: '%s'\n", family, name, value);
return NULL;
}
} else {
ast_log(LOG_ERROR, "Unrecognized option in %s: '%s'\n", family, name);
return NULL;
}
}
return config;
}
static void sorcery_realtime_close(void *data)
{
ast_free(data);
}
static int load_module(void)
{
if (ast_sorcery_wizard_register(&realtime_object_wizard)) {
return AST_MODULE_LOAD_DECLINE;
}
return AST_MODULE_LOAD_SUCCESS;
}
static int unload_module(void)
{
ast_sorcery_wizard_unregister(&realtime_object_wizard);
return 0;
}
AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_GLOBAL_SYMBOLS | AST_MODFLAG_LOAD_ORDER, "Sorcery Realtime Object Wizard",
.support_level = AST_MODULE_SUPPORT_CORE,
.load = load_module,
.unload = unload_module,
.load_pri = AST_MODPRI_REALTIME_DRIVER,
);