-
Notifications
You must be signed in to change notification settings - Fork 679
/
cyclemanager.go
244 lines (207 loc) · 5.03 KB
/
cyclemanager.go
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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
// _ _
// __ _____ __ ___ ___ __ _| |_ ___
// \ \ /\ / / _ \/ _` \ \ / / |/ _` | __/ _ \
// \ V V / __/ (_| |\ V /| | (_| | || __/
// \_/\_/ \___|\__,_| \_/ |_|\__,_|\__\___|
//
// Copyright © 2016 - 2024 Weaviate B.V. All rights reserved.
//
// CONTACT: hello@weaviate.io
//
package cyclemanager
import (
"context"
"fmt"
"runtime"
"sync"
"github.com/sirupsen/logrus"
enterrors "github.com/weaviate/weaviate/entities/errors"
)
var _NUMCPU = runtime.NumCPU()
type (
// indicates whether cyclemanager's stop was requested to allow safely
// abort execution of CycleCallback and stop cyclemanager earlier
ShouldAbortCallback func() bool
// return value indicates whether actual work was done in the cycle
CycleCallback func(shouldAbort ShouldAbortCallback) bool
)
type CycleManager interface {
Start()
Stop(ctx context.Context) chan bool
StopAndWait(ctx context.Context) error
Running() bool
}
type cycleManager struct {
sync.RWMutex
cycleCallback CycleCallback
cycleTicker CycleTicker
running bool
stopSignal chan struct{}
stopContexts []context.Context
stopResults []chan bool
logger logrus.FieldLogger
}
func NewManager(cycleTicker CycleTicker, cycleCallback CycleCallback, logger logrus.FieldLogger) CycleManager {
return &cycleManager{
cycleCallback: cycleCallback,
cycleTicker: cycleTicker,
running: false,
stopSignal: make(chan struct{}, 1),
logger: logger,
}
}
// Starts instance, does not block
// Does nothing if instance is already started
func (c *cycleManager) Start() {
c.Lock()
defer c.Unlock()
if c.running {
return
}
enterrors.GoWrapper(func() {
c.cycleTicker.Start()
defer c.cycleTicker.Stop()
for {
if c.isStopRequested() {
c.Lock()
if c.shouldStop() {
c.handleStopRequest(true)
c.Unlock()
break
}
c.handleStopRequest(false)
c.Unlock()
continue
}
c.cycleTicker.CycleExecuted(c.cycleCallback(c.shouldAbortCycleCallback))
}
}, c.logger)
c.running = true
}
// Stops running instance, does not block
// Returns channel with final stop result - true / false
//
// If given context is cancelled before it is handled by stop logic, instance is not stopped
// If called multiple times, all contexts have to be cancelled to cancel stop
// (any valid will result in stopping instance)
// stopResult is the same (consistent) for multiple calls
func (c *cycleManager) Stop(ctx context.Context) (stopResult chan bool) {
c.Lock()
defer c.Unlock()
stopResult = make(chan bool, 1)
if !c.running {
stopResult <- true
close(stopResult)
return stopResult
}
if len(c.stopContexts) == 0 {
defer func() {
c.stopSignal <- struct{}{}
}()
}
c.stopContexts = append(c.stopContexts, ctx)
c.stopResults = append(c.stopResults, stopResult)
return stopResult
}
// Stops running instance, waits for stop to occur or context to expire (which comes first)
// Returns error if instance was not stopped
func (c *cycleManager) StopAndWait(ctx context.Context) error {
// if both channels are ready, chan is selected randomly, therefore regardless of
// channel selected first, second one is also checked
stop := c.Stop(ctx)
done := ctx.Done()
select {
case <-done:
select {
case stopped := <-stop:
if !stopped {
return ctx.Err()
}
default:
return ctx.Err()
}
case stopped := <-stop:
if !stopped {
if ctx.Err() != nil {
return ctx.Err()
}
return fmt.Errorf("failed to stop cycle")
}
}
return nil
}
func (c *cycleManager) Running() bool {
c.RLock()
defer c.RUnlock()
return c.running
}
func (c *cycleManager) shouldStop() bool {
for _, ctx := range c.stopContexts {
if ctx.Err() == nil {
return true
}
}
return false
}
func (c *cycleManager) shouldAbortCycleCallback() bool {
c.RLock()
defer c.RUnlock()
return c.shouldStop()
}
func (c *cycleManager) isStopRequested() bool {
select {
case <-c.stopSignal:
case <-c.cycleTicker.C():
// as stop chan has higher priority,
// it is checked again in case of ticker was selected over stop if both were ready
select {
case <-c.stopSignal:
default:
return false
}
}
return true
}
func (c *cycleManager) handleStopRequest(stopped bool) {
for _, stopResult := range c.stopResults {
stopResult <- stopped
close(stopResult)
}
c.running = !stopped
c.stopContexts = nil
c.stopResults = nil
}
func NewManagerNoop() CycleManager {
return &cycleManagerNoop{running: false}
}
type cycleManagerNoop struct {
running bool
}
func (c *cycleManagerNoop) Start() {
c.running = true
}
func (c *cycleManagerNoop) Stop(ctx context.Context) chan bool {
if !c.running {
return c.closedChan(true)
}
if ctx.Err() != nil {
return c.closedChan(false)
}
c.running = false
return c.closedChan(true)
}
func (c *cycleManagerNoop) StopAndWait(ctx context.Context) error {
if <-c.Stop(ctx) {
return nil
}
return ctx.Err()
}
func (c *cycleManagerNoop) Running() bool {
return c.running
}
func (c *cycleManagerNoop) closedChan(val bool) chan bool {
ch := make(chan bool, 1)
ch <- val
close(ch)
return ch
}