sorcery: Add support for more intelligent reloading.

Some sorcery objects actually contain dynamic content
that can change despite the underlying configuration
itself not changing. A good example of this is the
res_pjsip_endpoint_identifier_ip module which allows
specifying hostnames. While the configuration may not
change between reloads the DNS information of the
hostnames can.

This change adds the ability for a sorcery object to be
marked as having dynamic contents which is then taken
into account when reloading by the sorcery file based
config module. If there is an object with dynamic content
then a reload will be forced while if there are none
then the existing behavior of not reloading occurs.

ASTERISK-29321

Change-Id: I9342dc55be46cc00204533c266a68d972760a0b1
This commit is contained in:
Joshua C. Colp 2021-03-01 17:35:20 -04:00 committed by Joshua Colp
parent 607603cf89
commit 304f8ddfb2
4 changed files with 53 additions and 1 deletions

View File

@ -1558,6 +1558,28 @@ const char *ast_sorcery_object_get_extended(const void *object, const char *name
*/
int ast_sorcery_object_set_extended(const void *object, const char *name, const char *value);
/*!
* \brief Get whether an object contains dynamic contents or not
*
* \param object Pointer to a sorcery object
*
* \since 19
* \since 18.3.0
* \since 16.17.0
*/
unsigned int ast_sorcery_object_has_dynamic_contents(const void *object);
/*!
* \brief Set the dynamic contents flag on a sorcery object
*
* \param object Pointer to a sorcery object
*
* \since 19
* \since 18.3.0
* \since 16.17.0
*/
void ast_sorcery_object_set_has_dynamic_contents(const void *object);
/*!
* \brief ao2 object comparator based on sorcery id.
*/

View File

@ -139,6 +139,9 @@ struct ast_sorcery_object {
/*! \brief Time that the object was created */
struct timeval created;
/*! \brief Whether this object has dynamic contents or not */
unsigned int has_dynamic_contents:1;
};
/*! \brief Structure for registered object type */
@ -2366,6 +2369,20 @@ int ast_sorcery_object_set_extended(const void *object, const char *name, const
return 0;
}
unsigned int ast_sorcery_object_has_dynamic_contents(const void *object)
{
const struct ast_sorcery_object_details *details = object;
return details->object->has_dynamic_contents;
}
void ast_sorcery_object_set_has_dynamic_contents(const void *object)
{
const struct ast_sorcery_object_details *details = object;
details->object->has_dynamic_contents = 1;
}
int ast_sorcery_observer_add(const struct ast_sorcery *sorcery, const char *type, const struct ast_sorcery_observer *callbacks)
{
RAII_VAR(struct ast_sorcery_object_type *, object_type, ao2_find(sorcery->types, type, OBJ_KEY), ao2_cleanup);

View File

@ -515,6 +515,9 @@ static int ip_identify_apply(const struct ast_sorcery *sorcery, void *obj)
return 0;
}
/* Hosts can produce dynamic content, so mark the identify as such */
ast_sorcery_object_set_has_dynamic_contents(obj);
/* Resolve the match addresses now */
i = ao2_iterator_init(identify->hosts, 0);
while ((current_string = ao2_iterator_next(&i))) {

View File

@ -65,6 +65,9 @@ struct sorcery_config {
/*! \brief Configuration is invalid in some way, force reload */
unsigned int configuration_invalid:1;
/*! \brief Configuration contains at least one object with dynamic contents */
unsigned int has_dynamic_contents:1;
/*! \brief Filename of the configuration file */
char filename[];
};
@ -313,12 +316,13 @@ static int sorcery_is_configuration_met(const struct ast_sorcery *sorcery, const
static void sorcery_config_internal_load(void *data, const struct ast_sorcery *sorcery, const char *type, unsigned int reload)
{
struct sorcery_config *config = data;
struct ast_flags flags = { reload && !config->configuration_invalid ? CONFIG_FLAG_FILEUNCHANGED : 0 };
struct ast_flags flags = { reload && !config->configuration_invalid && !config->has_dynamic_contents ? CONFIG_FLAG_FILEUNCHANGED : 0 };
struct ast_config *cfg = ast_config_load2(config->filename, config->uuid, flags);
struct ast_category *category = NULL;
RAII_VAR(struct ao2_container *, objects, NULL, ao2_cleanup);
const char *id = NULL;
unsigned int buckets = 0;
unsigned int has_dynamic_contents = 0;
if (!cfg) {
ast_log(LOG_ERROR, "Unable to load config file '%s'\n", config->filename);
@ -430,9 +434,15 @@ static void sorcery_config_internal_load(void *data, const struct ast_sorcery *s
ast_log(LOG_NOTICE, "Retaining existing configuration for object of type '%s' with id '%s'\n", type, id);
}
/* We store the dynamic contents state until the end in case this reload or load
* gets rolled back.
*/
has_dynamic_contents |= ast_sorcery_object_has_dynamic_contents(obj);
ao2_link(objects, obj);
}
config->has_dynamic_contents = has_dynamic_contents;
ao2_global_obj_replace_unref(config->objects, objects);
ast_config_destroy(cfg);
}