From f1cb5b40d52b74bddc1194c676d4b65432972c55 Mon Sep 17 00:00:00 2001 From: Dmitrii Stepanov Date: Mon, 27 Jan 2025 16:46:38 +0300 Subject: [PATCH] [#1] mclock: Use `time.Duration` for idle timeout Signed-off-by: Dmitrii Stepanov --- scheduling/mclock.go | 7 ++++--- scheduling/mclock_bench_test.go | 2 +- scheduling/mclock_test.go | 10 +++++----- 3 files changed, 10 insertions(+), 9 deletions(-) diff --git a/scheduling/mclock.go b/scheduling/mclock.go index 4cd6ff1..d50e60e 100644 --- a/scheduling/mclock.go +++ b/scheduling/mclock.go @@ -6,6 +6,7 @@ import ( "errors" "math" "sync" + "time" ) const ( @@ -74,11 +75,11 @@ type MClock struct { // waitLimit maximum allowed count of waiting requests // for tags specified by tagInfo. The value of idleTimeout defines // the difference between the current time and the time of -// the previous request in seconds, at which the tag considered idle. +// the previous request, at which the tag considered idle. // If idleTimeout is negative, it means that there is no idle tags allowed. // If waitLimit equals zero, it means that there is no limit on the // number of waiting requests. -func NewMClock(runLimit, waitLimit uint64, tagInfo map[string]TagInfo, idleTimeout float64) (*MClock, error) { +func NewMClock(runLimit, waitLimit uint64, tagInfo map[string]TagInfo, idleTimeout time.Duration) (*MClock, error) { if err := validateParams(runLimit, tagInfo); err != nil { return nil, err } @@ -86,7 +87,7 @@ func NewMClock(runLimit, waitLimit uint64, tagInfo map[string]TagInfo, idleTimeo runLimit: runLimit, waitLimit: int(waitLimit), clock: newSystemClock(), - idleTimeout: idleTimeout, + idleTimeout: idleTimeout.Seconds(), tagInfo: tagInfo, reservationQueue: &queue{}, diff --git a/scheduling/mclock_bench_test.go b/scheduling/mclock_bench_test.go index f18f421..09989e6 100644 --- a/scheduling/mclock_bench_test.go +++ b/scheduling/mclock_bench_test.go @@ -56,7 +56,7 @@ func BenchmarkMClock(b *testing.B) { tagInfos["tag"+strconv.FormatInt(int64(tag), 10)] = TagInfo{Share: 50, LimitIOPS: limit, ReservedIOPS: reservation} } - mClockQ, _ := NewMClock(math.MaxUint64, math.MaxUint64, tagInfos, math.MaxFloat64) + mClockQ, _ := NewMClock(math.MaxUint64, math.MaxUint64, tagInfos, time.Hour) resStr := "no" if reservation != nil { diff --git a/scheduling/mclock_test.go b/scheduling/mclock_test.go index a0b0bd3..9cebe05 100644 --- a/scheduling/mclock_test.go +++ b/scheduling/mclock_test.go @@ -275,7 +275,7 @@ func TestMClockReservationScheduling(t *testing.T) { func TestMClockIdleTag(t *testing.T) { t.Parallel() reqCount := 100 - idleTimeout := 2.0 + idleTimeout := 2 * time.Second cl := &noopClock{} q, err := NewMClock(1, math.MaxUint64, map[string]TagInfo{ "class1": {Share: 1}, @@ -287,7 +287,7 @@ func TestMClockIdleTag(t *testing.T) { var requests []*request tag := "class1" for i := 0; i < reqCount/2; i++ { - cl.v += idleTimeout / 2 + cl.v += idleTimeout.Seconds() / 2 req, _, err := q.pushRequest(tag) require.NoError(t, err) requests = append(requests, req) @@ -295,7 +295,7 @@ func TestMClockIdleTag(t *testing.T) { // class1 requests have shares [1.0; 2.0; 3.0; ... ] - cl.v += 2 * idleTimeout + cl.v += 2 * idleTimeout.Seconds() tag = "class2" req, _, err := q.pushRequest(tag) @@ -424,7 +424,7 @@ func TestMClockParameterValidation(t *testing.T) { require.NoError(t, err) _, err = NewMClock(1, 1, map[string]TagInfo{ "class1": {Share: 1}, - }, float64(0)) + }, 0) require.NoError(t, err) negativeValue := -1.0 zeroValue := float64(0) @@ -466,7 +466,7 @@ func TestMClockTimeBasedSchedule(t *testing.T) { limit := 1.0 // 1 request per second allowed cl := &noopClock{v: float64(1.5)} q, err := NewMClock(100, math.MaxUint64, map[string]TagInfo{ - "class1": {Shares: 1, Limit: &limit}, + "class1": {Share: 1, LimitIOPS: &limit}, }, 100) require.NoError(t, err) defer q.Close()