You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
It is possible to use sd_notify plus double-forking and passing of FDs to do a roughly-zero-downtime services restart. At ASG 2018 we discussed a way for systemd to provide this ability to services in a standard way that didn't require services to re-implement it. This RFE attempts to document that design discussion.
The idea is that upon a receiving a restart request for a service, sytsemd would:
Start a new copy of the service in a new cgroup (in a common parent from the previous one)
Hand the new service the FD to accept on
The user can use TimeoutStopSec if they want to limit how long the old generation of the should stick around
Once MainPID of the old cgroup terminates (or TimeoutStopSec is reached), the old cgroup is cleaned up as usual
Systemd should keep track of "generation number" such that users can specify which generation of a service they mean when more than one is running.
There was some discussion on how systemd could prevent the previous generation from messing with the FD it has (since it will be the same one) to ensure a safe transition. An exact plan was not solidified, but it seems possible to do something with BPF.
This feature would be opt-in, and opting in would cause an additional layer of cgroups to be used so that each generation would have a common parent.
We could introduce new option e.g. RestartMode= that would have two possible values.
auto - this would be default and mean no change in the behaviour
retain - stop part of the restart operation would not kill the processes that belong to the cgroup
In retain mode the service cgroup would not contain processes but instead there would be a sub-cgroup for every “generation” of the service. MainPID= of the service would always run in the latest generation. Ordering would be established using simple counter (generation_id) that would be a part of respective cgroup’s name. JOB_RESTART would not kill the current generation but rather start the new one while standalone JOB_STOP would kill all generations as configured by KillMode=.
I am not sure we need to handle sharing of resources like sockets among generations in the first iteration. By not dealing with that we would have to limit this option to non-socket-activated services and that would reduce its usefulness.
Your approach through jobs sounds sane. I'd add that from my experience, this should also add something that'd tell the the active generation to switch into "lameduck" mode. The most generic would likely be another Exec* like ExecPassivate= (because ExecRestart= would be confusing and ExecStop= is needed differently).
@Werkov Yes, we will need some interface/protocol for making sure that service can learn there is newer generation. I was thinking about (D-Bus) signal but custom command seems most flexible.
There was some discussion on how systemd could prevent the previous generation from messing with the FD it has (since it will be the same one) to ensure a safe transition. An exact plan was not solidified, but it seems possible to do something with BPF.
I'd add that from my experience, this should also add something that'd tell the the active generation to switch into "lameduck" mode. The most generic would likely be another Exec* like ExecPassivate=
I would expect the restart protocol to look roughly like this:
service running
restart job started
launch ExecPassivate= and wait for it to exit
launch new generation and pass all FDs from FDStore to the service, set an environment variable indicating non-first generation
start stop job for the old generation
Done this way, I don't see a reason why it wouldn't work for socket activated services. Step 3 makes sure that they would not be fighting over the socket. I think it's exactly the same for non-socket-activated services.
because ExecRestart= would be confusing and ExecStop= is needed differently
I think calling the directive ExecPreRestart= would be pretty clear and could be even used at all times without any ties to the RestartMode feature. Someone might find it useful just on its own.
And for the RestartMode=, I think sequential+overlapped might be better naming then auto+retain as it more clearly describes what it does.
There was some discussion on how systemd could prevent the previous generation from messing with the FD it has (since it will be the same one) to ensure a safe transition. An exact plan was not solidified, but it seems possible to do something with BPF.
Maybe we could do this somehow with BPF_SOCKMAP.
I don't know anything about previous discussions, but to me it seems like a bad idea. From systemd side, it looks like the different generations might be messing with the FDs in a wrong way. From application developer's point of view though, having systemd mess with my FDs in any non-trivial manner is just plain annoying. It would have to be very clearly defined, what exactly systemd does, when and how. And the behavior can't be ever changed without accidentally breaking something. As an application developer, I would like to be given a simple description of what systemd does and handle the rest myself. Having some sort of BPF magic messing with my FDs does not sound very predictable and helpful.
Shameless plug: I wrote a bachelor's thesis on this topic 2 years ago, supervised by @Werkov. You can find the text here and the code here. I actually implemented a patch for systemd to work with my application library. In the end, it didn't seem worth it to merge the code as it could be implemented better by just reusing existing components in systemd without reliance on a random third party library.
Hi @vakabus, thank you for your suggestions! However, I don't necessarily agree on step 5. I am not sure if stop of the old instance should be (blocking?) part of the protocol. I'd expect that either ExecPassivate= command or the new instance "tell" the old instance to exit at some point. From what I know about use-cases for this feature, in some cases it might take significant time for the old instance to become unneeded.
At any rate, we are mostly in agreement and I think that this last "stop-old" step can added later, if desired.
Step 5 is there mainly to prevent accidental resource leaks. Sure, in an ideal world, the ExecPassivate= command could stop the old instance and the stop job would be pointless. However, only systemd is in the position to properly clean up all processes. Leaking processes during the transparent restart must be IMO impossible. Reusing normal stop job with it's configuration allows the user to define in detail what should be done when stopping and should be easy to implement.
And more so, I personally like the semantics:
ExecPassivate= - command that informs the running process that it should dump all state so that it could be restarted (similar to sync command to flush IO caches)
ExecStop= - command to actually stop the running process
Having ExecPassivate= do both of these tasks at once seems wrong to me as it leads to duplicate stopping logic
Can I suggest ExecDrain or ExecShutdownForUpgrade instead of ExecPassivate? I know passivate is a word, but it's one I never saw before (had to check it's really in the dictionary). In past jobs the process of getting a server to stop accepting new connections and finish handling the existing connections was called draining, which seems like an apt analogy.
I think there is a need to support both modes (maybe not both at once):
The old server stops accepting new connections but finishes handling existing connections, then shuts itself down. During this time systemd should keep new inbound connections hanging. This mode is good for processes that handle short lived connections and which need exclusive access to some state e.g. a set of database files, or in which the machine is not provisioned to run two instances at once and some latency across upgrades is OK.
The old server stops accepting new connections and the new server can start up immediately in parallel. This is good for servers that are stateless or where concurrency is handled externally (e.g. rdbms), and where the machine can handle multiple instances at once without exhausting RAM.
Activity
msekletar commentedon Jul 1, 2022
We could introduce new option e.g.
RestartMode=that would have two possible values.auto- this would be default and mean no change in the behaviourretain- stop part of the restart operation would not kill the processes that belong to the cgroupIn retain mode the service cgroup would not contain processes but instead there would be a sub-cgroup for every “generation” of the service.
MainPID=of the service would always run in the latest generation. Ordering would be established using simple counter (generation_id) that would be a part of respective cgroup’s name.JOB_RESTARTwould not kill the current generation but rather start the new one while standaloneJOB_STOPwould kill all generations as configured byKillMode=.I am not sure we need to handle sharing of resources like sockets among generations in the first iteration. By not dealing with that we would have to limit this option to non-socket-activated services and that would reduce its usefulness.
Werkov commentedon Jul 1, 2022
Your approach through jobs sounds sane. I'd add that from my experience, this should also add something that'd tell the the active generation to switch into "lameduck" mode. The most generic would likely be another
Exec*likeExecPassivate=(becauseExecRestart=would be confusing andExecStop=is needed differently).msekletar commentedon Jul 1, 2022
@Werkov Yes, we will need some interface/protocol for making sure that service can learn there is newer generation. I was thinking about (D-Bus) signal but custom command seems most flexible.
msekletar commentedon Jul 1, 2022
Maybe we could do this somehow with BPF_SOCKMAP.
vakabus commentedon Jul 5, 2022
I would expect the restart protocol to look roughly like this:
ExecPassivate=and wait for it to exitFDStoreto the service, set an environment variable indicating non-first generationDone this way, I don't see a reason why it wouldn't work for socket activated services. Step 3 makes sure that they would not be fighting over the socket. I think it's exactly the same for non-socket-activated services.
I think calling the directive
ExecPreRestart=would be pretty clear and could be even used at all times without any ties to theRestartModefeature. Someone might find it useful just on its own.And for the
RestartMode=, I thinksequential+overlappedmight be better naming thenauto+retainas it more clearly describes what it does.I don't know anything about previous discussions, but to me it seems like a bad idea. From systemd side, it looks like the different generations might be messing with the FDs in a wrong way. From application developer's point of view though, having systemd mess with my FDs in any non-trivial manner is just plain annoying. It would have to be very clearly defined, what exactly systemd does, when and how. And the behavior can't be ever changed without accidentally breaking something. As an application developer, I would like to be given a simple description of what systemd does and handle the rest myself. Having some sort of BPF magic messing with my FDs does not sound very predictable and helpful.
Shameless plug: I wrote a bachelor's thesis on this topic 2 years ago, supervised by @Werkov. You can find the text here and the code here. I actually implemented a patch for systemd to work with my application library. In the end, it didn't seem worth it to merge the code as it could be implemented better by just reusing existing components in systemd without reliance on a random third party library.
msekletar commentedon Nov 28, 2022
Hi @vakabus, thank you for your suggestions! However, I don't necessarily agree on step 5. I am not sure if stop of the old instance should be (blocking?) part of the protocol. I'd expect that either ExecPassivate= command or the new instance "tell" the old instance to exit at some point. From what I know about use-cases for this feature, in some cases it might take significant time for the old instance to become unneeded.
At any rate, we are mostly in agreement and I think that this last "stop-old" step can added later, if desired.
vakabus commentedon Nov 29, 2022
Step 5 is there mainly to prevent accidental resource leaks. Sure, in an ideal world, the
ExecPassivate=command could stop the old instance and the stop job would be pointless. However, only systemd is in the position to properly clean up all processes. Leaking processes during the transparent restart must be IMO impossible. Reusing normal stop job with it's configuration allows the user to define in detail what should be done when stopping and should be easy to implement.And more so, I personally like the semantics:
ExecPassivate=- command that informs the running process that it should dump all state so that it could be restarted (similar tosynccommand to flush IO caches)ExecStop=- command to actually stop the running processHaving
ExecPassivate=do both of these tasks at once seems wrong to me as it leads to duplicate stopping logicmikehearn commentedon Mar 1, 2023
Can I suggest
ExecDrainorExecShutdownForUpgradeinstead ofExecPassivate? I know passivate is a word, but it's one I never saw before (had to check it's really in the dictionary). In past jobs the process of getting a server to stop accepting new connections and finish handling the existing connections was called draining, which seems like an apt analogy.I think there is a need to support both modes (maybe not both at once):
[DRAFT] core: add RestartMode= and ExecRestart= options
[DRAFT] core: add RestartMode= and ExecRestart= options
[DRAFT] core: add RestartMode= and ExecRestart= options
[DRAFT] core: add RestartMode= and ExecRestart= options
[DRAFT] core: add RestartMode= and ExecRestart= options
[DRAFT] core: add RestartMode= and ExecRestart= options
[DRAFT] core: add RestartMode= and ExecRestart= options
[DRAFT] core: add RestartMode= and ExecRestart= options
[DRAFT] core: add RestartMode= and ExecRestart= options
poettering commentedon Jun 15, 2023
Please see #27309 (comment) for the results of the latest discussion about the concept.