Skip to content

Commit

Permalink
Don't crash on UnrepeatableReadErrors from knowledge db
Browse files Browse the repository at this point in the history
  • Loading branch information
egbertbouman committed Sep 5, 2024
1 parent d63f266 commit 7518afe
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 7 deletions.
17 changes: 11 additions & 6 deletions src/tribler/core/database/layers/knowledge.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

from pony import orm
from pony.orm import raw_sql
from pony.orm.core import Database, Entity, Query, select
from pony.orm.core import Database, Entity, Query, UnrepeatableReadError, select
from pony.utils import between

from tribler.core.database.layers.layer import EntityImpl, Layer
Expand Down Expand Up @@ -113,7 +113,7 @@ class StatementOp(EntityImpl):
updated_at: datetime.datetime
auto_generated: bool

def __init__(self, statement: Statement, peer: Peer, operation: int, clock: int, # noqa: D107, PLR0913
def __init__(self, statement: Statement, peer: Peer, operation: int, clock: int, # noqa: D107
signature: bytes, auto_generated: bool) -> None: ...

@staticmethod
Expand Down Expand Up @@ -278,7 +278,7 @@ def _get_resources(self, resource_type: ResourceType | None, name: str | None, c
results = results.filter(lambda r: r.type == resource_type.value)
return results

def get_statements(self, source_type: ResourceType | None, source_name: str | None, # noqa: PLR0913
def get_statements(self, source_type: ResourceType | None, source_name: str | None,
statements_getter: Callable[[Entity], Entity],
target_condition: Callable[[Statement], bool], condition: Callable[[Statement], bool],
case_sensitive: bool, ) -> Iterator[Statement]:
Expand Down Expand Up @@ -443,9 +443,14 @@ def get_simple_statements(self, subject_type: ResourceType | None = None, subjec
case_sensitive=case_sensitive,
)

return [SimpleStatement(subject_type=s.subject.type, subject=s.subject.name, predicate=s.object.type,
object=s.object.name)
for s in statements]
results = []
for s in statements:
try:
results.append(SimpleStatement(subject_type=s.subject.type, subject=s.subject.name,
predicate=s.object.type, object=s.object.name))
except UnrepeatableReadError as e:
self.logger.exception(e)
return results

def get_suggestions(self, subject_type: ResourceType | None = None, subject: str | None = "",
predicate: ResourceType | None = None, case_sensitive: bool = True) -> List[str]:
Expand Down
4 changes: 3 additions & 1 deletion src/tribler/core/knowledge/community.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
from ipv8.keyvault.private.libnaclkey import LibNaCLSK
from ipv8.lazy_community import lazy_wrapper
from ipv8.types import Key, Peer
from pony.orm import db_session
from pony.orm import UnrepeatableReadError, db_session

from tribler.core.database.layers.knowledge import Operation, ResourceType
from tribler.core.database.tribler_database import TriblerDatabase
Expand Down Expand Up @@ -128,6 +128,8 @@ def on_request(self, peer: Peer, operation: RequestStatementOperationMessage) ->
sent_operations.append(operation)
except ValueError as e: # validation error
self.logger.warning(e)
except UnrepeatableReadError as e: # database error
self.logger.exception(e)
if sent_operations:
sent_tags_info = ", ".join(f"({t})" for t in sent_operations)
self.logger.debug("-> sent operations (%s) to peer: %s", sent_tags_info, peer.mid.hex())
Expand Down

0 comments on commit 7518afe

Please sign in to comment.