From 7c8ce7723d74deb8a246e01529803190c8adf4bd Mon Sep 17 00:00:00 2001 From: Andris Zeila Date: Wed, 14 Apr 2021 11:27:52 +0300 Subject: ........S. [ZBXNEXT-6497] added read/write locking to value cache Merge in ZBX/zabbix from feature/DEV-1639-5.0 to release/5.0 * commit '50df03c3cfe89dcb755b734159cf364532f5d00d': .......... [DEV-1639] added component to changelog entry ........S. [DEV-1639] fixed warning ........S. [DEV-1639] fixed item update during valuecache read lock, fixed item removal with disabled value cache ........S. [DEV-1639] moved time() call outside loop/lock when updating statistics ........S. [DEV-1639] fixed possibility of item being removed by housekeeper when item is added and data has not been yet retrieved ........S. [DEV-1639] changed write lock to read lock when retrieving value cache statistics ........S. [DEV-1639] fixed item removal from value cache on data retrieval failure ........S. [DEV-1639] fixed value cache item cleanup on data retrieval failure ........S. [DEV-1639] added value cache rwlock to diaginfo stats ........S. [DEV-1639] fixed indentation ........S. [DEV-1639] added const .......... [ZBXNEXT-6497] added changelog entry ........S. [DEV-1639] removed valuecache item state ........S. [DEV-1639] added read/write locks to value cache (cherry picked from commit 36ad10a84cba20853fd9e2d82ca14298e688b07a) --- tests/libs/zbxdbcache/valuecache_test.c | 21 +++------------------ tests/libs/zbxdbcache/zbx_vc_add_values.c | 3 +++ tests/libs/zbxdbcache/zbx_vc_get_value.c | 4 ++++ tests/libs/zbxdbcache/zbx_vc_get_values.c | 4 ++++ 4 files changed, 14 insertions(+), 18 deletions(-) (limited to 'tests/libs/zbxdbcache') diff --git a/tests/libs/zbxdbcache/valuecache_test.c b/tests/libs/zbxdbcache/valuecache_test.c index 1570e039828..d85bdf04273 100644 --- a/tests/libs/zbxdbcache/valuecache_test.c +++ b/tests/libs/zbxdbcache/valuecache_test.c @@ -31,8 +31,6 @@ int zbx_vc_get_cached_values(zbx_uint64_t itemid, unsigned char value_type, zbx_ int i; zbx_vc_chunk_t *chunk; - vc_try_lock(); - if (NULL == (item = zbx_hashset_search(&vc_cache->items, &itemid))) return FAIL; @@ -45,8 +43,6 @@ int zbx_vc_get_cached_values(zbx_uint64_t itemid, unsigned char value_type, zbx_ vc_history_record_vector_append(values, value_type, &chunk->slots[i]); } - vc_try_unlock(); - return SUCCEED; } @@ -56,8 +52,6 @@ int zbx_vc_precache_values(zbx_uint64_t itemid, int value_type, int seconds, int int ret; zbx_vector_history_record_t values; - vc_try_lock(); - /* add item to cache if necessary */ if (NULL == (item = (zbx_vc_item_t *)zbx_hashset_search(&vc_cache->items, &itemid))) { @@ -66,18 +60,17 @@ int zbx_vc_precache_values(zbx_uint64_t itemid, int value_type, int seconds, int } /* perform request to cache values */ - vc_item_addref(item); zbx_history_record_vector_create(&values); + RDLOCK_CACHE; ret = vch_item_get_values(item, &values, seconds, count, ts); + UNLOCK_CACHE; + zbx_vc_flush_stats(); zbx_history_record_vector_destroy(&values, value_type); - vc_item_release(item); /* reset cache statistics */ vc_cache->hits = 0; vc_cache->misses = 0; - vc_try_unlock(); - return ret; } @@ -87,8 +80,6 @@ int zbx_vc_get_item_state(zbx_uint64_t itemid, int *status, int *active_range, i zbx_vc_item_t *item; int ret = FAIL; - vc_try_lock(); - if (NULL != (item = (zbx_vc_item_t *)zbx_hashset_search(&vc_cache->items, &itemid))) { *status = item->status; @@ -99,8 +90,6 @@ int zbx_vc_get_item_state(zbx_uint64_t itemid, int *status, int *active_range, i ret = SUCCEED; } - vc_try_unlock(); - return ret; } @@ -109,13 +98,9 @@ int zbx_vc_get_cache_state(int *mode, zbx_uint64_t *hits, zbx_uint64_t *misses) if (NULL == vc_cache) return FAIL; - vc_try_lock(); - *mode = vc_cache->mode; *hits = vc_cache->hits; *misses = vc_cache->misses; - vc_try_unlock(); - return SUCCEED; } diff --git a/tests/libs/zbxdbcache/zbx_vc_add_values.c b/tests/libs/zbxdbcache/zbx_vc_add_values.c index 42d45860431..ac80bedbb50 100644 --- a/tests/libs/zbxdbcache/zbx_vc_add_values.c +++ b/tests/libs/zbxdbcache/zbx_vc_add_values.c @@ -53,6 +53,9 @@ void zbx_mock_test_entry(void **state) /* set small cache size to force smaller cache free request size (5% of cache size) */ CONFIG_VALUE_CACHE_SIZE = ZBX_KIBIBYTE; + err = zbx_locks_create(&error); + zbx_mock_assert_result_eq("Lock initialization failed", SUCCEED, err); + err = zbx_vc_init(&error); zbx_mock_assert_result_eq("Value cache initialization failed", SUCCEED, err); diff --git a/tests/libs/zbxdbcache/zbx_vc_get_value.c b/tests/libs/zbxdbcache/zbx_vc_get_value.c index f20fe48a077..aa7ea5bde3d 100644 --- a/tests/libs/zbxdbcache/zbx_vc_get_value.c +++ b/tests/libs/zbxdbcache/zbx_vc_get_value.c @@ -52,6 +52,9 @@ void zbx_mock_test_entry(void **state) /* set small cache size to force smaller cache free request size (5% of cache size) */ CONFIG_VALUE_CACHE_SIZE = ZBX_KIBIBYTE; + err = zbx_locks_create(&error); + zbx_mock_assert_result_eq("Lock initialization failed", SUCCEED, err); + err = zbx_vc_init(&error); zbx_mock_assert_result_eq("Value cache initialization failed", SUCCEED, err); @@ -89,6 +92,7 @@ void zbx_mock_test_entry(void **state) zbx_vector_history_record_reserve(&returned, 1); err = zbx_vc_get_value(itemid, value_type, &ts, &returned.values[0]); + zbx_vc_flush_stats(); returned.values_num = 1; zbx_mock_assert_result_eq("zbx_vc_get_values() return value", SUCCEED, err); diff --git a/tests/libs/zbxdbcache/zbx_vc_get_values.c b/tests/libs/zbxdbcache/zbx_vc_get_values.c index 76816f9cf33..cbff161a96e 100644 --- a/tests/libs/zbxdbcache/zbx_vc_get_values.c +++ b/tests/libs/zbxdbcache/zbx_vc_get_values.c @@ -52,6 +52,9 @@ void zbx_mock_test_entry(void **state) /* set small cache size to force smaller cache free request size (5% of cache size) */ CONFIG_VALUE_CACHE_SIZE = ZBX_KIBIBYTE; + err = zbx_locks_create(&error); + zbx_mock_assert_result_eq("Lock initialization failed", SUCCEED, err); + err = zbx_vc_init(&error); zbx_mock_assert_result_eq("Value cache initialization failed", SUCCEED, err); @@ -83,6 +86,7 @@ void zbx_mock_test_entry(void **state) zbx_vcmock_get_request_params(handle, &itemid, &value_type, &seconds, &count, &ts); err = zbx_vc_get_values(itemid, value_type, &returned, seconds, count, &ts); + zbx_vc_flush_stats(); zbx_mock_assert_result_eq("zbx_vc_get_values() return value", SUCCEED, err); /* validate results */ -- cgit v1.2.3