99import java .time .Clock ;
1010import java .time .temporal .ChronoUnit ;
1111import java .util .ArrayList ;
12+ import java .util .Arrays ;
1213import java .util .List ;
14+ import java .util .Locale ;
1315import java .util .Map ;
1416import java .util .concurrent .TimeUnit ;
1517import java .util .stream .Collectors ;
@@ -338,8 +340,18 @@ public void findMedianIntervalAdaptive(ActionListener<IntervalTimeConfiguration>
338340 .aggregation (AggregationBuilders .max ("max_ts" ).field (TS_FIELD ));
339341
340342 SearchRequest boundsReq = new SearchRequest (config .getIndices ().toArray (new String [0 ])).source (boundsSrc );
341-
342- client .search (boundsReq , ActionListener .wrap (r -> {
343+ logger .debug ("Min and max timestamp request: {}" , boundsReq );
344+ final ActionListener <SearchResponse > boundsRequestListener = ActionListener .wrap (r -> {
345+ logger .debug ("Min and max timestamp response: {}" , r );
346+
347+ // Fail earlier if we aren't able to get any bounds from the query
348+ if (r .getTotalShards () == 0 || r .getSuccessfulShards () == 0 ) {
349+ String errorMsg = String
350+ .format (Locale .ROOT , CommonMessages .NO_SHARDS_FOUND_IN_INDEX , Arrays .toString (config .getIndices ().toArray ()));
351+ logger .error (errorMsg );
352+ listener .onFailure (new ValidationException (errorMsg , ValidationIssueType .INDICES , ValidationAspect .MODEL ));
353+ return ;
354+ }
343355
344356 Min minAgg = r .getAggregations ().get ("min_ts" );
345357 Max maxAgg = r .getAggregations ().get ("max_ts" );
@@ -379,8 +391,21 @@ public void findMedianIntervalAdaptive(ActionListener<IntervalTimeConfiguration>
379391 }
380392
381393 refineGap (initBucketMins , -1 , baseFilter , listener , MIN_BUCKET_WIDTH_MINS , ChronoUnit .MINUTES , TS_FIELD , 0 , minMs , maxMs );
382-
383- }, listener ::onFailure ));
394+ }, e -> {
395+ logger .error (e .getMessage (), e );
396+ listener .onFailure (e );
397+
398+ });
399+
400+ clientUtil
401+ .<SearchRequest , SearchResponse >asyncRequestWithInjectedSecurity (
402+ boundsReq ,
403+ client ::search ,
404+ user ,
405+ client ,
406+ context ,
407+ boundsRequestListener
408+ );
384409 }
385410
386411 /* ----------------------------------------------------------------------
@@ -521,11 +546,11 @@ public void refineGap(
521546
522547 SearchRequest searchRequest = new SearchRequest (config .getIndices ().toArray (new String [0 ])).source (src );
523548 logger .debug ("Minimum interval search request: {}" , searchRequest );
524- client . search ( searchRequest , ActionListener .wrap (resp -> {
525- logger .debug ("Minimum interval search response: {}" , resp );
549+ final ActionListener < SearchResponse > minIntervalSearchListener = ActionListener .wrap (r -> {
550+ logger .debug ("Minimum interval search response: {}" , r );
526551 double gap = Double .NaN ;
527552 boolean hasEmptyBuckets = false ;
528- Histogram histogram = resp .getAggregations ().get ("dyn" );
553+ Histogram histogram = r .getAggregations ().get ("dyn" );
529554 if (histogram != null ) {
530555 List <? extends Histogram .Bucket > buckets = histogram .getBuckets ();
531556 int firstNonEmpty = -1 ;
@@ -614,8 +639,20 @@ public void refineGap(
614639 }
615640
616641 refineGap (nextBucketMins , nextDir , baseFilter , listener , minBucketMins , returnUnit , tsField , depth + 1 , sliceMinMs , sliceMaxMs );
617-
618- }, listener ::onFailure ));
642+ }, e -> {
643+ logger .error (e .getMessage (), e );
644+ listener .onFailure (e );
645+ });
646+
647+ clientUtil
648+ .<SearchRequest , SearchResponse >asyncRequestWithInjectedSecurity (
649+ searchRequest ,
650+ client ::search ,
651+ user ,
652+ client ,
653+ context ,
654+ minIntervalSearchListener
655+ );
619656 }
620657
621658 /**
@@ -652,7 +689,8 @@ public void runAutoDate(BoolQueryBuilder filter, ActionListener<IntervalTimeConf
652689 .aggregation (PipelineAggregatorBuilders .minBucket ("shortest" , "auto>gap" ));
653690
654691 SearchRequest searchRequest = new SearchRequest (config .getIndices ().toArray (new String [0 ])).source (src );
655- client .search (searchRequest , ActionListener .wrap (r -> {
692+
693+ final ActionListener <SearchResponse > autoDateSearchListener = ActionListener .wrap (r -> {
656694 NumericMetricsAggregation .SingleValue v = r .getAggregations ().get ("shortest" );
657695 if (v == null || Double .isNaN (v .value ())) {
658696 listener .onResponse (null );
@@ -664,7 +702,21 @@ public void runAutoDate(BoolQueryBuilder filter, ActionListener<IntervalTimeConf
664702 } else {
665703 listener .onResponse (null );
666704 }
667- }, listener ::onFailure ));
705+ }, e -> {
706+ logger .error (e .getMessage (), e );
707+ listener .onFailure (e );
708+ });
709+
710+ clientUtil
711+ .<SearchRequest , SearchResponse >asyncRequestWithInjectedSecurity (
712+ searchRequest ,
713+ client ::search ,
714+ user ,
715+ client ,
716+ context ,
717+ autoDateSearchListener
718+ );
719+
668720 }
669721
670722 /**
0 commit comments