forked from Shiloh/githaven
62eb1b0f25
* Queue: Add generic graceful queues with settings * Queue & Setting: Add worker pool implementation * Queue: Add worker settings * Queue: Make resizing worker pools * Queue: Add name variable to queues * Queue: Add monitoring * Queue: Improve logging * Issues: Gracefulise the issues indexer Remove the old now unused specific queues * Task: Move to generic queue and gracefulise * Issues: Standardise the issues indexer queue settings * Fix test * Queue: Allow Redis to connect to unix * Prevent deadlock during early shutdown of issue indexer * Add MaxWorker settings to queues * Merge branch 'master' into graceful-queues * Update modules/indexer/issues/indexer.go Co-Authored-By: guillep2k <18600385+guillep2k@users.noreply.github.com> * Update modules/indexer/issues/indexer.go Co-Authored-By: guillep2k <18600385+guillep2k@users.noreply.github.com> * Update modules/queue/queue_channel.go Co-Authored-By: guillep2k <18600385+guillep2k@users.noreply.github.com> * Update modules/queue/queue_disk.go * Update modules/queue/queue_disk_channel.go Co-Authored-By: guillep2k <18600385+guillep2k@users.noreply.github.com> * Rename queue.Description to queue.ManagedQueue as per @guillep2k * Cancel pool workers when removed * Remove dependency on queue from setting * Update modules/queue/queue_redis.go Co-Authored-By: guillep2k <18600385+guillep2k@users.noreply.github.com> * As per @guillep2k add mutex locks on shutdown/terminate * move unlocking out of setInternal * Add warning if number of workers < 0 * Small changes as per @guillep2k * No redis host specified not found * Clean up documentation for queues * Update docs/content/doc/advanced/config-cheat-sheet.en-us.md * Update modules/indexer/issues/indexer_test.go * Ensure that persistable channel queue is added to manager * Rename QUEUE_NAME REDIS_QUEUE_NAME * Revert "Rename QUEUE_NAME REDIS_QUEUE_NAME" This reverts commit 1f83b4fc9b9dabda186257b38c265fe7012f90df. Co-authored-by: guillep2k <18600385+guillep2k@users.noreply.github.com> Co-authored-by: Lauris BH <lauris@nix.lv> Co-authored-by: techknowlogick <matti@mdranta.net> Co-authored-by: Lunny Xiao <xiaolunwen@gmail.com>
194 lines
5.7 KiB
Go
194 lines
5.7 KiB
Go
// 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"
|
|
"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
|
|
type PersistableChannelQueue struct {
|
|
*ChannelQueue
|
|
delayedStarter
|
|
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{
|
|
QueueLength: config.QueueLength,
|
|
BatchLength: config.BatchLength,
|
|
Workers: config.Workers,
|
|
MaxWorkers: config.MaxWorkers,
|
|
BlockTimeout: config.BlockTimeout,
|
|
BoostTimeout: config.BoostTimeout,
|
|
BoostWorkers: config.BoostWorkers,
|
|
Name: config.Name + "-channel",
|
|
}, exemplar)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
// the level backend only needs temporary workers to catch up with the previously dropped work
|
|
levelCfg := LevelQueueConfiguration{
|
|
DataDir: config.DataDir,
|
|
QueueLength: config.QueueLength,
|
|
BatchLength: config.BatchLength,
|
|
Workers: 1,
|
|
MaxWorkers: 6,
|
|
BlockTimeout: 1 * time.Second,
|
|
BoostTimeout: 5 * time.Minute,
|
|
BoostWorkers: 5,
|
|
Name: config.Name + "-level",
|
|
}
|
|
|
|
levelQueue, err := NewLevelQueue(handle, levelCfg, exemplar)
|
|
if err == nil {
|
|
queue := &PersistableChannelQueue{
|
|
ChannelQueue: channelQueue.(*ChannelQueue),
|
|
delayedStarter: delayedStarter{
|
|
internal: levelQueue.(*LevelQueue),
|
|
name: config.Name,
|
|
},
|
|
closed: make(chan struct{}),
|
|
}
|
|
_ = GetManager().Add(queue, PersistableChannelQueueType, config, exemplar, nil)
|
|
return queue, nil
|
|
}
|
|
if IsErrInvalidConfiguration(err) {
|
|
// Retrying ain't gonna make this any better...
|
|
return nil, ErrInvalidConfiguration{cfg: cfg}
|
|
}
|
|
|
|
queue := &PersistableChannelQueue{
|
|
ChannelQueue: channelQueue.(*ChannelQueue),
|
|
delayedStarter: delayedStarter{
|
|
cfg: levelCfg,
|
|
underlying: LevelQueueType,
|
|
timeout: config.Timeout,
|
|
maxAttempts: config.MaxAttempts,
|
|
name: config.Name,
|
|
},
|
|
closed: make(chan struct{}),
|
|
}
|
|
_ = GetManager().Add(queue, PersistableChannelQueueType, config, exemplar, nil)
|
|
return queue, nil
|
|
}
|
|
|
|
// Name returns the name of this queue
|
|
func (p *PersistableChannelQueue) Name() string {
|
|
return p.delayedStarter.name
|
|
}
|
|
|
|
// Push will push the indexer data to queue
|
|
func (p *PersistableChannelQueue) Push(data Data) error {
|
|
select {
|
|
case <-p.closed:
|
|
return p.internal.Push(data)
|
|
default:
|
|
return p.ChannelQueue.Push(data)
|
|
}
|
|
}
|
|
|
|
// Run starts to run the queue
|
|
func (p *PersistableChannelQueue) Run(atShutdown, atTerminate func(context.Context, func())) {
|
|
p.lock.Lock()
|
|
if p.internal == nil {
|
|
err := p.setInternal(atShutdown, p.ChannelQueue.pool.handle, p.exemplar)
|
|
p.lock.Unlock()
|
|
if err != nil {
|
|
log.Fatal("Unable to create internal queue for %s Error: %v", p.Name(), err)
|
|
return
|
|
}
|
|
} else {
|
|
p.lock.Unlock()
|
|
}
|
|
atShutdown(context.Background(), p.Shutdown)
|
|
atTerminate(context.Background(), p.Terminate)
|
|
|
|
// Just run the level queue - we shut it down later
|
|
go p.internal.Run(func(_ context.Context, _ func()) {}, func(_ context.Context, _ func()) {})
|
|
|
|
go func() {
|
|
_ = p.ChannelQueue.pool.AddWorkers(p.workers, 0)
|
|
}()
|
|
|
|
log.Trace("PersistableChannelQueue: %s Waiting til closed", p.delayedStarter.name)
|
|
<-p.closed
|
|
log.Trace("PersistableChannelQueue: %s Cancelling pools", p.delayedStarter.name)
|
|
p.ChannelQueue.pool.cancel()
|
|
p.internal.(*LevelQueue).pool.cancel()
|
|
log.Trace("PersistableChannelQueue: %s Waiting til done", p.delayedStarter.name)
|
|
p.ChannelQueue.pool.Wait()
|
|
p.internal.(*LevelQueue).pool.Wait()
|
|
// Redirect all remaining data in the chan to the internal channel
|
|
go func() {
|
|
log.Trace("PersistableChannelQueue: %s Redirecting remaining data", p.delayedStarter.name)
|
|
for data := range p.ChannelQueue.pool.dataChan {
|
|
_ = p.internal.Push(data)
|
|
}
|
|
log.Trace("PersistableChannelQueue: %s Done Redirecting remaining data", p.delayedStarter.name)
|
|
}()
|
|
log.Trace("PersistableChannelQueue: %s Done main loop", p.delayedStarter.name)
|
|
}
|
|
|
|
// Shutdown processing this queue
|
|
func (p *PersistableChannelQueue) Shutdown() {
|
|
log.Trace("PersistableChannelQueue: %s Shutdown", p.delayedStarter.name)
|
|
select {
|
|
case <-p.closed:
|
|
default:
|
|
p.lock.Lock()
|
|
defer p.lock.Unlock()
|
|
if p.internal != nil {
|
|
p.internal.(*LevelQueue).Shutdown()
|
|
}
|
|
close(p.closed)
|
|
}
|
|
}
|
|
|
|
// Terminate this queue and close the queue
|
|
func (p *PersistableChannelQueue) Terminate() {
|
|
log.Trace("PersistableChannelQueue: %s Terminating", p.delayedStarter.name)
|
|
p.Shutdown()
|
|
p.lock.Lock()
|
|
defer p.lock.Unlock()
|
|
if p.internal != nil {
|
|
p.internal.(*LevelQueue).Terminate()
|
|
}
|
|
}
|
|
|
|
func init() {
|
|
queuesMap[PersistableChannelQueueType] = NewPersistableChannelQueue
|
|
}
|