1010import org .apache .logging .log4j .LogManager ;
1111import org .apache .logging .log4j .Logger ;
1212import org .elasticsearch .action .ActionListener ;
13- import org .elasticsearch .action .ActionRequestValidationException ;
14- import org .elasticsearch .action .IndicesRequest ;
1513import org .elasticsearch .action .support .ActionFilters ;
16- import org .elasticsearch .action .support .IndicesOptions ;
17- import org .elasticsearch .action .support .master .AcknowledgedRequest ;
1814import org .elasticsearch .action .support .master .AcknowledgedResponse ;
1915import org .elasticsearch .action .support .master .TransportMasterNodeAction ;
2016import org .elasticsearch .cluster .AckedClusterStateUpdateTask ;
2622import org .elasticsearch .cluster .metadata .IndexNameExpressionResolver ;
2723import org .elasticsearch .cluster .metadata .LifecycleExecutionState ;
2824import org .elasticsearch .cluster .service .ClusterService ;
29- import org .elasticsearch .common .io .stream .StreamInput ;
30- import org .elasticsearch .common .io .stream .StreamOutput ;
3125import org .elasticsearch .common .util .concurrent .EsExecutors ;
3226import org .elasticsearch .core .SuppressForbidden ;
33- import org .elasticsearch .core .TimeValue ;
3427import org .elasticsearch .injection .guice .Inject ;
3528import org .elasticsearch .tasks .Task ;
3629import org .elasticsearch .threadpool .ThreadPool ;
3730import org .elasticsearch .transport .TransportService ;
3831import org .elasticsearch .xpack .core .ilm .Step .StepKey ;
3932import org .elasticsearch .xpack .core .ilm .action .ILMActions ;
33+ import org .elasticsearch .xpack .core .ilm .action .RetryActionRequest ;
4034import org .elasticsearch .xpack .ilm .IndexLifecycleService ;
4135
42- import java .io .IOException ;
43- import java .util .Arrays ;
44- import java .util .Objects ;
45-
46- public class TransportRetryAction extends TransportMasterNodeAction <TransportRetryAction .Request , AcknowledgedResponse > {
36+ public class TransportRetryAction extends TransportMasterNodeAction <RetryActionRequest , AcknowledgedResponse > {
4737
4838 private static final Logger logger = LogManager .getLogger (TransportRetryAction .class );
4939
@@ -64,15 +54,25 @@ public TransportRetryAction(
6454 clusterService ,
6555 threadPool ,
6656 actionFilters ,
67- Request ::new ,
57+ RetryActionRequest ::new ,
6858 AcknowledgedResponse ::readFrom ,
6959 EsExecutors .DIRECT_EXECUTOR_SERVICE
7060 );
7161 this .indexLifecycleService = indexLifecycleService ;
7262 }
7363
7464 @ Override
75- protected void masterOperation (Task task , Request request , ClusterState state , ActionListener <AcknowledgedResponse > listener ) {
65+ protected void masterOperation (
66+ Task task ,
67+ RetryActionRequest request ,
68+ ClusterState state ,
69+ ActionListener <AcknowledgedResponse > listener
70+ ) {
71+ if (request .requireError () == false ) {
72+ maybeRunAsyncAction (state , request .indices ());
73+ listener .onResponse (AcknowledgedResponse .TRUE );
74+ return ;
75+ }
7676 submitUnbatchedTask ("ilm-re-run" , new AckedClusterStateUpdateTask (request , listener ) {
7777 @ Override
7878 public ClusterState execute (ClusterState currentState ) {
@@ -81,101 +81,33 @@ public ClusterState execute(ClusterState currentState) {
8181
8282 @ Override
8383 public void clusterStateProcessed (ClusterState oldState , ClusterState newState ) {
84- for (String index : request .indices ()) {
85- IndexMetadata idxMeta = newState .metadata ().index (index );
86- LifecycleExecutionState lifecycleState = idxMeta .getLifecycleExecutionState ();
87- StepKey retryStep = new StepKey (lifecycleState .phase (), lifecycleState .action (), lifecycleState .step ());
88- if (idxMeta == null ) {
89- // The index has somehow been deleted - there shouldn't be any opportunity for this to happen, but just in case.
90- logger .debug (
91- "index ["
92- + index
93- + "] has been deleted after moving to step ["
94- + lifecycleState .step ()
95- + "], skipping async action check"
96- );
97- return ;
98- }
99- indexLifecycleService .maybeRunAsyncAction (newState , idxMeta , retryStep );
100- }
84+ maybeRunAsyncAction (newState , request .indices ());
10185 }
10286 });
10387 }
10488
89+ private void maybeRunAsyncAction (ClusterState state , String [] indices ) {
90+ for (String index : indices ) {
91+ IndexMetadata idxMeta = state .metadata ().index (index );
92+ if (idxMeta == null ) {
93+ // The index has somehow been deleted - there shouldn't be any opportunity for this to happen, but just in case.
94+ logger .debug ("index [" + index + "] has been deleted, skipping async action check" );
95+ return ;
96+ }
97+ LifecycleExecutionState lifecycleState = idxMeta .getLifecycleExecutionState ();
98+ StepKey retryStep = new StepKey (lifecycleState .phase (), lifecycleState .action (), lifecycleState .step ());
99+ indexLifecycleService .maybeRunAsyncAction (state , idxMeta , retryStep );
100+ }
101+ }
102+
105103 @ SuppressForbidden (reason = "legacy usage of unbatched task" ) // TODO add support for batching here
106104 private void submitUnbatchedTask (@ SuppressWarnings ("SameParameterValue" ) String source , ClusterStateUpdateTask task ) {
107105 clusterService .submitUnbatchedStateUpdateTask (source , task );
108106 }
109107
110108 @ Override
111- protected ClusterBlockException checkBlock (Request request , ClusterState state ) {
109+ protected ClusterBlockException checkBlock (RetryActionRequest request , ClusterState state ) {
112110 return state .blocks ().globalBlockedException (ClusterBlockLevel .METADATA_WRITE );
113111 }
114112
115- public static class Request extends AcknowledgedRequest <Request > implements IndicesRequest .Replaceable {
116- private String [] indices ;
117- private IndicesOptions indicesOptions = IndicesOptions .strictExpandOpen ();
118-
119- public Request (TimeValue masterNodeTimeout , TimeValue ackTimeout , String ... indices ) {
120- super (masterNodeTimeout , ackTimeout );
121- this .indices = indices ;
122- }
123-
124- public Request (StreamInput in ) throws IOException {
125- super (in );
126- this .indices = in .readStringArray ();
127- this .indicesOptions = IndicesOptions .readIndicesOptions (in );
128- }
129-
130- @ Override
131- public Request indices (String ... indices ) {
132- this .indices = indices ;
133- return this ;
134- }
135-
136- @ Override
137- public String [] indices () {
138- return indices ;
139- }
140-
141- @ Override
142- public IndicesOptions indicesOptions () {
143- return indicesOptions ;
144- }
145-
146- public Request indicesOptions (IndicesOptions indicesOptions ) {
147- this .indicesOptions = indicesOptions ;
148- return this ;
149- }
150-
151- @ Override
152- public ActionRequestValidationException validate () {
153- return null ;
154- }
155-
156- @ Override
157- public void writeTo (StreamOutput out ) throws IOException {
158- super .writeTo (out );
159- out .writeStringArray (indices );
160- indicesOptions .writeIndicesOptions (out );
161- }
162-
163- @ Override
164- public int hashCode () {
165- return Objects .hash (Arrays .hashCode (indices ), indicesOptions );
166- }
167-
168- @ Override
169- public boolean equals (Object obj ) {
170- if (obj == null ) {
171- return false ;
172- }
173- if (obj .getClass () != getClass ()) {
174- return false ;
175- }
176- Request other = (Request ) obj ;
177- return Objects .deepEquals (indices , other .indices ) && Objects .equals (indicesOptions , other .indicesOptions );
178- }
179-
180- }
181113}
0 commit comments