From f49713d84cd255f3ecbea0935e0b90a950fbd142 Mon Sep 17 00:00:00 2001 From: tnazarew Date: Thu, 19 Nov 2026 16:04:00 +0100 Subject: [PATCH] hive: run the scenarios locally the way CI does CI and the new local script call the same runner scripts: - runner/cluster.json: image and Hive properties per Hive release (were inline in the workflow) - runner/create_cluster.sh: creates the cluster with dev/dataproc/install_jars.sh, uploaded per cluster instead of to a shared path that concurrent runs overwrote; keeps the fixed transports-gcs snapshot as the default - runner/run_scenarios.sh: one Hive job per scenario, continues past failures, records the job's state and id in status.json - run_hive_dataproc_tests.sh: one command for a local run (jar from Maven, cluster deleted at the end, validation) run-hive-query now exits non-zero when the job doesn't end in DONE, so a failed query is never recorded as DONE. Removes runner/get_hive_openlineage_jars.sh and the unused properties in the run step. Co-Authored-By: Claude Opus 5.5 Signed-off-by: tnazarew --- .github/workflows/producer_hive_dataproc.yml | 110 ++--------- dev/dataproc/dataproc_workflow.py | 5 + producer/README.md | 1 + producer/hive_dataproc/README.md | 101 +++++++++- .../hive_dataproc/run_hive_dataproc_tests.sh | 127 +++++++++++++ producer/hive_dataproc/runner/cluster.json | 12 ++ .../hive_dataproc/runner/create_cluster.sh | 66 +++++++ .../runner/get_hive_openlineage_jars.sh | 32 ---- .../hive_dataproc/runner/run_scenarios.sh | 97 ++++++++++ scripts/tests/test_hive_runner.py | 178 ++++++++++++++++++ scripts/tests/test_run_scripts.py | 3 + 11 files changed, 603 insertions(+), 129 deletions(-) create mode 100644 producer/hive_dataproc/runner/cluster.json create mode 100755 producer/hive_dataproc/runner/create_cluster.sh delete mode 100755 producer/hive_dataproc/runner/get_hive_openlineage_jars.sh create mode 100755 producer/hive_dataproc/runner/run_scenarios.sh create mode 100644 scripts/tests/test_hive_runner.py diff --git a/.github/workflows/producer_hive_dataproc.yml b/.github/workflows/producer_hive_dataproc.yml index d92cd46b..de25d1be 100644 --- a/.github/workflows/producer_hive_dataproc.yml +++ b/.github/workflows/producer_hive_dataproc.yml @@ -32,19 +32,10 @@ jobs: run: | scenarios=$(./scripts/get_valid_test_scenarios.sh "producer/hive_dataproc/scenarios/" ${{ inputs.component_release }} ${{ inputs.ol_release }} ) [[ "$scenarios" == "" ]] || echo "scenarios=$scenarios" >> $GITHUB_OUTPUT - echo "openlineage_cluster_suffix=$(echo '${{ inputs.ol_release }}' | sed 's/\.//g')" >> $GITHUB_OUTPUT - echo "component_cluster_suffix=$(echo '${{ inputs.component_release }}' | sed 's/\.//g')" >> $GITHUB_OUTPUT - case "${{ inputs.component_release }}" in - "3.1.3") - echo "hive_short=3" >> $GITHUB_OUTPUT - echo "dataproc_version=2.1-debian11" >> $GITHUB_OUTPUT - ;; - *) - echo "hive version ${{ inputs.component_release }} not supported" - exit 1 - ;; - esac - + echo "cluster_name=hive-dataproc-producer-test-$(echo '${{ inputs.component_release }}' | sed 's/\.//g')-$(echo '${{ inputs.ol_release }}' | sed 's/\.//g')-${{ github.run_id }}" >> "$GITHUB_OUTPUT" + # the cluster settings per Hive release are in producer/hive_dataproc/runner/cluster.json + jq -e --arg v '${{ inputs.component_release }}' 'has($v)' producer/hive_dataproc/runner/cluster.json > /dev/null \ + || { echo "hive version ${{ inputs.component_release }} not supported"; exit 1; } - name: GCP authorization id: gcp-auth @@ -77,15 +68,6 @@ jobs: gcs-path: "gs://open-lineage-e2e/jars" credentials: ${{ steps.gcp-auth.outputs.credentials_file_path }} - - name: Upload initialization actions to GCS - id: upload-initialization-actions - if: ${{ steps.init.outputs.scenarios }} - uses: ./.github/actions/upload_artifacts - with: - local-file-path: producer/hive_dataproc/runner/get_hive_openlineage_jars.sh - gcs-path: "gs://open-lineage-e2e/scripts" - credentials: ${{ steps.gcp-auth.outputs.credentials_file_path }} - - name: Set up Python 3.11 if: ${{ steps.init.outputs.scenarios }} uses: actions/setup-python@v5 @@ -106,32 +88,15 @@ jobs: if: ${{ steps.init.outputs.scenarios }} uses: ./.github/actions/dataproc_cluster with: - cluster-name: "hive-dataproc-producer-test-${{steps.init.outputs.component_cluster_suffix}}-${{ steps.init.outputs.openlineage_cluster_suffix }}-${{ github.run_id }}" + cluster-name: ${{ steps.init.outputs.cluster_name }} project-id: gcp-open-lineage-testing region: us-west1 + # the GCS transport: runner/create_cluster.sh's default, a fixed snapshot (released 1.40.0-1.55.0 break Hive) create-command: | - metadata='' - # Temporary: released transports-gcs 1.40.0-1.55.0 break Hive on Dataproc (half-relocated Woodstox stops - # HiveServer2, the bundled Conscrypt native library aborts the Tez AM). This snapshot has both fixed; go back - # to uploading the matching transport (get-ol-artifacts' gcs output) once a release has the fix (1.56.0?). - metadata+='GCS_TRANSPORT_JAR=gs://open-lineage-e2e/jars/transports-gcs-1.56.0-SNAPSHOT-conscrypt-fix.jar' - metadata+=',OPENLINEAGE_HIVE_URL=${{ steps.upload-hive-integration.outputs.uploaded-file }}' - - properties='' - properties+='hive:hive.exec.post.hooks=io.openlineage.hive.hooks.HiveOpenLineageHook' - properties+=',hive:hive.conf.validation=false' - properties+=',hive:hive.server2.logging.operation.level=VERBOSE' - properties+=',hive:hive.root.logger=DEBUG' - properties+=',hive:hive.log.explain.output=true' - - python dev/dataproc/dataproc_workflow.py create-cluster \ - --project-id "gcp-open-lineage-testing" \ - --region "us-west1" \ - --cluster-name "hive-dataproc-producer-test-${{steps.init.outputs.component_cluster_suffix}}-${{ steps.init.outputs.openlineage_cluster_suffix }}-${{ github.run_id }}" \ - --dataproc-image-version ${{ steps.init.outputs.dataproc_version }} \ - --cluster-properties "$properties" \ - --metadata "$metadata" \ - --initialization-actions "${{ steps.upload-initialization-actions.outputs.uploaded-file }}" \ + producer/hive_dataproc/runner/create_cluster.sh \ + --hive-release "${{ inputs.component_release }}" \ + --cluster-name "${{ steps.init.outputs.cluster_name }}" \ + --hive-jar "${{ steps.upload-hive-integration.outputs.uploaded-file }}" \ --credentials-file "${{ steps.gcp-auth.outputs.credentials_file_path }}" - name: Set producer output event dir @@ -143,55 +108,12 @@ jobs: - name: Run producer jobs and create OL events if: ${{ steps.init.outputs.scenarios }} id: run-producer - run: | - set -e - IFS=';' read -ra scenarios <<< "${{ steps.init.outputs.scenarios }}" - - properties='' - properties+='hive.extraListeners=io.openlineage.hive.hooks.HiveOpenLineageHook' - properties+=',hive.sql.warehouse.dir=/tmp/warehouse' - properties+=',hive.openlineage.transport.type=gcs' - properties+=',hive.scenario.suffix=${{steps.init.outputs.component_cluster_suffix}}-${{steps.init.outputs.openlineage_cluster_suffix}}' - - # one entry per scenario, [{scenario, state, job_id}] as written by the spark runner; a failed query fails - # its scenario in validation instead of stopping the other scenarios - status_file="${{ steps.set-producer-output.outputs.event_dir }}/status.json" - mkdir -p "${{ steps.set-producer-output.outputs.event_dir }}" - echo '[]' > "$status_file" - - for scenario in "${scenarios[@]}" - do - echo "Getting script for scenario $scenario" - query_file=$(find "producer/hive_dataproc/scenarios/$scenario/test/" -maxdepth 1 -type f -name "*.sql" | head -n 1) - query=$(cat $query_file) - - echo "Running hive job for scenario: $scenario" - - properties='' - properties+='hive.openlineage.transport.type=gcs' - properties+=",hive.openlineage.transport.bucketName=open-lineage-e2e" - properties+=",hive.openlineage.transport.fileNamePrefix=events/$scenario/$(date +%s%3N)/" - - if python dev/dataproc/dataproc_workflow.py run-hive-query \ - --project-id gcp-open-lineage-testing \ - --region us-west1 \ - --cluster-name "hive-dataproc-producer-test-${{steps.init.outputs.component_cluster_suffix}}-${{ steps.init.outputs.openlineage_cluster_suffix }}-${{ github.run_id }}" \ - --query "$query" \ - --credentials-file "${{ steps.gcp-auth.outputs.credentials_file_path }}" \ - --output-directory "${{ steps.set-producer-output.outputs.event_dir }}/$scenario" \ - --properties "$properties" - then - state=DONE - echo "Finished running hive job for scenario: $scenario" - else - state=FAILED - echo "::warning::hive job failed for scenario $scenario" - fi - jq --arg scenario "$scenario" --arg state "$state" '. + [{scenario: $scenario, state: $state, job_id: ""}]' \ - "$status_file" > "$status_file.tmp" && mv "$status_file.tmp" "$status_file" - done - - echo "Finished running all scenarios" + run: | + producer/hive_dataproc/runner/run_scenarios.sh \ + --cluster-name "${{ steps.init.outputs.cluster_name }}" \ + --scenarios "${{ steps.init.outputs.scenarios }}" \ + --events-dir "${{ steps.set-producer-output.outputs.event_dir }}" \ + --credentials-file "${{ steps.gcp-auth.outputs.credentials_file_path }}" - name: Validation if: ${{ steps.init.outputs.scenarios }} diff --git a/dev/dataproc/dataproc_workflow.py b/dev/dataproc/dataproc_workflow.py index 34c77507..30c19442 100644 --- a/dev/dataproc/dataproc_workflow.py +++ b/dev/dataproc/dataproc_workflow.py @@ -775,6 +775,11 @@ async def run_hive_query_command(args): else: print("Could not determine bucket or prefix for event files. Skipping download.") + # after the download: a failed query can still have emitted events worth looking at + print(f"Hive job {job_id} ended in state {result['state']}") + if result["state"] != "DONE": + raise SystemExit(f"Hive job {job_id} did not finish successfully (state {result['state']})") + def main(): parser = argparse.ArgumentParser( diff --git a/producer/README.md b/producer/README.md index d259b8a3..ffe92863 100644 --- a/producer/README.md +++ b/producer/README.md @@ -68,5 +68,6 @@ Producers with a one-command local run, described step by step in their README ( | Producer | Command | |---|---| | dbt | `producer/dbt/run_dbt_tests.sh --component-version 1.8.0 --openlineage-version 1.55.0` ([playbook](dbt/README.md#run-locally-as-ci-does)) | +| hive_dataproc | `producer/hive_dataproc/run_hive_dataproc_tests.sh --component-version 3.1.3 --openlineage-version 1.55.0` ([playbook](hive_dataproc/README.md#run-locally-as-ci-does)); needs GCP access | A new producer starts from `producer/template/run_template_tests.sh` and the sections in `producer/template/README.md`. diff --git a/producer/hive_dataproc/README.md b/producer/hive_dataproc/README.md index 8f8c32f9..f1a3a1e9 100644 --- a/producer/hive_dataproc/README.md +++ b/producer/hive_dataproc/README.md @@ -1,6 +1,101 @@ -# Spark Dataproc +# Hive Dataproc -Checks the compatibility of the producer by running the spark jobs on dataproc cluster and then running validation script on the output. +Checks the OpenLineage Hive integration (`openlineage-hive`, a Hive post-execution hook) by running each scenario's +`test/*.sql` as a Hive job on a Dataproc cluster and validating the events the hook writes to GCS. -## Running locally +``` +producer/hive_dataproc/ +├── scenarios// config.json, events/ (expected events), test/.sql, scenario.md +├── runner/cluster.json per Hive release: Dataproc image and Hive properties +├── runner/create_cluster.sh creates the cluster with the hook and the GCS transport installed +├── runner/run_scenarios.sh runs the scenarios on a cluster, downloads the events, writes status.json +├── run_hive_dataproc_tests.sh runs everything locally, as CI does +├── versions.json tested Hive and OpenLineage versions +└── dataproc_images.json Dataproc image family per Hive release (the release check watches it for new images) +``` +CI (`.github/workflows/producer_hive_dataproc.yml`) runs on pull requests that touch Hive and in the scheduled release +check. It calls the same `runner/` scripts as the local run. + +**GCS transport:** released `transports-gcs` 1.40.0–1.55.0 break Hive on Dataproc, so for now the cluster gets a fixed +snapshot (`runner/create_cluster.sh`, `DEFAULT_GCS_TRANSPORT_JAR`) instead of the transport of the OpenLineage release +being tested. + +## Run locally, as CI does + +Use this to test a new or changed scenario before you commit it. The cluster, the jobs and the validation are the same +as in CI; only the cluster name (yours, with a timestamp) and the credentials differ. + +**Prerequisites:** see [`dev/README.md`](../../dev/README.md#prerequisites-for-running-producers-locally): Python 3.11+, +`jq`, `git`, `curl`, the gcloud CLI logged in (`gcloud auth login` and `gcloud auth application-default login`) with +access to `gcp-open-lineage-testing`, or your own project and bucket (`--project`, `--region`, `--bucket`). + +**Pick the versions** from `versions.json` (Hive: a key of `runner/cluster.json`; OpenLineage: the latest release, or +the one you're testing). Scenarios only run for the versions in their `config.json`. + +**Cost and time:** a cluster (1 master, 2 workers) is created for the run and deleted at the end, also when a step +fails or you press Ctrl-C. Creating it takes about 3 minutes, each scenario about 1 minute. + +### One command + +From the repository root: + +```bash +producer/hive_dataproc/run_hive_dataproc_tests.sh --component-version 3.1.3 --openlineage-version 1.55.0 +``` + +Options (`--help` lists everything): +- `--scenarios 'aggregate_job'` runs only the given scenarios. +- `--hive-jar path/to/openlineage-hive.jar` tests your own build of the integration instead of the release. +- `--keep-cluster` keeps the cluster for more runs; `--cluster-name ` then runs on it without creating or deleting + one. Delete it when you're done: `dev/dataproc/delete_cluster.sh --cluster-name `. `dev/dataproc/delete_cluster.sh + --list-mine` lists clusters left behind under your name. +- `--events-dir` and `--report-dir` move the output. + +### The same steps by hand + +One command per CI step, so you can stop after any of them, look at the output, or re-run just one: + +```bash +V=3.1.3 OL=1.55.0 EVENTS=$PWD/hive-dataproc-events-$V-$OL CLUSTER=hive-$USER-test + +# "initialize tests": the scenarios valid for the versions +SCENARIOS=$(scripts/get_valid_test_scenarios.sh producer/hive_dataproc/scenarios $V $OL); echo "$SCENARIOS" + +# "Install dependencies" (any virtualenv outside the repo; the scripts call `python`) +python3 -m venv ~/.venvs/dataproc && source ~/.venvs/dataproc/bin/activate +pip install -r dev/dataproc/requirements.txt + +# "Get OL artifacts" and "Upload openlineage hive integration to GCS" +curl -fsSLO https://repo1.maven.org/maven2/io/openlineage/openlineage-hive/$OL/openlineage-hive-$OL.jar +HIVE_JAR=$(dev/dataproc/gcs_upload.sh openlineage-hive-$OL.jar gs://open-lineage-e2e/jars) + +# "Start producer" +producer/hive_dataproc/runner/create_cluster.sh --hive-release $V --cluster-name $CLUSTER --hive-jar "$HIVE_JAR" + +# "Run producer jobs and create OL events" +producer/hive_dataproc/runner/run_scenarios.sh --cluster-name $CLUSTER --scenarios "$SCENARIOS" --events-dir "$EVENTS" + +# "Validation" +dev/validation/validate_locally.sh --component hive_dataproc --component-version $V --openlineage-version $OL --events-dir "$EVENTS" + +# the cluster action's cleanup +dev/dataproc/delete_cluster.sh --cluster-name $CLUSTER +``` + +### What you get + +- `hive-dataproc-events--
    //*.json`: the events the hook wrote (also kept in GCS under + `gs:///events///`). +- `hive-dataproc-events--
      /status.json`: how each scenario's Hive job ended (`DONE`, or e.g. `ERROR` with the + Dataproc job id). A scenario not in `DONE` fails validation with a "producer job" test. To see why, open the job in + the Cloud Console (Dataproc → Jobs) or run `gcloud dataproc jobs describe --region us-west1`. +- `hive-dataproc--
        -report.json`: the report, with the same name and format as the CI artifact. The script prints + a line per scenario with its status and the tests that didn't pass, and exits 1 if a scenario failed. A test that + doesn't match its expected event lists the differences in its `details`: + + ```bash + jq '.[].scenarios[] | {name, status, failed: [.tests[] | select(.status == "FAILURE") | {name, details}]}' hive-dataproc-3.1.3-1.55.0-report.json + ``` + +Both outputs are git-ignored when they're in the repository root. diff --git a/producer/hive_dataproc/run_hive_dataproc_tests.sh b/producer/hive_dataproc/run_hive_dataproc_tests.sh index e69de29b..c393f560 100755 --- a/producer/hive_dataproc/run_hive_dataproc_tests.sh +++ b/producer/hive_dataproc/run_hive_dataproc_tests.sh @@ -0,0 +1,127 @@ +#!/bin/bash + +# Runs the Hive scenarios for one Hive version x OpenLineage version on a Dataproc cluster, the way CI does +# (.github/workflows/producer_hive_dataproc.yml), and validates the events. See "Run locally, as CI does" in README.md. +# +# 1. a virtualenv with dev/dataproc/requirements.txt (cached, reused by later runs) +# 2. the scenarios valid for the versions (scripts/get_valid_test_scenarios.sh), or the ones given with --scenarios +# 3. the openlineage-hive jar from Maven Central, uploaded to gs:///jars (skipped when already there) +# 4. runner/create_cluster.sh: a cluster named after you; deleted at the end, also when a step fails +# 5. runner/run_scenarios.sh: runs the scenarios, downloads the events, writes status.json +# 6. dev/validation/validate_locally.sh: validates the events, writes the report, exits 1 if a scenario failed + +set -euo pipefail + +HIVE_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" +REPO_ROOT="$(cd "$HIVE_DIR/../.." && pwd)" +CACHE_DIR="${OL_COMPAT_CACHE:-${XDG_CACHE_HOME:-$HOME/.cache}/openlineage-compatibility-tests}" +# shellcheck source=dev/defaults.env +source "$REPO_ROOT/dev/defaults.env" + +usage() { + cat >&2 <-
          ) + --report-dir DIR where the report goes (default: .); named hive-dataproc--
            -report.json like the CI artifact + --project PROJECT GCP project (default $GCP_PROJECT) + --region REGION region (default $GCP_REGION) + --bucket BUCKET bucket for jars, the init action and events (default $GCS_BUCKET) + +Credentials: the application default credentials (gcloud auth application-default login) and the gcloud CLI login. +Environment: PYTHON (default python3) creates the virtualenv; OL_COMPAT_CACHE moves the cache +(default ~/.cache/openlineage-compatibility-tests). +USAGE + exit "${1:-1}" +} + +component_version="" ol_version="" scenarios="" hive_jar="" cluster_name="" keep_cluster=false events_dir="" report_dir="." +while [[ $# -gt 0 ]]; do + case "$1" in + --component-version) component_version="$2"; shift 2 ;; + --openlineage-version) ol_version="$2"; shift 2 ;; + --scenarios) scenarios="$2"; shift 2 ;; + --hive-jar) hive_jar="$2"; shift 2 ;; + --cluster-name) cluster_name="$2"; shift 2 ;; + --keep-cluster) keep_cluster=true; shift ;; + --events-dir) events_dir="$2"; shift 2 ;; + --report-dir) report_dir="$2"; shift 2 ;; + --project) GCP_PROJECT="$2"; shift 2 ;; + --region) GCP_REGION="$2"; shift 2 ;; + --bucket) GCS_BUCKET="$2"; shift 2 ;; + -h|--help) usage 0 ;; + *) echo "Unknown option: $1" >&2; usage ;; + esac +done +[[ -n "$component_version" && -n "$ol_version" ]] || usage +events_dir="${events_dir:-$PWD/hive-dataproc-events-$component_version-$ol_version}" +mkdir -p "$report_dir" +report="$(cd "$report_dir" && pwd)/hive-dataproc-$component_version-$ol_version-report.json" +gcp=(--project "$GCP_PROJECT" --region "$GCP_REGION" --bucket "$GCS_BUCKET") + +echo "== 1. Dataproc client" +venv="$CACHE_DIR/venvs/dataproc" +requirements_hash=$(shasum "$REPO_ROOT/dev/dataproc/requirements.txt" | cut -d' ' -f1) +if [[ ! -x "$venv/bin/python" || "$(cat "$venv/.requirements-hash" 2> /dev/null)" != "$requirements_hash" ]]; then + rm -rf "$venv" + "${PYTHON:-python3}" -m venv "$venv" + "$venv/bin/python" -m pip install --quiet --upgrade pip + "$venv/bin/python" -m pip install --quiet -r "$REPO_ROOT/dev/dataproc/requirements.txt" + echo "$requirements_hash" > "$venv/.requirements-hash" +fi +export PATH="$venv/bin:$PATH" # the dev/ and runner/ scripts call `python` +echo "virtualenv: $venv" + +echo "== 2. Scenarios" +if [[ -z "$scenarios" ]]; then + scenarios=$("$REPO_ROOT/scripts/get_valid_test_scenarios.sh" "$HIVE_DIR/scenarios" "$component_version" "$ol_version") + [[ -n "$scenarios" ]] || { echo "No scenario is valid for Hive $component_version and OpenLineage $ol_version" >&2; exit 1; } +fi +echo "$scenarios" + +if [[ -n "$cluster_name" ]]; then + echo "== 3-4. Using the existing cluster $cluster_name" +else + echo "== 3. openlineage-hive jar" + if [[ -z "$hive_jar" ]]; then + # the release: the same file CI uploads, so an upload from an earlier run (or CI) is reused + jar="$CACHE_DIR/jars/openlineage-hive-$ol_version.jar" + if [[ ! -s "$jar" ]]; then + mkdir -p "$(dirname "$jar")" + curl -fsSL -o "$jar.part" \ + "https://repo1.maven.org/maven2/io/openlineage/openlineage-hive/$ol_version/openlineage-hive-$ol_version.jar" + mv "$jar.part" "$jar" + fi + hive_jar=$("$REPO_ROOT/dev/dataproc/gcs_upload.sh" "$jar" "gs://$GCS_BUCKET/jars") + elif [[ "$hive_jar" != gs://* ]]; then + # a local build: a directory per content, so it never reuses (or replaces) another jar with the same name + hive_jar=$("$REPO_ROOT/dev/dataproc/gcs_upload.sh" "$hive_jar" \ + "gs://$GCS_BUCKET/jars/local/$(shasum "$hive_jar" | cut -c1-12)") + fi + echo "$hive_jar" + + echo "== 4. Cluster" + user_slug=$(echo "${USER:-user}" | tr '[:upper:]' '[:lower:]' | tr -c 'a-z0-9\n' '-' | cut -c1-15) + cluster_name="hive-${user_slug}-${component_version//./}-${ol_version//./}-$(date +%s)" + if [[ "$keep_cluster" == "true" ]]; then + echo "The cluster is kept; delete it with: dev/dataproc/delete_cluster.sh --cluster-name $cluster_name" + else + # shellcheck disable=SC2064 # expand now: the names are fixed from here on + trap "'$REPO_ROOT/dev/dataproc/delete_cluster.sh' --cluster-name '$cluster_name' --project '$GCP_PROJECT' --region '$GCP_REGION'" EXIT + fi + "$HIVE_DIR/runner/create_cluster.sh" --hive-release "$component_version" --cluster-name "$cluster_name" \ + --hive-jar "$hive_jar" "${gcp[@]}" +fi + +echo "== 5. Run" +"$HIVE_DIR/runner/run_scenarios.sh" --cluster-name "$cluster_name" --scenarios "$scenarios" --events-dir "$events_dir" "${gcp[@]}" + +echo "== 6. Validate" +"$REPO_ROOT/dev/validation/validate_locally.sh" --component hive_dataproc --component-version "$component_version" \ + --openlineage-version "$ol_version" --events-dir "$events_dir" --report "$report" diff --git a/producer/hive_dataproc/runner/cluster.json b/producer/hive_dataproc/runner/cluster.json new file mode 100644 index 00000000..861c8dd5 --- /dev/null +++ b/producer/hive_dataproc/runner/cluster.json @@ -0,0 +1,12 @@ +{ + "3.1.3": { + "image": "2.1-debian11", + "properties": { + "hive:hive.exec.post.hooks": "io.openlineage.hive.hooks.HiveOpenLineageHook", + "hive:hive.conf.validation": "false", + "hive:hive.server2.logging.operation.level": "VERBOSE", + "hive:hive.root.logger": "DEBUG", + "hive:hive.log.explain.output": "true" + } + } +} diff --git a/producer/hive_dataproc/runner/create_cluster.sh b/producer/hive_dataproc/runner/create_cluster.sh new file mode 100755 index 00000000..96c3ab4f --- /dev/null +++ b/producer/hive_dataproc/runner/create_cluster.sh @@ -0,0 +1,66 @@ +#!/bin/bash + +# Creates the Dataproc cluster the Hive scenarios run on, for one Hive release: the image and Hive properties from +# runner/cluster.json, the OpenLineage hive jar and the GCS transport jar installed by dev/dataproc/install_jars.sh. +# Used by CI (producer_hive_dataproc.yml) and by run_hive_dataproc_tests.sh. + +set -euo pipefail + +HIVE_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)" +REPO_ROOT="$(cd "$HIVE_DIR/../.." && pwd)" +# shellcheck source=dev/defaults.env +source "$REPO_ROOT/dev/defaults.env" + +# Temporary: released transports-gcs 1.40.0-1.55.0 break Hive on Dataproc (half-relocated Woodstox stops HiveServer2, +# the bundled Conscrypt native library aborts the Tez AM). This snapshot has both fixed. Once a release has the fix +# (1.56.0?), drop the default: CI passes the matching transport (get-ol-artifacts' gcs output) and +# run_hive_dataproc_tests.sh downloads it from Maven like the hive jar. +DEFAULT_GCS_TRANSPORT_JAR="gs://open-lineage-e2e/jars/transports-gcs-1.56.0-SNAPSHOT-conscrypt-fix.jar" + +usage() { + cat >&2 <&2; usage ;; + esac +done +[[ -n "$hive_release" && -n "$cluster_name" && -n "$hive_jar" ]] || usage + +config=$(jq -e --arg v "$hive_release" '.[$v]' "$HIVE_DIR/runner/cluster.json") \ + || { echo "Hive $hive_release is not in $HIVE_DIR/runner/cluster.json" >&2; exit 1; } +image=$(jq -r '.image' <<< "$config") +properties=$(jq -r '.properties | to_entries | map("\(.key)=\(.value)") | join(",")' <<< "$config") + +# a copy per cluster: concurrent runs don't overwrite each other's init action +init_action=$("$REPO_ROOT/dev/dataproc/gcs_upload.sh" --force "$REPO_ROOT/dev/dataproc/install_jars.sh" \ + "gs://$GCS_BUCKET/scripts/$cluster_name") + +args=(--cluster-name "$cluster_name" --image-version "$image" --properties "$properties" + --metadata "JARS=$hive_jar;$gcs_transport_jar" --init-actions "$init_action" + --project "$GCP_PROJECT" --region "$GCP_REGION") +[[ -n "$credentials_file" ]] && args+=(--credentials-file "$credentials_file") +"$REPO_ROOT/dev/dataproc/create_cluster.sh" "${args[@]}" diff --git a/producer/hive_dataproc/runner/get_hive_openlineage_jars.sh b/producer/hive_dataproc/runner/get_hive_openlineage_jars.sh deleted file mode 100755 index 0e753785..00000000 --- a/producer/hive_dataproc/runner/get_hive_openlineage_jars.sh +++ /dev/null @@ -1,32 +0,0 @@ -#!/bin/bash - -# Installs OpenLineage jar onto a Cloud Dataproc cluster. - -set -euxo pipefail - -readonly VM_HADOOP_LIB_DIR=/usr/lib/hadoop/lib -readonly VM_DATAPROC_VM_HADOOP_LIB_DIR_DIR=/usr/local/share/google/dataproc/lib -readonly OPENLINEAGE_HIVE_URL=$(/usr/share/google/get_metadata_value attributes/OPENLINEAGE_HIVE_URL || echo "") -# the workflow sets GCS_TRANSPORT_JAR; GCS_TRANSPORT_JAR_URL is the old name, still read as a fallback -readonly GCS_TRANSPORT_JAR_URL=$(/usr/share/google/get_metadata_value attributes/GCS_TRANSPORT_JAR \ - || /usr/share/google/get_metadata_value attributes/GCS_TRANSPORT_JAR_URL || echo "") - -if [[ -d ${VM_DATAPROC_VM_HADOOP_LIB_DIR_DIR} ]]; then - vm_lib_dir=${VM_DATAPROC_VM_HADOOP_LIB_DIR_DIR} -else - vm_lib_dir=${VM_HADOOP_LIB_DIR} -fi - -copy_jar_or_default() { - local path="$1" - local default_path="$2" - if [[ -n "${path}" ]]; then - gsutil cp -P "${path}" "${vm_lib_dir}/" - else - gsutil cp -P "${default_path}" "${vm_lib_dir}/" - fi -} - -copy_jar_or_default "${OPENLINEAGE_HIVE_URL}" "gs://open-lineage-e2e/jars/openlineage-hive-1.34.0.jar " -copy_jar_or_default "${GCS_TRANSPORT_JAR_URL}" "gs://open-lineage-e2e/jars/transports-gcs-1.34.0.jar" - diff --git a/producer/hive_dataproc/runner/run_scenarios.sh b/producer/hive_dataproc/runner/run_scenarios.sh new file mode 100755 index 00000000..4d026474 --- /dev/null +++ b/producer/hive_dataproc/runner/run_scenarios.sh @@ -0,0 +1,97 @@ +#!/bin/bash + +# Runs Hive scenarios on an existing cluster (runner/create_cluster.sh) and records how each one ended in +# /status.json. Each scenario's test/*.sql runs as one Dataproc Hive job; the hook writes the events to GCS +# and they are downloaded to //. Used by CI and by run_hive_dataproc_tests.sh. +# +# A failed scenario doesn't stop the others and doesn't fail this script: it's recorded as FAILED, and validation then +# fails that scenario. + +set -euo pipefail + +REPO_ROOT="$(cd "$(dirname "${BASH_SOURCE[0]}")/../../.." && pwd)" +SCENARIOS_DIR="${HIVE_SCENARIOS_DIR:-$REPO_ROOT/producer/hive_dataproc/scenarios}" +# shellcheck source=dev/defaults.env +source "$REPO_ROOT/dev/defaults.env" +# shellcheck source=dev/lib/status.sh +source "$REPO_ROOT/dev/lib/status.sh" + +usage() { + cat >&2 <//, the run status to /status.json + --scenarios LIST scenarios to run, separated by ';' (see scripts/get_valid_test_scenarios.sh) + --project PROJECT GCP project (default $GCP_PROJECT) + --region REGION region (default $GCP_REGION) + --bucket BUCKET bucket the events are written to (default $GCS_BUCKET) + --credentials-file PATH service account key (default: application default credentials) +USAGE + exit "${1:-1}" +} + +warn() { + if [[ -n "${GITHUB_ACTIONS:-}" ]]; then echo "::warning::$1"; else echo "WARNING: $1" >&2; fi +} + +cluster_name="" events_dir="" scenarios="" credentials_file="" +while [[ $# -gt 0 ]]; do + case "$1" in + --cluster-name) cluster_name="$2"; shift 2 ;; + --events-dir) events_dir="$2"; shift 2 ;; + --scenarios) scenarios="$2"; shift 2 ;; + --project) GCP_PROJECT="$2"; shift 2 ;; + --region) GCP_REGION="$2"; shift 2 ;; + --bucket) GCS_BUCKET="$2"; shift 2 ;; + --credentials-file) credentials_file="$2"; shift 2 ;; + -h|--help) usage 0 ;; + *) echo "Unknown option: $1" >&2; usage ;; + esac +done +[[ -n "$cluster_name" && -n "$events_dir" && -n "$scenarios" ]] || usage + +mkdir -p "$events_dir" +events_dir="$(cd "$events_dir" && pwd)" +status_file="$events_dir/status.json" +status_init "$status_file" + +IFS=';' read -ra scenario_list <<< "$scenarios" +for scenario in "${scenario_list[@]}"; do + echo "Running Hive scenario: $scenario" + out="$events_dir/$scenario" + mkdir -p "$out" + rm -f "$out"/*.json # from an earlier local run into the same directory + + query_file=$(find "$SCENARIOS_DIR/$scenario/test/" -maxdepth 1 -type f -name "*.sql" 2> /dev/null | sort | head -n 1) + if [[ -z "$query_file" ]]; then + record_status "$status_file" "$scenario" FAILED + warn "no test/*.sql for Hive scenario $scenario" + continue + fi + + # a new GCS prefix per run, so only this run's events are downloaded + prefix="events/$scenario/$(python3 -c 'import time; print(int(time.time() * 1000))')/" + properties="hive.openlineage.transport.type=gcs" + properties+=",hive.openlineage.transport.bucketName=$GCS_BUCKET" + properties+=",hive.openlineage.transport.fileNamePrefix=$prefix" + + args=(--project-id "$GCP_PROJECT" --region "$GCP_REGION" --cluster-name "$cluster_name" --query "$(cat "$query_file")" + --output-directory "$out" --properties "$properties") + [[ -n "$credentials_file" ]] && args+=(--credentials-file "$credentials_file") + log=$(mktemp) + if python "$REPO_ROOT/dev/dataproc/dataproc_workflow.py" run-hive-query "${args[@]}" 2>&1 | tee "$log"; then + state=DONE + else + # the job's final state when the job ran (e.g. ERROR), FAILED when it couldn't be submitted or followed + state=$(sed -n 's/^Hive job .* ended in state \([A-Z_]*\)$/\1/p' "$log" | tail -n 1) + [[ -z "$state" || "$state" == DONE ]] && state=FAILED + warn "Hive job for scenario $scenario ended in state $state" + fi + job_id=$(sed -n 's/^Submitted Hive job with job_id: //p' "$log" | tail -n 1) + record_status "$status_file" "$scenario" "$state" "$job_id" + rm -f "$log" + echo "Finished scenario $scenario: $state" +done + +echo "Run status: $status_file" diff --git a/scripts/tests/test_hive_runner.py b/scripts/tests/test_hive_runner.py new file mode 100644 index 00000000..4057aa5e --- /dev/null +++ b/scripts/tests/test_hive_runner.py @@ -0,0 +1,178 @@ +"""The Hive runner scripts (producer/hive_dataproc/runner/) with stub `python` and `gcloud`, and run-hive-query's exit code.""" +import asyncio +import importlib.util +import json +import os +import re +import subprocess +from types import SimpleNamespace + +import pytest + +from conftest import REPO_ROOT + +RUNNER = REPO_ROOT / "producer" / "hive_dataproc" / "runner" +SNAPSHOT_TRANSPORT = "gs://open-lineage-e2e/jars/transports-gcs-1.56.0-SNAPSHOT-conscrypt-fix.jar" + + +@pytest.fixture +def stubs(tmp_path): + """Stub `python` (dataproc_workflow.py) and `gcloud` on PATH; returns (run, calls) helpers.""" + bin_dir = tmp_path / "bin" + bin_dir.mkdir() + calls = tmp_path / "calls.jsonl" + record = f"""python3 -c 'import json,sys; print(json.dumps(sys.argv[1:]))' "$(basename "$0")" "$@" >> {calls}""" + # python: a query containing "-- fail" ends in ERROR, one containing "-- nosubmit" fails before submitting + (bin_dir / "python").write_text(f"""#!/bin/bash +{record} +case "$*" in + *"-- nosubmit"*) echo "permission denied" >&2; exit 1 ;; + *"-- fail"*) echo "Submitted Hive job with job_id: job-fail"; echo "Hive job job-fail ended in state ERROR"; exit 1 ;; + *run-hive-query*) echo "Submitted Hive job with job_id: job-ok"; echo "Hive job job-ok ended in state DONE" ;; +esac +exit 0 +""") + # gcloud: nothing exists in GCS yet, copies succeed + (bin_dir / "gcloud").write_text(f"""#!/bin/bash +{record} +[[ "$1 $2" == "storage ls" ]] && exit 1 +exit 0 +""") + for stub in bin_dir.iterdir(): + stub.chmod(0o755) + + def run(script, *args, env=None): + full_env = {**os.environ, "PATH": f"{bin_dir}:{os.environ['PATH']}", **(env or {})} + full_env.pop("GITHUB_ACTIONS", None) + return subprocess.run(["bash", str(RUNNER / script), *args], env=full_env, capture_output=True, text=True) + + def recorded(): + return [json.loads(line) for line in calls.read_text().splitlines()] if calls.exists() else [] + + return run, recorded + + +def option(argv, name): + return argv[argv.index(name) + 1] + + +def test_create_cluster_uses_cluster_json_and_a_per_cluster_init_action(stubs): + run, recorded = stubs + result = run("create_cluster.sh", "--hive-release", "3.1.3", "--cluster-name", "c1", "--hive-jar", "gs://b/hive.jar") + assert result.returncode == 0, result.stderr + + copies = [c for c in recorded() if c[:3] == ["gcloud", "storage", "cp"]] + assert copies == [["gcloud", "storage", "cp", str(REPO_ROOT / "dev/dataproc/install_jars.sh"), + "gs://open-lineage-e2e/scripts/c1/install_jars.sh"]] + [create] = [c for c in recorded() if c[0] == "python"] + assert create[2] == "create-cluster" + assert option(create, "--cluster-name") == "c1" + assert option(create, "--dataproc-image-version") == "2.1-debian11" + assert option(create, "--metadata") == f"JARS=gs://b/hive.jar;{SNAPSHOT_TRANSPORT}" + assert option(create, "--initialization-actions") == "gs://open-lineage-e2e/scripts/c1/install_jars.sh" + assert option(create, "--cluster-properties").split(",") == [ + "hive:hive.exec.post.hooks=io.openlineage.hive.hooks.HiveOpenLineageHook", + "hive:hive.conf.validation=false", + "hive:hive.server2.logging.operation.level=VERBOSE", + "hive:hive.root.logger=DEBUG", + "hive:hive.log.explain.output=true", + ] + assert (option(create, "--project-id"), option(create, "--region")) == ("gcp-open-lineage-testing", "us-west1") + + +def test_create_cluster_takes_another_transport_and_project(stubs): + run, recorded = stubs + result = run("create_cluster.sh", "--hive-release", "3.1.3", "--cluster-name", "c1", "--hive-jar", "gs://b/hive.jar", + "--gcs-transport-jar", "gs://b/gcs.jar", "--project", "p", "--region", "r", "--bucket", "mine", + "--credentials-file", "k.json") + assert result.returncode == 0, result.stderr + [create] = [c for c in recorded() if c[0] == "python"] + assert option(create, "--metadata") == "JARS=gs://b/hive.jar;gs://b/gcs.jar" + assert option(create, "--initialization-actions") == "gs://mine/scripts/c1/install_jars.sh" + assert (option(create, "--project-id"), option(create, "--region")) == ("p", "r") + assert option(create, "--credentials-file") == "k.json" + + +def test_create_cluster_rejects_an_unknown_hive_release(stubs): + run, recorded = stubs + result = run("create_cluster.sh", "--hive-release", "9.9.9", "--cluster-name", "c1", "--hive-jar", "gs://b/hive.jar") + assert result.returncode != 0 + assert "9.9.9" in result.stderr + assert recorded() == [] + + +@pytest.fixture +def scenarios_dir(tmp_path): + root = tmp_path / "scenarios" + for name, query in [("ok", "SELECT 1; -- ok"), ("broken", "SELECT 1; -- fail"), ("denied", "SELECT 1; -- nosubmit")]: + (root / name / "test").mkdir(parents=True) + (root / name / "test" / "test.sql").write_text(query) + (root / "no_query" / "test").mkdir(parents=True) + return root + + +def test_run_scenarios_records_each_job_and_continues_past_failures(stubs, scenarios_dir, tmp_path): + run, recorded = stubs + events = tmp_path / "events" + (events / "ok").mkdir(parents=True) + (events / "ok" / "stale.json").write_text("{}") # from an earlier run into the same directory + + result = run("run_scenarios.sh", "--cluster-name", "c1", "--events-dir", str(events), + "--scenarios", "ok;broken;denied;no_query", env={"HIVE_SCENARIOS_DIR": str(scenarios_dir)}) + + assert result.returncode == 0, result.stderr + assert json.loads((events / "status.json").read_text()) == [ + {"scenario": "ok", "state": "DONE", "job_id": "job-ok"}, + {"scenario": "broken", "state": "ERROR", "job_id": "job-fail"}, + {"scenario": "denied", "state": "FAILED", "job_id": ""}, + {"scenario": "no_query", "state": "FAILED", "job_id": ""}, + ] + assert not (events / "ok" / "stale.json").exists() + assert sorted(p.name for p in events.iterdir()) == ["broken", "denied", "no_query", "ok", "status.json"] + + jobs = [c for c in recorded() if c[0] == "python"] + assert [option(j, "--query") for j in jobs] == ["SELECT 1; -- ok", "SELECT 1; -- fail", "SELECT 1; -- nosubmit"] + ok = jobs[0] + assert option(ok, "--cluster-name") == "c1" + assert option(ok, "--output-directory") == str(events / "ok") + props = option(ok, "--properties").split(",") + assert props[:2] == ["hive.openlineage.transport.type=gcs", "hive.openlineage.transport.bucketName=open-lineage-e2e"] + assert re.fullmatch(r"hive\.openlineage\.transport\.fileNamePrefix=events/ok/\d{13}/", props[2]) + + +def load_workflow(): + spec = importlib.util.spec_from_file_location("dataproc_workflow", REPO_ROOT / "dev" / "dataproc" / "dataproc_workflow.py") + module = importlib.util.module_from_spec(spec) + spec.loader.exec_module(module) + return module + + +@pytest.mark.parametrize("state, fails", [("DONE", False), ("ERROR", True), ("CANCELLED", True), ("RUNNING", True)]) +def test_run_hive_query_exits_non_zero_unless_the_job_is_done(monkeypatch, capsys, state, fails): + module = load_workflow() + + class Client: + def __init__(self, **kwargs): + pass + + async def submit_job_as_operation(self, request): + class Operation: + async def result(self): + return SimpleNamespace(reference=SimpleNamespace(job_id="job-1")) + return Operation() + + async def wait(**kwargs): + return {"job_id": "job-1", "state": state} + + monkeypatch.setattr(module, "get_gcp_credentials", lambda credentials_file=None: None) + monkeypatch.setattr(module, "JobControllerAsyncClient", Client) + monkeypatch.setattr(module, "wait_for_dataproc_job", wait) + args = SimpleNamespace(credentials_file=None, region="r", project_id="p", cluster_name="c", query="SELECT 1", + properties="", output_directory="") + + if fails: + with pytest.raises(SystemExit): + asyncio.run(module.run_hive_query_command(args)) + else: + asyncio.run(module.run_hive_query_command(args)) + assert f"Hive job job-1 ended in state {state}" in capsys.readouterr().out diff --git a/scripts/tests/test_run_scripts.py b/scripts/tests/test_run_scripts.py index aac36bfa..9923f143 100644 --- a/scripts/tests/test_run_scripts.py +++ b/scripts/tests/test_run_scripts.py @@ -13,6 +13,9 @@ "dev/validation/validate_locally.sh", "producer/dbt/run_dbt_tests.sh", "producer/dbt/runner/run_scenarios.sh", + "producer/hive_dataproc/run_hive_dataproc_tests.sh", + "producer/hive_dataproc/runner/create_cluster.sh", + "producer/hive_dataproc/runner/run_scenarios.sh", "producer/template/run_template_tests.sh", ]