Skip to content

RFE: Zero downtime restarts for services #10228

Closed

Description

@jaymzh

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.

Activity

  1. added
    RFE 🎁
    on Oct 2, 2018
  2. msekletar commented on Jul 1, 2022

    @msekletar
    Contributor

    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.

  3. Werkov commented on Jul 1, 2022

    @Werkov
    Contributor

    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).

  4. msekletar commented on Jul 1, 2022

    @msekletar
    Contributor

    @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.

  5. msekletar commented on Jul 1, 2022

    @msekletar
    Contributor

    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.

  6. vakabus commented on Jul 5, 2022

    @vakabus

    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:

    1. service running
    2. restart job started
    3. launch ExecPassivate= and wait for it to exit
    4. launch new generation and pass all FDs from FDStore to the service, set an environment variable indicating non-first generation
    5. 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.

  7. msekletar commented on Nov 28, 2022

    @msekletar
    Contributor

    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.

  8. vakabus commented on Nov 29, 2022

    @vakabus

    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

  9. mikehearn commented on Mar 1, 2023

    @mikehearn

    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):

    1. 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.
    2. 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.
  10. added a commit that references this issue on Apr 17, 2023
    6a20b7c
  11. added 9 commits that reference this issue on Apr 24, 2023
    ab9373c
    8876773
    b9ea794
    7767b45
    5a87b1a
    078e21a
    e51c298
    fe14590
  12. poettering commented on Jun 15, 2023

    @poettering
    Member

    Please see #27309 (comment) for the results of the latest discussion about the concept.

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    RFE 🎁pid1

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions