Skip to content

Commit

Permalink
feat: update both indexes if running reindex
Browse files Browse the repository at this point in the history
  • Loading branch information
rpenido committed Mar 19, 2024
1 parent 32f5bf2 commit d63195d
Showing 1 changed file with 17 additions and 17 deletions.
34 changes: 17 additions & 17 deletions openedx/core/djangoapps/content/search/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,19 +44,20 @@


@contextmanager
def _index_rebuild_lock(index_name: str) -> Generator[None, None, None]:
def _index_rebuild_lock(index_name: str) -> Generator[str, None, None]:
"""
Lock to prevent that the index is updated while it is being rebuilt
"""
timeout_at = time.monotonic() + LOCK_EXPIRE
lock_id = f"lock-meilisearch-index-{index_name}"
new_index_name = index_name + "_new"

while True:
status = cache.add(lock_id, True, LOCK_EXPIRE)
status = cache.add(lock_id, new_index_name, LOCK_EXPIRE)
if status:
# Lock acquired
try:
yield
yield new_index_name
finally:
break

Expand All @@ -69,18 +70,10 @@ def _index_rebuild_lock(index_name: str) -> Generator[None, None, None]:
cache.delete(lock_id)


def _wait_index_rebuild_lock(index_name: str) -> None:
"""
Wait for the index rebuild lock to be released
"""
timeout_at = time.monotonic() + LOCK_EXPIRE
def _get_running_rebuild_index_name(index_name: str) -> str | None:
lock_id = f"lock-meilisearch-index-{index_name}"

while cache.get(lock_id):
if time.monotonic() > timeout_at:
raise TimeoutError("Timeout waiting lock")

time.sleep(1)
return cache.get(lock_id)


def _get_meilisearch_client():
Expand Down Expand Up @@ -163,8 +156,7 @@ def nop(_):

client = _get_meilisearch_client()
status_cb("Checking index...")
temp_index_name = target_index + "_new"
with _index_rebuild_lock(target_index):
with _index_rebuild_lock(target_index) as temp_index_name:
if _index_exists(temp_index_name):
status_cb("Temporary index already exists. Deleting it...")
_wait_for_meili_task(client.delete_index(temp_index_name))
Expand Down Expand Up @@ -313,7 +305,7 @@ def upsert_xblock_index_doc(
"""
# If there is a rebuild in progress, wait for it to finish
# If a rebuild starts right after this check, it will already have the updated data, so this is not a problem.
_wait_index_rebuild_lock(INDEX_NAME)
current_rebuild_index_name = _get_running_rebuild_index_name(INDEX_NAME)

course = modulestore().get_item(usage_key)
client = _get_meilisearch_client()
Expand All @@ -329,7 +321,10 @@ def add_with_children(block):

add_with_children(course)

# FixMe: Create function to wait for multiple tasks to run this in parallel
_wait_for_meili_task(client.index(INDEX_NAME).update_documents(docs))
if current_rebuild_index_name:
_wait_for_meili_task(client.index(current_rebuild_index_name).update_documents(docs))


def delete_xblock_index_doc(usage_key: UsageKey) -> None:
Expand All @@ -338,10 +333,15 @@ def delete_xblock_index_doc(usage_key: UsageKey) -> None:
"""
# If there is a rebuild in progress, wait for it to finish
# If a rebuild starts right after this check, it will already have the updated data, so this is not a problem.
_wait_index_rebuild_lock(INDEX_NAME)
current_rebuild_index_name = _get_running_rebuild_index_name(INDEX_NAME)

client = _get_meilisearch_client()

# FixMe: Create function to wait for multiple tasks to run this in parallel
if current_rebuild_index_name:
# Updates the new index
_wait_for_meili_task(client.index(current_rebuild_index_name).delete_document(meili_id_from_opaque_key(usage_key)))

_wait_for_meili_task(client.index(INDEX_NAME).delete_document(meili_id_from_opaque_key(usage_key)))


Expand Down

0 comments on commit d63195d

Please sign in to comment.