-
Notifications
You must be signed in to change notification settings - Fork 108
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
repoID bitmap for speeding up findShard in compound shards #899
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
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 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 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 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 |
---|---|---|
|
@@ -16,6 +16,9 @@ import ( | |
|
||
"github.com/google/go-cmp/cmp" | ||
"github.com/google/go-cmp/cmp/cmpopts" | ||
"github.com/prometheus/client_golang/prometheus/testutil" | ||
"github.com/stretchr/testify/require" | ||
|
||
"github.com/sourcegraph/zoekt" | ||
) | ||
|
||
|
@@ -288,6 +291,80 @@ func TestPartialSuccess(t *testing.T) { | |
} | ||
} | ||
|
||
// Tests that we skip looping over repos in compound shards when we know that | ||
// the repository we are looking for is not in the shard. | ||
func TestSkipCompoundShards(t *testing.T) { | ||
metricCompoundShardLookups.Reset() | ||
|
||
compoundShards := [][]zoekt.Repository{ | ||
{ | ||
{Name: "repoA", ID: 1}, | ||
{Name: "repoB", ID: 2}, | ||
{Name: "repoC", ID: 3}, | ||
}, | ||
{ | ||
{Name: "repoD", ID: 4}, | ||
{Name: "repoE", ID: 5}, | ||
{Name: "repoF", ID: 6}, | ||
{Name: "repoF", ID: 7}, | ||
{Name: "repoF", ID: 8}, | ||
}, | ||
} | ||
var lookForRepoID uint32 = 99 | ||
wantSkippedCount := 2 | ||
|
||
indexDir := t.TempDir() | ||
for _, repositoryGroup := range compoundShards { | ||
createTestCompoundShard(t, indexDir, repositoryGroup) | ||
} | ||
o := &Options{ | ||
IndexDir: indexDir, | ||
RepositoryDescription: zoekt.Repository{ID: lookForRepoID}, | ||
} | ||
|
||
shard := o.findCompoundShard() | ||
require.Empty(t, shard) | ||
|
||
// Check if the "skipped" counter was incremented | ||
skippedCount := int(testutil.ToFloat64(metricCompoundShardLookups.WithLabelValues("skipped"))) | ||
require.Equal(t, wantSkippedCount, skippedCount) | ||
} | ||
|
||
// With optimization | ||
// BenchmarkFindCompoundShard-16 33505 36016 ns/op | ||
// | ||
// Without optimization | ||
// BenchmarkFindCompoundShard-16 76 15568589 ns/op | ||
func BenchmarkFindCompoundShard(b *testing.B) { | ||
// Generate a large compound shard | ||
const numRepos = 5000 | ||
repositories := make([]zoekt.Repository, numRepos) | ||
for i := 0; i < numRepos; i++ { | ||
repositories[i] = zoekt.Repository{ | ||
Name: fmt.Sprintf("repo%d", i+1), | ||
ID: uint32(i + 1), | ||
} | ||
} | ||
indexDir := b.TempDir() | ||
createTestCompoundShard(b, indexDir, repositories) | ||
|
||
// pick id that is not in the shard | ||
var searchRepoID uint32 = numRepos + 1 | ||
|
||
b.ResetTimer() | ||
for i := 0; i < b.N; i++ { | ||
o := &Options{ | ||
IndexDir: indexDir, | ||
RepositoryDescription: zoekt.Repository{ID: searchRepoID}, | ||
} | ||
|
||
shard := o.findCompoundShard() | ||
if shard != "" { | ||
b.Fatal("expected empty result") | ||
} | ||
} | ||
} | ||
|
||
func TestOptions_FindAllShards(t *testing.T) { | ||
type simpleShard struct { | ||
Repository zoekt.Repository | ||
|
@@ -361,17 +438,17 @@ func TestOptions_FindAllShards(t *testing.T) { | |
compoundShards: [][]zoekt.Repository{ | ||
{ | ||
{Name: "repoA", ID: 1}, | ||
{Name: "sameName", ID: 2}, | ||
{Name: "sameName", ID: 3}, | ||
{Name: "repoB", ID: 2}, | ||
{Name: "repoC", ID: 3}, | ||
}, | ||
{ | ||
{Name: "repoB", ID: 4}, | ||
{Name: "sameName", ID: 5}, | ||
{Name: "sameName", ID: 6}, | ||
{Name: "repoD", ID: 4}, | ||
{Name: "repoE", ID: 5}, | ||
{Name: "repoF", ID: 6}, | ||
}, | ||
}, | ||
expectedShardCount: 1, | ||
expectedRepository: zoekt.Repository{Name: "sameName", ID: 5}, | ||
expectedRepository: zoekt.Repository{Name: "something-else", ID: 5}, | ||
Comment on lines
-364
to
+451
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. fly-by change to make the intent of the test more obvious. Before, the name and id both matched so it was not obvious that we match by id only. |
||
}, | ||
} | ||
for _, tt := range tests { | ||
|
@@ -840,7 +917,7 @@ func TestIsLowPriority(t *testing.T) { | |
} | ||
} | ||
|
||
func createTestShard(t *testing.T, indexDir string, r zoekt.Repository, numShards int, optFns ...func(options *Options)) []string { | ||
func createTestShard(t testing.TB, indexDir string, r zoekt.Repository, numShards int, optFns ...func(options *Options)) []string { | ||
t.Helper() | ||
|
||
if err := os.MkdirAll(filepath.Dir(indexDir), 0o700); err != nil { | ||
|
@@ -891,7 +968,7 @@ func createTestShard(t *testing.T, indexDir string, r zoekt.Repository, numShard | |
return o.FindAllShards() | ||
} | ||
|
||
func createTestCompoundShard(t *testing.T, indexDir string, repositories []zoekt.Repository, optFns ...func(options *Options)) { | ||
func createTestCompoundShard(t testing.TB, indexDir string, repositories []zoekt.Repository, optFns ...func(options *Options)) { | ||
t.Helper() | ||
|
||
var shardNames []string | ||
|
This file contains 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 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 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 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 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
Binary file not shown.
Binary file not shown.
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.
Nice! I guess this answers my question here: