<!-- 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 -->
44 lines
1.7 KiB
Go
44 lines
1.7 KiB
Go
// Package bootstrap provides the public entry point for the chart bounded context.
|
|
// It wires together internal components and exposes them for use in serve.go.
|
|
package bootstrap
|
|
|
|
import (
|
|
"context"
|
|
"log/slog"
|
|
|
|
"github.com/fleetdm/fleet/v4/server/chart/api"
|
|
"github.com/fleetdm/fleet/v4/server/chart/internal/mysql"
|
|
"github.com/fleetdm/fleet/v4/server/chart/internal/service"
|
|
platform_authz "github.com/fleetdm/fleet/v4/server/platform/authz"
|
|
eu "github.com/fleetdm/fleet/v4/server/platform/endpointer"
|
|
platform_mysql "github.com/fleetdm/fleet/v4/server/platform/mysql"
|
|
"github.com/go-kit/kit/endpoint"
|
|
"github.com/jmoiron/sqlx"
|
|
)
|
|
|
|
// New creates a new chart service module and returns its service and route handler.
|
|
func New(
|
|
dbConns *platform_mysql.DBConnections,
|
|
authorizer platform_authz.Authorizer,
|
|
viewerProvider api.ViewerProvider,
|
|
logger *slog.Logger,
|
|
) (api.Service, func(authMiddleware endpoint.Middleware) eu.HandlerRoutesFunc) {
|
|
ds := mysql.NewDatastore(dbConns, logger)
|
|
svc := service.NewService(authorizer, ds, viewerProvider, logger)
|
|
|
|
routesFn := func(authMiddleware endpoint.Middleware) eu.HandlerRoutesFunc {
|
|
return service.GetRoutes(svc, authMiddleware)
|
|
}
|
|
|
|
return svc, routesFn
|
|
}
|
|
|
|
// TrackedCriticalCVEs returns the curated set of CVE IDs that the chart
|
|
// collector currently tracks. Exposed for development tools (e.g.
|
|
// charts-backfill) that need to mirror the production CVE-selection logic
|
|
// without constructing the full bounded context.
|
|
func TrackedCriticalCVEs(ctx context.Context, db *sqlx.DB, logger *slog.Logger) ([]string, error) {
|
|
ds := mysql.NewDatastore(&platform_mysql.DBConnections{Primary: db, Replica: db}, logger)
|
|
return ds.TrackedCriticalCVEs(ctx)
|
|
}
|