Skip to content

Commit 6eca27e

Browse files
jlevequelguohan
authored andcommitted
[services] Restart SwSS service upon unexpected critical process exit (#2845)
* [service] Restart SwSS Docker container if orchagent exits unexpectedly * Configure systemd to stop restarting swss if it attempts to restart more than 3 times in 20 minutes * Move supervisor-proc-exit-listener script * [docker-dhcp-relay] Enhance wait_for_intf.sh.j2 to utilize STATEDB * Ensure dependent services stop/start/restart with SwSS * Change 'StartLimitInterval' to 'StartLimitIntervalSec', as Stretch installs systemd 232 (>= v230) * Also update journald.conf options * Remove 'PartOf' option from unit files * Add '$(SUPERVISOR_PROC_EXIT_LISTENER_SCRIPT)' to new shared docker-orchagent makefile * Make supervisor-proc-exit-listener script read from 'critical_processes' file inside container * Update critical_processes file for swss container
1 parent 2736da9 commit 6eca27e

File tree

15 files changed

+126
-55
lines changed

15 files changed

+126
-55
lines changed
+18-20
Original file line numberDiff line numberDiff line change
@@ -1,42 +1,40 @@
11
#!/usr/bin/env bash
22

3-
function wait_until_iface_ready
4-
{
5-
IFACE=$1
3+
STATE_DB_IDX="6"
64

7-
echo "Waiting until interface $IFACE is up..."
8-
9-
# Wait for the interface to come up (i.e., 'ip link show' returns 0)
10-
until ip link show dev $IFACE up > /dev/null 2>&1; do
11-
sleep 1
12-
done
5+
PORT_TABLE_PREFIX="PORT_TABLE"
6+
VLAN_TABLE_PREFIX="VLAN_TABLE"
7+
LAG_TABLE_PREFIX="LAG_TABLE"
138

14-
echo "Interface $IFACE is up"
9+
function wait_until_iface_ready
10+
{
11+
TABLE_PREFIX=$1
12+
IFACE=$2
1513

16-
echo "Waiting until interface $IFACE has an IPv4 address..."
14+
echo "Waiting until interface $IFACE is ready..."
1715

18-
# Wait until the interface gets assigned an IPv4 address
16+
# Wait for the interface to come up
17+
# (i.e., interface is present in STATE_DB and state is "ok")
1918
while true; do
20-
IP=$(ip -4 addr show dev $IFACE | grep "inet " | awk '{ print $2 }' | cut -d '/' -f1)
21-
22-
if [ -n "$IP" ]; then
19+
RESULT=$(redis-cli -n ${STATE_DB_IDX} HGET "${TABLE_PREFIX}|${IFACE}" "state" 2> /dev/null)
20+
if [ x"$RESULT" == x"ok" ]; then
2321
break
2422
fi
2523

2624
sleep 1
2725
done
2826

29-
echo "Interface $IFACE is configured with IP $IP"
27+
echo "Interface ${IFACE} is ready!"
3028
}
3129

3230

33-
# Wait for all interfaces to come up and have IPv4 addresses assigned
31+
# Wait for all interfaces to be up and ready
3432
{% for (name, prefix) in INTERFACE %}
35-
wait_until_iface_ready {{ name }}
33+
wait_until_iface_ready ${PORT_TABLE_PREFIX} {{ name }}
3634
{% endfor %}
3735
{% for (name, prefix) in VLAN_INTERFACE %}
38-
wait_until_iface_ready {{ name }}
36+
wait_until_iface_ready ${VLAN_TABLE_PREFIX} {{ name }}
3937
{% endfor %}
4038
{% for (name, prefix) in PORTCHANNEL_INTERFACE %}
41-
wait_until_iface_ready {{ name }}
39+
wait_until_iface_ready ${LAG_TABLE_PREFIX} {{ name }}
4240
{% endfor %}

dockers/docker-orchagent/Dockerfile.j2

+2
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,8 @@ COPY ["files/arp_update", "/usr/bin"]
4141
COPY ["enable_counters.py", "/usr/bin"]
4242
COPY ["start.sh", "orchagent.sh", "swssconfig.sh", "/usr/bin/"]
4343
COPY ["supervisord.conf", "/etc/supervisor/conf.d/"]
44+
COPY ["files/supervisor-proc-exit-listener", "/usr/bin"]
45+
COPY ["critical_processes", "/etc/supervisor/"]
4446

4547
## Copy all Jinja2 template files into the templates folder
4648
COPY ["*.j2", "/usr/share/sonic/templates/"]
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
orchagent
2+
portsyncd
3+
neighsyncd
4+
vlanmgrd
5+
intfmgrd
6+
portmgrd
7+
buffermgrd
8+
vrfmgrd
9+
nbrmgrd
10+
vxlanmgrd
11+
intfsyncd

dockers/docker-orchagent/supervisord.conf

+7-1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,12 @@ logfile_maxbytes=1MB
33
logfile_backups=2
44
nodaemon=true
55

6+
[eventlistener:supervisor-proc-exit-listener]
7+
command=/usr/bin/supervisor-proc-exit-listener
8+
events=PROCESS_STATE_EXITED
9+
autostart=true
10+
autorestart=unexpected
11+
612
[program:start.sh]
713
command=/usr/bin/start.sh
814
priority=1
@@ -15,7 +21,7 @@ stderr_logfile=syslog
1521
command=/usr/sbin/rsyslogd -n
1622
priority=2
1723
autostart=false
18-
autorestart=false
24+
autorestart=unexpected
1925
stdout_logfile=syslog
2026
stderr_logfile=syslog
2127

files/build_templates/dhcp_relay.service.j2

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,4 +11,4 @@ ExecStart=/usr/bin/{{ docker_container_name }}.sh wait
1111
ExecStop=/usr/bin/{{ docker_container_name }}.sh stop
1212

1313
[Install]
14-
WantedBy=multi-user.target teamd.service
14+
WantedBy=multi-user.target swss.service teamd.service

files/build_templates/radv.service.j2

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,4 +11,4 @@ ExecStart=/usr/bin/{{ docker_container_name }}.sh wait
1111
ExecStop=/usr/bin/{{ docker_container_name }}.sh stop
1212

1313
[Install]
14-
WantedBy=multi-user.target
14+
WantedBy=multi-user.target swss.service

files/build_templates/snmp.service.j2

+3
Original file line numberDiff line numberDiff line change
@@ -9,3 +9,6 @@ Before=ntp-config.service
99
ExecStartPre=/usr/bin/{{docker_container_name}}.sh start
1010
ExecStart=/usr/bin/{{docker_container_name}}.sh wait
1111
ExecStop=/usr/bin/{{docker_container_name}}.sh stop
12+
13+
[Install]
14+
WantedBy=multi-user.target swss.service

files/build_templates/swss.service.j2

+4
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,17 @@ Requires=nps-modules-4.9.0-8-2-amd64.service
99
After=database.service updategraph.service
1010
After=interfaces-config.service
1111
Before=ntp-config.service
12+
StartLimitIntervalSec=1200
13+
StartLimitBurst=3
1214

1315
[Service]
1416
User=root
1517
Environment=sonic_asic_platform={{ sonic_asic_platform }}
1618
ExecStartPre=/usr/local/bin/swss.sh start
1719
ExecStart=/usr/local/bin/swss.sh wait
1820
ExecStop=/usr/local/bin/swss.sh stop
21+
Restart=always
22+
RestartSec=30
1923

2024
[Install]
2125
WantedBy=multi-user.target

files/build_templates/teamd.service.j2

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[Unit]
22
Description=TEAMD container
3-
Requires=updategraph.service
3+
Requires=updategraph.service swss.service
44
After=updategraph.service swss.service
55
Before=ntp-config.service
66

@@ -11,4 +11,4 @@ ExecStart=/usr/bin/{{docker_container_name}}.sh wait
1111
ExecStop=/usr/bin/{{docker_container_name}}.sh stop
1212

1313
[Install]
14-
WantedBy=multi-user.target
14+
WantedBy=multi-user.target swss.service

files/image_config/systemd/journald.conf

+1-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
#Seal=yes
1414
#SplitMode=uid
1515
#SyncIntervalSec=5m
16-
#RateLimitInterval=30s
16+
#RateLimitIntervalSec=30s
1717
#RateLimitBurst=1000
1818
SystemMaxUse=50M
1919
#SystemKeepFree=
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
#!/usr/bin/env python
2+
3+
import os
4+
import signal
5+
import sys
6+
import syslog
7+
8+
from supervisor import childutils
9+
10+
# Contents of file should be the names of critical processes (as defined in
11+
# supervisor.conf file), one per line
12+
CRITICAL_PROCESSES_FILE = '/etc/supervisor/critical_processes'
13+
14+
def main():
15+
# Read the list of critical processes from a file
16+
with open(CRITICAL_PROCESSES_FILE, 'r') as f:
17+
critical_processes = [line.rstrip('\n') for line in f]
18+
19+
while True:
20+
# Transition from ACKNOWLEDGED to READY
21+
childutils.listener.ready()
22+
23+
line = sys.stdin.readline()
24+
headers = childutils.get_headers(line)
25+
payload = sys.stdin.read(int(headers['len']))
26+
27+
# Transition from READY to ACKNOWLEDGED
28+
childutils.listener.ok()
29+
30+
# We only care about PROCESS_STATE_EXITED events
31+
if headers['eventname'] == 'PROCESS_STATE_EXITED':
32+
payload_headers, payload_data = childutils.eventdata(payload + '\n')
33+
34+
expected = int(payload_headers['expected'])
35+
processname = payload_headers['processname']
36+
37+
# If a critical process exited unexpectedly, terminate supervisor
38+
if expected == 0 and processname in critical_processes:
39+
MSG_FORMAT_STR = "Process {} exited unxepectedly. Terminating supervisor..."
40+
msg = MSG_FORMAT_STR.format(payload_headers['processname'])
41+
syslog.syslog(syslog.LOG_INFO, msg)
42+
os.kill(os.getppid(), signal.SIGTERM)
43+
44+
if __name__ == "__main__":
45+
main()

rules/docker-dhcp-relay.mk

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ DOCKER_DHCP_RELAY_DBG = $(DOCKER_DHCP_RELAY_STEM)-$(DBG_IMAGE_MARK).gz
66

77
$(DOCKER_DHCP_RELAY)_PATH = $(DOCKERS_PATH)/$(DOCKER_DHCP_RELAY_STEM)
88

9-
$(DOCKER_DHCP_RELAY)_DEPENDS += $(ISC_DHCP_COMMON) $(ISC_DHCP_RELAY)
9+
$(DOCKER_DHCP_RELAY)_DEPENDS += $(ISC_DHCP_COMMON) $(ISC_DHCP_RELAY) $(REDIS_TOOLS)
1010
$(DOCKER_DHCP_RELAY)_DBG_DEPENDS = $($(DOCKER_CONFIG_ENGINE_STRETCH)_DBG_DEPENDS)
1111
$(DOCKER_DHCP_RELAY)_DBG_IMAGE_PACKAGES = $($(DOCKER_CONFIG_ENGINE_STRETCH)_DBG_IMAGE_PACKAGES)
1212

rules/docker-orchagent.mk

+1-1
Original file line numberDiff line numberDiff line change
@@ -34,4 +34,4 @@ $(DOCKER_ORCHAGENT)_RUN_OPT += -v /etc/sonic:/etc/sonic:ro
3434
$(DOCKER_ORCHAGENT)_RUN_OPT += -v /var/log/swss:/var/log/swss:rw
3535

3636
$(DOCKER_ORCHAGENT)_BASE_IMAGE_FILES += swssloglevel:/usr/bin/swssloglevel
37-
$(DOCKER_ORCHAGENT)_FILES += $(ARP_UPDATE_SCRIPT)
37+
$(DOCKER_ORCHAGENT)_FILES += $(ARP_UPDATE_SCRIPT) $(SUPERVISOR_PROC_EXIT_LISTENER_SCRIPT)

rules/scripts.mk

+5-1
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,13 @@ $(BUFFERS_CONFIG_TEMPLATE)_PATH = files/build_templates
1111
QOS_CONFIG_TEMPLATE = qos_config.j2
1212
$(QOS_CONFIG_TEMPLATE)_PATH = files/build_templates
1313

14+
SUPERVISOR_PROC_EXIT_LISTENER_SCRIPT = supervisor-proc-exit-listener
15+
$(SUPERVISOR_PROC_EXIT_LISTENER_SCRIPT)_PATH = files/scripts
16+
1417
SONIC_COPY_FILES += $(CONFIGDB_LOAD_SCRIPT) \
1518
$(ARP_UPDATE_SCRIPT) \
1619
$(BUFFERS_CONFIG_TEMPLATE) \
17-
$(QOS_CONFIG_TEMPLATE)
20+
$(QOS_CONFIG_TEMPLATE) \
21+
$(SUPERVISOR_PROC_EXIT_LISTENER_SCRIPT)
1822

1923

Original file line numberDiff line numberDiff line change
@@ -1,43 +1,41 @@
11
#!/usr/bin/env bash
22

3-
function wait_until_iface_ready
4-
{
5-
IFACE=$1
3+
STATE_DB_IDX="6"
64

7-
echo "Waiting until interface $IFACE is up..."
8-
9-
# Wait for the interface to come up (i.e., 'ip link show' returns 0)
10-
until ip link show dev $IFACE up > /dev/null 2>&1; do
11-
sleep 1
12-
done
5+
PORT_TABLE_PREFIX="PORT_TABLE"
6+
VLAN_TABLE_PREFIX="VLAN_TABLE"
7+
LAG_TABLE_PREFIX="LAG_TABLE"
138

14-
echo "Interface $IFACE is up"
9+
function wait_until_iface_ready
10+
{
11+
TABLE_PREFIX=$1
12+
IFACE=$2
1513

16-
echo "Waiting until interface $IFACE has an IPv4 address..."
14+
echo "Waiting until interface $IFACE is ready..."
1715

18-
# Wait until the interface gets assigned an IPv4 address
16+
# Wait for the interface to come up
17+
# (i.e., interface is present in STATE_DB and state is "ok")
1918
while true; do
20-
IP=$(ip -4 addr show dev $IFACE | grep "inet " | awk '{ print $2 }' | cut -d '/' -f1)
21-
22-
if [ -n "$IP" ]; then
19+
RESULT=$(redis-cli -n ${STATE_DB_IDX} HGET "${TABLE_PREFIX}|${IFACE}" "state" 2> /dev/null)
20+
if [ x"$RESULT" == x"ok" ]; then
2321
break
2422
fi
2523

2624
sleep 1
2725
done
2826

29-
echo "Interface $IFACE is configured with IP $IP"
27+
echo "Interface ${IFACE} is ready!"
3028
}
3129

3230

33-
# Wait for all interfaces to come up and have IPv4 addresses assigned
34-
wait_until_iface_ready Vlan1000
35-
wait_until_iface_ready PortChannel01
36-
wait_until_iface_ready PortChannel01
37-
wait_until_iface_ready PortChannel02
38-
wait_until_iface_ready PortChannel02
39-
wait_until_iface_ready PortChannel03
40-
wait_until_iface_ready PortChannel03
41-
wait_until_iface_ready PortChannel04
42-
wait_until_iface_ready PortChannel04
31+
# Wait for all interfaces to be up and ready
32+
wait_until_iface_ready ${VLAN_TABLE_PREFIX} Vlan1000
33+
wait_until_iface_ready ${LAG_TABLE_PREFIX} PortChannel01
34+
wait_until_iface_ready ${LAG_TABLE_PREFIX} PortChannel01
35+
wait_until_iface_ready ${LAG_TABLE_PREFIX} PortChannel02
36+
wait_until_iface_ready ${LAG_TABLE_PREFIX} PortChannel02
37+
wait_until_iface_ready ${LAG_TABLE_PREFIX} PortChannel03
38+
wait_until_iface_ready ${LAG_TABLE_PREFIX} PortChannel03
39+
wait_until_iface_ready ${LAG_TABLE_PREFIX} PortChannel04
40+
wait_until_iface_ready ${LAG_TABLE_PREFIX} PortChannel04
4341

0 commit comments

Comments
 (0)