Skip to content

Commit a351764

Browse files
Michaelchenyulin0719
authored andcommitted
[YUNIKORN-2997] add unit test for tryPlaceholderAllocate (apache#1004)
Closes: apache#1004 Signed-off-by: Yu-Lin Chen <[email protected]>
1 parent 2113cd7 commit a351764

File tree

2 files changed

+174
-13
lines changed

2 files changed

+174
-13
lines changed

pkg/scheduler/objects/application_test.go

Lines changed: 171 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -501,10 +501,6 @@ func TestAddAllocAsk(t *testing.T) {
501501
assert.Assert(t, app.IsAccepted(), "Application should have stayed in accepted state")
502502

503503
// test PlaceholderData
504-
const (
505-
tg1 = "tg-1"
506-
tg2 = "tg-2"
507-
)
508504
ask = newAllocationAskTG(aKey, appID1, tg1, res)
509505
err = app.AddAllocationAsk(ask)
510506
assert.NilError(t, err, "ask should have been updated on app")
@@ -859,7 +855,7 @@ func TestStateChangeOnPlaceholderAdd(t *testing.T) {
859855
assert.Assert(t, app.IsNew(), "New application did not return new state: %s", app.CurrentState())
860856
res := resources.NewResourceFromMap(map[string]resources.Quantity{"first": 1})
861857
askID := "ask-1"
862-
ask := newAllocationAskTG(askID, appID1, "TG1", res)
858+
ask := newAllocationAskTG(askID, appID1, tg1, res)
863859
err = app.AddAllocationAsk(ask)
864860
assert.NilError(t, err, "ask should have been added to app")
865861
// app with ask, even for placeholder, should be accepted
@@ -884,7 +880,7 @@ func TestStateChangeOnPlaceholderAdd(t *testing.T) {
884880
// app with ask should be accepted
885881
assert.Assert(t, app.IsAccepted(), "Application did not change to accepted state: %s", app.CurrentState())
886882
// add an alloc based on the placeholder ask
887-
allocInfo := newAllocationAll(askID, appID1, nodeID1, "TG1", res, true, 0)
883+
allocInfo := newAllocationAll(askID, appID1, nodeID1, tg1, res, true, 0)
888884
app.AddAllocation(allocInfo)
889885
// app should be in the same state as it was before as it is a placeholder allocation
890886
assert.Assert(t, app.IsAccepted(), "Application did not return accepted state after alloc: %s", app.CurrentState())
@@ -1499,11 +1495,6 @@ func TestTimeoutPlaceholderHard(t *testing.T) {
14991495
}
15001496

15011497
func runTimeoutPlaceholderTest(t *testing.T, expectedState string, gangSchedulingStyle string) {
1502-
const (
1503-
tg1 = "tg-1"
1504-
tg2 = "tg-2"
1505-
)
1506-
15071498
setupUGM()
15081499
// create a fake queue
15091500
queue, err := createRootQueue(nil)
@@ -1595,8 +1586,6 @@ func runTimeoutPlaceholderTest(t *testing.T, expectedState string, gangSchedulin
15951586
}
15961587

15971588
func TestTimeoutPlaceholderAllocReleased(t *testing.T) {
1598-
const tg1 = "tg-1"
1599-
16001589
setupUGM()
16011590

16021591
originalPhTimeout := defaultPlaceholderTimeout
@@ -3438,3 +3427,172 @@ func TestApplication_canAllocationReserve(t *testing.T) {
34383427
})
34393428
}
34403429
}
3430+
3431+
func TestTryPlaceHolderAllocateNoPlaceHolders(t *testing.T) {
3432+
node := newNode(nodeID1, map[string]resources.Quantity{"first": 5})
3433+
nodeMap := map[string]*Node{nodeID1: node}
3434+
iterator := getNodeIteratorFn(node)
3435+
getNode := func(nodeID string) *Node {
3436+
return nodeMap[nodeID]
3437+
}
3438+
3439+
app := newApplication(appID0, "default", "root.default")
3440+
3441+
queue, err := createRootQueue(nil)
3442+
assert.NilError(t, err, "queue create failed")
3443+
app.queue = queue
3444+
3445+
res := resources.NewResourceFromMap(map[string]resources.Quantity{"first": 5})
3446+
ask := newAllocationAsk(aKey, appID0, res)
3447+
ask.taskGroupName = tg1
3448+
err = app.AddAllocationAsk(ask)
3449+
assert.NilError(t, err, "ask should have been added to app")
3450+
3451+
result := app.tryPlaceholderAllocate(iterator, getNode)
3452+
assert.Assert(t, result == nil, "result should be nil since there are no placeholders to allocate")
3453+
}
3454+
3455+
func TestTryPlaceHolderAllocateSmallerRequest(t *testing.T) {
3456+
node := newNode(nodeID1, map[string]resources.Quantity{"first": 5})
3457+
nodeMap := map[string]*Node{nodeID1: node}
3458+
iterator := getNodeIteratorFn(node)
3459+
getNode := func(nodeID string) *Node {
3460+
return nodeMap[nodeID]
3461+
}
3462+
3463+
app := newApplication(appID0, "default", "root.default")
3464+
3465+
queue, err := createRootQueue(nil)
3466+
assert.NilError(t, err, "queue create failed")
3467+
app.queue = queue
3468+
3469+
res := resources.NewResourceFromMap(map[string]resources.Quantity{"first": 5})
3470+
ph := newPlaceholderAlloc(appID0, nodeID1, res, tg1)
3471+
app.AddAllocation(ph)
3472+
app.addPlaceholderData(ph)
3473+
assertPlaceholderData(t, app, tg1, 1, 0, 0, res)
3474+
3475+
// allocation request is smaller than placeholder
3476+
smallerRes := resources.NewResourceFromMap(map[string]resources.Quantity{"first": 1})
3477+
ask := newAllocationAsk(aKey, appID0, smallerRes)
3478+
ask.taskGroupName = tg1
3479+
err = app.AddAllocationAsk(ask)
3480+
assert.NilError(t, err, "ask should have been added to app")
3481+
3482+
result := app.tryPlaceholderAllocate(iterator, getNode)
3483+
assert.Assert(t, result != nil, "result should not be nil since the ask is smaller than the placeholder")
3484+
assert.Equal(t, Replaced, result.ResultType, "result type should be Replaced")
3485+
assert.Equal(t, nodeID1, result.NodeID, "result should be on the same node as placeholder")
3486+
assert.Equal(t, ask, result.Request, "result should contain the ask")
3487+
assert.Equal(t, ph, result.Request.GetRelease(), "real allocation should link to placeholder")
3488+
assert.Equal(t, result.Request, ph.GetRelease(), "placeholder should link to real allocation")
3489+
// placeholder data remains unchanged until RM confirms the replacement
3490+
assertPlaceholderData(t, app, tg1, 1, 0, 0, res)
3491+
}
3492+
3493+
func TestTryPlaceHolderAllocateLargerRequest(t *testing.T) {
3494+
node := newNode(nodeID1, map[string]resources.Quantity{"first": 5})
3495+
nodeMap := map[string]*Node{nodeID1: node}
3496+
iterator := getNodeIteratorFn(node)
3497+
getNode := func(nodeID string) *Node {
3498+
return nodeMap[nodeID]
3499+
}
3500+
3501+
app := newApplication(appID0, "default", "root.default")
3502+
3503+
queue, err := createRootQueue(nil)
3504+
assert.NilError(t, err, "queue create failed")
3505+
app.queue = queue
3506+
3507+
res := resources.NewResourceFromMap(map[string]resources.Quantity{"first": 5})
3508+
ph := newPlaceholderAlloc(appID0, nodeID1, res, tg1)
3509+
app.AddAllocation(ph)
3510+
app.addPlaceholderData(ph)
3511+
assertPlaceholderData(t, app, tg1, 1, 0, 0, res)
3512+
3513+
// allocation request is larger than placeholder
3514+
largerRes := resources.NewResourceFromMap(map[string]resources.Quantity{"first": 10})
3515+
ask := newAllocationAsk(aKey, appID0, largerRes)
3516+
ask.taskGroupName = tg1
3517+
err = app.AddAllocationAsk(ask)
3518+
assert.NilError(t, err, "ask should have been added to app")
3519+
3520+
result := app.tryPlaceholderAllocate(iterator, getNode)
3521+
assert.Assert(t, result == nil, "result should be nil since the ask is larger than the placeholder")
3522+
assert.Assert(t, ph.IsReleased(), "placeholder should have been released")
3523+
// placeholder data remains unchanged until RM confirms the release
3524+
assertPlaceholderData(t, app, tg1, 1, 0, 0, res)
3525+
}
3526+
3527+
func TestTryPlaceHolderAllocateDifferentTaskGroups(t *testing.T) {
3528+
node := newNode(nodeID1, map[string]resources.Quantity{"first": 5})
3529+
nodeMap := map[string]*Node{nodeID1: node}
3530+
iterator := getNodeIteratorFn(node)
3531+
getNode := func(nodeID string) *Node {
3532+
return nodeMap[nodeID]
3533+
}
3534+
3535+
app := newApplication(appID0, "default", "root.default")
3536+
3537+
queue, err := createRootQueue(nil)
3538+
assert.NilError(t, err, "queue create failed")
3539+
app.queue = queue
3540+
3541+
res := resources.NewResourceFromMap(map[string]resources.Quantity{"first": 5})
3542+
ph := newPlaceholderAlloc(appID0, nodeID1, res, tg1)
3543+
app.AddAllocation(ph)
3544+
app.addPlaceholderData(ph)
3545+
assertPlaceholderData(t, app, tg1, 1, 0, 0, res)
3546+
3547+
// allocation request has a different task group
3548+
ask := newAllocationAsk(aKey, appID0, res)
3549+
ask.taskGroupName = tg2
3550+
err = app.AddAllocationAsk(ask)
3551+
assert.NilError(t, err, "ask should have been added to app")
3552+
3553+
result := app.tryPlaceholderAllocate(iterator, getNode)
3554+
assert.Assert(t, result == nil, "result should be nil since the ask has a different task group")
3555+
}
3556+
3557+
func TestTryPlaceHolderAllocateDifferentNodes(t *testing.T) {
3558+
node1 := newNode(nodeID1, map[string]resources.Quantity{"first": 5})
3559+
node2 := newNode(nodeID2, map[string]resources.Quantity{"first": 5})
3560+
nodeMap := map[string]*Node{nodeID1: node1, nodeID2: node2}
3561+
iterator := getNodeIteratorFn(node1, node2)
3562+
getNode := func(nodeID string) *Node {
3563+
return nodeMap[nodeID]
3564+
}
3565+
3566+
app := newApplication(appID0, "default", "root.default")
3567+
3568+
queue, err := createRootQueue(nil)
3569+
assert.NilError(t, err, "queue create failed")
3570+
app.queue = queue
3571+
3572+
res := resources.NewResourceFromMap(map[string]resources.Quantity{"first": 5})
3573+
ph := newPlaceholderAlloc(appID0, nodeID1, res, tg1)
3574+
app.AddAllocation(ph)
3575+
app.addPlaceholderData(ph)
3576+
assertPlaceholderData(t, app, tg1, 1, 0, 0, res)
3577+
3578+
// predicate check fails on node1 and passes on node2
3579+
mockPlugin := mockCommon.NewPredicatePlugin(false, map[string]int{nodeID1: 0})
3580+
plugins.RegisterSchedulerPlugin(mockPlugin)
3581+
defer plugins.UnregisterSchedulerPlugins()
3582+
3583+
// should allocate on node2
3584+
ask := newAllocationAsk(aKey, appID0, res)
3585+
ask.taskGroupName = tg1
3586+
err = app.AddAllocationAsk(ask)
3587+
assert.NilError(t, err, "ask should have been added to app")
3588+
3589+
result := app.tryPlaceholderAllocate(iterator, getNode)
3590+
assert.Assert(t, result != nil, "result should not be nil")
3591+
assert.Equal(t, Replaced, result.ResultType, "result type should be Replaced")
3592+
assert.Equal(t, nodeID2, result.NodeID, "result should be on node2")
3593+
assert.Equal(t, ask, result.Request, "result should contain the ask")
3594+
assert.Equal(t, ph, result.Request.GetRelease(), "real allocation should link to placeholder")
3595+
assert.Equal(t, result.Request, ph.GetRelease(), "placeholder should link to real allocation")
3596+
// placeholder data remains unchanged until RM confirms the replacement
3597+
assertPlaceholderData(t, app, tg1, 1, 0, 0, res)
3598+
}

pkg/scheduler/objects/utilities_test.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,9 @@ const (
5050
nodeID2 = "node-2"
5151
instType1 = "itype-1"
5252
testgroup = "testgroup"
53+
tg1 = "tg-1"
54+
tg2 = "tg-2"
55+
tg3 = "tg-3"
5356
foreignAlloc1 = "foreign-1"
5457
foreignAlloc2 = "foreign-2"
5558
)

0 commit comments

Comments
 (0)