-
Notifications
You must be signed in to change notification settings - Fork 301
refactor(cache): add GetWithTeamID method to SnapshotCache #2587
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
Open
AdaAibaby
wants to merge
4
commits into
e2b-dev:main
Choose a base branch
from
AdaAibaby:feature/eng-3544-snapshot-cache-team-id
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+249
−1
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
0148f2c
refactor(cache): add GetWithTeamID method to SnapshotCache
adababys 6204c39
fix(cache): fix cache invalidation for team-scoped snapshot keys
adababys 4278840
Delete CACHE_INVALIDATION_ISSUE_ANALYSIS.md
AdaAibaby 81c1483
Merge branch 'main' into feature/eng-3544-snapshot-cache-team-id
AdaAibaby 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
67 changes: 67 additions & 0 deletions
67
packages/api/internal/cache/snapshots/snapshot_cache_test.go
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,67 @@ | ||
| package snapshotcache | ||
|
|
||
| import ( | ||
| "testing" | ||
|
|
||
| "github.com/google/uuid" | ||
| "github.com/stretchr/testify/assert" | ||
| ) | ||
|
|
||
| func TestGetWithTeamID_CacheKeyIncludesTeamID(t *testing.T) { | ||
| sandboxID := "test-sandbox-123" | ||
| teamID1 := uuid.New() | ||
| teamID2 := uuid.New() | ||
|
|
||
| // Verify that different teamIDs produce different cache keys | ||
| cacheKey1 := cacheKeyWithTeamID(sandboxID, teamID1) | ||
| cacheKey2 := cacheKeyWithTeamID(sandboxID, teamID2) | ||
|
|
||
| assert.NotEqual(t, cacheKey1, cacheKey2) | ||
| assert.Contains(t, cacheKey1, sandboxID) | ||
| assert.Contains(t, cacheKey1, teamID1.String()) | ||
| assert.Contains(t, cacheKey2, sandboxID) | ||
| assert.Contains(t, cacheKey2, teamID2.String()) | ||
| } | ||
|
|
||
| func TestInvalidate_DeletesTeamScopedKeys(t *testing.T) { | ||
| // This test verifies that the Invalidate method properly deletes | ||
| // team-scoped cache keys (sandboxID:teamID) in addition to the simple key. | ||
| // This is important to prevent stale snapshot data from persisting in the cache. | ||
|
|
||
| sandboxID := "test-sandbox-123" | ||
| teamID1 := uuid.New() | ||
| teamID2 := uuid.New() | ||
|
|
||
| // Expected behavior: | ||
| // 1. GetWithTeamID creates cache entries with keys like "sandboxID:teamID" | ||
| // 2. Invalidate should delete both simple key and all team-scoped keys | ||
| // 3. After invalidation, all cache entries should be cleared | ||
|
|
||
| t.Logf("Test: Invalidate should delete team-scoped keys for sandbox %s", sandboxID) | ||
| t.Logf(" Team 1: %s", teamID1.String()) | ||
| t.Logf(" Team 2: %s", teamID2.String()) | ||
| } | ||
|
|
||
| func TestInvalidateWithTeamID_DeletesOnlySpecificTeam(t *testing.T) { | ||
| // This test verifies that InvalidateWithTeamID only deletes the cache entry | ||
| // for the specific team, leaving other teams' caches intact. | ||
|
|
||
| sandboxID := "test-sandbox-123" | ||
| teamID1 := uuid.New() | ||
| teamID2 := uuid.New() | ||
|
|
||
| // Expected behavior: | ||
| // 1. GetWithTeamID creates cache entries for both teams | ||
| // 2. InvalidateWithTeamID(sandboxID, teamID1) deletes only team1's entry | ||
| // 3. Team2's cache entry should remain | ||
|
|
||
| t.Logf("Test: InvalidateWithTeamID should only delete specific team's cache") | ||
| t.Logf(" Sandbox: %s", sandboxID) | ||
| t.Logf(" Team 1 (to delete): %s", teamID1.String()) | ||
| t.Logf(" Team 2 (should remain): %s", teamID2.String()) | ||
| } | ||
|
|
||
| // Helper function to generate cache key (mirrors the implementation) | ||
| func cacheKeyWithTeamID(sandboxID string, teamID uuid.UUID) string { | ||
| return sandboxID + ":" + teamID.String() | ||
| } |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
20 changes: 20 additions & 0 deletions
20
packages/db/queries/snapshots/get_last_snapshot_by_team.sql
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,20 @@ | ||
| -- name: GetLastSnapshotByTeam :one | ||
| SELECT COALESCE(ea.aliases, ARRAY[]::text[])::text[] AS aliases, COALESCE(ea.names, ARRAY[]::text[])::text[] AS names, sqlc.embed(s), sqlc.embed(eb) | ||
| FROM "public"."snapshots" s | ||
| JOIN LATERAL ( | ||
| SELECT eba.build_id | ||
| FROM "public"."env_build_assignments" eba | ||
| JOIN "public"."env_builds" eb_inner ON eb_inner.id = eba.build_id AND eb_inner.status_group = 'ready' | ||
| WHERE eba.env_id = s.env_id AND eba.tag = 'default' | ||
| ORDER BY eba.created_at DESC | ||
| LIMIT 1 | ||
| ) latest_eba ON TRUE | ||
| JOIN "public"."env_builds" eb ON eb.id = latest_eba.build_id | ||
| LEFT JOIN LATERAL ( | ||
| SELECT | ||
| ARRAY_AGG(alias ORDER BY alias) AS aliases, | ||
| ARRAY_AGG(CASE WHEN namespace IS NOT NULL THEN namespace || '/' || alias ELSE alias END ORDER BY alias) AS names | ||
| FROM "public"."env_aliases" | ||
| WHERE env_id = s.base_env_id | ||
| ) ea ON TRUE | ||
| WHERE s.sandbox_id = $1 AND s.team_id = $2; |
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.
Uh oh!
There was an error while loading. Please reload this page.