generated from kubernetes/kubernetes-template-project
-
Notifications
You must be signed in to change notification settings - Fork 22
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
fix: repear scheduler_perf to run correctly #116
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
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 |
---|---|---|
|
@@ -85,4 +85,4 @@ | |
params: | ||
initNodes: 500 | ||
initPods: 500 | ||
measurePods: 1000 | ||
measurePods: 1000 |
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,4 @@ | ||
# NodeNumber Plugin | ||
|
||
This is the nodenumber example wasm plugin, which only implements PreScore and Score. | ||
It doesn't use any additional host functions (klog, handle, etc) so that scheduler_perf can measure the overhead truely. |
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,136 @@ | ||
/* | ||
Copyright 2023 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 main is the entrypoint of the %.wasm file, compiled with | ||
// '-target=wasi'. See /guest/RATIONALE.md for details. | ||
package main | ||
|
||
import ( | ||
"encoding/json" | ||
"fmt" | ||
|
||
"sigs.k8s.io/kube-scheduler-wasm-extension/guest/api" | ||
"sigs.k8s.io/kube-scheduler-wasm-extension/guest/api/proto" | ||
"sigs.k8s.io/kube-scheduler-wasm-extension/guest/config" | ||
"sigs.k8s.io/kube-scheduler-wasm-extension/guest/klog" | ||
klogapi "sigs.k8s.io/kube-scheduler-wasm-extension/guest/klog/api" | ||
"sigs.k8s.io/kube-scheduler-wasm-extension/guest/plugin" | ||
) | ||
|
||
// main is compiled to a WebAssembly function named "_start", called by the | ||
// wasm scheduler plugin during initialization. | ||
func main() { | ||
p, err := New(klog.Get(), config.Get()) | ||
if err != nil { | ||
panic(err) | ||
} | ||
plugin.Set(p) | ||
} | ||
|
||
func New(klog klogapi.Klog, jsonConfig []byte) (api.Plugin, error) { | ||
var args nodeNumberArgs | ||
if jsonConfig != nil { | ||
if err := json.Unmarshal(jsonConfig, &args); err != nil { | ||
panic(fmt.Errorf("decode arg into NodeNumberArgs: %w", err)) | ||
} | ||
klog.Info("NodeNumberArgs is successfully applied") | ||
} | ||
return &NodeNumber{reverse: args.Reverse}, nil | ||
} | ||
|
||
// NodeNumber is an example plugin that favors nodes that share a numerical | ||
// suffix with the pod name. | ||
// | ||
// For example, when a pod named "Pod1" is scheduled, a node named "Node1" gets | ||
// a higher score than a node named "Node9". | ||
// | ||
// # Notes | ||
// | ||
// - Only the last character in names are considered. This means "Node99" is | ||
// treated the same as "Node9" | ||
// - The reverse field inverts the score. For example, when `reverse == true` | ||
// a numeric match gets a results in a lower score than a match. | ||
type NodeNumber struct { | ||
reverse bool | ||
} | ||
|
||
type nodeNumberArgs struct { | ||
Reverse bool `json:"reverse"` | ||
} | ||
|
||
const ( | ||
// Name is the name of the plugin used in the plugin registry and configurations. | ||
Name = "NodeNumber" | ||
preScoreStateKey = "PreScore" + Name | ||
) | ||
|
||
// preScoreState computed at PreScore and used at Score. | ||
type preScoreState struct { | ||
podSuffixNumber uint8 | ||
} | ||
|
||
// EventsToRegister implements api.EnqueueExtensions | ||
func (pl *NodeNumber) EventsToRegister() []api.ClusterEvent { | ||
return []api.ClusterEvent{ | ||
{Resource: api.Node, ActionType: api.Add}, | ||
} | ||
} | ||
|
||
// PreScore implements api.PreScorePlugin | ||
func (pl *NodeNumber) PreScore(state api.CycleState, pod proto.Pod, _ proto.NodeList) *api.Status { | ||
podnum, ok := lastNumber(pod.Spec().GetNodeName()) | ||
if !ok { | ||
return nil // return success even if its suffix is non-number. | ||
} | ||
state.Write(preScoreStateKey, &preScoreState{podSuffixNumber: podnum}) | ||
return nil | ||
} | ||
|
||
// Score implements api.ScorePlugin | ||
func (pl *NodeNumber) Score(state api.CycleState, pod proto.Pod, nodeName string) (int32, *api.Status) { | ||
var match bool | ||
if data, ok := state.Read(preScoreStateKey); ok { | ||
// Match is when there is a last digit, and it is the pod suffix. | ||
nodenum, ok := lastNumber(nodeName) | ||
match = ok && data.(*preScoreState).podSuffixNumber == nodenum | ||
} else { | ||
// Match is also when there is no pod spec node name. | ||
match = true | ||
} | ||
|
||
if pl.reverse { | ||
match = !match // invert the condition. | ||
} | ||
|
||
if match { | ||
return 10, nil | ||
} | ||
return 0, nil | ||
} | ||
|
||
// lastNumber returns the last number in the string or false. | ||
func lastNumber(str string) (uint8, bool) { | ||
if len(str) == 0 { | ||
return 0, false | ||
} | ||
|
||
// We have at least a single character name. See if the last is a digit. | ||
lastChar := str[len(str)-1] | ||
if '0' <= lastChar && lastChar <= '9' { | ||
return lastChar - '0', true | ||
} | ||
return 0, false | ||
} |
Binary file not shown.
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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit: What if the length of
normalizedScoreList
is not equal to the length ofnodeScoreList
?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We return the error:
https://github.com/kubernetes-sigs/kube-scheduler-wasm-extension/blob/main/scheduler/plugin/plugin.go#L341-L343
We shouldn't restore the score list in that case because if the length is unmatched, that's the bug in the guest. (The guest filled in an invalid node score list)
We should error out, rather than hiding the bug.
And, what we're trying to do here is not hide the bug. When the guest doesn't implement the normalizescore, we just return without doing anything, which results in an empty
resultNormalizedScoreList
, and hence fail.https://github.com/kubernetes-sigs/kube-scheduler-wasm-extension/blob/main/guest/scoreextensions/scoreextensions.go#L64-L68
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Make sense. Thank you so much for your explanation!