def find_available_worker(self, container): candidates = [] workers = LookUpTable.Workers.verbose() SysOut.debug_string("Found workers: " + str(workers)) if not workers: return None # loop through workers and make tuples of worker IP, load and if requested container is available locally for worker in workers: curr_worker = workers[worker] if container in curr_worker[Definition.REST.get_str_local_imgs()]: candidates.append( ((curr_worker[Definition.get_str_node_addr()], curr_worker[Definition.get_str_node_port()]), curr_worker[Definition.get_str_load5()], True)) else: candidates.append( ((curr_worker[Definition.get_str_node_addr()], curr_worker[Definition.get_str_node_port()]), curr_worker[Definition.get_str_load5()], False)) candidates.sort( key=lambda x: (-x[2], x[1]) ) # sort candidate workers first on availability of image, then on load (avg load last 5 mins) for candidate in list(candidates): if not float(candidate[1]) < 0.5: candidates.remove( candidate ) # remove candidates with higher than 50% cpu load return candidates
def send_data(self, container_name, container_os, data, priority=None): # The data must be byte array if not isinstance(data, bytearray): LocalError.err_invalid_data_container_type() if len(data) == 0: SysOut.err_string("No content in byte array.") return None digest = hashlib.md5(data).hexdigest() end_point = None counter = self.__max_try while not end_point: end_point = self.__get_stream_end_point(container_name, container_os, priority, digest) counter -= 1 if counter == 0: SysOut.err_string( "Cannot contact server. Exceed maximum retry {0}!".format( self.__max_try)) return False # Send data to worker for processing directly counter = self.__max_try if end_point[Definition.get_str_node_role()] == CRole.WORKER: while not self.__push_stream_end_point( end_point[Definition.get_str_node_addr()], end_point[Definition.get_str_node_port()], data): time.sleep(self.__std_idle_time) counter -= 1 if counter == 0: SysOut.err_string( "Cannot contact server. Exceed maximum retry {0}!". format(self.__max_try)) return False # Send data to master for queuing (?) elif end_point[ Definition.get_str_node_role()] == CRole.MESSAGING_SYSTEM: while not self.__push_stream_end_point_MS( end_point[Definition.get_str_node_addr()], end_point[Definition.get_str_node_port()], data, container_name): time.sleep(self.__std_idle_time) counter -= 1 if counter == 0: SysOut.err_string( "Cannot contact server. Exceed maximum retry {0}!". format(self.__max_try)) return False else: return False if end_point[Definition.get_str_node_role()] == CRole.WORKER: SysOut.out_string( "Push data to worker ({0}:{1}>{2}) successful.".format( end_point[Definition.get_str_node_addr()], end_point[Definition.get_str_node_port()], container_name)) elif end_point[ Definition.get_str_node_role()] == CRole.MESSAGING_SYSTEM: SysOut.out_string( "Push data to messaging system ({0}:{1}>{2}) successful.". format(end_point[Definition.get_str_node_addr()], end_point[Definition.get_str_node_port()], container_name)) else: SysOut.out_string( "Push data to unknown ({0}:{1}>{2}) successful.".format( end_point[Definition.get_str_node_addr()], end_point[Definition.get_str_node_port()], container_name))
def add_worker(dict_input): dict_input[Definition.get_str_last_update( )] = Services.get_current_timestamp() LookUpTable.Workers.__workers[dict_input[ Definition.get_str_node_addr()]] = dict_input