From f731d5feda5637c1855a8a16cf01f67b3bf4f26b Mon Sep 17 00:00:00 2001 From: Calen Pennington Date: Thu, 31 Jul 2014 13:41:30 -0400 Subject: [PATCH] Clarify thread-safety of bulk_write context manager --- .../lib/xmodule/xmodule/modulestore/__init__.py | 2 +- .../lib/xmodule/xmodule/modulestore/mongo/base.py | 15 ++++++++++++--- 2 files changed, 13 insertions(+), 4 deletions(-) diff --git a/common/lib/xmodule/xmodule/modulestore/__init__.py b/common/lib/xmodule/xmodule/modulestore/__init__.py index 6cfd36b483..e5e590e9bc 100644 --- a/common/lib/xmodule/xmodule/modulestore/__init__.py +++ b/common/lib/xmodule/xmodule/modulestore/__init__.py @@ -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. diff --git a/common/lib/xmodule/xmodule/modulestore/mongo/base.py b/common/lib/xmodule/xmodule/modulestore/mongo/base.py index e2ccc55a1f..04ce12b8e4 100644 --- a/common/lib/xmodule/xmodule/modulestore/mongo/base.py +++ b/common/lib/xmodule/xmodule/modulestore/mongo/base.py @@ -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): """