forked from armadaproject/armada
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Scheduler: efficient resource implementation (armadaproject#3561)
- Loading branch information
1 parent
586efd5
commit 7f2085d
Showing
27 changed files
with
408 additions
and
36 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
package internaltypes | ||
|
||
import ( | ||
"k8s.io/apimachinery/pkg/api/resource" | ||
) | ||
|
||
func QuantityToInt64RoundUp(q resource.Quantity, scale resource.Scale) int64 { | ||
return q.ScaledValue(scale) | ||
} | ||
|
||
func QuantityToInt64RoundDown(q resource.Quantity, scale resource.Scale) int64 { | ||
result := q.ScaledValue(scale) | ||
q2 := resource.NewScaledQuantity(result, scale) | ||
if q2.Cmp(q) > 0 { | ||
result-- | ||
} | ||
return result | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
package internaltypes | ||
|
||
import ( | ||
"testing" | ||
|
||
"gopkg.in/inf.v0" | ||
|
||
"github.com/stretchr/testify/assert" | ||
"k8s.io/apimachinery/pkg/api/resource" | ||
) | ||
|
||
type quantityTest struct { | ||
q resource.Quantity | ||
expectedIntRoundDown int64 | ||
expectedIntRoundUp int64 | ||
} | ||
|
||
func TestQuantityToInt64_WithScaleMillis(t *testing.T) { | ||
tests := []quantityTest{ | ||
{resource.MustParse("0"), 0, 0}, | ||
{resource.MustParse("1"), 1000, 1000}, | ||
{resource.MustParse("1m"), 1, 1}, | ||
{resource.MustParse("50m"), 50, 50}, | ||
{resource.MustParse("1Mi"), 1024 * 1024 * 1000, 1024 * 1024 * 1000}, | ||
{resource.MustParse("1e3"), 1000 * 1000, 1000 * 1000}, | ||
{*resource.NewMilliQuantity(1, resource.DecimalExponent), 1, 1}, | ||
{*resource.NewDecimalQuantity(*inf.NewDec(1, inf.Scale(0)), resource.DecimalExponent), 1000, 1000}, | ||
{resource.MustParse("1n"), 0, 1}, | ||
{resource.MustParse("100n"), 0, 1}, | ||
{resource.MustParse("999999999n"), 999, 1000}, | ||
{resource.MustParse("1000000000n"), 1000, 1000}, | ||
{resource.MustParse("1000000001n"), 1000, 1001}, | ||
{resource.MustParse("12.3m"), 12, 13}, | ||
{resource.MustParse("0.99m"), 0, 1}, | ||
{resource.MustParse("1.001m"), 1, 2}, | ||
} | ||
|
||
for _, test := range tests { | ||
assert.Equal(t, test.expectedIntRoundDown, QuantityToInt64RoundDown(test.q, resource.Milli), test.q) | ||
assert.Equal(t, test.expectedIntRoundUp, QuantityToInt64RoundUp(test.q, resource.Milli), test.q) | ||
} | ||
} | ||
|
||
func TestQuantityToInt64_WithUnitScale(t *testing.T) { | ||
const tebi = 1024 * 1024 * 1024 * 1024 | ||
tests := []quantityTest{ | ||
{resource.MustParse("0"), 0, 0}, | ||
{resource.MustParse("1"), 1, 1}, | ||
{resource.MustParse("1m"), 0, 1}, | ||
{resource.MustParse("50m"), 0, 1}, | ||
{resource.MustParse("1Mi"), 1024 * 1024, 1024 * 1024}, | ||
{resource.MustParse("1.5Mi"), 1536 * 1024, 1536 * 1024}, | ||
{resource.MustParse("4Ti"), 4 * tebi, 4 * tebi}, | ||
{resource.MustParse("100000Ti"), 100000 * tebi, 100000 * tebi}, | ||
{resource.MustParse("1e3"), 1000, 1000}, | ||
{*resource.NewMilliQuantity(1, resource.DecimalExponent), 0, 1}, | ||
{*resource.NewDecimalQuantity(*inf.NewDec(1, inf.Scale(0)), resource.DecimalExponent), 1, 1}, | ||
{resource.MustParse("1n"), 0, 1}, | ||
{resource.MustParse("100n"), 0, 1}, | ||
{resource.MustParse("999999999n"), 0, 1}, | ||
{resource.MustParse("1000000000n"), 1, 1}, | ||
{resource.MustParse("1000000001n"), 1, 2}, | ||
} | ||
|
||
for _, test := range tests { | ||
assert.Equal(t, test.expectedIntRoundDown, QuantityToInt64RoundDown(test.q, resource.Scale(0)), test.q) | ||
assert.Equal(t, test.expectedIntRoundUp, QuantityToInt64RoundUp(test.q, resource.Scale(0)), test.q) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
package internaltypes | ||
|
||
import "fmt" | ||
|
||
type ResourceList struct { | ||
resources []int64 | ||
factory *ResourceListFactory | ||
} | ||
|
||
func (rl *ResourceList) GetByName(name string) (int64, error) { | ||
index, ok := rl.factory.nameToIndex[name] | ||
if !ok { | ||
return 0, fmt.Errorf("resource type %s not found", name) | ||
} | ||
return rl.resources[index], nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
package internaltypes | ||
|
||
import ( | ||
"fmt" | ||
"math" | ||
|
||
"github.com/pkg/errors" | ||
|
||
k8sResource "k8s.io/apimachinery/pkg/api/resource" | ||
|
||
"github.com/armadaproject/armada/internal/armada/configuration" | ||
"github.com/armadaproject/armada/internal/scheduler/schedulerobjects" | ||
) | ||
|
||
type ResourceListFactory struct { | ||
nameToIndex map[string]int | ||
indexToName []string | ||
scales []k8sResource.Scale | ||
} | ||
|
||
func MakeResourceListFactory(supportedResourceTypes []configuration.ResourceType) (*ResourceListFactory, error) { | ||
if len(supportedResourceTypes) == 0 { | ||
return nil, errors.New("no resource types configured") | ||
} | ||
indexToName := make([]string, len(supportedResourceTypes)) | ||
nameToIndex := make(map[string]int, len(supportedResourceTypes)) | ||
scales := make([]k8sResource.Scale, len(supportedResourceTypes)) | ||
for i, t := range supportedResourceTypes { | ||
if dup, exists := nameToIndex[t.Name]; exists { | ||
return nil, fmt.Errorf("duplicate resource type name %q", dup) | ||
} | ||
nameToIndex[t.Name] = i | ||
indexToName[i] = t.Name | ||
scales[i] = resolutionToScale(t.Resolution) | ||
} | ||
return &ResourceListFactory{ | ||
indexToName: indexToName, | ||
nameToIndex: nameToIndex, | ||
scales: scales, | ||
}, nil | ||
} | ||
|
||
// Convert resolution to a k8sResource.Scale | ||
// e.g. | ||
// 1 -> 0 | ||
// 0.001 -> -3 | ||
// 1000 -> 3 | ||
func resolutionToScale(resolution k8sResource.Quantity) k8sResource.Scale { | ||
if resolution.Sign() < 1 { | ||
return k8sResource.Milli | ||
} | ||
return k8sResource.Scale(math.Floor(math.Log10(resolution.AsApproximateFloat64()))) | ||
} | ||
|
||
// Ignore unknown resources, round down. | ||
func (factory *ResourceListFactory) FromNodeProto(resources schedulerobjects.ResourceList) ResourceList { | ||
result := make([]int64, len(factory.indexToName)) | ||
for k, v := range resources.Resources { | ||
index, ok := factory.nameToIndex[k] | ||
if ok { | ||
result[index] = QuantityToInt64RoundDown(v, factory.scales[index]) | ||
} | ||
} | ||
return ResourceList{resources: result, factory: factory} | ||
} | ||
|
||
// Fail on unknown resources, round up. | ||
func (factory *ResourceListFactory) FromJobProto(resources schedulerobjects.ResourceList) (ResourceList, error) { | ||
result := make([]int64, len(factory.indexToName)) | ||
for k, v := range resources.Resources { | ||
index, ok := factory.nameToIndex[k] | ||
if ok { | ||
result[index] = QuantityToInt64RoundUp(v, factory.scales[index]) | ||
} else { | ||
return ResourceList{}, fmt.Errorf("unknown resource type %q", k) | ||
} | ||
} | ||
return ResourceList{resources: result, factory: factory}, nil | ||
} | ||
|
||
func (factory *ResourceListFactory) SummaryString() string { | ||
result := "" | ||
for i, name := range factory.indexToName { | ||
if i > 0 { | ||
result += " " | ||
} | ||
scale := factory.scales[i] | ||
resolution := k8sResource.NewScaledQuantity(1, scale) | ||
maxValue := k8sResource.NewScaledQuantity(math.MaxInt64, scale) | ||
result += fmt.Sprintf("%s (scale %v, resolution %v, maxValue %f)", name, scale, resolution, maxValue.AsApproximateFloat64()) | ||
} | ||
return result | ||
} |
Oops, something went wrong.