-
Notifications
You must be signed in to change notification settings - Fork 40
feat: Per-user ingestion coordination to prevent cross-source race conditions #171
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
anirudhaacharyap
wants to merge
4
commits into
XortexAI:main
Choose a base branch
from
anirudhaacharyap:feat/batch-ingest
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 3 commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
bc528b4
feat: add per-user ingestion coordination to prevent race conditions
anirudhaacharyap cae0a31
Optimize locking order in ingest_memory and add backpressure & struct…
anirudhaacharyap fc4ca93
Resolve merge conflict and optimize locking order in ingest_memory & …
anirudhaacharyap 02d79e9
feat: staged parallel hybrid ingestion pipeline and starvation preven…
anirudhaacharyap File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,36 @@ | ||
| import asyncio | ||
| from fastapi.testclient import TestClient | ||
| from unittest.mock import AsyncMock, patch | ||
|
|
||
| from src.api.app import create_app | ||
|
|
||
| app = create_app() | ||
| client = TestClient(app) | ||
|
|
||
| with patch("src.api.routes.memory.require_api_key", return_value={"username": "test_user"}): | ||
| from src.api.dependencies import require_api_key, enforce_rate_limit, require_ready | ||
| app.dependency_overrides[require_api_key] = lambda: {"username": "test_user"} | ||
| app.dependency_overrides[enforce_rate_limit] = lambda: True | ||
| app.dependency_overrides[require_ready] = lambda: True | ||
|
|
||
| payload = { | ||
| "items": [ | ||
| { | ||
| "user_query": "Hello world", | ||
| "agent_response": "Hi there", | ||
| "user_id": "test_user_1", | ||
| } | ||
| ] | ||
| } | ||
|
|
||
| try: | ||
| response = client.post( | ||
| "/v1/memory/batch-ingest", | ||
| json=payload, | ||
| headers={"Authorization": "Bearer test-key"} | ||
| ) | ||
| print("Status code:", response.status_code) | ||
| import json | ||
| print(json.dumps(response.json(), indent=2)) | ||
| except Exception as e: | ||
| print("Exception:", e) | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,94 @@ | ||
| """ | ||
| Per-user ingestion coordinator — serialises ingestion for each user. | ||
|
|
||
| Guarantees that only one ingestion pipeline runs at a time for any given | ||
| ``user_id``, while allowing different users to proceed in parallel. | ||
| Requests for the same user are processed in strict FIFO order. | ||
|
|
||
| This is the **in-memory** implementation (Option 1). A future distributed | ||
| lock (Redis, etc.) can be swapped in by implementing the same ``acquire()`` | ||
| context-manager interface. | ||
|
|
||
| Usage:: | ||
|
|
||
| from src.api.ingestion_coordinator import UserIngestionCoordinator | ||
|
|
||
| coordinator = UserIngestionCoordinator() | ||
|
|
||
| async with coordinator.acquire(user_id): | ||
| result = await pipeline.run(...) | ||
| """ | ||
|
|
||
| from __future__ import annotations | ||
|
|
||
| import asyncio | ||
| import logging | ||
| from contextlib import asynccontextmanager | ||
| from typing import AsyncIterator, Dict | ||
|
|
||
| logger = logging.getLogger("xmem.api.ingestion_coordinator") | ||
|
|
||
|
|
||
| class UserIngestionCoordinator: | ||
| """Per-user FIFO ingestion lock. | ||
|
|
||
| Internally maintains a ``dict[str, asyncio.Lock]`` keyed by ``user_id``. | ||
| Locks are created lazily on first access and removed once no tasks are | ||
| waiting or holding them, preventing unbounded memory growth. | ||
|
|
||
| Thread-safety note | ||
| ------------------ | ||
| All mutations to the internal registry are protected by a single | ||
| ``asyncio.Lock`` (the *registry lock*). Since this code runs on the | ||
| asyncio event loop, ``asyncio.Lock`` is sufficient — no OS-level | ||
| threading primitives are needed. | ||
| """ | ||
|
|
||
| def __init__(self) -> None: | ||
| # Maps user_id -> (asyncio.Lock, active_count) | ||
| # active_count tracks how many tasks are either holding or waiting | ||
| # for the lock so we know when it's safe to clean up. | ||
| self._locks: Dict[str, asyncio.Lock] = {} | ||
| self._waiters: Dict[str, int] = {} | ||
| self._registry_lock = asyncio.Lock() | ||
|
|
||
| @asynccontextmanager | ||
| async def acquire(self, user_id: str) -> AsyncIterator[None]: | ||
| """Acquire the per-user ingestion lock. | ||
|
|
||
| Usage:: | ||
|
|
||
| async with coordinator.acquire("user_123"): | ||
| # Only one coroutine per user_id reaches here at a time. | ||
| await do_work() | ||
|
|
||
| The lock is automatically released (and cleaned up if idle) when | ||
| the ``async with`` block exits, even if an exception is raised. | ||
| """ | ||
| # ── Get-or-create the user lock ────────────────────────────── | ||
| async with self._registry_lock: | ||
| if user_id not in self._locks: | ||
| self._locks[user_id] = asyncio.Lock() | ||
| self._waiters[user_id] = 0 | ||
| self._waiters[user_id] += 1 | ||
| user_lock = self._locks[user_id] | ||
|
|
||
| logger.debug("User %s: waiting for ingestion lock (waiters=%d)", user_id, self._waiters.get(user_id, 0)) | ||
|
|
||
| try: | ||
| async with user_lock: | ||
| logger.debug("User %s: ingestion lock acquired", user_id) | ||
| yield | ||
| finally: | ||
| # ── Cleanup: remove the lock if nobody else is waiting ──── | ||
| async with self._registry_lock: | ||
| self._waiters[user_id] -= 1 | ||
| if self._waiters[user_id] <= 0: | ||
| self._locks.pop(user_id, None) | ||
| self._waiters.pop(user_id, None) | ||
| logger.debug("User %s: ingestion lock cleaned up", user_id) | ||
|
|
||
| @property | ||
| def active_users(self) -> int: | ||
| """Return the number of users with active or pending ingestion locks.""" | ||
| return len(self._locks) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Binary file not shown.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,135 @@ | ||
| import pytest | ||
| from fastapi.testclient import TestClient | ||
| from unittest.mock import AsyncMock, patch | ||
| from typing import Dict, Any | ||
|
|
||
| from src.api.app import create_app | ||
| from src.api.schemas import BatchIngestRequest, IngestRequest | ||
| from src.pipelines.ingest import IngestPipeline | ||
|
|
||
| @pytest.fixture | ||
| def client(): | ||
| app = create_app() | ||
| return TestClient(app) | ||
|
|
||
| @pytest.fixture | ||
| def mock_ingest_pipeline(): | ||
| with patch("src.api.routes.memory.get_ingest_pipeline") as mock_get_pipeline: | ||
| from types import SimpleNamespace | ||
| mock_pipeline = AsyncMock(spec=IngestPipeline) | ||
| mock_pipeline.model = SimpleNamespace(model_name="test-model") | ||
|
|
||
| # Default mock behavior | ||
| async def mock_run(*args, **kwargs): | ||
| return { | ||
| "classification_result": SimpleNamespace(classifications=["test"]), | ||
| "profile_judge": None, | ||
| "profile_weaver": None, | ||
| "temporal_judge": None, | ||
| "temporal_weaver": None, | ||
| "summary_judge": None, | ||
| "summary_weaver": None, | ||
| "image_judge": None, | ||
| "image_weaver": None, | ||
| } | ||
|
|
||
| mock_pipeline.run.side_effect = mock_run | ||
| mock_get_pipeline.return_value = mock_pipeline | ||
| yield mock_pipeline | ||
|
|
||
| def test_batch_ingest_success(client, mock_ingest_pipeline): | ||
| """Test that multiple items can be successfully ingested in a batch.""" | ||
| payload = { | ||
| "items": [ | ||
| { | ||
| "user_query": "Hello world", | ||
| "agent_response": "Hi there", | ||
| "user_id": "test_user_1", | ||
| }, | ||
| { | ||
| "user_query": "Second message", | ||
| "agent_response": "Understood", | ||
| "user_id": "test_user_1", | ||
| } | ||
| ] | ||
| } | ||
|
|
||
| # You must provide API key or mock dependency for require_api_key | ||
| # For test purposes, we assume we override the dependency or add a test key | ||
| # Let's mock require_api_key in dependencies | ||
| with patch("src.api.routes.memory.require_api_key", return_value={"username": "test_user"}): | ||
| app = client.app | ||
| from src.api.dependencies import require_api_key, enforce_rate_limit, require_ready | ||
| app.dependency_overrides[require_api_key] = lambda: {"username": "test_user"} | ||
| app.dependency_overrides[enforce_rate_limit] = lambda: True | ||
| app.dependency_overrides[require_ready] = lambda: True | ||
|
|
||
| response = client.post( | ||
| "/v1/memory/batch-ingest", | ||
| json=payload, | ||
| headers={"Authorization": "Bearer test-key"} | ||
| ) | ||
|
|
||
| assert response.status_code == 200, response.json() | ||
| data = response.json() | ||
| assert data["status"] == "ok", data | ||
| assert len(data["data"]["results"]) == 2, data | ||
| for item in data["data"]["results"]: | ||
| assert item["model"] == "test-model" | ||
|
|
||
|
|
||
| def test_coordinator_serializes_concurrent_batches(client, mock_ingest_pipeline): | ||
| """Two concurrent batch-ingest requests for the same user must not overlap. | ||
|
|
||
| We verify this by checking that all 4 pipeline.run calls were made | ||
| (2 items × 2 batches) and both requests succeed. | ||
| """ | ||
| import threading | ||
|
|
||
| payload = { | ||
| "items": [ | ||
| { | ||
| "user_query": "Batch message 1", | ||
| "agent_response": "Ack 1", | ||
| "user_id": "same_user", | ||
| }, | ||
| { | ||
| "user_query": "Batch message 2", | ||
| "agent_response": "Ack 2", | ||
| "user_id": "same_user", | ||
| }, | ||
| ] | ||
| } | ||
|
|
||
| with patch("src.api.routes.memory.require_api_key", return_value={"username": "same_user"}): | ||
| app = client.app | ||
| from src.api.dependencies import require_api_key, enforce_rate_limit, require_ready | ||
| app.dependency_overrides[require_api_key] = lambda: {"username": "same_user"} | ||
| app.dependency_overrides[enforce_rate_limit] = lambda: True | ||
| app.dependency_overrides[require_ready] = lambda: True | ||
|
|
||
| # Send two batch requests concurrently via threads | ||
| results = [None, None] | ||
|
|
||
| def _send_batch(idx): | ||
| results[idx] = client.post( | ||
| "/v1/memory/batch-ingest", | ||
| json=payload, | ||
| headers={"Authorization": "Bearer test-key"}, | ||
| ) | ||
|
|
||
| t1 = threading.Thread(target=_send_batch, args=(0,)) | ||
| t2 = threading.Thread(target=_send_batch, args=(1,)) | ||
| t1.start() | ||
| t2.start() | ||
| t1.join() | ||
| t2.join() | ||
|
|
||
| # Both requests should succeed | ||
| for r in results: | ||
| assert r is not None | ||
| assert r.status_code == 200, r.json() | ||
|
|
||
| # All 4 pipeline.run calls (2 items × 2 batches) should have been made | ||
| assert mock_ingest_pipeline.run.call_count == 4 | ||
|
|
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
debug_test.py,test_output.txt(a binary artifact), andxlsx.py(an Excel workbook generator with no relation to this feature) were accidentally included in the PR. All three should be removed before merging —debug_test.pyis a throwaway debug script,test_output.txtis a build artifact, andxlsx.pyappears to be a personal utility script that does not belong in this codebase.