Cluster Idle Billing
EasyIntervals · Databricks
A compute cluster runs jobs and is billed for every second it is up. events lists job activity windows [start, end] (in seconds, start <= end), in no particular order; windows may overlap.
The cluster behaves like this:
- It starts at the start of a job if it is not already up.
- After all running jobs have finished, it stays up idle for
idle_timeoutmore seconds and then shuts down. So with last activity ending ate, it shuts down ate + idle_timeout. - If a job starts at or before the shutdown moment (
start <= e + idle_timeout), the cluster is still up and simply keeps running; otherwise it shuts down and later starts again for the new job.
Return the total number of seconds the cluster is up. (Equivalently: the total length covered by the union of the intervals [start, end + idle_timeout].) Return 0 if there are no jobs.
Example 1
- Input:
- events = [[12, 20], [0, 10], [16, 18], [40, 45]], idle_timeout = 5
- Output:
- 35
- Explanation
- Sorted by start, the cluster runs from 0. Job [12, 20] starts before the idle shutdown at 10 + 5 = 15, and [16, 18] overlaps it, so the cluster stays up until 20 + 5 = 25 (25 seconds). It is down when [40, 45] arrives and restarts for 45 + 5 - 40 = 10 more seconds. Total 35.
Example 2
- Input:
- events = [[0, 10], [15, 20]], idle_timeout = 5
- Output:
- 25
- Explanation
- The cluster would shut down at exactly 15, and the next job starts at 15, so it is reused: one uptime period from 0 to 25.
Example 3
- Input:
- events = [[0, 10], [16, 20]], idle_timeout = 5
- Output:
- 24
- Explanation
- The cluster shuts down at 15, one second before the next job, so it is billed 0 to 15 and then 16 to 25, for 15 + 9 = 24 seconds.
Constraints
0 <= events.length <= 10^50 <= start <= end <= 10^90 <= idle_timeout <= 10^9