def process_request(request): cmd = request.cmd data = request.data print '[QoS Manager] Incoming request: ' + cmd + ' from ' + \ ('cmdline' if request.from_cmdline else 'socket') if data != '': print 'Data:\n--------------------' print data + '\n--------------------' if cmd == '-schedule': spec = itf_spec.QosSpec() err = spec.parse_string(data) if not err: spec_db = itf_database.get_spec(spec.SpecId) if spec_db == None: # spec does not exist print '[QoS Manager] Schedule for new spec {' + spec.SpecId + '}' scheduled_containers, data = qos_scheduler.schedule(spec, task='new') if len(scheduled_containers) > 0: itf_database.add_scheduled_spec(spec, scheduled_containers, init=True) print '[QoS Manager] Successfully scheduled {' + spec.SpecId + '}' else: print '[QoS Manager] Fail to schedule new spec {' \ + spec.SpecId + '} {' + data + '}' exit(-1) else: # reschedule for updated spec print '[QoS Manager] User specification already exists' print '[QoS Manager] Reschedule for spec {' + spec.SpecId + '}' scheduled_containers, data = qos_scheduler.schedule(spec, task='update') if len(scheduled_containers) > 0: itf_database.add_scheduled_spec(spec, scheduled_containers) print '[QoS Manager] Successfully rescheduled {' + spec.SpecId + '}' else: print '[QoS Manager] Fail to reschedule spec {' \ + spec.SpecId + '} {' + data + '}' exit(-1) else: print '[QoS Manager] Error: Invalid QoS spec.' elif cmd == '-rm_spec': spec_id = data print '[QoS Manager] Remove spec {' + spec_id + '} from database' itf_database.remove_spec(spec_id) elif cmd == '-add_container': # data is the path to the container status file print '[QoS Manager] Add a new container with status: ' + data # this request is from system admin when creating new container monitor_container.insert(data) # insert status to db status = itf_database.get_status(data) print '[QoS Manager] Successfully adds new container ' + data elif cmd == '-rm_container': container_id = data print '[QoS Manager] Remove container id: ' + container_id # Set container availability to 0 for triggering reschedule status = itf_database.get_status(container_id) if status is not None: old_avail = status.ContainerAvailability status.ContainerAvailability = -1 itf_database.update_container(status) # Reschedule every related spec - finally no spec will be on container related_specs = itf_database.get_spec_ids_on_container(container_id) for spec_id in related_specs: print '[QoS Manager] Reschedule for spec {' + spec_id + '}' spec = itf_database.get_spec(spec_id) scheduled_containers, data = qos_scheduler.schedule(spec, task='new') if len(scheduled_containers) > 0: itf_database.add_scheduled_spec(spec, scheduled_containers) print '[QoS Manager] Successfully rescheduled {' + spec_id + '}' else: print '[QoS Manager] Fail to reschedule spec {' \ + spec_id + '} {' + data + '}' status.ContainerAvailability = old_avail itf_database.update_container(status) exit(-1) # Delete container in database # NOTE: Must after finishing the file copy succ = itf_database.remove_container(container_id) if succ: print '[QoS Manager] Container', container_id, 'is removed from database' else: print '[QoS Manager] Container', container_id, 'is not in database' elif cmd == '-show_db': print '[QoS Manager] QoS database summary:' itf_database.summary(verbose=False) elif cmd == '-show_db_verbose': print '[QoS Manager] QoS database summary:' itf_database.summary(verbose=True) elif cmd == '-destroy_db': print '[QoS Manager] Clearing all contents in the QoS database?' prompt = raw_input('[y/n]:') if prompt == 'y' or prompt == 'Y': itf_database.init() elif cmd == '-start_server': if request.from_cmdline: qos_server_main() elif cmd == '-stop_server': if request.from_cmdline: # send a stop request to server request = QosRequest() request.cmd = '-stop_server' client_send(request) else: return 1 # use this value for stop the server main loop else: print '[QoS Manager] Unsupported QoS request command: ', cmd return -1 return 0
def monitor_container(container_id): print '[QoS Monitor] Monitoring container: ' + container_id spec_id_list = itf_database.get_spec_ids_on_container(container_id) for spec_id in spec_id_list: monitor_spec(spec_id)