Skip to content

Commit

Permalink
fix(policy): compute desired number of matches before rooms (#654)
Browse files Browse the repository at this point in the history
If we first divide the number of running matches per maxMatches
we might end up with a number between 0 and 1, which is quirky
when using math.Ceil because it would propagate the error to when
computing number of desired rooms. To simplify and fix this, instead
we now compute how many matches is the desired and then convert
the final number to the amount of rooms/instances/pods needed to
provide this number of matches:

1. How many running matches we have?
* Get the value from the redis: scheduler:<scheduler_name>:occupancy
2. How many matches do we need to have based on readyTarget?
* Divide running matches by 1 - readyTarget
3. How many game rooms are needed to provide this amount of matches?
* Get the desired number of matches and divide by the max number of matches
per room as defined in the scheduler
  • Loading branch information
hspedro authored Jan 7, 2025
1 parent 6dac23b commit feb104a
Show file tree
Hide file tree
Showing 4 changed files with 7 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -96,12 +96,12 @@ func (p *Policy) CalculateDesiredNumberOfRooms(policyParameters autoscaling.Poli
return -1, errors.New("There are no runningMatches in the currentState")
}

maxMatches := currentState[MaxMatchesKey].(int)
maxMatchesPerRoom := currentState[MaxMatchesKey].(int)

occupiedRooms := math.Ceil(float64(runningMatches) / float64(maxMatches))
desiredNumberOfRoom := int(math.Ceil(float64(occupiedRooms) / (float64(1) - readyTarget)))
desiredNumberOfMatches := math.Ceil(float64(runningMatches) / (float64(1) - readyTarget))
desiredNumberOfRooms := int(math.Ceil(desiredNumberOfMatches / float64(maxMatchesPerRoom)))

return desiredNumberOfRoom, nil
return desiredNumberOfRooms, nil
}

func (p *Policy) CanDownscale(policyParameters autoscaling.PolicyParameters, currentState policies.CurrentState) (bool, error) {
Expand Down
2 changes: 1 addition & 1 deletion pkg/api/v1/rooms.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion proto/api/v1/rooms.proto
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ message UpdateRoomStatusRequest {
string status = 4;
// Timestamp of the status update.
int64 timestamp = 6;
// The number of running_matches in the game room.
// The number of running matches in the game room.
int64 running_matches = 7;
}

Expand Down
2 changes: 1 addition & 1 deletion proto/apidocs.swagger.json
Original file line number Diff line number Diff line change
Expand Up @@ -800,7 +800,7 @@
"runningMatches": {
"type": "string",
"format": "int64",
"description": "The number of running_matches in the game room."
"description": "The number of running matches in the game room."
}
},
"description": "The player event request."
Expand Down

0 comments on commit feb104a

Please sign in to comment.