Commit f731d5fe by Calen Pennington

Clarify thread-safety of bulk_write context manager

parent d2ffca2c
......@@ -646,7 +646,7 @@ class ModuleStoreWriteBase(ModuleStoreReadBase, ModuleStoreWrite):
@contextmanager
def bulk_write_operations(self, course_id):
"""
A context manager for notifying the store of bulk write events.
A context manager for notifying the store of bulk write events. This affects only the current thread.
In the case of Mongo, it temporarily disables refreshing the metadata inheritance tree
until the bulk operation is completed.
......
......@@ -414,7 +414,7 @@ class MongoModuleStore(ModuleStoreDraftAndPublished, ModuleStoreWriteBase):
# performance optimization to prevent updating the meta-data inheritance tree during
# bulk write operations
self.ignore_write_events_on_courses = set()
self.ignore_write_events_on_courses = threading.local()
self._course_run_cache = {}
def close_connections(self):
......@@ -439,13 +439,19 @@ class MongoModuleStore(ModuleStoreDraftAndPublished, ModuleStoreWriteBase):
"""
Prevent updating the meta-data inheritance cache for the given course
"""
self.ignore_write_events_on_courses.add(course_id)
if not hasattr(self.ignore_write_events_on_courses.courses):
self.ignore_write_events_on_courses.courses = set()
self.ignore_write_events_on_courses.courses.add(course_id)
def _end_bulk_write_operation(self, course_id):
"""
Restart updating the meta-data inheritance cache for the given course.
Refresh the meta-data inheritance cache now since it was temporarily disabled.
"""
if not hasattr(self.ignore_write_events_on_courses.courses):
return
if course_id in self.ignore_write_events_on_courses:
self.ignore_write_events_on_courses.remove(course_id)
self.refresh_cached_metadata_inheritance_tree(course_id)
......@@ -454,8 +460,11 @@ class MongoModuleStore(ModuleStoreDraftAndPublished, ModuleStoreWriteBase):
"""
Returns whether a bulk write operation is in progress for the given course.
"""
if not hasattr(self.ignore_write_events_on_courses.courses):
return False
course_id = course_id.for_branch(None)
return course_id in self.ignore_write_events_on_courses
return course_id in self.ignore_write_events_on_courses.courses
def fill_in_run(self, course_key):
"""
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment