Skip to content
This repository has been archived by the owner on Aug 4, 2023. It is now read-only.

Add Airflow variable used to configure overrides for task timeouts #976

Merged
merged 8 commits into from
Mar 9, 2023

Conversation

stacimc
Copy link
Contributor

@stacimc stacimc commented Jan 28, 2023

Fixes

Fixes WordPress/openverse#1437 by @AetherUnbound

Description

Adds an Airflow Variable that can be used optionally to temporarily alter the execution timeouts for any task in a provider DAG, using a task_id_pattern. The timeout overrides are configured in the format <days>d:<hours>h:<minutes>m:<seconds>s. Example using the CONFIGURATION_OVERRIDES Airflow variable:

{
  # Example showing overriding timeouts for multiple tasks within a DAG
  "brooklyn_museum_workflow": [
    {
      "task_id_pattern": "pull_image_data",
      "timeout": "10h"  # 10 hrs
    },
    {
      "task_id_pattern": "report_load_completion",
      "timeout": "4s"  # 4 seconds
    }
  ],
  # Example showing task_id_pattern matching reingestion tasks (that have `day_shift_x` suffixes)
  "metropolitan_museum_reingestion_workflow": [
    {
      "task_id_pattern": "upsert_data",
      "timeout": "6d:11h"  # 6 days, 11 hours
    }
  ],
  "inaturalist_workflow": [
    {
      "task_id_pattern": "load_taxa",
      "timeout": "6d:10h:9s"  # 6 days, 10 hours, 9 seconds
    },
    # Example of a mapped task
    {
      "task_id_pattern": "load_transformed_data",
      "timeout": "10m"  # 10 minutes
    }
  ]
}

This is intentionally implemented in such a way that it can be extended very quickly to override other properties of tasks. I had retries in mind but did not include it in this PR for simplicity.

Testing Instructions

You can use the example CONFIGURATION_OVERRIDES provided above, and play around with adding other configurations. For each timeout you configure, run the DAG and check the Task Instance Details page for the task to verify that the execution_timeout is set to the one you configured. You can try setting really short timeouts on long-running tasks like pull_data to verify that the timeouts will actually be hit.

Make sure to test:

  • tasks on a regular ingestion workflow, such as pull_image_data
  • tasks on a reingestion workflow with day_shift applied. Example, try upsert_data and verify that the timeout gets applied to all upsert_data_day_shift_x tasks within the workflow
  • tasks on iNaturalist (our only example of a 'custom' workflow)
    • specifically, iNaturalist's load_transformed_data task, which is a Mapped task. The timeout should be applied to each mapped task

Notes:

  • Improperly formatted overrides are simply ignored, rather than raising an error which will break all provider DAGs.

Checklist

  • My pull request has a descriptive title (not a vague title like
    Update index.md).
  • My pull request targets the default branch of the repository (main) or
    a parent feature branch.
  • My commit messages follow best practices.
  • My code follows the established code style of the repository.
  • I added or updated tests for the changes I made (if applicable).
  • I added or updated documentation (if applicable).
  • I tried running the project locally and verified that there are no visible
    errors.
  • I ran the DAG documentation generator (if applicable).

Developer Certificate of Origin

Developer Certificate of Origin
Developer Certificate of Origin
Version 1.1

Copyright (C) 2004, 2006 The Linux Foundation and its contributors.
1 Letterman Drive
Suite D4700
San Francisco, CA, 94129

Everyone is permitted to copy and distribute verbatim copies of this
license document, but changing it is not allowed.


Developer's Certificate of Origin 1.1

By making a contribution to this project, I certify that:

(a) The contribution was created in whole or in part by me and I
    have the right to submit it under the open source license
    indicated in the file; or

(b) The contribution is based upon previous work that, to the best
    of my knowledge, is covered under an appropriate open source
    license and I have the right under that license to submit that
    work with modifications, whether created in whole or in part
    by me, under the same open source license (unless I am
    permitted to submit under a different license), as indicated
    in the file; or

(c) The contribution was provided directly to me by some other
    person who certified (a), (b) or (c) and I have not modified
    it.

(d) I understand and agree that this project and the contribution
    are public and that a record of the contribution (including all
    personal information I submit with it, including my sign-off) is
    maintained indefinitely and may be redistributed consistent with
    this project or the open source license(s) involved.

@stacimc stacimc added 🟩 priority: low Low priority and doesn't need to be rushed ✨ goal: improvement Improvement to an existing user-facing feature 💻 aspect: code Concerns the software code in the repository labels Jan 28, 2023
@stacimc stacimc self-assigned this Jan 28, 2023
@AetherUnbound
Copy link
Contributor

Dang, this is really tough! The flexibility of defining agnostic, per-task timeouts is neat. But I think about what I had to do for WordPress/openverse#1301 - because of the reingestion workflows, we'd probably also need to make this a task ID pattern for matching timeouts 😕 How much additional complexity would that add do you think?

I think it's fair to try and target the typical ProviderDataIngester classes with this, and maybe try to fit iNaturalist into that mold as you're suggesting. Even that utility alone would be beneficial!

@stacimc
Copy link
Contributor Author

stacimc commented Feb 22, 2023

@AetherUnbound It ended up being easier than I thought! Updated version of the PR now allows arbitrarily overriding timeout on any task in a provider DAG, including iNaturalist.

@stacimc stacimc marked this pull request as ready for review February 22, 2023 20:14
@stacimc stacimc requested a review from a team as a code owner February 22, 2023 20:14
Copy link
Contributor

@AetherUnbound AetherUnbound left a comment

Choose a reason for hiding this comment

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

Woohoo! What an excellent feature this will unlock for us. I have one request regarding the timeout format that I'd like your thoughts on 🕐

openverse_catalog/dags/providers/provider_workflows.py Outdated Show resolved Hide resolved
# Override the dag_id
self.dag_id = f"{self.provider_name}_reingestion_workflow"
return
_, provider_name = self._get_module_info()
Copy link
Contributor

Choose a reason for hiding this comment

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

This is a much better approach! 😄

Comment on lines +121 to +126
# If the task is a MappedOperator, apply the timeout to partial
# kwargs to ensure it is set on each mapped task.
if isinstance(task, MappedOperator):
task.partial_kwargs["execution_timeout"] = timeout_override
else:
task.execution_timeout = timeout_override
Copy link
Contributor

Choose a reason for hiding this comment

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

Wow, what a find! 😱

Copy link
Contributor

@AetherUnbound AetherUnbound left a comment

Choose a reason for hiding this comment

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

This is an excellent change! I know it can be hard to test, but I love the ability to just have the time range we need specified (e.g. 10h). Very excited for this!

Comment on lines +84 to +92
match unit:
case "d":
days = int(count)
case "h":
hours = int(count)
case "m":
minutes = int(count)
case "s":
seconds = int(count)
Copy link
Contributor

Choose a reason for hiding this comment

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

A case statement!! Wow, my first ever! 😄

openverse_catalog/dags/providers/provider_workflows.py Outdated Show resolved Hide resolved
Copy link
Member

@krysal krysal left a comment

Choose a reason for hiding this comment

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

It's so flexible! Awesome, it worked like a charm! 💯

@obulat obulat added 🧱 stack: catalog Related to the catalog and Airflow DAGs and removed 🧱 stack: catalog Related to the catalog and Airflow DAGs labels Mar 9, 2023
@stacimc stacimc merged commit 63cfe8a into main Mar 9, 2023
@stacimc stacimc deleted the add/configuration-overrides-for-dag-timeouts branch March 9, 2023 18:24
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
💻 aspect: code Concerns the software code in the repository ✨ goal: improvement Improvement to an existing user-facing feature 🟩 priority: low Low priority and doesn't need to be rushed 🧱 stack: catalog Related to the catalog and Airflow DAGs
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Allow execution timeouts to be overridden by Variables
4 participants