2020-02-13 07:06:17 +01:00
|
|
|
// Copyright 2019 The Gitea Authors. All rights reserved.
|
2022-11-27 19:20:29 +01:00
|
|
|
// SPDX-License-Identifier: MIT
|
2020-02-13 07:06:17 +01:00
|
|
|
|
|
|
|
package issues
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
2022-01-27 09:30:51 +01:00
|
|
|
"net"
|
2020-02-13 07:06:17 +01:00
|
|
|
"strconv"
|
2022-01-27 09:30:51 +01:00
|
|
|
"sync"
|
2020-02-13 07:06:17 +01:00
|
|
|
"time"
|
|
|
|
|
2022-01-27 09:30:51 +01:00
|
|
|
"code.gitea.io/gitea/modules/graceful"
|
2020-02-13 07:06:17 +01:00
|
|
|
"code.gitea.io/gitea/modules/log"
|
|
|
|
|
|
|
|
"github.com/olivere/elastic/v7"
|
|
|
|
)
|
|
|
|
|
2022-01-20 18:46:10 +01:00
|
|
|
var _ Indexer = &ElasticSearchIndexer{}
|
2020-02-13 07:06:17 +01:00
|
|
|
|
|
|
|
// ElasticSearchIndexer implements Indexer interface
|
|
|
|
type ElasticSearchIndexer struct {
|
2022-01-27 09:30:51 +01:00
|
|
|
client *elastic.Client
|
|
|
|
indexerName string
|
|
|
|
available bool
|
|
|
|
availabilityCallback func(bool)
|
|
|
|
stopTimer chan struct{}
|
|
|
|
lock sync.RWMutex
|
2020-02-13 07:06:17 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
type elasticLogger struct {
|
2020-10-31 06:36:46 +01:00
|
|
|
log.LevelLogger
|
2020-02-13 07:06:17 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func (l elasticLogger) Printf(format string, args ...interface{}) {
|
2020-10-31 06:36:46 +01:00
|
|
|
_ = l.Log(2, l.GetLevel(), format, args...)
|
2020-02-13 07:06:17 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// NewElasticSearchIndexer creates a new elasticsearch indexer
|
|
|
|
func NewElasticSearchIndexer(url, indexerName string) (*ElasticSearchIndexer, error) {
|
|
|
|
opts := []elastic.ClientOptionFunc{
|
|
|
|
elastic.SetURL(url),
|
|
|
|
elastic.SetSniff(false),
|
|
|
|
elastic.SetHealthcheckInterval(10 * time.Second),
|
|
|
|
elastic.SetGzip(false),
|
|
|
|
}
|
|
|
|
|
|
|
|
logger := elasticLogger{log.GetLogger(log.DEFAULT)}
|
|
|
|
|
|
|
|
if logger.GetLevel() == log.TRACE || logger.GetLevel() == log.DEBUG {
|
|
|
|
opts = append(opts, elastic.SetTraceLog(logger))
|
|
|
|
} else if logger.GetLevel() == log.ERROR || logger.GetLevel() == log.CRITICAL || logger.GetLevel() == log.FATAL {
|
|
|
|
opts = append(opts, elastic.SetErrorLog(logger))
|
|
|
|
} else if logger.GetLevel() == log.INFO || logger.GetLevel() == log.WARN {
|
|
|
|
opts = append(opts, elastic.SetInfoLog(logger))
|
|
|
|
}
|
|
|
|
|
|
|
|
client, err := elastic.NewClient(opts...)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2022-01-27 09:30:51 +01:00
|
|
|
indexer := &ElasticSearchIndexer{
|
2020-02-13 07:06:17 +01:00
|
|
|
client: client,
|
|
|
|
indexerName: indexerName,
|
2022-01-27 09:30:51 +01:00
|
|
|
available: true,
|
|
|
|
stopTimer: make(chan struct{}),
|
|
|
|
}
|
|
|
|
|
|
|
|
ticker := time.NewTicker(10 * time.Second)
|
|
|
|
go func() {
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case <-ticker.C:
|
|
|
|
indexer.checkAvailability()
|
|
|
|
case <-indexer.stopTimer:
|
|
|
|
ticker.Stop()
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
return indexer, nil
|
2020-02-13 07:06:17 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
const (
|
|
|
|
defaultMapping = `{
|
|
|
|
"mappings": {
|
|
|
|
"properties": {
|
|
|
|
"id": {
|
|
|
|
"type": "integer",
|
|
|
|
"index": true
|
|
|
|
},
|
|
|
|
"repo_id": {
|
|
|
|
"type": "integer",
|
|
|
|
"index": true
|
|
|
|
},
|
|
|
|
"title": {
|
|
|
|
"type": "text",
|
|
|
|
"index": true
|
|
|
|
},
|
|
|
|
"content": {
|
|
|
|
"type": "text",
|
|
|
|
"index": true
|
|
|
|
},
|
|
|
|
"comments": {
|
|
|
|
"type" : "text",
|
|
|
|
"index": true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}`
|
|
|
|
)
|
|
|
|
|
|
|
|
// Init will initialize the indexer
|
|
|
|
func (b *ElasticSearchIndexer) Init() (bool, error) {
|
2022-01-27 09:30:51 +01:00
|
|
|
ctx := graceful.GetManager().HammerContext()
|
2020-02-13 07:06:17 +01:00
|
|
|
exists, err := b.client.IndexExists(b.indexerName).Do(ctx)
|
|
|
|
if err != nil {
|
2022-01-27 09:30:51 +01:00
|
|
|
return false, b.checkError(err)
|
2020-02-13 07:06:17 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if !exists {
|
2022-01-20 18:46:10 +01:00
|
|
|
mapping := defaultMapping
|
2020-02-13 07:06:17 +01:00
|
|
|
|
|
|
|
createIndex, err := b.client.CreateIndex(b.indexerName).BodyString(mapping).Do(ctx)
|
|
|
|
if err != nil {
|
2022-01-27 09:30:51 +01:00
|
|
|
return false, b.checkError(err)
|
2020-02-13 07:06:17 +01:00
|
|
|
}
|
|
|
|
if !createIndex.Acknowledged {
|
|
|
|
return false, errors.New("init failed")
|
|
|
|
}
|
|
|
|
|
|
|
|
return false, nil
|
|
|
|
}
|
|
|
|
return true, nil
|
|
|
|
}
|
|
|
|
|
2022-01-27 09:30:51 +01:00
|
|
|
// SetAvailabilityChangeCallback sets callback that will be triggered when availability changes
|
|
|
|
func (b *ElasticSearchIndexer) SetAvailabilityChangeCallback(callback func(bool)) {
|
|
|
|
b.lock.Lock()
|
|
|
|
defer b.lock.Unlock()
|
|
|
|
b.availabilityCallback = callback
|
|
|
|
}
|
|
|
|
|
|
|
|
// Ping checks if elastic is available
|
|
|
|
func (b *ElasticSearchIndexer) Ping() bool {
|
|
|
|
b.lock.RLock()
|
|
|
|
defer b.lock.RUnlock()
|
|
|
|
return b.available
|
|
|
|
}
|
|
|
|
|
2020-02-13 07:06:17 +01:00
|
|
|
// Index will save the index data
|
|
|
|
func (b *ElasticSearchIndexer) Index(issues []*IndexerData) error {
|
|
|
|
if len(issues) == 0 {
|
|
|
|
return nil
|
|
|
|
} else if len(issues) == 1 {
|
|
|
|
issue := issues[0]
|
|
|
|
_, err := b.client.Index().
|
|
|
|
Index(b.indexerName).
|
|
|
|
Id(fmt.Sprintf("%d", issue.ID)).
|
|
|
|
BodyJson(map[string]interface{}{
|
|
|
|
"id": issue.ID,
|
|
|
|
"repo_id": issue.RepoID,
|
|
|
|
"title": issue.Title,
|
|
|
|
"content": issue.Content,
|
|
|
|
"comments": issue.Comments,
|
|
|
|
}).
|
2022-01-27 09:30:51 +01:00
|
|
|
Do(graceful.GetManager().HammerContext())
|
|
|
|
return b.checkError(err)
|
2020-02-13 07:06:17 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
reqs := make([]elastic.BulkableRequest, 0)
|
|
|
|
for _, issue := range issues {
|
|
|
|
reqs = append(reqs,
|
|
|
|
elastic.NewBulkIndexRequest().
|
|
|
|
Index(b.indexerName).
|
|
|
|
Id(fmt.Sprintf("%d", issue.ID)).
|
|
|
|
Doc(map[string]interface{}{
|
|
|
|
"id": issue.ID,
|
|
|
|
"repo_id": issue.RepoID,
|
|
|
|
"title": issue.Title,
|
|
|
|
"content": issue.Content,
|
|
|
|
"comments": issue.Comments,
|
|
|
|
}),
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
_, err := b.client.Bulk().
|
|
|
|
Index(b.indexerName).
|
|
|
|
Add(reqs...).
|
2022-01-27 09:30:51 +01:00
|
|
|
Do(graceful.GetManager().HammerContext())
|
|
|
|
return b.checkError(err)
|
2020-02-13 07:06:17 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Delete deletes indexes by ids
|
|
|
|
func (b *ElasticSearchIndexer) Delete(ids ...int64) error {
|
|
|
|
if len(ids) == 0 {
|
|
|
|
return nil
|
|
|
|
} else if len(ids) == 1 {
|
|
|
|
_, err := b.client.Delete().
|
|
|
|
Index(b.indexerName).
|
|
|
|
Id(fmt.Sprintf("%d", ids[0])).
|
2022-01-27 09:30:51 +01:00
|
|
|
Do(graceful.GetManager().HammerContext())
|
|
|
|
return b.checkError(err)
|
2020-02-13 07:06:17 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
reqs := make([]elastic.BulkableRequest, 0)
|
|
|
|
for _, id := range ids {
|
|
|
|
reqs = append(reqs,
|
|
|
|
elastic.NewBulkDeleteRequest().
|
|
|
|
Index(b.indexerName).
|
|
|
|
Id(fmt.Sprintf("%d", id)),
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
_, err := b.client.Bulk().
|
|
|
|
Index(b.indexerName).
|
|
|
|
Add(reqs...).
|
2022-01-27 09:30:51 +01:00
|
|
|
Do(graceful.GetManager().HammerContext())
|
|
|
|
return b.checkError(err)
|
2020-02-13 07:06:17 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Search searches for issues by given conditions.
|
|
|
|
// Returns the matching issue IDs
|
2022-01-27 09:30:51 +01:00
|
|
|
func (b *ElasticSearchIndexer) Search(ctx context.Context, keyword string, repoIDs []int64, limit, start int) (*SearchResult, error) {
|
2020-02-13 07:06:17 +01:00
|
|
|
kwQuery := elastic.NewMultiMatchQuery(keyword, "title", "content", "comments")
|
|
|
|
query := elastic.NewBoolQuery()
|
|
|
|
query = query.Must(kwQuery)
|
|
|
|
if len(repoIDs) > 0 {
|
2022-01-20 18:46:10 +01:00
|
|
|
repoStrs := make([]interface{}, 0, len(repoIDs))
|
2020-02-13 07:06:17 +01:00
|
|
|
for _, repoID := range repoIDs {
|
|
|
|
repoStrs = append(repoStrs, repoID)
|
|
|
|
}
|
|
|
|
repoQuery := elastic.NewTermsQuery("repo_id", repoStrs...)
|
|
|
|
query = query.Must(repoQuery)
|
|
|
|
}
|
|
|
|
searchResult, err := b.client.Search().
|
|
|
|
Index(b.indexerName).
|
|
|
|
Query(query).
|
2021-01-16 05:55:17 +01:00
|
|
|
Sort("_score", false).
|
2020-02-13 07:06:17 +01:00
|
|
|
From(start).Size(limit).
|
2022-01-27 09:30:51 +01:00
|
|
|
Do(ctx)
|
2020-02-13 07:06:17 +01:00
|
|
|
if err != nil {
|
2022-01-27 09:30:51 +01:00
|
|
|
return nil, b.checkError(err)
|
2020-02-13 07:06:17 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
hits := make([]Match, 0, limit)
|
|
|
|
for _, hit := range searchResult.Hits.Hits {
|
|
|
|
id, _ := strconv.ParseInt(hit.Id, 10, 64)
|
|
|
|
hits = append(hits, Match{
|
|
|
|
ID: id,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
return &SearchResult{
|
|
|
|
Total: searchResult.TotalHits(),
|
|
|
|
Hits: hits,
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Close implements indexer
|
2022-01-27 09:30:51 +01:00
|
|
|
func (b *ElasticSearchIndexer) Close() {
|
|
|
|
select {
|
|
|
|
case <-b.stopTimer:
|
|
|
|
default:
|
|
|
|
close(b.stopTimer)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (b *ElasticSearchIndexer) checkError(err error) error {
|
|
|
|
var opErr *net.OpError
|
|
|
|
if !(elastic.IsConnErr(err) || (errors.As(err, &opErr) && (opErr.Op == "dial" || opErr.Op == "read"))) {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
b.setAvailability(false)
|
|
|
|
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
func (b *ElasticSearchIndexer) checkAvailability() {
|
|
|
|
if b.Ping() {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// Request cluster state to check if elastic is available again
|
|
|
|
_, err := b.client.ClusterState().Do(graceful.GetManager().ShutdownContext())
|
|
|
|
if err != nil {
|
|
|
|
b.setAvailability(false)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
b.setAvailability(true)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (b *ElasticSearchIndexer) setAvailability(available bool) {
|
|
|
|
b.lock.Lock()
|
|
|
|
defer b.lock.Unlock()
|
|
|
|
|
|
|
|
if b.available == available {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
b.available = available
|
|
|
|
if b.availabilityCallback != nil {
|
|
|
|
// Call the callback from within the lock to ensure that the ordering remains correct
|
|
|
|
b.availabilityCallback(b.available)
|
|
|
|
}
|
|
|
|
}
|