Skip to content

Commit 5b4b7e3

Browse files
committed
change the interface
1 parent d04084d commit 5b4b7e3

24 files changed

+71
-80
lines changed

RATIONALE.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -223,6 +223,17 @@ result, not a parameter. The only impact would be to new plugins or those
223223
ported to WebAssembly. We do not expect limiting the scores to two billion
224224
above the valid range to be a practical concern for these authors.
225225

226+
### Why does the Permit function return a uint32 representing milliseconds for the timeout, not `time.Duration`?
227+
228+
`framework.PermitPlugin` returns `time.Duration` to represent the timeout.
229+
`time.Duration` is int64 underneath and 1 time.Duration represents 1 nanosecond.
230+
231+
Given the scheduling throughput in the upstream kube-scheduler is around 300 pods/s,
232+
that is, 3+ milliseconds per pod,
233+
we consider a millisecond-level timeout with uint32 to be sufficiently fine-grained.
234+
235+
Also, tha maximum timeout is around 24 days, which also should be large enough.
236+
226237
## Why do we return a non-status, second numeric result as an i32?
227238

228239
Most compilers that target WebAssembly Core Specification 1.0, the only REC

guest/api/types.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,6 @@
1717
package api
1818

1919
import (
20-
"time"
21-
2220
"sigs.k8s.io/kube-scheduler-wasm-extension/guest/api/proto"
2321
)
2422

@@ -130,7 +128,8 @@ type ReservePlugin interface {
130128
type PermitPlugin interface {
131129
Plugin
132130

133-
Permit(state CycleState, p proto.Pod, nodeName string) (*Status, time.Duration)
131+
// Note: This is uint32, not time.Duration. See /RATIONALE.md for why.
132+
Permit(state CycleState, p proto.Pod, nodeName string) (status *Status, timeoutMilliSeconds uint32)
134133
}
135134

136135
// PreBindPlugin is a WebAssembly implementation of framework.PreBindPlugin.

guest/internal/mem/mem.go

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -53,15 +53,6 @@ func BytesToPtr(b []byte) (uint32, uint32) {
5353
return uint32(uintptr(ptr)), uint32(len(b))
5454
}
5555

56-
// Int64ToPtr returns a pointer for the given int64 number in a way compatible
57-
// with WebAssembly numeric types.
58-
// The returned pointer aliases the number hence it must be kept alive until ptr
59-
// is no longer needed.
60-
func Int64ToPtr(n int64) uint32 {
61-
ptr := unsafe.Pointer(&n)
62-
return uint32(uintptr(ptr))
63-
}
64-
6556
// Update is for decoding values from memory. The updater doesn't keep a
6657
// reference to the underlying bytes, so we don't need to copy them.
6758
func Update(

guest/permit/imports.go

Lines changed: 0 additions & 6 deletions
This file was deleted.

guest/permit/imports_stub.go

Lines changed: 0 additions & 6 deletions
This file was deleted.

guest/permit/permit.go

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,9 @@
1818
package permit
1919

2020
import (
21-
"runtime"
22-
2321
"sigs.k8s.io/kube-scheduler-wasm-extension/guest/api"
2422
"sigs.k8s.io/kube-scheduler-wasm-extension/guest/internal/cyclestate"
2523
"sigs.k8s.io/kube-scheduler-wasm-extension/guest/internal/imports"
26-
"sigs.k8s.io/kube-scheduler-wasm-extension/guest/internal/mem"
2724
"sigs.k8s.io/kube-scheduler-wasm-extension/guest/internal/plugin"
2825
)
2926

@@ -42,7 +39,7 @@ var permit api.PermitPlugin
4239
//
4340
// type permitPlugin struct{}
4441
//
45-
// func (permitPlugin) Permit(state api.CycleState, p proto.Pod, nodeName string) (status *api.Status, timeout time.Duration)
42+
// func (permitPlugin) Permit(state api.CycleState, p proto.Pod, nodeName string) (status *api.Status, timeout uint32)
4643
// // Write state you need on Permit
4744
// }
4845
func SetPlugin(permitPlugin api.PermitPlugin) {
@@ -54,12 +51,12 @@ func SetPlugin(permitPlugin api.PermitPlugin) {
5451
}
5552

5653
// prevent unused lint errors (lint is run with normal go).
57-
var _ func() uint32 = _permit
54+
var _ func() uint64 = _permit
5855

5956
// _permit is only exported to the host.
6057
//
6158
//export permit
62-
func _permit() uint32 {
59+
func _permit() uint64 {
6360
if permit == nil { // Then, the user didn't define one.
6461
// Unlike most plugins we always export permit so that we can reset
6562
// the cycle state: return success to avoid no-op overhead.
@@ -70,9 +67,7 @@ func _permit() uint32 {
7067
nodeName := imports.NodeName()
7168
status, timeout := permit.Permit(cyclestate.Values, pod, nodeName)
7269

73-
ptr := mem.Int64ToPtr(int64(timeout))
74-
setTimeoutResult(ptr)
75-
runtime.KeepAlive(timeout) // untir ptr is no longer needed.
76-
77-
return imports.StatusToCode(status)
70+
// Pack the score and status code into a single WebAssembly 1.0 compatible
71+
// result
72+
return (uint64(imports.StatusToCode(status)) << uint64(32)) | uint64(timeout)
7873
}

guest/testdata/cyclestate/main.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@ package main
2020

2121
import (
2222
"os"
23-
"time"
2423
"unsafe"
2524

2625
"sigs.k8s.io/kube-scheduler-wasm-extension/guest/api"
@@ -190,7 +189,7 @@ func (statePlugin) Unreserve(state api.CycleState, pod proto.Pod, nodeName strin
190189
mustFilterState(state)
191190
}
192191

193-
func (statePlugin) Permit(state api.CycleState, pod proto.Pod, nodeName string) (status *api.Status, timeout time.Duration) {
192+
func (statePlugin) Permit(state api.CycleState, pod proto.Pod, nodeName string) (status *api.Status, timeout uint32) {
194193
mustFilterState(state)
195194
return
196195
}
-1.05 KB
Binary file not shown.

guest/testdata/permit/main.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,13 +35,13 @@ func main() {
3535

3636
type permitPlugin struct{}
3737

38-
func (permitPlugin) Permit(state api.CycleState, pod proto.Pod, nodeName string) (*api.Status, time.Duration) {
38+
func (permitPlugin) Permit(state api.CycleState, pod proto.Pod, nodeName string) (*api.Status, uint32) {
3939
status, timeout := api.StatusCodeSuccess, time.Duration(0)
4040
if nodeName == "bad" {
4141
status = api.StatusCodeError
4242
} else if nodeName == "wait" {
4343
status = api.StatusCodeWait
4444
timeout = 10 * time.Second
4545
}
46-
return &api.Status{Code: status, Reason: "name is " + nodeName}, timeout
46+
return &api.Status{Code: status, Reason: "name is " + nodeName}, uint32(timeout.Milliseconds())
4747
}

guest/testdata/permit/main.wasm

1023 Bytes
Binary file not shown.

0 commit comments

Comments
 (0)