-
Notifications
You must be signed in to change notification settings - Fork 211
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
improve the nodeclaim sorting algorithm using heap
Signed-off-by: Jay Shane <327411586@qq.com>
- Loading branch information
Showing
4 changed files
with
105 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
/* | ||
Copyright The Kubernetes Authors. | ||
Licensed 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 scheduling | ||
|
||
import "container/heap" | ||
|
||
type NodeClaimHeap []*NodeClaim | ||
|
||
var ( | ||
_ = heap.Interface(&NodeClaimHeap{}) // NodeClaimHeap is a standard heap | ||
) | ||
|
||
func NewNodeClaimHeap(nodeClaims []*NodeClaim) *NodeClaimHeap { | ||
h := NodeClaimHeap(nodeClaims) | ||
return &h | ||
} | ||
|
||
func (h NodeClaimHeap) Len() int { return len(h) } | ||
func (h NodeClaimHeap) Less(i, j int) bool { return len(h[i].Pods) < len(h[j].Pods) } | ||
func (h NodeClaimHeap) Swap(i, j int) { h[i], h[j] = h[j], h[i] } | ||
|
||
func (h *NodeClaimHeap) Push(x interface{}) { | ||
*h = append(*h, x.(*NodeClaim)) | ||
} | ||
|
||
func (h *NodeClaimHeap) Pop() interface{} { | ||
old := *h | ||
n := len(old) | ||
item := old[n-1] | ||
*h = old[0 : n-1] | ||
return item | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
/* | ||
Copyright The Kubernetes Authors. | ||
Licensed 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 scheduling | ||
|
||
import ( | ||
"container/heap" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
corev1 "k8s.io/api/core/v1" | ||
) | ||
|
||
func TestNodeClaimHeap_PopOrder(t *testing.T) { | ||
// Create NodeClaims with different pod counts | ||
nc1 := &NodeClaim{Pods: []*corev1.Pod{{}, {}}} // 2 pods | ||
nc2 := &NodeClaim{Pods: []*corev1.Pod{{}}} // 1 pod | ||
nc3 := &NodeClaim{Pods: []*corev1.Pod{{}, {}, {}}} // 3 pods | ||
nc4 := &NodeClaim{Pods: []*corev1.Pod{}} // 0 pods | ||
|
||
// Initialize heap with NodeClaims | ||
h := NewNodeClaimHeap([]*NodeClaim{nc1, nc2, nc3, nc4}) | ||
heap.Init(h) | ||
|
||
// Pop items and verify they come out in ascending order of pod count | ||
expected := []*NodeClaim{nc4, nc2, nc1, nc3} | ||
|
||
for i := 0; i < len(expected); i++ { | ||
item := heap.Pop(h).(*NodeClaim) | ||
assert.Equal(t, len(expected[i].Pods), len(item.Pods), | ||
"Expected NodeClaim with %d pods, got %d pods", | ||
len(expected[i].Pods), len(item.Pods)) | ||
} | ||
|
||
// Verify heap is empty | ||
assert.Equal(t, 0, h.Len(), "Heap should be empty after popping all items") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters