Merge lp:~hloeung/ubuntu-repository-cache/cache-memory-tuning into lp:ubuntu-repository-cache

Proposed by Haw Loeung
Status: Merged
Approved by: Haw Loeung
Approved revision: 293
Merged at revision: 291
Proposed branch: lp:~hloeung/ubuntu-repository-cache/cache-memory-tuning
Merge into: lp:ubuntu-repository-cache
Diff against target: 187 lines (+84/-20)
4 files modified
config.yaml (+6/-0)
lib/ubuntu_repository_cache/squid.py (+33/-15)
lib/ubuntu_repository_cache/tests/test_squid.py (+43/-3)
templates/squid-deb-proxy/squid-deb-proxy.conf (+2/-2)
To merge this branch: bzr merge lp:~hloeung/ubuntu-repository-cache/cache-memory-tuning
Reviewer Review Type Date Requested Status
Stuart Bishop (community) Approve
Canonical IS Reviewers Pending
Review via email: mp+394925@code.launchpad.net

Commit message

Allow runtime tuning of memory used by squid

Description of the change

Before:

$ juju run --application ubuntu-repository-cache 'grep cache_mem /etc/squid-deb-proxy/squid-deb-proxy.conf'
- Stdout: |
    cache_mem 1428 MB
  UnitId: ubuntu-repository-cache/0
- Stdout: |
    cache_mem 256 MB
  UnitId: ubuntu-repository-cache/2
- Stdout: |
    cache_mem 256 MB
  UnitId: ubuntu-repository-cache/1

After:

$ juju run --application ubuntu-repository-cache 'free -g | grep -v 'Swap'; grep -E "^(cache_mem|cache_dir)" /etc/squid-deb-proxy/squid-deb-proxy.conf'
- Stdout: |2
                  total used free shared buff/cache available
    Mem: 13 0 2 0 10 12
    cache_dir aufs /srv/ubuntu-repository-cache/squid 50000 16 256
    cache_mem 3588 MB
  UnitId: ubuntu-repository-cache/0
- Stdout: |2
                  total used free shared buff/cache available
    Mem: 6 0 2 0 3 5
    cache_dir aufs /srv/ubuntu-repository-cache/squid 50000 16 256
    cache_mem 1241 MB
  UnitId: ubuntu-repository-cache/1
- Stdout: |2
                  total used free shared buff/cache available
    Mem: 6 0 2 0 3 5
    cache_dir aufs /srv/ubuntu-repository-cache/squid 50000 16 256
    cache_mem 1241 MB
  UnitId: ubuntu-repository-cache/2

To post a comment you must log in.
Revision history for this message
🤖 Canonical IS Merge Bot (canonical-is-mergebot) wrote :

This merge proposal is being monitored by mergebot. Change the status to Approved to merge.

292. By Haw Loeung

Tune squid cache memory usage reducing from 33% to 30%

Revision history for this message
Stuart Bishop (stub) wrote :

size_caches and cache_mem could do with some consistency using full stops. Code all looks good. One of your mocks should be adjusted per inline comments, although I doubt it will ever make a real difference to tests.

review: Approve
293. By Haw Loeung

Fixed based on reviews

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

Fixed description with regards to using full stops.

Revision history for this message
🤖 Canonical IS Merge Bot (canonical-is-mergebot) wrote :

Change successfully merged at revision 291

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== modified file 'config.yaml'
2--- config.yaml 2020-10-28 05:21:42 +0000
3+++ config.yaml 2020-12-07 03:36:38 +0000
4@@ -71,6 +71,12 @@
5 effect running units, only newly added units. An example would be
6 '/dev/xvdb,/dev/xvdc' to specify two ephemeral disks for cache
7 storage.
8+ cache-memory-size:
9+ default: 0
10+ type: int
11+ description: |
12+ Configurable option (in MBytes) to tune/override memory used by
13+ squid cache. If 0 or unset, then auto-calculate.
14 cache-storage-size:
15 default: 0
16 type: int
17
18=== modified file 'lib/ubuntu_repository_cache/squid.py'
19--- lib/ubuntu_repository_cache/squid.py 2020-10-06 10:04:18 +0000
20+++ lib/ubuntu_repository_cache/squid.py 2020-12-07 03:36:38 +0000
21@@ -35,18 +35,21 @@
22 subprocess.check_call(['/usr/sbin/squid3', '-N', '-z', '-f', '/etc/squid-deb-proxy/squid-deb-proxy.conf'])
23
24
25-def size_caches():
26- """Determine the sizes for in-memory/on-disk squid caches
27-
28- This must be run at installation time. Runtime tuning is unsupported.
29- """
30-
31- avail_disk = 0
32- for cache in unitdata.kv().get('squid-disk-caches'):
33- avail_disk += cache[1]
34- LOG('{}MB of disk cache available for squid.'.format(avail_disk), hookenv.DEBUG)
35-
36- memory = util.system_total_mem() / 3
37+def size_caches(system_total_memory=0):
38+ """Determine the sizes for in-memory/on-disk squid caches."""
39+
40+ config = hookenv.config()
41+ avail_disk = config.get('cache-storage-size', 0)
42+ if not avail_disk:
43+ for cache in unitdata.kv().get('squid-disk-caches'):
44+ avail_disk += cache[1]
45+ LOG('{}MB of disk cache available for squid.'.format(avail_disk), hookenv.DEBUG)
46+
47+ memory = system_total_memory
48+ if not system_total_memory:
49+ memory = util.system_total_mem()
50+ # Set aside 30% of system memory for squid memory cache.
51+ memory = memory * 0.30
52 # Estimate of 100MB of overhead for squid
53 memory = max(memory - 100, 0)
54
55@@ -103,7 +106,6 @@
56 host.service_pause(squid_service)
57 stop()
58
59- size_caches()
60 render_configs()
61 init_caches()
62
63@@ -165,6 +167,22 @@
64 return cache_str
65
66
67+def cache_mem(system_total_memory=0):
68+ """Generate 'cache_mem' lines for the squid configuration file.
69+
70+ Returns a string containing the cache_mem config
71+ """
72+ size_caches(system_total_memory)
73+
74+ config = hookenv.config()
75+ cache_mem = config.get('cache-memory-size', 0)
76+ if not cache_mem:
77+ cache_mem = unitdata.kv().get('squid-cache-mem')
78+
79+ cache_str = 'cache_mem {} MB'.format(str(cache_mem))
80+ return cache_str
81+
82+
83 @util.run_as_user('root')
84 def render_configs():
85 '''Render the configuration templates for all required services'''
86@@ -172,8 +190,8 @@
87 LOG('Rendering squid configuration templates')
88 config = hookenv.config()
89 context = {}
90- context['Cache_Config'] = cache_dirs()
91- context['Cache_Mem'] = unitdata.kv().get('squid-cache-mem')
92+ context['Cache_Storage_Config'] = cache_dirs()
93+ context['Cache_Memory_Config'] = cache_mem()
94 context['Cache_Peers'] = peer_config()
95 context['Cache_Hostname'] = hookenv.unit_private_ip()
96 if config['squid_snmp']:
97
98=== modified file 'lib/ubuntu_repository_cache/tests/test_squid.py'
99--- lib/ubuntu_repository_cache/tests/test_squid.py 2020-08-11 04:12:28 +0000
100+++ lib/ubuntu_repository_cache/tests/test_squid.py 2020-12-07 03:36:38 +0000
101@@ -12,17 +12,23 @@
102 class SquidTestCase(unittest.TestCase):
103 def setUp(self):
104 self.maxDiff = None
105+ self.tmpdir = tempfile.mkdtemp(prefix='charm-unittests-')
106+ self.addCleanup(shutil.rmtree, self.tmpdir)
107+
108 patcher = mock.patch('charmhelpers.core.hookenv.config')
109 self.mock_config = patcher.start()
110 self.addCleanup(patcher.stop)
111
112- self.tmpdir = tempfile.mkdtemp(prefix='charm-unittests-')
113- self.addCleanup(shutil.rmtree, self.tmpdir)
114+ patcher = mock.patch('charmhelpers.core.hookenv.log')
115+ self.mock_log = patcher.start()
116+ self.addCleanup(patcher.stop)
117+
118 os.environ['UNIT_STATE_DB'] = os.path.join(self.tmpdir, '.unit-state.db')
119+
120+ def test_squid_cache_size(self):
121 unitdata.kv().set('squid-cache-disk', 5000)
122 unitdata.kv().set('squid-disk-caches', [('/srv', 2000)])
123
124- def test_squid_cache_size(self):
125 self.mock_config.return_value = {
126 'squid-cache-disk': 20 * 1024,
127 'squid-disk-caches': [['/srv', 2000]],
128@@ -38,3 +44,37 @@
129 }
130 result = squid.cache_dirs()
131 self.assertEqual(result, 'cache_dir aufs /srv 1000 16 256\n')
132+
133+ def test_squid_cache_mem(self):
134+ # Lots of system RAM, 14G per Azure D3_v2. 50GB cache, auto calculate memory usage.
135+ self.mock_config.return_value = {
136+ 'cache-storage-size': 50000,
137+ 'cache-memory-size': 0,
138+ }
139+ result = squid.cache_mem(13997)
140+ self.assertEqual(result, 'cache_mem 3122 MB')
141+
142+ # 7GB per Azure D2_v2. 50GB cache, auto calculate memory usage.
143+ self.mock_config.return_value = {
144+ 'cache-storage-size': 50000,
145+ 'cache-memory-size': 0,
146+ }
147+ result = squid.cache_mem(6954)
148+ self.assertEqual(result, 'cache_mem 1009 MB')
149+
150+ # 7GB per Azure D2_v2. 50GB cache, fixed 1000 MB.
151+ self.mock_config.return_value = {
152+ 'cache-storage-size': 50000,
153+ 'cache-memory-size': 1000,
154+ }
155+ result = squid.cache_mem(6954)
156+ self.assertEqual(result, 'cache_mem 1000 MB')
157+
158+ # 7GB per Azure D2_v2. Disk with 50GB, auto calculate memory usage.
159+ unitdata.kv().set('squid-disk-caches', [('/srv', 50000)])
160+ self.mock_config.return_value = {
161+ 'cache-storage-size': 0,
162+ 'cache-memory-size': 0,
163+ }
164+ result = squid.cache_mem(6954)
165+ self.assertEqual(result, 'cache_mem 1009 MB')
166
167=== modified file 'templates/squid-deb-proxy/squid-deb-proxy.conf'
168--- templates/squid-deb-proxy/squid-deb-proxy.conf 2020-04-14 12:55:36 +0000
169+++ templates/squid-deb-proxy/squid-deb-proxy.conf 2020-12-07 03:36:38 +0000
170@@ -39,7 +39,7 @@
171 maximum_object_size 512 MB
172
173 # cache directory
174-{{ Cache_Config }}
175+{{ Cache_Storage_Config }}
176
177 # Cache swap more aggressive to avoid having the cache size going above the cache_dir size parameter
178 cache_swap_low 80
179@@ -51,7 +51,7 @@
180 #access_log daemon:/var/log/squid-deb-proxy/access.log
181
182 # tweaks to speed things up
183-cache_mem {{ Cache_Mem }} MB
184+{{ Cache_Memory_Config }}
185 maximum_object_size_in_memory 30 MB
186
187 collapsed_forwarding on

Subscribers

People subscribed via source and target branches