Skip to content
This repository has been archived by the owner on Dec 4, 2024. It is now read-only.

Commit

Permalink
Emulate behaviour of haproxy-systemd-wrapper. (#390)
Browse files Browse the repository at this point in the history
  • Loading branch information
brndnmtthws authored Jan 18, 2017
1 parent 8b0bdba commit e493bad
Show file tree
Hide file tree
Showing 5 changed files with 53 additions and 10 deletions.
2 changes: 1 addition & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ RUN set -x \
&& export GNUPGHOME="$(mktemp -d)" \
&& gpg --keyserver ha.pool.sks-keyservers.net --recv-keys "$TINI_GPG_KEY" \
&& gpg --batch --verify tini.asc tini \
&& rm -r "$GNUPGHOME" tini.asc \
&& rm -rf "$GNUPGHOME" tini.asc \
&& mv tini /usr/bin/tini \
&& chmod +x /usr/bin/tini \
&& tini -- true \
Expand Down
1 change: 0 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -278,7 +278,6 @@ The default value when not specified is `redispatch,http-server-close,dontlognul
< HTTP/1.1 200 OK
```
* Some of the features of marathon-lb assume that it is the only instance of itself running in a PID namespace. i.e. marathon-lb assumes that it is running in a container. Certain features like the `/_mlb_signal` endpoints and the `/_haproxy_getpids` endpoint (and by extension, zero-downtime deployments) may behave unexpectedly if more than one instance of marathon-lb is running in the same PID namespace or if there are other HAProxy processes in the same PID namespace.
* You may want to set the `HAPROXY_RELOAD_SIGTERM_DELAY` environment variable to a value such as `5m`. This value is passed directly to the `sleep` command, which is executed after every HAProxy reload before sending a SIGTERM to the old HAProxy PIDs (see [service/haproxy/run](service/haproxy/run)). For cases where you expect long-lived TCP connections, you may _not_ want to terminate HAProxy before all connections finish. See [this discussion](http://www.serverphorums.com/read.php?10,862139) for more on HAProxy reloads, and issues [#5](https://github.com/mesosphere/marathon-lb/issues/5), [#71](https://github.com/mesosphere/marathon-lb/issues/71), [#267](https://github.com/mesosphere/marathon-lb/issues/267), [#276](https://github.com/mesosphere/marathon-lb/issues/276), and [#318](https://github.com/mesosphere/marathon-lb/issues/318) for more. If you are reloading so frequently that PIDs are being reused within the delay you specify, this may result in SIGTERMs being sent to the wrong PIDs.

## Zero-downtime Deployments

Expand Down
48 changes: 48 additions & 0 deletions haproxy_wrapper.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
#!/usr/bin/env python3
import os
import sys
import time
import errno


def create_haproxy_pipe():
pipefd = os.pipe()
return pipefd


def close_and_swallow(fd):
try:
os.close(fd)
except OSError:
# swallow
pass


def wait_on_haproxy_pipe(pipefd):
try:
ret = os.read(pipefd[0], 1)
if len(ret) == 0:
close_and_swallow(pipefd[0])
close_and_swallow(pipefd[1])
except OSError as e:
if e.args[0] != errno.EINTR:
close_and_swallow(pipefd[0])
close_and_swallow(pipefd[1])
return False
return True

pipefd = create_haproxy_pipe()

pid = os.fork()

if not pid:
os.environ["HAPROXY_WRAPPER_FD"] = str(pipefd[1])
# Close the read side
os.close(pipefd[0])
os.execv(sys.argv[1], sys.argv[1:])

# Close the write side
os.close(pipefd[1])
while wait_on_haproxy_pipe(pipefd):
time.sleep(0.005)
sys.exit(0)
5 changes: 0 additions & 5 deletions run
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,6 @@ else
exit 1
fi

if [ -n "${HAPROXY_RELOAD_SIGTERM_DELAY-}" ]; then
echo $HAPROXY_RELOAD_SIGTERM_DELAY > $HAPROXY_SERVICE/env/HAPROXY_RELOAD_SIGTERM_DELAY
fi


# Find the --ssl-certs arg if one was provided,
# get the certs and remove them and the arg from the list
# of positional parameters so we don't duplicate them
Expand Down
7 changes: 4 additions & 3 deletions service/haproxy/run
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,10 @@ reload() {

# Trigger reload
LATEST_HAPROXY_PID=$(cat $PIDFILE)
haproxy -p $PIDFILE -f /marathon-lb/haproxy.cfg -D -sf $LATEST_HAPROXY_PID 200>&-
if [ -n "${HAPROXY_RELOAD_SIGTERM_DELAY-}" ]; then
sleep $HAPROXY_RELOAD_SIGTERM_DELAY && kill $LATEST_HAPROXY_PID 200>&- 2>/dev/null &
/marathon-lb/haproxy_wrapper.py `which haproxy` -p $PIDFILE -f /marathon-lb/haproxy.cfg -sf $LATEST_HAPROXY_PID 200>&-
local exit_code=$?
if [ $exit_code -ne 0 ]; then
echo "HAProxy reload failed" 1>&2
fi

# Remove the firewall rules
Expand Down

0 comments on commit e493bad

Please sign in to comment.