summaryrefslogtreecommitdiff
path: root/include/asterisk
diff options
context:
space:
mode:
authorGeorge Joseph <george.joseph@fairview5.com>2016-03-08 14:55:30 -0700
committerGeorge Joseph <george.joseph@fairview5.com>2016-03-27 22:43:27 -0500
commitc948ce96512b7948595c9416e6739e0526686529 (patch)
tree08af410650ff0c3f5096a8dd066a70d0b2f0bc1e /include/asterisk
parent77a9272431296772f5930301175bc0832076ac3e (diff)
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
Diffstat (limited to 'include/asterisk')
-rw-r--r--include/asterisk/config.h81
-rw-r--r--include/asterisk/res_pjsip.h8
-rw-r--r--include/asterisk/sorcery.h24
-rw-r--r--include/asterisk/strings.h30
4 files changed, 140 insertions, 3 deletions
diff --git a/include/asterisk/config.h b/include/asterisk/config.h
index 9a899d8d0..287635a8e 100644
--- a/include/asterisk/config.h
+++ b/include/asterisk/config.h
@@ -307,7 +307,7 @@ const char *ast_variable_retrieve(struct ast_config *config,
const char *category, const char *variable);
/*!
- * \brief Gets a variable from a specific category structure
+ * \brief Gets a variable value from a specific category structure by name
*
* \param category category structure under which the variable lies
* \param variable which variable you wish to get the data for
@@ -321,7 +321,7 @@ const char *ast_variable_retrieve(struct ast_config *config,
const char *ast_variable_find(const struct ast_category *category, const char *variable);
/*!
- * \brief Gets a variable from a variable list
+ * \brief Gets the value of a variable from a variable list by name
*
* \param list variable list to search
* \param variable which variable you wish to get the data for
@@ -335,7 +335,7 @@ const char *ast_variable_find(const struct ast_category *category, const char *v
const char *ast_variable_find_in_list(const struct ast_variable *list, const char *variable);
/*!
- * \brief Gets the LAST occurrence of a variable from a variable list
+ * \brief Gets the value of the LAST occurrence of a variable from a variable list
*
* \param list The ast_variable list to search
* \param variable The name of the ast_variable you wish to fetch data for
@@ -352,6 +352,21 @@ const char *ast_variable_find_in_list(const struct ast_variable *list, const cha
const char *ast_variable_find_last_in_list(const struct ast_variable *list, const char *variable);
/*!
+ * \brief Gets a variable from a variable list by name
+ * \since 13.9.0
+ *
+ * \param list variable list to search
+ * \param variable name you wish to get the data for
+ *
+ * \details
+ * Goes through a given variable list and searches for the given variable
+ *
+ * \retval The variable (not the value) on success
+ * \retval NULL if unable to find it.
+ */
+const struct ast_variable *ast_variable_find_variable_in_list(const struct ast_variable *list, const char *variable_name);
+
+/*!
* \brief Retrieve a category if it exists
*
* \param config which config to use
@@ -1217,6 +1232,66 @@ char *ast_realtime_decode_chunk(char *chunk);
*/
char *ast_realtime_encode_chunk(struct ast_str **dest, ssize_t maxlen, const char *chunk);
+/*!
+ * \brief Tests 2 variable values to see if they match
+ * \since 13.9.0
+ *
+ * \param left Variable to test
+ * \param right Variable to match against with an optional realtime-style operator in the name
+ *
+ * \retval 1 matches
+ * \retval 0 doesn't match
+ *
+ * \details
+ *
+ * The values of the variables are passed to ast_strings_match.
+ * If right->name is suffixed with a space and an operator, that operator
+ * is also passed to ast_strings_match.
+ *
+ * Examples:
+ *
+ * left->name = "id" (ignored)
+ * left->value = "abc"
+ * right->name = "id regex" (id is ignored)
+ * right->value = "a[bdef]c"
+ *
+ * will result in ast_strings_match("abc", "regex", "a[bdef]c") which will return 1.
+ *
+ * left->name = "id" (ignored)
+ * left->value = "abc"
+ * right->name = "id" (ignored)
+ * right->value = "abc"
+ *
+ * will result in ast_strings_match("abc", NULL, "abc") which will return 1.
+ *
+ * See the documentation for ast_strings_match for the valid operators.
+ */
+int ast_variables_match(const struct ast_variable *left, const struct ast_variable *right);
+
+/*!
+ * \brief Tests 2 variable lists to see if they match
+ * \since 13.9.0
+ *
+ * \param left Variable list to test
+ * \param right Variable list with an optional realtime-style operator in the names
+ * \param exact_match If true, all variables in left must match all variables in right
+ * and vice versa. This does exact value matches only. Operators aren't supported.
+ * Except for order, the left and right lists must be equal.
+ *
+ * If false, every variable in the right list must match some variable in the left list
+ * using the operators supplied. Variables in the left list that aren't in the right
+ * list are ignored for matching purposes.
+ *
+ * \retval 1 matches
+ * \retval 0 doesn't match
+ *
+ * \details
+ * Iterates over the variable lists calling ast_variables_match. If any match fails
+ * or a variable in the right list isn't in the left list, 0 is returned.
+ */
+int ast_variable_lists_match(const struct ast_variable *left, const struct ast_variable *right,
+ int exact_match);
+
#if defined(__cplusplus) || defined(c_plusplus)
}
#endif
diff --git a/include/asterisk/res_pjsip.h b/include/asterisk/res_pjsip.h
index 66370186a..b0ae2cefa 100644
--- a/include/asterisk/res_pjsip.h
+++ b/include/asterisk/res_pjsip.h
@@ -2147,6 +2147,14 @@ void ast_sip_get_default_from_user(char *from_user, size_t size);
unsigned int ast_sip_get_keep_alive_interval(void);
/*!
+ * \brief Retrieve the system contact expiration check interval setting.
+ *
+ * \retval the contact expiration check interval.
+ */
+unsigned int ast_sip_get_contact_expiration_check_interval(void);
+
+
+/*!
* \brief Retrieve the system max initial qualify time.
*
* \retval the maximum initial qualify time.
diff --git a/include/asterisk/sorcery.h b/include/asterisk/sorcery.h
index d681ebb5a..42ecc133f 100644
--- a/include/asterisk/sorcery.h
+++ b/include/asterisk/sorcery.h
@@ -1151,6 +1151,7 @@ void *ast_sorcery_retrieve_by_id(const struct ast_sorcery *sorcery, const char *
/*!
* \brief Retrieve an object or multiple objects using specific fields
+ * \since 13.9.0
*
* \param sorcery Pointer to a sorcery structure
* \param type Type of object to retrieve
@@ -1165,6 +1166,29 @@ void *ast_sorcery_retrieve_by_id(const struct ast_sorcery *sorcery, const char *
*
* \note If the AST_RETRIEVE_FLAG_ALL flag is used you may omit fields to retrieve all objects
* of the given type.
+ *
+ * \note The fields parameter can contain realtime-style expressions in variable->name.
+ * All operators defined for ast_strings_match can be used except for regex as
+ * there's no common support for regex in the realtime backends at this time.
+ * If multiple variables are in the fields list, all must match for an object to
+ * be returned. See ast_strings_match for more information.
+ *
+ * Example:
+ *
+ * The following code can be significantly faster when a realtime backend is in use
+ * because the expression "qualify_frequency > 0" is passed to the database to limit
+ * the number of rows returned.
+ *
+ * struct ast_variable *var = ast_variable_new("qualify_frequency >", "0", "");
+ * struct ao2_container *aors;
+ *
+ * if (!var) {
+ * return;
+ * }
+ *
+ * aors = ast_sorcery_retrieve_by_fields(ast_sip_get_sorcery(),
+ * "aor", AST_RETRIEVE_FLAG_MULTIPLE, var);
+ *
*/
void *ast_sorcery_retrieve_by_fields(const struct ast_sorcery *sorcery, const char *type, unsigned int flags, struct ast_variable *fields);
diff --git a/include/asterisk/strings.h b/include/asterisk/strings.h
index 3701b5305..0e2f69ba8 100644
--- a/include/asterisk/strings.h
+++ b/include/asterisk/strings.h
@@ -1335,4 +1335,34 @@ void ast_str_container_remove(struct ao2_container *str_container, const char *r
* \return A pointer to buf
*/
char *ast_generate_random_string(char *buf, size_t size);
+
+/*!
+ * \brief Compares 2 strings using realtime-style operators
+ * \since 13.9.0
+ *
+ * \param left The left side of the equation
+ * \param op The operator to apply
+ * \param right The right side of the equation
+ *
+ * \retval 1 matches
+ * \retval 0 doesn't match
+ *
+ * \details
+ *
+ * Operators:
+ * "=", "!=", "<", "<=", ">", ">=":
+ * If both left and right can be converted to float, then they will be
+ * compared as such. Otherwise the result will be derived from strcmp(left, right).
+ * "regex":
+ * The right value will be compiled as a regular expression and matched against the left
+ * value.
+ * "like":
+ * Any '%' character in the right value will be converted to '.*' and the resulting
+ * string will be handled as a regex.
+ * NULL , "":
+ * If the right value starts and ends with a '/' then it will be processed as a regex.
+ * Otherwise, same as "=".
+ */
+int ast_strings_match(const char *left, const char *op, const char *right);
+
#endif /* _ASTERISK_STRINGS_H */