|
| 1 | +# Database Locking for Concurrent Import Prevention |
| 2 | + |
| 3 | +## Overview |
| 4 | + |
| 5 | +The OpenEMR Code Import CLI tool now includes database locking to prevent concurrent imports of the same vocabulary type, which could otherwise result in data corruption or incomplete imports. |
| 6 | + |
| 7 | +## How It Works |
| 8 | + |
| 9 | +### Database Named Locks |
| 10 | + |
| 11 | +The system uses MySQL's `GET_LOCK()` and `RELEASE_LOCK()` functions to implement named locks: |
| 12 | + |
| 13 | +- **Lock Name Format**: `openemr_vocab_import_{CODE_TYPE}` |
| 14 | +- **Timeout**: 30 seconds (configurable) |
| 15 | +- **Scope**: Per code type (RXNORM, SNOMED, ICD9, ICD10, CQM_VALUESET) |
| 16 | + |
| 17 | +### Lock Behavior |
| 18 | + |
| 19 | +1. **Before Import**: Attempts to acquire a database lock for the specific code type |
| 20 | +2. **Retry Logic**: If lock is held, retries with exponential backoff (configurable) |
| 21 | +3. **During Import**: Holds the lock for the entire duration of the import process |
| 22 | +4. **After Import**: Automatically releases the lock (even if import fails) |
| 23 | +5. **Cleanup**: Destructor ensures locks are released if the process exits unexpectedly |
| 24 | + |
| 25 | +### Concurrent Access Scenarios |
| 26 | + |
| 27 | +| Scenario | Behavior | |
| 28 | +|----------|----------| |
| 29 | +| Same code type (e.g., two RXNORM imports) | Second process retries with exponential backoff (default: 10 attempts over ~8 minutes) | |
| 30 | +| Different code types (e.g., RXNORM + SNOMED) | Both processes run concurrently without interference | |
| 31 | +| Process crash/kill | Lock automatically released by MySQL when connection closes | |
| 32 | +| No-wait mode (`--lock-retry-delay=0`) | Second process fails immediately without waiting | |
| 33 | + |
| 34 | +## Error Messages |
| 35 | + |
| 36 | +### Lock Acquisition Failure (after retries) |
| 37 | +``` |
| 38 | +Failed to acquire database lock for RXNORM import after 10 attempts (510 seconds total). Another import may still be in progress. |
| 39 | +``` |
| 40 | + |
| 41 | +### No-wait Mode Failure |
| 42 | +``` |
| 43 | +Failed to acquire database lock for RXNORM import - another import is in progress and no-wait mode is enabled. |
| 44 | +``` |
| 45 | + |
| 46 | +### Database Error |
| 47 | +``` |
| 48 | +Database lock acquisition failed for RXNORM import due to a database error. |
| 49 | +``` |
| 50 | + |
| 51 | +## Benefits |
| 52 | + |
| 53 | +1. **Data Integrity**: Prevents table corruption from concurrent DROP/CREATE operations |
| 54 | +2. **Import Consistency**: Ensures complete, atomic imports |
| 55 | +3. **Resource Protection**: Prevents conflicts in temporary directories and tracking tables |
| 56 | +4. **Graceful Degradation**: Clear error messages when conflicts occur |
| 57 | + |
| 58 | +## Technical Implementation |
| 59 | + |
| 60 | +### CodeImporter.php Changes |
| 61 | + |
| 62 | +- Added `$currentLockName` property to track active locks |
| 63 | +- `acquireLock()` method uses `GET_LOCK()` with 30-second timeout |
| 64 | +- `releaseLock()` method uses `RELEASE_LOCK()` with error handling |
| 65 | +- `__destruct()` ensures cleanup on object destruction |
| 66 | +- `import()` method wrapped with try/finally for guaranteed lock release |
| 67 | + |
| 68 | +### Lock Names by Code Type |
| 69 | + |
| 70 | +- RXNORM: `openemr_vocab_import_RXNORM` |
| 71 | +- SNOMED: `openemr_vocab_import_SNOMED` |
| 72 | +- SNOMED_RF2: `openemr_vocab_import_SNOMED_RF2` |
| 73 | +- ICD9: `openemr_vocab_import_ICD9` |
| 74 | +- ICD10: `openemr_vocab_import_ICD10` |
| 75 | +- CQM_VALUESET: `openemr_vocab_import_CQM_VALUESET` |
| 76 | + |
| 77 | +## CLI Configuration Options |
| 78 | + |
| 79 | +### Lock Retry Configuration |
| 80 | + |
| 81 | +- `--lock-retry-attempts=N`: Number of retry attempts (default: 10) |
| 82 | +- `--lock-retry-delay=N`: Initial delay between retries in seconds (default: 30) |
| 83 | + - Set to 0 for no-wait mode (fail immediately) |
| 84 | + - Uses exponential backoff with jitter (caps at 5 minutes per attempt) |
| 85 | + |
| 86 | +### Examples |
| 87 | + |
| 88 | +**Default behavior** (10 retries with exponential backoff): |
| 89 | +```bash |
| 90 | +php oce-import-codes import /path/to/rxnorm.zip |
| 91 | +``` |
| 92 | + |
| 93 | +**Quick failure mode** (no waiting): |
| 94 | +```bash |
| 95 | +php oce-import-codes import /path/to/rxnorm.zip --lock-retry-delay=0 |
| 96 | +``` |
| 97 | + |
| 98 | +**Custom retry behavior** (3 attempts, 60-second initial delay): |
| 99 | +```bash |
| 100 | +php oce-import-codes import /path/to/rxnorm.zip --lock-retry-attempts=3 --lock-retry-delay=60 |
| 101 | +``` |
| 102 | + |
| 103 | +## Testing Concurrent Access |
| 104 | + |
| 105 | +To test the locking mechanism: |
| 106 | + |
| 107 | +1. Start an import process: |
| 108 | + ```bash |
| 109 | + php oce-import-codes import /path/to/rxnorm.zip |
| 110 | + ``` |
| 111 | + |
| 112 | +2. While the first is running, start a second import of the same type: |
| 113 | + ```bash |
| 114 | + php oce-import-codes import /path/to/another-rxnorm.zip |
| 115 | + ``` |
| 116 | + |
| 117 | +3. The second process should retry for several minutes with exponential backoff, displaying progress messages. |
| 118 | + |
| 119 | +## Monitoring Active Locks |
| 120 | + |
| 121 | +You can check for active import locks in MySQL: |
| 122 | + |
| 123 | +```sql |
| 124 | +SELECT * FROM performance_schema.metadata_locks |
| 125 | +WHERE OBJECT_NAME LIKE 'openemr_vocab_import_%'; |
| 126 | +``` |
| 127 | + |
| 128 | +Or check specific locks: |
| 129 | + |
| 130 | +```sql |
| 131 | +SELECT IS_USED_LOCK('openemr_vocab_import_RXNORM') as lock_status; |
| 132 | +``` |
| 133 | + |
| 134 | +## Troubleshooting |
| 135 | + |
| 136 | +### Stuck Locks |
| 137 | +If a lock appears stuck (process crashed without cleanup): |
| 138 | + |
| 139 | +```sql |
| 140 | +SELECT RELEASE_LOCK('openemr_vocab_import_RXNORM'); |
| 141 | +``` |
| 142 | + |
| 143 | +### Lock Timeout Issues |
| 144 | +If 30 seconds isn't enough for your environment, modify the timeout in `CodeImporter.php`: |
| 145 | + |
| 146 | +```php |
| 147 | +$result = sqlQuery("SELECT GET_LOCK(?, 60) as lock_result", [$lockName]); // 60 seconds |
| 148 | +``` |
| 149 | + |
| 150 | +## Backward Compatibility |
| 151 | + |
| 152 | +This change is fully backward compatible: |
| 153 | +- Single imports work exactly as before |
| 154 | +- No changes to command-line interface |
| 155 | +- No additional dependencies required |
| 156 | +- Falls back gracefully if database functions unavailable |
0 commit comments