Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 31 additions & 1 deletion diracx-db/src/diracx/db/os/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,14 @@
import json
import logging
from abc import ABCMeta, abstractmethod
from collections.abc import AsyncIterator
from collections.abc import AsyncIterator, Iterable
from contextvars import ContextVar
from datetime import datetime
from typing import Any, Self

from opensearchpy import AsyncOpenSearch
from opensearchpy.exceptions import RequestError
from opensearchpy.helpers import async_bulk

from diracx.core.exceptions import DocumentUpsertError, InvalidQueryError
from diracx.core.extensions import DiracEntryPoint, select_from_extension
Expand Down Expand Up @@ -213,6 +214,35 @@ async def upsert(self, vo: str, doc_id: int, document: Any) -> None:
response,
)

async def bulk_upsert(

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

bulk_upsert would need to overridden within job_parameters_db because we are inserting a JobID and a timestamp:

def upsert(self, vo, doc_id, document):
document = {
"JobID": doc_id,
"timestamp": int(datetime.now(tz=UTC).timestamp() * 1000),
**document,
}
return super().upsert(vo, doc_id, document)

Here it would not work I think (and it looks like it's not spotted within the tests).

I actually wonder whether upsert is useful now that we have bulk_upsert.
I would suggest we just drop upsert and replace it everywhere with bulk_upsert, what do you think?

self,
documents: Iterable[tuple[str, int, dict[str, Any]]],
) -> tuple[int, list[Any]]:
"""Bulk upsert documents."""
actions = (
{
"_op_type": "update",
"_index": self.index_name(vo, doc_id),
"_id": doc_id,
"doc": document,
"doc_as_upsert": True,
"retry_on_conflict": 10,
}
for vo, doc_id, document in documents
)

success, errors = await async_bulk(
self.client,
actions,
raise_on_error=False,
raise_on_exception=False,
Comment thread
aldbr marked this conversation as resolved.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm just wondering what happens if there is a connection issue with the DB and no exception is raised.
I guess you would get 0 success, N errors but would get any information to know that there is an issue with the DB itself?

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

)

if errors:
logger.warning("Bulk upsert completed with %d errors", len(errors))

return success, errors

async def search(
self, parameters, search, sorts, *, per_page: int = 100, page: int | None = None
) -> list[dict[str, Any]]:
Expand Down
3 changes: 3 additions & 0 deletions diracx-db/tests/conftest.py

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same comment here

Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
from __future__ import annotations

pytest_plugins = ["diracx.testing.mock_osdb"]
Loading
Loading