-
Notifications
You must be signed in to change notification settings - Fork 65
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(clickhouse-auto-import): add script + service for continuous Cli…
…ckHouse import PE-7137 Adds a clickhouse-auto-import script that loops forever exporting Parquet from the SQLite DB, importing it into ClickHouse, moving it to another directory for potential archival, and pruning old data from SQLite. When using Docker Compose, the script is run automatically in a separate service when the 'clickhouse' profile is in use. Currently the script is written in bash. In the future we may integrate it into the core service TypeScript codebase, but having a standalone script initially allows for faster iteration.
- Loading branch information
Showing
5 changed files
with
154 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
FROM debian:bullseye-slim | ||
|
||
# Install required packages and ClickHouse | ||
RUN apt-get update && apt-get install -y \ | ||
curl \ | ||
jq \ | ||
&& rm -rf /var/lib/apt/lists/* \ | ||
&& curl https://clickhouse.com/ | sh \ | ||
&& mv clickhouse /usr/local/bin/ | ||
|
||
# Create necessary directories | ||
WORKDIR /app | ||
RUN mkdir -p data/parquet/imported | ||
|
||
# Copy the auto-import script | ||
COPY scripts/clickhouse-auto-import /app/scripts/ | ||
COPY scripts/clickhouse-import /app/scripts/ | ||
|
||
# Make scripts executable | ||
RUN chmod +x /app/scripts/clickhouse-auto-import /app/scripts/clickhouse-import | ||
|
||
# Environment variables | ||
ENV ADMIN_API_KEY="" | ||
|
||
# Run the auto-import script | ||
CMD ["/app/scripts/clickhouse-auto-import"] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
#!/usr/bin/env bash | ||
|
||
set -euo pipefail | ||
|
||
# Load environment variables from .env if it exists | ||
if [ -f .env ]; then | ||
set -a | ||
source .env | ||
set +a | ||
fi | ||
|
||
# Set local variables with defaults | ||
ar_io_host=${AR_IO_HOST:-localhost} | ||
ar_io_port=${AR_IO_PORT:-4000} | ||
parquet_dir=${PARQUET_DATA_PATH:-./data/parquet} | ||
sleep_interval=${CLICKHOUSE_AUTO_IMPORT_SLEEP_INTERVAL:-60} # Export every hour by default | ||
|
||
if [ -z "${ADMIN_API_KEY:-}" ]; then | ||
echo "Error: ADMIN_API_KEY environment variable is not set in .env" | ||
exit 1 | ||
fi | ||
|
||
imported_dir="$parquet_dir/imported" | ||
height_interval=10000 | ||
max_rows_per_file=1000000 | ||
|
||
mkdir -p "$parquet_dir" "$imported_dir" | ||
|
||
while true; do | ||
# Get stable height range from admin debug endpoint | ||
debug_info=$(curl -s -H "Authorization: Bearer $ADMIN_API_KEY" "http://${ar_io_host}:${ar_io_port}/ar-io/admin/debug") | ||
min_height=$(echo "$debug_info" | jq -r '.db.heights.minStableDataItem') | ||
max_height=$(echo "$debug_info" | jq -r '.db.heights.maxStableDataItem') | ||
max_indexed_at=$(echo "$debug_info" | jq -r '.db.timestamps.maxStableDataItemIndexedAt') | ||
|
||
# Align to inverals of 10,000 | ||
current_height=$(((min_height / height_interval) * height_interval)) | ||
|
||
while [ "$current_height" -le "$max_height" ]; do | ||
end_height=$((current_height + height_interval)) | ||
|
||
echo "Processing heights $current_height to $end_height..." | ||
|
||
# Export to Parquet files using API | ||
curl -X POST "http://${ar_io_host}:${ar_io_port}/ar-io/admin/export-parquet" \ | ||
-H "Authorization: Bearer $ADMIN_API_KEY" \ | ||
-H "Content-Type: application/json" \ | ||
-d "{ | ||
\"outputDir\": \"$parquet_dir\", | ||
\"startHeight\": $current_height, | ||
\"endHeight\": $end_height, | ||
\"maxFileRows\": $max_rows_per_file | ||
}" | ||
|
||
# Wait for the export to complete | ||
while true; do | ||
if ! status=$(curl -s -f -H "Authorization: Bearer $ADMIN_API_KEY" "http://${ar_io_host}:${ar_io_port}/ar-io/admin/export-parquet/status"); then | ||
echo "Failed to get export status" | ||
rm -f "$parquet_dir"/*.parquet | ||
exit 1 | ||
fi | ||
|
||
export_status=$(echo "$status" | jq -r '.status') | ||
if [ "$export_status" = "completed" ]; then | ||
break | ||
elif [ "$export_status" = "errored" ]; then | ||
error=$(echo "$status" | jq -r '.error') | ||
echo "Export failed: $error" | ||
rm -f "$parquet_dir"/*.parquet | ||
exit 1 | ||
fi | ||
|
||
echo "Waiting for export to complete..." | ||
sleep 10 | ||
done | ||
|
||
# Import Parquet files | ||
./scripts/clickhouse-import | ||
|
||
# Move processed files to imported directory | ||
mv "$parquet_dir"/*.parquet "$imported_dir/" | ||
|
||
# Prune stable data items | ||
curl -X POST "http://${ar_io_host}:${ar_io_port}/ar-io/admin/prune-stable-data-items" \ | ||
-H "Authorization: Bearer $ADMIN_API_KEY" \ | ||
-H "Content-Type: application/json" \ | ||
-d "{\"indexedAtThreshold\": $max_indexed_at}" | ||
|
||
current_height=$end_height | ||
done | ||
|
||
echo "Sleeping for $sleep_interval seconds..." | ||
sleep "$sleep_interval" | ||
done |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters