Skip to content
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

use reservation resource units if they exist #242

Merged
merged 6 commits into from
May 15, 2024
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions bidengine/order.go
Original file line number Diff line number Diff line change
Expand Up @@ -372,9 +372,10 @@ loop:
pricech = runner.Do(metricsutils.ObserveRunner(func() runner.Result {
// Calculate price & bid
priceReq := Request{
Owner: group.GroupID.Owner,
GSpec: &group.GroupSpec,
PricePrecision: DefaultPricePrecision,
Owner: group.GroupID.Owner,
GSpec: &group.GroupSpec,
PricePrecision: DefaultPricePrecision,
AllocatedResources: reservation.GetAllocatedResources(),
}
return runner.NewResult(o.cfg.PricingStrategy.CalculatePrice(ctx, priceReq))
}, pricingDuration))
Expand Down
19 changes: 11 additions & 8 deletions bidengine/pricing.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,10 @@ import (
)

type Request struct {
Owner string `json:"owner"`
GSpec *dtypes.GroupSpec
PricePrecision int
Owner string `json:"owner"`
GSpec *dtypes.GroupSpec
AllocatedResources dtypes.ResourceUnits
PricePrecision int
}

const (
Expand Down Expand Up @@ -89,8 +90,8 @@ func MakeScalePricing(
memoryScale decimal.Decimal,
storageScale Storage,
endpointScale decimal.Decimal,
ipScale decimal.Decimal) (BidPricingStrategy, error) {

ipScale decimal.Decimal,
) (BidPricingStrategy, error) {
if cpuScale.IsZero() && memoryScale.IsZero() && storageScale.IsAnyZero() && endpointScale.IsZero() && ipScale.IsZero() {
return nil, errAllScalesZero
}
Expand Down Expand Up @@ -317,9 +318,11 @@ func calculatePriceRange(gspec *dtypes.GroupSpec) (sdk.DecCoin, sdk.DecCoin) {
return sdk.NewDecCoinFromDec(rmax.Denom, cmin), sdk.NewDecCoinFromDec(rmax.Denom, cmax)
}

var errPathEmpty = errors.New("script path cannot be the empty string")
var errProcessLimitZero = errors.New("process limit must be greater than zero")
var errProcessRuntimeLimitZero = errors.New("process runtime limit must be greater than zero")
var (
errPathEmpty = errors.New("script path cannot be the empty string")
errProcessLimitZero = errors.New("process limit must be greater than zero")
errProcessRuntimeLimitZero = errors.New("process runtime limit must be greater than zero")
)
andrewhare marked this conversation as resolved.
Show resolved Hide resolved

type storageElement struct {
Class string `json:"class"`
Expand Down
84 changes: 46 additions & 38 deletions bidengine/shellscript.go
Original file line number Diff line number Diff line change
Expand Up @@ -126,42 +126,11 @@ func parseStorage(resource atypes.Volumes) []storageElement {
return res
}

func (ssp shellScriptPricing) CalculatePrice(ctx context.Context, req Request) (sdk.DecCoin, error) {
buf := &bytes.Buffer{}

dataForScript := &dataForScript{
Resources: make([]dataForScriptElement, len(req.GSpec.Resources)),
Price: req.GSpec.Price(),
}

if req.PricePrecision > 0 {
dataForScript.PricePrecision = &req.PricePrecision
}

// iterate over everything & sum it up
for i, group := range req.GSpec.Resources {
groupCount := group.Count

cpuQuantity := parseCPU(group.Resources.CPU)
gpuQuantity := parseGPU(group.Resources.GPU)
memoryQuantity := parseMemory(group.Resources.Memory)
storageQuantity := parseStorage(group.Resources.Storage)
endpointQuantity := len(group.Resources.Endpoints)

dataForScript.Resources[i] = dataForScriptElement{
CPU: cpuQuantity,
GPU: gpuQuantity,
Memory: memoryQuantity,
Storage: storageQuantity,
Count: groupCount,
EndpointQuantity: endpointQuantity,
IPLeaseQuantity: util.GetEndpointQuantityOfResourceUnits(group.Resources, atypes.Endpoint_LEASED_IP),
}
}
func (ssp shellScriptPricing) CalculatePrice(ctx context.Context, r Request) (sdk.DecCoin, error) {
d := newDataForScript(r)

encoder := json.NewEncoder(buf)
err := encoder.Encode(dataForScript)
if err != nil {
buf := &bytes.Buffer{}
if err := json.NewEncoder(buf).Encode(&d); err != nil {
return sdk.DecCoin{}, err
}

Expand All @@ -181,14 +150,14 @@ func (ssp shellScriptPricing) CalculatePrice(ctx context.Context, req Request) (
stderrBuf := &bytes.Buffer{}
cmd.Stderr = stderrBuf

denom := req.GSpec.Price().Denom
denom := r.GSpec.Price().Denom

subprocEnv := os.Environ()
subprocEnv = append(subprocEnv, fmt.Sprintf("AKASH_OWNER=%s", req.Owner))
subprocEnv = append(subprocEnv, fmt.Sprintf("AKASH_OWNER=%s", r.Owner))
subprocEnv = append(subprocEnv, fmt.Sprintf("AKASH_DENOM=%s", denom))
cmd.Env = subprocEnv

err = cmd.Run()
err := cmd.Run()

if ctxErr := processCtx.Err(); ctxErr != nil {
return sdk.DecCoin{}, ctxErr
Expand Down Expand Up @@ -219,3 +188,42 @@ func (ssp shellScriptPricing) CalculatePrice(ctx context.Context, req Request) (

return sdk.NewDecCoinFromDec(denom, price), nil
}

func newDataForScript(r Request) dataForScript {
d := dataForScript{
Resources: make([]dataForScriptElement, len(r.GSpec.Resources)),
Price: r.GSpec.Price(),
}

if r.PricePrecision > 0 {
d.PricePrecision = &r.PricePrecision
}

resources := r.GSpec.Resources
if len(r.AllocatedResources) > 0 {
resources = r.AllocatedResources
}

// iterate over everything & sum it up
for i, group := range resources {
groupCount := group.Count

cpuQuantity := parseCPU(group.CPU)
gpuQuantity := parseGPU(group.GPU)
memoryQuantity := parseMemory(group.Memory)
storageQuantity := parseStorage(group.Storage)
endpointQuantity := len(group.Endpoints)

d.Resources[i] = dataForScriptElement{
CPU: cpuQuantity,
GPU: gpuQuantity,
Memory: memoryQuantity,
Storage: storageQuantity,
Count: groupCount,
EndpointQuantity: endpointQuantity,
IPLeaseQuantity: util.GetEndpointQuantityOfResourceUnits(group.Resources, atypes.Endpoint_LEASED_IP),
}
}

return d
}
Loading
Loading