summaryrefslogtreecommitdiffstats
path: root/modules/queue/queue_disk_channel.go
blob: 895c8ce918f3095855f901fce2691981b667794e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
// 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
}