- 
                Notifications
    
You must be signed in to change notification settings  - Fork 25.6k
 
[ML] Refactor inference request executor to leverage scheduled execution #126858
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
          
     Merged
      
      
            jonathan-buttner
  merged 5 commits into
  elastic:main
from
jonathan-buttner:ml-refactor-request-exec
  
      
      
   
  Apr 16, 2025 
      
    
  
     Merged
                    Changes from all commits
      Commits
    
    
            Show all changes
          
          
            5 commits
          
        
        Select commit
          Hold shift + click to select a range
      
      9e4c33b
              
                Using threadpool schedule and fixing tests
              
              
                jonathan-buttner 754ee20
              
                Update docs/changelog/126858.yaml
              
              
                jonathan-buttner d99f756
              
                Clean up
              
              
                jonathan-buttner e7e665e
              
                Merge branch 'ml-refactor-request-exec' of github.com:jonathan-buttne…
              
              
                jonathan-buttner 60f1660
              
                change log
              
              
                jonathan-buttner 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
    
  
  
    
              | Original file line number | Diff line number | Diff line change | 
|---|---|---|
| @@ -0,0 +1,6 @@ | ||
| pr: 126858 | ||
| summary: Leverage threadpool schedule for inference api to avoid long running thread | ||
| area: Machine Learning | ||
| type: bug | ||
| issues: | ||
| - 126853 | 
  
    
      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
    
  
  
    
              
  
    
      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 | 
|---|---|---|
| 
          
            
          
           | 
    @@ -51,7 +51,6 @@ | |
| import static org.mockito.ArgumentMatchers.any; | ||
| import static org.mockito.ArgumentMatchers.anyInt; | ||
| import static org.mockito.Mockito.doAnswer; | ||
| import static org.mockito.Mockito.doThrow; | ||
| import static org.mockito.Mockito.mock; | ||
| import static org.mockito.Mockito.times; | ||
| import static org.mockito.Mockito.verify; | ||
| 
          
            
          
           | 
    @@ -206,7 +205,7 @@ public void testExecute_Throws_WhenQueueIsFull() { | |
| assertFalse(thrownException.isExecutorShutdown()); | ||
| } | ||
| 
     | 
||
| public void testTaskThrowsError_CallsOnFailure() { | ||
| public void testTaskThrowsError_CallsOnFailure() throws InterruptedException { | ||
| var requestSender = mock(RetryingHttpSender.class); | ||
| 
     | 
||
| var service = createRequestExecutorService(null, requestSender); | ||
| 
        
          
        
         | 
    @@ -229,6 +228,8 @@ public void testTaskThrowsError_CallsOnFailure() { | |
| var thrownException = expectThrows(ElasticsearchException.class, () -> listener.actionGet(TIMEOUT)); | ||
| assertThat(thrownException.getMessage(), is(format("Failed to send request from inference entity id [%s]", "id"))); | ||
| assertThat(thrownException.getCause(), instanceOf(IllegalArgumentException.class)); | ||
| service.awaitTermination(TIMEOUT.getSeconds(), TimeUnit.SECONDS); | ||
| 
     | 
||
| assertTrue(service.isTerminated()); | ||
| } | ||
| 
     | 
||
| 
          
            
          
           | 
    @@ -361,7 +362,6 @@ public void testQueuePoll_DoesNotCauseServiceToTerminate_WhenItThrows() throws I | |
| createRequestExecutorServiceSettingsEmpty(), | ||
| requestSender, | ||
| Clock.systemUTC(), | ||
| RequestExecutorService.DEFAULT_SLEEPER, | ||
| RequestExecutorService.DEFAULT_RATE_LIMIT_CREATOR | ||
| ); | ||
| 
     | 
||
| 
        
          
        
         | 
    @@ -375,36 +375,7 @@ public void testQueuePoll_DoesNotCauseServiceToTerminate_WhenItThrows() throws I | |
| }); | ||
| service.start(); | ||
| 
     | 
||
| assertTrue(service.isTerminated()); | ||
| } | ||
| 
     | 
||
| public void testSleep_ThrowingInterruptedException_TerminatesService() throws Exception { | ||
| 
         There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We're no longer using a "sleeper" so we don't need this test anymore.  | 
||
| @SuppressWarnings("unchecked") | ||
| BlockingQueue<RejectableTask> queue = mock(LinkedBlockingQueue.class); | ||
| var sleeper = mock(RequestExecutorService.Sleeper.class); | ||
| doThrow(new InterruptedException("failed")).when(sleeper).sleep(any()); | ||
| 
     | 
||
| var service = new RequestExecutorService( | ||
| threadPool, | ||
| mockQueueCreator(queue), | ||
| null, | ||
| createRequestExecutorServiceSettingsEmpty(), | ||
| mock(RetryingHttpSender.class), | ||
| Clock.systemUTC(), | ||
| sleeper, | ||
| RequestExecutorService.DEFAULT_RATE_LIMIT_CREATOR | ||
| ); | ||
| 
     | 
||
| Future<?> executorTermination = threadPool.generic().submit(() -> { | ||
| try { | ||
| service.start(); | ||
| } catch (Exception e) { | ||
| fail(Strings.format("Failed to shutdown executor: %s", e)); | ||
| } | ||
| }); | ||
| 
     | 
||
| executorTermination.get(TIMEOUT.millis(), TimeUnit.MILLISECONDS); | ||
| 
     | 
||
| service.awaitTermination(TIMEOUT.getSeconds(), TimeUnit.SECONDS); | ||
| assertTrue(service.isTerminated()); | ||
| } | ||
| 
     | 
||
| 
          
            
          
           | 
    @@ -581,7 +552,6 @@ public void testDoesNotExecuteTask_WhenCannotReserveTokens() { | |
| settings, | ||
| requestSender, | ||
| Clock.systemUTC(), | ||
| RequestExecutorService.DEFAULT_SLEEPER, | ||
| rateLimiterCreator | ||
| ); | ||
| var requestManager = RequestManagerTests.createMock(requestSender); | ||
| 
          
            
          
           | 
    @@ -614,7 +584,6 @@ public void testDoesNotExecuteTask_WhenCannotReserveTokens_AndThenCanReserve_And | |
| settings, | ||
| requestSender, | ||
| Clock.systemUTC(), | ||
| RequestExecutorService.DEFAULT_SLEEPER, | ||
| rateLimiterCreator | ||
| ); | ||
| var requestManager = RequestManagerTests.createMock(requestSender); | ||
| 
        
          
        
         | 
    @@ -626,11 +595,15 @@ public void testDoesNotExecuteTask_WhenCannotReserveTokens_AndThenCanReserve_And | |
| 
     | 
||
| doAnswer(invocation -> { | ||
| service.shutdown(); | ||
| ActionListener<InferenceServiceResults> passedListener = invocation.getArgument(4); | ||
| passedListener.onResponse(null); | ||
| 
     | 
||
| return Void.TYPE; | ||
| }).when(requestSender).send(any(), any(), any(), any(), any()); | ||
| 
     | 
||
| service.start(); | ||
| 
     | 
||
| listener.actionGet(TIMEOUT); | ||
| verify(requestSender, times(1)).send(any(), any(), any(), any(), any()); | ||
| } | ||
| 
     | 
||
| 
        
          
        
         | 
    @@ -648,7 +621,6 @@ public void testRemovesRateLimitGroup_AfterStaleDuration() { | |
| settings, | ||
| requestSender, | ||
| clock, | ||
| RequestExecutorService.DEFAULT_SLEEPER, | ||
| RequestExecutorService.DEFAULT_RATE_LIMIT_CREATOR | ||
| ); | ||
| var requestManager = RequestManagerTests.createMock(requestSender, "id1"); | ||
| 
          
            
          
           | 
    @@ -682,7 +654,6 @@ public void testStartsCleanupThread() { | |
| settings, | ||
| requestSender, | ||
| Clock.systemUTC(), | ||
| RequestExecutorService.DEFAULT_SLEEPER, | ||
| RequestExecutorService.DEFAULT_RATE_LIMIT_CREATOR | ||
| ); | ||
| 
     | 
||
| 
          
            
          
           | 
    ||
      
      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.
Since we're not sleeping and we're not using a long lived thread we don't need to catch the interrupted exception.