'Changes the state of task instances in the list with one of the given states
to QUEUED atomically, and returns the TIs changed.
:param task_instances: TaskInstances to change the state of
:type task_instances: List[TaskInstance]
:param acceptable_states: Filters the TaskInstances updated to be in these states
:type acceptable_states: Iterable[State]
:return: List[TaskInstance]'

@provide_session
def _change_state_for_executable_task_instances(self, task_instances, acceptable_states, session=None):

    if (len(task_instances) == 0):
        session.commit()
        return []
    TI = models.TaskInstance
    filter_for_ti_state_change = [and_((TI.dag_id == ti.dag_id), (TI.task_id == ti.task_id), (TI.execution_date == ti.execution_date)) for ti in task_instances]
    ti_query = session.query(TI).filter(or_(*filter_for_ti_state_change))
    if (None in acceptable_states):
        ti_query = ti_query.filter(or_((TI.state == None), TI.state.in_(acceptable_states)))
    else:
        ti_query = ti_query.filter(TI.state.in_(acceptable_states))
    tis_to_set_to_queued = ti_query.with_for_update().all()
    if (len(tis_to_set_to_queued) == 0):
        self.logger.info(u'No    tasks    were    able    to    have    their    state    changed    to    queued.')
        session.commit()
        return []
    for task_instance in tis_to_set_to_queued:
        task_instance.state = State.QUEUED
        task_instance.queued_dttm = (datetime.now() if (not task_instance.queued_dttm) else task_instance.queued_dttm)
        session.merge(task_instance)
    filter_for_ti_enqueue = [and_((TI.dag_id == ti.dag_id), (TI.task_id == ti.task_id), (TI.execution_date == ti.execution_date)) for ti in tis_to_set_to_queued]
    session.commit()
    tis_to_be_queued = session.query(TI).filter(or_(*filter_for_ti_enqueue)).all()
    task_instance_str = u'\n DCTB '.join([u'{}'.format(x) for x in tis_to_be_queued])
    self.logger.info(u'Setting    the    follow    tasks    to    queued    state:\n DCTB {}'.format(task_instance_str))
    return tis_to_be_queued
