Skip to content
Coding · Hard · Deployment automation · Target O(steps) · Staff

Rolling deploy batch planner

Turn a replica count, max unavailable and max surge into the exact steps of a rolling update, and show that capacity stays between the floor and the ceiling.

Statement

A service runs replicas pods of an old version and must end with the same number of a new version. Write plan_rollout(replicas, max_unavailable, max_surge). Each limit is a whole number of pods, or a string such as "25%" meaning a percentage of replicas. A surge percentage rounds up; an unavailable percentage rounds down. A max_unavailable above replicas counts as replicas.

Two numbers bound the rollout. The floor is replicas minus max unavailable: at least that many pods must be ready at all times. The ceiling is replicas plus max surge: no more than that many pods, ready or not, may exist at once. If both limits come to zero the rollout cannot move, so raise ValueError. With zero replicas there is nothing to do: return an empty list.

The rollout runs in steps, and every pod is ready when a step begins. In a step, first stop as many old pods as the floor allows, then start as many new pods as the ceiling allows, never more than are still needed. The step ends when the pods it started are ready.

Return one (stop_old, start_new, ready, total) tuple per step: ready is the number of ready pods once the old ones have stopped, and total is ready plus the pods just started. Finish when no old pods remain and replicas new pods are ready.

Examples

In the first example 25% of 10 is 2.5, so max unavailable is 2 and max surge is 3: the floor is 8 and the ceiling is 13.

plan_rollout(10, "25%", "25%")
→ [(2, 5, 8, 13), (5, 5, 8, 13), (3, 0, 10, 10)]

plan_rollout(3, 0, 1)          # floor 3, ceiling 4
→ [(0, 1, 3, 4), (1, 1, 3, 4), (1, 1, 3, 4), (1, 0, 3, 3)]

plan_rollout(5, 0, 0)
→ ValueError

Constraints

0 ≤ replicas ≤ 10⁵. Absolute limits are whole numbers of 0 or more; percentages are whole numbers from 0% to 100%. Work in whole numbers, so that a percentage never picks up a floating-point error. The plan must be built in time proportional to its length.

Open to everyone until Pro goes on sale · sign in to save your attemptSolve it in the editor with visible tests
Open in the app