-
Notifications
You must be signed in to change notification settings - Fork 275
feat(storage): add compression primitives: FrameTable, V4 header #2246
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 4 commits
7691321
cf85907
3cb0bb1
4c6962a
2c65691
00149d4
be428aa
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,131 @@ | ||
| package storage | ||
|
|
||
| import ( | ||
| "context" | ||
| "fmt" | ||
|
|
||
| "github.com/e2b-dev/infra/packages/shared/pkg/featureflags" | ||
| ) | ||
|
|
||
| // CompressConfig is the base compression configuration, loaded from environment | ||
| // variables at startup. Feature flags can override individual fields at runtime | ||
| // via ResolveCompressConfig. | ||
| type CompressConfig struct { | ||
| Enabled bool `env:"COMPRESS_ENABLED" envDefault:"false"` | ||
| Type string `env:"COMPRESS_TYPE" envDefault:"zstd"` | ||
| Level int `env:"COMPRESS_LEVEL" envDefault:"2"` | ||
| FrameSizeKB int `env:"COMPRESS_FRAME_SIZE_KB" envDefault:"2048"` | ||
| TargetPartSizeMB int `env:"COMPRESS_TARGET_PART_SIZE_MB" envDefault:"50"` | ||
| FrameEncodeWorkers int `env:"COMPRESS_FRAME_ENCODE_WORKERS" envDefault:"4"` | ||
| EncoderConcurrency int `env:"COMPRESS_ENCODER_CONCURRENCY" envDefault:"1"` | ||
| } | ||
|
Comment on lines
+13
to
+21
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. isn't this defined by the feature flags already?
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. we need to override the FF settings for the integration tests, and also to configure the service in the future without FF |
||
|
|
||
| // CompressionType returns the parsed CompressionType. | ||
| func (c *CompressConfig) CompressionType() CompressionType { | ||
| if c == nil { | ||
| return CompressionNone | ||
| } | ||
|
|
||
| return ParseCompressionType(c.Type) | ||
| } | ||
|
|
||
| // FrameSize returns the frame size in bytes. | ||
| func (c *CompressConfig) FrameSize() int { | ||
| if c == nil || c.FrameSizeKB <= 0 { | ||
| return DefaultCompressFrameSize | ||
| } | ||
|
|
||
| return c.FrameSizeKB * 1024 | ||
| } | ||
|
|
||
| // TargetPartSize returns the target part size in bytes. | ||
| func (c *CompressConfig) TargetPartSize() int64 { | ||
| if c == nil || c.TargetPartSizeMB <= 0 { | ||
| return int64(gcpMultipartUploadChunkSize) | ||
| } | ||
|
|
||
| return int64(c.TargetPartSizeMB) * (1 << 20) | ||
| } | ||
|
|
||
| // IsEnabled reports whether compression is configured and active. | ||
| func (c *CompressConfig) IsEnabled() bool { | ||
| return c != nil && c.Enabled && c.CompressionType() != CompressionNone | ||
| } | ||
|
|
||
| // Validate checks that the config is internally consistent. | ||
| func (c *CompressConfig) Validate() error { | ||
| if c == nil || !c.IsEnabled() { | ||
| return nil | ||
| } | ||
|
|
||
| fs := c.FrameSize() | ||
| if fs <= 0 { | ||
| return fmt.Errorf("frame size must be positive, got %d KB", c.FrameSizeKB) | ||
| } | ||
| if MemoryChunkSize%fs != 0 && fs%MemoryChunkSize != 0 { | ||
| return fmt.Errorf("frame size (%d) must be a divisor or multiple of MemoryChunkSize (%d)", fs, MemoryChunkSize) | ||
| } | ||
|
|
||
| return nil | ||
| } | ||
|
|
||
| // Resolve returns a pointer to this config if compression is enabled, or nil. | ||
| // Callers use nil to mean "no compression". | ||
| func (c *CompressConfig) Resolve() *CompressConfig { | ||
| if c == nil || !c.IsEnabled() { | ||
| return nil | ||
| } | ||
|
|
||
| return c | ||
| } | ||
|
|
||
| // CompressConfigFromLDValue parses the LaunchDarkly CompressConfigFlag JSON | ||
| // into a CompressConfig. Returns nil if the flag disables compression. | ||
| func CompressConfigFromLDValue(ff *featureflags.Client, ctx context.Context) *CompressConfig { | ||
| if ff == nil { | ||
| return nil | ||
| } | ||
|
|
||
| v := ff.JSONFlag(ctx, featureflags.CompressConfigFlag).AsValueMap() | ||
|
|
||
| if !v.Get("compressBuilds").BoolValue() { | ||
| return nil | ||
| } | ||
|
|
||
| ct := v.Get("compressionType").StringValue() | ||
| if ParseCompressionType(ct) == CompressionNone { | ||
| return nil | ||
| } | ||
|
|
||
| return &CompressConfig{ | ||
| Enabled: true, | ||
| Type: ct, | ||
| Level: v.Get("compressionLevel").IntValue(), | ||
| FrameSizeKB: v.Get("frameSizeKB").IntValue(), | ||
| TargetPartSizeMB: v.Get("targetPartSizeMB").IntValue(), | ||
| FrameEncodeWorkers: v.Get("frameEncodeWorkers").IntValue(), | ||
| EncoderConcurrency: v.Get("encoderConcurrency").IntValue(), | ||
| } | ||
| } | ||
|
|
||
| // ResolveCompressConfig returns the effective compression config for a given | ||
| // file type and use case. Feature flags override the base config when active. | ||
| // Returns nil when compression is disabled. | ||
| // | ||
| // fileType and useCase are added to the LD evaluation context so that | ||
|
Check warning on line 115 in packages/shared/pkg/storage/compress_config.go
|
||
levb marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| // LaunchDarkly targeting rules can differentiate (e.g. compress memfile | ||
| // but not rootfs, or compress builds but not pauses). | ||
| func ResolveCompressConfig(ctx context.Context, base CompressConfig, ff *featureflags.Client, fileType, useCase string) *CompressConfig { | ||
| if ff != nil { | ||
| ctx = featureflags.AddToContext(ctx, | ||
| featureflags.CompressFileTypeContext(fileType), | ||
| featureflags.CompressUseCaseContext(useCase), | ||
| ) | ||
|
|
||
| if override := CompressConfigFromLDValue(ff, ctx); override != nil { | ||
| return override | ||
| } | ||
| } | ||
|
|
||
| return base.Resolve() | ||
| } | ||
Uh oh!
There was an error while loading. Please reload this page.