Skip to content

Add time scheduling with debouncing for gratuitous link statuses - #145

Merged
puddly merged 11 commits into
devfrom
puddly/time-based-scheduling
Nov 22, 2026
Merged

puddly merged 11 commits into
devfrom
puddly/time-based-scheduling

Conversation

@puddly

@puddly puddly commented Nov 17, 2026

Copy link
Copy Markdown
Member

Neighbor and route activity counters were implemented in a spec-literal manner: every time they are used, a number goes up. When a link status broadcast goes out, a number goes down. This couples an activity counter's time-based component to a fixed cadence and prevents the activity counter from being refreshed early due to double or non-counting. I've migrated the activity counters to still be mostly spec-compatible by storing them as u16 (in steps of 1/256) and converting them to u8 when emitting values, which allows them to be driven by time instead of by activity.

The purpose of the above migration is to enable us to send gratuitous Link Status responses to new routers coming online. This is done according to the spec. I've also added a slight deviation: hack_early_link_status_for_unknown_routers (true by default) to also send a gratuitous Link Status response when an unknown router comes online. This is still rate limited.

A shared Debouncer is now used by both MTORR and gratuitous Link Status broadcasts. In the future, more of this logic will be made sans-io to be better unit testable and to shrink the async runtime's state.

Addresses some of #143

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 Changes recommended

Activity decay can miscount traffic or panic on accepted period values, and the promised tunable for disabling early link-status broadcasts is missing.

4 open issues
What changed in this PR

This PR adds early Link Status broadcasts to help routers establish neighbor links sooner, addressing part of #143. It also moves activity counters toward elapsed-time accounting so broadcasts need not follow a fixed cadence.

Changes:

  • Introduces fractional activity counters and time-based decay for neighbors and routes.
  • Adds a reusable debouncer for Link Status and many-to-one route-request scheduling.
  • Adds bootstrap jitter settings and tests for scheduling, decay, and neighbor discovery.
File Description
crates/​ziggurat-zigbee/​src/​time.rs Adds a default value for core timestamps.
crates/​ziggurat-zigbee/​src/​nwk/​routing/​tests.rs Updates routing calls and tests inactive-route decay.
crates/​ziggurat-zigbee/​src/​nwk/​routing.rs Migrates route activity to fractional counters and elapsed-time decay.
crates/​ziggurat-zigbee/​src/​nwk/​neighbors.rs Tracks early-response needs and migrates neighbor activity counters.
crates/​ziggurat-zigbee/​src/​nwk/​activity.rs Adds fractional activity counters, decay calculations, and tests.
crates/​ziggurat-zigbee/​src/​nwk.rs Exports the activity module.
crates/​ziggurat-zigbee/​src/​lib.rs Exports the debouncer module.
crates/​ziggurat-zigbee/​src/​debouncer.rs Adds reusable deadline scheduling with tests.
crates/​ziggurat-zigbee/​src/​constants.rs Defines bootstrap jitter settings.
crates/​ziggurat-protocol/​src/​bridge.rs Supplies activity initialization values when restoring state.
crates/​ziggurat-driver/​src/​zigbee_stack/​sends.rs Passes the router age limit during discovery retries.
crates/​ziggurat-driver/​src/​zigbee_stack/​route.rs Uses debounced route advertisements and updated routing APIs.
crates/​ziggurat-driver/​src/​zigbee_stack/​nwk.rs Updates route cleanup and discovery calls.
crates/​ziggurat-driver/​src/​zigbee_stack/​neighbor.rs Schedules early Link Status broadcasts and elapsed-time decay.
crates/​ziggurat-driver/​src/​zigbee_stack/​joining.rs Supplies activity initialization values when attaching children.
crates/​ziggurat-driver/​src/​zigbee_stack.rs Stores debouncers and adds shared deadline-waiting logic.

Review effort: Balanced

@zigpy-review-bot zigpy-review-bot added the new feature Adds new feature label Nov 17, 2026
Base automatically changed from puddly/link-status-reading-fixes to dev November 18, 2026 00:01
@puddly
puddly force-pushed the puddly/time-based-scheduling branch from a289197 to 52e1bf9 Compare November 18, 2026 00:02
@t3hk0d3

t3hk0d3 commented Nov 18, 2026 •

Copy link
Copy Markdown
Contributor

Tested #144 + this PR on my bench, without #143:

  • The first MTORR after a restart is now relayed by every router. So Hold the first many-to-one route request until neighbors can accept it #143 isn't needed for that.
  • The early link status after a start lists only the routers heard so far. A router that hasn't spoken yet reads that as "I don't hear you" and zeroes its cost toward us. The first route discoveries then avoid it: in every restart we got a cost-4 route where a cost-3 one exists.

Holding early link statuses until the first periodic one fixed it in both runs.

@zigpy-review-bot zigpy-review-bot left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Post-merge review. The debouncer, the wake-up handling in wait_for_debouncer and the MTORR migration hold up, and two of the four Copilot findings from the earlier head are fully fixed: decay now runs before every update, and the decay timestamp moves past all elapsed steps. Three problems remain on dev. For the first, a fix is already in an open PR; fix PRs for the other two are linked below.

1. A restart makes routers not heard yet drop their link to us (regression)

t3hk0d3's bench report above is right, and the mechanism is in the code:

  • After a restart, restored router children have no LQI samples and sibling routers are not in the table yet, and is_in_link_status lists only routers with a sample.
  • The first router heard makes an early link status due, and 0.5–1 s later we send a complete list holding only the routers heard so far.
  • A router left out of a complete list sets its outgoing cost to us to 0 (spec 3.6.4.4.2). On a Ziggurat router that reports lost_link, invalidates its routes through us and drops our route requests (outgoing_cost == 0 in route.rs) until we have heard it and sent another link status.

Before this PR the first non-empty link status went out a full period after the start, when nearly every router had been heard. A probe test against Neighbors confirms the sequence: two restored routers, the first one heard makes the link status due, the list holds it alone, and the other router ends at outgoing cost 0 with lost_link set.

The fix exists: commit 315d7b6 in #147 holds every early link status until one period after the start (Debouncer::hold, unit-tested, with its own tunable), and the bench numbers there show cost 3 from the first discovery with it. That commit doesn't depend on the route refresh in the rest of #147, so it could land on its own while the route refresh vs source routing question is settled. Two trade-offs to keep in mind. Routers that zero their cost on our empty startup link status (spec behaviour, and Ziggurat routers before #144) get it back today about a second after we first hear them (a sibling router: after its first link status); with the hold they wait for the first periodic link status again, as before this PR. And because the hold sits in Debouncer::request, it also delays the spec's answer to an unknown router's empty list (3.6.4.4.2) during that first period.

2. hack_early_link_status_for_unknown_routers only gates one of the two paths

handle_link_status checks the tunable, but maybe_recompute_lqa asks for the same early link status through record_lqa without checking it. A restored or newly joined router child (or a router whose samples age() cleared, when it is heard again) takes the second path, because its first frame of any kind reaches record_lqa before on_link_status. With the tunable cleared, a restart still sends early link statuses as restored router children are heard (requests close together share one), contrary to its doc. Fix: #151.

3. link_status_period = 0 aborts the stack

The 1–255 µs case is fixed, but zero still reaches elapsed * steps_per_count / period in take_decay_steps. The setter accepts it, and decay now runs on the receive path, so the next received link status or relayed unicast panics, and in a release build (panic = "abort") the process exits. Fix: #152.

Notes, no change asked

  • Nothing reads router_inbound_activity, router_outbound_activity or recent_activity yet, so the time-based decay and its new start value have no observable effect today. The description's "converting them to u8 when emitting values" has no emitter on dev yet.
  • "This is still rate limited" comes down to the jitter. The early link status uses a zero minimum interval, so each newly listed router can get its own broadcast. A minimum interval of a second or two would bound both the new-router path and the answers to repeated empty lists.
  • Debouncer::request before the first fired() is dropped (next is 0). For MTORR that is harmless (the first request goes out at the end of the startup wait anyway). For link status it only matters if a frame is handled before the link status task is first polled: an answer to an empty list requested then waits for the periodic link status.
Verified (9 checks)
  • Read the full diff and the surrounding driver code at 34f6e5f, against spec R23.2 3.6.4.4.1/3.6.4.4.2.
  • cargo test --workspace --all-features at 34f6e5f: 199 passed.
  • Probe unit tests (not committed) for the restart sequence, take_decay_steps with a zero period, and Tunables::set("link_status_period", 0) returning Ok.
  • Checked wait_for_debouncer against the runtime's Notify: one stored permit, a single waiter, no await between the wait returning and fired(). A request made during delivered().await is picked up on the next loop turn.
  • MTORR timing matches the parent after the first advertisement: a kick fires at max(now, last_fired + mtorr_min_interval), otherwise every mtorr_max_interval. A kick during the startup wait no longer adds a second advertisement one min interval after the first.
  • Jitter arithmetic cannot panic for any accepted tunable value (min > max gives min).
  • Copilot findings 2 and 4 from the earlier head are fixed; finding 1 is half fixed (item 2 above) and finding 3 is fixed except for zero (item 3).
  • Each fix PR runs cargo fmt, cargo clippy with the CI flags and cargo test --workspace.
  • Commit 315d7b6 from #147 cherry-picks cleanly onto dev on its own; the workspace builds and the ziggurat-zigbee tests pass with it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

new feature Adds new feature

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants