예제 #1
0
def test_get_all_tasks_from_state():
    mock_task_1 = mock.Mock()
    mock_task_2 = mock.Mock()
    mock_task_3 = mock.Mock()
    mock_task_4 = mock.Mock()
    mock_state = {'frameworks': [{'tasks': [mock_task_1, mock_task_2]},
                                 {'tasks': [mock_task_3]}],
                  'orphan_tasks': [mock_task_4]}
    ret = mesos_tools.get_all_tasks_from_state(mock_state)
    expected = [mock_task_1, mock_task_2, mock_task_3]
    assert len(ret) == len(expected) and ret == expected

    ret = mesos_tools.get_all_tasks_from_state(mock_state, include_orphans=True)
    expected = [mock_task_1, mock_task_2, mock_task_3, mock_task_4]
    assert len(ret) == len(expected) and ret == expected
예제 #2
0
def get_resource_utilization_by_grouping(grouping_func,
                                         mesos_state,
                                         filters=[]):
    """ Given a function used to group slaves and mesos state, calculate
    resource utilization for each value of a given attribute.

    :grouping_func: a function that given a slave, will return the value of an
    attribtue to group by.
    :param mesos_state: the mesos state
    :param filters: filters to apply to the slaves in the calculation, with
    filtering preformed by filter_slaves
    :returns: a dict of {attribute_value: resource_usage}, where resource usage
    is the dict returned by ``calculate_resource_utilization_for_slaves`` for
    slaves grouped by attribute value.
    """
    slaves = mesos_state.get('slaves', [])
    slaves = filter_slaves(slaves, filters)
    if not has_registered_slaves(mesos_state):
        raise ValueError("There are no slaves registered in the mesos state.")

    tasks = get_all_tasks_from_state(mesos_state, include_orphans=True)
    non_terminal_tasks = [task for task in tasks if not is_task_terminal(task)]
    slave_groupings = group_slaves_by_key_func(grouping_func, slaves)

    return {
        attribute_value: calculate_resource_utilization_for_slaves(
            slaves=slaves,
            tasks=filter_tasks_for_slaves(slaves, non_terminal_tasks))
        for attribute_value, slaves in slave_groupings.items()
    }
예제 #3
0
def get_resource_utilization_by_grouping(
    grouping_func: _GenericNodeGroupingFunctionT,
    mesos_state: MesosState,
    filters: Sequence[_GenericNodeFilterFunctionT] = [],
    sort_func: _GenericNodeSortFunctionT = None,
) -> Mapping[_KeyFuncRetT, ResourceUtilizationDict]:
    """Given a function used to group slaves and mesos state, calculate
    resource utilization for each value of a given attribute.

    :grouping_func: a function that given a slave, will return the value of an
    attribute to group by.
    :param mesos_state: the mesos state
    :param filters: filters to apply to the slaves in the calculation, with
    filtering preformed by filter_slaves
    :param sort_func: a function that given a list of slaves, will return the
    sorted list of slaves.
    :returns: a dict of {attribute_value: resource_usage}, where resource usage
    is the dict returned by ``calculate_resource_utilization_for_slaves`` for
    slaves grouped by attribute value.
    """
    slaves: Sequence[_SlaveT] = mesos_state.get("slaves", [])
    slaves = filter_slaves(slaves, filters)
    if not has_registered_slaves(mesos_state):
        raise ValueError("There are no slaves registered in the mesos state.")

    tasks = get_all_tasks_from_state(mesos_state, include_orphans=True)
    non_terminal_tasks = [task for task in tasks if not is_task_terminal(task)]
    slave_groupings = group_slaves_by_key_func(grouping_func, slaves, sort_func)

    return {
        attribute_value: calculate_resource_utilization_for_slaves(
            slaves=slaves, tasks=filter_tasks_for_slaves(slaves, non_terminal_tasks)
        )
        for attribute_value, slaves in slave_groupings.items()
    }
예제 #4
0
def get_resource_utilization_by_grouping(grouping_func, mesos_state):
    """ Given a function used to group slaves and mesos state, calculate
    resource utilization for each value of a given attribute.

    :grouping_func: a function that given a slave, will return the value of an
    attribtue to group by.
    :param mesos_state: the mesos state
    :returns: a dict of {attribute_value: resource_usage}, where resource usage
    is the dict returned by ``calculate_resource_utilization_for_slaves`` for
    slaves grouped by attribute value.
    """
    slaves = mesos_state.get('slaves', [])
    if not has_registered_slaves(mesos_state):
        raise ValueError("There are no slaves registered in the mesos state.")

    tasks = get_all_tasks_from_state(mesos_state)

    slave_groupings = group_slaves_by_key_func(grouping_func, slaves)

    return {
        attribute_value: calculate_resource_utilization_for_slaves(slaves, filter_tasks_for_slaves(slaves, tasks))
        for attribute_value, slaves in slave_groupings.items()
    }
예제 #5
0
def get_resource_utilization_by_grouping(grouping_func, mesos_state):
    """ Given a function used to group slaves and mesos state, calculate
    resource utilization for each value of a given attribute.

    :grouping_func: a function that given a slave, will return the value of an
    attribtue to group by.
    :param mesos_state: the mesos state
    :returns: a dict of {attribute_value: resource_usage}, where resource usage
    is the dict returned by ``calculate_resource_utilization_for_slaves`` for
    slaves grouped by attribute value.
    """
    slaves = mesos_state.get('slaves', [])
    if not has_registered_slaves(mesos_state):
        raise ValueError("There are no slaves registered in the mesos state.")

    tasks = get_all_tasks_from_state(mesos_state)

    slave_groupings = group_slaves_by_key_func(grouping_func, slaves)

    return {
        attribute_value: calculate_resource_utilization_for_slaves(slaves, filter_tasks_for_slaves(slaves, tasks))
        for attribute_value, slaves in slave_groupings.items()
    }