instructor_task: Distribute extra items among subtasks of last query.
When creating an instructor task total_num_items may change between the time it and the number of subtasks is calculated and the time the subtasks are actually queued (all of this cannot happen in one transaction). In such a case the extra items are distributed among the subtasks of the last query. LMS-2090
This commit is contained in:
@@ -51,7 +51,7 @@ def _get_number_of_subtasks(total_num_items, items_per_query, items_per_task):
|
||||
return total_num_tasks
|
||||
|
||||
|
||||
def _generate_items_for_subtask(item_queryset, item_fields, total_num_items, items_per_query, items_per_task):
|
||||
def _generate_items_for_subtask(item_queryset, item_fields, total_num_items, total_num_subtasks, items_per_query, items_per_task):
|
||||
"""
|
||||
Generates a chunk of "items" that should be passed into a subtask.
|
||||
|
||||
@@ -68,15 +68,29 @@ def _generate_items_for_subtask(item_queryset, item_fields, total_num_items, ite
|
||||
Warning: if the algorithm here changes, the _get_number_of_subtasks() method should similarly be changed.
|
||||
"""
|
||||
num_queries = int(math.ceil(float(total_num_items) / float(items_per_query)))
|
||||
last_pk = item_queryset[0].pk - 1
|
||||
last_pk = item_queryset.order_by('pk')[0].pk - 1
|
||||
num_items_queued = 0
|
||||
available_num_subtasks = total_num_subtasks
|
||||
all_item_fields = list(item_fields)
|
||||
all_item_fields.append('pk')
|
||||
for _ in range(num_queries):
|
||||
item_sublist = list(item_queryset.order_by('pk').filter(pk__gt=last_pk).values(*all_item_fields)[:items_per_query])
|
||||
|
||||
for query_number in range(num_queries):
|
||||
# In case total_num_items has increased since it was initially calculated
|
||||
# include all remaining items in last query.
|
||||
item_sublist = item_queryset.order_by('pk').filter(pk__gt=last_pk).values(*all_item_fields)
|
||||
if query_number < num_queries - 1:
|
||||
item_sublist = list(item_sublist[:items_per_query])
|
||||
else:
|
||||
item_sublist = list(item_sublist)
|
||||
|
||||
last_pk = item_sublist[-1]['pk']
|
||||
num_items_this_query = len(item_sublist)
|
||||
num_tasks_this_query = int(math.ceil(float(num_items_this_query) / float(items_per_task)))
|
||||
|
||||
# In case total_num_items has increased since it was initially calculated just distribute the extra
|
||||
# items among the available subtasks.
|
||||
num_tasks_this_query = min(available_num_subtasks, int(math.ceil(float(num_items_this_query) / float(items_per_task))))
|
||||
available_num_subtasks -= num_tasks_this_query
|
||||
|
||||
chunk = int(math.ceil(float(num_items_this_query) / float(num_tasks_this_query)))
|
||||
for i in range(num_tasks_this_query):
|
||||
items_for_task = item_sublist[i * chunk:i * chunk + chunk]
|
||||
@@ -84,11 +98,12 @@ def _generate_items_for_subtask(item_queryset, item_fields, total_num_items, ite
|
||||
|
||||
num_items_queued += num_items_this_query
|
||||
|
||||
# Sanity check: we expect the chunking to be properly summing to the original count:
|
||||
# Because queueing does not happen in one transaction the number of items in the queryset may change
|
||||
# from the initial count. For example if the queryset is of the CourseEnrollment model students may
|
||||
# enroll or unenroll while queueing is in progress. The purpose of the original count is to estimate the
|
||||
# number of subtasks needed to perform the requested task.
|
||||
if num_items_queued != total_num_items:
|
||||
error_msg = "Number of items generated by chunking {} not equal to original total {}".format(num_items_queued, total_num_items)
|
||||
TASK_LOG.error(error_msg)
|
||||
raise ValueError(error_msg)
|
||||
TASK_LOG.info("Number of items generated by chunking %s not equal to original total %s", num_items_queued, total_num_items)
|
||||
|
||||
|
||||
class SubtaskStatus(object):
|
||||
@@ -278,6 +293,7 @@ def queue_subtasks_for_query(entry, action_name, create_subtask_fcn, item_querys
|
||||
item_queryset,
|
||||
item_fields,
|
||||
total_num_items,
|
||||
total_num_subtasks,
|
||||
items_per_query,
|
||||
items_per_task
|
||||
)
|
||||
@@ -293,13 +309,7 @@ def queue_subtasks_for_query(entry, action_name, create_subtask_fcn, item_querys
|
||||
new_subtask = create_subtask_fcn(item_list, subtask_status)
|
||||
new_subtask.apply_async()
|
||||
|
||||
# Sanity check: we expect the subtask to be properly summing to the original count:
|
||||
if num_subtasks != len(subtask_id_list):
|
||||
task_id = entry.task_id
|
||||
error_fmt = "Task {}: number of tasks generated {} not equal to original total {}"
|
||||
error_msg = error_fmt.format(task_id, num_subtasks, len(subtask_id_list))
|
||||
TASK_LOG.error(error_msg)
|
||||
raise ValueError(error_msg)
|
||||
# Subtasks have been queued so no exceptions should be raised after this point.
|
||||
|
||||
# Return the task progress as stored in the InstructorTask object.
|
||||
return progress
|
||||
|
||||
Reference in New Issue
Block a user