@cryptotaxi247 / netdata-1 / commits / d92f08f02

Fix cache (SIGSEGV) (#22026)

thiagoftsm committed Mar 25, 2026 at 22:29 UTC d92f08f02a550ce6c2f8062942518422c1fe6428
1 file changed +33 -1
src/database/engine/cache.c
+33 -1
@@ -216,12 +216,18 @@ static inline size_t page_size_from_assumed_size(PGC *cache, size_t assumed_size
216 // locking
217
218 static inline size_t pgc_indexing_partition(PGC *cache, Word_t metric_id) {
219 + static __thread PGC *last_cache = NULL;
220 + static __thread size_t last_partitions = 0;
221 static __thread Word_t last_metric_id = 0;
222 static __thread size_t last_partition = 0;
223
222 - if(metric_id == last_metric_id || cache->config.partitions == 1)
224 + if(cache == last_cache &&
225 + cache->config.partitions == last_partitions &&
226 + (metric_id == last_metric_id || cache->config.partitions == 1))
227 return last_partition;
228
229 + last_cache = cache;
230 + last_partitions = cache->config.partitions;
231 last_metric_id = metric_id;
232 last_partition = indexing_partition(metric_id, cache->config.partitions);
233
@@ -3142,6 +3148,32 @@ int pgc_unittest(void) {
3148
3149 pgc_destroy(cache, true);
3150
3151 + {
3152 + PGC *cache_a = pgc_create("partition-cache-a",
3153 + 32 * 1024 * 1024, unittest_free_clean_page_callback,
3154 + 64, NULL, unittest_save_dirty_page_callback,
3155 + 10, 10, 1000, 10,
3156 + PGC_OPTIONS_DEFAULT, 4, 0);
3157 + PGC *cache_b = pgc_create("partition-cache-b",
3158 + 32 * 1024 * 1024, unittest_free_clean_page_callback,
3159 + 64, NULL, unittest_save_dirty_page_callback,
3160 + 10, 10, 1000, 10,
3161 + PGC_OPTIONS_DEFAULT, 5, 0);
3162 +
3163 + Word_t metric_id = 5;
3164 + size_t partition_a = pgc_indexing_partition(cache_a, metric_id);
3165 + size_t partition_b = pgc_indexing_partition(cache_b, metric_id);
3166 +
3167 + if(partition_a != indexing_partition(metric_id, cache_a->config.partitions))
3168 + fatal("pgc_indexing_partition() returned the wrong partition for cache_a");
3169 +
3170 + if(partition_b != indexing_partition(metric_id, cache_b->config.partitions))
3171 + fatal("pgc_indexing_partition() returned the wrong partition for cache_b");
3172 +
3173 + pgc_destroy(cache_a, true);
3174 + pgc_destroy(cache_b, true);
3175 + }
3176 +
3177 #ifdef PGC_STRESS_TEST
3178 unittest_stress_test();
3179 #endif