-
Notifications
You must be signed in to change notification settings - Fork 60
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Core] Add retry for failed upserts and handle circular dependencies
1. Change location of files. 2. Exctract logic of handle failed into a function. 3. Update get_entities.
- Loading branch information
Ivan Kalinovski
authored and
Ivan Kalinovski
committed
Dec 22, 2024
1 parent
209e64b
commit 3d49fa7
Showing
15 changed files
with
154 additions
and
74 deletions.
There are no files selected for viewing
This file contains 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 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 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 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 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 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 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 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 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 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,93 @@ | ||
from typing import Any, Generator | ||
from port_ocean.core.models import Entity | ||
|
||
from dataclasses import dataclass, field | ||
from loguru import logger | ||
|
||
from graphlib import TopologicalSorter, CycleError | ||
from typing import Set | ||
|
||
from port_ocean.exceptions.core import OceanAbortException | ||
|
||
Node = tuple[str, str] | ||
|
||
|
||
@dataclass | ||
class EntityTopologicalSorter: | ||
entities: list[Entity] = field(default_factory=list) | ||
|
||
def register_entity( | ||
self, | ||
entity: Entity, | ||
) -> None: | ||
logger.debug( | ||
f"Will retry upserting entity - {entity.identifier} at the end of resync" | ||
) | ||
self.entities.append(entity) | ||
|
||
def is_to_execute(self): | ||
return len(self.entities) | ||
|
||
def get_entities(self, sorted: bool = True) -> Generator[Entity, Any, None]: | ||
if not sorted: | ||
for entity in self.entities: | ||
yield entity | ||
return | ||
|
||
entity_map: dict[str, Entity] = { | ||
f"{entity.identifier}-{entity.blueprint}": entity | ||
for entity in self.entities | ||
} | ||
sorted_and_mapped = EntityTopologicalSorter.order_by_entities_dependencies( | ||
self.entities | ||
) | ||
for obj in sorted_and_mapped: | ||
entity = entity_map.get(f"{obj.identifier}-{obj.blueprint}") | ||
if entity is not None: | ||
yield entity | ||
|
||
@staticmethod | ||
def node(entity: Entity) -> Node: | ||
return entity.identifier, entity.blueprint | ||
|
||
@staticmethod | ||
def order_by_entities_dependencies(entities: list[Entity]) -> list[Entity]: | ||
nodes: dict[Node, Set[Node]] = {} | ||
entities_map = {} | ||
for entity in entities: | ||
nodes[EntityTopologicalSorter.node(entity)] = set() | ||
entities_map[EntityTopologicalSorter.node(entity)] = entity | ||
|
||
for entity in entities: | ||
relation_target_ids: list[str] = sum( | ||
[ | ||
identifiers if isinstance(identifiers, list) else [identifiers] | ||
for identifiers in entity.relations.values() | ||
if identifiers is not None | ||
], | ||
[], | ||
) | ||
related_entities = [ | ||
related | ||
for related in entities | ||
if related.identifier in relation_target_ids | ||
] | ||
|
||
for related_entity in related_entities: | ||
if ( | ||
entity.blueprint is not related_entity.blueprint | ||
or entity.identifier is not related_entity.identifier | ||
): | ||
nodes[EntityTopologicalSorter.node(entity)].add( | ||
EntityTopologicalSorter.node(related_entity) | ||
) | ||
|
||
sort_op = TopologicalSorter(nodes) | ||
try: | ||
return [entities_map[item] for item in sort_op.static_order()] | ||
except CycleError as ex: | ||
raise OceanAbortException( | ||
"Cannot order entities due to cyclic dependencies. \n" | ||
"If you do want to have cyclic dependencies, please make sure to set the keys" | ||
" 'createMissingRelatedEntities' and 'deleteDependentEntities' in the integration config in Port." | ||
) from ex |
File renamed without changes.
This file contains 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 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
2 changes: 1 addition & 1 deletion
2
...s/utils/test_entity_topological_sorter.py → ...e/utils/test_entity_topological_sorter.py
This file contains 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
Oops, something went wrong.