Skip to content

feat: add caching mechanism to GetReleaseTargetsForDeploymentAndEnvir…#851

Merged
adityachoudhari26 merged 2 commits intomainfrom
cacjomg-stores
Mar 19, 2026
Merged

feat: add caching mechanism to GetReleaseTargetsForDeploymentAndEnvir…#851
adityachoudhari26 merged 2 commits intomainfrom
cacjomg-stores

Conversation

@jsbroks
Copy link
Copy Markdown
Member

@jsbroks jsbroks commented Mar 19, 2026

…onment

Summary by CodeRabbit

  • Infrastructure
    • Added query operations to fetch release targets by deployment, resource, and workspace.
    • Added a release-target existence check to validate whether a target is present.
    • Introduced configurable in-memory caching with adjustable TTLs to reduce repeated database lookups and improve performance.
    • Integrations updated to use the new release-target getters, enabling cached lookups where appropriate.

@coderabbitai
Copy link
Copy Markdown
Contributor

coderabbitai bot commented Mar 19, 2026

Caution

Review failed

Pull request was closed or merged during review

📝 Walkthrough

Walkthrough

This pull request adds a new releasetargets store package with multiple PostgreSQL-backed query implementations (deployment, workspace, resource, existence check) plus configurable in-memory caching. It also wires those getters into several policy-evaluator and controller constructors, replacing some local query implementations and adding dependency injection for the new getters.

Changes

Cohort / File(s) Summary
Caching Infrastructure
apps/workspace-engine/pkg/store/releasetargets/options.go
Adds Option pattern and WithCache(ttl); buildCache() constructs optional gocache.Cache with TTL and cleanup interval.
New Release Target Queries
apps/workspace-engine/pkg/store/releasetargets/get_for_deployment.go, apps/workspace-engine/pkg/store/releasetargets/get_for_resource.go, apps/workspace-engine/pkg/store/releasetargets/get_for_workspace.go, apps/workspace-engine/pkg/store/releasetargets/release_target_exists.go
Introduces interfaces and Postgres implementations for retrieval/existence checks; each supports optional in-memory cache, tracing, UUID parsing, DB query via db.GetQueries(ctx), result mapping, and caching.
Deployment-Environment Query Extension
apps/workspace-engine/pkg/store/releasetargets/get_for_deployment_environment.go
Adds optional cache field and New... constructor; uses composite cache key deploymentID:environmentID and caches results.
Callsite/DI and Consumers
apps/workspace-engine/pkg/workspace/releasemanager/policy/evaluator/.../getters.go (gradualrollout & environmentprogression), apps/workspace-engine/svc/controllers/desiredrelease/getters_postgres.go, .../policyeval/getter_postgres.go, apps/workspace-engine/svc/controllers/desiredrelease/controller.go, apps/workspace-engine/svc/controllers/policyeval/getters_postgres.go, apps/workspace-engine/pkg/workspace/releasemanager/policy/evaluator/gradualrollout/getters_test.go
Removes local GetReleaseTargetsForDeployment implementations, extends several Getters constructors to accept injected releasetargets getters (rtForDep, rtForDepEnv), updates callers/tests to pass (or lazily construct) these injected getters; controller now holds queries and lazily builds default releasetargets getters with a 5m cache when getter is nil.

Sequence Diagram

sequenceDiagram
    participant Caller
    participant Store as Store (Query Method)
    participant Cache as gocache.Cache
    participant DB as PostgreSQL

    Caller->>Store: GetReleaseTargets(ctx, id)
    Store->>Cache: Get(cacheKey)
    alt Cache Hit
        Cache-->>Store: cached targets
        Store-->>Caller: return targets
    else Cache Miss
        Store->>Store: Parse id(s) as UUID(s)
        Store->>DB: GetQueries().GetReleaseTargets...(ctx, uuid)
        DB-->>Store: []Row
        Store->>Store: Convert rows -> []oapi.ReleaseTarget
        Store->>Cache: SetDefault(cacheKey, targets)
        Store-->>Caller: return targets
    end
Loading

Estimated code review effort

🎯 4 (Complex) | ⏱️ ~45 minutes

Possibly related PRs

  • feat: eligibility controller #840 — modifies and wires the same releasetargets getters and updates policy-evaluator constructors to accept injected getters.
  • chore: version jobs go endpoint #689 — implements server-side handlers that load release targets for a deployment; overlaps with introduced retrieval interfaces/implementations.
  • simplify logic #715 — touches release-target storage/retrieval APIs and handlers; likely overlapping with store/query semantics introduced here.

Poem

🐰 I hopped through code, a tiny patch in tow,
Fetching targets where UUIDs grow,
Caches warmed, queries trimmed with care,
Getters now pass and thread everywhere,
A carrot for tests — joy in every row! 🥕

🚥 Pre-merge checks | ✅ 1 | ❌ 2

❌ Failed checks (2 warnings)

Check name Status Explanation Resolution
Title check ⚠️ Warning The PR title refers to adding a caching mechanism to GetReleaseTargetsForDeploymentAndEnvironment, but the changeset is significantly broader—it introduces a new releasetargets store package with caching for four different getter functions, refactors multiple controller and policy evaluator interfaces, and performs substantial dependency injection changes across the codebase. Revise the title to accurately reflect the full scope: something like 'feat: introduce releasetargets store package with caching and refactor dependency injection' would better summarize the main changes.
Docstring Coverage ⚠️ Warning Docstring coverage is 0.00% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (1 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
📝 Generate docstrings
  • Create stacked PR
  • Commit on current branch
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch cacjomg-stores
📝 Coding Plan
  • Generate coding plan for human review comments

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

Copy link
Copy Markdown
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 1

🧹 Nitpick comments (1)
apps/workspace-engine/pkg/store/releasetargets/get_for_workspace.go (1)

58-60: Consider memory implications of caching workspace-level results.

Workspaces may contain a large number of release targets. Caching entire []oapi.ReleaseTarget slices per workspace could lead to significant memory usage if many workspaces are queried.

If this is a concern, consider adding a size threshold or LRU eviction policy. Otherwise, if workspaces are expected to have bounded target counts, this is fine.

🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@apps/workspace-engine/pkg/store/releasetargets/get_for_workspace.go` around
lines 58 - 60, The current caching of entire workspace slices via
s.cache.SetDefault(workspaceID, targets) may blow memory for large workspaces;
modify the code in GetForWorkspace to only cache when the number of targets is
below a threshold (e.g., add const maxCachedTargets = N and if len(targets) <=
maxCachedTargets then s.cache.SetDefault(...)), or replace/augment s.cache with
an LRU-capable cache and use that instead so eviction controls memory; refer to
s.cache, workspaceID and targets when making the change.
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.

Inline comments:
In `@apps/workspace-engine/pkg/store/releasetargets/release_target_exists.go`:
- Around line 63-65: The current logic caches both true and false results via
s.cache.SetDefault(cacheKey, exists), which can serve stale false negatives;
update the code to only cache positive confirmations (i.e., call
s.cache.SetDefault(cacheKey, exists) only when exists == true) or make caching
behavior configurable (e.g., a short TTL or a flag) so that false results are
not cached long-term; modify the code around s.cache, cacheKey, and the variable
exists to implement this change.

---

Nitpick comments:
In `@apps/workspace-engine/pkg/store/releasetargets/get_for_workspace.go`:
- Around line 58-60: The current caching of entire workspace slices via
s.cache.SetDefault(workspaceID, targets) may blow memory for large workspaces;
modify the code in GetForWorkspace to only cache when the number of targets is
below a threshold (e.g., add const maxCachedTargets = N and if len(targets) <=
maxCachedTargets then s.cache.SetDefault(...)), or replace/augment s.cache with
an LRU-capable cache and use that instead so eviction controls memory; refer to
s.cache, workspaceID and targets when making the change.

ℹ️ Review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Pro

Run ID: 8eefe409-889c-48ac-bc76-8204aa94f008

📥 Commits

Reviewing files that changed from the base of the PR and between a71345b and 5ee28bd.

📒 Files selected for processing (6)
  • apps/workspace-engine/pkg/store/releasetargets/get_for_deployment.go
  • apps/workspace-engine/pkg/store/releasetargets/get_for_deployment_environment.go
  • apps/workspace-engine/pkg/store/releasetargets/get_for_resource.go
  • apps/workspace-engine/pkg/store/releasetargets/get_for_workspace.go
  • apps/workspace-engine/pkg/store/releasetargets/options.go
  • apps/workspace-engine/pkg/store/releasetargets/release_target_exists.go

Comment on lines +63 to +65
if s.cache != nil {
s.cache.SetDefault(cacheKey, exists)
}
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue | 🟡 Minor

Caching false results may cause staleness issues.

Caching a false existence result means that if a release target is created shortly after, the cached false will be served until TTL expiry. This could cause callers to incorrectly believe the target doesn't exist.

Consider whether caching only true results (existence confirmed) is safer for your use case, or if the TTL is short enough that temporary staleness is acceptable.

🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@apps/workspace-engine/pkg/store/releasetargets/release_target_exists.go`
around lines 63 - 65, The current logic caches both true and false results via
s.cache.SetDefault(cacheKey, exists), which can serve stale false negatives;
update the code to only cache positive confirmations (i.e., call
s.cache.SetDefault(cacheKey, exists) only when exists == true) or make caching
behavior configurable (e.g., a short TTL or a flag) so that false results are
not cached long-term; modify the code around s.cache, cacheKey, and the variable
exists to implement this change.

@adityachoudhari26 adityachoudhari26 merged commit be8e180 into main Mar 19, 2026
6 of 9 checks passed
@adityachoudhari26 adityachoudhari26 deleted the cacjomg-stores branch March 19, 2026 23:37
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants