Skip to content

Commit

Permalink
fix: correct total tasks when sharding is enabled
Browse files Browse the repository at this point in the history
  • Loading branch information
WangYihang committed Mar 14, 2024
1 parent ac5fa2c commit d25d34f
Showing 1 changed file with 23 additions and 2 deletions.
25 changes: 23 additions & 2 deletions scheduler.go
Original file line number Diff line number Diff line change
Expand Up @@ -153,9 +153,30 @@ func (s *Scheduler) SetMaxRuntimePerTaskSeconds(maxRuntimePerTaskSeconds int) *S
return s
}

// SetTotalTasks sets the total number of tasks
func (s *Scheduler) SetTotalTasks(numTotalTasks int64) *Scheduler {
s.NumTotalTasks.Store(numTotalTasks)
// Check if NumShards is set and is greater than 0
if s.NumShards <= 0 {
panic("NumShards must be greater than 0")
}

// Check if Shard is set and is within the valid range [0, NumShards)
if s.Shard < 0 || s.Shard >= s.NumShards {
panic("Shard must be within the range [0, NumShards)")
}

// Calculate the base number of tasks per shard
baseTasksPerShard := numTotalTasks / int64(s.NumShards)

// Calculate the remainder
remainder := numTotalTasks % int64(s.NumShards)

// Adjust task count for shards that need to handle an extra task due to the remainder
if int64(s.Shard) < remainder {
baseTasksPerShard++
}

// Store the number of tasks for this shard
s.NumTotalTasks.Store(baseTasksPerShard)
return s
}

Expand Down

0 comments on commit d25d34f

Please sign in to comment.