Skip to content

Commit

Permalink
Use flock to avoid lock file stalling
Browse files Browse the repository at this point in the history
  • Loading branch information
GitGab19 committed Sep 30, 2024
1 parent 1d731ef commit 4cc2cce
Showing 1 changed file with 36 additions and 50 deletions.
86 changes: 36 additions & 50 deletions containers-scripts/update-mainnet-chainstate.sh
Original file line number Diff line number Diff line change
Expand Up @@ -11,20 +11,26 @@ fi

# Shared volume path for snapshot storage
SNAPSHOT_DIR="/shared_volume"

# Paths for chainstate directory in each container
CHAINSTATE_DIR="/root/.bitcoin/chainstate"

# Paths for the timestamp file that marks when the snapshot was last downloaded
TIMESTAMP_FILE="$SNAPSHOT_DIR/last_download_timestamp"

# Backup URL and interval settings
BACKUP_BASE_URL="http://75.119.150.111/backup"
DOWNLOAD_INTERVAL_DAYS=1 # 1 day = 24 hours

# Maximum retries for downloading
MAX_RETRIES=30
RETRY_INTERVAL=60 # 1 minute retry interval
# Lock settings
LOCK_FILE="$SNAPSHOT_DIR/download.lock"

# Function to clean up the lock file
cleanup() {
rm -f "$LOCK_FILE"
}

# Trap to ensure the lock file is removed on exit
trap cleanup EXIT

# Check if the local chainstate directory is updated for this container
if [ -d "$CHAINSTATE_DIR" ]; then
Expand All @@ -46,23 +52,11 @@ CURRENT_UTC=$(date -u +"%Y-%m-%d_%H-UTC")
BACKUP_FILE_NAME="backup_mainnet_blocks_chainstate_$CURRENT_UTC.tar.gz"
BACKUP_URL="$BACKUP_BASE_URL/$BACKUP_FILE_NAME"
BACKUP_HASH_URL="$BACKUP_URL.sha256"

BACKUP_FILE="$SNAPSHOT_DIR/$BACKUP_FILE_NAME"
BACKUP_HASH_FILE="$SNAPSHOT_DIR/$BACKUP_FILE_NAME.sha256"

# Create a lock file to prevent concurrent downloads
LOCK_FILE="$SNAPSHOT_DIR/download.lock"

# Function to clean up the lock file
cleanup() {
rm -rf "$LOCK_FILE"
}

# Trap to ensure the lock file is removed on exit
trap cleanup EXIT

# Check if there is an existing backup file in the shared volume
LATEST_BACKUP_FILE=$(find /shared_volume -maxdepth 1 -name "backup_mainnet_blocks_chainstate_*.tar.gz" -type f -printf "%T@ %p\n" | sort -n | tail -1 | cut -d' ' -f2)
LATEST_BACKUP_FILE=$(find "$SNAPSHOT_DIR" -maxdepth 1 -name "backup_mainnet_blocks_chainstate_*.tar.gz" -type f -printf "%T@ %p\n" | sort -n | tail -1 | cut -d' ' -f2)

if [ -f "$TIMESTAMP_FILE" ]; then
TIMESTAMP_MOD_TIME=$(stat -c %Y "$TIMESTAMP_FILE")
Expand All @@ -75,15 +69,27 @@ if [ -f "$TIMESTAMP_FILE" ]; then
tar -xzvf "$LATEST_BACKUP_FILE" -C /root/.bitcoin
echo "Extraction complete."
exit 0
else
echo "Warning: No backup file found. Unable to proceed with extraction."
exit 1
fi
fi
fi

# Acquire the lock before downloading
if mkdir "$LOCK_FILE" 2>/dev/null; then
# Use flock for the lock file to prevent concurrent downloads
{
flock -n 9 || {
echo "Another container is currently downloading the snapshot. Waiting for download to finish..."
# Wait for the download to complete
while [ -f "$LOCK_FILE" ]; do
sleep 5 # Wait for 5 seconds before checking again
done
echo "Download finished. Proceeding to extraction."
# Check again if the backup file exists after the wait
if [ -f "$LATEST_BACKUP_FILE" ]; then
tar -xzvf "$LATEST_BACKUP_FILE" -C /root/.bitcoin
echo "Extraction complete."
exit 0
fi
}

echo "Lock acquired. Proceeding with snapshot download..."

# Remove any old backup files in the shared volume
Expand Down Expand Up @@ -120,39 +126,19 @@ if mkdir "$LOCK_FILE" 2>/dev/null; then

if [ $success -eq 0 ]; then
echo "Failed to download the snapshot after $MAX_RETRIES attempts. Aborting."
cleanup # Clean up the lock file
exit 1
fi

cleanup # Clean up the lock file

# Extract the snapshot to the container's local chainstate directory
echo "Extracting the snapshot to the container's local chainstate..."
tar -xzvf "$BACKUP_FILE" -C /root/.bitcoin

# Update the timestamp file to mark the download time
touch "$TIMESTAMP_FILE"

echo "Update and extraction completed successfully."
else
echo "Another container is currently downloading the snapshot. Waiting for it to complete..."

# Wait for the download to finish and the lock to be released
while [ -d "$LOCK_FILE" ]; do
sleep 5 # Check every 5 seconds
done
# Release the lock file to allow other containers to start extracting
} 9>"$LOCK_FILE" # Using file descriptor 9 for the lock file

# After the download, proceed with extraction
echo "Snapshot downloaded by another container. Checking for backup file..."

if [ -f "$BACKUP_HASH_FILE" ]; then
echo "Proceeding to extraction for this container."
tar -xzvf "$BACKUP_FILE" -C /root/.bitcoin
echo "Extraction complete."
else
echo "Expected backup file $BACKUP_FILE does not exist. Unable to proceed with extraction."
exit 1
fi
fi
# Extract the downloaded snapshot
echo "Extracting the downloaded snapshot..."
tar -xzvf "$BACKUP_FILE" -C /root/.bitcoin
echo "Extraction complete."

cleanup # Clean up the lock file if it was acquired
# If we reach here, the download was successful, and other containers can now extract the snapshot
echo "Update and extraction completed successfully."

0 comments on commit 4cc2cce

Please sign in to comment.