- 
                Notifications
    You must be signed in to change notification settings 
- Fork 25.6k
Ensure that RefreshListener do not access engine under refresh lock #124328
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
          
     Closed
      
      
            tlrx
  wants to merge
  23
  commits into
  elastic:main
from
tlrx:ensure-safe-engine-access-in-refresh-listeners
  
      
      
   
      
    
  
     Closed
                    Changes from all commits
      Commits
    
    
            Show all changes
          
          
            23 commits
          
        
        Select commit
          Hold shift + click to select a range
      
      efebd52
              
                Ensure that RefreshListener do not access engine under refresh lock
              
              
                tlrx 94632da
              
                [CI] Auto commit changes from spotless
              
              
                 6bcf4a8
              
                more registration
              
              
                tlrx f2da65e
              
                Merge branch 'main' into ensure-safe-engine-access-in-refresh-listeners
              
              
                tlrx de3f416
              
                defer engine
              
              
                tlrx af744e9
              
                fix TransportReplicationActionTests
              
              
                tlrx 20d5dfc
              
                also replica
              
              
                tlrx 1f31512
              
                fix TransportReplicationActionTests
              
              
                tlrx f7c193f
              
                fork in rewriteAndFetchShardRequest
              
              
                tlrx f5c1ea0
              
                fork in rewriteAndFetchShardRequest
              
              
                tlrx 37c3409
              
                [CI] Auto commit changes from spotless
              
              
                 b1e673d
              
                remove replica
              
              
                tlrx e33744c
              
                remove replica
              
              
                tlrx d88b476
              
                replica again
              
              
                tlrx 0b34aa8
              
                replication action
              
              
                tlrx 9d51165
              
                Merge branch 'main' into ensure-safe-engine-access-in-refresh-listeners
              
              
                tlrx e053aa0
              
                replication action
              
              
                tlrx cd2333c
              
                bug
              
              
                tlrx c5153c1
              
                bug
              
              
                tlrx a422586
              
                bug
              
              
                tlrx 110426f
              
                bug
              
              
                tlrx b9e646c
              
                revert replication changes
              
              
                tlrx 3e18fc8
              
                Merge branch 'main' into ensure-safe-engine-access-in-refresh-listeners
              
              
                tlrx File filter
Filter by extension
Conversations
          Failed to load comments.   
        
        
          
      Loading
        
  Jump to
        
          Jump to file
        
      
      
          Failed to load files.   
        
        
          
      Loading
        
  Diff view
Diff view
There are no files selected for viewing
  
    
      This file contains hidden or 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 hidden or 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
    
  
  
    
              
        
          
          
            104 changes: 104 additions & 0 deletions
          
          104 
        
  server/src/main/java/org/elasticsearch/index/engine/AbstractReaderManager.java
  
  
      
      
   
        
      
      
    
  
    
      This file contains hidden or 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,104 @@ | ||
| /* | ||
| * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
| * or more contributor license agreements. Licensed under the "Elastic License | ||
| * 2.0", the "GNU Affero General Public License v3.0 only", and the "Server Side | ||
| * Public License v 1"; you may not use this file except in compliance with, at | ||
| * your election, the "Elastic License 2.0", the "GNU Affero General Public | ||
| * License v3.0 only", or the "Server Side Public License, v 1". | ||
| */ | ||
|  | ||
| package org.elasticsearch.index.engine; | ||
|  | ||
| import org.apache.lucene.search.ReferenceManager; | ||
| import org.elasticsearch.common.lucene.index.ElasticsearchDirectoryReader; | ||
| import org.elasticsearch.core.Assertions; | ||
| import org.elasticsearch.core.Nullable; | ||
| import org.elasticsearch.core.SuppressForbidden; | ||
|  | ||
| import java.io.IOException; | ||
| import java.util.Map; | ||
| import java.util.Objects; | ||
| import java.util.concurrent.ConcurrentHashMap; | ||
|  | ||
| @SuppressForbidden(reason = "reference counting is required here") | ||
| public abstract class AbstractReaderManager extends ReferenceManager<ElasticsearchDirectoryReader> { | ||
|  | ||
| @Nullable // if assertions are disabled | ||
| private final Map<RefreshListener, AssertingRefreshListener> assertingListeners = Assertions.ENABLED ? new ConcurrentHashMap<>() : null; | ||
|  | ||
| @Override | ||
| protected boolean tryIncRef(ElasticsearchDirectoryReader reference) { | ||
| return reference.tryIncRef(); | ||
| } | ||
|  | ||
| @Override | ||
| protected int getRefCount(ElasticsearchDirectoryReader reference) { | ||
| return reference.getRefCount(); | ||
| } | ||
|  | ||
| @Override | ||
| protected void decRef(ElasticsearchDirectoryReader reference) throws IOException { | ||
| reference.decRef(); | ||
| } | ||
|  | ||
| @Override | ||
| public final void addListener(RefreshListener listener) { | ||
| if (Assertions.ENABLED == false) { | ||
| super.addListener(listener); | ||
| return; | ||
| } | ||
|  | ||
| final var assertingListener = new AssertingRefreshListener(listener); | ||
| var previous = assertingListeners.put(listener, assertingListener); | ||
| assert previous == null : "listener already added"; | ||
| super.addListener(assertingListener); | ||
| } | ||
|  | ||
| @Override | ||
| public final void removeListener(RefreshListener listener) { | ||
| if (Assertions.ENABLED == false) { | ||
| super.removeListener(listener); | ||
| return; | ||
| } | ||
|  | ||
| final var assertingListener = assertingListeners.remove(listener); | ||
| assert assertingListener != null : "listener already removed"; | ||
| super.removeListener(assertingListener); | ||
| } | ||
|  | ||
| /** | ||
| * A delegating {@link RefreshListener} used to assert that refresh listeners are not accessing the engine within before/after refresh | ||
| * methods. | ||
| */ | ||
| private static class AssertingRefreshListener implements RefreshListener { | ||
|  | ||
| private final RefreshListener delegate; | ||
|  | ||
| private AssertingRefreshListener(RefreshListener delegate) { | ||
| this.delegate = Objects.requireNonNull(delegate); | ||
| if (Assertions.ENABLED == false) { | ||
| throw new AssertionError("Only use this when assertions are enabled"); | ||
| } | ||
| } | ||
|  | ||
| @Override | ||
| public void beforeRefresh() throws IOException { | ||
| SafeEngineAccessThreadLocal.accessStart(); | ||
| try { | ||
| delegate.beforeRefresh(); | ||
| } finally { | ||
| SafeEngineAccessThreadLocal.accessEnd(); | ||
| } | ||
| } | ||
|  | ||
| @Override | ||
| public void afterRefresh(boolean didRefresh) throws IOException { | ||
| SafeEngineAccessThreadLocal.accessStart(); | ||
| try { | ||
| delegate.afterRefresh(didRefresh); | ||
| } finally { | ||
| SafeEngineAccessThreadLocal.accessEnd(); | ||
| } | ||
| } | ||
| } | ||
| } | 
  
    
      This file contains hidden or 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
    
  
  
    
              
        
          
          
            20 changes: 20 additions & 0 deletions
          
          20 
        
  server/src/main/java/org/elasticsearch/index/engine/EngineAwareRefreshListener.java
  
  
      
      
   
        
      
      
    
  
    
      This file contains hidden or 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,20 @@ | ||
| /* | ||
| * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
| * or more contributor license agreements. Licensed under the "Elastic License | ||
| * 2.0", the "GNU Affero General Public License v3.0 only", and the "Server Side | ||
| * Public License v 1"; you may not use this file except in compliance with, at | ||
| * your election, the "Elastic License 2.0", the "GNU Affero General Public | ||
| * License v3.0 only", or the "Server Side Public License, v 1". | ||
| */ | ||
|  | ||
| package org.elasticsearch.index.engine; | ||
|  | ||
| import org.apache.lucene.search.ReferenceManager; | ||
|  | ||
| /** | ||
| * A type of {@link ReferenceManager.RefreshListener} that is called back when a new {@link Engine} is instanciated. | ||
| */ | ||
| public interface EngineAwareRefreshListener extends ReferenceManager.RefreshListener { | ||
|  | ||
| void onNewEngine(Engine engine); | ||
| } | 
  
    
      This file contains hidden or 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
    
  
  
    
              
      
      Oops, something went wrong.
        
    
  
  Add this suggestion to a batch that can be applied as a single commit.
  This suggestion is invalid because no changes were made to the code.
  Suggestions cannot be applied while the pull request is closed.
  Suggestions cannot be applied while viewing a subset of changes.
  Only one suggestion per line can be applied in a batch.
  Add this suggestion to a batch that can be applied as a single commit.
  Applying suggestions on deleted lines is not supported.
  You must change the existing code in this line in order to create a valid suggestion.
  Outdated suggestions cannot be applied.
  This suggestion has been applied or marked resolved.
  Suggestions cannot be applied from pending reviews.
  Suggestions cannot be applied on multi-line comments.
  Suggestions cannot be applied while the pull request is queued to merge.
  Suggestion cannot be applied right now. Please check back later.
  
    
  
    
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This invocation has been caught by the new
SafeEngineAccessThreadLocal