-
Notifications
You must be signed in to change notification settings - Fork 1.1k
perf: introduce a terraform template/dynamic parameter render cache on prebuilds path #21201
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
Closed
Closed
Changes from all commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
22f5008
feat: add render cache for dynamic parameters
72d711d
feat: wire render cache into prebuilds reconciler
acd9bed
feat: add Prometheus metrics for render cache observability
353adc7
feat: add TTL-based cache cleanup for render cache
318f692
feat: refresh cache entry timestamp on hits
a54f1ae
refactor: make render cache non-nullable with interface
890a058
fix lint and fmt, required refactoring of the metrics testing
cstyan f7daab4
fix missing Close calls in tests
cstyan ba4e508
fix: cleanup render cache goroutines in tests
bea92ad
fix: add missing context import and remaining cleanup calls
85bbae6
fix: add Close() to RenderCache interface for proper cleanup
4f9b23a
fix: close render cache even when reconciler never ran
1c4b645
test: add prebuild cache test to verify dual-render behavior
b816191
perf(coderd/dynamicparameters): skip caching introspection render calls
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,214 @@ | ||
| package dynamicparameters | ||
|
|
||
| import ( | ||
| "context" | ||
| "fmt" | ||
| "sort" | ||
| "sync" | ||
| "time" | ||
|
|
||
| "github.com/cespare/xxhash/v2" | ||
| "github.com/google/uuid" | ||
| "github.com/prometheus/client_golang/prometheus" | ||
|
|
||
| "github.com/coder/preview" | ||
| "github.com/coder/quartz" | ||
| ) | ||
|
|
||
| // RenderCacheImpl is a simple in-memory cache for preview.Preview results. | ||
| // It caches based on (templateVersionID, ownerID, parameterValues). | ||
| type RenderCacheImpl struct { | ||
| mu sync.RWMutex | ||
| entries map[cacheKey]*cacheEntry | ||
|
|
||
| // Metrics (optional) | ||
| cacheHits prometheus.Counter | ||
| cacheMisses prometheus.Counter | ||
| cacheSize prometheus.Gauge | ||
|
|
||
| // TTL cleanup | ||
| clock quartz.Clock | ||
| ttl time.Duration | ||
| stopOnce sync.Once | ||
| stopCh chan struct{} | ||
| doneCh chan struct{} | ||
| } | ||
|
|
||
| type cacheEntry struct { | ||
| output *preview.Output | ||
| timestamp time.Time | ||
| } | ||
|
|
||
| type cacheKey struct { | ||
| templateVersionID uuid.UUID | ||
| ownerID uuid.UUID | ||
| parameterHash uint64 | ||
| } | ||
|
|
||
| // NewRenderCache creates a new render cache with a default TTL of 1 hour. | ||
| func NewRenderCache() *RenderCacheImpl { | ||
| return newCache(quartz.NewReal(), time.Hour, nil, nil, nil) | ||
| } | ||
|
|
||
| // NewRenderCacheWithMetrics creates a new render cache with Prometheus metrics. | ||
| func NewRenderCacheWithMetrics(cacheHits, cacheMisses prometheus.Counter, cacheSize prometheus.Gauge) *RenderCacheImpl { | ||
| return newCache(quartz.NewReal(), time.Hour, cacheHits, cacheMisses, cacheSize) | ||
| } | ||
|
|
||
| func newCache(clock quartz.Clock, ttl time.Duration, cacheHits, cacheMisses prometheus.Counter, cacheSize prometheus.Gauge) *RenderCacheImpl { | ||
| c := &RenderCacheImpl{ | ||
| entries: make(map[cacheKey]*cacheEntry), | ||
| clock: clock, | ||
| cacheHits: cacheHits, | ||
| cacheMisses: cacheMisses, | ||
| cacheSize: cacheSize, | ||
| ttl: ttl, | ||
| stopCh: make(chan struct{}), | ||
| doneCh: make(chan struct{}), | ||
| } | ||
|
|
||
| // Start cleanup goroutine | ||
| go c.cleanupLoop(context.Background()) | ||
|
|
||
| return c | ||
| } | ||
|
|
||
| // NewRenderCacheForTest creates a new render cache for testing purposes. | ||
| func NewRenderCacheForTest() *RenderCacheImpl { | ||
| return NewRenderCache() | ||
| } | ||
|
|
||
| // Close stops the cleanup goroutine and waits for it to finish. | ||
| func (c *RenderCacheImpl) Close() { | ||
| c.stopOnce.Do(func() { | ||
| close(c.stopCh) | ||
| <-c.doneCh | ||
| }) | ||
| } | ||
|
|
||
| func (c *RenderCacheImpl) get(templateVersionID, ownerID uuid.UUID, parameters map[string]string) (*preview.Output, bool) { | ||
| key := makeKey(templateVersionID, ownerID, parameters) | ||
| c.mu.RLock() | ||
| entry, ok := c.entries[key] | ||
| c.mu.RUnlock() | ||
|
|
||
| if !ok { | ||
| // Record miss | ||
| if c.cacheMisses != nil { | ||
| c.cacheMisses.Inc() | ||
| } | ||
| return nil, false | ||
| } | ||
|
|
||
| // Check if entry has expired | ||
| if c.clock.Since(entry.timestamp) > c.ttl { | ||
| // Expired entry, treat as miss | ||
| if c.cacheMisses != nil { | ||
| c.cacheMisses.Inc() | ||
| } | ||
| return nil, false | ||
| } | ||
|
|
||
| // Record hit and refresh timestamp | ||
| if c.cacheHits != nil { | ||
| c.cacheHits.Inc() | ||
| } | ||
|
|
||
| // Refresh timestamp on hit to keep frequently accessed entries alive | ||
| c.mu.Lock() | ||
| entry.timestamp = c.clock.Now() | ||
| c.mu.Unlock() | ||
|
|
||
| return entry.output, true | ||
| } | ||
|
|
||
| func (c *RenderCacheImpl) put(templateVersionID, ownerID uuid.UUID, parameters map[string]string, output *preview.Output) { | ||
| key := makeKey(templateVersionID, ownerID, parameters) | ||
| c.mu.Lock() | ||
| defer c.mu.Unlock() | ||
|
|
||
| c.entries[key] = &cacheEntry{ | ||
| output: output, | ||
| timestamp: c.clock.Now(), | ||
| } | ||
|
|
||
| // Update cache size metric | ||
| if c.cacheSize != nil { | ||
| c.cacheSize.Set(float64(len(c.entries))) | ||
| } | ||
| } | ||
|
|
||
| func makeKey(templateVersionID, ownerID uuid.UUID, parameters map[string]string) cacheKey { | ||
| return cacheKey{ | ||
| templateVersionID: templateVersionID, | ||
| ownerID: ownerID, | ||
| parameterHash: hashParameters(parameters), | ||
| } | ||
| } | ||
|
|
||
| // hashParameters creates a deterministic hash of the parameter map. | ||
| func hashParameters(params map[string]string) uint64 { | ||
| if len(params) == 0 { | ||
| return 0 | ||
| } | ||
|
|
||
| // Sort keys for deterministic hashing | ||
| keys := make([]string, 0, len(params)) | ||
| for k := range params { | ||
| keys = append(keys, k) | ||
| } | ||
| sort.Strings(keys) | ||
|
|
||
| // Hash the sorted key-value pairs | ||
| var b string | ||
| for _, k := range keys { | ||
| b += fmt.Sprintf("%s:%s,", k, params[k]) | ||
| } | ||
|
|
||
| return xxhash.Sum64String(b) | ||
| } | ||
|
|
||
| // cleanupLoop runs periodically to remove expired cache entries. | ||
| func (c *RenderCacheImpl) cleanupLoop(ctx context.Context) { | ||
| defer close(c.doneCh) | ||
|
|
||
| // Run cleanup every 15 minutes | ||
| cleanupFunc := func() error { | ||
| c.cleanup() | ||
| return nil | ||
| } | ||
|
|
||
| // Run once immediately | ||
| _ = cleanupFunc() | ||
|
|
||
| // Create a cancellable context for the ticker | ||
| tickerCtx, cancel := context.WithCancel(ctx) | ||
| defer cancel() | ||
|
|
||
| // Create ticker for periodic cleanup | ||
| tkr := c.clock.TickerFunc(tickerCtx, 15*time.Minute, cleanupFunc, "render-cache-cleanup") | ||
|
|
||
| // Wait for stop signal | ||
| <-c.stopCh | ||
| cancel() | ||
|
|
||
| _ = tkr.Wait() | ||
| } | ||
|
|
||
| // cleanup removes expired entries from the cache. | ||
| func (c *RenderCacheImpl) cleanup() { | ||
| c.mu.Lock() | ||
| defer c.mu.Unlock() | ||
|
|
||
| now := c.clock.Now() | ||
| for key, entry := range c.entries { | ||
| if now.Sub(entry.timestamp) > c.ttl { | ||
| delete(c.entries, key) | ||
| } | ||
| } | ||
|
|
||
| // Update cache size metric after cleanup | ||
| if c.cacheSize != nil { | ||
| c.cacheSize.Set(float64(len(c.entries))) | ||
| } | ||
| } | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The pattern we use other places is we keep the metrics on the
RenderCacheImpl. Then callcache.Register(prometheusregistry)from outside. Or just pass in the prometheus registry toNew.If the registry is
nil, like in tests, just don't attach the metrics.