2121import  org .elasticsearch .cluster .ClusterState ;
2222import  org .elasticsearch .cluster .ClusterStateTaskConfig ;
2323import  org .elasticsearch .cluster .ClusterStateTaskExecutor ;
24- import  org .elasticsearch .cluster .ClusterStateUpdateTask ;
24+ import  org .elasticsearch .cluster .ClusterStateTaskListener ;
2525import  org .elasticsearch .cluster .service .ClusterService ;
2626import  org .elasticsearch .common .Priority ;
2727import  org .elasticsearch .common .Strings ;
3535import  org .elasticsearch .common .settings .Settings ;
3636import  org .elasticsearch .common .util .set .Sets ;
3737import  org .elasticsearch .core .Nullable ;
38- import  org .elasticsearch .core .SuppressForbidden ;
3938import  org .elasticsearch .core .TimeValue ;
4039import  org .elasticsearch .index .Index ;
4140import  org .elasticsearch .index .IndexMode ;
@@ -132,7 +131,7 @@ public class MetadataIndexTemplateService {
132131            try  {
133132                final  var  task  = taskContext .getTask ();
134133                state  = task .execute (state );
135-                 taskContext .success (new   ClusterStateTaskExecutor . LegacyClusterTaskResultActionListener ( task ,  currentState ));
134+                 taskContext .success (task . listener . map ( ignored  ->  AcknowledgedResponse . TRUE ));
136135            } catch  (Exception  e ) {
137136                taskContext .onFailure (e );
138137            }
@@ -144,20 +143,14 @@ public class MetadataIndexTemplateService {
144143     * A specialized cluster state update task that always takes a listener handling an 
145144     * AcknowledgedResponse, as all template actions have simple acknowledged yes/no responses. 
146145     */ 
147-     private  abstract  static  class  TemplateClusterStateUpdateTask  extends   ClusterStateUpdateTask  {
148-         private   final  ActionListener <AcknowledgedResponse > listener ;
146+     private  abstract  static  class  TemplateClusterStateUpdateTask  implements   ClusterStateTaskListener  {
147+         final  ActionListener <AcknowledgedResponse > listener ;
149148
150-         TemplateClusterStateUpdateTask (Priority  priority , TimeValue  timeout , ActionListener <AcknowledgedResponse > listener ) {
151-             super (priority , timeout );
149+         TemplateClusterStateUpdateTask (ActionListener <AcknowledgedResponse > listener ) {
152150            this .listener  = listener ;
153151        }
154152
155-         public  abstract  ClusterState  doExecute (ClusterState  currentState ) throws  Exception ;
156- 
157-         @ Override 
158-         public  ClusterState  execute (ClusterState  currentState ) throws  Exception  {
159-             return  doExecute (currentState );
160-         }
153+         public  abstract  ClusterState  execute (ClusterState  currentState ) throws  Exception ;
161154
162155        @ Override 
163156        public  void  onFailure (Exception  e ) {
@@ -166,7 +159,7 @@ public void onFailure(Exception e) {
166159
167160        @ Override 
168161        public  void  clusterStateProcessed (ClusterState  oldState , ClusterState  newState ) {
169-             listener . onResponse ( AcknowledgedResponse . TRUE ) ;
162+             assert   false  :  "not called" ;
170163        }
171164    }
172165
@@ -189,43 +182,33 @@ public MetadataIndexTemplateService(
189182        this .indexSettingProviders  = indexSettingProviders .getIndexSettingProviders ();
190183    }
191184
192-     @ SuppressForbidden (reason  = "legacy usage of unbatched task" ) // TODO add support for batching here 
193-     private  void  submitUnbatchedTask (String  source , ClusterStateUpdateTask  task ) {
194-         clusterService .submitUnbatchedStateUpdateTask (source , task );
195-     }
196- 
197185    public  void  removeTemplates (final  RemoveRequest  request , final  ActionListener <AcknowledgedResponse > listener ) {
198-         clusterService .submitStateUpdateTask (
199-             "remove-index-template ["  + request .name  + "]" ,
200-             new  TemplateClusterStateUpdateTask (Priority .URGENT , request .masterTimeout , listener ) {
201-                 @ Override 
202-                 public  ClusterState  doExecute (ClusterState  currentState ) {
203-                     Set <String > templateNames  = new  HashSet <>();
204-                     for  (Map .Entry <String , IndexTemplateMetadata > cursor  : currentState .metadata ().templates ().entrySet ()) {
205-                         String  templateName  = cursor .getKey ();
206-                         if  (Regex .simpleMatch (request .name , templateName )) {
207-                             templateNames .add (templateName );
208-                         }
209-                     }
210-                     if  (templateNames .isEmpty ()) {
211-                         // if its a match all pattern, and no templates are found (we have none), don't 
212-                         // fail with index missing... 
213-                         if  (Regex .isMatchAllPattern (request .name )) {
214-                             return  currentState ;
215-                         }
216-                         throw  new  IndexTemplateMissingException (request .name );
186+         clusterService .submitStateUpdateTask ("remove-index-template ["  + request .name  + "]" , new  TemplateClusterStateUpdateTask (listener ) {
187+             @ Override 
188+             public  ClusterState  execute (ClusterState  currentState ) {
189+                 Set <String > templateNames  = new  HashSet <>();
190+                 for  (Map .Entry <String , IndexTemplateMetadata > cursor  : currentState .metadata ().templates ().entrySet ()) {
191+                     String  templateName  = cursor .getKey ();
192+                     if  (Regex .simpleMatch (request .name , templateName )) {
193+                         templateNames .add (templateName );
217194                    }
218-                     Metadata .Builder  metadata  = Metadata .builder (currentState .metadata ());
219-                     for  (String  templateName  : templateNames ) {
220-                         logger .info ("removing template [{}]" , templateName );
221-                         metadata .removeTemplate (templateName );
195+                 }
196+                 if  (templateNames .isEmpty ()) {
197+                     // if its a match all pattern, and no templates are found (we have none), don't 
198+                     // fail with index missing... 
199+                     if  (Regex .isMatchAllPattern (request .name )) {
200+                         return  currentState ;
222201                    }
223-                     return   ClusterState . builder ( currentState ). metadata ( metadata ). build ( );
202+                     throw   new   IndexTemplateMissingException ( request . name );
224203                }
225-             },
226-             ClusterStateTaskConfig .build (Priority .URGENT , request .masterTimeout ),
227-             TEMPLATE_TASK_EXECUTOR 
228-         );
204+                 Metadata .Builder  metadata  = Metadata .builder (currentState .metadata ());
205+                 for  (String  templateName  : templateNames ) {
206+                     logger .info ("removing template [{}]" , templateName );
207+                     metadata .removeTemplate (templateName );
208+                 }
209+                 return  ClusterState .builder (currentState ).metadata (metadata ).build ();
210+             }
211+         }, ClusterStateTaskConfig .build (Priority .URGENT , request .masterTimeout ), TEMPLATE_TASK_EXECUTOR );
229212    }
230213
231214    /** 
@@ -242,9 +225,9 @@ public void putComponentTemplate(
242225    ) {
243226        clusterService .submitStateUpdateTask (
244227            "create-component-template ["  + name  + "], cause ["  + cause  + "]" ,
245-             new  TemplateClusterStateUpdateTask (Priority . URGENT ,  masterTimeout ,  listener ) {
228+             new  TemplateClusterStateUpdateTask (listener ) {
246229                @ Override 
247-                 public  ClusterState  doExecute (ClusterState  currentState ) throws  Exception  {
230+                 public  ClusterState  execute (ClusterState  currentState ) throws  Exception  {
248231                    return  addComponentTemplate (currentState , create , name , template );
249232                }
250233            },
@@ -404,9 +387,9 @@ public void removeComponentTemplate(
404387        validateNotInUse (state .metadata (), names );
405388        clusterService .submitStateUpdateTask (
406389            "remove-component-template ["  + String .join ("," , names ) + "]" ,
407-             new  TemplateClusterStateUpdateTask (Priority . URGENT ,  masterTimeout ,  listener ) {
390+             new  TemplateClusterStateUpdateTask (listener ) {
408391                @ Override 
409-                 public  ClusterState  doExecute (ClusterState  currentState ) {
392+                 public  ClusterState  execute (ClusterState  currentState ) {
410393                    return  innerRemoveComponentTemplate (currentState , names );
411394                }
412395            },
@@ -511,9 +494,9 @@ public void putIndexTemplateV2(
511494        validateV2TemplateRequest (clusterService .state ().metadata (), name , template );
512495        clusterService .submitStateUpdateTask (
513496            "create-index-template-v2 ["  + name  + "], cause ["  + cause  + "]" ,
514-             new  TemplateClusterStateUpdateTask (Priority . URGENT ,  masterTimeout ,  listener ) {
497+             new  TemplateClusterStateUpdateTask (listener ) {
515498                @ Override 
516-                 public  ClusterState  doExecute (ClusterState  currentState ) throws  Exception  {
499+                 public  ClusterState  execute (ClusterState  currentState ) throws  Exception  {
517500                    return  addIndexTemplateV2 (currentState , create , name , template );
518501                }
519502            },
@@ -895,9 +878,9 @@ public void removeIndexTemplateV2(
895878    ) {
896879        clusterService .submitStateUpdateTask (
897880            "remove-index-template-v2 ["  + String .join ("," , names ) + "]" ,
898-             new  TemplateClusterStateUpdateTask (Priority . URGENT ,  masterTimeout ,  listener ) {
881+             new  TemplateClusterStateUpdateTask (listener ) {
899882                @ Override 
900-                 public  ClusterState  doExecute (ClusterState  currentState ) {
883+                 public  ClusterState  execute (ClusterState  currentState ) {
901884                    return  innerRemoveIndexTemplateV2 (currentState , names );
902885                }
903886            },
@@ -1019,9 +1002,9 @@ public void putTemplate(final PutRequest request, final ActionListener<Acknowled
10191002
10201003        clusterService .submitStateUpdateTask (
10211004            "create-index-template ["  + request .name  + "], cause ["  + request .cause  + "]" ,
1022-             new  TemplateClusterStateUpdateTask (Priority . URGENT ,  request . masterTimeout ,  listener ) {
1005+             new  TemplateClusterStateUpdateTask (listener ) {
10231006                @ Override 
1024-                 public  ClusterState  doExecute (ClusterState  currentState ) throws  Exception  {
1007+                 public  ClusterState  execute (ClusterState  currentState ) throws  Exception  {
10251008                    validateTemplate (request .settings , request .mappings , indicesService );
10261009                    return  innerPutTemplate (currentState , request , templateBuilder );
10271010                }
0 commit comments