2020-01-07 16:53:09 +05:30
|
|
|
// 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"
|
|
|
|
"sync"
|
2020-01-29 06:31:06 +05:30
|
|
|
"sync/atomic"
|
2020-01-07 16:53:09 +05:30
|
|
|
"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 {
|
2020-01-29 06:31:06 +05:30
|
|
|
WorkerPoolConfiguration
|
|
|
|
DataDir string
|
|
|
|
Workers int
|
|
|
|
Name string
|
2020-01-07 16:53:09 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
// LevelQueue implements a disk library queue
|
|
|
|
type LevelQueue struct {
|
2020-01-29 06:31:06 +05:30
|
|
|
*WorkerPool
|
2020-01-07 16:53:09 +05:30
|
|
|
queue *levelqueue.Queue
|
|
|
|
closed chan struct{}
|
|
|
|
terminated chan struct{}
|
|
|
|
lock sync.Mutex
|
|
|
|
exemplar interface{}
|
|
|
|
workers int
|
|
|
|
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)
|
|
|
|
|
|
|
|
internal, err := levelqueue.Open(config.DataDir)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
queue := &LevelQueue{
|
2020-01-29 06:31:06 +05:30
|
|
|
WorkerPool: NewWorkerPool(handle, config.WorkerPoolConfiguration),
|
2020-01-07 16:53:09 +05:30
|
|
|
queue: internal,
|
|
|
|
exemplar: exemplar,
|
|
|
|
closed: make(chan struct{}),
|
|
|
|
terminated: make(chan struct{}),
|
|
|
|
workers: config.Workers,
|
|
|
|
name: config.Name,
|
|
|
|
}
|
2020-01-29 06:31:06 +05:30
|
|
|
queue.qid = GetManager().Add(queue, LevelQueueType, config, exemplar)
|
2020-01-07 16:53:09 +05:30
|
|
|
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)
|
2020-01-29 06:31:06 +05:30
|
|
|
log.Debug("LevelQueue: %s Starting", l.name)
|
2020-01-07 16:53:09 +05:30
|
|
|
|
|
|
|
go func() {
|
2020-01-29 06:31:06 +05:30
|
|
|
_ = l.AddWorkers(l.workers, 0)
|
2020-01-07 16:53:09 +05:30
|
|
|
}()
|
|
|
|
|
|
|
|
go l.readToChan()
|
|
|
|
|
|
|
|
log.Trace("LevelQueue: %s Waiting til closed", l.name)
|
|
|
|
<-l.closed
|
|
|
|
|
|
|
|
log.Trace("LevelQueue: %s Waiting til done", l.name)
|
2020-01-29 06:31:06 +05:30
|
|
|
l.Wait()
|
2020-01-07 16:53:09 +05:30
|
|
|
|
|
|
|
log.Trace("LevelQueue: %s Waiting til cleaned", l.name)
|
|
|
|
ctx, cancel := context.WithCancel(context.Background())
|
|
|
|
atTerminate(ctx, cancel)
|
2020-01-29 06:31:06 +05:30
|
|
|
l.CleanUp(ctx)
|
2020-01-07 16:53:09 +05:30
|
|
|
cancel()
|
|
|
|
log.Trace("LevelQueue: %s Cleaned", l.name)
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
func (l *LevelQueue) readToChan() {
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case <-l.closed:
|
|
|
|
// tell the pool to shutdown.
|
2020-01-29 06:31:06 +05:30
|
|
|
l.cancel()
|
2020-01-07 16:53:09 +05:30
|
|
|
return
|
|
|
|
default:
|
2020-01-29 06:31:06 +05:30
|
|
|
atomic.AddInt64(&l.numInQueue, 1)
|
2020-01-07 16:53:09 +05:30
|
|
|
bs, err := l.queue.RPop()
|
|
|
|
if err != nil {
|
|
|
|
if err != levelqueue.ErrNotFound {
|
|
|
|
log.Error("LevelQueue: %s Error on RPop: %v", l.name, err)
|
|
|
|
}
|
2020-01-29 06:31:06 +05:30
|
|
|
atomic.AddInt64(&l.numInQueue, -1)
|
2020-01-07 16:53:09 +05:30
|
|
|
time.Sleep(time.Millisecond * 100)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(bs) == 0 {
|
2020-01-29 06:31:06 +05:30
|
|
|
atomic.AddInt64(&l.numInQueue, -1)
|
2020-01-07 16:53:09 +05:30
|
|
|
time.Sleep(time.Millisecond * 100)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2020-01-29 06:31:06 +05:30
|
|
|
data, err := unmarshalAs(bs, l.exemplar)
|
2020-01-07 16:53:09 +05:30
|
|
|
if err != nil {
|
|
|
|
log.Error("LevelQueue: %s Failed to unmarshal with error: %v", l.name, err)
|
2020-01-29 06:31:06 +05:30
|
|
|
atomic.AddInt64(&l.numInQueue, -1)
|
2020-01-07 16:53:09 +05:30
|
|
|
time.Sleep(time.Millisecond * 100)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
log.Trace("LevelQueue %s: Task found: %#v", l.name, data)
|
2020-01-29 06:31:06 +05:30
|
|
|
l.WorkerPool.Push(data)
|
|
|
|
atomic.AddInt64(&l.numInQueue, -1)
|
2020-01-07 16:53:09 +05:30
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Push will push the indexer data to queue
|
|
|
|
func (l *LevelQueue) Push(data Data) error {
|
2020-01-29 06:31:06 +05:30
|
|
|
if !assignableTo(data, l.exemplar) {
|
|
|
|
return fmt.Errorf("Unable to assign data: %v to same type as exemplar: %v in %s", data, l.exemplar, l.name)
|
2020-01-07 16:53:09 +05:30
|
|
|
}
|
|
|
|
bs, err := json.Marshal(data)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return l.queue.LPush(bs)
|
|
|
|
}
|
|
|
|
|
2020-01-29 06:31:06 +05:30
|
|
|
// IsEmpty checks whether the queue is empty
|
|
|
|
func (l *LevelQueue) IsEmpty() bool {
|
|
|
|
if !l.WorkerPool.IsEmpty() {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
return l.queue.Len() == 0
|
|
|
|
}
|
|
|
|
|
2020-01-07 16:53:09 +05:30
|
|
|
// Shutdown this queue and stop processing
|
|
|
|
func (l *LevelQueue) Shutdown() {
|
|
|
|
l.lock.Lock()
|
|
|
|
defer l.lock.Unlock()
|
2020-01-29 06:31:06 +05:30
|
|
|
log.Trace("LevelQueue: %s Shutting down", l.name)
|
2020-01-07 16:53:09 +05:30
|
|
|
select {
|
|
|
|
case <-l.closed:
|
|
|
|
default:
|
|
|
|
close(l.closed)
|
|
|
|
}
|
2020-01-29 06:31:06 +05:30
|
|
|
log.Debug("LevelQueue: %s Shutdown", l.name)
|
2020-01-07 16:53:09 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
// Terminate this queue and close the queue
|
|
|
|
func (l *LevelQueue) Terminate() {
|
|
|
|
log.Trace("LevelQueue: %s Terminating", l.name)
|
|
|
|
l.Shutdown()
|
|
|
|
l.lock.Lock()
|
|
|
|
select {
|
|
|
|
case <-l.terminated:
|
|
|
|
l.lock.Unlock()
|
|
|
|
default:
|
|
|
|
close(l.terminated)
|
|
|
|
l.lock.Unlock()
|
2020-01-29 06:31:06 +05:30
|
|
|
if log.IsDebug() {
|
|
|
|
log.Debug("LevelQueue: %s Closing with %d tasks left in queue", l.name, l.queue.Len())
|
|
|
|
}
|
2020-01-07 16:53:09 +05:30
|
|
|
if err := l.queue.Close(); err != nil && err.Error() != "leveldb: closed" {
|
|
|
|
log.Error("Error whilst closing internal queue in %s: %v", l.name, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
2020-01-29 06:31:06 +05:30
|
|
|
log.Debug("LevelQueue: %s Terminated", l.name)
|
2020-01-07 16:53:09 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
// Name returns the name of this queue
|
|
|
|
func (l *LevelQueue) Name() string {
|
|
|
|
return l.name
|
|
|
|
}
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
queuesMap[LevelQueueType] = NewLevelQueue
|
|
|
|
}
|