@@ -109,61 +109,85 @@ protected void masterOperation(Task task, Request request, ClusterState state, A
109
109
}
110
110
}
111
111
112
- submitUnbatchedTask ("put-lifecycle-" + request .getPolicy ().getName (), new AckedClusterStateUpdateTask (request , listener ) {
113
- @ Override
114
- public ClusterState execute (ClusterState currentState ) throws Exception {
115
- final IndexLifecycleMetadata currentMetadata = currentState .metadata ()
116
- .custom (IndexLifecycleMetadata .TYPE , IndexLifecycleMetadata .EMPTY );
117
- final LifecyclePolicyMetadata existingPolicyMetadata = currentMetadata .getPolicyMetadatas ()
118
- .get (request .getPolicy ().getName ());
112
+ submitUnbatchedTask (
113
+ "put-lifecycle-" + request .getPolicy ().getName (),
114
+ new UpdateLifecyclePolicyTask (request , listener , licenseState , filteredHeaders , xContentRegistry , client )
115
+ );
116
+ }
119
117
120
- // Double-check for no-op in the state update task, in case it was changed/reset in the meantime
121
- if (isNoopUpdate (existingPolicyMetadata , request .getPolicy (), filteredHeaders )) {
122
- return currentState ;
123
- }
118
+ public static class UpdateLifecyclePolicyTask extends AckedClusterStateUpdateTask {
119
+ private final Request request ;
120
+ private final XPackLicenseState licenseState ;
121
+ private final Map <String , String > filteredHeaders ;
122
+ private final NamedXContentRegistry xContentRegistry ;
123
+ private final Client client ;
124
124
125
- validatePrerequisites (request .getPolicy (), currentState );
125
+ public UpdateLifecyclePolicyTask (
126
+ Request request ,
127
+ ActionListener <AcknowledgedResponse > listener ,
128
+ XPackLicenseState licenseState ,
129
+ Map <String , String > filteredHeaders ,
130
+ NamedXContentRegistry xContentRegistry ,
131
+ Client client
132
+ ) {
133
+ super (request , listener );
134
+ this .request = request ;
135
+ this .licenseState = licenseState ;
136
+ this .filteredHeaders = filteredHeaders ;
137
+ this .xContentRegistry = xContentRegistry ;
138
+ this .client = client ;
139
+ }
126
140
127
- ClusterState .Builder stateBuilder = ClusterState .builder (currentState );
128
- long nextVersion = (existingPolicyMetadata == null ) ? 1L : existingPolicyMetadata .getVersion () + 1L ;
129
- SortedMap <String , LifecyclePolicyMetadata > newPolicies = new TreeMap <>(currentMetadata .getPolicyMetadatas ());
130
- LifecyclePolicyMetadata lifecyclePolicyMetadata = new LifecyclePolicyMetadata (
131
- request .getPolicy (),
132
- filteredHeaders ,
133
- nextVersion ,
134
- Instant .now ().toEpochMilli ()
135
- );
136
- LifecyclePolicyMetadata oldPolicy = newPolicies .put (lifecyclePolicyMetadata .getName (), lifecyclePolicyMetadata );
137
- if (oldPolicy == null ) {
138
- logger .info ("adding index lifecycle policy [{}]" , request .getPolicy ().getName ());
139
- } else {
140
- logger .info ("updating index lifecycle policy [{}]" , request .getPolicy ().getName ());
141
- }
142
- IndexLifecycleMetadata newMetadata = new IndexLifecycleMetadata (newPolicies , currentMetadata .getOperationMode ());
143
- stateBuilder .metadata (
144
- Metadata .builder (currentState .getMetadata ()).putCustom (IndexLifecycleMetadata .TYPE , newMetadata ).build ()
145
- );
146
- ClusterState nonRefreshedState = stateBuilder .build ();
147
- if (oldPolicy == null ) {
141
+ @ Override
142
+ public ClusterState execute (ClusterState currentState ) throws Exception {
143
+ final IndexLifecycleMetadata currentMetadata = currentState .metadata ()
144
+ .custom (IndexLifecycleMetadata .TYPE , IndexLifecycleMetadata .EMPTY );
145
+ final LifecyclePolicyMetadata existingPolicyMetadata = currentMetadata .getPolicyMetadatas ().get (request .getPolicy ().getName ());
146
+
147
+ // Double-check for no-op in the state update task, in case it was changed/reset in the meantime
148
+ if (isNoopUpdate (existingPolicyMetadata , request .getPolicy (), filteredHeaders )) {
149
+ return currentState ;
150
+ }
151
+
152
+ validatePrerequisites (request .getPolicy (), currentState , licenseState );
153
+
154
+ ClusterState .Builder stateBuilder = ClusterState .builder (currentState );
155
+ long nextVersion = (existingPolicyMetadata == null ) ? 1L : existingPolicyMetadata .getVersion () + 1L ;
156
+ SortedMap <String , LifecyclePolicyMetadata > newPolicies = new TreeMap <>(currentMetadata .getPolicyMetadatas ());
157
+ LifecyclePolicyMetadata lifecyclePolicyMetadata = new LifecyclePolicyMetadata (
158
+ request .getPolicy (),
159
+ filteredHeaders ,
160
+ nextVersion ,
161
+ Instant .now ().toEpochMilli ()
162
+ );
163
+ LifecyclePolicyMetadata oldPolicy = newPolicies .put (lifecyclePolicyMetadata .getName (), lifecyclePolicyMetadata );
164
+ if (oldPolicy == null ) {
165
+ logger .info ("adding index lifecycle policy [{}]" , request .getPolicy ().getName ());
166
+ } else {
167
+ logger .info ("updating index lifecycle policy [{}]" , request .getPolicy ().getName ());
168
+ }
169
+ IndexLifecycleMetadata newMetadata = new IndexLifecycleMetadata (newPolicies , currentMetadata .getOperationMode ());
170
+ stateBuilder .metadata (Metadata .builder (currentState .getMetadata ()).putCustom (IndexLifecycleMetadata .TYPE , newMetadata ).build ());
171
+ ClusterState nonRefreshedState = stateBuilder .build ();
172
+ if (oldPolicy == null ) {
173
+ return nonRefreshedState ;
174
+ } else {
175
+ try {
176
+ return updateIndicesForPolicy (
177
+ nonRefreshedState ,
178
+ xContentRegistry ,
179
+ client ,
180
+ oldPolicy .getPolicy (),
181
+ lifecyclePolicyMetadata ,
182
+ licenseState
183
+ );
184
+ } catch (Exception e ) {
185
+ logger .warn (() -> "unable to refresh indices phase JSON for updated policy [" + oldPolicy .getName () + "]" , e );
186
+ // Revert to the non-refreshed state
148
187
return nonRefreshedState ;
149
- } else {
150
- try {
151
- return updateIndicesForPolicy (
152
- nonRefreshedState ,
153
- xContentRegistry ,
154
- client ,
155
- oldPolicy .getPolicy (),
156
- lifecyclePolicyMetadata ,
157
- licenseState
158
- );
159
- } catch (Exception e ) {
160
- logger .warn (() -> "unable to refresh indices phase JSON for updated policy [" + oldPolicy .getName () + "]" , e );
161
- // Revert to the non-refreshed state
162
- return nonRefreshedState ;
163
- }
164
188
}
165
189
}
166
- });
190
+ }
167
191
}
168
192
169
193
@ SuppressForbidden (reason = "legacy usage of unbatched task" ) // TODO add support for batching here
@@ -193,7 +217,7 @@ static boolean isNoopUpdate(
193
217
* @param policy The lifecycle policy
194
218
* @param state The cluster state
195
219
*/
196
- private void validatePrerequisites (LifecyclePolicy policy , ClusterState state ) {
220
+ private static void validatePrerequisites (LifecyclePolicy policy , ClusterState state , XPackLicenseState licenseState ) {
197
221
List <Phase > phasesWithSearchableSnapshotActions = policy .getPhases ()
198
222
.values ()
199
223
.stream ()
0 commit comments