fork of https://github.com/sourcegraph/zoekt
0

Configure Feed

Select the types of activity you want to include in your feed.

1// Copyright 2016 Google Inc. All rights reserved. 2// 3// Licensed under the Apache License, Version 2.0 (the "License"); 4// you may not use this file except in compliance with the License. 5// You may obtain a copy of the License at 6// 7// http://www.apache.org/licenses/LICENSE-2.0 8// 9// Unless required by applicable law or agreed to in writing, software 10// distributed under the License is distributed on an "AS IS" BASIS, 11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12// See the License for the specific language governing permissions and 13// limitations under the License. 14 15package search 16 17import ( 18 "context" 19 "fmt" 20 "log" 21 "math" 22 "os" 23 "runtime" 24 "runtime/debug" 25 "slices" 26 "sort" 27 "strconv" 28 "sync" 29 "time" 30 31 "github.com/prometheus/client_golang/prometheus" 32 "github.com/prometheus/client_golang/prometheus/promauto" 33 "go.uber.org/atomic" 34 "golang.org/x/sync/semaphore" 35 36 "github.com/sourcegraph/zoekt" 37 "github.com/sourcegraph/zoekt/index" 38 "github.com/sourcegraph/zoekt/internal/tenant/systemtenant" 39 "github.com/sourcegraph/zoekt/internal/trace" 40 "github.com/sourcegraph/zoekt/query" 41) 42 43var ( 44 metricShardsLoaded = promauto.NewGauge(prometheus.GaugeOpts{ 45 Name: "zoekt_shards_loaded", 46 Help: "The number of shards currently loaded", 47 }) 48 metricShardsLoadedTotal = promauto.NewCounter(prometheus.CounterOpts{ 49 Name: "zoekt_shards_loaded_total", 50 Help: "The total number of shards loaded", 51 }) 52 metricShardsLoadFailedTotal = promauto.NewCounter(prometheus.CounterOpts{ 53 Name: "zoekt_shards_load_failed_total", 54 Help: "The total number of shard loads that failed", 55 }) 56 57 metricSearchRunning = promauto.NewGauge(prometheus.GaugeOpts{ 58 Name: "zoekt_search_running", 59 Help: "The number of concurrent search requests running", 60 }) 61 metricSearchShardRunning = promauto.NewGauge(prometheus.GaugeOpts{ 62 Name: "zoekt_search_shard_running", 63 Help: "The number of concurrent search requests in a shard running", 64 }) 65 metricSearchFailedTotal = promauto.NewCounter(prometheus.CounterOpts{ 66 Name: "zoekt_search_failed_total", 67 Help: "The total number of search requests that failed", 68 }) 69 metricSearchDuration = promauto.NewHistogram(prometheus.HistogramOpts{ 70 Name: "zoekt_search_duration_seconds", 71 Help: "The duration a search request took in seconds", 72 Buckets: prometheus.DefBuckets, // DefBuckets good for service timings 73 }) 74 75 // A Counter per Stat. Name should match field in zoekt.Stats. 76 metricSearchContentBytesLoadedTotal = promauto.NewCounter(prometheus.CounterOpts{ 77 Name: "zoekt_search_content_loaded_bytes_total", 78 Help: "Total amount of I/O for reading contents", 79 }) 80 metricSearchIndexBytesLoadedTotal = promauto.NewCounter(prometheus.CounterOpts{ 81 Name: "zoekt_search_index_loaded_bytes_total", 82 Help: "Total amount of I/O for reading from index", 83 }) 84 metricSearchCrashesTotal = promauto.NewCounter(prometheus.CounterOpts{ 85 Name: "zoekt_search_crashes_total", 86 Help: "Total number of search shards that had a crash", 87 }) 88 metricSearchFileCountTotal = promauto.NewCounter(prometheus.CounterOpts{ 89 Name: "zoekt_search_file_count_total", 90 Help: "Total number of files containing a match", 91 }) 92 metricSearchShardFilesConsideredTotal = promauto.NewCounter(prometheus.CounterOpts{ 93 Name: "zoekt_search_shard_files_considered_total", 94 Help: "Total number of files in shards that we considered", 95 }) 96 metricSearchFilesConsideredTotal = promauto.NewCounter(prometheus.CounterOpts{ 97 Name: "zoekt_search_files_considered_total", 98 Help: "Total files that we evaluated. Equivalent to files for which all atom matches (including negations) evaluated to true", 99 }) 100 metricSearchFilesLoadedTotal = promauto.NewCounter(prometheus.CounterOpts{ 101 Name: "zoekt_search_files_loaded_total", 102 Help: "Total files for which we loaded file content to verify substring matches", 103 }) 104 metricSearchFilesSkippedTotal = promauto.NewCounter(prometheus.CounterOpts{ 105 Name: "zoekt_search_files_skipped_total", 106 Help: "Total candidate files whose contents weren't examined because we gathered enough matches", 107 }) 108 metricSearchShardsSkippedTotal = promauto.NewCounter(prometheus.CounterOpts{ 109 Name: "zoekt_search_shards_skipped_total", 110 Help: "Total shards that we did not process because a query was canceled", 111 }) 112 metricSearchMatchCountTotal = promauto.NewCounter(prometheus.CounterOpts{ 113 Name: "zoekt_search_match_count_total", 114 Help: "Total number of non-overlapping matches", 115 }) 116 metricSearchNgramMatchesTotal = promauto.NewCounter(prometheus.CounterOpts{ 117 Name: "zoekt_search_ngram_matches_total", 118 Help: "Total number of candidate matches as a result of searching ngrams", 119 }) 120 metricSearchNgramLookupsTotal = promauto.NewCounter(prometheus.CounterOpts{ 121 Name: "zoekt_search_ngram_lookups_total", 122 Help: "Total number of times we accessed an ngram in the index", 123 }) 124 metricSearchRegexpsConsideredTotal = promauto.NewCounter(prometheus.CounterOpts{ 125 Name: "zoekt_search_regexps_considered_total", 126 Help: "Total number of times regexp was called on files that we evaluated", 127 }) 128 129 metricListRunning = promauto.NewGauge(prometheus.GaugeOpts{ 130 Name: "zoekt_list_running", 131 Help: "The number of concurrent list requests running", 132 }) 133 metricListShardRunning = promauto.NewGauge(prometheus.GaugeOpts{ 134 Name: "zoekt_list_shard_running", 135 Help: "The number of concurrent list requests in a shard running", 136 }) 137 metricShardsBatchReplaceDurationSeconds = promauto.NewHistogram(prometheus.HistogramOpts{ 138 Name: "zoekt_shards_batch_replace_duration_seconds", 139 Help: "The time it takes to replace a batch of Searchers.", 140 Buckets: []float64{0.01, 0.02, 0.05, 0.1, 0.2, 0.5, 1, 2, 5, 10, 30}, 141 }) 142 metricListAllRepos = promauto.NewGauge(prometheus.GaugeOpts{ 143 Name: "zoekt_list_all_stats_repos", 144 Help: "The last List(true) value for RepoStats.Repos. Repos is used for aggregrating the number of repositories.", 145 }) 146 metricListAllShards = promauto.NewGauge(prometheus.GaugeOpts{ 147 Name: "zoekt_list_all_stats_shards", 148 Help: "The last List(true) value for RepoStats.Shards. Shards is the total number of search shards.", 149 }) 150 metricListAllDocuments = promauto.NewGauge(prometheus.GaugeOpts{ 151 Name: "zoekt_list_all_stats_documents", 152 Help: "The last List(true) value for RepoStats.Documents. Documents holds the number of documents or files.", 153 }) 154 metricListAllIndexBytes = promauto.NewGauge(prometheus.GaugeOpts{ 155 Name: "zoekt_list_all_stats_index_bytes", 156 Help: "The last List(true) value for RepoStats.IndexBytes. IndexBytes is the amount of RAM used for index overhead.", 157 }) 158 metricListAllContentBytes = promauto.NewGauge(prometheus.GaugeOpts{ 159 Name: "zoekt_list_all_stats_content_bytes", 160 Help: "The last List(true) value for RepoStats.ContentBytes. ContentBytes is the amount of RAM used for raw content.", 161 }) 162 metricListAllNewLinesCount = promauto.NewGauge(prometheus.GaugeOpts{ 163 Name: "zoekt_list_all_stats_new_lines_count", 164 Help: "The last List(true) value for RepoStats.NewLinesCount.", 165 }) 166 metricListAllDefaultBranchNewLinesCount = promauto.NewGauge(prometheus.GaugeOpts{ 167 Name: "zoekt_list_all_stats_default_branch_new_lines_count", 168 Help: "The last List(true) value for RepoStats.DefaultBranchNewLinesCount.", 169 }) 170 metricListAllOtherBranchesNewLinesCount = promauto.NewGauge(prometheus.GaugeOpts{ 171 Name: "zoekt_list_all_stats_other_branches_new_lines_count", 172 Help: "The last List(true) value for RepoStats.OtherBranchesNewLinesCount.", 173 }) 174) 175 176type rankedShard struct { 177 zoekt.Searcher 178 179 priority float64 // maximum priority across all repos in the shard 180 181 // We have out of band ranking on compound shards which can change even if 182 // the shard file does not. So we compute a rank in getShards. We store 183 // repos here to avoid the cost of List in the search request path. 184 // 185 // repos is nil only if that call failed. 186 repos []*zoekt.Repository 187} 188 189// loaded stores the state we compute when updating the state of shards from 190// disk. 191type loaded struct { 192 // shards is the currently loaded shards sorted by decreasing rank and 193 // should not be mutated. 194 shards []*rankedShard 195 196 // ready is true if sharded searcher has finished loading all initial 197 // shards on startup. 198 ready bool 199} 200 201type shardedSearcher struct { 202 // Limit the number of parallel queries. Since searching is 203 // CPU bound, we can't do better than #CPU queries in 204 // parallel. If we do so, we just create more memory 205 // pressure. 206 sched scheduler 207 208 mu sync.Mutex // protects writes to shards 209 shards map[string]*rankedShard 210 211 ready atomic.Bool 212 ranked atomic.Value 213} 214 215func newShardedSearcher(n int64) *shardedSearcher { 216 ss := &shardedSearcher{ 217 shards: make(map[string]*rankedShard), 218 sched: newScheduler(n), 219 } 220 return ss 221} 222 223// NewDirectorySearcher returns a searcher instance that loads all 224// shards corresponding to a glob into memory. 225func NewDirectorySearcher(dir string) (zoekt.Streamer, error) { 226 return newDirectorySearcher(dir, true) 227} 228 229// NewDirectorySearcherFast is like NewDirectorySearcher, but does not block 230// on the initial loading of shards. 231// 232// This exists since in the case of zoekt-webserver we are happy with having 233// partial availability since that is better than no availability on large 234// instances. 235func NewDirectorySearcherFast(dir string) (zoekt.Streamer, error) { 236 return newDirectorySearcher(dir, false) 237} 238 239func newDirectorySearcher(dir string, waitUntilReady bool) (zoekt.Streamer, error) { 240 ss := newShardedSearcher(int64(runtime.GOMAXPROCS(0))) 241 tl := &loader{ 242 ss: ss, 243 } 244 dw, err := newDirectoryWatcher(dir, tl) 245 if err != nil { 246 return nil, err 247 } 248 249 if waitUntilReady { 250 if err := dw.WaitUntilReady(); err != nil { 251 return nil, err 252 } 253 } 254 255 ds := &directorySearcher{ 256 Streamer: ss, 257 directoryWatcher: dw, 258 } 259 260 return &typeRepoSearcher{Streamer: ds}, nil 261} 262 263type directorySearcher struct { 264 zoekt.Streamer 265 266 directoryWatcher *DirectoryWatcher 267} 268 269func (s *directorySearcher) Close() { 270 // We need to Stop directoryWatcher first since it calls load/unload on 271 // Searcher. 272 s.directoryWatcher.Stop() 273 s.Streamer.Close() 274} 275 276type loader struct { 277 ss *shardedSearcher 278} 279 280func (tl *loader) load(keys ...string) { 281 // This is called with all keys on startup, so once this function has 282 // finished running shardedSearcher will be ready. 283 defer tl.ss.markReady() 284 285 if len(keys) == 0 { 286 // If there's nothing to load, we exit early here, but we want to mark 287 // ourselves as ready. 288 return 289 } 290 291 var ( 292 mu sync.Mutex // synchronizes writes to the shards map 293 wg sync.WaitGroup // used to wait for all shards to load 294 sem = semaphore.NewWeighted(int64(runtime.GOMAXPROCS(0))) 295 loadedShards = make(map[string]zoekt.Searcher) 296 ) 297 298 publishLoaded := func() { 299 mu.Lock() 300 chunk := loadedShards 301 loadedShards = make(map[string]zoekt.Searcher) 302 mu.Unlock() 303 tl.ss.replace(chunk) 304 } 305 306 log.Printf("[INFO] loading %d shard(s): %s", len(keys), humanTruncateList(keys, 5)) 307 308 lastProgress := time.Now() 309 for i, key := range keys { 310 // If taking a while to start-up occasionally give a progress message 311 if time.Since(lastProgress) > 5*time.Second { 312 log.Printf("[INFO] still need to load %d shards...", len(keys)-i) 313 lastProgress = time.Now() 314 315 publishLoaded() 316 } 317 318 _ = sem.Acquire(context.Background(), 1) 319 wg.Add(1) 320 321 go func(key string) { 322 defer sem.Release(1) 323 defer wg.Done() 324 325 shard, err := loadShard(key) 326 if err != nil { 327 metricShardsLoadFailedTotal.Inc() 328 log.Printf("[ERROR] reloading: %s, err %v ", key, err) 329 return 330 } 331 metricShardsLoadedTotal.Inc() 332 333 mu.Lock() 334 loadedShards[key] = shard 335 mu.Unlock() 336 }(key) 337 } 338 339 wg.Wait() 340 341 publishLoaded() 342} 343 344func (tl *loader) drop(keys ...string) { 345 shards := make(map[string]zoekt.Searcher, len(keys)) 346 for _, key := range keys { 347 shards[key] = nil 348 } 349 tl.ss.replace(shards) 350} 351 352func (ss *shardedSearcher) String() string { 353 return "shardedSearcher" 354} 355 356// Close closes references to open files. It may be called only once. 357func (ss *shardedSearcher) Close() { 358 ss.mu.Lock() 359 shards := make(map[string]zoekt.Searcher, len(ss.shards)) 360 for k := range ss.shards { 361 shards[k] = nil 362 } 363 ss.mu.Unlock() 364 365 ss.replace(shards) 366} 367 368func selectRepoSet(shards []*rankedShard, q query.Q) ([]*rankedShard, query.Q) { 369 and, ok := q.(*query.And) 370 if ok { 371 return doSelectRepoSet(shards, and) 372 } 373 374 // We have queries which look like (reposet ...) and we want to do the same 375 // optimizations. To simplify we just always wrap the query in And and then 376 // on the return value call Simplify to unwrap. In particular this is 377 // important for List calls. 378 and = &query.And{Children: []query.Q{q}} 379 shards, q = doSelectRepoSet(shards, and) 380 return shards, query.Simplify(q) 381} 382 383func doSelectRepoSet(shards []*rankedShard, and *query.And) ([]*rankedShard, query.Q) { 384 // (and (reposet ...) (q)) 385 // (and true (q)) with a filtered shards 386 // (and false) // noop 387 388 // (and (repobranches ...) (q)) 389 // (and (repobranches ...) (q)) 390 391 // Note: we also support (and (repo ...) (q)) even though sourcegraph does 392 // not generate those sorts of queries. This is to support manual testing. 393 394 hasReposForPredicate := func(pred func(repo *zoekt.Repository) bool) func(repos []*zoekt.Repository) (any, all bool) { 395 return func(repos []*zoekt.Repository) (any, all bool) { 396 any = false 397 all = true 398 for _, repo := range repos { 399 b := pred(repo) 400 any = any || b 401 all = all && b 402 } 403 return any, all 404 } 405 } 406 407 for i, c := range and.Children { 408 var setSize int 409 var hasRepos func([]*zoekt.Repository) (bool, bool) 410 switch setQuery := c.(type) { 411 case *query.RepoSet: 412 setSize = len(setQuery.Set) 413 hasRepos = hasReposForPredicate(func(repo *zoekt.Repository) bool { 414 return setQuery.Set[repo.Name] 415 }) 416 case *query.RepoIDs: 417 setSize = int(setQuery.Repos.GetCardinality()) 418 hasRepos = hasReposForPredicate(func(repo *zoekt.Repository) bool { 419 return setQuery.Repos.Contains(repo.ID) 420 }) 421 case *query.Repo: 422 setSize = 0 423 hasRepos = hasReposForPredicate(func(repo *zoekt.Repository) bool { 424 return setQuery.Regexp.MatchString(repo.Name) 425 }) 426 case *query.BranchesRepos: 427 for _, br := range setQuery.List { 428 setSize += int(br.Repos.GetCardinality()) 429 } 430 431 hasRepos = hasReposForPredicate(func(repo *zoekt.Repository) bool { 432 for _, br := range setQuery.List { 433 if br.Repos.Contains(repo.ID) { 434 return true 435 } 436 } 437 return false 438 }) 439 default: 440 continue 441 } 442 443 // setSize may be larger than the number of shards we have. The size of 444 // filtered is bounded by min(len(set), len(shards)) 445 if setSize > len(shards) { 446 setSize = len(shards) 447 } 448 449 filtered := make([]*rankedShard, 0, setSize) 450 filteredAll := true 451 452 for _, s := range shards { 453 if s.repos == nil { 454 // repos is nil if we failed to List the shard. This shouldn't 455 // happen, but if it does we don't know what is in it and must search 456 // it without simplifying the query. 457 filtered = append(filtered, s) 458 filteredAll = false 459 } else if any, all := hasRepos(s.repos); any { 460 filtered = append(filtered, s) 461 filteredAll = filteredAll && all 462 } 463 } 464 465 // We don't need to adjust the query since we are returning an empty set 466 // of shards to search. 467 if len(filtered) == 0 { 468 return filtered, and 469 } 470 471 // We can't simplify the query since we are searching shards which contain 472 // repos we aren't supposed to search. 473 if !filteredAll { 474 return filtered, and 475 } 476 477 // We don't want to mutate the original and, so we clone it before 478 // mutating it. 479 and = &query.And{Children: slices.Clone(and.Children)} 480 481 // This optimization allows us to avoid the work done by 482 // indexData.simplify for each shard. 483 // 484 // For example if our query is (and (reposet foo bar) (content baz)) 485 // then at this point filtered is [foo bar] and q is the same. For each 486 // shard indexData.simplify will simplify to (and true (content baz)) -> 487 // (content baz). This work can be done now once, rather than per shard. 488 switch c := c.(type) { 489 case *query.RepoSet, *query.RepoIDs, *query.Repo: 490 and.Children[i] = &query.Const{Value: true} 491 return filtered, query.Simplify(and) 492 493 case *query.BranchesRepos: 494 // We can only replace if all the repos want the same branches. We 495 // simplify and just check that we are requesting 1 branch. The common 496 // case is just asking for HEAD, so this should be effective. 497 if len(c.List) != 1 { 498 return filtered, and 499 } 500 501 // Every repo wants the same branches, so we can replace RepoBranches 502 // with a list of branch queries. 503 and.Children[i] = &query.Branch{Pattern: c.List[0].Branch, Exact: true} 504 return filtered, query.Simplify(and) 505 } 506 507 // Stop after first RepoSet, otherwise we might append duplicate 508 // shards to `filtered` 509 return filtered, and 510 } 511 512 return shards, and 513} 514 515func (ss *shardedSearcher) Search(ctx context.Context, q query.Q, opts *zoekt.SearchOptions) (sr *zoekt.SearchResult, err error) { 516 tr, ctx := trace.New(ctx, "shardedSearcher.Search", "") 517 tr.LazyLog(q, true) 518 tr.LazyPrintf("opts: %+v", opts) 519 defer func() { 520 if sr != nil { 521 tr.LazyPrintf("num files: %d", len(sr.Files)) 522 tr.LazyPrintf("stats: %+v", sr.Stats) 523 } 524 if err != nil { 525 tr.LazyPrintf("error: %v", err) 526 tr.SetError(err) 527 } 528 tr.Finish() 529 }() 530 ctx, cancel := context.WithCancel(ctx) 531 defer cancel() 532 533 collectSender := newCollectSender(opts) 534 535 start := time.Now() 536 proc, err := ss.sched.Acquire(ctx) 537 if err != nil { 538 return nil, err 539 } 540 defer proc.Release() 541 tr.LazyPrintf("acquired process") 542 543 wait := time.Since(start) 544 start = time.Now() 545 546 loaded := ss.getLoaded() 547 done, err := streamSearch(ctx, proc, q, opts, loaded.shards, collectSender) 548 defer done() 549 if err != nil { 550 return nil, err 551 } 552 553 aggregate, ok := collectSender.Done() 554 if !ok { 555 aggregate = &zoekt.SearchResult{ 556 RepoURLs: map[string]string{}, 557 LineFragments: map[string]string{}, 558 } 559 } 560 561 copyFiles(aggregate) 562 563 if !loaded.ready { 564 // We may have missed results due to not being fully loaded. 565 aggregate.Stats.Crashes++ 566 } 567 568 aggregate.Stats.Wait = wait 569 aggregate.Stats.Duration = time.Since(start) 570 571 return aggregate, nil 572} 573 574func (ss *shardedSearcher) StreamSearch(ctx context.Context, q query.Q, opts *zoekt.SearchOptions, sender zoekt.Sender) (err error) { 575 tr, ctx := trace.New(ctx, "shardedSearcher.StreamSearch", "") 576 defer func() { 577 if err != nil { 578 tr.LazyPrintf("error: %v", err) 579 tr.SetError(err) 580 } 581 tr.Finish() 582 }() 583 584 start := time.Now() 585 proc, err := ss.sched.Acquire(ctx) 586 if err != nil { 587 return err 588 } 589 defer proc.Release() 590 tr.LazyPrintf("acquired process") 591 592 loaded := ss.getLoaded() 593 shards := loaded.shards 594 595 maxPendingPriority := math.Inf(-1) 596 if len(shards) > 0 { 597 maxPendingPriority = shards[0].priority 598 } 599 600 stillLoadingCrashes := 0 601 if !loaded.ready { 602 // We may have missed results due to not being fully loaded. 603 stillLoadingCrashes++ 604 } 605 606 sender.Send(&zoekt.SearchResult{ 607 Stats: zoekt.Stats{ 608 Crashes: stillLoadingCrashes, 609 Wait: time.Since(start), 610 }, 611 Progress: zoekt.Progress{ 612 MaxPendingPriority: maxPendingPriority, 613 }, 614 }) 615 616 // Matches flow from the shards up the stack in the following order: 617 // 618 // 1. Search shards 619 // 2. flushCollectSender (aggregate) 620 // 3. limitSender (limit) 621 // 4. copyFileSender (copy) 622 // 623 // For streaming, the wrapping has to happen in the inverted order. 624 sender = copyFileSender(sender) 625 626 if truncator, hasLimits := index.NewDisplayTruncator(opts); hasLimits { 627 var cancel context.CancelFunc 628 ctx, cancel = context.WithCancel(ctx) 629 defer cancel() 630 sender = limitSender(cancel, sender, truncator) 631 } 632 633 sender, flush := newFlushCollectSender(opts, sender) 634 635 done, err := streamSearch(ctx, proc, q, opts, shards, sender) 636 637 // Even though streaming is done, we may have results sitting in a buffer we 638 // need to flush. So we need to send those before calling done. 639 flush() 640 done() 641 642 return err 643} 644 645// streamSearch is an internal helper since both Search and StreamSearch are 646// largely similar. 647// 648// done must always be called, even if err is non-nil. The SearchResults sent 649// via sender contain references to the underlying mmap data that the garbage 650// collector can't see. Calling done informs the garbage collector it is free 651// to collect those shards. The caller must call copyFiles on any 652// SearchResults it returns/streams out before calling done. 653func streamSearch(ctx context.Context, proc *process, q query.Q, opts *zoekt.SearchOptions, shards []*rankedShard, sender zoekt.Sender) (done func(), err error) { 654 tr, ctx := trace.New(ctx, "shardedSearcher.streamSearch", "") 655 overallStart := time.Now() 656 metricSearchRunning.Inc() 657 defer func() { 658 metricSearchRunning.Dec() 659 metricSearchDuration.Observe(time.Since(overallStart).Seconds()) 660 if err != nil { 661 metricSearchFailedTotal.Inc() 662 663 tr.LazyPrintf("error: %v", err) 664 tr.SetError(err) 665 } 666 tr.Finish() 667 }() 668 669 // Select the subset of shards that we will search over for the given query. 670 { 671 beforeLen := len(shards) 672 beforeQ := q 673 shards, q = selectRepoSet(shards, q) 674 tr.LazyPrintf("selectRepoSet shards=%d->%d q=%s->%s", beforeLen, len(shards), beforeQ, q) 675 } 676 677 if len(shards) == 0 { 678 return func() {}, nil 679 } 680 681 var cancel context.CancelFunc 682 if opts.MaxWallTime == 0 { 683 ctx, cancel = context.WithCancel(ctx) 684 } else { 685 ctx, cancel = context.WithTimeout(ctx, opts.MaxWallTime) 686 } 687 688 defer cancel() 689 690 // We set the number of workers to GOMAXPROCS, or the number of shards, 691 // whichever is smaller. 692 workers := min(runtime.GOMAXPROCS(0), len(shards)) 693 694 type result struct { 695 priority float64 696 *zoekt.SearchResult 697 err error 698 } 699 700 var ( 701 // buffered channels to continue searching when sending back results 702 // takes a while / blocks. The maximum pending result set is workers * 2. 703 results = make(chan *result, workers) 704 search = make(chan *rankedShard, workers) 705 wg sync.WaitGroup 706 ) 707 708 // Start workers that receive shards from the search channel, search them, 709 // and send the results to the results channel. This process is repeated 710 // until the search channel is closed. 711 // 712 // Note: Making "search" a buffered channel has the effect of limiting the number of parallel shard searches. 713 // Since searching is mostly CPU bound, limiting parallel shard searches also reduces the peak working set. 714 wg.Add(workers) 715 for range workers { 716 go func() { 717 defer wg.Done() 718 for s := range search { 719 sr, err := searchOneShard(ctx, s, q, opts) 720 r := &result{priority: s.priority, SearchResult: sr, err: err} 721 results <- r 722 } 723 }() 724 } 725 726 go func() { 727 wg.Wait() 728 close(results) 729 }() 730 731 var ( 732 pending = make(prioritySlice, 0, workers) 733 shard = 0 734 next = shards[shard] 735 736 // We need a separate nil-able reference to the same channel so we can close(search) for the worker 737 // go-routines to finish but also set work to nil in order for the select statement below to ignore 738 // that case when we want to stop a search. This is needed because sending on a closed channel panics. 739 work = search 740 ) 741 742 stop := func() { 743 if work != nil { 744 close(search) 745 work = nil 746 next = nil 747 } 748 } 749 750 // tracked so we can stop when we hit TotalMaxMatchCount 751 var totalMatchCount int 752 753search: 754 for { 755 // At the top of each iteration, have the proc associated with this search yield its won "timeslice" 756 // to possibly allow other searches to make progress 757 _ = proc.Yield(ctx) // Note: we let searchOneShard handle context errors 758 759 select { 760 case work <- next: // is there a worker available to search the next shard? 761 pending.append(next.priority) 762 763 shard++ 764 if shard == len(shards) { 765 stop() 766 } else { 767 next = shards[shard] 768 } 769 case r, ok := <-results: // is there a result to send back? 770 if !ok { 771 break search 772 } 773 774 // delete this result's priority from pending before computing the new max pending priority 775 pending.remove(r.priority) 776 777 if r.err != nil { 778 // Set final error and stop searching new shards, but consume any pending 779 // search results. 780 stop() 781 err = r.err 782 continue 783 } 784 785 // Update the match count statistics and stop searching new shards if we've 786 // reached the limit set in the options. 787 totalMatchCount += r.SearchResult.Stats.MatchCount 788 if opts.TotalMaxMatchCount > 0 && totalMatchCount > opts.TotalMaxMatchCount { 789 stop() 790 } 791 792 observeMetrics(r.SearchResult) 793 794 r.Priority = r.priority 795 r.MaxPendingPriority = pending.max() 796 797 sendByRepository(r.SearchResult, opts, sender) // send the result back to the client 798 } 799 } 800 801 return func() { runtime.KeepAlive(shards) }, err 802} 803 804// sendByRepository splits a zoekt.SearchResult by repository and calls 805// sender.Send for each batch. Ranking in Sourcegraph expects zoekt.SearchResult 806// to contain results with the same zoekt.SearchResult.priority only. 807// 808// We split by repository instead of by priority because it is easier to set 809// RepoURLs and LineFragments in zoekt.SearchResult. 810func sendByRepository(result *zoekt.SearchResult, opts *zoekt.SearchOptions, sender zoekt.Sender) { 811 if len(result.RepoURLs) <= 1 || len(result.Files) == 0 { 812 index.SortFiles(result.Files) 813 sender.Send(result) 814 return 815 } 816 817 send := func(repoName string, a, b int, stats zoekt.Stats) { 818 index.SortFiles(result.Files[a:b]) 819 820 filteredRepoURLs := map[string]string{repoName: result.RepoURLs[repoName]} 821 filteredLineFragments := map[string]string{repoName: result.LineFragments[repoName]} 822 823 // Filter RepoURLs and LineFragments to only those of repoName and its 824 // subRepositories if there are subRepositories 825 for _, file := range result.Files[a:b] { 826 name := file.SubRepositoryName 827 if name == "" { 828 continue 829 } 830 _, repoSet := filteredRepoURLs[name] 831 url, ok := result.RepoURLs[name] 832 if !repoSet && ok { 833 filteredRepoURLs[name] = url 834 } 835 _, fragSet := filteredLineFragments[name] 836 frag, ok := result.LineFragments[name] 837 if !fragSet && ok { 838 filteredLineFragments[name] = frag 839 } 840 } 841 842 sender.Send(&zoekt.SearchResult{ 843 Stats: stats, 844 Progress: zoekt.Progress{ 845 Priority: result.Files[a].RepositoryPriority, 846 MaxPendingPriority: result.MaxPendingPriority, 847 }, 848 Files: result.Files[a:b], 849 RepoURLs: filteredRepoURLs, 850 LineFragments: filteredLineFragments, 851 }) 852 } 853 854 var startIndex, endIndex int 855 curRepoID := result.Files[0].RepositoryID 856 curRepoName := result.Files[0].Repository 857 858 fm := zoekt.FileMatch{} 859 for endIndex, fm = range result.Files { 860 if curRepoID != fm.RepositoryID { 861 // Stats must stay aggregate-able, hence we sent the aggregate stats with the 862 // last event. 863 send(curRepoName, startIndex, endIndex, zoekt.Stats{}) 864 865 startIndex = endIndex 866 curRepoID = fm.RepositoryID 867 curRepoName = fm.Repository 868 } 869 } 870 871 send(curRepoName, startIndex, endIndex+1, result.Stats) 872} 873 874func observeMetrics(sr *zoekt.SearchResult) { 875 metricSearchContentBytesLoadedTotal.Add(float64(sr.Stats.ContentBytesLoaded)) 876 metricSearchIndexBytesLoadedTotal.Add(float64(sr.Stats.IndexBytesLoaded)) 877 metricSearchCrashesTotal.Add(float64(sr.Stats.Crashes)) 878 metricSearchFileCountTotal.Add(float64(sr.Stats.FileCount)) 879 metricSearchShardFilesConsideredTotal.Add(float64(sr.Stats.ShardFilesConsidered)) 880 metricSearchFilesConsideredTotal.Add(float64(sr.Stats.FilesConsidered)) 881 metricSearchFilesLoadedTotal.Add(float64(sr.Stats.FilesLoaded)) 882 metricSearchFilesSkippedTotal.Add(float64(sr.Stats.FilesSkipped)) 883 metricSearchShardsSkippedTotal.Add(float64(sr.Stats.ShardsSkipped)) 884 metricSearchMatchCountTotal.Add(float64(sr.Stats.MatchCount)) 885 metricSearchNgramMatchesTotal.Add(float64(sr.Stats.NgramMatches)) 886 metricSearchNgramLookupsTotal.Add(float64(sr.Stats.NgramLookups)) 887 metricSearchRegexpsConsideredTotal.Add(float64(sr.Stats.RegexpsConsidered)) 888} 889 890func copySlice(src *[]byte) { 891 if *src == nil { 892 return 893 } 894 dst := make([]byte, len(*src)) 895 copy(dst, *src) 896 *src = dst 897} 898 899func copyFiles(sr *zoekt.SearchResult) { 900 for i := range sr.Files { 901 copySlice(&sr.Files[i].Content) 902 copySlice(&sr.Files[i].Checksum) 903 for l := range sr.Files[i].LineMatches { 904 copySlice(&sr.Files[i].LineMatches[l].Line) 905 copySlice(&sr.Files[i].LineMatches[l].Before) 906 copySlice(&sr.Files[i].LineMatches[l].After) 907 } 908 for c := range sr.Files[i].ChunkMatches { 909 copySlice(&sr.Files[i].ChunkMatches[c].Content) 910 } 911 } 912} 913 914func searchOneShard(ctx context.Context, s zoekt.Searcher, q query.Q, opts *zoekt.SearchOptions) (sr *zoekt.SearchResult, err error) { 915 metricSearchShardRunning.Inc() 916 defer func() { 917 metricSearchShardRunning.Dec() 918 if e := recover(); e != nil { 919 log.Printf("[ERROR] crashed shard: %s: %#v, %s", s, e, debug.Stack()) 920 921 if sr == nil { 922 sr = &zoekt.SearchResult{} 923 } 924 sr.Stats.Crashes = 1 925 } 926 }() 927 928 return s.Search(ctx, q, opts) 929} 930 931type shardListResult struct { 932 rl *zoekt.RepoList 933 err error 934} 935 936func listOneShard(ctx context.Context, s zoekt.Searcher, q query.Q, opts *zoekt.ListOptions, sink chan shardListResult) { 937 metricListShardRunning.Inc() 938 defer func() { 939 metricListShardRunning.Dec() 940 if r := recover(); r != nil { 941 log.Printf("[ERROR] crashed shard: %s: %s, %s", s.String(), r, debug.Stack()) 942 sink <- shardListResult{ 943 &zoekt.RepoList{Crashes: 1}, nil, 944 } 945 } 946 }() 947 948 ms, err := s.List(ctx, q, opts) 949 sink <- shardListResult{ms, err} 950} 951 952func (ss *shardedSearcher) List(ctx context.Context, q query.Q, opts *zoekt.ListOptions) (rl *zoekt.RepoList, err error) { 953 tr, ctx := trace.New(ctx, "shardedSearcher.List", "") 954 metricListRunning.Inc() 955 defer func() { 956 metricListRunning.Dec() 957 if rl != nil { 958 tr.LazyPrintf("repos.size=%d reposmap.size=%d crashes=%d stats=%+v", len(rl.Repos), len(rl.ReposMap), rl.Crashes, rl.Stats) 959 } 960 if err != nil { 961 tr.LazyPrintf("error: %v", err) 962 tr.SetError(err) 963 } 964 tr.Finish() 965 }() 966 967 q = query.Simplify(q) 968 isAll := false 969 if c, ok := q.(*query.Const); ok { 970 isAll = c.Value 971 } 972 973 proc, err := ss.sched.Acquire(ctx) 974 if err != nil { 975 return nil, err 976 } 977 defer proc.Release() 978 tr.LazyPrintf("acquired process") 979 980 loaded := ss.getLoaded() 981 shards := loaded.shards 982 983 // Setup what we return now, since we may short circuit if there are no 984 // shards to search. 985 stillLoadingCrashes := 0 986 if !loaded.ready { 987 // We may have missed results due to not being fully loaded. 988 stillLoadingCrashes++ 989 } 990 agg := zoekt.RepoList{ 991 Crashes: stillLoadingCrashes, 992 ReposMap: zoekt.ReposMap{}, 993 Repos: []*zoekt.RepoListEntry{}, 994 } 995 996 // PERF: Select the subset of shards that we will search over for the given 997 // query. A common List query only asks for a specific repo, so this is an 998 // important optimization. 999 { 1000 beforeLen := len(shards) 1001 beforeQ := q 1002 shards, q = selectRepoSet(shards, q) 1003 tr.LazyPrintf("selectRepoSet shards=%d->%d q=%s->%s", beforeLen, len(shards), beforeQ, q) 1004 } 1005 1006 if len(shards) == 0 { 1007 return &agg, nil 1008 } 1009 1010 shardCount := len(shards) 1011 all := make(chan shardListResult, shardCount) 1012 feeder := make(chan zoekt.Searcher, len(shards)) 1013 for _, s := range shards { 1014 feeder <- s 1015 } 1016 close(feeder) 1017 1018 for range runtime.GOMAXPROCS(0) { 1019 go func() { 1020 for s := range feeder { 1021 listOneShard(ctx, s, q, opts, all) 1022 } 1023 }() 1024 } 1025 1026 uniq := map[string]*zoekt.RepoListEntry{} 1027 1028 for range shards { 1029 r := <-all 1030 if r.err != nil { 1031 return nil, r.err 1032 } 1033 1034 agg.Crashes += r.rl.Crashes 1035 agg.Stats.Add(&r.rl.Stats) 1036 1037 for _, r := range r.rl.Repos { 1038 prev, ok := uniq[r.Repository.Name] 1039 if !ok { 1040 cp := *r // We need to copy because we mutate r.Stats when merging duplicates 1041 uniq[r.Repository.Name] = &cp 1042 } else { 1043 prev.Stats.Add(&r.Stats) 1044 } 1045 } 1046 1047 for id, r := range r.rl.ReposMap { 1048 _, ok := agg.ReposMap[id] 1049 if !ok { 1050 agg.ReposMap[id] = r 1051 } 1052 } 1053 } 1054 1055 agg.Repos = make([]*zoekt.RepoListEntry, 0, len(uniq)) 1056 for _, r := range uniq { 1057 agg.Repos = append(agg.Repos, r) 1058 } 1059 1060 // Only one of these fields is populated and in all cases the size of that 1061 // field is the number of Repos. 1062 // 1063 // Note: we don't just add individual Stats.Repos since a repository can 1064 // have multiple shards. 1065 agg.Stats.Repos = len(uniq) + len(agg.ReposMap) 1066 1067 if isAll && len(agg.Repos) > 0 { 1068 reportListAllMetrics(agg.Repos) 1069 } 1070 1071 return &agg, nil 1072} 1073 1074func reportListAllMetrics(repos []*zoekt.RepoListEntry) { 1075 var stats zoekt.RepoStats 1076 for _, r := range repos { 1077 stats.Add(&r.Stats) 1078 } 1079 1080 metricListAllRepos.Set(float64(stats.Repos)) 1081 metricListAllIndexBytes.Set(float64(stats.IndexBytes)) 1082 metricListAllContentBytes.Set(float64(stats.ContentBytes)) 1083 metricListAllDocuments.Set(float64(stats.Documents)) 1084 metricListAllShards.Set(float64(stats.Shards)) 1085 metricListAllNewLinesCount.Set(float64(stats.NewLinesCount)) 1086 metricListAllDefaultBranchNewLinesCount.Set(float64(stats.DefaultBranchNewLinesCount)) 1087 metricListAllOtherBranchesNewLinesCount.Set(float64(stats.OtherBranchesNewLinesCount)) 1088} 1089 1090// getLoaded returns the currently loaded shards. Shared so do not mutate. 1091func (s *shardedSearcher) getLoaded() loaded { 1092 // next commit will store the true value of this, for now we keep the 1093 // backwards compatible behaviour. 1094 ready := s.ready.Load() 1095 // ranked is loaded after ready to avoid a race were ready is true but 1096 // ranked is still not the final set of shards. 1097 ranked, _ := s.ranked.Load().([]*rankedShard) 1098 return loaded{ 1099 shards: ranked, 1100 ready: ready, 1101 } 1102} 1103 1104func mkRankedShard(s zoekt.Searcher) *rankedShard { 1105 q := query.Const{Value: true} 1106 // We need to use WithUnsafeContext here, otherwise we cannot return a proper 1107 // rankedShard. On the user request path we use selectRepoSet which relies on 1108 // rankedShard.repos being set. 1109 result, err := s.List(systemtenant.WithUnsafeContext(context.Background()), &q, nil) 1110 if err != nil { 1111 log.Printf("[ERROR] mkRankedShard(%s): failed to cache repository list: %v", s, err) 1112 return &rankedShard{Searcher: s} 1113 } 1114 1115 var ( 1116 maxPriority float64 1117 repos = make([]*zoekt.Repository, 0, len(result.Repos)) 1118 ) 1119 for i := range result.Repos { 1120 repo := &result.Repos[i].Repository 1121 repos = append(repos, repo) 1122 if repo.RawConfig != nil { 1123 priority, _ := strconv.ParseFloat(repo.RawConfig["priority"], 64) 1124 if priority > maxPriority { 1125 maxPriority = priority 1126 } 1127 } 1128 } 1129 1130 return &rankedShard{ 1131 Searcher: s, 1132 repos: repos, 1133 priority: maxPriority, 1134 } 1135} 1136 1137// markReady should be called once all shards have been passed into replace on 1138// startup. Once s is marked as ready it stops reporting a Crash in the 1139// response Stats. 1140func (s *shardedSearcher) markReady() { 1141 s.ready.CompareAndSwap(false, true) 1142} 1143 1144func (s *shardedSearcher) replace(shards map[string]zoekt.Searcher) { 1145 if len(shards) == 0 { 1146 return 1147 } 1148 1149 defer func(began time.Time) { 1150 metricShardsBatchReplaceDurationSeconds.Observe(time.Since(began).Seconds()) 1151 }(time.Now()) 1152 1153 s.mu.Lock() 1154 defer s.mu.Unlock() 1155 1156 for key, shard := range shards { 1157 var r *rankedShard 1158 if shard != nil { 1159 r = mkRankedShard(shard) 1160 } 1161 1162 old := s.shards[key] 1163 if shard == nil { 1164 delete(s.shards, key) 1165 } else { 1166 s.shards[key] = r 1167 } 1168 1169 if old != nil && old.Searcher != nil { 1170 // _ ___ /^^\ /^\ /^^\_ 1171 // _ _@)@) \ ,,/ '` ~ `'~~ ', `\. 1172 // _/o\_ _ _ _/~`.`...'~\ ./~~..,'`','',.,' ' ~: 1173 // / `,'.~,~.~ . , . , ~|, ,/ .,' , ,. .. ,,. `, ~\_ 1174 // ( ' _' _ '_` _ ' . , `\_/ .' ..' ' ` ` `.. `, \_ 1175 // ~V~ V~ V~ V~ ~\ ` ' . ' , ' .,.,''`.,.''`.,.``. ', \_ 1176 // _/\ /\ /\ /\_/, . ' , `_/~\_ .' .,. ,, , _/~\_ `. `. '., \_ 1177 // < ~ ~ '~`'~'`, ., . `_: ::: \_ ' `_/ ::: \_ `.,' . ', \_ 1178 // \ ' `_ '`_ _ ',/ _::_::_ \ _ _/ _::_::_ \ `.,'.,`., \-,-,-,_,_, 1179 // `'~~ `'~~ `'~~ `'~~ \(_)(_)(_)/ `~~' \(_)(_)(_)/ ~'`\_.._,._,'_;_;_;_;_; 1180 // 1181 // We can't just call Close now, because there may be ongoing searches 1182 // which have old in the shards list. Previously we used an exclusive 1183 // lock to guarantee there were no concurrent searches. However, that 1184 // led to blocking on the read path. 1185 // 1186 // We could introduce granular locking per rankedShard to know when 1187 // there are no more references. However, this becomes tricky in 1188 // practice. Instead we rely on the garbage collector noticing old is no 1189 // longer used. We take care in our searchers to runtime.KeepAlive until 1190 // we have stopped referencing the underling mmap data. 1191 runtime.SetFinalizer(old, func(r *rankedShard) { 1192 r.Close() 1193 }) 1194 } 1195 } 1196 1197 ranked := make([]*rankedShard, 0, len(s.shards)) 1198 for _, r := range s.shards { 1199 ranked = append(ranked, r) 1200 } 1201 1202 sort.Slice(ranked, func(i, j int) bool { 1203 priorityDiff := ranked[i].priority - ranked[j].priority 1204 if priorityDiff != 0 { 1205 return priorityDiff > 0 1206 } 1207 if len(ranked[i].repos) == 0 || len(ranked[j].repos) == 0 { 1208 // Protect against empty names which can happen if we fail to List or 1209 // the shard is full of tombstones. Prefer the shard which has names. 1210 return len(ranked[i].repos) >= len(ranked[j].repos) 1211 } 1212 return ranked[i].repos[0].Name < ranked[j].repos[0].Name 1213 }) 1214 1215 s.ranked.Store(ranked) 1216 1217 metricShardsLoaded.Set(float64(len(ranked))) 1218} 1219 1220func loadShard(fn string) (zoekt.Searcher, error) { 1221 f, err := os.Open(fn) 1222 if err != nil { 1223 return nil, err 1224 } 1225 1226 iFile, err := index.NewIndexFile(f) 1227 if err != nil { 1228 return nil, err 1229 } 1230 s, err := index.NewSearcher(iFile) 1231 if err != nil { 1232 iFile.Close() 1233 return nil, fmt.Errorf("NewSearcher(%s): %v", fn, err) 1234 } 1235 1236 return s, nil 1237} 1238 1239// prioritySlice is a trivial implementation of an array that provides three 1240// things: appending a value, removing a value, and getting the array's max. 1241// Operations take O(n) time, which is acceptable because N is restricted to 1242// GOMAXPROCS (i.e., number of cpu cores) by the shardedSearcher interface. 1243type prioritySlice []float64 1244 1245func (p *prioritySlice) append(pri float64) { 1246 *p = append(*p, pri) 1247} 1248 1249func (p *prioritySlice) remove(pri float64) { 1250 for i, opri := range *p { 1251 if opri == pri { 1252 if i != len(*p)-1 { 1253 // swap to make this element the tail 1254 (*p)[i] = (*p)[len(*p)-1] 1255 } 1256 // pop the end off 1257 *p = (*p)[:len(*p)-1] 1258 break 1259 } 1260 } 1261} 1262 1263func (p *prioritySlice) max() float64 { 1264 // remove() and max() could be combined, but this is easier to read and 1265 // the expected performance difference from the extra lock and loop is 1266 // almost certainly irrelevant. 1267 maxPri := math.Inf(-1) 1268 for _, pri := range *p { 1269 if pri > maxPri { 1270 maxPri = pri 1271 } 1272 } 1273 return maxPri 1274}