Before I talk about extension state implementation details let’s define what extension state is. For most people extension state is really just seen as the “hints” you define in the dialplan and the blinky lights on your phone. This represents the configuration and external usage aspects. Getting from the configuration in dialplan to the blinky lights is really where the complicated part is.
Extension state aggregates two different sources of information – device state (what your phone is doing) and presence state (explicit status information like I’m do not disturb). I refer to these as aggregates because you can configure multiple devices or presence states for a hint. These bits of information are then given to any part of Asterisk that has requested to receive them, such as the pubsub module in PJSIP.
When the stasis message bus was implemented device state and presence state were changed to use it to make the information available to other parts of Asterisk, including extension state. Extension state subscribed to receive updates for ALL device states and for ALL presence states – even those it had no interest in. This occurred because subscriptions were not selective – it was one subscription for ALL changes. On systems with heavy device usage this could result in a lot of work for extension state, particularly for updates that it didn’t care about. It needed to check every time to see if it should care or not. It also made device state updates single threaded which made extension state hint updates single threaded.
Through experimentation and prototyping I was able to determine that this was inefficient and determined that changes would essentially result in a rewrite of extension state, so I took the opportunity to take advantage of modern Asterisk APIs and improve things further.
Extension state was previously part of the PBX core itself. This complicated the code in the PBX core and made it very difficult to safely make changes or improvements. This has now been changed so that extension state lives in its own set of source files and is instead informed by the PBX core when changes occur, so it can update its own state.
Each extension state hint now subscribes only to the devices it is interested in. This is done as synchronous subscriptions as the overhead of doing it asynchronously did not yield any benefit, even under heavy load. The cost of handling the device state updates is minor and less than the asynchronous method.
Presence state updates remain using a single subscription as presence state has no capability to do individual subscriptions. Presence state updates are also extremely infrequent in comparison to device state updates and so determining what hints to update is minor.
Extension state hints now cache information in themselves instead of having to query elsewhere. This speeds up aggregation calculation and also explicit querying of the state of a hint.
Finally, extension state hints now provide stasis message bus topics like device state and other things in Asterisk, allowing consumers to use our common API instead which ensures handling is done in a multithreaded fashion. Prior to this a custom API was provided that used callbacks. These callbacks were done synchronously and were single threaded. The existing custom API was reimplemented on top of stasis, so existing things continue to work as they did before without any updating required and provided the benefit of gaining multithreading support.
Putting all of these together ultimately made the code cleaner, simplified things, and made it multithreaded. This yielded less CPU usage under heavy use which is always nice to see.
These improvements are available as of Asterisk 20.21.0, 22.11.0, and 23.5.0. I would suggest using even more recent releases though as issues were uncovered after release and fixed.