Skip to content

Commit 11dc2dd

Browse files
committed
fix priority queue ordering when item priority changes
Signed-off-by: zach593 <[email protected]>
1 parent adb6465 commit 11dc2dd

File tree

2 files changed

+23
-1
lines changed

2 files changed

+23
-1
lines changed

pkg/controller/priorityqueue/priorityqueue.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -161,12 +161,12 @@ func (w *priorityqueue[T]) AddWithOpts(o AddOpts, items ...T) {
161161
Priority: ptr.Deref(o.Priority, 0),
162162
ReadyAt: readyAt,
163163
}
164+
w.addedCounter++
164165
w.items[key] = item
165166
w.queue.ReplaceOrInsert(item)
166167
if item.ReadyAt == nil {
167168
w.metrics.add(key, item.Priority)
168169
}
169-
w.addedCounter++
170170
continue
171171
}
172172

@@ -179,6 +179,8 @@ func (w *priorityqueue[T]) AddWithOpts(o AddOpts, items ...T) {
179179
w.metrics.updateDepthWithPriorityMetric(item.Priority, newPriority)
180180
}
181181
item.Priority = newPriority
182+
item.AddedCounter = w.addedCounter
183+
w.addedCounter++
182184
}
183185

184186
if item.ReadyAt != nil && (readyAt == nil || readyAt.Before(*item.ReadyAt)) {

pkg/controller/priorityqueue/priorityqueue_test.go

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -320,6 +320,26 @@ var _ = Describe("Controllerworkqueue", func() {
320320
Expect(depth).To(Equal(0))
321321
}
322322
})
323+
324+
It("follows FIFO order in the new priority queue when item priority changes", func() {
325+
q, _ := newQueue()
326+
defer q.ShutDown()
327+
328+
q.AddWithOpts(AddOpts{Priority: ptr.To(0)}, "foo")
329+
q.AddWithOpts(AddOpts{Priority: ptr.To(1)}, "bar")
330+
q.AddWithOpts(AddOpts{Priority: ptr.To(1)}, "foo")
331+
Expect(q.Len()).To(Equal(2))
332+
333+
item, priority, _ := q.GetWithPriority()
334+
Expect(item).To(Equal("bar"))
335+
Expect(priority).To(Equal(1))
336+
Expect(q.Len()).To(Equal(1))
337+
338+
item, priority, _ = q.GetWithPriority()
339+
Expect(item).To(Equal("foo"))
340+
Expect(priority).To(Equal(1))
341+
Expect(q.Len()).To(Equal(0))
342+
})
323343
})
324344

325345
func BenchmarkAddGetDone(b *testing.B) {

0 commit comments

Comments
 (0)