-
Notifications
You must be signed in to change notification settings - Fork 372
chore(services): store observed counts in permissionsServer #2929
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
Draft
barakmich
wants to merge
1
commit into
main
Choose a base branch
from
barakmich/ps_counts
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.
Draft
Changes from all commits
Commits
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,226 @@ | ||
| package v1 | ||
|
|
||
| import ( | ||
| "sync" | ||
| "testing" | ||
|
|
||
| "github.com/stretchr/testify/require" | ||
|
|
||
| "github.com/authzed/spicedb/pkg/query" | ||
| ) | ||
|
|
||
| func TestNewQueryPlanMetadata(t *testing.T) { | ||
| metadata := NewQueryPlanMetadata() | ||
| require.NotNil(t, metadata) | ||
| require.NotNil(t, metadata.stats) | ||
| require.Empty(t, metadata.GetStats()) | ||
| } | ||
|
|
||
| func TestQueryPlanMetadataMergeCountStats(t *testing.T) { | ||
| metadata := NewQueryPlanMetadata() | ||
| key1 := query.CanonicalKey("test-key-1") | ||
| key2 := query.CanonicalKey("test-key-2") | ||
|
|
||
| // First merge | ||
| counts1 := map[query.CanonicalKey]query.CountStats{ | ||
| key1: { | ||
| CheckCalls: 5, | ||
| IterSubjectsCalls: 3, | ||
| IterResourcesCalls: 2, | ||
| CheckResults: 10, | ||
| IterSubjectsResults: 6, | ||
| IterResourcesResults: 4, | ||
| }, | ||
| key2: { | ||
| CheckCalls: 2, | ||
| IterSubjectsCalls: 1, | ||
| IterResourcesCalls: 1, | ||
| CheckResults: 4, | ||
| IterSubjectsResults: 2, | ||
| IterResourcesResults: 2, | ||
| }, | ||
| } | ||
|
|
||
| metadata.MergeCountStats(counts1) | ||
|
|
||
| stats := metadata.GetStats() | ||
| require.Equal(t, counts1[key1], stats[key1]) | ||
| require.Equal(t, counts1[key2], stats[key2]) | ||
|
|
||
| // Second merge - should accumulate | ||
| counts2 := map[query.CanonicalKey]query.CountStats{ | ||
| key1: { | ||
| CheckCalls: 3, | ||
| IterSubjectsCalls: 2, | ||
| IterResourcesCalls: 1, | ||
| CheckResults: 6, | ||
| IterSubjectsResults: 4, | ||
| IterResourcesResults: 2, | ||
| }, | ||
| } | ||
|
|
||
| metadata.MergeCountStats(counts2) | ||
|
|
||
| stats = metadata.GetStats() | ||
| expected := query.CountStats{ | ||
| CheckCalls: 8, // 5 + 3 | ||
| IterSubjectsCalls: 5, // 3 + 2 | ||
| IterResourcesCalls: 3, // 2 + 1 | ||
| CheckResults: 16, // 10 + 6 | ||
| IterSubjectsResults: 10, // 6 + 4 | ||
| IterResourcesResults: 6, // 4 + 2 | ||
| } | ||
| require.Equal(t, expected, stats[key1]) | ||
| require.Equal(t, counts1[key2], stats[key2]) // key2 unchanged | ||
| } | ||
|
|
||
| func TestQueryPlanMetadataGetStatsReturnsACopy(t *testing.T) { | ||
| metadata := NewQueryPlanMetadata() | ||
| key := query.CanonicalKey("test-key") | ||
|
|
||
| counts := map[query.CanonicalKey]query.CountStats{ | ||
| key: { | ||
| CheckCalls: 5, | ||
| CheckResults: 10, | ||
| }, | ||
| } | ||
| metadata.MergeCountStats(counts) | ||
|
|
||
| stats1 := metadata.GetStats() | ||
| stats2 := metadata.GetStats() | ||
|
|
||
| // Should be equal | ||
| require.Equal(t, stats1, stats2) | ||
|
|
||
| // Modifying the returned map should not affect the metadata | ||
| stats1[key] = query.CountStats{CheckCalls: 999} | ||
| stats3 := metadata.GetStats() | ||
|
|
||
| require.NotEqual(t, 999, stats3[key].CheckCalls) | ||
| require.Equal(t, 5, stats3[key].CheckCalls) | ||
| } | ||
|
|
||
| func TestQueryPlanMetadataConcurrency(t *testing.T) { | ||
| metadata := NewQueryPlanMetadata() | ||
| key1 := query.CanonicalKey("test-key-1") | ||
| key2 := query.CanonicalKey("test-key-2") | ||
|
|
||
| var wg sync.WaitGroup | ||
| iterations := 100 | ||
|
|
||
| // Goroutine 1 - merges stats for key1 | ||
| wg.Add(1) | ||
| go func() { | ||
| defer wg.Done() | ||
| for i := 0; i < iterations; i++ { | ||
| metadata.MergeCountStats(map[query.CanonicalKey]query.CountStats{ | ||
| key1: {CheckCalls: 1, CheckResults: 2}, | ||
| }) | ||
| } | ||
| }() | ||
|
|
||
| // Goroutine 2 - merges stats for key2 | ||
| wg.Add(1) | ||
| go func() { | ||
| defer wg.Done() | ||
| for i := 0; i < iterations; i++ { | ||
| metadata.MergeCountStats(map[query.CanonicalKey]query.CountStats{ | ||
| key2: {IterSubjectsCalls: 1, IterSubjectsResults: 3}, | ||
| }) | ||
| } | ||
| }() | ||
|
|
||
| // Goroutine 3 - reads stats | ||
| wg.Add(1) | ||
| go func() { | ||
| defer wg.Done() | ||
| for i := 0; i < iterations; i++ { | ||
| _ = metadata.GetStats() | ||
| } | ||
| }() | ||
|
|
||
| wg.Wait() | ||
|
|
||
| // Verify final counts | ||
| stats := metadata.GetStats() | ||
| require.Equal(t, iterations, stats[key1].CheckCalls) | ||
| require.Equal(t, iterations*2, stats[key1].CheckResults) | ||
| require.Equal(t, iterations, stats[key2].IterSubjectsCalls) | ||
| require.Equal(t, iterations*3, stats[key2].IterSubjectsResults) | ||
| } | ||
|
|
||
| func TestQueryPlanMetadataEmptyMerge(t *testing.T) { | ||
| metadata := NewQueryPlanMetadata() | ||
|
|
||
| // Merging empty map should not cause issues | ||
| metadata.MergeCountStats(map[query.CanonicalKey]query.CountStats{}) | ||
|
|
||
| stats := metadata.GetStats() | ||
| require.Empty(t, stats) | ||
| } | ||
|
|
||
| func TestQueryPlanMetadataMergeMultipleKeys(t *testing.T) { | ||
| metadata := NewQueryPlanMetadata() | ||
|
|
||
| // Merge multiple keys at once | ||
| counts := map[query.CanonicalKey]query.CountStats{ | ||
| query.CanonicalKey("key1"): {CheckCalls: 1, CheckResults: 2}, | ||
| query.CanonicalKey("key2"): {IterSubjectsCalls: 3, IterSubjectsResults: 4}, | ||
| query.CanonicalKey("key3"): {IterResourcesCalls: 5, IterResourcesResults: 6}, | ||
| } | ||
|
|
||
| metadata.MergeCountStats(counts) | ||
|
|
||
| stats := metadata.GetStats() | ||
| require.Len(t, stats, 3) | ||
| require.Equal(t, counts[query.CanonicalKey("key1")], stats[query.CanonicalKey("key1")]) | ||
| require.Equal(t, counts[query.CanonicalKey("key2")], stats[query.CanonicalKey("key2")]) | ||
| require.Equal(t, counts[query.CanonicalKey("key3")], stats[query.CanonicalKey("key3")]) | ||
| } | ||
|
|
||
| func TestPermissionServerQueryPlanMetadataInitialization(t *testing.T) { | ||
| // Test that permissionServer properly initializes queryPlanMetadata | ||
| ps := &permissionServer{ | ||
| queryPlanMetadata: nil, // Explicitly set to nil to test lazy init | ||
| } | ||
|
|
||
| require.Nil(t, ps.queryPlanMetadata) | ||
|
|
||
| // The lazy initialization would happen in checkPermissionWithQueryPlan, | ||
| // but we can't easily test that without a full integration test. | ||
| // Instead, we verify that NewQueryPlanMetadata works correctly | ||
| ps.queryPlanMetadata = NewQueryPlanMetadata() | ||
| require.NotNil(t, ps.queryPlanMetadata) | ||
| require.Empty(t, ps.queryPlanMetadata.GetStats()) | ||
| } | ||
|
|
||
| func TestQueryPlanMetadataAccumulatesAcrossQueries(t *testing.T) { | ||
| metadata := NewQueryPlanMetadata() | ||
| key := query.CanonicalKey("test-iterator") | ||
|
|
||
| // Simulate first query execution | ||
| firstQueryStats := map[query.CanonicalKey]query.CountStats{ | ||
| key: { | ||
| CheckCalls: 2, | ||
| CheckResults: 3, | ||
| }, | ||
| } | ||
| metadata.MergeCountStats(firstQueryStats) | ||
|
|
||
| stats := metadata.GetStats() | ||
| require.Equal(t, 2, stats[key].CheckCalls) | ||
| require.Equal(t, 3, stats[key].CheckResults) | ||
|
|
||
| // Simulate second query execution - should accumulate | ||
| secondQueryStats := map[query.CanonicalKey]query.CountStats{ | ||
| key: { | ||
| CheckCalls: 1, | ||
| CheckResults: 2, | ||
| }, | ||
| } | ||
| metadata.MergeCountStats(secondQueryStats) | ||
|
|
||
| stats = metadata.GetStats() | ||
| require.Equal(t, 3, stats[key].CheckCalls, "Should accumulate: 2 + 1 = 3") | ||
| require.Equal(t, 5, stats[key].CheckResults, "Should accumulate: 3 + 2 = 5") | ||
| } |
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
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.
should we put a cap on how much this map can grow?