-
Notifications
You must be signed in to change notification settings - Fork 29
/
Copy pathinflight_queue.go
188 lines (155 loc) · 3.86 KB
/
inflight_queue.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
package common
import (
"context"
"sync"
)
////////////////////////////////////////////////////////////////////////////////
type Milestone struct {
Value uint32
ProcessedValueCount uint32
}
////////////////////////////////////////////////////////////////////////////////
type item struct {
value uint32
inflight bool
}
type InflightQueue struct {
milestone Milestone
milestoneHint uint32
processedValues <-chan uint32
holeValues ChannelWithCancellation
inflightLimit int
items []item
inflightCount int
lastHoleValue *uint32
mutex sync.RWMutex
possibleToAdd Cond
}
// Not thread-safe.
func (q *InflightQueue) Add(ctx context.Context, value uint32) (bool, error) {
// Wait until hole values are ahead of inflight values.
if !q.holeValues.Empty() {
more := true
for more {
if q.lastHoleValue != nil && *q.lastHoleValue >= value {
if *q.lastHoleValue == value {
return false, nil
}
break
}
q.mutex.Lock()
if len(q.items) == 0 {
// NBS-4141: it is safe to update milestone.
q.milestone.Value = value
}
q.mutex.Unlock()
var holeValue uint32
var err error
holeValue, more, err = q.holeValues.Receive(ctx)
if err != nil {
return false, err
}
if more {
q.lastHoleValue = &holeValue
}
}
}
q.mutex.Lock()
defer q.mutex.Unlock()
for q.inflightCount >= q.inflightLimit {
err := q.possibleToAdd.Wait(ctx)
if err != nil {
return false, err
}
}
q.items = append(
q.items,
item{value: value, inflight: true},
)
q.inflightCount += 1
return true, nil
}
func (q *InflightQueue) Milestone() Milestone {
q.mutex.RLock()
defer q.mutex.RUnlock()
return q.milestone
}
// If this method is called with some value v,
// values less then v should not be added to the inflight queue anymore.
func (q *InflightQueue) UpdateMilestoneHint(value uint32) {
q.mutex.Lock()
defer q.mutex.Unlock()
q.milestoneHint = value
if len(q.items) == 0 && q.milestoneHint > q.milestone.Value {
// It is safe to update milestone here because the queue is empty.
q.milestone.Value = q.milestoneHint
}
}
func (q *InflightQueue) Close() {
q.holeValues.Cancel()
}
////////////////////////////////////////////////////////////////////////////////
func NewInflightQueue(
milestone Milestone,
processedValues <-chan uint32,
holeValues ChannelWithCancellation,
inflightLimit int,
) *InflightQueue {
q := &InflightQueue{
milestone: milestone,
processedValues: processedValues,
holeValues: holeValues,
inflightLimit: inflightLimit,
items: make([]item, 0),
}
q.possibleToAdd = NewCond(&q.mutex)
go func() {
q.drainLoop()
}()
return q
}
////////////////////////////////////////////////////////////////////////////////
func (q *InflightQueue) drainLoop() {
for value := range q.processedValues {
q.valueProcessed(value)
}
}
func (q *InflightQueue) valueProcessed(value uint32) {
q.mutex.Lock()
defer q.mutex.Unlock()
for i := 0; i < len(q.items); i++ {
if q.items[i].value == value {
q.items[i].inflight = false
q.inflightCount -= 1
q.possibleToAdd.Signal()
break
}
}
toRemoveCount := 0
for _, item := range q.items {
if item.inflight {
break
} else {
toRemoveCount += 1
}
}
if toRemoveCount == 0 {
return
}
newMilestoneValue := uint32(0)
if toRemoveCount >= len(q.items) {
lastItemValue := q.items[len(q.items)-1].value
newMilestoneValue = lastItemValue + 1
if q.milestoneHint > newMilestoneValue {
// It is safe to update milestone here because
// all items in the queue are already processed.
newMilestoneValue = q.milestoneHint
}
} else {
newMilestoneValue = q.items[toRemoveCount].value
}
q.milestone.Value = newMilestoneValue
q.milestone.ProcessedValueCount += uint32(toRemoveCount)
// Remove processed (not in-flight) items from the head.
q.items = q.items[toRemoveCount:]
}