Skip to content

Commit 8038610

Browse files
authored
Automatically pause queue if index service is unavailable (#15066)
* Handle keyword search error when issue indexer service is not available * Implement automatic disabling and resume of code indexer queue
1 parent 2649edd commit 8038610

File tree

28 files changed

+522
-151
lines changed

28 files changed

+522
-151
lines changed

docs/content/doc/developers/hacking-on-gitea.en-us.md

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -187,6 +187,27 @@ make lint-frontend
187187

188188
Note: When working on frontend code, set `USE_SERVICE_WORKER` to `false` in `app.ini` to prevent undesirable caching of frontend assets.
189189

190+
### Configuring local ElasticSearch instance
191+
192+
Start local ElasticSearch instance using docker:
193+
194+
```sh
195+
mkdir -p $(pwd)/data/elasticsearch
196+
sudo chown -R 1000:1000 $(pwd)/data/elasticsearch
197+
docker run --rm -p 127.0.0.1:9200:9200 -p 127.0.0.1:9300:9300 -e "discovery.type=single-node" -v "$(pwd)/data/elasticsearch:/usr/share/elasticsearch/data" docker.elastic.co/elasticsearch/elasticsearch:7.16.3
198+
```
199+
200+
Configure `app.ini`:
201+
202+
```ini
203+
[indexer]
204+
ISSUE_INDEXER_TYPE = elasticsearch
205+
ISSUE_INDEXER_CONN_STR = http://elastic:changeme@localhost:9200
206+
REPO_INDEXER_ENABLED = true
207+
REPO_INDEXER_TYPE = elasticsearch
208+
REPO_INDEXER_CONN_STR = http://elastic:changeme@localhost:9200
209+
```
210+
190211
### Building and adding SVGs
191212

192213
SVG icons are built using the `make svg` target which compiles the icon sources defined in `build/generate-svg.js` into the output directory `public/img/svg`. Custom icons can be added in the `web_src/svg` directory.

integrations/repo_search_test.go

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,7 @@ import (
1717
)
1818

1919
func resultFilenames(t testing.TB, doc *HTMLDoc) []string {
20-
resultsSelection := doc.doc.Find(".repository.search")
21-
assert.EqualValues(t, 1, resultsSelection.Length(),
22-
"Invalid template (repo search template has changed?)")
23-
filenameSelections := resultsSelection.Find(".repo-search-result").Find(".header").Find("span.file")
20+
filenameSelections := doc.doc.Find(".repository.search").Find(".repo-search-result").Find(".header").Find("span.file")
2421
result := make([]string, filenameSelections.Length())
2522
filenameSelections.Each(func(i int, selection *goquery.Selection) {
2623
result[i] = selection.Text()

models/db/engine.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@ type Engine interface {
6565
Query(...interface{}) ([]map[string][]byte, error)
6666
Cols(...string) *xorm.Session
6767
Context(ctx context.Context) *xorm.Session
68+
Ping() error
6869
}
6970

7071
// TableInfo returns table's information via an object

models/issue.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1859,7 +1859,7 @@ func GetRepoIssueStats(repoID, uid int64, filterMode int, isPull bool) (numOpen,
18591859
}
18601860

18611861
// SearchIssueIDsByKeyword search issues on database
1862-
func SearchIssueIDsByKeyword(kw string, repoIDs []int64, limit, start int) (int64, []int64, error) {
1862+
func SearchIssueIDsByKeyword(ctx context.Context, kw string, repoIDs []int64, limit, start int) (int64, []int64, error) {
18631863
repoCond := builder.In("repo_id", repoIDs)
18641864
subQuery := builder.Select("id").From("issue").Where(repoCond)
18651865
kw = strings.ToUpper(kw)
@@ -1884,7 +1884,7 @@ func SearchIssueIDsByKeyword(kw string, repoIDs []int64, limit, start int) (int6
18841884
ID int64
18851885
UpdatedUnix int64
18861886
}, 0, limit)
1887-
err := db.GetEngine(db.DefaultContext).Distinct("id", "updated_unix").Table("issue").Where(cond).
1887+
err := db.GetEngine(ctx).Distinct("id", "updated_unix").Table("issue").Where(cond).
18881888
OrderBy("`updated_unix` DESC").Limit(limit, start).
18891889
Find(&res)
18901890
if err != nil {
@@ -1894,7 +1894,7 @@ func SearchIssueIDsByKeyword(kw string, repoIDs []int64, limit, start int) (int6
18941894
ids = append(ids, r.ID)
18951895
}
18961896

1897-
total, err := db.GetEngine(db.DefaultContext).Distinct("id").Table("issue").Where(cond).Count()
1897+
total, err := db.GetEngine(ctx).Distinct("id").Table("issue").Where(cond).Count()
18981898
if err != nil {
18991899
return 0, nil, err
19001900
}

models/issue_test.go

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
package models
66

77
import (
8+
"context"
89
"fmt"
910
"sort"
1011
"sync"
@@ -303,23 +304,23 @@ func TestIssue_loadTotalTimes(t *testing.T) {
303304

304305
func TestIssue_SearchIssueIDsByKeyword(t *testing.T) {
305306
assert.NoError(t, unittest.PrepareTestDatabase())
306-
total, ids, err := SearchIssueIDsByKeyword("issue2", []int64{1}, 10, 0)
307+
total, ids, err := SearchIssueIDsByKeyword(context.TODO(), "issue2", []int64{1}, 10, 0)
307308
assert.NoError(t, err)
308309
assert.EqualValues(t, 1, total)
309310
assert.EqualValues(t, []int64{2}, ids)
310311

311-
total, ids, err = SearchIssueIDsByKeyword("first", []int64{1}, 10, 0)
312+
total, ids, err = SearchIssueIDsByKeyword(context.TODO(), "first", []int64{1}, 10, 0)
312313
assert.NoError(t, err)
313314
assert.EqualValues(t, 1, total)
314315
assert.EqualValues(t, []int64{1}, ids)
315316

316-
total, ids, err = SearchIssueIDsByKeyword("for", []int64{1}, 10, 0)
317+
total, ids, err = SearchIssueIDsByKeyword(context.TODO(), "for", []int64{1}, 10, 0)
317318
assert.NoError(t, err)
318319
assert.EqualValues(t, 5, total)
319320
assert.ElementsMatch(t, []int64{1, 2, 3, 5, 11}, ids)
320321

321322
// issue1's comment id 2
322-
total, ids, err = SearchIssueIDsByKeyword("good", []int64{1}, 10, 0)
323+
total, ids, err = SearchIssueIDsByKeyword(context.TODO(), "good", []int64{1}, 10, 0)
323324
assert.NoError(t, err)
324325
assert.EqualValues(t, 1, total)
325326
assert.EqualValues(t, []int64{1}, ids)
@@ -464,7 +465,7 @@ func TestCorrectIssueStats(t *testing.T) {
464465
wg.Wait()
465466

466467
// Now we will get all issueID's that match the "Bugs are nasty" query.
467-
total, ids, err := SearchIssueIDsByKeyword("Bugs are nasty", []int64{1}, issueAmount, 0)
468+
total, ids, err := SearchIssueIDsByKeyword(context.TODO(), "Bugs are nasty", []int64{1}, issueAmount, 0)
468469

469470
// Just to be sure.
470471
assert.NoError(t, err)

modules/context/repo.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import (
2121
user_model "code.gitea.io/gitea/models/user"
2222
"code.gitea.io/gitea/modules/cache"
2323
"code.gitea.io/gitea/modules/git"
24+
code_indexer "code.gitea.io/gitea/modules/indexer/code"
2425
"code.gitea.io/gitea/modules/log"
2526
"code.gitea.io/gitea/modules/markup/markdown"
2627
"code.gitea.io/gitea/modules/setting"
@@ -522,6 +523,9 @@ func RepoAssignment(ctx *Context) (cancel context.CancelFunc) {
522523
ctx.Data["ExposeAnonSSH"] = setting.SSH.ExposeAnonymous
523524
ctx.Data["DisableHTTP"] = setting.Repository.DisableHTTPGit
524525
ctx.Data["RepoSearchEnabled"] = setting.Indexer.RepoIndexerEnabled
526+
if setting.Indexer.RepoIndexerEnabled {
527+
ctx.Data["CodeIndexerUnavailable"] = !code_indexer.IsAvailable()
528+
}
525529
ctx.Data["CloneLink"] = repo.CloneLink()
526530
ctx.Data["WikiCloneLink"] = repo.WikiCloneLink()
527531

modules/indexer/code/bleve.go

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -271,6 +271,15 @@ func (b *BleveIndexer) Close() {
271271
log.Info("PID: %d Repository Indexer closed", os.Getpid())
272272
}
273273

274+
// SetAvailabilityChangeCallback does nothing
275+
func (b *BleveIndexer) SetAvailabilityChangeCallback(callback func(bool)) {
276+
}
277+
278+
// Ping does nothing
279+
func (b *BleveIndexer) Ping() bool {
280+
return true
281+
}
282+
274283
// Index indexes the data
275284
func (b *BleveIndexer) Index(ctx context.Context, repo *repo_model.Repository, sha string, changes *repoChanges) error {
276285
batch := gitea_bleve.NewFlushingBatch(b.indexer, maxBatchSize)
@@ -319,7 +328,7 @@ func (b *BleveIndexer) Delete(repoID int64) error {
319328

320329
// Search searches for files in the specified repo.
321330
// Returns the matching file-paths
322-
func (b *BleveIndexer) Search(repoIDs []int64, language, keyword string, page, pageSize int, isMatch bool) (int64, []*SearchResult, []*SearchResultLanguages, error) {
331+
func (b *BleveIndexer) Search(ctx context.Context, repoIDs []int64, language, keyword string, page, pageSize int, isMatch bool) (int64, []*SearchResult, []*SearchResultLanguages, error) {
323332
var (
324333
indexerQuery query.Query
325334
keywordQuery query.Query
@@ -372,7 +381,7 @@ func (b *BleveIndexer) Search(repoIDs []int64, language, keyword string, page, p
372381
searchRequest.AddFacet("languages", bleve.NewFacetRequest("Language", 10))
373382
}
374383

375-
result, err := b.indexer.Search(searchRequest)
384+
result, err := b.indexer.SearchInContext(ctx, searchRequest)
376385
if err != nil {
377386
return 0, nil, nil, err
378387
}

0 commit comments

Comments
 (0)