Skip to content
This repository has been archived by the owner on Sep 30, 2024. It is now read-only.

Search backend: pass runtime dependencies at runtime #33760

Merged
merged 20 commits into from
Apr 12, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 9 additions & 8 deletions cmd/frontend/graphqlbackend/search_results.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,14 @@ import (
"github.com/sourcegraph/sourcegraph/internal/database"
"github.com/sourcegraph/sourcegraph/internal/deviceid"
"github.com/sourcegraph/sourcegraph/internal/featureflag"
"github.com/sourcegraph/sourcegraph/internal/gitserver"
"github.com/sourcegraph/sourcegraph/internal/goroutine"
"github.com/sourcegraph/sourcegraph/internal/honey"
searchhoney "github.com/sourcegraph/sourcegraph/internal/honey/search"
"github.com/sourcegraph/sourcegraph/internal/rcache"
"github.com/sourcegraph/sourcegraph/internal/search"
"github.com/sourcegraph/sourcegraph/internal/search/execute"
"github.com/sourcegraph/sourcegraph/internal/search/job"
"github.com/sourcegraph/sourcegraph/internal/search/job/jobutil"
"github.com/sourcegraph/sourcegraph/internal/search/query"
"github.com/sourcegraph/sourcegraph/internal/search/result"
Expand Down Expand Up @@ -437,12 +439,12 @@ func LogSearchLatency(ctx context.Context, db database.DB, wg *sync.WaitGroup, s
}
}

// JobArgs converts the parts of search resolver state to values needed to create search jobs.
func (r *searchResolver) JobArgs() *jobutil.Args {
Copy link
Member Author

Choose a reason for hiding this comment

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

jobutil.Args is superceded by job.RuntimeClients

return &jobutil.Args{
SearchInputs: r.SearchInputs,
func (r *searchResolver) JobClients() job.RuntimeClients {
return job.RuntimeClients{
DB: r.db,
Zoekt: r.zoekt,
SearcherURLs: r.searcherURLs,
Gitserver: gitserver.NewClient(r.db),
}
}

Expand Down Expand Up @@ -504,7 +506,7 @@ func logBatch(ctx context.Context, db database.DB, searchInputs *run.SearchInput
func (r *searchResolver) Results(ctx context.Context) (*SearchResultsResolver, error) {
start := time.Now()
agg := streaming.NewAggregatingStream()
alert, err := execute.Execute(ctx, r.db, agg, r.JobArgs())
alert, err := execute.Execute(ctx, agg, r.SearchInputs, r.JobClients())
srr := r.resultsToResolver(agg.Results, alert, agg.Stats)
srr.elapsed = time.Since(start)
logBatch(ctx, r.db, r.SearchInputs, srr, err)
Expand Down Expand Up @@ -591,16 +593,15 @@ func (r *searchResolver) Stats(ctx context.Context) (stats *searchResultsStats,
attempts := 0
var v *SearchResultsResolver

args := r.JobArgs()
for {
// Query search results.
var err error
j, err := jobutil.ToSearchJob(args, r.SearchInputs.Query, r.db)
j, err := jobutil.ToSearchJob(r.SearchInputs, r.SearchInputs.Query, r.db)
if err != nil {
return nil, err
}
agg := streaming.NewAggregatingStream()
alert, err := j.Run(ctx, r.db, agg)
alert, err := j.Run(ctx, r.JobClients(), agg)
if err != nil {
return nil, err // do not cache errors.
}
Expand Down
5 changes: 2 additions & 3 deletions cmd/frontend/graphqlbackend/search_results_stats_languages.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,15 +39,14 @@ func (srs *searchResultsStats) Languages(ctx context.Context) ([]*languageStatis
}

func (srs *searchResultsStats) getResults(ctx context.Context) (result.Matches, error) {
args := srs.sr.JobArgs()
srs.once.Do(func() {
j, err := jobutil.ToSearchJob(args, srs.sr.SearchInputs.Query, srs.sr.db)
j, err := jobutil.ToSearchJob(srs.sr.SearchInputs, srs.sr.SearchInputs.Query, srs.sr.db)
if err != nil {
srs.err = err
return
}
agg := streaming.NewAggregatingStream()
_, err = j.Run(ctx, srs.sr.db, agg)
_, err = j.Run(ctx, srs.sr.JobClients(), agg)
if err != nil {
srs.err = err
return
Expand Down
16 changes: 8 additions & 8 deletions enterprise/internal/codemonitors/search.go
Original file line number Diff line number Diff line change
Expand Up @@ -114,13 +114,13 @@ func Search(ctx context.Context, db database.DB, query string, monitorID int64,
}

// Inline job creation so we can mutate the commit job before running it
jobArgs := searchClient.JobArgs(inputs)
plan, err := predicate.Expand(ctx, db, jobArgs, inputs.Plan)
clients := searchClient.JobClients()
plan, err := predicate.Expand(ctx, clients, inputs, inputs.Plan)
if err != nil {
return nil, err
}

planJob, err := jobutil.FromExpandedPlan(jobArgs, plan, db)
planJob, err := jobutil.FromExpandedPlan(inputs, plan, db)
if err != nil {
return nil, err
}
Expand All @@ -137,7 +137,7 @@ func Search(ctx context.Context, db database.DB, query string, monitorID int64,

// Execute the search
agg := streaming.NewAggregatingStream()
_, err = planJob.Run(ctx, db, agg)
_, err = planJob.Run(ctx, clients, agg)
if err != nil {
return nil, err
}
Expand All @@ -164,13 +164,13 @@ func Snapshot(ctx context.Context, db database.DB, query string, monitorID int64
return err
}

jobArgs := searchClient.JobArgs(inputs)
plan, err := predicate.Expand(ctx, db, jobArgs, inputs.Plan)
clients := searchClient.JobClients()
plan, err := predicate.Expand(ctx, clients, inputs, inputs.Plan)
if err != nil {
return err
}

planJob, err := jobutil.FromExpandedPlan(jobArgs, plan, db)
planJob, err := jobutil.FromExpandedPlan(inputs, plan, db)
if err != nil {
return err
}
Expand All @@ -183,7 +183,7 @@ func Snapshot(ctx context.Context, db database.DB, query string, monitorID int64
return err
}

_, err = planJob.Run(ctx, db, streaming.NewNullStream())
_, err = planJob.Run(ctx, clients, streaming.NewNullStream())
return err
}

Expand Down
14 changes: 8 additions & 6 deletions internal/search/client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,10 @@ import (

"github.com/sourcegraph/sourcegraph/internal/database"
"github.com/sourcegraph/sourcegraph/internal/endpoint"
"github.com/sourcegraph/sourcegraph/internal/gitserver"
"github.com/sourcegraph/sourcegraph/internal/search"
"github.com/sourcegraph/sourcegraph/internal/search/execute"
"github.com/sourcegraph/sourcegraph/internal/search/job/jobutil"
"github.com/sourcegraph/sourcegraph/internal/search/job"
"github.com/sourcegraph/sourcegraph/internal/search/run"
"github.com/sourcegraph/sourcegraph/internal/search/streaming"
"github.com/sourcegraph/sourcegraph/schema"
Expand All @@ -33,7 +34,7 @@ type SearchClient interface {
inputs *run.SearchInputs,
) (_ *search.Alert, err error)

JobArgs(*run.SearchInputs) *jobutil.Args
JobClients() job.RuntimeClients
}

func NewSearchClient(db database.DB, zoektStreamer zoekt.Streamer, searcherURLs *endpoint.Map) SearchClient {
Expand Down Expand Up @@ -67,13 +68,14 @@ func (s *searchClient) Execute(
stream streaming.Sender,
inputs *run.SearchInputs,
) (*search.Alert, error) {
return execute.Execute(ctx, s.db, stream, s.JobArgs(inputs))
return execute.Execute(ctx, stream, inputs, s.JobClients())
}

func (s *searchClient) JobArgs(inputs *run.SearchInputs) *jobutil.Args {
return &jobutil.Args{
SearchInputs: inputs,
func (s *searchClient) JobClients() job.RuntimeClients {
return job.RuntimeClients{
DB: s.db,
Zoekt: s.zoekt,
SearcherURLs: s.searcherURLs,
Gitserver: gitserver.NewClient(s.db),
}
}
93 changes: 45 additions & 48 deletions internal/search/client/mock_client.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading