Merge lp:~tcuthbert/ubuntu-repository-cache/squid-mem-qos into lp:ubuntu-repository-cache

Proposed by Thomas Cuthbert
Status: Rejected
Rejected by: Haw Loeung
Proposed branch: lp:~tcuthbert/ubuntu-repository-cache/squid-mem-qos
Merge into: lp:ubuntu-repository-cache
Diff against target: 227 lines (+148/-1)
5 files modified
config.yaml (+18/-0)
lib/ubuntu_repository_cache/service.py (+1/-0)
lib/ubuntu_repository_cache/squid.py (+69/-1)
lib/ubuntu_repository_cache/util.py (+52/-0)
templates/squid-deb-proxy.service.d/squid-deb-proxy.conf (+8/-0)
To merge this branch: bzr merge lp:~tcuthbert/ubuntu-repository-cache/squid-mem-qos
Reviewer Review Type Date Requested Status
Ubuntu Repository Cache Charmers, Canonical Pending
Review via email: mp+429532@code.launchpad.net
To post a comment you must log in.
Revision history for this message
Haw Loeung (hloeung) wrote :

Comments inline. I tested and confirmed memory limits are applied - https://pastebin.canonical.com/p/3Jh3mSBJfV/

Revision history for this message
Haw Loeung (hloeung) wrote :
Revision history for this message
Haw Loeung (hloeung) wrote :

Unmerged revisions

374. By Thomas Cuthbert

WIP: squid systemd memory qos

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
=== modified file 'config.yaml'
--- config.yaml 2022-08-22 02:45:49 +0000
+++ config.yaml 2022-09-06 23:31:29 +0000
@@ -76,6 +76,24 @@
76 effect running units, only newly added units. An example would be76 effect running units, only newly added units. An example would be
77 '/dev/xvdb,/dev/xvdc' to specify two ephemeral disks for cache77 '/dev/xvdb,/dev/xvdc' to specify two ephemeral disks for cache
78 storage.78 storage.
79 squid-memory-high:
80 default: 90%
81 type: string
82 description: |
83 Configurable systemd.resource-control for high squid memory. Takes a
84 memory size in either bytes with an optional suffix or a percentage
85 relative to physical system memory. The series must be xenial or
86 higher for this to take effect.
87 See man systemd.resource-control for details.
88 squid-memory-max:
89 default: 95%
90 type: string
91 description: |
92 Configurable systemd.resource-control for maximum squid memory. Takes a
93 memory size in either bytes with an optional suffix or a percentage
94 relative to physical system memory. The series must be xenial or
95 higher for this to take effect.
96 See man systemd.resource-control for details.
79 cache-memory-size:97 cache-memory-size:
80 default: 098 default: 0
81 type: int99 type: int
82100
=== modified file 'lib/ubuntu_repository_cache/service.py'
--- lib/ubuntu_repository_cache/service.py 2022-08-24 04:03:17 +0000
+++ lib/ubuntu_repository_cache/service.py 2022-09-06 23:31:29 +0000
@@ -190,6 +190,7 @@
190 """190 """
191191
192 return {192 return {
193 ('/etc/systemd/system/squid-deb-proxy.service.d/', 'squid-deb-proxy.conf'): ['squid-deb-proxy'],
193 '/etc/squid-deb-proxy/squid-deb-proxy.conf': ['squid-deb-proxy'],194 '/etc/squid-deb-proxy/squid-deb-proxy.conf': ['squid-deb-proxy'],
194 '/etc/squid-deb-proxy/allowed-networks-src.acl': ['squid-deb-proxy'],195 '/etc/squid-deb-proxy/allowed-networks-src.acl': ['squid-deb-proxy'],
195 ('/etc/squid-deb-proxy/mirror-dstdomain.acl.d/' '99-ubuntu-repository-cache'): ['squid-deb-proxy'],196 ('/etc/squid-deb-proxy/mirror-dstdomain.acl.d/' '99-ubuntu-repository-cache'): ['squid-deb-proxy'],
196197
=== modified file 'lib/ubuntu_repository_cache/squid.py'
--- lib/ubuntu_repository_cache/squid.py 2022-08-22 02:49:47 +0000
+++ lib/ubuntu_repository_cache/squid.py 2022-09-06 23:31:29 +0000
@@ -1,8 +1,11 @@
1'''Functions related to squid-deb-proxy use within this charm'''1'''Functions related to squid-deb-proxy use within this charm'''
22
3import filecmp
3import os4import os
5import re
6import shutil
4import subprocess7import subprocess
5import re8import tempfile
69
7from charmhelpers import fetch10from charmhelpers import fetch
811
@@ -80,6 +83,68 @@
80 )83 )
8184
8285
86@util.run_as_user('root')
87def configure_systemd_override():
88 """Configure squid-deb-proxy with systemd-resource controls.
89
90 1. shallow[^1] compare config changes from tempdir
91 2. safely[^2] move configs into /etc/systemd/system
92 - cleanup any leftover files from /etc/systemd on error
93 3. call `systemctl daemon-reload`, handling OS related errors where possible
94
95 [^1]: https://docs.python.org/3/library/filecmp.html#the-dircmp-class
96 [^2]: https://docs.python.org/3/library/shutil.html#shutil.move
97 """
98 context = {}
99 config = hookenv.config()
100
101 LOG("Installing squid-deb-proxy systemd service override configuration", hookenv.INFO)
102 context["squid_memory_high"] = config.get("squid-memory-max", "90%")
103 context["squid_memory_max"] = config.get("squid-memory-max", "95%")
104
105 dirname = "/etc/systemd/system/squid-deb-proxy.service.d"
106 configdir_exists = os.path.exists(dirname)
107 cleanup_files = []
108
109 with tempfile.TemporaryDirectory() as tmpdirname:
110 charm_squid_path = os.path.join(os.getcwd(), "templates/squid-deb-proxy.service.d")
111 temp_squid_path = os.path.join(tmpdirname, "templates/squid-deb-proxy.service.d")
112
113 shutil.copytree(charm_squid_path, temp_squid_path)
114 templating.render(
115 "squid-deb-proxy.service.d/" + "squid-deb-proxy.conf",
116 os.path.join(temp_squid_path, "squid-deb-proxy.conf"),
117 context,
118 )
119
120 config_files = [os.path.join(dirname, f) for f in os.listdir(temp_squid_path)]
121 if configdir_exists:
122 config_files = [os.path.join(dirname, f) for f in filecmp.dircmp(temp_squid_path, dirname).diff_files]
123
124 try:
125 shutil.move(temp_squid_path, "/etc/systemd/system/")
126 verified = util.systemd_verify("squid-deb-proxy.service")
127
128 except Exception as e:
129 cleanup_files.extend(config_files)
130 err_msg = "Installing squid-deb-proxy.service.d files failed: {0}, {err}".format(dirname, err=e)
131 LOG(err_msg, hookenv.ERROR)
132 raise e from None
133
134 else:
135 LOG("Reloading systemd daemons", hookenv.INFO)
136 with util.systemctl_daemon_reload() as (reloaded, stderr):
137 if reloaded and verified:
138 LOG("squid-deb-proxy.service.d overrides reconfigured, restarting squid", hookenv.INFO)
139 host.service_restart('squid-deb-proxy')
140 else:
141 LOG("squid-deb-proxy.service.d in an unexpected state: {}".format(stderr), hookenv.ERROR)
142
143 finally:
144 for f in (f for f in cleanup_files):
145 os.remove(f)
146
147
83def install():148def install():
84 '''Perform the install/config and disable avahi'''149 '''Perform the install/config and disable avahi'''
85150
@@ -107,6 +172,7 @@
107 stop()172 stop()
108173
109 render_configs()174 render_configs()
175 configure_systemd_override()
110 init_caches()176 init_caches()
111177
112178
@@ -246,6 +312,8 @@
246 dstdomain_context,312 dstdomain_context,
247 )313 )
248314
315 configure_systemd_override()
316
249317
250def update_checks(nrpe_config):318def update_checks(nrpe_config):
251 '''Update Nagios checks for squid serving archive pool content'''319 '''Update Nagios checks for squid serving archive pool content'''
252320
=== modified file 'lib/ubuntu_repository_cache/util.py'
--- lib/ubuntu_repository_cache/util.py 2022-08-29 02:29:43 +0000
+++ lib/ubuntu_repository_cache/util.py 2022-09-06 23:31:29 +0000
@@ -7,6 +7,8 @@
7import socket7import socket
8import subprocess8import subprocess
99
10from contextlib import contextmanager
11
10from charmhelpers.contrib.hahelpers import cluster12from charmhelpers.contrib.hahelpers import cluster
11from charmhelpers.core import (13from charmhelpers.core import (
12 hookenv,14 hookenv,
@@ -167,6 +169,56 @@
167 return int(output[1].split()[1])169 return int(output[1].split()[1])
168170
169171
172def has_systemd():
173 release = host.lsb_release()['DISTRIB_RELEASE']
174 rv = True
175 if release < '16.04': # before xenial
176 rv = False
177 return rv
178
179
180def systemd_verify(unit_file, timeout=5):
181 args = ["systemd-analyze", "verify", "--system", "--generators", unit_file]
182 ok = 0
183 try:
184 return subprocess.check_call(args, timeout=timeout) == ok
185 except subprocess.CalledProcessError as cpe:
186 LOG("failed to verify systemd override configuration: {}".format(cpe), hookenv.ERROR)
187 raise cpe from None
188 except subprocess.TimeoutExpired as te:
189 LOG("timed out verifying systemd override files: {}".format(te), hookenv.ERROR)
190 raise te from None
191
192
193@contextmanager
194def systemctl_daemon_reload(timeout=5):
195 """
196 contextmanager around the systemctl daemon-reload command.
197
198 Defaults with a 5s timeout that will send SIGTERM on expiry.
199 Yields true and stderr if the process returned 0.
200 Attempts to cleanup the process with SIGKILL[^1] if it's wedged.
201
202 [^1]: https://docs.python.org/3/library/subprocess.html#subprocess.Popen.returncode
203 """
204 cmd = ["/bin/systemctl", "daemon-reload"]
205 proc = subprocess.Popen(cmd, stderr=subprocess.PIPE)
206 stderr = None
207 ok = 0
208 try:
209 _, stderr = proc.communicate(timeout=timeout)
210 yield proc.returncode == ok, stderr
211 except subprocess.TimeoutExpired:
212 LOG("Timeout reloading squid-deb-proxy systemd override config", hookenv.ERROR)
213 finally:
214 proc.terminate()
215 try:
216 proc.wait(timeout=2)
217 except subprocess.TimeoutExpired:
218 LOG("systemctl daemon-reload did not terminate, attempting SIGKILL", hookenv.ERROR)
219 proc.kill()
220
221
170def change_aptsources_url(url='http://archive.ubuntu.com/ubuntu'):222def change_aptsources_url(url='http://archive.ubuntu.com/ubuntu'):
171 """Change the URL for the current apt sources223 """Change the URL for the current apt sources
172224
173225
=== added directory 'templates/squid-deb-proxy.service.d'
=== added file 'templates/squid-deb-proxy.service.d/squid-deb-proxy.conf'
--- templates/squid-deb-proxy.service.d/squid-deb-proxy.conf 1970-01-01 00:00:00 +0000
+++ templates/squid-deb-proxy.service.d/squid-deb-proxy.conf 2022-09-06 23:31:29 +0000
@@ -0,0 +1,8 @@
1[Unit]
2StartLimitIntervalSec=3m
3StartLimitBurst=3
4
5[Service]
6MemoryHigh={{ squid_memory_high }}
7MemoryMax={{ squid_memory_max }}
8Restart=on-failure

Subscribers

People subscribed via source and target branches