Periodically invalidate rank cache during bulk add

This commit changes `RankCache.BulkAdd()` so that entries are
limited to an upper bound of 2x `maxEntries`. When this bound
is exceeded then the cache is automatically recalculated.
This commit is contained in:
Ben Johnson 2022-02-15 08:25:45 -07:00
parent 6e41c663e0
commit 6b84d685d5
2 changed files with 20 additions and 0 deletions

View file

@ -194,6 +194,14 @@ func (c *rankCache) BulkAdd(id uint64, n uint64) {
}
c.entries[id] = n
// FB-1206: Periodically invalidate the cache when we are bulk loading
// as this can take up an upbounded amount of memory. This is especially
// true when restoring shards as all rows will be added.
if len(c.entries) > int(2*c.maxEntries) {
c.stats.Count(MetricRecalculateCache, 1, 1.0)
c.recalculate()
}
}
// Get returns a count for a given id.

View file

@ -70,3 +70,15 @@ func TestCache_Rank_Dirty(t *testing.T) {
t.Fatalf("wrote %v but got %v", expect, got)
}
}
func TestCache_Rank_BulkAdd(t *testing.T) {
const cacheSize = 10
cache := pilosa.NewRankCache(uint32(cacheSize))
for i := uint64(0); i < 1000; i++ {
cache.BulkAdd(i, i)
if n := cache.Len(); n > cacheSize*2 {
t.Fatalf("entry count exceed 2x cache size: %d", n)
}
}
}