Skip to content

Commit d01d192

Browse files
mitdesaipbacsko
authored andcommitted
[YUNIKORN-3084] Fix Inconsistency in Allocation Removal from sortedRequests (#1022)
Closes: #1022 Signed-off-by: Peter Bacsko <[email protected]> (cherry picked from commit a5b865c)
1 parent 2d0abda commit d01d192

File tree

2 files changed

+64
-6
lines changed

2 files changed

+64
-6
lines changed

pkg/scheduler/objects/sorted_asks.go

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -50,13 +50,12 @@ func (s *sortedRequests) insertAt(index int, ask *Allocation) {
5050
}
5151

5252
func (s *sortedRequests) remove(ask *Allocation) {
53-
idx := sort.Search(len(*s), func(i int) bool {
54-
return (*s)[i].LessThan(ask)
55-
})
56-
if idx == len(*s) || (*s)[idx].allocationKey != ask.allocationKey {
57-
return
53+
for i, a := range *s {
54+
if a.allocationKey == ask.allocationKey {
55+
s.removeAt(i)
56+
return
57+
}
5858
}
59-
s.removeAt(idx)
6059
}
6160

6261
func (s *sortedRequests) removeAt(index int) {

pkg/scheduler/objects/sorted_asks_test.go

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,65 @@ func TestInsertRemove(t *testing.T) {
9898
assert.Equal(t, 99, len(sorted))
9999
}
100100

101+
func TestRemoveWithSamePriorityAndTime(t *testing.T) {
102+
// Create a sorted requests list
103+
sorted := sortedRequests{}
104+
105+
// Create allocations with same priority and creation time
106+
// but different allocation keys
107+
baseTime := time.Now()
108+
alloc1 := &Allocation{
109+
createTime: baseTime,
110+
priority: 10,
111+
allocationKey: "alloc-1",
112+
}
113+
alloc2 := &Allocation{
114+
createTime: baseTime,
115+
priority: 10,
116+
allocationKey: "alloc-2",
117+
}
118+
alloc3 := &Allocation{
119+
createTime: baseTime,
120+
priority: 10,
121+
allocationKey: "alloc-3",
122+
}
123+
124+
// Insert the allocations
125+
sorted.insert(alloc1)
126+
sorted.insert(alloc2)
127+
sorted.insert(alloc3)
128+
129+
// Verify all allocations are in the list
130+
assert.Equal(t, 3, len(sorted))
131+
assert.Assert(t, askPresent(alloc1, sorted), "alloc1 should be present")
132+
assert.Assert(t, askPresent(alloc2, sorted), "alloc2 should be present")
133+
assert.Assert(t, askPresent(alloc3, sorted), "alloc3 should be present")
134+
135+
// Try to remove alloc2
136+
sorted.remove(alloc2)
137+
138+
// Verify alloc2 is removed but alloc1 and alloc3 are still there
139+
assert.Equal(t, 2, len(sorted))
140+
assert.Assert(t, askPresent(alloc1, sorted), "alloc1 should still be present")
141+
assert.Assert(t, !askPresent(alloc2, sorted), "alloc2 should be removed")
142+
assert.Assert(t, askPresent(alloc3, sorted), "alloc3 should still be present")
143+
144+
// Try to remove alloc1
145+
sorted.remove(alloc1)
146+
147+
// Verify alloc1 is removed and only alloc3 remains
148+
assert.Equal(t, 1, len(sorted))
149+
assert.Assert(t, !askPresent(alloc1, sorted), "alloc1 should be removed")
150+
assert.Assert(t, askPresent(alloc3, sorted), "alloc3 should still be present")
151+
152+
// Try to remove alloc3
153+
sorted.remove(alloc3)
154+
155+
// Verify all allocations are removed
156+
assert.Equal(t, 0, len(sorted))
157+
assert.Assert(t, !askPresent(alloc3, sorted), "alloc3 should be removed")
158+
}
159+
101160
func askPresent(ask *Allocation, asks []*Allocation) bool {
102161
for _, a := range asks {
103162
if a.allocationKey == ask.allocationKey {

0 commit comments

Comments
 (0)