diff options
author | Andris Zeila <andris.zeila@zabbix.com> | 2022-01-21 18:04:31 +0300 |
---|---|---|
committer | Andris Zeila <andris.zeila@zabbix.com> | 2022-01-21 18:04:31 +0300 |
commit | 7c244a3137ff3e7ca95d8eeefb66af9fc0428153 (patch) | |
tree | bae0b1884393e1928020536a1905579c9a967ebd /src | |
parent | d8edaf13526317a33148934b24423dce1b5d1017 (diff) |
........S. [ZBX-19961] fixed item-trigger update when removing triggers
Diffstat (limited to 'src')
-rw-r--r-- | src/libs/zbxdbcache/dbconfig.c | 220 | ||||
-rw-r--r-- | src/libs/zbxdbcache/dbconfig.h | 2 | ||||
-rw-r--r-- | src/libs/zbxdbcache/dbconfig_dump.c | 10 |
3 files changed, 165 insertions, 67 deletions
diff --git a/src/libs/zbxdbcache/dbconfig.c b/src/libs/zbxdbcache/dbconfig.c index df6b710566f..320c3e7821f 100644 --- a/src/libs/zbxdbcache/dbconfig.c +++ b/src/libs/zbxdbcache/dbconfig.c @@ -102,6 +102,7 @@ extern int CONFIG_TIMER_FORKS; ZBX_MEM_FUNC_IMPL(__config, config_mem) static void dc_maintenance_precache_nested_groups(void); +static void dc_item_reset_triggers(ZBX_DC_ITEM *item); /* by default the macro environment is non-secure and all secret macros are masked with ****** */ static unsigned char macro_env = ZBX_MACRO_ENV_NONSECURE; @@ -3706,17 +3707,14 @@ static void DCsync_triggers(zbx_dbsync_t *sync) trigger->recovery_expression_bin = config_decode_serialized_expression(row[17]); trigger->timer = atoi(row[18]); trigger->revision = config->sync_start_ts; + trigger->itemids = NULL; } /* remove deleted triggers from buffer */ if (SUCCEED == ret) { - zbx_vector_uint64_t functionids; - int i; - ZBX_DC_ITEM *item; - ZBX_DC_FUNCTION *function; - - zbx_vector_uint64_create(&functionids); + ZBX_DC_ITEM *item; + zbx_uint64_t *itemid; for (; SUCCEED == ret; ret = zbx_dbsync_next(sync, &rowid, &row, &tag)) { @@ -3724,36 +3722,14 @@ static void DCsync_triggers(zbx_dbsync_t *sync) continue; /* force trigger list update for items used in removed trigger */ - - if (NULL != trigger->expression_bin) + if (NULL != trigger->itemids) { - zbx_get_serialized_expression_functionids(trigger->expression, trigger->expression_bin, - &functionids); - } - - if (TRIGGER_RECOVERY_MODE_RECOVERY_EXPRESSION == trigger->recovery_mode && - NULL != trigger->recovery_expression_bin) - { - zbx_get_serialized_expression_functionids(trigger->recovery_expression, - trigger->recovery_expression_bin, &functionids); - } - - for (i = 0; i < functionids.values_num; i++) - { - if (NULL == (function = (ZBX_DC_FUNCTION *)zbx_hashset_search(&config->functions, &functionids.values[i]))) - continue; - - if (NULL == (item = (ZBX_DC_ITEM *)zbx_hashset_search(&config->items, &function->itemid))) - continue; - - item->update_triggers = 1; - if (NULL != item->triggers) + for (itemid = trigger->itemids; 0 != *itemid; itemid++) { - config->items.mem_free_func(item->triggers); - item->triggers = NULL; + if (NULL != (item = (ZBX_DC_ITEM *)zbx_hashset_search(&config->items, itemid))) + dc_item_reset_triggers(item); } } - zbx_vector_uint64_clear(&functionids); zbx_strpool_release(trigger->description); zbx_strpool_release(trigger->expression); @@ -3772,7 +3748,6 @@ static void DCsync_triggers(zbx_dbsync_t *sync) zbx_hashset_remove_direct(&config->triggers, trigger); } - zbx_vector_uint64_destroy(&functionids); } zabbix_log(LOG_LEVEL_DEBUG, "End of %s()", __func__); @@ -4258,14 +4233,7 @@ static void DCsync_functions(zbx_dbsync_t *sync) ZBX_DC_ITEM *item_last; if (NULL != (item_last = zbx_hashset_search(&config->items, &function->itemid))) - { - item_last->update_triggers = 1; - if (NULL != item_last->triggers) - { - config->items.mem_free_func(item_last->triggers); - item_last->triggers = NULL; - } - } + dc_item_reset_triggers(item_last); } } else @@ -4279,9 +4247,7 @@ static void DCsync_functions(zbx_dbsync_t *sync) function->type = zbx_get_function_type(function->function); function->revision = config->sync_start_ts; - item->update_triggers = 1; - if (NULL != item->triggers) - item->triggers[0] = NULL; + dc_item_reset_triggers(item); } for (; SUCCEED == ret; ret = zbx_dbsync_next(sync, &rowid, &row, &tag)) @@ -4290,14 +4256,7 @@ static void DCsync_functions(zbx_dbsync_t *sync) continue; if (NULL != (item = (ZBX_DC_ITEM *)zbx_hashset_search(&config->items, &function->itemid))) - { - item->update_triggers = 1; - if (NULL != item->triggers) - { - config->items.mem_free_func(item->triggers); - item->triggers = NULL; - } - } + dc_item_reset_triggers(item); zbx_strpool_release(function->function); zbx_strpool_release(function->parameter); @@ -5760,6 +5719,103 @@ static int zbx_default_ptr_pair_ptr_compare_func(const void *d1, const void *d2) return 0; } +static int zbx_default_ptr_pair_ptr_second_compare_func(const void *d1, const void *d2) +{ + const zbx_ptr_pair_t *p1 = (const zbx_ptr_pair_t *)d1; + const zbx_ptr_pair_t *p2 = (const zbx_ptr_pair_t *)d2; + + ZBX_RETURN_IF_NOT_EQUAL(p1->second, p2->second); + ZBX_RETURN_IF_NOT_EQUAL(p1->first, p2->first); + + return 0; +} + +/****************************************************************************** + * * + * Function: dc_trigger_merge_itemids * + * * + * Purpose: merges new itemids into trigger itemids array * + * * + ******************************************************************************/ +static void dc_trigger_merge_itemids(ZBX_DC_TRIGGER *trigger, zbx_vector_uint64_t *itemids) +{ + zbx_uint64_t *itemid; + int i; + + if (NULL != trigger->itemids) + { + for (itemid = trigger->itemids; 0 != *itemid; itemid++) + { + if (FAIL != (i = zbx_vector_uint64_search(itemids, *itemid, ZBX_DEFAULT_UINT64_COMPARE_FUNC))) + zbx_vector_uint64_remove_noorder(itemids, i); + } + + if (0 == itemids->values_num) + return; + + trigger->itemids = (zbx_uint64_t *)__config_mem_realloc_func(trigger->itemids, + sizeof(zbx_uint64_t) * (size_t)(itemids->values_num + 1)); + } + else + { + trigger->itemids = (zbx_uint64_t *)__config_mem_malloc_func(trigger->itemids, + sizeof(zbx_uint64_t) * (size_t)(itemids->values_num + 1)); + trigger->itemids[0] = 0; + } + + for (itemid = trigger->itemids; 0 != *itemid; itemid++) + ; + + for (i = 0; i < itemids->values_num; i++) + *itemid++ = itemids->values[i]; + + *itemid = 0; +} + +/****************************************************************************** + * * + * Function: dc_item_reset_triggers * + * * + * Purpose: reset item trigger links and remove corresponding itemids from * + * affected triggers * + * * + ******************************************************************************/ +static void dc_item_reset_triggers(ZBX_DC_ITEM *item) +{ + ZBX_DC_TRIGGER **trigger; + + item->update_triggers = 1; + + if (NULL == item->triggers) + return; + + for (trigger = item->triggers; NULL != *trigger; trigger++) + { + zbx_uint64_t *itemid; + + if (NULL != (*trigger)->itemids) + { + for (itemid = (*trigger)->itemids; 0 != *itemid; itemid++) + { + if (item->itemid == *itemid) + { + do + { + *itemid = itemid[1]; + itemid++; + } + while (0 != *itemid); + + break; + } + } + } + } + + config->items.mem_free_func(item->triggers); + item->triggers = NULL; +} + /****************************************************************************** * * * Function: dc_trigger_update_cache * @@ -5819,28 +5875,58 @@ static void dc_trigger_update_cache(void) } } - zbx_vector_ptr_pair_sort(&itemtrigs, zbx_default_ptr_pair_ptr_compare_func); - zbx_vector_ptr_pair_uniq(&itemtrigs, zbx_default_ptr_pair_ptr_compare_func); - - /* update links from items to triggers */ - for (i = 0; i < itemtrigs.values_num; i++) + if (0 != itemtrigs.values_num) { - for (j = i + 1; j < itemtrigs.values_num; j++) + zbx_vector_uint64_t itemids; + + zbx_vector_ptr_pair_sort(&itemtrigs, zbx_default_ptr_pair_ptr_compare_func); + zbx_vector_ptr_pair_uniq(&itemtrigs, zbx_default_ptr_pair_ptr_compare_func); + + /* update links from items to triggers */ + for (i = 0; i < itemtrigs.values_num; i++) { - if (itemtrigs.values[i].first != itemtrigs.values[j].first) - break; + for (j = i + 1; j < itemtrigs.values_num; j++) + { + if (itemtrigs.values[i].first != itemtrigs.values[j].first) + break; + } + + item = (ZBX_DC_ITEM *)itemtrigs.values[i].first; + item->update_triggers = 0; + item->triggers = (ZBX_DC_TRIGGER **)config->items.mem_realloc_func(item->triggers, + (size_t)(j - i + 1) * sizeof(ZBX_DC_TRIGGER *)); + + for (k = i; k < j; k++) + item->triggers[k - i] = (ZBX_DC_TRIGGER *)itemtrigs.values[k].second; + + item->triggers[j - i] = NULL; + + i = j - 1; } - item = (ZBX_DC_ITEM *)itemtrigs.values[i].first; - item->update_triggers = 0; - item->triggers = (ZBX_DC_TRIGGER **)config->items.mem_realloc_func(item->triggers, (j - i + 1) * sizeof(ZBX_DC_TRIGGER *)); + /* update reverse links from trigger to items */ - for (k = i; k < j; k++) - item->triggers[k - i] = (ZBX_DC_TRIGGER *)itemtrigs.values[k].second; + zbx_vector_uint64_create(&itemids); + zbx_vector_ptr_pair_sort(&itemtrigs, zbx_default_ptr_pair_ptr_second_compare_func); + + trigger = (ZBX_DC_TRIGGER *)itemtrigs.values[0].second; + for (i = 0; i < itemtrigs.values_num; i++) + { + if (trigger != itemtrigs.values[i].second) + { + dc_trigger_merge_itemids(trigger, &itemids); + trigger = (ZBX_DC_TRIGGER *)itemtrigs.values[i].second; + zbx_vector_uint64_clear(&itemids); + } + + item = (ZBX_DC_ITEM *)itemtrigs.values[i].first; + zbx_vector_uint64_append(&itemids, item->itemid); + } - item->triggers[j - i] = NULL; + if (0 != itemids.values_num) + dc_trigger_merge_itemids(trigger, &itemids); - i = j - 1; + zbx_vector_uint64_destroy(&itemids); } zbx_vector_ptr_pair_destroy(&itemtrigs); diff --git a/src/libs/zbxdbcache/dbconfig.h b/src/libs/zbxdbcache/dbconfig.h index 4e51bfe0b33..ca5352cfb27 100644 --- a/src/libs/zbxdbcache/dbconfig.h +++ b/src/libs/zbxdbcache/dbconfig.h @@ -51,6 +51,8 @@ typedef struct unsigned char correlation_mode; /* see ZBX_TRIGGER_CORRELATION_* defines */ unsigned char timer; + zbx_uint64_t *itemids; + zbx_vector_ptr_t tags; } ZBX_DC_TRIGGER; diff --git a/src/libs/zbxdbcache/dbconfig_dump.c b/src/libs/zbxdbcache/dbconfig_dump.c index 11671bed60c..06c1673f980 100644 --- a/src/libs/zbxdbcache/dbconfig_dump.c +++ b/src/libs/zbxdbcache/dbconfig_dump.c @@ -874,6 +874,8 @@ static void DCdump_triggers(void) for (i = 0; i < index.values_num; i++) { + zbx_uint64_t *itemid; + trigger = (ZBX_DC_TRIGGER *)index.values[i]; zabbix_log(LOG_LEVEL_TRACE, "triggerid:" ZBX_FS_UI64 " description:'%s' event_name:'%s' type:%u" @@ -889,6 +891,14 @@ static void DCdump_triggers(void) trigger->functional, trigger->locked); zabbix_log(LOG_LEVEL_TRACE, " opdata:'%s'", trigger->opdata); + if (NULL != trigger->itemids) + { + zabbix_log(LOG_LEVEL_TRACE, " itemids:"); + + for (itemid = trigger->itemids; 0 != *itemid; itemid++) + zabbix_log(LOG_LEVEL_TRACE, " " ZBX_FS_UI64, *itemid); + } + if (0 != trigger->tags.values_num) DCdump_trigger_tags(trigger); } |