|
| 1 | +# Solution for Elastica Issue #2219 |
| 2 | + |
| 3 | +## Problem Summary |
| 4 | +**Issue**: BC Break from 7.3.1 to 7.3.2 - `Call to a member function hasConnection() on null` |
| 5 | +**Root Cause**: Code in `src/Bulk.php` was trying to use removed Connection API methods |
| 6 | +**Impact**: Any bulk operations (populate, etc.) fail with fatal error |
| 7 | + |
| 8 | +## Root Cause Analysis |
| 9 | + |
| 10 | +### What Happened |
| 11 | +In version 7.3.2, code was added to `src/Bulk.php` that attempted to use the old Connection API: |
| 12 | + |
| 13 | +```php |
| 14 | +// BROKEN CODE (7.3.2) |
| 15 | +if (!$document->hasRetryOnConflict() && $this->_client->hasConnection() && $this->_client->getConnection()->hasParam('retryOnConflict') && ($retry = $this->_client->getConnection()->getParam('retryOnConflict')) > 0) { |
| 16 | + $document->setRetryOnConflict($retry); |
| 17 | +} |
| 18 | +``` |
| 19 | + |
| 20 | +### Why It Failed |
| 21 | +1. **Connection Class Removed**: In Elastica 8.0, the `Elastica\Connection` class was removed |
| 22 | +2. **Methods Don't Exist**: `hasConnection()` and `getConnection()` methods no longer exist on Client |
| 23 | +3. **Architecture Change**: Elastica 8.0+ uses the official Elasticsearch PHP client's transport layer |
| 24 | + |
| 25 | +## Solution Approaches |
| 26 | + |
| 27 | +### Approach 1: Quick Fix for 7.3.2 (Recommended) |
| 28 | +Replace the Connection-based code with configuration-based approach: |
| 29 | + |
| 30 | +```php |
| 31 | +// FIXED CODE |
| 32 | +if (!$document->hasRetryOnConflict()) { |
| 33 | + $retry = $this->_client->getConfigValue('retryOnConflict', 0); |
| 34 | + if ($retry > 0) { |
| 35 | + $document->setRetryOnConflict($retry); |
| 36 | + } |
| 37 | +} |
| 38 | +``` |
| 39 | + |
| 40 | +### Approach 2: Upgrade to 8.0+ (Long-term) |
| 41 | +The issue is already fixed in Elastica 8.0+ where the architecture was modernized. |
| 42 | + |
| 43 | +## Implementation |
| 44 | + |
| 45 | +### Step 1: Apply the Fix |
| 46 | +Replace the problematic code in `src/Bulk.php` at two locations: |
| 47 | + |
| 48 | +1. **In `addDocument()` method** (around line 135) |
| 49 | +2. **In `addScript()` method** (around line 161) |
| 50 | + |
| 51 | +### Step 2: Test the Fix |
| 52 | +```php |
| 53 | +// Test code to verify the fix works |
| 54 | +$client = new \Elastica\Client(['hosts' => ['localhost:9200']]); |
| 55 | +$index = $client->getIndex('test'); |
| 56 | +$document = new \Elastica\Document('1', ['field' => 'value']); |
| 57 | + |
| 58 | +// This should not throw the hasConnection() error |
| 59 | +$bulk = new \Elastica\Bulk($client); |
| 60 | +$bulk->addDocument($document); |
| 61 | +$bulk->send(); |
| 62 | +``` |
| 63 | + |
| 64 | +### Step 3: Verify Configuration |
| 65 | +Ensure that `retryOnConflict` is properly configured: |
| 66 | + |
| 67 | +```php |
| 68 | +$client = new \Elastica\Client([ |
| 69 | + 'hosts' => ['localhost:9200'], |
| 70 | + 'retryOnConflict' => 3 // This will now work correctly |
| 71 | +]); |
| 72 | +``` |
| 73 | + |
| 74 | +## Files to Modify |
| 75 | + |
| 76 | +### src/Bulk.php |
| 77 | +- **Line ~135**: Replace Connection-based retry logic in `addDocument()` |
| 78 | +- **Line ~161**: Replace Connection-based retry logic in `addScript()` |
| 79 | + |
| 80 | +## Testing Strategy |
| 81 | + |
| 82 | +### Unit Tests |
| 83 | +1. Test that bulk operations work without Connection API |
| 84 | +2. Test that retryOnConflict configuration is respected |
| 85 | +3. Test both Document and Script bulk operations |
| 86 | + |
| 87 | +### Integration Tests |
| 88 | +1. Test with actual Elasticsearch instance |
| 89 | +2. Test bulk operations with retryOnConflict > 0 |
| 90 | +3. Test bulk operations with retryOnConflict = 0 (default) |
| 91 | + |
| 92 | +## Migration Path |
| 93 | + |
| 94 | +### For Users on 7.3.1 |
| 95 | +1. **Option A**: Apply the fix patch to 7.3.2 |
| 96 | +2. **Option B**: Stay on 7.3.1 until ready to upgrade to 8.0+ |
| 97 | + |
| 98 | +### For Users on 7.3.2 |
| 99 | +1. **Immediate**: Apply the fix patch |
| 100 | +2. **Long-term**: Plan upgrade to 8.0+ for full architecture benefits |
| 101 | + |
| 102 | +### For New Users |
| 103 | +- Use Elastica 8.0+ where this issue doesn't exist |
| 104 | + |
| 105 | +## Related Issues |
| 106 | +- **Issue #2219**: The main issue being addressed |
| 107 | +- **PR #2188**: Removed Connection classes (8.0) |
| 108 | +- **PR #2184**: Ported retryOnConflict from 7.x (8.0) |
| 109 | + |
| 110 | +## Next Steps |
| 111 | +1. **Immediate**: Create 7.3.3 release with the fix |
| 112 | +2. **Documentation**: Update upgrade guides |
| 113 | +3. **Communication**: Notify users about the fix |
| 114 | +4. **Long-term**: Encourage migration to 8.0+ |
| 115 | + |
| 116 | +## Code Quality |
| 117 | +- The fix maintains backward compatibility |
| 118 | +- No breaking changes to public API |
| 119 | +- Preserves existing functionality |
| 120 | +- Uses modern configuration approach |
0 commit comments