From f4d8ebf13db79b7a3f6eba60ddca681c023d55fc Mon Sep 17 00:00:00 2001 From: Dmitrii Stepanov Date: Tue, 28 Jan 2025 11:35:54 +0300 Subject: [PATCH] [#1] mclock: Add assert package Signed-off-by: Dmitrii Stepanov --- internal/assert/cond.go | 9 +++++++++ scheduling/mclock.go | 34 ++++++++++------------------------ 2 files changed, 19 insertions(+), 24 deletions(-) create mode 100644 internal/assert/cond.go diff --git a/internal/assert/cond.go b/internal/assert/cond.go new file mode 100644 index 0000000..4a1b201 --- /dev/null +++ b/internal/assert/cond.go @@ -0,0 +1,9 @@ +package assert + +import "strings" + +func Cond(cond bool, details ...string) { + if !cond { + panic(strings.Join(details, " ")) + } +} diff --git a/scheduling/mclock.go b/scheduling/mclock.go index a38f6f3..82037d6 100644 --- a/scheduling/mclock.go +++ b/scheduling/mclock.go @@ -7,6 +7,8 @@ import ( "math" "sync" "time" + + "git.frostfs.info/TrueCloudLab/frostfs-qos/internal/assert" ) const ( @@ -172,9 +174,7 @@ func (q *MClock) dropRequest(req *request) { select { case <-req.scheduled: - if q.inProgress == 0 { - panic("invalid requests count") - } + assert.Cond(q.inProgress > 0, "invalid requests count") q.inProgress-- default: } @@ -199,9 +199,7 @@ func (q *MClock) pushRequest(tag string) (*request, ReleaseFunc, error) { return nil, nil, ErrMClockSchedulerUnknownTag } prev, ok := q.previous[tag] - if !ok { - panic("undefined previous: " + tag) - } + assert.Cond(ok, "undefined previous:", tag) if q.idleTimeout >= 0 && now-prev.ts > q.idleTimeout { // was inactive for q.idleTimeout q.adjustTags(now, tag) @@ -321,9 +319,7 @@ func (q *MClock) scheduleByLimitAndWeight(now float64) { q.removeFromQueues(next.r) tagInfo, ok := q.tagInfo[next.r.tag] - if !ok { - panic("unknown tag: " + next.r.tag) // must be checked on top level - } + assert.Cond(ok, "unknown tag:", next.r.tag) if tagInfo.ReservedIOPS != nil && hadReservation { var updated bool for _, i := range q.reservationQueue.items { @@ -390,24 +386,14 @@ func (q *MClock) requestCompleted() { return } - if q.inProgress == 0 { - panic("invalid requests count") - } + assert.Cond(q.inProgress > 0, "invalid requests count") q.inProgress-- q.scheduleRequestUnsafe() } func assertIndexInvalid(r *request) { - if r.limitIdx != invalidIndex { - panic("limitIdx is not -1") - } - if r.sharesIdx != invalidIndex { - panic("sharesIdx is not -1") - } - if r.reservationIdx != invalidIndex { - panic("reservationIdx is not -1") - } - if r.readyIdx != invalidIndex { - panic("readyIdx is not -1") - } + assert.Cond(r.limitIdx == invalidIndex, "limitIdx is not -1") + assert.Cond(r.sharesIdx == invalidIndex, "sharesIdx is not -1") + assert.Cond(r.reservationIdx == invalidIndex, "reservationIdx is not -1") + assert.Cond(r.readyIdx == invalidIndex, "readyIdx is not -1") }