2020-01-07 04:23:09 -07:00
|
|
|
// 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"
|
2020-01-28 18:01:06 -07:00
|
|
|
"fmt"
|
2020-01-11 12:06:35 -07:00
|
|
|
"sync"
|
2020-01-28 18:01:06 -07:00
|
|
|
"sync/atomic"
|
2020-01-07 04:23:09 -07:00
|
|
|
"time"
|
|
|
|
|
|
|
|
"code.gitea.io/gitea/modules/log"
|
|
|
|
)
|
|
|
|
|
|
|
|
// PersistableChannelQueueType is the type for persistable queue
|
|
|
|
const PersistableChannelQueueType Type = "persistable-channel"
|
|
|
|
|
|
|
|
// PersistableChannelQueueConfiguration is the configuration for a PersistableChannelQueue
|
|
|
|
type PersistableChannelQueueConfiguration struct {
|
|
|
|
Name string
|
|
|
|
DataDir string
|
|
|
|
BatchLength int
|
|
|
|
QueueLength int
|
|
|
|
Timeout time.Duration
|
|
|
|
MaxAttempts int
|
|
|
|
Workers int
|
|
|
|
MaxWorkers int
|
|
|
|
BlockTimeout time.Duration
|
|
|
|
BoostTimeout time.Duration
|
|
|
|
BoostWorkers int
|
|
|
|
}
|
|
|
|
|
|
|
|
// PersistableChannelQueue wraps a channel queue and level queue together
|
2020-01-28 18:01:06 -07:00
|
|
|
// The disk level queue will be used to store data at shutdown and terminate - and will be restored
|
|
|
|
// on start up.
|
2020-01-07 04:23:09 -07:00
|
|
|
type PersistableChannelQueue struct {
|
2020-01-28 18:01:06 -07:00
|
|
|
channelQueue *ChannelQueue
|
2020-01-07 04:23:09 -07:00
|
|
|
delayedStarter
|
2020-01-11 12:06:35 -07:00
|
|
|
lock sync.Mutex
|
2020-01-07 04:23:09 -07:00
|
|
|
closed chan struct{}
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewPersistableChannelQueue creates a wrapped batched channel queue with persistable level queue backend when shutting down
|
|
|
|
// This differs from a wrapped queue in that the persistent queue is only used to persist at shutdown/terminate
|
|
|
|
func NewPersistableChannelQueue(handle HandlerFunc, cfg, exemplar interface{}) (Queue, error) {
|
|
|
|
configInterface, err := toConfig(PersistableChannelQueueConfiguration{}, cfg)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
config := configInterface.(PersistableChannelQueueConfiguration)
|
|
|
|
|
|
|
|
channelQueue, err := NewChannelQueue(handle, ChannelQueueConfiguration{
|
2020-01-28 18:01:06 -07:00
|
|
|
WorkerPoolConfiguration: WorkerPoolConfiguration{
|
|
|
|
QueueLength: config.QueueLength,
|
|
|
|
BatchLength: config.BatchLength,
|
|
|
|
BlockTimeout: config.BlockTimeout,
|
|
|
|
BoostTimeout: config.BoostTimeout,
|
|
|
|
BoostWorkers: config.BoostWorkers,
|
|
|
|
MaxWorkers: config.MaxWorkers,
|
|
|
|
},
|
|
|
|
Workers: config.Workers,
|
|
|
|
Name: config.Name + "-channel",
|
2020-01-07 04:23:09 -07:00
|
|
|
}, exemplar)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// the level backend only needs temporary workers to catch up with the previously dropped work
|
|
|
|
levelCfg := LevelQueueConfiguration{
|
2020-02-02 16:19:58 -07:00
|
|
|
ByteFIFOQueueConfiguration: ByteFIFOQueueConfiguration{
|
|
|
|
WorkerPoolConfiguration: WorkerPoolConfiguration{
|
|
|
|
QueueLength: config.QueueLength,
|
|
|
|
BatchLength: config.BatchLength,
|
|
|
|
BlockTimeout: 1 * time.Second,
|
|
|
|
BoostTimeout: 5 * time.Minute,
|
2021-05-02 01:22:30 -06:00
|
|
|
BoostWorkers: 1,
|
|
|
|
MaxWorkers: 5,
|
2020-02-02 16:19:58 -07:00
|
|
|
},
|
2021-05-02 01:22:30 -06:00
|
|
|
Workers: 0,
|
2020-02-02 16:19:58 -07:00
|
|
|
Name: config.Name + "-level",
|
2020-01-28 18:01:06 -07:00
|
|
|
},
|
|
|
|
DataDir: config.DataDir,
|
2020-01-07 04:23:09 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
levelQueue, err := NewLevelQueue(handle, levelCfg, exemplar)
|
|
|
|
if err == nil {
|
|
|
|
queue := &PersistableChannelQueue{
|
2020-01-28 18:01:06 -07:00
|
|
|
channelQueue: channelQueue.(*ChannelQueue),
|
2020-01-07 04:23:09 -07:00
|
|
|
delayedStarter: delayedStarter{
|
|
|
|
internal: levelQueue.(*LevelQueue),
|
|
|
|
name: config.Name,
|
|
|
|
},
|
|
|
|
closed: make(chan struct{}),
|
|
|
|
}
|
2020-01-28 18:01:06 -07:00
|
|
|
_ = GetManager().Add(queue, PersistableChannelQueueType, config, exemplar)
|
2020-01-07 04:23:09 -07:00
|
|
|
return queue, nil
|
|
|
|
}
|
|
|
|
if IsErrInvalidConfiguration(err) {
|
|
|
|
// Retrying ain't gonna make this any better...
|
|
|
|
return nil, ErrInvalidConfiguration{cfg: cfg}
|
|
|
|
}
|
|
|
|
|
|
|
|
queue := &PersistableChannelQueue{
|
2020-01-28 18:01:06 -07:00
|
|
|
channelQueue: channelQueue.(*ChannelQueue),
|
2020-01-07 04:23:09 -07:00
|
|
|
delayedStarter: delayedStarter{
|
|
|
|
cfg: levelCfg,
|
|
|
|
underlying: LevelQueueType,
|
|
|
|
timeout: config.Timeout,
|
|
|
|
maxAttempts: config.MaxAttempts,
|
|
|
|
name: config.Name,
|
|
|
|
},
|
|
|
|
closed: make(chan struct{}),
|
|
|
|
}
|
2020-01-28 18:01:06 -07:00
|
|
|
_ = GetManager().Add(queue, PersistableChannelQueueType, config, exemplar)
|
2020-01-07 04:23:09 -07:00
|
|
|
return queue, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Name returns the name of this queue
|
2020-02-02 16:19:58 -07:00
|
|
|
func (q *PersistableChannelQueue) Name() string {
|
|
|
|
return q.delayedStarter.name
|
2020-01-07 04:23:09 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// Push will push the indexer data to queue
|
2020-02-02 16:19:58 -07:00
|
|
|
func (q *PersistableChannelQueue) Push(data Data) error {
|
2020-01-07 04:23:09 -07:00
|
|
|
select {
|
2020-02-02 16:19:58 -07:00
|
|
|
case <-q.closed:
|
|
|
|
return q.internal.Push(data)
|
2020-01-07 04:23:09 -07:00
|
|
|
default:
|
2020-02-02 16:19:58 -07:00
|
|
|
return q.channelQueue.Push(data)
|
2020-01-07 04:23:09 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Run starts to run the queue
|
2021-05-15 08:22:26 -06:00
|
|
|
func (q *PersistableChannelQueue) Run(atShutdown, atTerminate func(func())) {
|
2020-02-02 16:19:58 -07:00
|
|
|
log.Debug("PersistableChannelQueue: %s Starting", q.delayedStarter.name)
|
2021-05-15 08:22:26 -06:00
|
|
|
_ = q.channelQueue.AddWorkers(q.channelQueue.workers, 0)
|
2020-01-28 18:01:06 -07:00
|
|
|
|
2020-02-02 16:19:58 -07:00
|
|
|
q.lock.Lock()
|
|
|
|
if q.internal == nil {
|
|
|
|
err := q.setInternal(atShutdown, q.channelQueue.handle, q.channelQueue.exemplar)
|
|
|
|
q.lock.Unlock()
|
2020-01-07 04:23:09 -07:00
|
|
|
if err != nil {
|
2020-02-02 16:19:58 -07:00
|
|
|
log.Fatal("Unable to create internal queue for %s Error: %v", q.Name(), err)
|
2020-01-07 04:23:09 -07:00
|
|
|
return
|
|
|
|
}
|
|
|
|
} else {
|
2020-02-02 16:19:58 -07:00
|
|
|
q.lock.Unlock()
|
2020-01-07 04:23:09 -07:00
|
|
|
}
|
2021-05-15 08:22:26 -06:00
|
|
|
atShutdown(q.Shutdown)
|
|
|
|
atTerminate(q.Terminate)
|
2020-01-07 04:23:09 -07:00
|
|
|
|
2021-05-15 08:22:26 -06:00
|
|
|
if lq, ok := q.internal.(*LevelQueue); ok && lq.byteFIFO.Len(lq.shutdownCtx) != 0 {
|
|
|
|
// Just run the level queue - we shut it down once it's flushed
|
|
|
|
go q.internal.Run(func(_ func()) {}, func(_ func()) {})
|
|
|
|
go func() {
|
|
|
|
for !q.IsEmpty() {
|
|
|
|
_ = q.internal.Flush(0)
|
|
|
|
select {
|
|
|
|
case <-time.After(100 * time.Millisecond):
|
|
|
|
case <-q.internal.(*LevelQueue).shutdownCtx.Done():
|
|
|
|
log.Warn("LevelQueue: %s shut down before completely flushed", q.internal.(*LevelQueue).Name())
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
log.Debug("LevelQueue: %s flushed so shutting down", q.internal.(*LevelQueue).Name())
|
|
|
|
q.internal.(*LevelQueue).Shutdown()
|
|
|
|
GetManager().Remove(q.internal.(*LevelQueue).qid)
|
|
|
|
}()
|
|
|
|
} else {
|
|
|
|
log.Debug("PersistableChannelQueue: %s Skipping running the empty level queue", q.delayedStarter.name)
|
|
|
|
q.internal.(*LevelQueue).Shutdown()
|
|
|
|
GetManager().Remove(q.internal.(*LevelQueue).qid)
|
|
|
|
}
|
2020-01-07 04:23:09 -07:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2020-01-28 18:01:06 -07:00
|
|
|
// Flush flushes the queue and blocks till the queue is empty
|
2020-02-02 16:19:58 -07:00
|
|
|
func (q *PersistableChannelQueue) Flush(timeout time.Duration) error {
|
2020-01-28 18:01:06 -07:00
|
|
|
var ctx context.Context
|
|
|
|
var cancel context.CancelFunc
|
|
|
|
if timeout > 0 {
|
|
|
|
ctx, cancel = context.WithTimeout(context.Background(), timeout)
|
|
|
|
} else {
|
|
|
|
ctx, cancel = context.WithCancel(context.Background())
|
|
|
|
}
|
|
|
|
defer cancel()
|
2020-02-02 16:19:58 -07:00
|
|
|
return q.FlushWithContext(ctx)
|
2020-01-28 18:01:06 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// FlushWithContext flushes the queue and blocks till the queue is empty
|
2020-02-02 16:19:58 -07:00
|
|
|
func (q *PersistableChannelQueue) FlushWithContext(ctx context.Context) error {
|
2020-01-28 18:01:06 -07:00
|
|
|
errChan := make(chan error, 1)
|
|
|
|
go func() {
|
2020-02-02 16:19:58 -07:00
|
|
|
errChan <- q.channelQueue.FlushWithContext(ctx)
|
2020-01-28 18:01:06 -07:00
|
|
|
}()
|
|
|
|
go func() {
|
2020-02-02 16:19:58 -07:00
|
|
|
q.lock.Lock()
|
|
|
|
if q.internal == nil {
|
|
|
|
q.lock.Unlock()
|
|
|
|
errChan <- fmt.Errorf("not ready to flush internal queue %s yet", q.Name())
|
2020-01-28 18:01:06 -07:00
|
|
|
return
|
|
|
|
}
|
2020-02-02 16:19:58 -07:00
|
|
|
q.lock.Unlock()
|
|
|
|
errChan <- q.internal.FlushWithContext(ctx)
|
2020-01-28 18:01:06 -07:00
|
|
|
}()
|
|
|
|
err1 := <-errChan
|
|
|
|
err2 := <-errChan
|
|
|
|
|
|
|
|
if err1 != nil {
|
|
|
|
return err1
|
|
|
|
}
|
|
|
|
return err2
|
|
|
|
}
|
|
|
|
|
|
|
|
// IsEmpty checks if a queue is empty
|
2020-02-02 16:19:58 -07:00
|
|
|
func (q *PersistableChannelQueue) IsEmpty() bool {
|
|
|
|
if !q.channelQueue.IsEmpty() {
|
2020-01-28 18:01:06 -07:00
|
|
|
return false
|
|
|
|
}
|
2020-02-02 16:19:58 -07:00
|
|
|
q.lock.Lock()
|
|
|
|
defer q.lock.Unlock()
|
|
|
|
if q.internal == nil {
|
2020-01-28 18:01:06 -07:00
|
|
|
return false
|
|
|
|
}
|
2020-02-02 16:19:58 -07:00
|
|
|
return q.internal.IsEmpty()
|
2020-01-28 18:01:06 -07:00
|
|
|
}
|
|
|
|
|
2020-01-07 04:23:09 -07:00
|
|
|
// Shutdown processing this queue
|
2020-02-02 16:19:58 -07:00
|
|
|
func (q *PersistableChannelQueue) Shutdown() {
|
|
|
|
log.Trace("PersistableChannelQueue: %s Shutting down", q.delayedStarter.name)
|
|
|
|
q.lock.Lock()
|
2021-05-15 08:22:26 -06:00
|
|
|
|
2020-01-07 04:23:09 -07:00
|
|
|
select {
|
2020-02-02 16:19:58 -07:00
|
|
|
case <-q.closed:
|
2021-05-15 08:22:26 -06:00
|
|
|
q.lock.Unlock()
|
|
|
|
return
|
2020-01-07 04:23:09 -07:00
|
|
|
default:
|
|
|
|
}
|
2021-05-15 08:22:26 -06:00
|
|
|
q.channelQueue.Shutdown()
|
|
|
|
if q.internal != nil {
|
|
|
|
q.internal.(*LevelQueue).Shutdown()
|
|
|
|
}
|
|
|
|
close(q.closed)
|
|
|
|
q.lock.Unlock()
|
|
|
|
|
|
|
|
log.Trace("PersistableChannelQueue: %s Cancelling pools", q.delayedStarter.name)
|
|
|
|
q.channelQueue.baseCtxCancel()
|
|
|
|
q.internal.(*LevelQueue).baseCtxCancel()
|
|
|
|
log.Trace("PersistableChannelQueue: %s Waiting til done", q.delayedStarter.name)
|
|
|
|
q.channelQueue.Wait()
|
|
|
|
q.internal.(*LevelQueue).Wait()
|
|
|
|
// Redirect all remaining data in the chan to the internal channel
|
|
|
|
go func() {
|
|
|
|
log.Trace("PersistableChannelQueue: %s Redirecting remaining data", q.delayedStarter.name)
|
|
|
|
for data := range q.channelQueue.dataChan {
|
|
|
|
_ = q.internal.Push(data)
|
|
|
|
atomic.AddInt64(&q.channelQueue.numInQueue, -1)
|
|
|
|
}
|
|
|
|
log.Trace("PersistableChannelQueue: %s Done Redirecting remaining data", q.delayedStarter.name)
|
|
|
|
}()
|
|
|
|
|
|
|
|
log.Debug("PersistableChannelQueue: %s Shutdown", q.delayedStarter.name)
|
2020-01-07 04:23:09 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// Terminate this queue and close the queue
|
2020-02-02 16:19:58 -07:00
|
|
|
func (q *PersistableChannelQueue) Terminate() {
|
|
|
|
log.Trace("PersistableChannelQueue: %s Terminating", q.delayedStarter.name)
|
|
|
|
q.Shutdown()
|
|
|
|
q.lock.Lock()
|
|
|
|
defer q.lock.Unlock()
|
2021-05-15 08:22:26 -06:00
|
|
|
q.channelQueue.Terminate()
|
2020-02-02 16:19:58 -07:00
|
|
|
if q.internal != nil {
|
|
|
|
q.internal.(*LevelQueue).Terminate()
|
2020-01-07 04:23:09 -07:00
|
|
|
}
|
2020-02-02 16:19:58 -07:00
|
|
|
log.Debug("PersistableChannelQueue: %s Terminated", q.delayedStarter.name)
|
2020-01-07 04:23:09 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
queuesMap[PersistableChannelQueueType] = NewPersistableChannelQueue
|
|
|
|
}
|