-
Notifications
You must be signed in to change notification settings - Fork 3k
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
fix(ingestion/airflow-plugin): airflow remove old tasks #10485
Merged
anshbansal
merged 9 commits into
datahub-project:master
from
dushayntAW:fix/ING-447/airflow-not-removing-old-task
Jun 10, 2024
Merged
Changes from 6 commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
4f7133f
fix(ingestion/airflow-plugin): airflow remove old tasks
dushayntAW f76fdd0
fix(ingestion/airflow-plugin): fixed linter issues
dushayntAW 0d36c63
fix(ingestion/airflow-plugin): airflow remove old tasks
dushayntAW 49c689c
fix(ingestion/airflow-plugin): airflow remove old tasks
dushayntAW 5050641
fix(ingestion/airflow-plugin): fix linter issue
dushayntAW 94f7c0d
fix(ingestion/airflow-plugin): fix linter issue
dushayntAW bdfb809
fix(ingestion/airflow-plugin): fixed review comments
dushayntAW 4c1ed05
fix(ingestion/airflow-plugin): added a functionality ti filter out th…
dushayntAW 071b9ec
fix(ingestion/airflow-plugin): fixed linter issues
dushayntAW File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
import asyncio | ||
import copy | ||
import functools | ||
import logging | ||
|
@@ -8,6 +9,7 @@ | |
|
||
import airflow | ||
import datahub.emitter.mce_builder as builder | ||
from airflow.models.serialized_dag import SerializedDagModel | ||
from datahub.api.entities.datajob import DataJob | ||
from datahub.api.entities.dataprocess.dataprocess_instance import InstanceRunResult | ||
from datahub.emitter.mcp import MetadataChangeProposalWrapper | ||
|
@@ -68,6 +70,7 @@ def hookimpl(f: _F) -> _F: # type: ignore[misc] # noqa: F811 | |
"1", | ||
) | ||
_RUN_IN_THREAD_TIMEOUT = 30 | ||
_DATAHUB_CLEANUP_DAG = "Datahub_Cleanup" | ||
|
||
|
||
def get_airflow_plugin_listener() -> Optional["DataHubListener"]: | ||
|
@@ -541,6 +544,48 @@ def on_dag_start(self, dag_run: "DagRun") -> None: | |
|
||
self.emitter.emit(event) | ||
|
||
if dag.dag_id == _DATAHUB_CLEANUP_DAG: | ||
assert self.graph | ||
|
||
logger.debug("Initiating the cleanup of obsselete data from datahub") | ||
|
||
ingested_dataflow_urns = list( | ||
self.graph.get_urns_by_filter( | ||
platform="airflow", entity_types=["dataFlow"] | ||
) | ||
) | ||
ingested_datajob_urns = list( | ||
self.graph.get_urns_by_filter( | ||
platform="airflow", entity_types=["dataJob"] | ||
) | ||
) | ||
|
||
all_airflow_dags = SerializedDagModel.read_all_dags().values() | ||
|
||
airflow_flow_urns: List = [] | ||
airflow_job_urns: List = [] | ||
|
||
for dag in all_airflow_dags: | ||
flow_urn = builder.make_data_flow_urn( | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. cluster should be passed in if exists There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. same as other comment |
||
orchestrator="airflow", flow_id=dag.dag_id | ||
) | ||
airflow_flow_urns.append(flow_urn) | ||
|
||
for task in dag.tasks: | ||
airflow_job_urns.append( | ||
builder.make_data_job_urn_with_flow(str(flow_urn), task.task_id) | ||
) | ||
|
||
obsolete_pipelines = set(ingested_dataflow_urns) - set(airflow_flow_urns) | ||
obsolete_tasks = set(ingested_datajob_urns) - set(airflow_job_urns) | ||
|
||
obsolete_urns = obsolete_pipelines.union(obsolete_tasks) | ||
|
||
asyncio.run(self._soft_delete_obsolete_urns(obsolete_urns=obsolete_urns)) | ||
|
||
logger.debug(f"total pipelines removed = {len(obsolete_pipelines)}") | ||
logger.debug(f"total tasks removed = {len(obsolete_tasks)}") | ||
|
||
if HAS_AIRFLOW_DAG_LISTENER_API: | ||
|
||
@hookimpl | ||
|
@@ -577,3 +622,13 @@ def on_dataset_changed(self, dataset: "Dataset") -> None: | |
logger.debug( | ||
f"DataHub listener got notification about dataset change for {dataset}" | ||
) | ||
|
||
async def _soft_delete_obsolete_urns(self, obsolete_urns): | ||
delete_tasks = [self._delete_obsolete_data(urn) for urn in obsolete_urns] | ||
await asyncio.gather(*delete_tasks) | ||
|
||
async def _delete_obsolete_data(self, obsolete_urn): | ||
assert self.graph | ||
|
||
if self.graph.exists(str(obsolete_urn)): | ||
self.graph.soft_delete_entity(str(obsolete_urn)) |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think you should filter for
cluster
as well; otherwise if user has multiple Airflow instance you will delete dags which you shouldn't.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I am filtering the entire URN which is already having the
cluster
i.e.urn:li:dataFlow:(airflow,simple_dag,prod)
So, still we need to match/filter
cluster
explicitly? or my understanding is wrong.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If you check here we use cluster or env to generate the DataFlow Urns, so it is part of the urn. ->
datahub/metadata-ingestion/src/datahub/api/entities/datajob/dataflow.py
Line 75 in 90febde
This means if the env or cluster is set and has multiple Airflow environments like
DEV
andPROD
, then your query will return the urns for both PROD and DEV, which we don't want in this case as these are different Airflow environment.You should add cluster/env as a filter parameter.