@@ -274,6 +274,18 @@ public class SearchService extends AbstractLifecycleComponent implements IndexEv
274274 Property .NodeScope
275275 );
276276
277+ /**
278+ * The size of the buffer used for memory accounting.
279+ * This buffer is used to locally track the memory accummulate during the executiong of
280+ * a search request before submitting the accumulated value to the circuit breaker.
281+ */
282+ public static final Setting <ByteSizeValue > MEMORY_ACCOUNTING_BUFFER_SIZE = Setting .byteSizeSetting (
283+ "search.memory_accounting_buffer_size" ,
284+ ByteSizeValue .of (32 , ByteSizeUnit .KB ),
285+ Property .Dynamic ,
286+ Property .NodeScope
287+ );
288+
277289 public static final int DEFAULT_SIZE = 10 ;
278290 public static final int DEFAULT_FROM = 0 ;
279291 private static final StackTraceElement [] EMPTY_STACK_TRACE_ARRAY = new StackTraceElement [0 ];
@@ -311,6 +323,8 @@ public class SearchService extends AbstractLifecycleComponent implements IndexEv
311323
312324 private volatile boolean enableRewriteAggsToFilterByFilter ;
313325
326+ private volatile ByteSizeValue memoryAccountingBufferSize ;
327+
314328 private final Cancellable keepAliveReaper ;
315329
316330 private final AtomicLong idGenerator = new AtomicLong ();
@@ -388,6 +402,9 @@ public SearchService(
388402 enableQueryPhaseParallelCollection = QUERY_PHASE_PARALLEL_COLLECTION_ENABLED .get (settings );
389403 clusterService .getClusterSettings ()
390404 .addSettingsUpdateConsumer (QUERY_PHASE_PARALLEL_COLLECTION_ENABLED , this ::setEnableQueryPhaseParallelCollection );
405+
406+ memoryAccountingBufferSize = MEMORY_ACCOUNTING_BUFFER_SIZE .get (settings );
407+ clusterService .getClusterSettings ().addSettingsUpdateConsumer (MEMORY_ACCOUNTING_BUFFER_SIZE , this ::setMemoryAccountingBufferSize );
391408 }
392409
393410 private void setEnableSearchWorkerThreads (boolean enableSearchWorkerThreads ) {
@@ -402,6 +419,10 @@ private void setEnableQueryPhaseParallelCollection(boolean enableQueryPhaseParal
402419 this .enableQueryPhaseParallelCollection = enableQueryPhaseParallelCollection ;
403420 }
404421
422+ private void setMemoryAccountingBufferSize (ByteSizeValue memoryAccountingBufferSize ) {
423+ this .memoryAccountingBufferSize = memoryAccountingBufferSize ;
424+ }
425+
405426 private static void validateKeepAlives (TimeValue defaultKeepAlive , TimeValue maxKeepAlive ) {
406427 if (defaultKeepAlive .millis () > maxKeepAlive .millis ()) {
407428 throw new IllegalArgumentException (
@@ -790,7 +811,7 @@ public void executeRankFeaturePhase(RankFeatureShardRequest request, SearchShard
790811 return searchContext .rankFeatureResult ();
791812 }
792813 RankFeatureShardPhase .prepareForFetch (searchContext , request );
793- fetchPhase .execute (searchContext , docIds , null , circuitBreaker );
814+ fetchPhase .execute (searchContext , docIds , null , circuitBreaker , memoryAccountingBufferSize . getBytes () );
794815 RankFeatureShardPhase .processFetch (searchContext );
795816 var rankFeatureResult = searchContext .rankFeatureResult ();
796817 rankFeatureResult .incRef ();
@@ -808,7 +829,7 @@ private QueryFetchSearchResult executeFetchPhase(ReaderContext reader, SearchCon
808829 Releasable scope = tracer .withScope (context .getTask ());
809830 SearchOperationListenerExecutor executor = new SearchOperationListenerExecutor (context , true , afterQueryTime )
810831 ) {
811- fetchPhase .execute (context , shortcutDocIdsToLoad (context ), null , circuitBreaker );
832+ fetchPhase .execute (context , shortcutDocIdsToLoad (context ), null , circuitBreaker , memoryAccountingBufferSize . getBytes () );
812833 if (reader .singleSession ()) {
813834 freeReaderContext (reader .id ());
814835 }
@@ -974,7 +995,13 @@ public void executeFetchPhase(ShardFetchRequest request, CancellableTask task, A
974995 System .nanoTime ()
975996 )
976997 ) {
977- fetchPhase .execute (searchContext , request .docIds (), request .getRankDocks (), circuitBreaker );
998+ fetchPhase .execute (
999+ searchContext ,
1000+ request .docIds (),
1001+ request .getRankDocks (),
1002+ circuitBreaker ,
1003+ memoryAccountingBufferSize .getBytes ()
1004+ );
9781005 if (readerContext .singleSession ()) {
9791006 freeReaderContext (request .contextId ());
9801007 }
0 commit comments