Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
86 changes: 86 additions & 0 deletions .github/workflows/cloud-cleanup.yml
Original file line number Diff line number Diff line change
@@ -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_<run id>_<random>_<epoch ms>, and
# tests derive more from that name (<name>_db1, ...). Local runs against Cloud name theirs
# clickhouse_java_<hex>_test_<epoch ms> (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"
14 changes: 3 additions & 11 deletions .github/workflows/cloud.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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;
Loading