Skip to content

Commit

Permalink
fix: Invalidate cache on project reset (#3113)
Browse files Browse the repository at this point in the history
* Invalidate cache on project reset

* Do not reset cache if it doesn't exist
  • Loading branch information
anticorrelator committed May 7, 2024
1 parent 79da9e7 commit 2944ae5
Show file tree
Hide file tree
Showing 6 changed files with 14 additions and 2 deletions.
4 changes: 4 additions & 0 deletions src/phoenix/db/insertion/span.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@ class SpanInsertionResult(NamedTuple):
project_rowid: int


class ClearProjectSpansResult(NamedTuple):
project_rowid: int


async def insert_span(
session: AsyncSession,
span: Span,
Expand Down
2 changes: 2 additions & 0 deletions src/phoenix/server/api/context.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

from phoenix.core.model_schema import Model
from phoenix.server.api.dataloaders import (
CacheForDataLoaders,
DocumentEvaluationsDataLoader,
DocumentEvaluationSummaryDataLoader,
DocumentRetrievalMetricsDataLoader,
Expand Down Expand Up @@ -49,6 +50,7 @@ class Context:
response: Optional[Response]
db: Callable[[], AsyncContextManager[AsyncSession]]
data_loaders: DataLoaders
cache_for_dataloaders: Optional[CacheForDataLoaders]
model: Model
export_path: Path
corpus: Optional[Model] = None
Expand Down
5 changes: 3 additions & 2 deletions src/phoenix/server/api/dataloaders/__init__.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
from dataclasses import dataclass, field
from functools import singledispatchmethod
from typing import Union

from phoenix.db.insertion.evaluation import (
DocumentEvaluationInsertionResult,
SpanEvaluationInsertionResult,
TraceEvaluationInsertionResult,
)
from phoenix.db.insertion.span import SpanInsertionResult
from phoenix.db.insertion.span import ClearProjectSpansResult, SpanInsertionResult

from .document_evaluation_summaries import (
DocumentEvaluationSummaryCache,
Expand Down Expand Up @@ -61,7 +62,7 @@ class CacheForDataLoaders:
)

@singledispatchmethod
def invalidate(self, result: SpanInsertionResult) -> None:
def invalidate(self, result: Union[SpanInsertionResult, ClearProjectSpansResult]) -> None:
project_rowid, *_ = result
self.latency_ms_quantile.invalidate(project_rowid)
self.token_count.invalidate(project_rowid)
Expand Down
3 changes: 3 additions & 0 deletions src/phoenix/server/api/schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@

from phoenix.config import DEFAULT_PROJECT_NAME
from phoenix.db import models
from phoenix.db.insertion.span import ClearProjectSpansResult
from phoenix.pointcloud.clustering import Hdbscan
from phoenix.server.api.context import Context
from phoenix.server.api.helpers import ensure_list
Expand Down Expand Up @@ -294,6 +295,8 @@ async def clear_project(self, info: Info[Context, None], id: GlobalID) -> Query:
delete_statement = delete(models.Trace).where(models.Trace.project_rowid == project_id)
async with info.context.db() as session:
await session.execute(delete_statement)
if cache := info.context.cache_for_dataloaders:
cache.invalidate(ClearProjectSpansResult(project_rowid=project_id))
return Query()


Expand Down
1 change: 1 addition & 0 deletions src/phoenix/server/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -225,6 +225,7 @@ async def get_context(
),
trace_evaluations=TraceEvaluationsDataLoader(self.db),
),
cache_for_dataloaders=self.cache_for_dataloaders,
)


Expand Down
1 change: 1 addition & 0 deletions tests/server/api/types/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ def create_context(
export_path=Path(TemporaryDirectory().name),
db=None, # TODO(persistence): add mock for db
data_loaders=None, # TODO(persistence): add mock for data_loaders
cache_for_dataloaders=None,
)

return create_context
Expand Down

0 comments on commit 2944ae5

Please sign in to comment.