mirror of
https://github.com/asterisk/asterisk.git
synced 2025-09-03 03:20:57 +00:00
sorcery: Prevent duplicate objects and ensure missing objects are created on update
This patch resolves two issues in Sorcery objectset handling with multiple backends: 1. Prevent duplicate objects: When an object exists in more than one backend (e.g., a contact in both 'astdb' and 'realtime'), the objectset previously returned multiple instances of the same logical object. This caused logic failures in components like the PJSIP registrar, where duplicate contact entries led to overcounting and incorrect deletions, when max_contacts=1 and remove_existing=yes. This patch ensures only one instance of an object with a given key is added to the objectset, avoiding these duplicate-related side effects. 2. Ensure missing objects are created: When using multiple writable backends, a temporary backend failure can lead to objects missing permanently from that backend. Currently, .update() silently fails if the object is not present, and no .create() is attempted. This results in inconsistent state across backends (e.g. astdb vs. realtime). This patch introduces a new global option in sorcery.conf: [general] update_or_create_on_update_miss = yes|no Default: no (preserves existing behavior). When enabled: if .update() fails with no data found, .create() is attempted in that backend. This ensures that objects missing due to temporary backend outages are re-synchronized once the backend is available again. Added a new CLI command: sorcery show settings Displays global Sorcery settings, including the current value of update_or_create_on_update_miss. Updated tests to validate both flag enabled/disabled behavior. Fixes: #1289 UserNote: Users relying on Sorcery multiple writable backends configurations (e.g., astdb + realtime) may now enable update_or_create_on_update_miss = yes in sorcery.conf to ensure missing objects are recreated after temporary backend failures. Default behavior remains unchanged unless explicitly enabled.
This commit is contained in:
@@ -373,7 +373,7 @@ static int sorcery_astdb_update(const struct ast_sorcery *sorcery, void *data, v
|
||||
snprintf(family, sizeof(family), "%s/%s", prefix, ast_sorcery_object_get_type(object));
|
||||
|
||||
/* It is okay for the value to be truncated, we are only checking that it exists */
|
||||
if (ast_db_get(family, ast_sorcery_object_get_id(object), value, sizeof(value))) {
|
||||
if (ast_db_get(family, ast_sorcery_object_get_id(object), value, sizeof(value)) && !ast_sorcery_update_or_create_on_update_miss) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
|
@@ -229,12 +229,17 @@ static int sorcery_memory_update(const struct ast_sorcery *sorcery, void *data,
|
||||
|
||||
ao2_lock(data);
|
||||
|
||||
if (!(existing = ao2_find(data, ast_sorcery_object_get_id(object), OBJ_KEY | OBJ_UNLINK))) {
|
||||
if (!(existing = ao2_find(data, ast_sorcery_object_get_id(object), OBJ_KEY | OBJ_UNLINK)) && !ast_sorcery_update_or_create_on_update_miss) {
|
||||
ao2_unlock(data);
|
||||
return -1;
|
||||
}
|
||||
|
||||
ao2_link(data, object);
|
||||
if (existing) {
|
||||
ao2_link(data, object);
|
||||
} else {
|
||||
/* Not found: only create if the global flag is enabled */
|
||||
ao2_link_flags(data, object, OBJ_NOLOCK);
|
||||
}
|
||||
|
||||
ao2_unlock(data);
|
||||
|
||||
|
@@ -286,12 +286,37 @@ static int sorcery_realtime_update(const struct ast_sorcery *sorcery, void *data
|
||||
{
|
||||
struct sorcery_config *config = data;
|
||||
RAII_VAR(struct ast_variable *, fields, ast_sorcery_objectset_create(sorcery, object), ast_variables_destroy);
|
||||
int ret;
|
||||
struct ast_variable *id;
|
||||
|
||||
if (!fields) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
return (ast_update_realtime_fields(config->family, UUID_FIELD, ast_sorcery_object_get_id(object), fields) < 0) ? -1 : 0;
|
||||
ret = ast_update_realtime_fields(config->family, UUID_FIELD, ast_sorcery_object_get_id(object), fields);
|
||||
if (ret < 0) {
|
||||
/* An error occurred */
|
||||
return -1;
|
||||
} else if (ret > 0) {
|
||||
/* The object was updated */
|
||||
return 0;
|
||||
}
|
||||
|
||||
if (!ast_sorcery_update_or_create_on_update_miss) {
|
||||
/* The object does not exist (nothing was updated) and fallback disabled */
|
||||
return -1;
|
||||
}
|
||||
|
||||
id = ast_variable_new(UUID_FIELD, ast_sorcery_object_get_id(object), "");
|
||||
if (!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;
|
||||
}
|
||||
|
||||
static int sorcery_realtime_delete(const struct ast_sorcery *sorcery, void *data, void *object)
|
||||
|
Reference in New Issue
Block a user