mirror of
https://github.com/aptly-dev/aptly.git
synced 2026-07-15 11:57:59 +00:00
Fix race conditions and improve etcd timeout handling
This commit addresses several critical race conditions and improves the reliability of etcd operations through better timeout and retry handling. ## Race Condition Fixes 1. **Task Resource Management Bug** - Fixed incorrect variable usage in task/list.go:78 - Was using completed task's resources instead of idle task's resources - This caused resource conflicts and potential deadlocks 2. **Database Channel Initialization** - Added sync.Once pattern to ensure thread-safe channel initialization - Prevents panic from concurrent access during startup - Created initDBRequests() function for safe initialization 3. **Published Storage Double-Checked Locking** - Implemented double-checked locking pattern in GetPublishedStorage - Reduces lock contention while preventing concurrent initialization - Improves performance for frequently accessed storage 4. **File Operation Synchronization** - Created FileLockRegistry in utils/filelock.go - Prevents concurrent file operations (create, rename, delete, link) - Implements deadlock prevention for multi-file operations - Critical for preventing file corruption during parallel publishes 5. **WaitGroup Miscount Prevention** - Added defer pattern to ensure Done() is always called - Protects against panics during task execution - Prevents "negative WaitGroup counter" errors ## etcd Improvements 1. **Timeout Protection** - Replaced global context.TODO() with per-operation timeout contexts - Default timeout: 60 seconds (configurable) - Prevents indefinite hangs when etcd is unresponsive 2. **Environment Variable Configuration** - APTLY_ETCD_TIMEOUT: Operation timeout (default: 60s) - APTLY_ETCD_DIAL_TIMEOUT: Connection timeout (default: 60s) - APTLY_ETCD_KEEPALIVE: Keep-alive timeout (default: 7200s) - APTLY_ETCD_MAX_MSG_SIZE: Max message size (default: 50MB) 3. **Retry Logic for Read Operations** - Get operations retry up to 3 times with exponential backoff - Only retries on temporary/network errors - Improves reliability without risking data inconsistency 4. **Enhanced Error Logging** - All etcd errors now logged with operation context - Replaces silent failures with actionable error messages - Improves debugging and monitoring capabilities 5. **Increased Message Size Limits** - Default increased from 10MB to 50MB - Configurable via environment variable - Prevents "message too large" errors for large operations ## Testing - Added comprehensive tests for etcd timeout functionality - Tests verify context timeout, retry logic, and configuration - All existing tests pass with the new implementation ## Documentation - Updated README.rst with etcd configuration section - Documented all environment variables and their defaults - Added examples and feature descriptions These changes significantly improve the reliability and debuggability of aptly when using etcd as the database backend, while also fixing critical race conditions that could cause data corruption or service crashes.
This commit is contained in:
+49
@@ -135,3 +135,52 @@ Scala sbt:
|
||||
Molior:
|
||||
|
||||
- `Molior Debian Build System <https://github.com/molior-dbs/molior>`_ by André Roth
|
||||
|
||||
Configuration
|
||||
=============
|
||||
|
||||
etcd Database Configuration
|
||||
---------------------------
|
||||
|
||||
When using etcd as the database backend, aptly supports several environment variables for configuration:
|
||||
|
||||
**Timeout Configuration:**
|
||||
|
||||
- ``APTLY_ETCD_TIMEOUT``: Operation timeout for etcd requests (default: ``60s``)
|
||||
|
||||
Example: ``export APTLY_ETCD_TIMEOUT=30s``
|
||||
|
||||
- ``APTLY_ETCD_DIAL_TIMEOUT``: Connection timeout when establishing etcd connection (default: ``60s``)
|
||||
|
||||
Example: ``export APTLY_ETCD_DIAL_TIMEOUT=10s``
|
||||
|
||||
**Connection Configuration:**
|
||||
|
||||
- ``APTLY_ETCD_KEEPALIVE``: Keep-alive timeout for etcd connections (default: ``7200s``)
|
||||
|
||||
Example: ``export APTLY_ETCD_KEEPALIVE=3600s``
|
||||
|
||||
- ``APTLY_ETCD_MAX_MSG_SIZE``: Maximum message size in bytes for etcd requests/responses (default: ``52428800`` - 50MB)
|
||||
|
||||
Example: ``export APTLY_ETCD_MAX_MSG_SIZE=104857600`` # 100MB
|
||||
|
||||
**Example Configuration:**
|
||||
|
||||
.. code-block:: bash
|
||||
|
||||
# Set shorter timeouts for faster failure detection
|
||||
export APTLY_ETCD_TIMEOUT=30s
|
||||
export APTLY_ETCD_DIAL_TIMEOUT=10s
|
||||
|
||||
# Increase message size for large package operations
|
||||
export APTLY_ETCD_MAX_MSG_SIZE=104857600
|
||||
|
||||
# Run aptly with etcd backend
|
||||
aptly -config=/etc/aptly-etcd.conf mirror update debian-stable
|
||||
|
||||
**Features:**
|
||||
|
||||
- **Automatic Retry**: Read operations (Get) automatically retry up to 3 times with exponential backoff on temporary failures
|
||||
- **Timeout Protection**: All etcd operations use context with timeout to prevent indefinite hangs
|
||||
- **Enhanced Logging**: All etcd errors are logged with operation context for better debugging
|
||||
- **Configurable Limits**: Message size limits can be adjusted for large package operations
|
||||
|
||||
Reference in New Issue
Block a user