Skip to content

[DRAFT] core: add RestartMode= and ExecRestart= options - #27309

Closed
msekletar wants to merge 1 commit into
systemd:mainfrom
msekletar:zero-downtime-restart
Closed

msekletar wants to merge 1 commit into
systemd:mainfrom
msekletar:zero-downtime-restart

Conversation

@msekletar

Copy link
Copy Markdown
Contributor

Main motivation for this work is to allow zero-downtime service restarts. Currently the restart of a service unit is composed of two main steps, stop of the currently running service which is then followed by start of the service. This prevents the service from running long-term after restart of the service is initiated. Therefore, clients of the service can experience the disruption as their connection to the service can be abruptly terminated. This can be partly mitigated by setting using non-default KillMode= and adjusting TimeoutStopSec= timeout. However, full mitigation of this behavior is currently not possible.

This commit introduces a new systemd features that will allow an old version of a service (generation) to continue running until it voluntarily exits or the service is explicitly stopped. Main goal is to allow for undisturbed handling of previously started work (e.g. client connections, system management operations) to run into full completion even if the service is restarted. It is expected that clients requesting the service after the restart will communicate with the latest version (generation) of the service.

This behavior will be achieved using a combination of two newly introduced service unit options, RestartMode= and ExecRestart=.

Fixes: #10228

TODO

  • [bug] main service cgroup stays around after stop of the service
  • [design] figure out behavior in combination with KillMode=process|mixed
  • [design] maybe rewrite the test service in C ?
  • [design] is UNIT_MAINTAINANCE state appropriate for RestartMode=keep ?
  • [tests] - add test cases for different KillModes= - maybe rewrite the test using Beakerlib?

@github-actions github-actions Bot added documentation systemctl tests util-lib please-review PR is ready for (re-)review by a maintainer labels Apr 17, 2023
@msekletar
msekletar marked this pull request as draft April 17, 2023 14:58
@bluca

bluca commented Apr 17, 2023

Copy link
Copy Markdown
Member

My experience with these kind of setups is that they are an unmitigated disaster, because they are invariably used by services that cannot do process management right and get it awfully wrong, and manage to completely break the node. Can't the exact same result be obtained by simply using the FD store to pass FDs over? There should be no interruptions or connection closures in that case.

@msekletar

Copy link
Copy Markdown
Contributor Author

My experience with these kind of setups is that they are an unmitigated disaster, because they are invariably used by services that cannot do process management right and get it awfully wrong, and manage to completely break the node. Can't the exact same result be obtained by simply using the FD store to pass FDs over? There should be no interruptions or connection closures in that case.

Not closing file descriptors (i.e. not dropping connections) is just part of the issue. More complex problem is usually state transfer from one invocation of the service to another. Not that many traditional services implement it because it is inherently difficult to do in C.

@bluca

bluca commented Apr 17, 2023

Copy link
Copy Markdown
Member

Yeah for my use case I'm having everyone who needs that switch to store state in memfds that can be passed over. I'd recommend you do the same if you have a specific use case scenario where this is coming from, as you are going to find yourself in a world of hurt when broken services start using this... A neverending cascade of processes using cpu/memory/disk resources and never terminating and so on. Not a pretty sight. Been there, done that ;-)

@fweimer-rh

Copy link
Copy Markdown
Contributor

Would this change address the issue that local users can impersonate services by crashing them if they do not listen on privileged ports? Previous discussion:

@msekletar
msekletar force-pushed the zero-downtime-restart branch 7 times, most recently from 5a87b1a to 078e21a Compare April 28, 2023 17:53
@msekletar
msekletar force-pushed the zero-downtime-restart branch from 078e21a to e51c298 Compare May 2, 2023 09:02
Main motivation for this work is to allow zero-downtime service
restarts. Currently the restart of a service unit is composed of two
main steps, stop of the currently running service which is then followed
by start of the service. This prevents the service from running
long-term after restart of the service is initiated. Therefore, clients
of the service can experience the disruption as their connection to the
service can be abruptly terminated. This can be partly mitigated by
setting using non-default KillMode= and adjusting TimeoutStopSec=
timeout. However, full mitigation of this behavior is currently not
possible.

This commit introduces a new systemd features that will allow an old
version of a service (generation) to continue running until it
voluntarily exits or the service is explicitly stopped. Main goal is to
allow for undisturbed handling of previously started work (e.g. client
connections, system management operations) to run into full completion
even if the service is restarted. It is expected that clients requesting
the service after the restart will communicate with the latest version
(generation) of the service.

This behavior will be achieved using a combination of two newly
introduced service unit options, RestartMode= and ExecRestart=.

Fixes: systemd#10228

TODO
----
- [bug] main service cgroup stays around after stop of the service
- [design] figure out behavior in combination with KillMode=process|mixed
- [design] is UNIT_MAINTAINANCE state appropriate for RestartMode=keep ?
- [bug] RestarMode property exported over dbus
- [bug] ExecRestart= is not exported over dbus
- [bug] Support for transient units
- [tests]
    - add test cases for different KillModes=
    - maybe rewrite the test using Beakerlib?
@msekletar
msekletar force-pushed the zero-downtime-restart branch from e51c298 to fe14590 Compare May 2, 2023 13:32
@msekletar

Copy link
Copy Markdown
Contributor Author

Would this change address the issue that local users can impersonate services by crashing them if they do not listen on privileged ports? Previous discussion:

I don't think that this feature would help with this problem.

@Werkov

Werkov commented May 2, 2023

Copy link
Copy Markdown
Contributor

I like:

  • generation in the environment
  • systemctl integration
  • covered socket activation

I don't like:

  • squashing all changes in one commit

I have yet to understand the lifecycle changes better. Soon (tm), I should be able to put forward an alternate approach, I've been cooking recently to pick the best of both ;-)

@poettering

poettering commented Jun 15, 2023 •

Copy link
Copy Markdown
Member

As discussed on that videocall that one time, we think it's nicer not to support something like this explicitly, but instead push people towards:

  1. passing over open fds via the fdstore from old to new instance
  2. serializing state to RuntimeDirectory= or a memfd in the fdstore
  3. passing over auxiliary processes that shall survive the service restart by moving them into their own scope unit (either placed there when they are created, or moved there shortly before the zero-downtime restart)

This should generally be a safer simpler outcome because we can maintain our guarantee that everything we start anew for the service comes with a clean set of process/security settings, cgroup parameters and so on.

We also discussed maybe simplifiyng step 3 by providing an asynchronous sd_notify() based API for moving processes to keep around to an auxiliary scope unit.

@poettering poettering added reviewed/needs-rework 🔨 PR has been reviewed and needs another round of reworks and removed please-review PR is ready for (re-)review by a maintainer labels Jun 15, 2023
@mikehearn

Copy link
Copy Markdown

Maybe it's worth trying to upgrade different types of program to use such a scheme, and then comparing? Off hand I don't see how it's possible to cheaply do that for even something quite simple like a static file server, as the state would need to be serialized for things like the entire TLS stack, the open fds of the file being served, etc. It could be done as an interesting academic exercise but it seems clear that the cost would be a lot higher than just letting new inbound connections be handled by the new instance and let the old instance finish when done. And once you get into app servers that can have arbitrarily complex connection state, it seems to become totally unrealistic given that other systems will happily let a transition between old and new instances happen with them both overlapping.

@bluca

bluca commented Jun 15, 2023

Copy link
Copy Markdown
Member

I'm not sure I buy that it's not possible to pass state around by FD, sure it requires some work, but so what, everything does. The alternative is just hopelessly broken by design.

@vcaputo

vcaputo commented Jun 15, 2023

Copy link
Copy Markdown
Contributor

Don't most service providers achieve this kind of thing at the network/"cloud" granularity these days? i.e. You direct users to the updated instances at the load balancer, and it's on you to provide resources for those lingering sessions pinning the old world if it's so important to provide Zero Down Time. Because at the end of the day, that's what's required to do it right in a generally correct fashion without shitting the bed in unpredictable ways - pinning resources to the old world until the refcount drops to zero. While ensuring the new world isn't stepping on the old's global state in the databases/filesystems/whatever snowflake external app-specific dependencies people are paid big bucks to understand and worry about. Do we really want systemd to get in that inherently messy business at the service level within a single host? It seems misguided to me.

At best I'd think systemd could maybe offer some features in the spirit of narrowing the service down time window as much as possible, more like Epsilon Down time, by permitting some pre-available-for-service startup stuff to overlap before replacement. But still having a clean break where the old is replaced with the new, and connections must be reestablished etc.

@bluca

bluca commented Jun 15, 2023

Copy link
Copy Markdown
Member

Yeah that's what the FD store and scopes are supposed to help with as mentioned above

@msekletar

Copy link
Copy Markdown
Contributor Author

Replaced by #28836

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

Development

Successfully merging this pull request may close these issues.

RFE: Zero downtime restarts for services

8 participants