~hloeung/ubuntu-repository-cache/cache-memory-tuning-fix

« back to all changes in this revision

Viewing changes to lib/ubuntu_repository_cache/squid.py

  • Committer: mergebot at canonical
  • Author(s): "Haw Loeung"
  • Date: 2020-05-14 22:29:12 UTC
  • mfrom: (269.1.2 lib-unitdata-kv-for-persistent-storage)
  • Revision ID: mergebot@juju-139df4-prod-is-toolbox-0.canonical.com-20200514222912-zg91msgbtet2rwq0
lib/ubuntu_repository_cache: Switch to using unitdata.kv() for persistent storage rather than modifying config context

Reviewed-on: https://code.launchpad.net/~hloeung/ubuntu-repository-cache/lib-unitdata-kv-for-persistent-storage/+merge/383841
Reviewed-by: Tom Haddon <tom.haddon@canonical.com>

Show diffs side-by-side

added added

removed removed

Lines of Context:
4
4
import subprocess
5
5
import re
6
6
 
7
 
# pylint can't find the modules  # pylint: disable=F0401
8
7
from charmhelpers import fetch
9
8
 
10
9
from charmhelpers.core import (
11
10
    hookenv,
12
11
    host,
13
12
    templating,
 
13
    unitdata,
14
14
)
15
15
 
16
16
from charmhelpers.core.host import (
18
18
)
19
19
 
20
20
from . import util
21
 
# pylint: enable=F0401
22
21
 
23
22
LOG = hookenv.log
24
23
SERVICE = 'squid-deb-proxy'
32
31
    configuration has been written.
33
32
    '''
34
33
 
35
 
    if not hookenv.config()['squid-disk-caches']:
 
34
    if not unitdata.kv().get('squid-disk-caches'):
36
35
        LOG('No on disk cache directories founds for squid.',
37
36
            hookenv.ERROR)
38
37
        return
46
45
    This must be run at installation time.  Runtime tuning is unsupported.
47
46
    '''
48
47
 
49
 
    config = hookenv.config()
50
48
    avail_disk = 0
51
 
    for cache in config['squid-disk-caches']:
 
49
    for cache in unitdata.kv().get('squid-disk-caches'):
52
50
        avail_disk += cache[1]
53
51
    LOG('{}MB of disk cache available for squid.'.format(avail_disk),
54
52
        hookenv.DEBUG)
64
62
        raise RuntimeError('Too little system memory for squid deployment')
65
63
 
66
64
    # Start by giving squid a maximum of 256MB for in-memory cache
67
 
    config['squid-cache-mem'] = min(memory, 256)
68
 
    memory -= config['squid-cache-mem']
 
65
    squid_cache_mem = min(memory, 256)
 
66
    memory -= squid_cache_mem
69
67
 
70
68
    # Estimate 20MB of memory overhead per GB of on-disk cache
71
69
    max_disk = int(memory / 20.0 * 1024)
72
 
    config['squid-cache-disk'] = min(max_disk, avail_disk)
73
 
    disk_overhead = int(round(config['squid-cache-disk'] / 1024.0 * 20))
 
70
    squid_cache_disk = min(max_disk, avail_disk)
 
71
    disk_overhead = int(round(squid_cache_disk / 1024.0 * 20))
74
72
    memory -= disk_overhead
75
73
 
76
74
    # Remaining memory (up to 1/2 total RAM) is given to in-memory cache
77
 
    config['squid-cache-mem'] += memory
78
 
    config['squid-cache-mem'] = int(config['squid-cache-mem'])
 
75
    squid_cache_mem += memory
 
76
    squid_cache_mem = int(squid_cache_mem)
79
77
 
 
78
    unitdata.kv().set('squid-cache-mem', squid_cache_mem)
 
79
    unitdata.kv().set('squid-cache-disk', squid_cache_disk)
80
80
    LOG('Squid sizing complete. In-memory cache: {}MB '
81
 
        'On-disk cache: {}MB'.format(config['squid-cache-mem'],
82
 
                                     config['squid-cache-disk']),
 
81
        'On-disk cache: {}MB'.format(unitdata.kv().get('squid-cache-mem'),
 
82
                                     unitdata.kv().get('squid-cache-disk')),
83
83
        hookenv.INFO)
84
84
 
85
 
    config.save()
86
 
 
87
85
 
88
86
def install():
89
87
    '''Perform the install/config and disable avahi'''
159
157
 
160
158
    Returns a string containing the cache_dir line(s)
161
159
    '''
162
 
    config = hookenv.config()
163
160
    cache_str = ''
164
 
    max_size = config['squid-cache-disk']
165
 
    for path, free in config['squid-disk-caches']:
 
161
    config = hookenv.config()
 
162
    max_size = unitdata.kv().get('squid-cache-disk')
 
163
    for path, free in unitdata.kv().get('squid-disk-caches'):
166
164
        if config.get('cache-storage-size', 0):
167
165
            size = int(config['cache-storage-size'])
168
166
        else:
183
181
    config = hookenv.config()
184
182
    context = {}
185
183
    context['Cache_Config'] = cache_dirs()
186
 
    context['Cache_Mem'] = config['squid-cache-mem']
 
184
    context['Cache_Mem'] = unitdata.kv().get('squid-cache-mem')
187
185
    context['Cache_Peers'] = peer_config()
188
186
    context['Cache_Hostname'] = hookenv.unit_private_ip()
189
187
    if config['squid_snmp']: