1616import  org .opensearch .tasks .CancellableTask ;
1717
1818import  java .util .Map ;
19+ import  java .util .Optional ;
20+ import  java .util .function .Supplier ;
1921
2022import  static  org .opensearch .search .SearchService .NO_TIMEOUT ;
2123
2527public  class  QueryGroupTask  extends  CancellableTask  {
2628
2729    private  static  final  Logger  logger  = LogManager .getLogger (QueryGroupTask .class );
30+     public  static  final  String  QUERY_GROUP_ID_HEADER  = "queryGroupId" ;
31+     public  static  final  Supplier <String > DEFAULT_QUERY_GROUP_ID_SUPPLIER  = () -> "DEFAULT_QUERY_GROUP" ;
2832    private  String  queryGroupId ;
2933
3034    public  QueryGroupTask (long  id , String  type , String  action , String  description , TaskId  parentTaskId , Map <String , String > headers ) {
@@ -47,7 +51,7 @@ public QueryGroupTask(
4751     * This method should always be called after calling setQueryGroupId at least once on this object 
4852     * @return task queryGroupId 
4953     */ 
50-     public  String  getQueryGroupId () {
54+     public  final   String  getQueryGroupId () {
5155        if  (queryGroupId  == null ) {
5256            logger .warn ("QueryGroup _id can't be null, It should be set before accessing it. This is abnormal behaviour " );
5357        }
@@ -59,12 +63,10 @@ public String getQueryGroupId() {
5963     * This method was defined since the queryGroupId can only be evaluated after task creation 
6064     * @param threadContext current threadContext 
6165     */ 
62-     public  void  setQueryGroupId (final  ThreadContext  threadContext ) {
63-         this .queryGroupId  = QueryGroupConstants .DEFAULT_QUERY_GROUP_ID_SUPPLIER .get ();
64- 
65-         if  (threadContext  != null  && threadContext .getHeader (QueryGroupConstants .QUERY_GROUP_ID_HEADER ) != null ) {
66-             this .queryGroupId  = threadContext .getHeader (QueryGroupConstants .QUERY_GROUP_ID_HEADER );
67-         }
66+     public  final  void  setQueryGroupId (final  ThreadContext  threadContext ) {
67+         this .queryGroupId  = Optional .ofNullable (threadContext )
68+             .map (threadContext1  -> threadContext1 .getHeader (QUERY_GROUP_ID_HEADER ))
69+             .orElse (DEFAULT_QUERY_GROUP_ID_SUPPLIER .get ());
6870    }
6971
7072    @ Override 
0 commit comments