Skip to content

Commit e2e5214

Browse files
authored
fix: use reservation resource units if they exist (#242)
1 parent f2ee3b7 commit e2e5214

File tree

4 files changed

+294
-49
lines changed

4 files changed

+294
-49
lines changed

bidengine/order.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -372,9 +372,10 @@ loop:
372372
pricech = runner.Do(metricsutils.ObserveRunner(func() runner.Result {
373373
// Calculate price & bid
374374
priceReq := Request{
375-
Owner: group.GroupID.Owner,
376-
GSpec: &group.GroupSpec,
377-
PricePrecision: DefaultPricePrecision,
375+
Owner: group.GroupID.Owner,
376+
GSpec: &group.GroupSpec,
377+
PricePrecision: DefaultPricePrecision,
378+
AllocatedResources: reservation.GetAllocatedResources(),
378379
}
379380
return runner.NewResult(o.cfg.PricingStrategy.CalculatePrice(ctx, priceReq))
380381
}, pricingDuration))

bidengine/pricing.go

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,10 @@ import (
1919
)
2020

2121
type Request struct {
22-
Owner string `json:"owner"`
23-
GSpec *dtypes.GroupSpec
24-
PricePrecision int
22+
Owner string `json:"owner"`
23+
GSpec *dtypes.GroupSpec
24+
AllocatedResources dtypes.ResourceUnits
25+
PricePrecision int
2526
}
2627

2728
const (
@@ -89,8 +90,8 @@ func MakeScalePricing(
8990
memoryScale decimal.Decimal,
9091
storageScale Storage,
9192
endpointScale decimal.Decimal,
92-
ipScale decimal.Decimal) (BidPricingStrategy, error) {
93-
93+
ipScale decimal.Decimal,
94+
) (BidPricingStrategy, error) {
9495
if cpuScale.IsZero() && memoryScale.IsZero() && storageScale.IsAnyZero() && endpointScale.IsZero() && ipScale.IsZero() {
9596
return nil, errAllScalesZero
9697
}
@@ -317,9 +318,11 @@ func calculatePriceRange(gspec *dtypes.GroupSpec) (sdk.DecCoin, sdk.DecCoin) {
317318
return sdk.NewDecCoinFromDec(rmax.Denom, cmin), sdk.NewDecCoinFromDec(rmax.Denom, cmax)
318319
}
319320

320-
var errPathEmpty = errors.New("script path cannot be the empty string")
321-
var errProcessLimitZero = errors.New("process limit must be greater than zero")
322-
var errProcessRuntimeLimitZero = errors.New("process runtime limit must be greater than zero")
321+
var (
322+
errPathEmpty = errors.New("script path cannot be the empty string")
323+
errProcessLimitZero = errors.New("process limit must be greater than zero")
324+
errProcessRuntimeLimitZero = errors.New("process runtime limit must be greater than zero")
325+
)
323326

324327
type storageElement struct {
325328
Class string `json:"class"`

bidengine/pricing_test.go

Lines changed: 233 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import (
1919

2020
sdk "github.com/cosmos/cosmos-sdk/types"
2121
"github.com/shopspring/decimal"
22+
"github.com/stretchr/testify/assert"
2223
"github.com/stretchr/testify/require"
2324

2425
dtypes "github.com/akash-network/akash-api/go/node/deployment/v1beta3"
@@ -731,3 +732,235 @@ func TestRationalToIntConversion(t *testing.T) {
731732
a := ceilBigRatToBigInt(big.NewRat(3, 2))
732733
require.Equal(t, big.NewInt(2), a)
733734
}
735+
736+
func Test_parseGPU_LastAttribute(t *testing.T) {
737+
gpu := atypes.GPU{
738+
Units: atypes.ResourceValue{
739+
Val: sdk.NewInt(111),
740+
},
741+
Attributes: atypes.Attributes{
742+
{
743+
Key: "vendor/nvidia/model/a100",
744+
Value: "true",
745+
},
746+
{
747+
Key: "vendor/nvidia/model/v100",
748+
Value: "true",
749+
},
750+
{
751+
Key: "vendor/nvidia/model/h100",
752+
Value: "true",
753+
},
754+
},
755+
}
756+
757+
e := parseGPU(&gpu)
758+
759+
v, ok := e.Attributes.Vendor["nvidia"]
760+
require.True(t, ok)
761+
762+
assert.Equal(t, "h100", v.Model)
763+
}
764+
765+
func Test_newDataForScript_GPUWildcard(t *testing.T) {
766+
cases := []struct {
767+
desc string
768+
r Request
769+
gpu gpuElement
770+
}{
771+
{
772+
desc: "wildcard and allocated resources",
773+
r: Request{
774+
GSpec: &dtypes.GroupSpec{
775+
Resources: dtypes.ResourceUnits{
776+
{
777+
Price: sdk.NewDecCoin("denom", sdk.NewInt(111)),
778+
Resources: atypes.Resources{
779+
CPU: &atypes.CPU{
780+
Units: atypes.ResourceValue{
781+
Val: sdk.NewInt(111),
782+
},
783+
},
784+
Memory: &atypes.Memory{
785+
Quantity: atypes.ResourceValue{
786+
Val: sdk.NewInt(111),
787+
},
788+
},
789+
GPU: &atypes.GPU{
790+
Units: atypes.ResourceValue{
791+
Val: sdk.NewInt(111),
792+
},
793+
Attributes: atypes.Attributes{
794+
{
795+
Key: "vendor/nvidia/model/*",
796+
Value: "true",
797+
},
798+
},
799+
},
800+
},
801+
},
802+
},
803+
},
804+
AllocatedResources: dtypes.ResourceUnits{
805+
{
806+
Resources: atypes.Resources{
807+
ID: 111,
808+
CPU: &atypes.CPU{
809+
Units: atypes.ResourceValue{
810+
Val: sdk.NewInt(111),
811+
},
812+
},
813+
Memory: &atypes.Memory{
814+
Quantity: atypes.ResourceValue{
815+
Val: sdk.NewInt(111),
816+
},
817+
},
818+
GPU: &atypes.GPU{
819+
Units: atypes.ResourceValue{
820+
Val: sdk.NewInt(111),
821+
},
822+
Attributes: atypes.Attributes{
823+
{
824+
Key: "vendor/nvidia/model/a100",
825+
Value: "true",
826+
},
827+
},
828+
},
829+
},
830+
},
831+
},
832+
},
833+
gpu: gpuElement{
834+
Units: 111,
835+
Attributes: gpuAttributes{
836+
Vendor: map[string]gpuVendorAttributes{
837+
"nvidia": {Model: "a100"},
838+
},
839+
},
840+
},
841+
},
842+
{
843+
desc: "wildcard and no reservation value",
844+
r: Request{
845+
GSpec: &dtypes.GroupSpec{
846+
Resources: dtypes.ResourceUnits{
847+
{
848+
Price: sdk.NewDecCoin("denom", sdk.NewInt(111)),
849+
Resources: atypes.Resources{
850+
CPU: &atypes.CPU{
851+
Units: atypes.ResourceValue{
852+
Val: sdk.NewInt(111),
853+
},
854+
},
855+
Memory: &atypes.Memory{
856+
Quantity: atypes.ResourceValue{
857+
Val: sdk.NewInt(111),
858+
},
859+
},
860+
GPU: &atypes.GPU{
861+
Units: atypes.ResourceValue{
862+
Val: sdk.NewInt(111),
863+
},
864+
Attributes: atypes.Attributes{
865+
{
866+
Key: "vendor/nvidia/model/*",
867+
Value: "true",
868+
},
869+
},
870+
},
871+
},
872+
},
873+
},
874+
},
875+
},
876+
gpu: gpuElement{
877+
Units: 111,
878+
Attributes: gpuAttributes{
879+
Vendor: map[string]gpuVendorAttributes{
880+
"nvidia": {Model: "*"},
881+
},
882+
},
883+
},
884+
},
885+
{
886+
desc: "no wildcard and reservation value",
887+
r: Request{
888+
GSpec: &dtypes.GroupSpec{
889+
Resources: dtypes.ResourceUnits{
890+
{
891+
Price: sdk.NewDecCoin("denom", sdk.NewInt(111)),
892+
Resources: atypes.Resources{
893+
CPU: &atypes.CPU{
894+
Units: atypes.ResourceValue{
895+
Val: sdk.NewInt(111),
896+
},
897+
},
898+
Memory: &atypes.Memory{
899+
Quantity: atypes.ResourceValue{
900+
Val: sdk.NewInt(111),
901+
},
902+
},
903+
GPU: &atypes.GPU{
904+
Units: atypes.ResourceValue{
905+
Val: sdk.NewInt(111),
906+
},
907+
Attributes: atypes.Attributes{
908+
{
909+
Key: "vendor/nvidia/model/h100",
910+
Value: "true",
911+
},
912+
},
913+
},
914+
},
915+
},
916+
},
917+
},
918+
AllocatedResources: dtypes.ResourceUnits{
919+
{
920+
Resources: atypes.Resources{
921+
ID: 111,
922+
CPU: &atypes.CPU{
923+
Units: atypes.ResourceValue{
924+
Val: sdk.NewInt(111),
925+
},
926+
},
927+
Memory: &atypes.Memory{
928+
Quantity: atypes.ResourceValue{
929+
Val: sdk.NewInt(111),
930+
},
931+
},
932+
GPU: &atypes.GPU{
933+
Units: atypes.ResourceValue{
934+
Val: sdk.NewInt(111),
935+
},
936+
Attributes: atypes.Attributes{
937+
{
938+
Key: "vendor/nvidia/model/a100",
939+
Value: "true",
940+
},
941+
},
942+
},
943+
},
944+
},
945+
},
946+
},
947+
gpu: gpuElement{
948+
Units: 111,
949+
Attributes: gpuAttributes{
950+
Vendor: map[string]gpuVendorAttributes{
951+
"nvidia": {Model: "h100"},
952+
},
953+
},
954+
},
955+
},
956+
}
957+
958+
for _, c := range cases {
959+
c := c
960+
961+
t.Run(c.desc, func(t *testing.T) {
962+
d := newDataForScript(c.r)
963+
assert.NotEmpty(t, d)
964+
})
965+
}
966+
}

0 commit comments

Comments
 (0)