2017-01-25 10:43:02 +08:00
|
|
|
// Copyright 2016 The Gitea Authors. All rights reserved.
|
2022-11-28 02:20:29 +08:00
|
|
|
// SPDX-License-Identifier: MIT
|
2017-01-25 10:43:02 +08:00
|
|
|
|
2019-12-10 21:29:40 +08:00
|
|
|
package code
|
2017-01-25 10:43:02 +08:00
|
|
|
|
|
|
|
import (
|
2019-12-24 15:26:34 +08:00
|
|
|
"context"
|
|
|
|
"os"
|
2022-04-01 01:01:43 +08:00
|
|
|
"runtime/pprof"
|
2020-08-31 00:08:01 +08:00
|
|
|
"strconv"
|
|
|
|
"strings"
|
2019-12-23 20:31:16 +08:00
|
|
|
"time"
|
2017-09-17 04:16:21 +08:00
|
|
|
|
2021-09-19 19:49:59 +08:00
|
|
|
"code.gitea.io/gitea/models/db"
|
2021-12-10 09:27:50 +08:00
|
|
|
repo_model "code.gitea.io/gitea/models/repo"
|
2019-12-23 20:31:16 +08:00
|
|
|
"code.gitea.io/gitea/modules/graceful"
|
|
|
|
"code.gitea.io/gitea/modules/log"
|
2022-04-01 01:01:43 +08:00
|
|
|
"code.gitea.io/gitea/modules/process"
|
2020-09-07 23:05:08 +08:00
|
|
|
"code.gitea.io/gitea/modules/queue"
|
2018-02-06 02:29:17 +08:00
|
|
|
"code.gitea.io/gitea/modules/setting"
|
2020-02-21 03:53:55 +08:00
|
|
|
"code.gitea.io/gitea/modules/timeutil"
|
2023-05-25 16:13:47 +08:00
|
|
|
"code.gitea.io/gitea/modules/util"
|
2019-12-23 20:31:16 +08:00
|
|
|
)
|
2018-02-06 02:29:17 +08:00
|
|
|
|
2019-12-23 20:31:16 +08:00
|
|
|
// SearchResult result of performing a search in a repo
|
|
|
|
type SearchResult struct {
|
2020-02-21 03:53:55 +08:00
|
|
|
RepoID int64
|
|
|
|
StartIndex int
|
|
|
|
EndIndex int
|
|
|
|
Filename string
|
|
|
|
Content string
|
|
|
|
CommitID string
|
|
|
|
UpdatedUnix timeutil.TimeStamp
|
|
|
|
Language string
|
|
|
|
Color string
|
|
|
|
}
|
|
|
|
|
|
|
|
// SearchResultLanguages result of top languages count in search results
|
|
|
|
type SearchResultLanguages struct {
|
|
|
|
Language string
|
|
|
|
Color string
|
|
|
|
Count int
|
2017-09-17 04:16:21 +08:00
|
|
|
}
|
|
|
|
|
2020-09-07 23:05:08 +08:00
|
|
|
// Indexer defines an interface to index and search code contents
|
2019-12-23 20:31:16 +08:00
|
|
|
type Indexer interface {
|
2022-01-27 16:30:51 +08:00
|
|
|
Ping() bool
|
2022-01-20 07:26:57 +08:00
|
|
|
Index(ctx context.Context, repo *repo_model.Repository, sha string, changes *repoChanges) error
|
2019-12-23 20:31:16 +08:00
|
|
|
Delete(repoID int64) error
|
2022-01-27 16:30:51 +08:00
|
|
|
Search(ctx context.Context, repoIDs []int64, language, keyword string, page, pageSize int, isMatch bool) (int64, []*SearchResult, []*SearchResultLanguages, error)
|
2019-12-23 20:31:16 +08:00
|
|
|
Close()
|
2017-09-17 04:16:21 +08:00
|
|
|
}
|
|
|
|
|
2020-08-31 00:08:01 +08:00
|
|
|
func filenameIndexerID(repoID int64, filename string) string {
|
|
|
|
return indexerID(repoID) + "_" + filename
|
|
|
|
}
|
|
|
|
|
2020-09-14 18:40:07 +08:00
|
|
|
func indexerID(id int64) string {
|
|
|
|
return strconv.FormatInt(id, 36)
|
|
|
|
}
|
|
|
|
|
2020-08-31 00:08:01 +08:00
|
|
|
func parseIndexerID(indexerID string) (int64, string) {
|
|
|
|
index := strings.IndexByte(indexerID, '_')
|
|
|
|
if index == -1 {
|
|
|
|
log.Error("Unexpected ID in repo indexer: %s", indexerID)
|
|
|
|
}
|
2020-09-14 18:40:07 +08:00
|
|
|
repoID, _ := strconv.ParseInt(indexerID[:index], 36, 64)
|
2020-08-31 00:08:01 +08:00
|
|
|
return repoID, indexerID[index+1:]
|
|
|
|
}
|
|
|
|
|
|
|
|
func filenameOfIndexerID(indexerID string) string {
|
|
|
|
index := strings.IndexByte(indexerID, '_')
|
|
|
|
if index == -1 {
|
|
|
|
log.Error("Unexpected ID in repo indexer: %s", indexerID)
|
|
|
|
}
|
|
|
|
return indexerID[index+1:]
|
|
|
|
}
|
|
|
|
|
2020-09-07 23:05:08 +08:00
|
|
|
// IndexerData represents data stored in the code indexer
|
|
|
|
type IndexerData struct {
|
2021-11-02 11:14:24 +08:00
|
|
|
RepoID int64
|
2020-09-07 23:05:08 +08:00
|
|
|
}
|
|
|
|
|
Rewrite queue (#24505)
# ⚠️ Breaking
Many deprecated queue config options are removed (actually, they should
have been removed in 1.18/1.19).
If you see the fatal message when starting Gitea: "Please update your
app.ini to remove deprecated config options", please follow the error
messages to remove these options from your app.ini.
Example:
```
2023/05/06 19:39:22 [E] Removed queue option: `[indexer].ISSUE_INDEXER_QUEUE_TYPE`. Use new options in `[queue.issue_indexer]`
2023/05/06 19:39:22 [E] Removed queue option: `[indexer].UPDATE_BUFFER_LEN`. Use new options in `[queue.issue_indexer]`
2023/05/06 19:39:22 [F] Please update your app.ini to remove deprecated config options
```
Many options in `[queue]` are are dropped, including:
`WRAP_IF_NECESSARY`, `MAX_ATTEMPTS`, `TIMEOUT`, `WORKERS`,
`BLOCK_TIMEOUT`, `BOOST_TIMEOUT`, `BOOST_WORKERS`, they can be removed
from app.ini.
# The problem
The old queue package has some legacy problems:
* complexity: I doubt few people could tell how it works.
* maintainability: Too many channels and mutex/cond are mixed together,
too many different structs/interfaces depends each other.
* stability: due to the complexity & maintainability, sometimes there
are strange bugs and difficult to debug, and some code doesn't have test
(indeed some code is difficult to test because a lot of things are mixed
together).
* general applicability: although it is called "queue", its behavior is
not a well-known queue.
* scalability: it doesn't seem easy to make it work with a cluster
without breaking its behaviors.
It came from some very old code to "avoid breaking", however, its
technical debt is too heavy now. It's a good time to introduce a better
"queue" package.
# The new queue package
It keeps using old config and concept as much as possible.
* It only contains two major kinds of concepts:
* The "base queue": channel, levelqueue, redis
* They have the same abstraction, the same interface, and they are
tested by the same testing code.
* The "WokerPoolQueue", it uses the "base queue" to provide "worker
pool" function, calls the "handler" to process the data in the base
queue.
* The new code doesn't do "PushBack"
* Think about a queue with many workers, the "PushBack" can't guarantee
the order for re-queued unhandled items, so in new code it just does
"normal push"
* The new code doesn't do "pause/resume"
* The "pause/resume" was designed to handle some handler's failure: eg:
document indexer (elasticsearch) is down
* If a queue is paused for long time, either the producers blocks or the
new items are dropped.
* The new code doesn't do such "pause/resume" trick, it's not a common
queue's behavior and it doesn't help much.
* If there are unhandled items, the "push" function just blocks for a
few seconds and then re-queue them and retry.
* The new code doesn't do "worker booster"
* Gitea's queue's handlers are light functions, the cost is only the
go-routine, so it doesn't make sense to "boost" them.
* The new code only use "max worker number" to limit the concurrent
workers.
* The new "Push" never blocks forever
* Instead of creating more and more blocking goroutines, return an error
is more friendly to the server and to the end user.
There are more details in code comments: eg: the "Flush" problem, the
strange "code.index" hanging problem, the "immediate" queue problem.
Almost ready for review.
TODO:
* [x] add some necessary comments during review
* [x] add some more tests if necessary
* [x] update documents and config options
* [x] test max worker / active worker
* [x] re-run the CI tasks to see whether any test is flaky
* [x] improve the `handleOldLengthConfiguration` to provide more
friendly messages
* [x] fine tune default config values (eg: length?)
## Code coverage:
![image](https://user-images.githubusercontent.com/2114189/236620635-55576955-f95d-4810-b12f-879026a3afdf.png)
2023-05-08 19:49:59 +08:00
|
|
|
var indexerQueue *queue.WorkerPoolQueue[*IndexerData]
|
2020-09-07 23:05:08 +08:00
|
|
|
|
2022-01-20 07:26:57 +08:00
|
|
|
func index(ctx context.Context, indexer Indexer, repoID int64) error {
|
2022-12-03 10:48:26 +08:00
|
|
|
repo, err := repo_model.GetRepositoryByID(ctx, repoID)
|
2021-12-10 09:27:50 +08:00
|
|
|
if repo_model.IsErrRepoNotExist(err) {
|
2021-11-02 11:14:24 +08:00
|
|
|
return indexer.Delete(repoID)
|
|
|
|
}
|
2020-09-07 23:05:08 +08:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2023-05-25 16:13:47 +08:00
|
|
|
repoTypes := setting.Indexer.RepoIndexerRepoTypes
|
|
|
|
|
|
|
|
if len(repoTypes) == 0 {
|
|
|
|
repoTypes = []string{"sources"}
|
|
|
|
}
|
|
|
|
|
|
|
|
// skip forks from being indexed if unit is not present
|
|
|
|
if !util.SliceContains(repoTypes, "forks") && repo.IsFork {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// skip mirrors from being indexed if unit is not present
|
|
|
|
if !util.SliceContains(repoTypes, "mirrors") && repo.IsMirror {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// skip templates from being indexed if unit is not present
|
|
|
|
if !util.SliceContains(repoTypes, "templates") && repo.IsTemplate {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// skip regular repos from being indexed if unit is not present
|
|
|
|
if !util.SliceContains(repoTypes, "sources") && !repo.IsFork && !repo.IsMirror && !repo.IsTemplate {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-01-20 07:26:57 +08:00
|
|
|
sha, err := getDefaultBranchSha(ctx, repo)
|
2020-09-07 23:05:08 +08:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2022-01-20 07:26:57 +08:00
|
|
|
changes, err := getRepoChanges(ctx, repo, sha)
|
2020-09-07 23:05:08 +08:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
} else if changes == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-01-20 07:26:57 +08:00
|
|
|
if err := indexer.Index(ctx, repo, sha, changes); err != nil {
|
2020-09-07 23:05:08 +08:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2022-05-20 22:08:52 +08:00
|
|
|
return repo_model.UpdateIndexerStatus(ctx, repo, repo_model.RepoIndexerTypeCode, sha)
|
2020-09-07 23:05:08 +08:00
|
|
|
}
|
|
|
|
|
2019-12-23 20:31:16 +08:00
|
|
|
// Init initialize the repo indexer
|
|
|
|
func Init() {
|
|
|
|
if !setting.Indexer.RepoIndexerEnabled {
|
2019-12-24 15:26:34 +08:00
|
|
|
indexer.Close()
|
2019-12-23 20:31:16 +08:00
|
|
|
return
|
|
|
|
}
|
2017-09-25 08:08:48 +08:00
|
|
|
|
2022-04-01 01:01:43 +08:00
|
|
|
ctx, cancel, finished := process.GetManager().AddTypedContext(context.Background(), "Service: CodeIndexer", process.SystemProcessType, false)
|
2019-12-24 15:26:34 +08:00
|
|
|
|
2021-05-15 22:22:26 +08:00
|
|
|
graceful.GetManager().RunAtTerminate(func() {
|
|
|
|
select {
|
|
|
|
case <-ctx.Done():
|
|
|
|
return
|
|
|
|
default:
|
|
|
|
}
|
|
|
|
cancel()
|
2019-12-24 15:26:34 +08:00
|
|
|
log.Debug("Closing repository indexer")
|
|
|
|
indexer.Close()
|
|
|
|
log.Info("PID: %d Repository Indexer closed", os.Getpid())
|
2022-04-01 01:01:43 +08:00
|
|
|
finished()
|
2019-12-24 15:26:34 +08:00
|
|
|
})
|
|
|
|
|
2022-04-27 07:22:26 +08:00
|
|
|
waitChannel := make(chan time.Duration, 1)
|
2020-09-07 23:05:08 +08:00
|
|
|
|
|
|
|
// Create the Queue
|
|
|
|
switch setting.Indexer.RepoType {
|
|
|
|
case "bleve", "elasticsearch":
|
Rewrite queue (#24505)
# ⚠️ Breaking
Many deprecated queue config options are removed (actually, they should
have been removed in 1.18/1.19).
If you see the fatal message when starting Gitea: "Please update your
app.ini to remove deprecated config options", please follow the error
messages to remove these options from your app.ini.
Example:
```
2023/05/06 19:39:22 [E] Removed queue option: `[indexer].ISSUE_INDEXER_QUEUE_TYPE`. Use new options in `[queue.issue_indexer]`
2023/05/06 19:39:22 [E] Removed queue option: `[indexer].UPDATE_BUFFER_LEN`. Use new options in `[queue.issue_indexer]`
2023/05/06 19:39:22 [F] Please update your app.ini to remove deprecated config options
```
Many options in `[queue]` are are dropped, including:
`WRAP_IF_NECESSARY`, `MAX_ATTEMPTS`, `TIMEOUT`, `WORKERS`,
`BLOCK_TIMEOUT`, `BOOST_TIMEOUT`, `BOOST_WORKERS`, they can be removed
from app.ini.
# The problem
The old queue package has some legacy problems:
* complexity: I doubt few people could tell how it works.
* maintainability: Too many channels and mutex/cond are mixed together,
too many different structs/interfaces depends each other.
* stability: due to the complexity & maintainability, sometimes there
are strange bugs and difficult to debug, and some code doesn't have test
(indeed some code is difficult to test because a lot of things are mixed
together).
* general applicability: although it is called "queue", its behavior is
not a well-known queue.
* scalability: it doesn't seem easy to make it work with a cluster
without breaking its behaviors.
It came from some very old code to "avoid breaking", however, its
technical debt is too heavy now. It's a good time to introduce a better
"queue" package.
# The new queue package
It keeps using old config and concept as much as possible.
* It only contains two major kinds of concepts:
* The "base queue": channel, levelqueue, redis
* They have the same abstraction, the same interface, and they are
tested by the same testing code.
* The "WokerPoolQueue", it uses the "base queue" to provide "worker
pool" function, calls the "handler" to process the data in the base
queue.
* The new code doesn't do "PushBack"
* Think about a queue with many workers, the "PushBack" can't guarantee
the order for re-queued unhandled items, so in new code it just does
"normal push"
* The new code doesn't do "pause/resume"
* The "pause/resume" was designed to handle some handler's failure: eg:
document indexer (elasticsearch) is down
* If a queue is paused for long time, either the producers blocks or the
new items are dropped.
* The new code doesn't do such "pause/resume" trick, it's not a common
queue's behavior and it doesn't help much.
* If there are unhandled items, the "push" function just blocks for a
few seconds and then re-queue them and retry.
* The new code doesn't do "worker booster"
* Gitea's queue's handlers are light functions, the cost is only the
go-routine, so it doesn't make sense to "boost" them.
* The new code only use "max worker number" to limit the concurrent
workers.
* The new "Push" never blocks forever
* Instead of creating more and more blocking goroutines, return an error
is more friendly to the server and to the end user.
There are more details in code comments: eg: the "Flush" problem, the
strange "code.index" hanging problem, the "immediate" queue problem.
Almost ready for review.
TODO:
* [x] add some necessary comments during review
* [x] add some more tests if necessary
* [x] update documents and config options
* [x] test max worker / active worker
* [x] re-run the CI tasks to see whether any test is flaky
* [x] improve the `handleOldLengthConfiguration` to provide more
friendly messages
* [x] fine tune default config values (eg: length?)
## Code coverage:
![image](https://user-images.githubusercontent.com/2114189/236620635-55576955-f95d-4810-b12f-879026a3afdf.png)
2023-05-08 19:49:59 +08:00
|
|
|
handler := func(items ...*IndexerData) (unhandled []*IndexerData) {
|
2020-09-07 23:05:08 +08:00
|
|
|
idx, err := indexer.get()
|
|
|
|
if idx == nil || err != nil {
|
Improve queue and logger context (#24924)
Before there was a "graceful function": RunWithShutdownFns, it's mainly
for some modules which doesn't support context.
The old queue system doesn't work well with context, so the old queues
need it.
After the queue refactoring, the new queue works with context well, so,
use Golang context as much as possible, the `RunWithShutdownFns` could
be removed (replaced by RunWithCancel for context cancel mechanism), the
related code could be simplified.
This PR also fixes some legacy queue-init problems, eg:
* typo : archiver: "unable to create codes indexer queue" => "unable to
create repo-archive queue"
* no nil check for failed queues, which causes unfriendly panic
After this PR, many goroutines could have better display name:
![image](https://github.com/go-gitea/gitea/assets/2114189/701b2a9b-8065-4137-aeaa-0bda2b34604a)
![image](https://github.com/go-gitea/gitea/assets/2114189/f1d5f50f-0534-40f0-b0be-f2c9daa5fe92)
2023-05-26 15:31:55 +08:00
|
|
|
log.Warn("Codes indexer handler: indexer is not ready, retry later.")
|
Rewrite queue (#24505)
# ⚠️ Breaking
Many deprecated queue config options are removed (actually, they should
have been removed in 1.18/1.19).
If you see the fatal message when starting Gitea: "Please update your
app.ini to remove deprecated config options", please follow the error
messages to remove these options from your app.ini.
Example:
```
2023/05/06 19:39:22 [E] Removed queue option: `[indexer].ISSUE_INDEXER_QUEUE_TYPE`. Use new options in `[queue.issue_indexer]`
2023/05/06 19:39:22 [E] Removed queue option: `[indexer].UPDATE_BUFFER_LEN`. Use new options in `[queue.issue_indexer]`
2023/05/06 19:39:22 [F] Please update your app.ini to remove deprecated config options
```
Many options in `[queue]` are are dropped, including:
`WRAP_IF_NECESSARY`, `MAX_ATTEMPTS`, `TIMEOUT`, `WORKERS`,
`BLOCK_TIMEOUT`, `BOOST_TIMEOUT`, `BOOST_WORKERS`, they can be removed
from app.ini.
# The problem
The old queue package has some legacy problems:
* complexity: I doubt few people could tell how it works.
* maintainability: Too many channels and mutex/cond are mixed together,
too many different structs/interfaces depends each other.
* stability: due to the complexity & maintainability, sometimes there
are strange bugs and difficult to debug, and some code doesn't have test
(indeed some code is difficult to test because a lot of things are mixed
together).
* general applicability: although it is called "queue", its behavior is
not a well-known queue.
* scalability: it doesn't seem easy to make it work with a cluster
without breaking its behaviors.
It came from some very old code to "avoid breaking", however, its
technical debt is too heavy now. It's a good time to introduce a better
"queue" package.
# The new queue package
It keeps using old config and concept as much as possible.
* It only contains two major kinds of concepts:
* The "base queue": channel, levelqueue, redis
* They have the same abstraction, the same interface, and they are
tested by the same testing code.
* The "WokerPoolQueue", it uses the "base queue" to provide "worker
pool" function, calls the "handler" to process the data in the base
queue.
* The new code doesn't do "PushBack"
* Think about a queue with many workers, the "PushBack" can't guarantee
the order for re-queued unhandled items, so in new code it just does
"normal push"
* The new code doesn't do "pause/resume"
* The "pause/resume" was designed to handle some handler's failure: eg:
document indexer (elasticsearch) is down
* If a queue is paused for long time, either the producers blocks or the
new items are dropped.
* The new code doesn't do such "pause/resume" trick, it's not a common
queue's behavior and it doesn't help much.
* If there are unhandled items, the "push" function just blocks for a
few seconds and then re-queue them and retry.
* The new code doesn't do "worker booster"
* Gitea's queue's handlers are light functions, the cost is only the
go-routine, so it doesn't make sense to "boost" them.
* The new code only use "max worker number" to limit the concurrent
workers.
* The new "Push" never blocks forever
* Instead of creating more and more blocking goroutines, return an error
is more friendly to the server and to the end user.
There are more details in code comments: eg: the "Flush" problem, the
strange "code.index" hanging problem, the "immediate" queue problem.
Almost ready for review.
TODO:
* [x] add some necessary comments during review
* [x] add some more tests if necessary
* [x] update documents and config options
* [x] test max worker / active worker
* [x] re-run the CI tasks to see whether any test is flaky
* [x] improve the `handleOldLengthConfiguration` to provide more
friendly messages
* [x] fine tune default config values (eg: length?)
## Code coverage:
![image](https://user-images.githubusercontent.com/2114189/236620635-55576955-f95d-4810-b12f-879026a3afdf.png)
2023-05-08 19:49:59 +08:00
|
|
|
return items
|
2020-09-07 23:05:08 +08:00
|
|
|
}
|
|
|
|
|
Rewrite queue (#24505)
# ⚠️ Breaking
Many deprecated queue config options are removed (actually, they should
have been removed in 1.18/1.19).
If you see the fatal message when starting Gitea: "Please update your
app.ini to remove deprecated config options", please follow the error
messages to remove these options from your app.ini.
Example:
```
2023/05/06 19:39:22 [E] Removed queue option: `[indexer].ISSUE_INDEXER_QUEUE_TYPE`. Use new options in `[queue.issue_indexer]`
2023/05/06 19:39:22 [E] Removed queue option: `[indexer].UPDATE_BUFFER_LEN`. Use new options in `[queue.issue_indexer]`
2023/05/06 19:39:22 [F] Please update your app.ini to remove deprecated config options
```
Many options in `[queue]` are are dropped, including:
`WRAP_IF_NECESSARY`, `MAX_ATTEMPTS`, `TIMEOUT`, `WORKERS`,
`BLOCK_TIMEOUT`, `BOOST_TIMEOUT`, `BOOST_WORKERS`, they can be removed
from app.ini.
# The problem
The old queue package has some legacy problems:
* complexity: I doubt few people could tell how it works.
* maintainability: Too many channels and mutex/cond are mixed together,
too many different structs/interfaces depends each other.
* stability: due to the complexity & maintainability, sometimes there
are strange bugs and difficult to debug, and some code doesn't have test
(indeed some code is difficult to test because a lot of things are mixed
together).
* general applicability: although it is called "queue", its behavior is
not a well-known queue.
* scalability: it doesn't seem easy to make it work with a cluster
without breaking its behaviors.
It came from some very old code to "avoid breaking", however, its
technical debt is too heavy now. It's a good time to introduce a better
"queue" package.
# The new queue package
It keeps using old config and concept as much as possible.
* It only contains two major kinds of concepts:
* The "base queue": channel, levelqueue, redis
* They have the same abstraction, the same interface, and they are
tested by the same testing code.
* The "WokerPoolQueue", it uses the "base queue" to provide "worker
pool" function, calls the "handler" to process the data in the base
queue.
* The new code doesn't do "PushBack"
* Think about a queue with many workers, the "PushBack" can't guarantee
the order for re-queued unhandled items, so in new code it just does
"normal push"
* The new code doesn't do "pause/resume"
* The "pause/resume" was designed to handle some handler's failure: eg:
document indexer (elasticsearch) is down
* If a queue is paused for long time, either the producers blocks or the
new items are dropped.
* The new code doesn't do such "pause/resume" trick, it's not a common
queue's behavior and it doesn't help much.
* If there are unhandled items, the "push" function just blocks for a
few seconds and then re-queue them and retry.
* The new code doesn't do "worker booster"
* Gitea's queue's handlers are light functions, the cost is only the
go-routine, so it doesn't make sense to "boost" them.
* The new code only use "max worker number" to limit the concurrent
workers.
* The new "Push" never blocks forever
* Instead of creating more and more blocking goroutines, return an error
is more friendly to the server and to the end user.
There are more details in code comments: eg: the "Flush" problem, the
strange "code.index" hanging problem, the "immediate" queue problem.
Almost ready for review.
TODO:
* [x] add some necessary comments during review
* [x] add some more tests if necessary
* [x] update documents and config options
* [x] test max worker / active worker
* [x] re-run the CI tasks to see whether any test is flaky
* [x] improve the `handleOldLengthConfiguration` to provide more
friendly messages
* [x] fine tune default config values (eg: length?)
## Code coverage:
![image](https://user-images.githubusercontent.com/2114189/236620635-55576955-f95d-4810-b12f-879026a3afdf.png)
2023-05-08 19:49:59 +08:00
|
|
|
for _, indexerData := range items {
|
2021-11-02 11:14:24 +08:00
|
|
|
log.Trace("IndexerData Process Repo: %d", indexerData.RepoID)
|
|
|
|
|
Rewrite queue (#24505)
# ⚠️ Breaking
Many deprecated queue config options are removed (actually, they should
have been removed in 1.18/1.19).
If you see the fatal message when starting Gitea: "Please update your
app.ini to remove deprecated config options", please follow the error
messages to remove these options from your app.ini.
Example:
```
2023/05/06 19:39:22 [E] Removed queue option: `[indexer].ISSUE_INDEXER_QUEUE_TYPE`. Use new options in `[queue.issue_indexer]`
2023/05/06 19:39:22 [E] Removed queue option: `[indexer].UPDATE_BUFFER_LEN`. Use new options in `[queue.issue_indexer]`
2023/05/06 19:39:22 [F] Please update your app.ini to remove deprecated config options
```
Many options in `[queue]` are are dropped, including:
`WRAP_IF_NECESSARY`, `MAX_ATTEMPTS`, `TIMEOUT`, `WORKERS`,
`BLOCK_TIMEOUT`, `BOOST_TIMEOUT`, `BOOST_WORKERS`, they can be removed
from app.ini.
# The problem
The old queue package has some legacy problems:
* complexity: I doubt few people could tell how it works.
* maintainability: Too many channels and mutex/cond are mixed together,
too many different structs/interfaces depends each other.
* stability: due to the complexity & maintainability, sometimes there
are strange bugs and difficult to debug, and some code doesn't have test
(indeed some code is difficult to test because a lot of things are mixed
together).
* general applicability: although it is called "queue", its behavior is
not a well-known queue.
* scalability: it doesn't seem easy to make it work with a cluster
without breaking its behaviors.
It came from some very old code to "avoid breaking", however, its
technical debt is too heavy now. It's a good time to introduce a better
"queue" package.
# The new queue package
It keeps using old config and concept as much as possible.
* It only contains two major kinds of concepts:
* The "base queue": channel, levelqueue, redis
* They have the same abstraction, the same interface, and they are
tested by the same testing code.
* The "WokerPoolQueue", it uses the "base queue" to provide "worker
pool" function, calls the "handler" to process the data in the base
queue.
* The new code doesn't do "PushBack"
* Think about a queue with many workers, the "PushBack" can't guarantee
the order for re-queued unhandled items, so in new code it just does
"normal push"
* The new code doesn't do "pause/resume"
* The "pause/resume" was designed to handle some handler's failure: eg:
document indexer (elasticsearch) is down
* If a queue is paused for long time, either the producers blocks or the
new items are dropped.
* The new code doesn't do such "pause/resume" trick, it's not a common
queue's behavior and it doesn't help much.
* If there are unhandled items, the "push" function just blocks for a
few seconds and then re-queue them and retry.
* The new code doesn't do "worker booster"
* Gitea's queue's handlers are light functions, the cost is only the
go-routine, so it doesn't make sense to "boost" them.
* The new code only use "max worker number" to limit the concurrent
workers.
* The new "Push" never blocks forever
* Instead of creating more and more blocking goroutines, return an error
is more friendly to the server and to the end user.
There are more details in code comments: eg: the "Flush" problem, the
strange "code.index" hanging problem, the "immediate" queue problem.
Almost ready for review.
TODO:
* [x] add some necessary comments during review
* [x] add some more tests if necessary
* [x] update documents and config options
* [x] test max worker / active worker
* [x] re-run the CI tasks to see whether any test is flaky
* [x] improve the `handleOldLengthConfiguration` to provide more
friendly messages
* [x] fine tune default config values (eg: length?)
## Code coverage:
![image](https://user-images.githubusercontent.com/2114189/236620635-55576955-f95d-4810-b12f-879026a3afdf.png)
2023-05-08 19:49:59 +08:00
|
|
|
// FIXME: it seems there is a bug in `CatFileBatch` or `nio.Pipe`, which will cause the process to hang forever in rare cases
|
|
|
|
/*
|
|
|
|
sync.(*Cond).Wait(cond.go:70)
|
|
|
|
github.com/djherbis/nio/v3.(*PipeReader).Read(sync.go:106)
|
|
|
|
bufio.(*Reader).fill(bufio.go:106)
|
|
|
|
bufio.(*Reader).ReadSlice(bufio.go:372)
|
|
|
|
bufio.(*Reader).collectFragments(bufio.go:447)
|
|
|
|
bufio.(*Reader).ReadString(bufio.go:494)
|
|
|
|
code.gitea.io/gitea/modules/git.ReadBatchLine(batch_reader.go:149)
|
|
|
|
code.gitea.io/gitea/modules/indexer/code.(*BleveIndexer).addUpdate(bleve.go:214)
|
|
|
|
code.gitea.io/gitea/modules/indexer/code.(*BleveIndexer).Index(bleve.go:296)
|
|
|
|
code.gitea.io/gitea/modules/indexer/code.(*wrappedIndexer).Index(wrapped.go:74)
|
|
|
|
code.gitea.io/gitea/modules/indexer/code.index(indexer.go:105)
|
|
|
|
*/
|
2022-01-20 07:26:57 +08:00
|
|
|
if err := index(ctx, indexer, indexerData.RepoID); err != nil {
|
Rewrite queue (#24505)
# ⚠️ Breaking
Many deprecated queue config options are removed (actually, they should
have been removed in 1.18/1.19).
If you see the fatal message when starting Gitea: "Please update your
app.ini to remove deprecated config options", please follow the error
messages to remove these options from your app.ini.
Example:
```
2023/05/06 19:39:22 [E] Removed queue option: `[indexer].ISSUE_INDEXER_QUEUE_TYPE`. Use new options in `[queue.issue_indexer]`
2023/05/06 19:39:22 [E] Removed queue option: `[indexer].UPDATE_BUFFER_LEN`. Use new options in `[queue.issue_indexer]`
2023/05/06 19:39:22 [F] Please update your app.ini to remove deprecated config options
```
Many options in `[queue]` are are dropped, including:
`WRAP_IF_NECESSARY`, `MAX_ATTEMPTS`, `TIMEOUT`, `WORKERS`,
`BLOCK_TIMEOUT`, `BOOST_TIMEOUT`, `BOOST_WORKERS`, they can be removed
from app.ini.
# The problem
The old queue package has some legacy problems:
* complexity: I doubt few people could tell how it works.
* maintainability: Too many channels and mutex/cond are mixed together,
too many different structs/interfaces depends each other.
* stability: due to the complexity & maintainability, sometimes there
are strange bugs and difficult to debug, and some code doesn't have test
(indeed some code is difficult to test because a lot of things are mixed
together).
* general applicability: although it is called "queue", its behavior is
not a well-known queue.
* scalability: it doesn't seem easy to make it work with a cluster
without breaking its behaviors.
It came from some very old code to "avoid breaking", however, its
technical debt is too heavy now. It's a good time to introduce a better
"queue" package.
# The new queue package
It keeps using old config and concept as much as possible.
* It only contains two major kinds of concepts:
* The "base queue": channel, levelqueue, redis
* They have the same abstraction, the same interface, and they are
tested by the same testing code.
* The "WokerPoolQueue", it uses the "base queue" to provide "worker
pool" function, calls the "handler" to process the data in the base
queue.
* The new code doesn't do "PushBack"
* Think about a queue with many workers, the "PushBack" can't guarantee
the order for re-queued unhandled items, so in new code it just does
"normal push"
* The new code doesn't do "pause/resume"
* The "pause/resume" was designed to handle some handler's failure: eg:
document indexer (elasticsearch) is down
* If a queue is paused for long time, either the producers blocks or the
new items are dropped.
* The new code doesn't do such "pause/resume" trick, it's not a common
queue's behavior and it doesn't help much.
* If there are unhandled items, the "push" function just blocks for a
few seconds and then re-queue them and retry.
* The new code doesn't do "worker booster"
* Gitea's queue's handlers are light functions, the cost is only the
go-routine, so it doesn't make sense to "boost" them.
* The new code only use "max worker number" to limit the concurrent
workers.
* The new "Push" never blocks forever
* Instead of creating more and more blocking goroutines, return an error
is more friendly to the server and to the end user.
There are more details in code comments: eg: the "Flush" problem, the
strange "code.index" hanging problem, the "immediate" queue problem.
Almost ready for review.
TODO:
* [x] add some necessary comments during review
* [x] add some more tests if necessary
* [x] update documents and config options
* [x] test max worker / active worker
* [x] re-run the CI tasks to see whether any test is flaky
* [x] improve the `handleOldLengthConfiguration` to provide more
friendly messages
* [x] fine tune default config values (eg: length?)
## Code coverage:
![image](https://user-images.githubusercontent.com/2114189/236620635-55576955-f95d-4810-b12f-879026a3afdf.png)
2023-05-08 19:49:59 +08:00
|
|
|
if !idx.Ping() {
|
|
|
|
log.Error("Code indexer handler: indexer is unavailable.")
|
|
|
|
unhandled = append(unhandled, indexerData)
|
2022-01-27 16:30:51 +08:00
|
|
|
continue
|
|
|
|
}
|
Rewrite queue (#24505)
# ⚠️ Breaking
Many deprecated queue config options are removed (actually, they should
have been removed in 1.18/1.19).
If you see the fatal message when starting Gitea: "Please update your
app.ini to remove deprecated config options", please follow the error
messages to remove these options from your app.ini.
Example:
```
2023/05/06 19:39:22 [E] Removed queue option: `[indexer].ISSUE_INDEXER_QUEUE_TYPE`. Use new options in `[queue.issue_indexer]`
2023/05/06 19:39:22 [E] Removed queue option: `[indexer].UPDATE_BUFFER_LEN`. Use new options in `[queue.issue_indexer]`
2023/05/06 19:39:22 [F] Please update your app.ini to remove deprecated config options
```
Many options in `[queue]` are are dropped, including:
`WRAP_IF_NECESSARY`, `MAX_ATTEMPTS`, `TIMEOUT`, `WORKERS`,
`BLOCK_TIMEOUT`, `BOOST_TIMEOUT`, `BOOST_WORKERS`, they can be removed
from app.ini.
# The problem
The old queue package has some legacy problems:
* complexity: I doubt few people could tell how it works.
* maintainability: Too many channels and mutex/cond are mixed together,
too many different structs/interfaces depends each other.
* stability: due to the complexity & maintainability, sometimes there
are strange bugs and difficult to debug, and some code doesn't have test
(indeed some code is difficult to test because a lot of things are mixed
together).
* general applicability: although it is called "queue", its behavior is
not a well-known queue.
* scalability: it doesn't seem easy to make it work with a cluster
without breaking its behaviors.
It came from some very old code to "avoid breaking", however, its
technical debt is too heavy now. It's a good time to introduce a better
"queue" package.
# The new queue package
It keeps using old config and concept as much as possible.
* It only contains two major kinds of concepts:
* The "base queue": channel, levelqueue, redis
* They have the same abstraction, the same interface, and they are
tested by the same testing code.
* The "WokerPoolQueue", it uses the "base queue" to provide "worker
pool" function, calls the "handler" to process the data in the base
queue.
* The new code doesn't do "PushBack"
* Think about a queue with many workers, the "PushBack" can't guarantee
the order for re-queued unhandled items, so in new code it just does
"normal push"
* The new code doesn't do "pause/resume"
* The "pause/resume" was designed to handle some handler's failure: eg:
document indexer (elasticsearch) is down
* If a queue is paused for long time, either the producers blocks or the
new items are dropped.
* The new code doesn't do such "pause/resume" trick, it's not a common
queue's behavior and it doesn't help much.
* If there are unhandled items, the "push" function just blocks for a
few seconds and then re-queue them and retry.
* The new code doesn't do "worker booster"
* Gitea's queue's handlers are light functions, the cost is only the
go-routine, so it doesn't make sense to "boost" them.
* The new code only use "max worker number" to limit the concurrent
workers.
* The new "Push" never blocks forever
* Instead of creating more and more blocking goroutines, return an error
is more friendly to the server and to the end user.
There are more details in code comments: eg: the "Flush" problem, the
strange "code.index" hanging problem, the "immediate" queue problem.
Almost ready for review.
TODO:
* [x] add some necessary comments during review
* [x] add some more tests if necessary
* [x] update documents and config options
* [x] test max worker / active worker
* [x] re-run the CI tasks to see whether any test is flaky
* [x] improve the `handleOldLengthConfiguration` to provide more
friendly messages
* [x] fine tune default config values (eg: length?)
## Code coverage:
![image](https://user-images.githubusercontent.com/2114189/236620635-55576955-f95d-4810-b12f-879026a3afdf.png)
2023-05-08 19:49:59 +08:00
|
|
|
if !setting.IsInTesting {
|
|
|
|
log.Error("Codes indexer handler: index error for repo %v: %v", indexerData.RepoID, err)
|
|
|
|
}
|
2020-09-07 23:05:08 +08:00
|
|
|
}
|
|
|
|
}
|
2022-01-27 16:30:51 +08:00
|
|
|
return unhandled
|
2020-09-07 23:05:08 +08:00
|
|
|
}
|
|
|
|
|
Improve queue and logger context (#24924)
Before there was a "graceful function": RunWithShutdownFns, it's mainly
for some modules which doesn't support context.
The old queue system doesn't work well with context, so the old queues
need it.
After the queue refactoring, the new queue works with context well, so,
use Golang context as much as possible, the `RunWithShutdownFns` could
be removed (replaced by RunWithCancel for context cancel mechanism), the
related code could be simplified.
This PR also fixes some legacy queue-init problems, eg:
* typo : archiver: "unable to create codes indexer queue" => "unable to
create repo-archive queue"
* no nil check for failed queues, which causes unfriendly panic
After this PR, many goroutines could have better display name:
![image](https://github.com/go-gitea/gitea/assets/2114189/701b2a9b-8065-4137-aeaa-0bda2b34604a)
![image](https://github.com/go-gitea/gitea/assets/2114189/f1d5f50f-0534-40f0-b0be-f2c9daa5fe92)
2023-05-26 15:31:55 +08:00
|
|
|
indexerQueue = queue.CreateUniqueQueue(ctx, "code_indexer", handler)
|
2020-09-07 23:05:08 +08:00
|
|
|
if indexerQueue == nil {
|
|
|
|
log.Fatal("Unable to create codes indexer queue")
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
log.Fatal("Unknown codes indexer type; %s", setting.Indexer.RepoType)
|
|
|
|
}
|
|
|
|
|
2019-12-23 20:31:16 +08:00
|
|
|
go func() {
|
2022-04-01 01:01:43 +08:00
|
|
|
pprof.SetGoroutineLabels(ctx)
|
2019-12-23 20:31:16 +08:00
|
|
|
start := time.Now()
|
2020-08-31 00:08:01 +08:00
|
|
|
var (
|
|
|
|
rIndexer Indexer
|
|
|
|
populate bool
|
|
|
|
err error
|
|
|
|
)
|
|
|
|
switch setting.Indexer.RepoType {
|
|
|
|
case "bleve":
|
|
|
|
log.Info("PID: %d Initializing Repository Indexer at: %s", os.Getpid(), setting.Indexer.RepoPath)
|
|
|
|
defer func() {
|
|
|
|
if err := recover(); err != nil {
|
|
|
|
log.Error("PANIC whilst initializing repository indexer: %v\nStacktrace: %s", err, log.Stack(2))
|
|
|
|
log.Error("The indexer files are likely corrupted and may need to be deleted")
|
|
|
|
log.Error("You can completely remove the \"%s\" directory to make Gitea recreate the indexes", setting.Indexer.RepoPath)
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
rIndexer, populate, err = NewBleveIndexer(setting.Indexer.RepoPath)
|
|
|
|
if err != nil {
|
2020-02-29 06:00:09 +08:00
|
|
|
cancel()
|
|
|
|
indexer.Close()
|
|
|
|
close(waitChannel)
|
2020-08-31 00:08:01 +08:00
|
|
|
log.Fatal("PID: %d Unable to initialize the bleve Repository Indexer at path: %s Error: %v", os.Getpid(), setting.Indexer.RepoPath, err)
|
2020-02-29 06:00:09 +08:00
|
|
|
}
|
2020-08-31 00:08:01 +08:00
|
|
|
case "elasticsearch":
|
|
|
|
log.Info("PID: %d Initializing Repository Indexer at: %s", os.Getpid(), setting.Indexer.RepoConnStr)
|
|
|
|
defer func() {
|
|
|
|
if err := recover(); err != nil {
|
|
|
|
log.Error("PANIC whilst initializing repository indexer: %v\nStacktrace: %s", err, log.Stack(2))
|
|
|
|
log.Error("The indexer files are likely corrupted and may need to be deleted")
|
|
|
|
log.Error("You can completely remove the \"%s\" index to make Gitea recreate the indexes", setting.Indexer.RepoConnStr)
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
rIndexer, populate, err = NewElasticSearchIndexer(setting.Indexer.RepoConnStr, setting.Indexer.RepoIndexerName)
|
|
|
|
if err != nil {
|
|
|
|
cancel()
|
|
|
|
indexer.Close()
|
|
|
|
close(waitChannel)
|
|
|
|
log.Fatal("PID: %d Unable to initialize the elasticsearch Repository Indexer connstr: %s Error: %v", os.Getpid(), setting.Indexer.RepoConnStr, err)
|
2019-12-24 15:26:34 +08:00
|
|
|
}
|
2020-08-31 00:08:01 +08:00
|
|
|
default:
|
|
|
|
log.Fatal("PID: %d Unknown Indexer type: %s", os.Getpid(), setting.Indexer.RepoType)
|
2019-12-23 20:31:16 +08:00
|
|
|
}
|
2020-08-31 00:08:01 +08:00
|
|
|
|
|
|
|
indexer.set(rIndexer)
|
2017-09-25 08:08:48 +08:00
|
|
|
|
2020-09-07 23:05:08 +08:00
|
|
|
// Start processing the queue
|
Improve queue and logger context (#24924)
Before there was a "graceful function": RunWithShutdownFns, it's mainly
for some modules which doesn't support context.
The old queue system doesn't work well with context, so the old queues
need it.
After the queue refactoring, the new queue works with context well, so,
use Golang context as much as possible, the `RunWithShutdownFns` could
be removed (replaced by RunWithCancel for context cancel mechanism), the
related code could be simplified.
This PR also fixes some legacy queue-init problems, eg:
* typo : archiver: "unable to create codes indexer queue" => "unable to
create repo-archive queue"
* no nil check for failed queues, which causes unfriendly panic
After this PR, many goroutines could have better display name:
![image](https://github.com/go-gitea/gitea/assets/2114189/701b2a9b-8065-4137-aeaa-0bda2b34604a)
![image](https://github.com/go-gitea/gitea/assets/2114189/f1d5f50f-0534-40f0-b0be-f2c9daa5fe92)
2023-05-26 15:31:55 +08:00
|
|
|
go graceful.GetManager().RunWithCancel(indexerQueue)
|
2017-09-25 08:08:48 +08:00
|
|
|
|
2020-08-31 00:08:01 +08:00
|
|
|
if populate {
|
2020-09-07 23:05:08 +08:00
|
|
|
go graceful.GetManager().RunWithShutdownContext(populateRepoIndexer)
|
2019-12-23 20:31:16 +08:00
|
|
|
}
|
2019-12-24 15:26:34 +08:00
|
|
|
select {
|
|
|
|
case waitChannel <- time.Since(start):
|
|
|
|
case <-graceful.GetManager().IsShutdown():
|
|
|
|
}
|
2017-09-25 08:08:48 +08:00
|
|
|
|
2019-12-24 15:26:34 +08:00
|
|
|
close(waitChannel)
|
2019-12-23 20:31:16 +08:00
|
|
|
}()
|
2017-09-25 08:08:48 +08:00
|
|
|
|
2019-12-23 20:31:16 +08:00
|
|
|
if setting.Indexer.StartupTimeout > 0 {
|
|
|
|
go func() {
|
2022-04-01 01:01:43 +08:00
|
|
|
pprof.SetGoroutineLabels(ctx)
|
2019-12-23 20:31:16 +08:00
|
|
|
timeout := setting.Indexer.StartupTimeout
|
|
|
|
if graceful.GetManager().IsChild() && setting.GracefulHammerTime > 0 {
|
|
|
|
timeout += setting.GracefulHammerTime
|
|
|
|
}
|
|
|
|
select {
|
2019-12-24 15:26:34 +08:00
|
|
|
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
|
|
|
|
}
|
2019-12-23 20:31:16 +08:00
|
|
|
log.Info("Repository Indexer Initialization took %v", duration)
|
|
|
|
case <-time.After(timeout):
|
2019-12-24 15:26:34 +08:00
|
|
|
cancel()
|
|
|
|
indexer.Close()
|
2019-12-23 20:31:16 +08:00
|
|
|
log.Fatal("Repository Indexer Initialization Timed-Out after: %v", timeout)
|
|
|
|
}
|
|
|
|
}()
|
2017-09-25 08:08:48 +08:00
|
|
|
}
|
|
|
|
}
|
2020-09-07 23:05:08 +08:00
|
|
|
|
|
|
|
// UpdateRepoIndexer update a repository's entries in the indexer
|
2021-12-10 09:27:50 +08:00
|
|
|
func UpdateRepoIndexer(repo *repo_model.Repository) {
|
2020-09-07 23:05:08 +08:00
|
|
|
indexData := &IndexerData{RepoID: repo.ID}
|
|
|
|
if err := indexerQueue.Push(indexData); err != nil {
|
|
|
|
log.Error("Update repo index data %v failed: %v", indexData, err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-01-27 16:30:51 +08:00
|
|
|
// IsAvailable checks if issue indexer is available
|
|
|
|
func IsAvailable() bool {
|
|
|
|
idx, err := indexer.get()
|
|
|
|
if err != nil {
|
|
|
|
log.Error("IsAvailable(): unable to get indexer: %v", err)
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
return idx.Ping()
|
|
|
|
}
|
|
|
|
|
2020-09-07 23:05:08 +08:00
|
|
|
// populateRepoIndexer populate the repo indexer with pre-existing data. This
|
|
|
|
// should only be run when the indexer is created for the first time.
|
|
|
|
func populateRepoIndexer(ctx context.Context) {
|
|
|
|
log.Info("Populating the repo indexer with existing repositories")
|
|
|
|
|
2021-09-19 19:49:59 +08:00
|
|
|
exist, err := db.IsTableNotEmpty("repository")
|
2020-09-07 23:05:08 +08:00
|
|
|
if err != nil {
|
|
|
|
log.Fatal("System error: %v", err)
|
|
|
|
} else if !exist {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// if there is any existing repo indexer metadata in the DB, delete it
|
|
|
|
// since we are starting afresh. Also, xorm requires deletes to have a
|
|
|
|
// condition, and we want to delete everything, thus 1=1.
|
2021-09-19 19:49:59 +08:00
|
|
|
if err := db.DeleteAllRecords("repo_indexer_status"); err != nil {
|
2020-09-07 23:05:08 +08:00
|
|
|
log.Fatal("System error: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
var maxRepoID int64
|
2021-09-19 19:49:59 +08:00
|
|
|
if maxRepoID, err = db.GetMaxID("repository"); err != nil {
|
2020-09-07 23:05:08 +08:00
|
|
|
log.Fatal("System error: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// start with the maximum existing repo ID and work backwards, so that we
|
|
|
|
// don't include repos that are created after gitea starts; such repos will
|
|
|
|
// already be added to the indexer, and we don't need to add them again.
|
|
|
|
for maxRepoID > 0 {
|
|
|
|
select {
|
|
|
|
case <-ctx.Done():
|
|
|
|
log.Info("Repository Indexer population shutdown before completion")
|
|
|
|
return
|
|
|
|
default:
|
|
|
|
}
|
2021-12-10 09:27:50 +08:00
|
|
|
ids, err := repo_model.GetUnindexedRepos(repo_model.RepoIndexerTypeCode, maxRepoID, 0, 50)
|
2020-09-07 23:05:08 +08:00
|
|
|
if err != nil {
|
|
|
|
log.Error("populateRepoIndexer: %v", err)
|
|
|
|
return
|
|
|
|
} else if len(ids) == 0 {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
for _, id := range ids {
|
|
|
|
select {
|
|
|
|
case <-ctx.Done():
|
|
|
|
log.Info("Repository Indexer population shutdown before completion")
|
|
|
|
return
|
|
|
|
default:
|
|
|
|
}
|
|
|
|
if err := indexerQueue.Push(&IndexerData{RepoID: id}); err != nil {
|
|
|
|
log.Error("indexerQueue.Push: %v", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
maxRepoID = id - 1
|
|
|
|
}
|
|
|
|
}
|
|
|
|
log.Info("Done (re)populating the repo indexer with existing repositories")
|
|
|
|
}
|