controller.py 2 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62
from __future__ import print_function

import logging

logger = logging.getLogger('nrm')


class Action(object):

    """Information about a control action."""

    def __init__(self, target, command):
        self.target = target
        self.command = command


class Controller(object):

    """Implements a control loop for resource management."""

    def __init__(self, am, cm, rm):
        self.application_manager = am
        self.container_manager = cm
        self.resource_manager = rm

    def planify(self, target, machineinfo):
        """Plan the next action for the control loop."""
        total_power = machineinfo['energy']['power']['total']
        if total_power < target:
            for identity, application in \
                    self.application_manager.applications.iteritems():
                if 'i' in application.get_allowed_thread_requests():
                    return Action(application, 'i')
        elif total_power > target:
            for identity, application in \
                    self.application_manager.applications.iteritems():
                if 'd' in application.get_allowed_thread_requests():
                    return Action(application, 'd')
        return None

    def execute(self, action):
        """Build the action for the appropriate manager."""
        assert action
        target_threads = action.target.threads
        update = {'type': 'application',
                  'command': 'threads',
                  'uuid': action.target.uuid,
                  'event': 'threads',
                  }
        if action.command == 'i':
            payload = target_threads['cur'] + 1
        elif action.command == 'd':
            payload = target_threads['cur'] - 1
        else:
            assert False, "impossible command"
        update['payload'] = payload
        return update

    def update(self, action, request):
        """Update tracking across the board to reflect the last action."""
        assert action
        action.target.do_thread_transition(action.command)