~peter-sabaini/charm-helpers/bcache-helpers

« back to all changes in this revision

Viewing changes to charmhelpers/contrib/openstack/context.py

  • Committer: james.page at ubuntu
  • Date: 2017-04-26 09:48:47 UTC
  • mfrom: (737.1.3 fix-wsgi-workers)
  • Revision ID: james.page@ubuntu.com-20170426094847-cyehqgpxlv9i91rl
Refactor worker multiplier code.

Ensure WSGI processes are capped by default when running in containers.

Skew public processes in favour of admin processes.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1232
1232
DEFAULT_MULTIPLIER = 2
1233
1233
 
1234
1234
 
 
1235
def _calculate_workers():
 
1236
    '''
 
1237
    Determine the number of worker processes based on the CPU
 
1238
    count of the unit containing the application.
 
1239
 
 
1240
    Workers will be limited to MAX_DEFAULT_WORKERS in
 
1241
    container environments where no worker-multipler configuration
 
1242
    option been set.
 
1243
 
 
1244
    @returns int: number of worker processes to use
 
1245
    '''
 
1246
    multiplier = config('worker-multiplier') or DEFAULT_MULTIPLIER
 
1247
    count = int(_num_cpus() * multiplier)
 
1248
    if multiplier > 0 and count == 0:
 
1249
        count = 1
 
1250
 
 
1251
    if config('worker-multiplier') is None and is_container():
 
1252
        # NOTE(jamespage): Limit unconfigured worker-multiplier
 
1253
        #                  to MAX_DEFAULT_WORKERS to avoid insane
 
1254
        #                  worker configuration in LXD containers
 
1255
        #                  on large servers
 
1256
        # Reference: https://pad.lv/1665270
 
1257
        count = min(count, MAX_DEFAULT_WORKERS)
 
1258
 
 
1259
    return count
 
1260
 
 
1261
 
 
1262
def _num_cpus():
 
1263
    '''
 
1264
    Compatibility wrapper for calculating the number of CPU's
 
1265
    a unit has.
 
1266
 
 
1267
    @returns: int: number of CPU cores detected
 
1268
    '''
 
1269
    try:
 
1270
        return psutil.cpu_count()
 
1271
    except AttributeError:
 
1272
        return psutil.NUM_CPUS
 
1273
 
 
1274
 
1235
1275
class WorkerConfigContext(OSContextGenerator):
1236
1276
 
1237
 
    @property
1238
 
    def num_cpus(self):
1239
 
        # NOTE: use cpu_count if present (16.04 support)
1240
 
        if hasattr(psutil, 'cpu_count'):
1241
 
            return psutil.cpu_count()
1242
 
        else:
1243
 
            return psutil.NUM_CPUS
1244
 
 
1245
1277
    def __call__(self):
1246
 
        multiplier = config('worker-multiplier') or DEFAULT_MULTIPLIER
1247
 
        count = int(self.num_cpus * multiplier)
1248
 
        if multiplier > 0 and count == 0:
1249
 
            count = 1
1250
 
 
1251
 
        if config('worker-multiplier') is None and is_container():
1252
 
            # NOTE(jamespage): Limit unconfigured worker-multiplier
1253
 
            #                  to MAX_DEFAULT_WORKERS to avoid insane
1254
 
            #                  worker configuration in LXD containers
1255
 
            #                  on large servers
1256
 
            # Reference: https://pad.lv/1665270
1257
 
            count = min(count, MAX_DEFAULT_WORKERS)
1258
 
 
1259
 
        ctxt = {"workers": count}
 
1278
        ctxt = {"workers": _calculate_workers()}
1260
1279
        return ctxt
1261
1280
 
1262
1281
 
1264
1283
 
1265
1284
    def __init__(self, name=None, script=None, admin_script=None,
1266
1285
                 public_script=None, process_weight=1.00,
1267
 
                 admin_process_weight=0.75, public_process_weight=0.25):
 
1286
                 admin_process_weight=0.25, public_process_weight=0.75):
1268
1287
        self.service_name = name
1269
1288
        self.user = name
1270
1289
        self.group = name
1276
1295
        self.public_process_weight = public_process_weight
1277
1296
 
1278
1297
    def __call__(self):
1279
 
        multiplier = config('worker-multiplier') or 1
1280
 
        total_processes = self.num_cpus * multiplier
 
1298
        total_processes = _calculate_workers()
1281
1299
        ctxt = {
1282
1300
            "service_name": self.service_name,
1283
1301
            "user": self.user,