Model Breakdowns
Make a resource fail at random and be repaired, decide what happens to the work it was doing, and check how long it was down.
Before You Start
- Interruptions explains
on_interrupt. - Priority and Preemption describes the down state.
Steps
Give the Resource a Breakdown
Give the resource a breakdown with two distributions: time_to_failure, the time from the start or from a repair to the next failure, and repair, how long a repair takes.
Here a packing station of two units fails about once a minute on average (rate: 0.0167) and takes 5 to 15 seconds to repair:
The breakdowns run as a SimPy process, started beside the arrivals:
| examples/low-level/breakdowns_example.py:processes | |
|---|---|
An interrupted order catches simpy.Interrupt, whose cause is "breakdown", gives back its request and asks again, as the full file shows.
With the default clock: calendar, the next failure is drawn at the start and after each repair, and the time to it passes whether the station is busy or idle. A failure works as follows:
- All the station's units fail together. Its capacity is 0 until the repair ends, so no order starts.
- Its
current_capkeeps the planned value. A scenario step or an ingress message that changes it during the repair sets the capacity the station returns to. It does not end the repair. - The station publishes an event keyed by its name at each failure,
{"status": "down", "reason": "failure"}, and at each repair,{"status": "repaired", "reason": "failure"}.
The two distributions are registry paths, Shop.breakdown.station.time_to_failure and .repair, so a live change can make failures more or less frequent. The two cannot both always give 0, because the station would then fail and be repaired for ever at one instant. The load refuses a pair that always gives 0. A live change that makes both 0 stops the run with an error that names the station. So does a pair that gives only 0 although its distribution has a spread, such as a normal time whose mean is many times its std below 0: the run stops after 10,000 failures at one instant.
A breakdown that lets work finish and then holds the resource for a fixed time each day is a capacity schedule. Vary Arrivals by Time writes it as scenario steps.
Choose What Happens to the Work in Service
Choose work: interrupt, the default, stops the tasks being served when the resource fails. finish lets them finish and stops only new work.
Choose What an Interrupted Order Does
With interrupt, give the task an on_interrupt: resume packs what was left after the repair, restart packs again from the start, and abandon ends the order.
Count Only the Time the Station Works
Choose the failure clock. calendar, the default, counts the time to failure whether the station is busy or idle. clock: busy counts it only while at least one unit of the resource is in use, so an idle station does not wear out. The time to failure is still drawn once for each cycle. A task's resource, a seize, a wait request and an open unit all count as in use. A time to failure of 0 fails at the grant of the first unit.
The clock waits for two events of the resource. when_busy() succeeds when the resource next holds a unit after holding none, and when_idle() succeeds when it next holds none after holding one. A station that is down keeps the units its users hold, so it is not idle until they are given back.
Here a station fails after 5 seconds of work. One order arrives at 10 seconds and takes 8, and work: finish lets it finish:
| docs/snippets/yaml/breakdowns_busy.yaml:busy | |
|---|---|
| docs/snippets/guides/breakdowns_busy.py:busy | |
|---|---|
The process that fails and repairs the station takes the same clock:
In the Builder and Low-level blocks, clock is the argument of the function that builds the model: "busy" or "calendar".
With the busy clock, every form puts the station down at 15 seconds, 5 seconds into the order, and repairs it at 17 seconds: [(15.0, 'down'), (17.0, 'repaired')]. With clock: calendar, the same station fails at 5, 12, 19 and 26 seconds, mostly while it is idle.
Check the Result
Download the example's three forms into the folder you work in with desflow examples get breakdowns. The check is a script, check_breakdowns.py, made of the blocks below in order. It reads the example from breakdowns/yaml/, breakdowns/builder/ and breakdowns/low-level/, so run it from the folder where you ran the command.
With statistics at minimal or above, the station publishes down: 1 while it is down and 0 otherwise. From basic it also publishes downtime, the seconds it has been down, and failures, how many times it has failed. The script starts with a class that keeps every record the example publishes, and a function that loads an example file without running it. The imports of desflow follow the class, in the line from desflow import DynamicRealtimeEnvironment, Sampler, SimulationContext:
The next function takes the records of a run, its end time and its statistics, finds the down periods from the events, and checks both counts against them. It is the same for every form:
The YAML and builder forms add a capturing egress beside the example's own. The low-level form builds the example's environment with the capture as its only egress. Each passes what it captured to availability:
| docs/snippets/guides/breakdowns.py:yaml | |
|---|---|
Last, the script runs the three forms. Run it, and keep the lines that start with a brace, because the example also prints each of its events:
| docs/snippets/guides/breakdowns.py:main | |
|---|---|
For one 300-second run of the example, every form returns five down periods. The first is from 5.6 to 10.9 seconds. Together they give 38.6 seconds of downtime in 300, or 12.9 percent. 8 orders are interrupted, and all 8 resume after a repair. Report the downtime beside the waits: a station that is down 12.9 percent of the time has that much less capacity, and its waits grow as on Queues and Capacity.
Example
The breakdowns example is the model above: two units at a packing station, an order every 1.67 seconds on average (rate: 0.6), and every order packed once the station is back. It needs no Docker service and ends on its own after 300 simulation seconds.
Quick Start
desflow examples get downloads the example at the release you install, into a folder of its own. Pick uv or pip:
Quick Start
desflow examples get downloads the example at the release you install, into a folder of its own. Pick uv or pip:
Quick Start
desflow examples get downloads the example at the release you install, into a folder of its own. Pick uv or pip: