hop airflow-bundle

Overview

hop airflow-bundle lets Apache Airflow run the workflows of a Hop project. Every workflow of the project becomes a DAG with one task per action: see Tasks.

The command writes a small stub jar, hop-airflow.jar, at the root of the project. The stub holds no code: it tells Airflow’s Java coordinator to start the Hop installation on the worker. Airflow reads the project as a DAG bundle in one of two ways:

  • Git: without --target, the stub goes into the project folder. Commit it with the project; Airflow reads the repository with a Git DAG bundle.

  • Folder: with --target, the project is copied to that folder and the stub is written there. Airflow reads the folder with a local DAG bundle. Hidden files and folders, such as .git, are not copied. The folder must be empty or hold a bundle written there before, which is replaced as a whole. Hop marks such a folder with a hidden file, .hop-airflow-bundle, and leaves any other folder alone.

To publish a folder bundle and run a workflow on Airflow from Hop GUI, use the Apache Airflow run configuration.

This was tested with Airflow 3.4.0 and its Java SDK 1.0.0, which Airflow calls experimental.

Usage

bash
./hop airflow-bundle -j sales
./hop airflow-bundle -j sales -t /data/airflow/bundles/sales
Option Description

-j, --project

The project to make a DAG bundle of. Its name becomes the name of the project on the workers, and the start of the DAG ids.

-t, --target

Copy the project to this folder and write the stub there.

-hh, --worker-hop-home

Where Hop is installed on the Airflow workers. Default: /opt/hop.

-r, --runconfig

The workflow run configuration of the runs. Default: local.

-l, --level

The log level of the runs. Default: Basic.

-w, --workflows

Glob patterns of the workflows that become DAGs, relative to the project, for example main-.hwf,etl/*. Default: every workflow.

-ef, --environment-files

Environment configuration files for the runs, relative to the project folder. The runs then use an environment called airflow with these files.

--dag-id-prefix

The start of every DAG id. Default: the project name and two underscores.

The stub is tied to the Hop version that wrote it. After you upgrade Hop on the workers, run hop airflow-bundle again with the new version. Otherwise the DAGs can fail to load: Airflow then reports an import error, and the DAG processor’s log says that the stub and the workers' Hop do not match.

What Airflow gets

  • DAG id: the prefix and the path of the workflow without .hwf, with folders separated by two underscores. etl/load.hwf in project sales becomes salesetlload.

  • Tags: hop and the project name.

  • Description: the description of the workflow.

  • Schedule: none. Start the DAGs from Airflow, through its REST API, with the Trigger Airflow DAG run action, or with the Apache Airflow run configuration. The schedule of the Start action is not used: a Start action that repeats runs the workflow once.

  • Run conf: each key of the DAG run’s conf becomes a parameter, or a variable when the workflow does not declare it, as with hop run -p. Key hop_workflow_version is the exception: the Apache Airflow run configuration sets it to the version of the workflow it published, and the tasks of a run fail when the bundle holds another version. Text is passed as it is, including commas; numbers, lists and objects are passed as JSON. A key with = fails the task: hop run would split it in the wrong place. The Java SDK cannot declare DAG params, so the trigger form in Airflow shows no fields: pass the values as JSON.

  • Result: every task keeps the outcome of its run in XCom hop_outcome, including success, exitCode, name, executionId, startDate, endDate, the counters, and error when the run failed. Apart from conditions (see below), the return value of the task holds the same outcome.

  • Stop: marking the DAG run failed stops the running actions as the Stop button in Hop GUI does.

Tasks

Each action that the Start action leads to becomes a task, with the name of the action as its task id. Characters that Airflow does not allow in a task id become _. The hops become the dependencies between the tasks:

In the workflow In Airflow

A success hop

The next task runs when the task before it succeeded. A task fails when the result of its action is false.

A failure hop

The next task runs when the task before it failed (one_failed).

An unconditional hop

The next task runs either way (none_skipped).

An action with both success and failure hops leaving it

The task is a condition: it succeeds, and Airflow skips the side its result does not take. A failure that the workflow handles leaves the DAG run successful, as the workflow is in Hop. Its return value is the result of the action, true or false.

Several hops that lead to one action

The task runs once, when the first of those hops is followed (one_success). In Hop, the action runs each time one of them is.

A Join action

The task waits for all the tasks before it, as the Join does in Hop (none_failed_min_one_success). It fails when an action with a success hop into the Join ended false, or did not run because an action before it ended false, as the Join does in Hop. Its failure hops are followed then, also when Airflow did not run the Join because a task before it failed. It does not start Hop.

Where several hops lead to one action, and where a side of a condition has more than one action, the hops go through gates: tasks called after the action before them, such as Load__else, that pass its result and variables on. A gate whose hop is not followed is skipped.

When a task before a failure or unconditional hop did not run, because a task before that failed, the workflow stops there in Hop. Airflow still runs the next task: that task then fails without running its action, and says why in its log.

Hops that Hop never follows are left out: disabled hops, and success or failure hops that leave an action that does not evaluate its result, such as Start or Dummy. hop airflow-bundle lists those actions in the stub, from the plugins of the Hop installation that writes it: write the stub with the same plugins as the workers.

Each task runs its action in a new JVM, which takes a few seconds to start Hop. What an action passes on to the next ones in Hop is passed on through XCom:

  • the result of the action: its rows, files and counters. As in Hop, the actions after an action that runs the next actions in parallel get its result without the rows.

  • the variables set so far. Where branches meet, each variable keeps the value that was set last.

Airflow keeps XCom in its database: a task that would pass on more than 1 MB fails. Pass large results on through a file or a table instead.

Everyone who can see the DAG runs in Airflow can read what goes through XCom and the run conf, as plain text: the result rows, the variables that actions set during the run, and the parameter values. Keep passwords and other secrets out of them: put them in the environment files of the workers, or let a pipeline read them where it needs them.

Parallel branches run in parallel in Airflow, also when their action does not run the next actions in parallel in Hop.

A workflow runs as one task, run_workflow, when its hops don’t fit Airflow:

  • a hop that leads back to an earlier action, such as a loop that waits and checks again.

  • an action that the Start action leads to, and another action as well.

  • no Start action, or more than one.

The DAG processor’s log says why.

Set up Airflow

The workers

The workers that run the Hop tasks need Java 21 and a Hop installation, at /opt/hop unless you used --worker-hop-home. The workers do not need a Hop configuration for the project: the project of the bundle is registered in memory for each run. HOP_CONFIG_FOLDER, HOP_SHARED_JDBC_FOLDERS, HOP_PLUGIN_BASE_FOLDERS, HOP_AUDIT_FOLDER and the password encoder variables are used when they are set on the worker, as the hop script does. Relative folders in them are relative to the Hop installation, as with the hop script.

The Java coordinator

Airflow starts a Java task through a coordinator, and finds the coordinator through the task’s queue. The DAGs of a Hop bundle use queue java. Configure a Java coordinator for that queue and for the DAG bundle, here called sales, with the Java options of the hop script:

bash
AIRFLOW__SDK__COORDINATORS='{"java": {"classpath": "airflow.sdk.coordinators.java.JavaCoordinator",
  "kwargs": {"java_executable": "/opt/java/openjdk/bin/java",
             "jvm_args": ["-Xmx2g",
               "--add-opens", "java.xml/jdk.xml.internal=ALL-UNNAMED",
               "--add-opens", "java.base/java.lang=ALL-UNNAMED",
               "--add-opens", "java.base/java.lang.invoke=ALL-UNNAMED",
               "--add-opens", "java.base/java.lang.reflect=ALL-UNNAMED",
               "--add-opens", "java.base/java.io=ALL-UNNAMED",
               "--add-opens", "java.base/java.net=ALL-UNNAMED",
               "--add-opens", "java.base/java.nio=ALL-UNNAMED",
               "--add-opens", "java.base/java.util=ALL-UNNAMED",
               "--add-opens", "java.base/java.util.concurrent=ALL-UNNAMED",
               "--add-opens", "java.base/java.util.concurrent.atomic=ALL-UNNAMED",
               "--add-opens", "java.base/sun.nio.ch=ALL-UNNAMED",
               "--add-opens", "java.base/sun.nio.cs=ALL-UNNAMED",
               "--add-opens", "java.base/sun.security.action=ALL-UNNAMED",
               "--add-opens", "java.base/sun.util.calendar=ALL-UNNAMED",
               "--add-exports", "java.base/sun.nio.ch=ALL-UNNAMED"]}}}'
AIRFLOW__SDK__QUEUE_TO_COORDINATOR='{"java": "java"}'
AIRFLOW__SDK__DAG_BUNDLE_TO_COORDINATOR='{"sales": "java"}'

The DAG bundle

A Git repository:

bash
AIRFLOW__DAG_PROCESSOR__DAG_BUNDLE_CONFIG_LIST='[{"name": "sales",
  "classpath": "airflow.providers.git.bundles.git.GitDagBundle",
  "kwargs": {"tracking_ref": "main", "git_conn_id": "sales_repository"}}]'

When the project is in a folder of the repository, add "subdir": "<folder>" to the kwargs.

A folder written with --target:

bash
AIRFLOW__DAG_PROCESSOR__DAG_BUNDLE_CONFIG_LIST='[{"name": "sales",
  "classpath": "airflow.dag_processing.bundles.local.LocalDagBundle",
  "kwargs": {"path": "/data/airflow/bundles/sales"}}]'

Keep the bundles of your Python DAGs in the list as well.