forked from forgejo/forgejo
Wrap the code indexer (#9476)
* Wrap the code indexer In order to prevent a data race in the code indexer it must be wrapped with a holder otherwise it is possible to Search/Index on an incompletely initialised indexer, and search will fail with a nil pointer until the repository indexer is initialised. Further a completely initialised repository indexer should not be closed until Termination otherwise actions in Hammer/Shutdown phases could block or be lost. Finally, there is a complex dance of shutdown etiquette should the index initialisation fail. This PR restores that. * Always return err if closed whilst waiting Co-authored-by: techknowlogick <matti@mdranta.net>
This commit is contained in:
parent
34688e1db3
commit
30181d459d
3 changed files with 132 additions and 13 deletions
|
@ -5,6 +5,8 @@
|
|||
package code
|
||||
|
||||
import (
|
||||
"context"
|
||||
"os"
|
||||
"time"
|
||||
|
||||
"code.gitea.io/gitea/modules/graceful"
|
||||
|
@ -12,10 +14,6 @@ import (
|
|||
"code.gitea.io/gitea/modules/setting"
|
||||
)
|
||||
|
||||
var (
|
||||
indexer Indexer
|
||||
)
|
||||
|
||||
// SearchResult result of performing a search in a repo
|
||||
type SearchResult struct {
|
||||
RepoID int64
|
||||
|
@ -36,28 +34,45 @@ type Indexer interface {
|
|||
// Init initialize the repo indexer
|
||||
func Init() {
|
||||
if !setting.Indexer.RepoIndexerEnabled {
|
||||
indexer.Close()
|
||||
return
|
||||
}
|
||||
|
||||
ctx, cancel := context.WithCancel(context.Background())
|
||||
|
||||
graceful.GetManager().RunAtTerminate(ctx, func() {
|
||||
log.Debug("Closing repository indexer")
|
||||
indexer.Close()
|
||||
log.Info("PID: %d Repository Indexer closed", os.Getpid())
|
||||
})
|
||||
|
||||
waitChannel := make(chan time.Duration)
|
||||
go func() {
|
||||
start := time.Now()
|
||||
log.Info("Initializing Repository Indexer")
|
||||
var created bool
|
||||
var err error
|
||||
indexer, created, err = NewBleveIndexer(setting.Indexer.RepoPath)
|
||||
log.Info("PID: %d Initializing Repository Indexer at: %s", os.Getpid(), setting.Indexer.RepoPath)
|
||||
bleveIndexer, created, err := NewBleveIndexer(setting.Indexer.RepoPath)
|
||||
if err != nil {
|
||||
if bleveIndexer != nil {
|
||||
bleveIndexer.Close()
|
||||
}
|
||||
cancel()
|
||||
indexer.Close()
|
||||
log.Fatal("indexer.Init: %v", err)
|
||||
close(waitChannel)
|
||||
log.Fatal("PID: %d Unable to initialize the Repository Indexer at path: %s Error: %v", os.Getpid(), setting.Indexer.RepoPath, err)
|
||||
}
|
||||
indexer.set(bleveIndexer)
|
||||
|
||||
go processRepoIndexerOperationQueue(indexer)
|
||||
|
||||
if created {
|
||||
go populateRepoIndexer()
|
||||
}
|
||||
select {
|
||||
case waitChannel <- time.Since(start):
|
||||
case <-graceful.GetManager().IsShutdown():
|
||||
}
|
||||
|
||||
waitChannel <- time.Since(start)
|
||||
close(waitChannel)
|
||||
}()
|
||||
|
||||
if setting.Indexer.StartupTimeout > 0 {
|
||||
|
@ -67,9 +82,21 @@ func Init() {
|
|||
timeout += setting.GracefulHammerTime
|
||||
}
|
||||
select {
|
||||
case duration := <-waitChannel:
|
||||
case <-graceful.GetManager().IsShutdown():
|
||||
log.Warn("Shutdown before Repository Indexer completed initialization")
|
||||
cancel()
|
||||
indexer.Close()
|
||||
case duration, ok := <-waitChannel:
|
||||
if !ok {
|
||||
log.Warn("Repository Indexer Initialization failed")
|
||||
cancel()
|
||||
indexer.Close()
|
||||
return
|
||||
}
|
||||
log.Info("Repository Indexer Initialization took %v", duration)
|
||||
case <-time.After(timeout):
|
||||
cancel()
|
||||
indexer.Close()
|
||||
log.Fatal("Repository Indexer Initialization Timed-Out after: %v", timeout)
|
||||
}
|
||||
}()
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue