-
Notifications
You must be signed in to change notification settings - Fork 0
/
scheduler.go
306 lines (267 loc) · 7.58 KB
/
scheduler.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
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
// Copyright 2016 The Cockroach Authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
// implied. See the License for the specific language governing
// permissions and limitations under the License.
package storage
import (
"container/list"
"fmt"
"sync"
"golang.org/x/net/context"
"github.com/cockroachdb/cockroach/pkg/roachpb"
"github.com/cockroachdb/cockroach/pkg/util/log"
"github.com/cockroachdb/cockroach/pkg/util/stop"
"github.com/cockroachdb/cockroach/pkg/util/syncutil"
)
const rangeIDChunkSize = 1000
type rangeIDChunk struct {
// Valid contents are buf[rd:wr], read at buf[rd], write at buf[wr].
buf [rangeIDChunkSize]roachpb.RangeID
rd, wr int
}
func (c *rangeIDChunk) PushBack(id roachpb.RangeID) bool {
if c.WriteCap() == 0 {
return false
}
c.buf[c.wr] = id
c.wr++
return true
}
func (c *rangeIDChunk) PopFront() (roachpb.RangeID, bool) {
if c.Len() == 0 {
return 0, false
}
id := c.buf[c.rd]
c.rd++
return id, true
}
func (c *rangeIDChunk) WriteCap() int {
return len(c.buf) - c.wr
}
func (c *rangeIDChunk) Len() int {
return c.wr - c.rd
}
// rangeIDQueue is a chunked queue of range IDs. Instead of a separate list
// element for every range ID, it uses a rangeIDChunk to hold many range IDs,
// amortizing the allocation/GC cost. Using a chunk queue avoids any copying
// that would occur if a slice were used (the copying would occur on slice
// reallocation).
type rangeIDQueue struct {
chunks list.List
len int
}
func (q *rangeIDQueue) PushBack(id roachpb.RangeID) {
if q.chunks.Len() == 0 || q.back().WriteCap() == 0 {
q.chunks.PushBack(&rangeIDChunk{})
}
q.len++
if !q.back().PushBack(id) {
panic(fmt.Sprintf(
"unable to push rangeID to chunk: len=%d, cap=%d",
q.back().Len(), q.back().WriteCap()))
}
}
func (q *rangeIDQueue) PopFront() (roachpb.RangeID, bool) {
if q.len == 0 {
return 0, false
}
frontElem := q.chunks.Front()
front := frontElem.Value.(*rangeIDChunk)
id, ok := front.PopFront()
if !ok {
panic("encountered empty chunk")
}
q.len--
if front.Len() == 0 && front.WriteCap() == 0 {
q.chunks.Remove(frontElem)
}
return id, true
}
func (q *rangeIDQueue) Len() int {
return q.len
}
func (q *rangeIDQueue) back() *rangeIDChunk {
return q.chunks.Back().Value.(*rangeIDChunk)
}
type raftProcessor interface {
processReady(rangeID roachpb.RangeID)
processRequestQueue(rangeID roachpb.RangeID)
// Process a raft tick for the specified range. Return true if the range
// should be queued for ready processing.
processTick(rangeID roachpb.RangeID) bool
}
type raftScheduleState int
const (
stateQueued raftScheduleState = 1 << iota
stateRaftReady
stateRaftRequest
stateRaftTick
)
type raftScheduler struct {
processor raftProcessor
numWorkers int
mu struct {
syncutil.Mutex
cond *sync.Cond
queue rangeIDQueue
state map[roachpb.RangeID]raftScheduleState
stopped bool
}
done sync.WaitGroup
}
func newRaftScheduler(
ambient log.AmbientContext, metrics *StoreMetrics, processor raftProcessor, numWorkers int,
) *raftScheduler {
s := &raftScheduler{
processor: processor,
numWorkers: numWorkers,
}
s.mu.cond = sync.NewCond(&s.mu.Mutex)
s.mu.state = make(map[roachpb.RangeID]raftScheduleState)
return s
}
func (s *raftScheduler) Start(stopper *stop.Stopper) {
ctx := context.TODO()
stopper.RunWorker(ctx, func(context.Context) {
<-stopper.ShouldStop()
s.mu.Lock()
s.mu.stopped = true
s.mu.Unlock()
s.mu.cond.Broadcast()
})
s.done.Add(s.numWorkers)
for i := 0; i < s.numWorkers; i++ {
stopper.RunWorker(ctx, func(context.Context) {
s.worker(stopper)
})
}
}
func (s *raftScheduler) Wait(context.Context) {
s.done.Wait()
}
func (s *raftScheduler) worker(stopper *stop.Stopper) {
defer s.done.Done()
// We use a sync.Cond for worker notification instead of a buffered
// channel. Buffered channels have internal overhead for maintaining the
// buffer even when the elements are empty. And the buffer isn't necessary as
// the raftScheduler work is already buffered on the internal queue. Lastly,
// signaling a sync.Cond is significantly faster than selecting and sending
// on a buffered channel.
s.mu.Lock()
for {
var id roachpb.RangeID
for {
if s.mu.stopped {
s.mu.Unlock()
return
}
var ok bool
if id, ok = s.mu.queue.PopFront(); ok {
break
}
s.mu.cond.Wait()
}
// Grab and clear the existing state for the range ID. Note that we leave
// the range ID marked as "queued" so that a concurrent Enqueue* will not
// queue the range ID again.
state := s.mu.state[id]
s.mu.state[id] = stateQueued
s.mu.Unlock()
if state&stateRaftTick != 0 {
// processRaftTick returns true if the range should perform ready
// processing. Do not reorder this below the call to processReady.
if s.processor.processTick(id) {
state |= stateRaftReady
}
}
if state&stateRaftReady != 0 {
s.processor.processReady(id)
}
// Process requests last. This avoids a scenario where a tick and a
// "quiesce" message are processed in the same iteration and intervening
// raft ready processing unquiesced the replica. Note that request
// processing could also occur first, it just shouldn't occur in between
// ticking and ready processing. It is possible for a tick to be enqueued
// concurrently with the quiescing in which case the replica will
// unquiesce when the tick is processed, but we'll wake the leader in
// that case.
if state&stateRaftRequest != 0 {
s.processor.processRequestQueue(id)
}
s.mu.Lock()
state = s.mu.state[id]
if state == stateQueued {
// No further processing required by the range ID, clear it from the
// state map.
delete(s.mu.state, id)
} else {
// There was a concurrent call to one of the Enqueue* methods. Queue the
// range ID for further processing.
s.mu.queue.PushBack(id)
s.mu.cond.Signal()
}
}
}
func (s *raftScheduler) enqueue1Locked(addState raftScheduleState, id roachpb.RangeID) int {
prevState := s.mu.state[id]
if prevState&addState == addState {
return 0
}
var queued int
newState := prevState | addState
if newState&stateQueued == 0 {
newState |= stateQueued
queued++
s.mu.queue.PushBack(id)
}
s.mu.state[id] = newState
return queued
}
func (s *raftScheduler) enqueue1(addState raftScheduleState, id roachpb.RangeID) int {
s.mu.Lock()
count := s.enqueue1Locked(addState, id)
s.mu.Unlock()
return count
}
func (s *raftScheduler) enqueueN(addState raftScheduleState, ids ...roachpb.RangeID) int {
// Enqueue the ids in chunks to avoid hold raftScheduler.mu for too long.
const enqueueChunkSize = 128
var count int
s.mu.Lock()
for i, id := range ids {
count += s.enqueue1Locked(addState, id)
if (i+1)%enqueueChunkSize == 0 {
s.mu.Unlock()
s.mu.Lock()
}
}
s.mu.Unlock()
return count
}
func (s *raftScheduler) signal(count int) {
if count >= s.numWorkers {
s.mu.cond.Broadcast()
} else {
for i := 0; i < count; i++ {
s.mu.cond.Signal()
}
}
}
func (s *raftScheduler) EnqueueRaftReady(id roachpb.RangeID) {
s.signal(s.enqueue1(stateRaftReady, id))
}
func (s *raftScheduler) EnqueueRaftRequest(id roachpb.RangeID) {
s.signal(s.enqueue1(stateRaftRequest, id))
}
func (s *raftScheduler) EnqueueRaftTick(ids ...roachpb.RangeID) {
s.signal(s.enqueueN(stateRaftTick, ids...))
}