Repository navigation
Horizon: let the template fork workers for supervisors inside the master - #7
Open
volodymyr-aloware wants to merge 2 commits into
Open
volodymyr-aloware wants to merge 2 commits into
volodymyr-aloware wants to merge 2 commits into
Conversation
A unit's fork server forks, admits, signals and reaps the horizon:work workers of its supervisor loop. That handling moves into WorkerSet, unchanged, so another server can fork workers the same way. UnitServer keeps the loop and passes it every worker message, as before.
Some apps run Horizon's supervisors inside the master process instead of one process each. Their workers can now be forked as well: the master asks its template for each worker (a spawn with role: worker), and the template forks it through WorkerSet, with no fork server in between. The template TERMs the workers it forked when its master is gone, so none outlives it. On the master's side, Master::start() in inline mode (queue-prefork.horizon_inline) skips the check for the app's own AddSupervisor binding, applies the off switches itself and pumps on MasterSupervisorLooped. Master::workerProcess() hands out forked handles with a crash breaker per supervisor, and ForksWorkersFromTemplate lets the app's process pool use them. Horizon restarts a worker on the handle it has, so a stopped handle whose template is gone, or whose supervisor the breaker turned stock, is replaced with a new one, forked again or stock. The doctor reads horizon_inline as well.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Some apps run Horizon's supervisors inside the master process instead of one process each, to save the memory of a booted app per supervisor. Today such an app has to run queue-prefork stock: its own
AddSupervisorbinding fails the preflight, and nothing could fork its workers anyway, since workers are forked only by a unit's fork server at its supervisor loop's request.This PR lets the template fork those workers directly, on the master's request. In our measurements the two savings add up: the in-process supervisors cost nothing extra, and every worker shares the template's compiled code.
What changes
WorkerSet(first commit, no behaviour change). The fork server's handling ofhorizon:workworkers (fork, admit, pause state, cancel, signal, ready, reap) moves intosrc/Server/WorkerSet.phpso that a second server can use it.UnitServerkeeps its loop and passes it every worker message as before.The template forks workers (second commit).
ForkedProcesssends its role inspawn. Forrole: worker, the template forks ahorizon:workworker through its ownWorkerSet, routesadmit,pause_state,cancelandsignalfor worker ids, and reaps them. Anything else still forks a unit, so existing masters are unaffected.masterExited(), before it exits or execs the stock master; spawned: when the master is gone or its channel closes), so none outlives it. The fork blocks the relayed signals until the child has reset its handlers.Master::start()takes an inline flag, defaulting to the new confighorizon_inline(QUEUE_PREFORK_HORIZON_INLINE). Inline mode skips Preflight's bindings check only, applies the off switches itself (asWiringdoes before binding the package's command), and registers its own tick onMasterSupervisorLooped, since the app's command replacesPreforkHorizonCommand.Master::workerProcess(SupervisorOptions)hands out a forked handle, or null for stock, with the unit breaker's rule kept per supervisor.ForksWorkersFromTemplateis a trait for the app'sProcessPoolsubclass.createProcess()asks the master for a forked handle. Horizon restarts a stopped worker on the handle it has, somonitor()first replaces a stopped forked handle whose template is gone, or whose supervisor the breaker turned stock, with a new one.queue-prefork:doctorchecks this mode whenhorizon_inlineis set.horizon:purgemust not run in this mode); CHANGELOG gets an Unreleased entry.Limitations
AddSupervisor::createProcess()could useunitProcess()for it, but that is left out here.Happy to rename the config key, split the PR, or move the trait wherever it fits better.