Skip to content
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

[Core] Add retry for failed upserts and handle circular dependencies #1241

Conversation

ivankalinovski
Copy link
Contributor

@ivankalinovski ivankalinovski commented Dec 15, 2024

Description

What -

  1. Register callbacks of failed entities.
  2. When done with upserts, try topological sort on failed entities.
  3. On fail of retry because of topological sort - try unsorted upsert.
  4. Update topological's sort tree creation so an entity cannot be it's own dependency.
  5. Test upsert with dependencies, with self circular dependency and external entity dependency.

Why -
Users having circular dependency errors and entities are not upserted.

Type of change

Please leave one option from the following and delete the rest:

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • New Integration (non-breaking change which adds a new integration)
  • Breaking change (fix or feature that would cause existing functionality to not work as expected)
  • Non-breaking change (fix of existing functionality that will not change current behavior)
  • Documentation (added/updated documentation)

All tests should be run against the port production environment(using a testing org).

Core testing checklist

  • Integration able to create all default resources from scratch
  • Resync finishes successfully
  • Resync able to create entities
  • Resync able to update entities
  • Resync able to detect and delete entities
  • Scheduled resync able to abort existing resync and start a new one
  • Tested with at least 2 integrations from scratch
  • Tested with Kafka and Polling event listeners
  • Tested deletion of entities that don't pass the selector

Integration testing checklist

  • Integration able to create all default resources from scratch
  • Resync able to create entities
  • Resync able to update entities
  • Resync able to detect and delete entities
  • Resync finishes successfully
  • If new resource kind is added or updated in the integration, add example raw data, mapping and expected result to the examples folder in the integration directory.
  • If resource kind is updated, run the integration with the example data and check if the expected result is achieved
  • If new resource kind is added or updated, validate that live-events for that resource are working as expected
  • Docs PR link here

Preflight checklist

  • Handled rate limiting
  • Handled pagination
  • Implemented the code in async
  • Support Multi account

Screenshots

Include screenshots from your environment showing how the resources of the integration will look.

API Documentation

Provide links to the API documentation used for this integration.

@ivankalinovski ivankalinovski requested a review from a team as a code owner December 15, 2024 13:51
@ivankalinovski ivankalinovski force-pushed the PORT-11871-bug-bug-in-cycle-dependencies-affecting-argo-cd-integration branch from e152ca7 to 6ef411a Compare December 15, 2024 14:23
1. Register callbacks of failed entities.
2. When done with upserts, try topological sort on failed entities.
3. On fail of retry because of topological sort - try unsorted upsert.
4. Update topological's sort tree creation so an entity cannot be it's own dependency.
5. Test upsert with dependencies, with self circular dependency and external entity dependency.
@ivankalinovski ivankalinovski force-pushed the PORT-11871-bug-bug-in-cycle-dependencies-affecting-argo-cd-integration branch from 5ab5b5b to c89b550 Compare December 15, 2024 15:11
Copy link
Contributor

@Tankilevitch Tankilevitch left a comment

Choose a reason for hiding this comment

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

nice job!! left a few comments

Comment on lines 129 to 130
if upsertedEntity is False:
event.register_failed_upsert_call_arguments(
Copy link
Contributor

Choose a reason for hiding this comment

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

maybe worth adding a log message that we will try it at the end of the sync

CHANGELOG.md Outdated
- Register callbacks of failed entities.
- Test upsert with dependencies, with self circular dependency and external entity dependency.
- Update topologicals sort tree creation so an entity cannot be its own dependency.
- When done with upserts, try topological sort on failed entities.
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
- When done with upserts, try topological sort on failed entities.
- When `createMissingRelatedEntities` is set to `false` and upserting entity failed on not existing entity, the entity will be gathered to the end of the resync and will try sorting all the failed entities through a topological sort and upsert them as well

CHANGELOG.md Outdated
- On fail of retry because of topological sort - try unsorted upsert.
- Register callbacks of failed entities.
- Test upsert with dependencies, with self circular dependency and external entity dependency.
- Update topologicals sort tree creation so an entity cannot be its own dependency.
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
- Update topologicals sort tree creation so an entity cannot be its own dependency.
- Fix topologicals sort tree creation so an entity cannot be its own dependency

CHANGELOG.md Outdated

### Bug Fixes

- On fail of retry because of topological sort - try unsorted upsert.
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
- On fail of retry because of topological sort - try unsorted upsert.
- When experiencing cyclic error on topological sort try unsorted upsert of the entities

CHANGELOG.md Outdated
### Bug Fixes

- On fail of retry because of topological sort - try unsorted upsert.
- Register callbacks of failed entities.
Copy link
Contributor

Choose a reason for hiding this comment

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

can be removed

result = response.json()
if (
response.status_code == 404
and result.get("ok") is False
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
and result.get("ok") is False
and not result.get("ok")

port_ocean/clients/port/mixins/entities.py Outdated Show resolved Hide resolved
port_ocean/context/event.py Outdated Show resolved Hide resolved
Comment on lines 70 to 79
async def handle_failed(self) -> None:
entity_map: dict[
str, Callable[[], Coroutine[Any, Any, Entity | Literal[False] | None]]
] = {
f"{obj.identifier}-{obj.blueprint}": func
for obj, func in self._failed_entity_callback_list
}
entity_list: list[Entity] = [
obj for obj, func in self._failed_entity_callback_list
]
Copy link
Contributor

Choose a reason for hiding this comment

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

I wonder if this is actually a method that should be in the event(and maybe the whole handling stuff), I am leaning towards having a class for managing all of this and moving the instance for it to the event instance.

Copy link
Contributor

Choose a reason for hiding this comment

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

e.g.

EntityTopologicalHandler:
      def register_entities
      
      async def sort_and_upsert():


EventContext:
      entity_topological_handler = EntityTopologicalHandler()

Comment on lines 117 to 118
ordered_created_entities = reversed(
entities_with_search_identifier
+ order_by_entities_dependencies(entities_without_search_identifier)
entities_with_search_identifier + entities_without_search_identifier
Copy link
Contributor

Choose a reason for hiding this comment

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

what happens with search relation and identifier? do we want to register them as well?

1. Add robust tests.
2. Change the implementation of registering failed entities.
@ivankalinovski ivankalinovski force-pushed the PORT-11871-bug-bug-in-cycle-dependencies-affecting-argo-cd-integration branch from eac0300 to b7b88ff Compare December 19, 2024 14:37
@github-actions github-actions bot added size/L and removed size/M labels Dec 19, 2024
@@ -57,6 +57,13 @@ async def upsert_entity(
f"entity: {entity.identifier} of "
f"blueprint: {entity.blueprint}"
)
result = response.json()
if (
response.status_code == 404
Copy link
Contributor

Choose a reason for hiding this comment

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

lets use const of httpstatus.code

@@ -127,6 +126,13 @@ async def upsert(
)
if upsertedEntity:
modified_entities.append(upsertedEntity)
if upsertedEntity is False:
Copy link
Contributor

Choose a reason for hiding this comment

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

lets add a comment why is False and not if not upsertedEntity

Comment on lines 461 to 462
except:
await event.failed_entity_handler.handle_failed_no_sort()
Copy link
Contributor

Choose a reason for hiding this comment

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

lets except specific error type

Comment on lines 130 to 134
event.failed_entity_handler.register_failed_upsert_call_arguments(
entity,
event.port_app_config.get_port_request_options(),
user_agent_type,
self.context.port_client.upsert_entity,
Copy link
Contributor

Choose a reason for hiding this comment

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

lets only pass entity to it



@dataclass
class FailedEntityHandler:
Copy link
Contributor

Choose a reason for hiding this comment

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

I think this can be called
EntityTopologicalSorter

Comment on lines 459 to 462
try:
await event.failed_entity_handler.handle_failed()
except:
await event.failed_entity_handler.handle_failed_no_sort()
Copy link
Contributor

Choose a reason for hiding this comment

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

maybe something like this

Suggested change
try:
await event.failed_entity_handler.handle_failed()
except:
await event.failed_entity_handler.handle_failed_no_sort()
try:
for entity in event.failed_entity_handler.get_sorted()
await self.upsert_entity(
entity,
request_options,
user_agent_type,
should_raise=should_raise,
)
for entity in entities
),
except:
await event.failed_entity_handler.handle_failed_no_sort()

1. Extrac logic of topological sort into a class.
2. Modify tests.
@ivankalinovski ivankalinovski force-pushed the PORT-11871-bug-bug-in-cycle-dependencies-affecting-argo-cd-integration branch from c63bd2a to 209e64b Compare December 22, 2024 08:44
Comment on lines 61 to 73

if (
response.status_code == status.HTTP_404_NOT_FOUND
and not result.get("ok")
and result.get("error") == PortApiStatus.NOT_FOUND.value
):
return False
Copy link
Contributor

Choose a reason for hiding this comment

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

lets add a comment why we return False and what it means, add it to the method description as well

@@ -27,6 +27,10 @@ def is_installation_type_compatible(self, installation_type: str) -> bool:
) or installation_type == self.value


class PortApiStatus(Enum):
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
class PortApiStatus(Enum):
class PortAPIErrorMessage(Enum):

Copy link
Contributor

Choose a reason for hiding this comment

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

I think this should be in the core/utils rather than in the utils exposed to the integrations clients

Copy link
Contributor

Choose a reason for hiding this comment

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

I guess we can squeeze the entity_topological_sorter together with this?

@@ -29,7 +30,7 @@
)
from port_ocean.core.utils import zip_and_sum, gather_and_split_errors_from_results
from port_ocean.exceptions.core import OceanAbortException

import json
Copy link
Contributor

Choose a reason for hiding this comment

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

not used

Comment on lines 28 to 37
def get_entities(self) -> Generator[Entity, Any, None]:
entity_map: dict[str, Entity] = {
f"{entity.identifier}-{entity.blueprint}": entity
for entity in self.entities
}
sorted_and_mapped = 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
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
def get_entities(self) -> Generator[Entity, Any, None]:
entity_map: dict[str, Entity] = {
f"{entity.identifier}-{entity.blueprint}": entity
for entity in self.entities
}
sorted_and_mapped = 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
def get_entities(self, sorted: bool = True) -> Generator[Entity, Any, None]:
if not sorted:
for entity in self.entities:
yield entity
entity_map: dict[str, Entity] = {
f"{entity.identifier}-{entity.blueprint}": entity
for entity in self.entities
}
sorted_and_mapped = 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

Comment on lines 460 to 467
try:
for entity in event.entity_topological_sorter.get_entities():
await self.entities_state_applier.context.port_client.upsert_entity(entity,event.port_app_config.get_port_request_options(),user_agent_type,should_raise=False)

except OceanAbortException as ocean_abort:
if isinstance(ocean_abort.__cause__,CycleError):
for entity in event.entity_topological_sorter.entities:
await self.entities_state_applier.context.port_client.upsert_entity(entity,event.port_app_config.get_port_request_options(),user_agent_type,should_raise=False)
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
try:
for entity in event.entity_topological_sorter.get_entities():
await self.entities_state_applier.context.port_client.upsert_entity(entity,event.port_app_config.get_port_request_options(),user_agent_type,should_raise=False)
except OceanAbortException as ocean_abort:
if isinstance(ocean_abort.__cause__,CycleError):
for entity in event.entity_topological_sorter.entities:
await self.entities_state_applier.context.port_client.upsert_entity(entity,event.port_app_config.get_port_request_options(),user_agent_type,should_raise=False)
try:
for entity in event.entity_topological_sorter.get_entities():
await self.entities_state_applier.context.port_client.upsert_entity(entity,event.port_app_config.get_port_request_options(),user_agent_type,should_raise=False)
except OceanAbortException as ocean_abort:
if isinstance(ocean_abort.__cause__,CycleError):
for entity in event.entity_topological_sorter.get_entities(sorted=False):
await self.entities_state_applier.context.port_client.upsert_entity(entity,event.port_app_config.get_port_request_options(),user_agent_type,should_raise=False)

Comment on lines 460 to 462
try:
for entity in event.entity_topological_sorter.get_entities():
await self.entities_state_applier.context.port_client.upsert_entity(entity,event.port_app_config.get_port_request_options(),user_agent_type,should_raise=False)
Copy link
Contributor

Choose a reason for hiding this comment

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

lets add a log that x entities found that failed due to non existing relations, trying to re-ingest

and also a log if not

@ivankalinovski ivankalinovski force-pushed the PORT-11871-bug-bug-in-cycle-dependencies-affecting-argo-cd-integration branch from 7041bd7 to b9cf2c3 Compare December 22, 2024 14:51
1. Change location of files.
2. Exctract logic of handle failed into a function.
3. Update get_entities.
@ivankalinovski ivankalinovski force-pushed the PORT-11871-bug-bug-in-cycle-dependencies-affecting-argo-cd-integration branch from b9cf2c3 to 3d49fa7 Compare December 22, 2024 15:12
Comment on lines 33 to 38
) -> Entity | None | Literal[False]:
"""
[Entity] will be returned on happy flow
[None] will be returned if entity is using search identifier
[False] will be returned if upsert failed because of unmet dependency
"""
Copy link
Contributor

Choose a reason for hiding this comment

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

lets follow the function description format like in process_in_queue

@@ -115,8 +114,7 @@ async def upsert(
entities_without_search_identifier.append(entity)

ordered_created_entities = reversed(
Copy link
Contributor

Choose a reason for hiding this comment

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

there is no ordering needed any more, lets just use entities

Ivan Kalinovski and others added 4 commits December 23, 2024 16:36
1. Update `upsert_entity` function description.
2. Remove unused logic from `upsert`.
1. Fix lint
2. change order of assert in test
1. Update `entity_topological_sorter` to be created seperatly for each class instance.
2. Remove data class from EntityTopologicalSorter.
3. Update `entity_topological_sorter` to `_entity_topological_sorter`.
@ivankalinovski ivankalinovski force-pushed the PORT-11871-bug-bug-in-cycle-dependencies-affecting-argo-cd-integration branch from ffe2c27 to 5099a80 Compare December 23, 2024 19:17
Ivan Kalinovski added 3 commits December 24, 2024 10:25
Rename _entity_topological_sorter to entity_topological_sorter
1. Modify logs and pass count as param.
2. Verify `create_missing_related_entities` is false before `sort_and_upsert_failed_entities` execution.
Copy link
Contributor

@Tankilevitch Tankilevitch left a comment

Choose a reason for hiding this comment

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

lgtm


async def sort_and_upsert_failed_entities(self,user_agent_type: UserAgentType)->None:
try:
if not event.entity_topological_sorter.is_to_execute():
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
if not event.entity_topological_sorter.is_to_execute():
if not event.entity_topological_sorter.should_execute():

@ivankalinovski ivankalinovski merged commit b5690b8 into main Dec 24, 2024
18 checks passed
@ivankalinovski ivankalinovski deleted the PORT-11871-bug-bug-in-cycle-dependencies-affecting-argo-cd-integration branch December 24, 2024 14:51
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants