<!-- Add the related story/sub-task/bug number, like Resolves #123, or remove if NA --> **Related issue:** Resolves #45715 # Details This PR refactors the way the charts module stores historical data to use the [roaring bitmap](https://github.com/RoaringBitmap/roaring) package instead of saving raw bitmaps. See [this blurb](https://github.com/RoaringBitmap/roaring#how-does-roaring-compares-with-the-alternatives) to learn how roaring compresses data, but TL;DR for our purposes it represents a huge improvement especially for larger deployments where host ID numbers may be very large. In testing, some data was reduced 96%. The majority of the changes in this PR are straight swapping of types from `[]byte` to `*roaring.Bitmap` in vars and function signatures, and updating the internals of our bit math helpers to use roaring methods instead of native AND and OR methods. I've tried to comment on all functional changes. Since the charts have been shipped already, so there will be data in the wild in the prior "dense" format, the code still handles dense bitmaps on _read_, but will always _write_ roaring bitmaps. The majority of the data will therefore have turned over within 30 days on its own, but I plan on a follow-up PR that will transform open rows when the cron runs so that we should be guaranteed to turn over completely within 30 days. # Checklist for submitter If some of the following don't apply, delete the relevant line. - [X] Changes file added for user-visible changes in `changes/`, `orbit/changes/` or `ee/fleetd-chrome/changes`. See [Changes files](https://github.com/fleetdm/fleet/blob/main/docs/Contributing/guides/committing-changes.md#changes-files) for more information. - [X] Input data is properly validated, `SELECT *` is avoided, SQL injection is prevented (using placeholders for values in statements), JS inline code is prevented especially for url redirects, and untrusted data interpolated into shell scripts/commands is validated against shell metacharacters. ## Testing - [X] Added/updated automated tests - Tests updated to accommodate the new format, and existing unchanged tests act as proof against regression - [X] QA'd all new/changed functionality manually - Using a tool that dumps the `host_scd_data` rows data into a JSON file (with the keys being entity_id+data and the values being host IDs on that date), compared the data from main branch and this and confirmed they're identical - With a host count of ~9000, some of which have IDs of over 1,000,000, the data storage requirements were: * 82,558,976 bytes for dense * 2,867,200 for roaring (a 96% decrease) For unreleased bug fixes in a release candidate, one of: - [X] Confirmed that the fix is not expected to adversely impact load test results - should hugely improve - [X] Alerted the release DRI if additional load testing is needed ## Database migrations - [X] Checked schema for all modified table for columns that will auto-update timestamps during migration. <!-- This is an auto-generated comment: release notes by coderabbit.ai --> ## Summary by CodeRabbit * **New Features** * Implemented roaring bitmaps in historical data collection to optimize bitmap handling for chart data aggregation * Added encoding support to bitmap storage schema for flexible data representation <!-- end of auto-generated comment: release notes by coderabbit.ai -->
186 lines
5.9 KiB
Go
186 lines
5.9 KiB
Go
package service
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"sync"
|
|
"sync/atomic"
|
|
"testing"
|
|
"time"
|
|
|
|
"github.com/RoaringBitmap/roaring"
|
|
"github.com/fleetdm/fleet/v4/server/chart/internal/types"
|
|
"github.com/stretchr/testify/assert"
|
|
"github.com/stretchr/testify/require"
|
|
)
|
|
|
|
func TestHashHostFilterDeterministic(t *testing.T) {
|
|
t.Run("slice order and duplicates don't change the key", func(t *testing.T) {
|
|
a := &types.HostFilter{
|
|
LabelIDs: []uint{3, 1, 2},
|
|
Platforms: []string{"windows", "darwin"},
|
|
IncludeHostIDs: []uint{9, 7},
|
|
ExcludeHostIDs: []uint{5, 4},
|
|
}
|
|
b := &types.HostFilter{
|
|
LabelIDs: []uint{1, 2, 3},
|
|
Platforms: []string{"darwin", "windows"},
|
|
IncludeHostIDs: []uint{7, 9},
|
|
ExcludeHostIDs: []uint{4, 5},
|
|
}
|
|
assert.Equal(t, hashHostFilter(a), hashHostFilter(b))
|
|
})
|
|
|
|
t.Run("teams distinguishes nil, empty, zero-team, and specific teams", func(t *testing.T) {
|
|
// Four semantically distinct values that must not share a cache key:
|
|
// nil — no team filter (global user, no team_id)
|
|
// empty slice — match nothing (team user with zero accessible teams)
|
|
// [0] — no-team hosts (team_id=0 query)
|
|
// [5] — specific team
|
|
keys := map[string]struct{}{
|
|
hashHostFilter(&types.HostFilter{TeamIDs: nil}): {},
|
|
hashHostFilter(&types.HostFilter{TeamIDs: []uint{}}): {},
|
|
hashHostFilter(&types.HostFilter{TeamIDs: []uint{0}}): {},
|
|
hashHostFilter(&types.HostFilter{TeamIDs: []uint{5}}): {},
|
|
hashHostFilter(&types.HostFilter{TeamIDs: []uint{1, 2}}): {},
|
|
}
|
|
assert.Len(t, keys, 5, "all five team-scope variants must produce distinct keys")
|
|
})
|
|
|
|
t.Run("label vs include collision guard", func(t *testing.T) {
|
|
// Without a separator, labels=[1,2] + include=[] could collide with
|
|
// labels=[] + include=[1,2] if the key was naively concatenated.
|
|
a := &types.HostFilter{LabelIDs: []uint{1, 2}}
|
|
b := &types.HostFilter{IncludeHostIDs: []uint{1, 2}}
|
|
assert.NotEqual(t, hashHostFilter(a), hashHostFilter(b))
|
|
})
|
|
}
|
|
|
|
func TestHostFilterCacheServesFromCacheUntilTTL(t *testing.T) {
|
|
cache := newHostFilterCache(10 * time.Second)
|
|
|
|
// Override the clock so TTL behavior is deterministic.
|
|
var now atomic.Int64
|
|
now.Store(time.Now().UnixNano())
|
|
cache.clock = func() time.Time { return time.Unix(0, now.Load()) }
|
|
|
|
var calls atomic.Int32
|
|
fetch := func(_ context.Context) (*roaring.Bitmap, error) {
|
|
calls.Add(1)
|
|
return roaring.BitmapOf(1, 2, 3), nil
|
|
}
|
|
|
|
filter := &types.HostFilter{LabelIDs: []uint{1}}
|
|
for range 5 {
|
|
b, err := cache.Get(t.Context(), filter, fetch)
|
|
require.NoError(t, err)
|
|
assert.Equal(t, uint64(3), b.GetCardinality())
|
|
}
|
|
assert.Equal(t, int32(1), calls.Load(), "repeated gets within TTL should hit the cache")
|
|
|
|
// Advance past TTL.
|
|
now.Add(int64(11 * time.Second))
|
|
_, err := cache.Get(t.Context(), filter, fetch)
|
|
require.NoError(t, err)
|
|
assert.Equal(t, int32(2), calls.Load(), "expired entry should trigger a refetch")
|
|
}
|
|
|
|
func TestHostFilterCacheDistinctFiltersMissSeparately(t *testing.T) {
|
|
cache := newHostFilterCache(time.Minute)
|
|
|
|
var calls atomic.Int32
|
|
fetch := func(_ context.Context) (*roaring.Bitmap, error) {
|
|
calls.Add(1)
|
|
return roaring.BitmapOf(1), nil
|
|
}
|
|
|
|
_, err := cache.Get(t.Context(), &types.HostFilter{TeamIDs: []uint{1}}, fetch)
|
|
require.NoError(t, err)
|
|
_, err = cache.Get(t.Context(), &types.HostFilter{TeamIDs: []uint{2}}, fetch)
|
|
require.NoError(t, err)
|
|
|
|
assert.Equal(t, int32(2), calls.Load(), "different filter keys should each trigger a fetch")
|
|
}
|
|
|
|
func TestHostFilterCacheSingleflightCoalescesConcurrentMisses(t *testing.T) {
|
|
cache := newHostFilterCache(time.Minute)
|
|
|
|
var calls atomic.Int32
|
|
unblock := make(chan struct{})
|
|
fetch := func(_ context.Context) (*roaring.Bitmap, error) {
|
|
calls.Add(1)
|
|
<-unblock // hold the fetch until all goroutines are parked on singleflight
|
|
return roaring.BitmapOf(1), nil
|
|
}
|
|
|
|
filter := &types.HostFilter{LabelIDs: []uint{42}}
|
|
|
|
var wg sync.WaitGroup
|
|
const goroutines = 20
|
|
wg.Add(goroutines)
|
|
for range goroutines {
|
|
go func() {
|
|
defer wg.Done()
|
|
b, err := cache.Get(t.Context(), filter, fetch)
|
|
assert.NoError(t, err)
|
|
assert.Equal(t, uint64(1), b.GetCardinality())
|
|
}()
|
|
}
|
|
|
|
// Give the goroutines a moment to all reach Get; then release the fetch.
|
|
time.Sleep(20 * time.Millisecond)
|
|
close(unblock)
|
|
wg.Wait()
|
|
|
|
assert.Equal(t, int32(1), calls.Load(), "singleflight should coalesce concurrent misses")
|
|
}
|
|
|
|
func TestHostFilterCacheSweepsExpiredEntriesOnWrite(t *testing.T) {
|
|
cache := newHostFilterCache(10 * time.Second)
|
|
|
|
var now atomic.Int64
|
|
now.Store(time.Now().UnixNano())
|
|
cache.clock = func() time.Time { return time.Unix(0, now.Load()) }
|
|
|
|
fetch := func(_ context.Context) (*roaring.Bitmap, error) { return roaring.BitmapOf(1), nil }
|
|
|
|
// Seed two entries that will later be expired.
|
|
_, err := cache.Get(t.Context(), &types.HostFilter{LabelIDs: []uint{1}}, fetch)
|
|
require.NoError(t, err)
|
|
_, err = cache.Get(t.Context(), &types.HostFilter{LabelIDs: []uint{2}}, fetch)
|
|
require.NoError(t, err)
|
|
|
|
cache.mu.RLock()
|
|
assert.Len(t, cache.entries, 2)
|
|
cache.mu.RUnlock()
|
|
|
|
// Advance past TTL and write a new entry — the two stale entries should
|
|
// be swept during the write path.
|
|
now.Add(int64(11 * time.Second))
|
|
_, err = cache.Get(t.Context(), &types.HostFilter{LabelIDs: []uint{3}}, fetch)
|
|
require.NoError(t, err)
|
|
|
|
cache.mu.RLock()
|
|
defer cache.mu.RUnlock()
|
|
assert.Len(t, cache.entries, 1, "expired entries should be swept on write")
|
|
}
|
|
|
|
func TestHostFilterCacheDoesNotCacheErrors(t *testing.T) {
|
|
cache := newHostFilterCache(time.Minute)
|
|
|
|
var calls atomic.Int32
|
|
sentinel := errors.New("boom")
|
|
fetch := func(_ context.Context) (*roaring.Bitmap, error) {
|
|
calls.Add(1)
|
|
return nil, sentinel
|
|
}
|
|
|
|
filter := &types.HostFilter{}
|
|
_, err := cache.Get(t.Context(), filter, fetch)
|
|
require.ErrorIs(t, err, sentinel)
|
|
_, err = cache.Get(t.Context(), filter, fetch)
|
|
require.ErrorIs(t, err, sentinel)
|
|
|
|
assert.Equal(t, int32(2), calls.Load(), "failed fetches must not poison the cache")
|
|
}
|