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

Some more simulator improvements #4007

Merged
merged 2 commits into from
Oct 16, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
37 changes: 0 additions & 37 deletions internal/scheduler/simulator/runner.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,50 +5,13 @@ import (
"path/filepath"
"strings"

"github.com/mattn/go-zglob"
"github.com/pkg/errors"
"github.com/spf13/viper"

commonconfig "github.com/armadaproject/armada/internal/common/config"
"github.com/armadaproject/armada/internal/scheduler/configuration"
)

func SchedulingConfigsByFilePathFromPattern(pattern string) (map[string]configuration.SchedulingConfig, error) {
filePaths, err := zglob.Glob(pattern)
if err != nil {
return nil, errors.WithStack(err)
}
filePathConfigMap := make(map[string]configuration.SchedulingConfig)
for _, path := range filePaths {
config, err := SchedulingConfigsFromFilePaths(filePaths)
if err != nil {
return nil, err
}
filePathConfigMap[path] = config[0]
}
return filePathConfigMap, nil
}

func SchedulingConfigsFromPattern(pattern string) ([]configuration.SchedulingConfig, error) {
filePaths, err := zglob.Glob(pattern)
if err != nil {
return nil, errors.WithStack(err)
}
return SchedulingConfigsFromFilePaths(filePaths)
}

func SchedulingConfigsFromFilePaths(filePaths []string) ([]configuration.SchedulingConfig, error) {
rv := make([]configuration.SchedulingConfig, len(filePaths))
for i, filePath := range filePaths {
config, err := SchedulingConfigFromFilePath(filePath)
if err != nil {
return nil, err
}
rv[i] = config
}
return rv, nil
}

func SchedulingConfigFromFilePath(filePath string) (configuration.SchedulingConfig, error) {
config := configuration.SchedulingConfig{}
v := viper.NewWithOptions(viper.KeyDelimiter("::"))
Expand Down
58 changes: 24 additions & 34 deletions internal/scheduler/simulator/simulator.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@ package simulator
import (
"container/heap"
"fmt"
"math"
"math/rand"
"sync/atomic"
"time"

"github.com/gogo/protobuf/proto"
"github.com/pkg/errors"
"golang.org/x/exp/maps"
"golang.org/x/exp/slices"
Expand Down Expand Up @@ -38,6 +38,8 @@ import (
"github.com/armadaproject/armada/pkg/armadaevents"
)

var epochStart = time.Unix(0, 0).UTC()

// Simulator captures the parameters and state of the Armada simulator.
type Simulator struct {
ClusterSpec *ClusterSpec
Expand Down Expand Up @@ -72,10 +74,9 @@ type Simulator struct {
// Simulated events are emitted on these event channels.
// Create a channel by calling s.StateTransitions() before running the simulator.
stateTransitionChannels []chan model.StateTransition
// Global job scheduling rate-limiter.
// Global job scheduling rate-limiter. Note that this will always be set to unlimited as we do not yet support
// effective rate limiting based on simulated time.
limiter *rate.Limiter
// Per-queue job scheduling rate-limiters.
limiterByQueue map[string]*rate.Limiter
// Used to generate random numbers from a chosen seed.
rand *rand.Rand
// Used to ensure each job is given a unique time stamp.
Expand Down Expand Up @@ -119,8 +120,6 @@ func NewSimulator(
return nil, err
}

clusterSpec = proto.Clone(clusterSpec).(*ClusterSpec)
workloadSpec = proto.Clone(workloadSpec).(*WorkloadSpec)
initialiseWorkloadSpec(workloadSpec)
if err := validateClusterSpec(clusterSpec); err != nil {
return nil, err
Expand Down Expand Up @@ -152,22 +151,17 @@ func NewSimulator(
allocationByPoolAndQueueAndPriorityClass: make(map[string]map[string]schedulerobjects.QuantityByTAndResourceType[string]),
demandByQueue: make(map[string]schedulerobjects.ResourceList),
totalResourcesByPool: make(map[string]schedulerobjects.ResourceList),
limiter: rate.NewLimiter(
rate.Limit(schedulingConfig.MaximumSchedulingRate),
schedulingConfig.MaximumSchedulingBurst,
),
limiterByQueue: make(map[string]*rate.Limiter),
rand: rand.New(rand.NewSource(randomSeed)),
resourceListFactory: resourceListFactory,
enableFastForward: enableFastForward,
hardTerminationMinutes: hardTerminationMinutes,
schedulerCyclePeriodSeconds: schedulerCyclePeriodSeconds,
floatingResourceTypes: floatingResourceTypes,
time: time.Unix(0, 0).UTC(),
sink: sink,
limiter: rate.NewLimiter(rate.Inf, math.MaxInt), // Unlimited
rand: rand.New(rand.NewSource(randomSeed)),
resourceListFactory: resourceListFactory,
enableFastForward: enableFastForward,
hardTerminationMinutes: hardTerminationMinutes,
schedulerCyclePeriodSeconds: schedulerCyclePeriodSeconds,
floatingResourceTypes: floatingResourceTypes,
time: epochStart,
sink: sink,
}
jobDb.SetClock(s)
s.limiter.SetBurstAt(s.time, schedulingConfig.MaximumSchedulingBurst)
if err := s.setupClusters(); err != nil {
return nil, err
}
Expand Down Expand Up @@ -204,7 +198,7 @@ func (s *Simulator) Run(ctx *armadacontext.Context) error {
ctx.Infof("No termination time set, will run until all workloads have completed")
}

lastLogTime := s.time
lastLogTime := time.Now()
// Then run the scheduler until all jobs have completed.
for s.eventLog.Len() > 0 {
select {
Expand All @@ -216,7 +210,7 @@ func (s *Simulator) Run(ctx *armadacontext.Context) error {
return err
}
}
if s.time.Unix()-lastLogTime.Unix() >= 60 {
if time.Now().Unix()-lastLogTime.Unix() >= 15 {
ctx.Infof("Simulator time %s", s.time)
lastLogTime = s.time
}
Expand Down Expand Up @@ -512,22 +506,13 @@ func (s *Simulator) handleScheduleEvent(ctx *armadacontext.Context) error {
// To ensure fair share is computed only from active queues, i.e., queues with jobs queued or running.
continue
}
limiter, ok := s.limiterByQueue[queue.Name]
if !ok {
limiter = rate.NewLimiter(
rate.Limit(s.schedulingConfig.MaximumPerQueueSchedulingRate),
s.schedulingConfig.MaximumPerQueueSchedulingBurst,
)
limiter.SetBurstAt(s.time, s.schedulingConfig.MaximumPerQueueSchedulingBurst)
s.limiterByQueue[queue.Name] = limiter
}
err := sctx.AddQueueSchedulingContext(
queue.Name,
queue.Weight,
s.allocationByPoolAndQueueAndPriorityClass[pool][queue.Name],
demand,
demand,
limiter,
s.limiter,
)
if err != nil {
return err
Expand Down Expand Up @@ -630,12 +615,17 @@ func (s *Simulator) handleScheduleEvent(ctx *armadacontext.Context) error {
}

// Update event timestamps to be consistent with simulated time.
t := s.time
for _, eventSequence := range eventSequences {
for _, event := range eventSequence.Events {
event.Created = protoutil.ToTimestamp(t)
event.Created = protoutil.ToTimestamp(s.time)
}
}

// If nothing changed, we're in steady state and can safely skip scheduling until something external has changed.
// Do this only if a non-zero amount of time has passed.
if !s.time.Equal(epochStart) && len(result.ScheduledJobs) == 0 && len(result.PreemptedJobs) == 0 {
s.shouldSchedule = false
}
}
txn.Commit()

Expand Down
33 changes: 0 additions & 33 deletions internal/scheduler/simulator/simulator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -484,39 +484,6 @@ func TestSimulator(t *testing.T) {
}
}

func TestSchedulingConfigsFromPattern(t *testing.T) {
actual, err := SchedulingConfigsFromPattern("./testdata/configs/basicSchedulingConfig.yaml")
require.NoError(t, err)
expected := []configuration.SchedulingConfig{GetBasicSchedulingConfig()}
assert.Equal(t, expected, actual)
}

//func TestClusterSpecsFromPattern(t *testing.T) {
// clusterSpecs, err := ClusterSpecsFromPattern("./testdata/clusters/tinyCluster.yaml")
// require.NoError(t, err)
// assert.Equal(t, []*ClusterSpec{GetTwoPoolTwoNodeCluster()}, clusterSpecs)
// require.NoError(t, err)
//}
//
//func TestWorkloadsFromPattern(t *testing.T) {
// workloadSpecs, err := WorkloadsFromPattern("./testdata/workloads/basicWorkload.yaml")
// require.NoError(t, err)
// assert.Equal(t, []*WorkloadSpec{GetOneQueue10JobWorkload()}, workloadSpecs)
// require.NoError(t, err)
//}
//
//func TestClusterSpecTotalResources(t *testing.T) {
// actual := GetTwoPoolTwoNodeCluster().TotalResources()
// expected := schedulerobjects.ResourceList{
// Resources: map[string]resource.Quantity{
// "cpu": resource.MustParse("160"),
// "memory": resource.MustParse("4352Gi"),
// "nvidia.com/gpu": resource.MustParse("8"),
// },
// }
// assert.True(t, expected.Equal(actual), "expected %s, but got %s", expected.CompactString(), actual.CompactString())
//}

func TestGenerateRandomShiftedExponentialDuration(t *testing.T) {
assert.Equal(
t,
Expand Down