wilfred-s commented on a change in pull request #173:
URL: 
https://github.com/apache/incubator-yunikorn-core/pull/173#discussion_r450652511



##########
File path: pkg/scheduler/scheduling_queue.go
##########
@@ -482,9 +482,26 @@ func (sq *SchedulingQueue) getHeadRoom() 
*resources.Resource {
        if sq.parent != nil {
                parentHeadRoom = sq.parent.getHeadRoom()
        }
+       return sq.internalHeadRoom(parentHeadRoom)
+}
+
+// this function returns the max headRoom of a queue
+// this doesn't get the partition resources into the consideration
+func (sq *SchedulingQueue) getMaxHeadRoom() *resources.Resource {

Review comment:
       I do not see how this would solve the hierarchy issue.
   It might even cause more issues as the headroom for a queue might be 
different for different locations in the queue hierarchy. The normal headroom 
includes the max from the root (i.e cluster size) so we cannot use that and 
need to use the max as defined below to start with.
   

##########
File path: pkg/scheduler/scheduling_application.go
##########
@@ -409,6 +409,21 @@ func (sa *SchedulingApplication) sortRequests(ascending 
bool) {
        }
 }
 
+func (sa *SchedulingApplication) getOutstandingRequests(headRoom 
*resources.Resource, total *outstandingRequests) {

Review comment:
       The requests are not allocatable, if they were we would and should not 
see them in the list.
   

##########
File path: pkg/scheduler/scheduling_queue_test.go
##########
@@ -827,3 +1118,157 @@ func TestIsEmpty(t *testing.T) {
        leaf.addSchedulingApplication(app)
        assert.Equal(t, leaf.isEmpty(), false, "queue with registered app 
should not be empty")
 }
+
+func TestGetOutstandingRequest(t *testing.T) {
+       const app1ID = "app1"
+       const app2ID = "app1"
+
+       // queue structure:
+       // root
+       //   - queue1 (max.cpu = 10)
+       //   - queue2 (max.cpu = 5)
+       //
+       // submit app1 to root.queue1, app2 to root.queue2
+       // app1 asks for 20 1x1CPU requests, app2 asks for 20 1x1CPU requests
+       // verify the outstanding requests for each of the queue is up to its 
max capacity, 10/5 respectively
+       root, err := createRootQueue(nil)
+       assert.NilError(t, err, "failed to create root queue with limit")
+       var queue1, queue2 *SchedulingQueue
+       queue1, err = createManagedQueue(root, "queue1", false, 
map[string]string{"cpu": "10"})
+       assert.NilError(t, err, "failed to create queue1 queue")
+       queue2, err = createManagedQueue(root, "queue2", false, 
map[string]string{"cpu": "5"})
+       assert.NilError(t, err, "failed to create queue2 queue")
+
+       app1Info := cache.NewApplicationInfo(app1ID, "default", "root.queue1", 
security.UserGroup{}, nil)
+       app1 := newSchedulingApplication(app1Info)
+       app1.queue = queue1
+       queue1.addSchedulingApplication(app1)
+       res, err := resources.NewResourceFromConf(map[string]string{"cpu": "1"})
+       assert.NilError(t, err, "failed to create basic resource")
+       for i := 0; i < 20; i++ {
+               _, err = app1.addAllocationAsk(
+                       newAllocationAsk(fmt.Sprintf("alloc-%d", i), app1ID, 
res))
+               assert.NilError(t, err, "failed to add allocation ask")
+       }
+
+       app2Info := cache.NewApplicationInfo(app2ID, "default", "root.queue2", 
security.UserGroup{}, nil)
+       app2 := newSchedulingApplication(app2Info)
+       app2.queue = queue2
+       queue2.addSchedulingApplication(app2)
+       for i := 0; i < 20; i++ {
+               _, err = app2.addAllocationAsk(
+                       newAllocationAsk(fmt.Sprintf("alloc-%d", i), app2ID, 
res))
+               assert.NilError(t, err, "failed to add allocation ask")
+       }
+
+       // verify get outstanding requests for root, and child queues
+       rootTotal := newOutstandingRequests()
+       root.getQueueOutstandingRequests(rootTotal)
+       assert.Equal(t, len(rootTotal.getRequests()), 15)
+
+       queue1Total := newOutstandingRequests()
+       queue1.getQueueOutstandingRequests(queue1Total)
+       assert.Equal(t, len(queue1Total.getRequests()), 10)
+
+       queue2Total := newOutstandingRequests()
+       queue2.getQueueOutstandingRequests(queue2Total)
+       assert.Equal(t, len(queue2Total.getRequests()), 5)
+
+       // simulate queue1 has some allocating resources
+       // after allocation, the max available becomes to be 5
+       allocatingRest := map[string]string{"cpu": "5"}
+       allocation, err := resources.NewResourceFromConf(allocatingRest)
+       assert.NilError(t, err, "failed to create basic resource")
+       queue1.incAllocatingResource(allocation)
+
+       queue1Total = newOutstandingRequests()
+       queue1.getQueueOutstandingRequests(queue1Total)
+       assert.Equal(t, len(queue1Total.getRequests()), 5)
+
+       queue2Total = newOutstandingRequests()
+       queue2.getQueueOutstandingRequests(queue2Total)
+       assert.Equal(t, len(queue2Total.getRequests()), 5)
+
+       rootTotal = newOutstandingRequests()
+       root.getQueueOutstandingRequests(rootTotal)
+       assert.Equal(t, len(rootTotal.getRequests()), 10)
+
+       // remove app2 from queue2
+       queue2.removeSchedulingApplication(app2)
+       queue2Total = newOutstandingRequests()
+       queue2.getQueueOutstandingRequests(queue2Total)
+       assert.Equal(t, len(queue2Total.getRequests()), 0)
+
+       rootTotal = newOutstandingRequests()
+       root.getQueueOutstandingRequests(rootTotal)
+       assert.Equal(t, len(rootTotal.getRequests()), 5)
+}
+
+func TestGetOutstandingRequestWithStateAwareSortingPolicy(t *testing.T) {
+       const app1ID = "app1"
+       const app2ID = "app2"

Review comment:
       A policy must not impact the tests. The outcome of the policy is a 
filtered list of sorted applications. The internal working of the policy is 
tested in the policy testing.
   The policies internal working will never directly impact the tests.
   Please remove the test.

##########
File path: pkg/scheduler/scheduling_queue_test.go
##########
@@ -688,11 +694,296 @@ func TestHeadroom(t *testing.T) {
        res, err = resources.NewResourceFromConf(map[string]string{"first": 
"10", "second": "2"})
        assert.NilError(t, err, "failed to create resource")
        headRoom = leaf1.getHeadRoom()
-       if !resources.Equals(res, headRoom) {
+       maxHeadRoom = leaf1.getMaxHeadRoom()
+       if !resources.Equals(res, headRoom) || !resources.Equals(res, 
maxHeadRoom) {
                t.Errorf("leaf1 queue head room not as expected %v, got: %v 
(err %v)", res, headRoom, err)
        }
        headRoom = leaf2.getHeadRoom()
+       maxHeadRoom = leaf2.getMaxHeadRoom()
+       if !resources.Equals(res, headRoom) || !resources.Equals(res, 
maxHeadRoom) {
+               t.Errorf("leaf1 queue head room not as expected %v, got: %v 
(err %v)", res, headRoom, err)
+       }
+}
+
+//nolint: funlen
+func TestMaxHeadroom(t *testing.T) {
+       // create the root: nil test
+       root, err := createRootQueue(nil)
+       assert.NilError(t, err, "queue create failed")
+       headRoom := root.getMaxHeadRoom()
+       if headRoom != nil {
+               t.Errorf("empty cluster with root queue should not have 
headroom: %v", headRoom)
+       }
+
+       var parent *SchedulingQueue
+       // empty parent queue: nil test
+       parent, err = createManagedQueue(root, "parent", true, nil)
+       assert.NilError(t, err, "failed to create parent queue")
+       headRoom = parent.getMaxHeadRoom()
+       if headRoom != nil {
+               t.Errorf("empty cluster with parent queue should not have 
headroom: %v", headRoom)
+       }
+
+       // recreate the structure, all queues have no max capacity set
+       // structure is:
+       // root (max: nil)
+       //   - parent (max: nil)
+       //     - leaf1 (max: nil)  (alloc: 5,3)
+       //     - leaf2 (max: nil)  (alloc: 5,3)
+       root, err = createRootQueue(nil)
+       assert.NilError(t, err, "failed to create root queue with limit")
+       parent, err = createManagedQueue(root, "parent", true, nil)
+       assert.NilError(t, err, "failed to create parent queue")
+       var leaf1, leaf2 *SchedulingQueue
+       leaf1, err = createManagedQueue(parent, "leaf1", false, nil)
+       assert.NilError(t, err, "failed to create leaf1 queue")
+       leaf2, err = createManagedQueue(parent, "leaf2", false, nil)
+       assert.NilError(t, err, "failed to create leaf2 queue")
+
+       // allocating and allocated
+       var res *resources.Resource
+       res, err = resources.NewResourceFromConf(map[string]string{"first": 
"1", "second": "1"})
+       assert.NilError(t, err, "failed to create resource")
+       leaf1.incAllocatingResource(res)
+       leaf2.incAllocatingResource(res)
+       res, err = resources.NewResourceFromConf(map[string]string{"first": 
"4", "second": "2"})
+       assert.NilError(t, err, "failed to create resource")
+       err = leaf1.QueueInfo.IncAllocatedResource(res, true)
+       assert.NilError(t, err, "failed to set allocated resource on leaf1")
+       err = leaf2.QueueInfo.IncAllocatedResource(res, true)
+       assert.NilError(t, err, "failed to set allocated resource on leaf2")
+
+       headRoom = root.getMaxHeadRoom()
+       assert.Assert(t, headRoom == nil, "headRoom of root should be nil 
because no max set for all queues")
+
+       headRoom = parent.getMaxHeadRoom()
+       assert.Assert(t, headRoom == nil, "headRoom of parent should be nil 
because no max set for all queues")
+
+       headRoom = leaf1.getMaxHeadRoom()
+       assert.Assert(t, headRoom == nil, "headRoom of leaf1 should be nil 
because no max set for all queues")
+
+       headRoom = leaf2.getMaxHeadRoom()
+       assert.Assert(t, headRoom == nil, "headRoom of leaf2 should be nil 
because no max set for all queues")
+
+       // recreate the structure, set max capacity only in parent level
+       // structure is:
+       // root (max: nil)
+       //   - parent (max: 20,8)
+       //     - leaf1 (max: nil)  (alloc: 5,3)
+       //     - leaf2 (max: nil)  (alloc: 6,4)
+       root, err = createRootQueue(nil)
+       resMap := map[string]string{"first": "20", "second": "8"}
+       assert.NilError(t, err, "failed to create root queue with limit")
+       parent, err = createManagedQueue(root, "parent", true, resMap)
+       assert.NilError(t, err, "failed to create parent queue")
+       leaf1, err = createManagedQueue(parent, "leaf1", false, nil)
+       assert.NilError(t, err, "failed to create leaf1 queue")
+       leaf2, err = createManagedQueue(parent, "leaf2", false, nil)
+       assert.NilError(t, err, "failed to create leaf2 queue")
+
+       // allocating and allocated
+       res, err = resources.NewResourceFromConf(map[string]string{"first": 
"1", "second": "1"})
+       assert.NilError(t, err, "failed to create resource")
+       leaf1.incAllocatingResource(res)
+       leaf2.incAllocatingResource(res)
+       leaf2.incAllocatingResource(res)
+       res, err = resources.NewResourceFromConf(map[string]string{"first": 
"4", "second": "2"})
+       assert.NilError(t, err, "failed to create resource")
+       err = leaf1.QueueInfo.IncAllocatedResource(res, true)
+       assert.NilError(t, err, "failed to set allocated resource on leaf1")
+       err = leaf2.QueueInfo.IncAllocatedResource(res, true)
+       assert.NilError(t, err, "failed to set allocated resource on leaf2")
+
+       // root headroom should be nil
+       headRoom = root.getMaxHeadRoom()
+       assert.Assert(t, headRoom == nil, "headRoom of root should be nil 
because no max set for all queues")
+
+       // parent headroom = parentMax - leaf1Allocated - leaf2Allocated
+       // parent headroom = (20 - 5 - 6, 8 - 3 - 4) = (9, 1)
+       res, err = resources.NewResourceFromConf(map[string]string{"first": 
"9", "second": "1"})
+       headRoom = parent.getMaxHeadRoom()
+       if err != nil || !resources.Equals(res, headRoom) {
+               t.Errorf("parent queue head room not as expected %v, got: %v 
(err %v)", res, headRoom, err)
+       }
+
+       // leaf1 headroom = parentMax - leaf1Allocated - leaf2Allocated
+       headRoom = leaf1.getMaxHeadRoom()
+       if !resources.Equals(res, headRoom) {
+               t.Errorf("leaf1 queue head room not as expected %v, got: %v 
(err %v)", res, headRoom, err)
+       }
+
+       headRoom = leaf2.getMaxHeadRoom()
+       if !resources.Equals(res, headRoom) {
+               t.Errorf("leaf2 queue head room not as expected %v, got: %v 
(err %v)", res, headRoom, err)
+       }
+
+       // recreate the structure, set max capacity in root level, verify that 
will be ignored
+       // structure is:
+       // root (max: 1, 1)
+       //   - parent (max: 20,8)
+       //     - leaf1 (max: nil)  (alloc: 5,3)
+       //     - leaf2 (max: nil)  (alloc: 6,4)
+       resMap = map[string]string{"first": "1", "second": "1"}
+       root, err = createRootQueue(resMap)
+       assert.NilError(t, err, "failed to create root queue with limit")
+       resMap = map[string]string{"first": "20", "second": "8"}
+       parent, err = createManagedQueue(root, "parent", true, resMap)
+       assert.NilError(t, err, "failed to create parent queue")
+       leaf1, err = createManagedQueue(parent, "leaf1", false, nil)
+       assert.NilError(t, err, "failed to create leaf1 queue")
+       leaf2, err = createManagedQueue(parent, "leaf2", false, nil)
+       assert.NilError(t, err, "failed to create leaf2 queue")
+
+       // allocating and allocated
+       res, err = resources.NewResourceFromConf(map[string]string{"first": 
"1", "second": "1"})
+       assert.NilError(t, err, "failed to create resource")
+       leaf1.incAllocatingResource(res)
+       leaf2.incAllocatingResource(res)
+       leaf2.incAllocatingResource(res)
+       res, err = resources.NewResourceFromConf(map[string]string{"first": 
"4", "second": "2"})
+       assert.NilError(t, err, "failed to create resource")
+       err = leaf1.QueueInfo.IncAllocatedResource(res, true)
+       assert.NilError(t, err, "failed to set allocated resource on leaf1")
+       err = leaf2.QueueInfo.IncAllocatedResource(res, true)
+       assert.NilError(t, err, "failed to set allocated resource on leaf2")
+
+       // root headroom should be nil
+       headRoom = root.getMaxHeadRoom()
+       assert.Assert(t, headRoom == nil, "headRoom of root should be nil 
because no max set for all queues")
+
+       // parent headroom = parentMax - leaf1Allocated - leaf2Allocated
+       // parent headroom = (20 - 5 - 6, 8 - 3 - 4) = (9, 1)
+       res, err = resources.NewResourceFromConf(map[string]string{"first": 
"9", "second": "1"})
+       headRoom = parent.getMaxHeadRoom()
+       if err != nil || !resources.Equals(res, headRoom) {
+               t.Errorf("parent queue head room not as expected %v, got: %v 
(err %v)", res, headRoom, err)
+       }
+
+       // leaf1 headroom = parentMax - leaf1Allocated - leaf2Allocated
+       headRoom = leaf1.getMaxHeadRoom()
+       if !resources.Equals(res, headRoom) {
+               t.Errorf("leaf1 queue head room not as expected %v, got: %v 
(err %v)", res, headRoom, err)
+       }
+
+       // leaf2 headroom = parentMax - leaf1Allocated - leaf2Allocated
+       headRoom = leaf2.getMaxHeadRoom()
        if !resources.Equals(res, headRoom) {
+               t.Errorf("leaf2 queue head room not as expected %v, got: %v 
(err %v)", res, headRoom, err)
+       }
+
+       // recreate the structure, set max capacity in parent and a leaf queue
+       // structure is:
+       // root (max: nil)
+       //   - parent (max: 20,8)
+       //     - leaf1 (max: 10, 8)  (alloc: 5,3)
+       //     - leaf2 (max: nil)    (alloc: 6,4)
+       resMap = map[string]string{"first": "20", "second": "8"}
+       root, err = createRootQueue(nil)
+       assert.NilError(t, err, "failed to create root queue with limit")
+       parent, err = createManagedQueue(root, "parent", true, resMap)
+       assert.NilError(t, err, "failed to create parent queue")
+       resMap = map[string]string{"first": "10", "second": "8"}
+       leaf1, err = createManagedQueue(parent, "leaf1", false, resMap)
+       assert.NilError(t, err, "failed to create leaf1 queue")
+       leaf2, err = createManagedQueue(parent, "leaf2", false, nil)
+       assert.NilError(t, err, "failed to create leaf2 queue")
+
+       // allocating and allocated
+       res, err = resources.NewResourceFromConf(map[string]string{"first": 
"1", "second": "1"})
+       assert.NilError(t, err, "failed to create resource")
+       leaf1.incAllocatingResource(res)
+       leaf2.incAllocatingResource(res)
+       leaf2.incAllocatingResource(res)
+       res, err = resources.NewResourceFromConf(map[string]string{"first": 
"4", "second": "2"})
+       assert.NilError(t, err, "failed to create resource")
+       err = leaf1.QueueInfo.IncAllocatedResource(res, true)
+       assert.NilError(t, err, "failed to set allocated resource on leaf1")
+       err = leaf2.QueueInfo.IncAllocatedResource(res, true)
+       assert.NilError(t, err, "failed to set allocated resource on leaf2")
+
+       // root headroom should be nil
+       headRoom = root.getMaxHeadRoom()
+       assert.Assert(t, headRoom == nil, "headRoom of root should be nil 
because no max set for all queues")
+
+       // parent headroom = parentMax - leaf1Allocated - leaf2Allocated
+       // parent headroom = (20 - 5 - 6, 8 - 3 - 4) = (9, 1)
+       res, err = resources.NewResourceFromConf(map[string]string{"first": 
"9", "second": "1"})
+       headRoom = parent.getMaxHeadRoom()
+       if err != nil || !resources.Equals(res, headRoom) {
+               t.Errorf("parent queue head room not as expected %v, got: %v 
(err %v)", res, headRoom, err)
+       }
+
+       // leaf1 headroom = MIN(parentHeadRoom, leaf1Max - leaf1Allocated)
+       // leaf1 headroom = MIN((9,1), (10-5, 8-3)) = MIN((9,1), (5,5)) = 
MIN(5, 1)
+       res, err = resources.NewResourceFromConf(map[string]string{"first": 
"5", "second": "1"})
+       headRoom = leaf1.getMaxHeadRoom()
+       if err != nil || !resources.Equals(res, headRoom) {
+               t.Errorf("leaf1 queue head room not as expected %v, got: %v 
(err %v)", res, headRoom, err)
+       }
+
+       // leaf2 headroom = parentMax - leaf1Allocated - leaf2Allocated
+       res, err = resources.NewResourceFromConf(map[string]string{"first": 
"9", "second": "1"})
+       headRoom = leaf2.getMaxHeadRoom()
+       if err != nil || !resources.Equals(res, headRoom) {
+               t.Errorf("leaf2 queue head room not as expected %v, got: %v 
(err %v)", res, headRoom, err)
+       }
+
+       // recreate the structure, set max capacity in parent and a leaf queue
+       // structure is:
+       // root (max: nil)
+       //   - parent (max: 20,8)
+       //     - parent1 (max: nil)
+       //       - parent2 (max: 10,4)
+       //         - leaf1 (max: nil)   (alloc: 3,3)

Review comment:
       This tests the same as a parent with a leaf below it and does not 
include any new test case.
   please remove this case

##########
File path: pkg/scheduler/scheduling_partition.go
##########
@@ -356,6 +356,17 @@ func (psc *partitionSchedulingContext) 
removeSchedulingNode(nodeID string) {
        }
 }
 
+func (psc *partitionSchedulingContext) calculateOutstandingRequests() 
*outstandingRequests {

Review comment:
       see above, not sure that this is allocatable

##########
File path: pkg/scheduler/scheduling_queue.go
##########
@@ -558,6 +575,19 @@ func (sq *SchedulingQueue) tryAllocate(ctx 
*partitionSchedulingContext) *schedul
        return nil
 }
 
+func (sq *SchedulingQueue) getQueueOutstandingRequests(total 
*outstandingRequests) {

Review comment:
       See above, not sure that this is allocatable

##########
File path: pkg/scheduler/scheduling_queue_test.go
##########
@@ -827,3 +1118,157 @@ func TestIsEmpty(t *testing.T) {
        leaf.addSchedulingApplication(app)
        assert.Equal(t, leaf.isEmpty(), false, "queue with registered app 
should not be empty")
 }
+
+func TestGetOutstandingRequest(t *testing.T) {
+       const app1ID = "app1"
+       const app2ID = "app1"

Review comment:
       The logic for calculating this is in the app and not in the queue.
   If the app returns the correct thing the queue just add them at this point. 
We should test the app not the queue.

##########
File path: pkg/scheduler/scheduling_queue_test.go
##########
@@ -688,11 +694,296 @@ func TestHeadroom(t *testing.T) {
        res, err = resources.NewResourceFromConf(map[string]string{"first": 
"10", "second": "2"})
        assert.NilError(t, err, "failed to create resource")
        headRoom = leaf1.getHeadRoom()
-       if !resources.Equals(res, headRoom) {
+       maxHeadRoom = leaf1.getMaxHeadRoom()
+       if !resources.Equals(res, headRoom) || !resources.Equals(res, 
maxHeadRoom) {
                t.Errorf("leaf1 queue head room not as expected %v, got: %v 
(err %v)", res, headRoom, err)
        }
        headRoom = leaf2.getHeadRoom()
+       maxHeadRoom = leaf2.getMaxHeadRoom()
+       if !resources.Equals(res, headRoom) || !resources.Equals(res, 
maxHeadRoom) {
+               t.Errorf("leaf1 queue head room not as expected %v, got: %v 
(err %v)", res, headRoom, err)
+       }
+}
+
+//nolint: funlen
+func TestMaxHeadroom(t *testing.T) {
+       // create the root: nil test
+       root, err := createRootQueue(nil)
+       assert.NilError(t, err, "queue create failed")
+       headRoom := root.getMaxHeadRoom()
+       if headRoom != nil {
+               t.Errorf("empty cluster with root queue should not have 
headroom: %v", headRoom)
+       }
+
+       var parent *SchedulingQueue
+       // empty parent queue: nil test
+       parent, err = createManagedQueue(root, "parent", true, nil)
+       assert.NilError(t, err, "failed to create parent queue")
+       headRoom = parent.getMaxHeadRoom()
+       if headRoom != nil {
+               t.Errorf("empty cluster with parent queue should not have 
headroom: %v", headRoom)
+       }
+
+       // recreate the structure, all queues have no max capacity set
+       // structure is:
+       // root (max: nil)
+       //   - parent (max: nil)
+       //     - leaf1 (max: nil)  (alloc: 5,3)
+       //     - leaf2 (max: nil)  (alloc: 5,3)
+       root, err = createRootQueue(nil)
+       assert.NilError(t, err, "failed to create root queue with limit")
+       parent, err = createManagedQueue(root, "parent", true, nil)
+       assert.NilError(t, err, "failed to create parent queue")
+       var leaf1, leaf2 *SchedulingQueue
+       leaf1, err = createManagedQueue(parent, "leaf1", false, nil)
+       assert.NilError(t, err, "failed to create leaf1 queue")
+       leaf2, err = createManagedQueue(parent, "leaf2", false, nil)
+       assert.NilError(t, err, "failed to create leaf2 queue")
+
+       // allocating and allocated
+       var res *resources.Resource
+       res, err = resources.NewResourceFromConf(map[string]string{"first": 
"1", "second": "1"})
+       assert.NilError(t, err, "failed to create resource")
+       leaf1.incAllocatingResource(res)
+       leaf2.incAllocatingResource(res)
+       res, err = resources.NewResourceFromConf(map[string]string{"first": 
"4", "second": "2"})
+       assert.NilError(t, err, "failed to create resource")
+       err = leaf1.QueueInfo.IncAllocatedResource(res, true)
+       assert.NilError(t, err, "failed to set allocated resource on leaf1")
+       err = leaf2.QueueInfo.IncAllocatedResource(res, true)
+       assert.NilError(t, err, "failed to set allocated resource on leaf2")
+
+       headRoom = root.getMaxHeadRoom()
+       assert.Assert(t, headRoom == nil, "headRoom of root should be nil 
because no max set for all queues")
+
+       headRoom = parent.getMaxHeadRoom()
+       assert.Assert(t, headRoom == nil, "headRoom of parent should be nil 
because no max set for all queues")
+
+       headRoom = leaf1.getMaxHeadRoom()
+       assert.Assert(t, headRoom == nil, "headRoom of leaf1 should be nil 
because no max set for all queues")
+
+       headRoom = leaf2.getMaxHeadRoom()
+       assert.Assert(t, headRoom == nil, "headRoom of leaf2 should be nil 
because no max set for all queues")
+
+       // recreate the structure, set max capacity only in parent level
+       // structure is:
+       // root (max: nil)
+       //   - parent (max: 20,8)
+       //     - leaf1 (max: nil)  (alloc: 5,3)
+       //     - leaf2 (max: nil)  (alloc: 6,4)
+       root, err = createRootQueue(nil)
+       resMap := map[string]string{"first": "20", "second": "8"}
+       assert.NilError(t, err, "failed to create root queue with limit")
+       parent, err = createManagedQueue(root, "parent", true, resMap)
+       assert.NilError(t, err, "failed to create parent queue")
+       leaf1, err = createManagedQueue(parent, "leaf1", false, nil)
+       assert.NilError(t, err, "failed to create leaf1 queue")
+       leaf2, err = createManagedQueue(parent, "leaf2", false, nil)
+       assert.NilError(t, err, "failed to create leaf2 queue")
+
+       // allocating and allocated
+       res, err = resources.NewResourceFromConf(map[string]string{"first": 
"1", "second": "1"})
+       assert.NilError(t, err, "failed to create resource")
+       leaf1.incAllocatingResource(res)
+       leaf2.incAllocatingResource(res)
+       leaf2.incAllocatingResource(res)
+       res, err = resources.NewResourceFromConf(map[string]string{"first": 
"4", "second": "2"})
+       assert.NilError(t, err, "failed to create resource")
+       err = leaf1.QueueInfo.IncAllocatedResource(res, true)
+       assert.NilError(t, err, "failed to set allocated resource on leaf1")
+       err = leaf2.QueueInfo.IncAllocatedResource(res, true)
+       assert.NilError(t, err, "failed to set allocated resource on leaf2")
+
+       // root headroom should be nil
+       headRoom = root.getMaxHeadRoom()
+       assert.Assert(t, headRoom == nil, "headRoom of root should be nil 
because no max set for all queues")
+
+       // parent headroom = parentMax - leaf1Allocated - leaf2Allocated
+       // parent headroom = (20 - 5 - 6, 8 - 3 - 4) = (9, 1)
+       res, err = resources.NewResourceFromConf(map[string]string{"first": 
"9", "second": "1"})
+       headRoom = parent.getMaxHeadRoom()
+       if err != nil || !resources.Equals(res, headRoom) {
+               t.Errorf("parent queue head room not as expected %v, got: %v 
(err %v)", res, headRoom, err)
+       }
+
+       // leaf1 headroom = parentMax - leaf1Allocated - leaf2Allocated
+       headRoom = leaf1.getMaxHeadRoom()
+       if !resources.Equals(res, headRoom) {
+               t.Errorf("leaf1 queue head room not as expected %v, got: %v 
(err %v)", res, headRoom, err)
+       }
+
+       headRoom = leaf2.getMaxHeadRoom()
+       if !resources.Equals(res, headRoom) {
+               t.Errorf("leaf2 queue head room not as expected %v, got: %v 
(err %v)", res, headRoom, err)
+       }
+
+       // recreate the structure, set max capacity in root level, verify that 
will be ignored
+       // structure is:
+       // root (max: 1, 1)
+       //   - parent (max: 20,8)
+       //     - leaf1 (max: nil)  (alloc: 5,3)
+       //     - leaf2 (max: nil)  (alloc: 6,4)

Review comment:
       This does not add anything that is/cannot tested by calling 
getMaxHeadroom on the root queue directly.
   Should be rolled into the `nil` tests at the start.
   Please remove this case.

##########
File path: pkg/scheduler/scheduling_queue_test.go
##########
@@ -688,11 +694,296 @@ func TestHeadroom(t *testing.T) {
        res, err = resources.NewResourceFromConf(map[string]string{"first": 
"10", "second": "2"})
        assert.NilError(t, err, "failed to create resource")
        headRoom = leaf1.getHeadRoom()
-       if !resources.Equals(res, headRoom) {
+       maxHeadRoom = leaf1.getMaxHeadRoom()
+       if !resources.Equals(res, headRoom) || !resources.Equals(res, 
maxHeadRoom) {
                t.Errorf("leaf1 queue head room not as expected %v, got: %v 
(err %v)", res, headRoom, err)
        }
        headRoom = leaf2.getHeadRoom()
+       maxHeadRoom = leaf2.getMaxHeadRoom()
+       if !resources.Equals(res, headRoom) || !resources.Equals(res, 
maxHeadRoom) {
+               t.Errorf("leaf1 queue head room not as expected %v, got: %v 
(err %v)", res, headRoom, err)
+       }
+}
+
+//nolint: funlen
+func TestMaxHeadroom(t *testing.T) {
+       // create the root: nil test
+       root, err := createRootQueue(nil)
+       assert.NilError(t, err, "queue create failed")
+       headRoom := root.getMaxHeadRoom()
+       if headRoom != nil {
+               t.Errorf("empty cluster with root queue should not have 
headroom: %v", headRoom)
+       }
+
+       var parent *SchedulingQueue
+       // empty parent queue: nil test
+       parent, err = createManagedQueue(root, "parent", true, nil)
+       assert.NilError(t, err, "failed to create parent queue")
+       headRoom = parent.getMaxHeadRoom()
+       if headRoom != nil {
+               t.Errorf("empty cluster with parent queue should not have 
headroom: %v", headRoom)
+       }
+
+       // recreate the structure, all queues have no max capacity set
+       // structure is:
+       // root (max: nil)
+       //   - parent (max: nil)
+       //     - leaf1 (max: nil)  (alloc: 5,3)
+       //     - leaf2 (max: nil)  (alloc: 5,3)
+       root, err = createRootQueue(nil)
+       assert.NilError(t, err, "failed to create root queue with limit")
+       parent, err = createManagedQueue(root, "parent", true, nil)
+       assert.NilError(t, err, "failed to create parent queue")
+       var leaf1, leaf2 *SchedulingQueue
+       leaf1, err = createManagedQueue(parent, "leaf1", false, nil)
+       assert.NilError(t, err, "failed to create leaf1 queue")
+       leaf2, err = createManagedQueue(parent, "leaf2", false, nil)
+       assert.NilError(t, err, "failed to create leaf2 queue")
+
+       // allocating and allocated
+       var res *resources.Resource
+       res, err = resources.NewResourceFromConf(map[string]string{"first": 
"1", "second": "1"})
+       assert.NilError(t, err, "failed to create resource")
+       leaf1.incAllocatingResource(res)
+       leaf2.incAllocatingResource(res)
+       res, err = resources.NewResourceFromConf(map[string]string{"first": 
"4", "second": "2"})
+       assert.NilError(t, err, "failed to create resource")
+       err = leaf1.QueueInfo.IncAllocatedResource(res, true)
+       assert.NilError(t, err, "failed to set allocated resource on leaf1")
+       err = leaf2.QueueInfo.IncAllocatedResource(res, true)
+       assert.NilError(t, err, "failed to set allocated resource on leaf2")
+
+       headRoom = root.getMaxHeadRoom()
+       assert.Assert(t, headRoom == nil, "headRoom of root should be nil 
because no max set for all queues")
+
+       headRoom = parent.getMaxHeadRoom()
+       assert.Assert(t, headRoom == nil, "headRoom of parent should be nil 
because no max set for all queues")
+
+       headRoom = leaf1.getMaxHeadRoom()
+       assert.Assert(t, headRoom == nil, "headRoom of leaf1 should be nil 
because no max set for all queues")
+
+       headRoom = leaf2.getMaxHeadRoom()
+       assert.Assert(t, headRoom == nil, "headRoom of leaf2 should be nil 
because no max set for all queues")
+
+       // recreate the structure, set max capacity only in parent level
+       // structure is:
+       // root (max: nil)
+       //   - parent (max: 20,8)
+       //     - leaf1 (max: nil)  (alloc: 5,3)
+       //     - leaf2 (max: nil)  (alloc: 6,4)

Review comment:
       This is testing the special case of the below setup with one leaf with a 
max. Both do the same for the leaf queue that has no limit set.
   Please remove this case.

##########
File path: pkg/scheduler/outstanding_requests.go
##########
@@ -0,0 +1,43 @@
+/*
+ Licensed to the Apache Software Foundation (ASF) under one
+ or more contributor license agreements.  See the NOTICE file
+ distributed with this work for additional information
+ regarding copyright ownership.  The ASF licenses this file
+ to you 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 scheduler
+
+// an outstanding request is a request that fits into the queue capacity but in
+// pending state because there is no enough partition resources to allocate it
+type outstandingRequests struct {

Review comment:
       agree, no need for this structure at all just return a simple array 
(please remove)




----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
[email protected]


Reply via email to