gitea/modules/queue/queue_disk.go

208 lines
4.8 KiB
Go
Raw Normal View History

// Copyright 2019 The Gitea Authors. All rights reserved.
// Use of this source code is governed by a MIT-style
// license that can be found in the LICENSE file.
package queue
import (
"context"
"encoding/json"
"fmt"
"reflect"
"time"
"code.gitea.io/gitea/modules/log"
"gitea.com/lunny/levelqueue"
)
// LevelQueueType is the type for level queue
const LevelQueueType Type = "level"
// LevelQueueConfiguration is the configuration for a LevelQueue
type LevelQueueConfiguration struct {
2019-12-07 17:44:37 +01:00
DataDir string
QueueLength int
BatchLength int
Workers int
2019-12-30 16:54:19 +01:00
MaxWorkers int
2019-12-07 17:44:37 +01:00
BlockTimeout time.Duration
BoostTimeout time.Duration
BoostWorkers int
2019-12-07 17:46:36 +01:00
Name string
}
// LevelQueue implements a disk library queue
type LevelQueue struct {
2019-12-07 17:44:37 +01:00
pool *WorkerPool
queue *levelqueue.Queue
closed chan struct{}
terminated chan struct{}
exemplar interface{}
workers int
2019-12-07 17:46:36 +01:00
name string
}
// NewLevelQueue creates a ledis local queue
func NewLevelQueue(handle HandlerFunc, cfg, exemplar interface{}) (Queue, error) {
configInterface, err := toConfig(LevelQueueConfiguration{}, cfg)
if err != nil {
return nil, err
}
config := configInterface.(LevelQueueConfiguration)
2019-12-07 17:48:21 +01:00
internal, err := levelqueue.Open(config.DataDir)
if err != nil {
return nil, err
}
2019-12-07 17:44:37 +01:00
dataChan := make(chan Data, config.QueueLength)
ctx, cancel := context.WithCancel(context.Background())
2019-12-07 17:48:21 +01:00
queue := &LevelQueue{
2019-12-07 17:44:37 +01:00
pool: &WorkerPool{
2019-12-30 16:54:19 +01:00
baseCtx: ctx,
cancel: cancel,
batchLength: config.BatchLength,
handle: handle,
dataChan: dataChan,
blockTimeout: config.BlockTimeout,
boostTimeout: config.BoostTimeout,
boostWorkers: config.BoostWorkers,
maxNumberOfWorkers: config.MaxWorkers,
2019-12-07 17:44:37 +01:00
},
2019-12-07 17:48:21 +01:00
queue: internal,
2019-12-07 17:44:37 +01:00
exemplar: exemplar,
closed: make(chan struct{}),
terminated: make(chan struct{}),
workers: config.Workers,
2019-12-07 17:46:36 +01:00
name: config.Name,
2019-12-07 17:48:21 +01:00
}
2019-12-30 16:54:19 +01:00
queue.pool.qid = GetManager().Add(queue, LevelQueueType, config, exemplar, queue.pool)
2019-12-07 17:48:21 +01:00
return queue, nil
}
// Run starts to run the queue
func (l *LevelQueue) Run(atShutdown, atTerminate func(context.Context, func())) {
atShutdown(context.Background(), l.Shutdown)
atTerminate(context.Background(), l.Terminate)
2019-12-07 17:48:21 +01:00
go func() {
_ = l.pool.AddWorkers(l.workers, 0)
}()
2019-12-07 17:44:37 +01:00
go l.readToChan()
2019-12-11 21:34:05 +01:00
log.Trace("LevelQueue: %s Waiting til closed", l.name)
2019-12-07 17:44:37 +01:00
<-l.closed
2019-12-11 21:34:05 +01:00
log.Trace("LevelQueue: %s Waiting til done", l.name)
2019-12-07 17:44:37 +01:00
l.pool.Wait()
2019-12-11 21:34:05 +01:00
log.Trace("LevelQueue: %s Waiting til cleaned", l.name)
ctx, cancel := context.WithCancel(context.Background())
atTerminate(ctx, cancel)
l.pool.CleanUp(ctx)
cancel()
log.Trace("LevelQueue: %s Cleaned", l.name)
2019-12-07 17:44:37 +01:00
}
2019-12-07 17:44:37 +01:00
func (l *LevelQueue) readToChan() {
for {
select {
case <-l.closed:
2019-12-07 17:44:37 +01:00
// tell the pool to shutdown.
l.pool.cancel()
return
default:
2019-12-07 17:44:37 +01:00
bs, err := l.queue.RPop()
if err != nil {
if err != levelqueue.ErrNotFound {
2019-12-11 21:34:05 +01:00
log.Error("LevelQueue: %s Error on RPop: %v", l.name, err)
2019-12-07 17:44:37 +01:00
}
time.Sleep(time.Millisecond * 100)
continue
}
2019-12-07 17:44:37 +01:00
if len(bs) == 0 {
time.Sleep(time.Millisecond * 100)
continue
}
2019-12-07 17:44:37 +01:00
var data Data
if l.exemplar != nil {
t := reflect.TypeOf(l.exemplar)
n := reflect.New(t)
ne := n.Elem()
err = json.Unmarshal(bs, ne.Addr().Interface())
data = ne.Interface().(Data)
} else {
err = json.Unmarshal(bs, &data)
}
if err != nil {
2019-12-11 21:34:05 +01:00
log.Error("LevelQueue: %s Failed to unmarshal with error: %v", l.name, err)
time.Sleep(time.Millisecond * 100)
2019-12-07 17:44:37 +01:00
continue
}
2019-12-11 21:34:05 +01:00
log.Trace("LevelQueue %s: Task found: %#v", l.name, data)
2019-12-07 17:44:37 +01:00
l.pool.Push(data)
2019-12-11 21:34:05 +01:00
time.Sleep(time.Millisecond * 10)
2019-12-07 17:44:37 +01:00
}
}
}
// Push will push the indexer data to queue
func (l *LevelQueue) Push(data Data) error {
if l.exemplar != nil {
// Assert data is of same type as r.exemplar
value := reflect.ValueOf(data)
t := value.Type()
exemplarType := reflect.ValueOf(l.exemplar).Type()
if !t.AssignableTo(exemplarType) || data == nil {
return fmt.Errorf("Unable to assign data: %v to same type as exemplar: %v in %s", data, l.exemplar, l.name)
}
}
bs, err := json.Marshal(data)
if err != nil {
return err
}
return l.queue.LPush(bs)
}
// Shutdown this queue and stop processing
func (l *LevelQueue) Shutdown() {
2019-12-11 21:34:05 +01:00
log.Trace("LevelQueue: %s Shutdown", l.name)
select {
case <-l.closed:
default:
close(l.closed)
}
}
// Terminate this queue and close the queue
func (l *LevelQueue) Terminate() {
2019-12-11 21:34:05 +01:00
log.Trace("LevelQueue: %s Terminating", l.name)
l.Shutdown()
2019-12-11 21:34:05 +01:00
select {
case <-l.terminated:
default:
close(l.terminated)
if err := l.queue.Close(); err != nil && err.Error() != "leveldb: closed" {
log.Error("Error whilst closing internal queue in %s: %v", l.name, err)
}
}
}
2019-12-07 17:48:21 +01:00
// Name returns the name of this queue
func (l *LevelQueue) Name() string {
return l.name
}
func init() {
queuesMap[LevelQueueType] = NewLevelQueue
}