From cf9273271ff08867b8f1d627d8ba0effd9d50a2d Mon Sep 17 00:00:00 2001 From: monosoul Date: Thu, 3 Dec 2026 14:17:17 +0100 Subject: [PATCH] ci: drop old Cloud test databases in a scheduled workflow clickhouse-java test databases pile up on the Cloud service shared with the other clients: the per-job DROP covers only TEST_DB_NAME, and it waits 180 s and fails when a replica lags, which pushes the jobs past their timeout. - cloud-cleanup.yml: hourly (and on demand, with a dry run), drops the clickhouse_java_* databases older than 60 minutes, one at a time and without waiting for every replica. Same approach as clickhouse-js (ClickHouse/clickhouse-js#1076). - cloud.yml: add the epoch-ms creation time to the database name, and remove the per-job DROP. Refs #3508 Co-Authored-By: Claude Opus 5.5 --- .github/workflows/cloud-cleanup.yml | 86 +++++++++++++++++++++++++++++ .github/workflows/cloud.yml | 14 +---- 2 files changed, 89 insertions(+), 11 deletions(-) create mode 100644 .github/workflows/cloud-cleanup.yml diff --git a/.github/workflows/cloud-cleanup.yml b/.github/workflows/cloud-cleanup.yml new file mode 100644 index 000000000..603c99dc0 --- /dev/null +++ b/.github/workflows/cloud-cleanup.yml @@ -0,0 +1,86 @@ +name: Cloud Cleanup + +# Drops the test databases that clickhouse-java leaves on the ClickHouse Cloud service used by +# cloud.yml. The service is shared with other client repositories and limits the number of +# databases. The test jobs don't drop their databases: a DROP there waits for every replica and +# could take minutes off the job's time. Same approach as clickhouse-js (ClickHouse/clickhouse-js#1076). +on: + schedule: + # Not at minute 0, when GitHub delays or drops the most scheduled runs, and not at :17, + # when clickhouse-js cleans up the same service. + - cron: "41 * * * *" + workflow_dispatch: + inputs: + dry_run: + description: "Only list the databases that would be dropped" + type: boolean + default: false + +permissions: {} + +# One cleanup at a time. A newer run waits instead of cancelling a long cleanup. +concurrency: + group: ${{ github.workflow }} + cancel-in-progress: false + +jobs: + cleanup: + name: Drop old test databases + runs-on: ubuntu-latest + # Ends a stuck run before the next hourly one. Dropped databases stay dropped, and the + # next run continues with the rest. + timeout-minutes: 45 + env: + CLICKHOUSE_HOST: ${{ secrets.INTEGRATIONS_TEAM_TESTS_CLOUD_HOST_SMT_PROD }} + CLICKHOUSE_USER: default + CLICKHOUSE_PASSWORD: ${{ secrets.INTEGRATIONS_TEAM_TESTS_CLOUD_PASSWORD_SMT_PROD }} + # Longer than a cloud.yml job can run (timeout-minutes: 20), so a running job keeps its databases. + TTL_MINUTES: 60 + DRY_RUN: ${{ inputs.dry_run || false }} + steps: + - name: Drop test databases older than the TTL + run: | + query() { + curl --fail-with-body -sS -u "$CLICKHOUSE_USER:$CLICKHOUSE_PASSWORD" \ + "https://$CLICKHOUSE_HOST:8443/" --data-binary "$1" + } + + # cloud.yml names its databases clickhouse_java_test___, and + # tests derive more from that name (_db1, ...). Local runs against Cloud name theirs + # clickhouse_java__test_ (ClickHouseTestEnvironment). + list=$(query "SELECT name FROM system.databases WHERE startsWith(name, 'clickhouse_java_') ORDER BY name FORMAT TSVRaw") + databases=() + if [ -n "$list" ]; then + mapfile -t databases <<< "$list" + fi + echo "Found ${#databases[@]} database(s) starting with clickhouse_java_" + + now_ms=$(date +%s%3N) + ttl_ms=$((TTL_MINUTES * 60 * 1000)) + skipped=0 + dropped=0 + failed=0 + for name in "${databases[@]}"; do + # The first 13-digit token is the creation time in epoch ms. Run IDs and $RANDOM are + # shorter. Names without one predate the timestamp and are dropped. + if [[ "$name" =~ _(1[0-9]{12})(_|$) ]] && (( now_ms - BASH_REMATCH[1] < ttl_ms )); then + echo "Skipping $name: created less than $TTL_MINUTES minutes ago" + skipped=$((skipped + 1)) + continue + fi + if [ "$DRY_RUN" = "true" ]; then + echo "Would drop $name" + continue + fi + # distributed_ddl_task_timeout=0: don't wait for every replica. A drop that does not + # complete leaves the database for the next run. + if out=$(query "DROP DATABASE IF EXISTS \`${name//\`/\`\`}\` SETTINGS distributed_ddl_task_timeout=0" 2>&1); then + echo "Dropped $name" + dropped=$((dropped + 1)) + else + echo "::warning::Failed to drop $name: $out" + failed=$((failed + 1)) + fi + done + + echo "Found: ${#databases[@]}, skipped (TTL not expired): $skipped, dropped: $dropped, failed: $failed, dry run: $DRY_RUN" diff --git a/.github/workflows/cloud.yml b/.github/workflows/cloud.yml index c6208d7a1..1bb8e2c43 100644 --- a/.github/workflows/cloud.yml +++ b/.github/workflows/cloud.yml @@ -67,7 +67,9 @@ jobs: run: mvn --also-make --batch-mode --no-transfer-progress -DskipTests=true -Dmaven.javadoc.skip=true install - name: Generate Database Name run: | - RANDOM_DB="clickhouse_java_test_${GITHUB_RUN_ID}_${RANDOM}" + # cloud-cleanup.yml drops this database, and the ones tests derive from its name, + # once the epoch-ms timestamp in the name is older than its TTL + RANDOM_DB="clickhouse_java_test_${GITHUB_RUN_ID}_${RANDOM}_$(date +%s%3N)" echo "TEST_DB_NAME=$RANDOM_DB" >> $GITHUB_ENV echo "Generated database name: $RANDOM_DB" - name: Create Temporary Database @@ -109,13 +111,3 @@ jobs: mvn --batch-mode --no-transfer-progress --projects ${{ matrix.project }} -Dprotocol=http -Dmaven.javadoc.skip=true \ -Dtestng.verbose=5 -Dmaven.test.redirectTestOutputToFile=false \ verify - - name: Cleanup Database Unconditionally - if: always() - run: | - if [ -n "$TEST_DB_NAME" ]; then - DROP_STMT="DROP DATABASE IF EXISTS \`$TEST_DB_NAME\`" - echo "Cleaning up database: \`$TEST_DB_NAME\` with statement '$DROP_STMT'"; - curl --fail-with-body -s -u "$TEST_CLICKHOUSE_USER:$TEST_CLICKHOUSE_PASSWORD" "https://$TEST_CLICKHOUSE_HOST:8443/?wait_end_of_query=1" --data-binary "$DROP_STMT"; - else - echo "No database name was generated; skipping cleanup."; - fi;