Skip to content

Commit b486386

Browse files
authored
Miscellaneous ILM cleanups (#118488) (#118498)
1 parent 9018bcd commit b486386

File tree

11 files changed

+60
-95
lines changed

11 files changed

+60
-95
lines changed

x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ilm/OperationModeUpdateTask.java

+5-1
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
import org.elasticsearch.cluster.metadata.Metadata;
1818
import org.elasticsearch.common.Priority;
1919
import org.elasticsearch.core.Nullable;
20+
import org.elasticsearch.core.Strings;
2021

2122
import static org.elasticsearch.xpack.core.ilm.LifecycleOperationMetadata.currentILMMode;
2223
import static org.elasticsearch.xpack.core.ilm.LifecycleOperationMetadata.currentSLMMode;
@@ -143,7 +144,10 @@ private ClusterState updateSLMState(final ClusterState currentState) {
143144

144145
@Override
145146
public void onFailure(Exception e) {
146-
logger.error("unable to update lifecycle metadata with new ilm mode [" + ilmMode + "], slm mode [" + slmMode + "]", e);
147+
logger.error(
148+
() -> Strings.format("unable to update lifecycle metadata with new ilm mode [%s], slm mode [%s]", ilmMode, slmMode),
149+
e
150+
);
147151
}
148152

149153
@Override

x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ilm/LifecyclePolicyTests.java

+7-11
Original file line numberDiff line numberDiff line change
@@ -151,17 +151,7 @@ public static LifecyclePolicy randomTimeseriesLifecyclePolicy(@Nullable String l
151151
// Remove the frozen phase, we'll randomly re-add it later
152152
.filter(pn -> TimeseriesLifecycleType.FROZEN_PHASE.equals(pn) == false)
153153
.collect(Collectors.toList());
154-
Map<String, Phase> phases = Maps.newMapWithExpectedSize(phaseNames.size());
155-
Function<String, Set<String>> validActions = getPhaseToValidActions();
156-
Function<String, LifecycleAction> randomAction = getNameToActionFunction();
157-
// as what actions end up in the hot phase influence what actions are allowed in the subsequent phases we'll move the hot phase
158-
// at the front of the phases to process (if it exists)
159-
if (phaseNames.contains(TimeseriesLifecycleType.HOT_PHASE)) {
160-
phaseNames.remove(TimeseriesLifecycleType.HOT_PHASE);
161-
phaseNames.add(0, TimeseriesLifecycleType.HOT_PHASE);
162-
}
163-
boolean hotPhaseContainsSearchableSnap = false;
164-
boolean coldPhaseContainsSearchableSnap = false;
154+
165155
// let's order the phases so we can reason about actions in a previous phase in order to generate a random *valid* policy
166156
List<String> orderedPhases = new ArrayList<>(phaseNames.size());
167157
for (String validPhase : TimeseriesLifecycleType.ORDERED_VALID_PHASES) {
@@ -170,6 +160,12 @@ public static LifecyclePolicy randomTimeseriesLifecyclePolicy(@Nullable String l
170160
}
171161
}
172162

163+
Map<String, Phase> phases = Maps.newMapWithExpectedSize(phaseNames.size());
164+
Function<String, Set<String>> validActions = getPhaseToValidActions();
165+
Function<String, LifecycleAction> randomAction = getNameToActionFunction();
166+
boolean hotPhaseContainsSearchableSnap = false;
167+
boolean coldPhaseContainsSearchableSnap = false;
168+
173169
TimeValue prev = null;
174170
for (String phase : orderedPhases) {
175171
TimeValue after = prev == null

x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ilm/MockAction.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222

2323
public class MockAction implements LifecycleAction {
2424
public static final String NAME = "TEST_ACTION";
25-
private List<Step> steps;
25+
private final List<Step> steps;
2626

2727
private static final ObjectParser<MockAction, Void> PARSER = new ObjectParser<>(NAME, MockAction::new);
2828
private final boolean safe;

x-pack/plugin/ilm/src/main/java/org/elasticsearch/xpack/ilm/IndexLifecycle.java

+28-43
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,6 @@
9191
import java.io.IOException;
9292
import java.time.Clock;
9393
import java.util.ArrayList;
94-
import java.util.Arrays;
9594
import java.util.Collection;
9695
import java.util.List;
9796
import java.util.function.LongSupplier;
@@ -121,7 +120,7 @@ protected Clock getClock() {
121120

122121
@Override
123122
public List<Setting<?>> getSettings() {
124-
return Arrays.asList(
123+
return List.of(
125124
LifecycleSettings.LIFECYCLE_POLL_INTERVAL_SETTING,
126125
LifecycleSettings.LIFECYCLE_NAME_SETTING,
127126
LifecycleSettings.LIFECYCLE_INDEXING_COMPLETE_SETTING,
@@ -204,7 +203,7 @@ public List<NamedXContentRegistry.Entry> getNamedXContent() {
204203
}
205204

206205
private static List<NamedXContentRegistry.Entry> xContentEntries() {
207-
return Arrays.asList(
206+
return List.of(
208207
// Custom Metadata
209208
new NamedXContentRegistry.Entry(
210209
Metadata.Custom.class,
@@ -260,52 +259,38 @@ public List<RestHandler> getRestHandlers(
260259
Supplier<DiscoveryNodes> nodesInCluster,
261260
Predicate<NodeFeature> clusterSupportsFeature
262261
) {
263-
List<RestHandler> handlers = new ArrayList<>();
264-
265-
handlers.addAll(
266-
Arrays.asList(
267-
// add ILM rest handlers
268-
new RestPutLifecycleAction(),
269-
new RestGetLifecycleAction(),
270-
new RestDeleteLifecycleAction(),
271-
new RestExplainLifecycleAction(),
272-
new RestRemoveIndexLifecyclePolicyAction(),
273-
new RestMoveToStepAction(),
274-
new RestRetryAction(),
275-
new RestStopAction(),
276-
new RestStartILMAction(),
277-
new RestGetStatusAction(),
278-
new RestMigrateToDataTiersAction()
279-
)
262+
return List.of(
263+
new RestPutLifecycleAction(),
264+
new RestGetLifecycleAction(),
265+
new RestDeleteLifecycleAction(),
266+
new RestExplainLifecycleAction(),
267+
new RestRemoveIndexLifecyclePolicyAction(),
268+
new RestMoveToStepAction(),
269+
new RestRetryAction(),
270+
new RestStopAction(),
271+
new RestStartILMAction(),
272+
new RestGetStatusAction(),
273+
new RestMigrateToDataTiersAction()
280274
);
281-
return handlers;
282275
}
283276

284277
@Override
285278
public List<ActionHandler<? extends ActionRequest, ? extends ActionResponse>> getActions() {
286-
var ilmUsageAction = new ActionHandler<>(XPackUsageFeatureAction.INDEX_LIFECYCLE, IndexLifecycleUsageTransportAction.class);
287-
var ilmInfoAction = new ActionHandler<>(XPackInfoFeatureAction.INDEX_LIFECYCLE, IndexLifecycleInfoTransportAction.class);
288-
var migrateToDataTiersAction = new ActionHandler<>(MigrateToDataTiersAction.INSTANCE, TransportMigrateToDataTiersAction.class);
289-
List<ActionHandler<? extends ActionRequest, ? extends ActionResponse>> actions = new ArrayList<>();
290-
actions.add(ilmUsageAction);
291-
actions.add(ilmInfoAction);
292-
actions.add(migrateToDataTiersAction);
293-
actions.addAll(
294-
Arrays.asList(
295-
// add ILM actions
296-
new ActionHandler<>(ILMActions.PUT, TransportPutLifecycleAction.class),
297-
new ActionHandler<>(GetLifecycleAction.INSTANCE, TransportGetLifecycleAction.class),
298-
new ActionHandler<>(DeleteLifecycleAction.INSTANCE, TransportDeleteLifecycleAction.class),
299-
new ActionHandler<>(ExplainLifecycleAction.INSTANCE, TransportExplainLifecycleAction.class),
300-
new ActionHandler<>(RemoveIndexLifecyclePolicyAction.INSTANCE, TransportRemoveIndexLifecyclePolicyAction.class),
301-
new ActionHandler<>(ILMActions.MOVE_TO_STEP, TransportMoveToStepAction.class),
302-
new ActionHandler<>(ILMActions.RETRY, TransportRetryAction.class),
303-
new ActionHandler<>(ILMActions.START, TransportStartILMAction.class),
304-
new ActionHandler<>(ILMActions.STOP, TransportStopILMAction.class),
305-
new ActionHandler<>(GetStatusAction.INSTANCE, TransportGetStatusAction.class)
306-
)
279+
return List.of(
280+
new ActionHandler<>(XPackUsageFeatureAction.INDEX_LIFECYCLE, IndexLifecycleUsageTransportAction.class),
281+
new ActionHandler<>(XPackInfoFeatureAction.INDEX_LIFECYCLE, IndexLifecycleInfoTransportAction.class),
282+
new ActionHandler<>(MigrateToDataTiersAction.INSTANCE, TransportMigrateToDataTiersAction.class),
283+
new ActionHandler<>(ILMActions.PUT, TransportPutLifecycleAction.class),
284+
new ActionHandler<>(GetLifecycleAction.INSTANCE, TransportGetLifecycleAction.class),
285+
new ActionHandler<>(DeleteLifecycleAction.INSTANCE, TransportDeleteLifecycleAction.class),
286+
new ActionHandler<>(ExplainLifecycleAction.INSTANCE, TransportExplainLifecycleAction.class),
287+
new ActionHandler<>(RemoveIndexLifecyclePolicyAction.INSTANCE, TransportRemoveIndexLifecyclePolicyAction.class),
288+
new ActionHandler<>(ILMActions.MOVE_TO_STEP, TransportMoveToStepAction.class),
289+
new ActionHandler<>(ILMActions.RETRY, TransportRetryAction.class),
290+
new ActionHandler<>(ILMActions.START, TransportStartILMAction.class),
291+
new ActionHandler<>(ILMActions.STOP, TransportStopILMAction.class),
292+
new ActionHandler<>(GetStatusAction.INSTANCE, TransportGetStatusAction.class)
307293
);
308-
return actions;
309294
}
310295

311296
List<ReservedClusterStateHandler<?>> reservedClusterStateHandlers() {

x-pack/plugin/ilm/src/main/java/org/elasticsearch/xpack/ilm/IndexLifecycleRunner.java

+6-25
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
import org.elasticsearch.cluster.service.ClusterService;
1919
import org.elasticsearch.cluster.service.MasterServiceTaskQueue;
2020
import org.elasticsearch.common.Priority;
21+
import org.elasticsearch.common.Strings;
2122
import org.elasticsearch.core.Nullable;
2223
import org.elasticsearch.core.SuppressForbidden;
2324
import org.elasticsearch.core.TimeValue;
@@ -39,7 +40,6 @@
3940

4041
import java.util.Collections;
4142
import java.util.HashSet;
42-
import java.util.Locale;
4343
import java.util.Objects;
4444
import java.util.Set;
4545
import java.util.function.LongSupplier;
@@ -290,13 +290,7 @@ void onErrorMaybeRetryFailedStep(String policy, StepKey currentStep, IndexMetada
290290
// IndexLifecycleRunner#runPeriodicStep} run the policy will still be in the ERROR step, as we haven't been able
291291
// to move it back into the failed step, so we'll try again
292292
submitUnlessAlreadyQueued(
293-
String.format(
294-
Locale.ROOT,
295-
"ilm-retry-failed-step {policy [%s], index [%s], failedStep [%s]}",
296-
policy,
297-
index,
298-
failedStep.getKey()
299-
),
293+
Strings.format("ilm-retry-failed-step {policy [%s], index [%s], failedStep [%s]}", policy, index, failedStep.getKey()),
300294
new MoveToRetryFailedStepUpdateTask(indexMetadata.getIndex(), policy, currentStep, failedStep)
301295
);
302296
} else {
@@ -444,7 +438,7 @@ void runPolicyAfterStateChange(String policy, IndexMetadata indexMetadata) {
444438
} else if (currentStep instanceof ClusterStateActionStep || currentStep instanceof ClusterStateWaitStep) {
445439
logger.debug("[{}] running policy with current-step [{}]", indexMetadata.getIndex().getName(), currentStep.getKey());
446440
submitUnlessAlreadyQueued(
447-
String.format(Locale.ROOT, "ilm-execute-cluster-state-steps [%s]", currentStep),
441+
Strings.format("ilm-execute-cluster-state-steps [%s]", currentStep),
448442
new ExecuteStepsUpdateTask(policy, indexMetadata.getIndex(), currentStep, stepRegistry, this, nowSupplier)
449443
);
450444
} else {
@@ -459,8 +453,7 @@ void runPolicyAfterStateChange(String policy, IndexMetadata indexMetadata) {
459453
private void moveToStep(Index index, String policy, Step.StepKey currentStepKey, Step.StepKey newStepKey) {
460454
logger.debug("[{}] moving to step [{}] {} -> {}", index.getName(), policy, currentStepKey, newStepKey);
461455
submitUnlessAlreadyQueued(
462-
String.format(
463-
Locale.ROOT,
456+
Strings.format(
464457
"ilm-move-to-step {policy [%s], index [%s], currentStep [%s], nextStep [%s]}",
465458
policy,
466459
index.getName(),
@@ -486,13 +479,7 @@ private void moveToErrorStep(Index index, String policy, Step.StepKey currentSte
486479
e
487480
);
488481
submitUnlessAlreadyQueued(
489-
String.format(
490-
Locale.ROOT,
491-
"ilm-move-to-error-step {policy [%s], index [%s], currentStep [%s]}",
492-
policy,
493-
index.getName(),
494-
currentStepKey
495-
),
482+
Strings.format("ilm-move-to-error-step {policy [%s], index [%s], currentStep [%s]}", policy, index.getName(), currentStepKey),
496483
new MoveToErrorStepUpdateTask(index, policy, currentStepKey, e, nowSupplier, stepRegistry::getStep, clusterState -> {
497484
IndexMetadata indexMetadata = clusterState.metadata().index(index);
498485
registerFailedOperation(indexMetadata, e);
@@ -506,13 +493,7 @@ private void moveToErrorStep(Index index, String policy, Step.StepKey currentSte
506493
*/
507494
private void setStepInfo(Index index, String policy, @Nullable Step.StepKey currentStepKey, ToXContentObject stepInfo) {
508495
submitUnlessAlreadyQueued(
509-
String.format(
510-
Locale.ROOT,
511-
"ilm-set-step-info {policy [%s], index [%s], currentStep [%s]}",
512-
policy,
513-
index.getName(),
514-
currentStepKey
515-
),
496+
Strings.format("ilm-set-step-info {policy [%s], index [%s], currentStep [%s]}", policy, index.getName(), currentStepKey),
516497
new SetStepInfoUpdateTask(index, policy, currentStepKey, stepInfo)
517498
);
518499
}

x-pack/plugin/ilm/src/main/java/org/elasticsearch/xpack/ilm/IndexLifecycleService.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -354,7 +354,7 @@ private void cancelJob() {
354354
@Override
355355
public void triggered(SchedulerEngine.Event event) {
356356
if (event.jobName().equals(XPackField.INDEX_LIFECYCLE)) {
357-
logger.trace("job triggered: " + event.jobName() + ", " + event.scheduledTime() + ", " + event.triggeredTime());
357+
logger.trace("job triggered: {}, {}, {}", event.jobName(), event.scheduledTime(), event.triggeredTime());
358358
triggerPolicies(clusterService.state(), false);
359359
}
360360
}

x-pack/plugin/ilm/src/main/java/org/elasticsearch/xpack/ilm/PolicyStepsRegistry.java

+3-4
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
import org.elasticsearch.cluster.DiffableUtils;
1515
import org.elasticsearch.cluster.metadata.IndexMetadata;
1616
import org.elasticsearch.cluster.metadata.Metadata;
17+
import org.elasticsearch.common.Strings;
1718
import org.elasticsearch.common.io.stream.StreamInput;
1819
import org.elasticsearch.common.io.stream.StreamOutput;
1920
import org.elasticsearch.core.Nullable;
@@ -42,7 +43,6 @@
4243
import java.util.HashMap;
4344
import java.util.LinkedHashMap;
4445
import java.util.List;
45-
import java.util.Locale;
4646
import java.util.Map;
4747
import java.util.Objects;
4848
import java.util.Set;
@@ -269,9 +269,8 @@ public Set<Step.StepKey> parseStepKeysFromPhase(String policy, String currentPha
269269
return parseStepsFromPhase(policy, currentPhase, phaseDefNonNull).stream().map(Step::getKey).collect(Collectors.toSet());
270270
} catch (IOException e) {
271271
logger.trace(
272-
() -> String.format(
273-
Locale.ROOT,
274-
"unable to parse steps for policy [{}], phase [{}], and phase definition [{}]",
272+
() -> Strings.format(
273+
"unable to parse steps for policy [%s], phase [%s], and phase definition [%s]",
275274
policy,
276275
currentPhase,
277276
phaseDef

x-pack/plugin/ilm/src/main/java/org/elasticsearch/xpack/ilm/action/TransportMigrateToDataTiersAction.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,7 @@ public void onFailure(Exception e) {
145145

146146
@Override
147147
public void clusterStateProcessed(ClusterState oldState, ClusterState newState) {
148-
rerouteService.reroute("cluster migrated to data tiers routing", Priority.NORMAL, new ActionListener<Void>() {
148+
rerouteService.reroute("cluster migrated to data tiers routing", Priority.NORMAL, new ActionListener<>() {
149149
@Override
150150
public void onResponse(Void ignored) {}
151151

x-pack/plugin/ilm/src/main/java/org/elasticsearch/xpack/ilm/history/ILMHistoryStore.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ public class ILMHistoryStore implements Closeable {
5858

5959
public static final String ILM_HISTORY_DATA_STREAM = "ilm-history-" + INDEX_TEMPLATE_VERSION;
6060

61-
private static int ILM_HISTORY_BULK_SIZE = StrictMath.toIntExact(
61+
private static final int ILM_HISTORY_BULK_SIZE = StrictMath.toIntExact(
6262
ByteSizeValue.parseBytesSizeValue(
6363
System.getProperty("es.indices.lifecycle.history.bulk.size", "50MB"),
6464
"es.indices.lifecycle.history.bulk.size"

x-pack/plugin/ilm/src/test/java/org/elasticsearch/xpack/ilm/IndexLifecycleTransitionTests.java

+5-5
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ public class IndexLifecycleTransitionTests extends ESTestCase {
7272
public void testMoveClusterStateToNextStep() {
7373
String indexName = "my_index";
7474
LifecyclePolicy policy = randomValueOtherThanMany(
75-
p -> p.getPhases().size() == 0,
75+
p -> p.getPhases().isEmpty(),
7676
() -> LifecyclePolicyTests.randomTestLifecyclePolicy("policy")
7777
);
7878
Phase nextPhase = policy.getPhases()
@@ -125,7 +125,7 @@ public void testMoveClusterStateToNextStep() {
125125
public void testMoveClusterStateToNextStepSamePhase() {
126126
String indexName = "my_index";
127127
LifecyclePolicy policy = randomValueOtherThanMany(
128-
p -> p.getPhases().size() == 0,
128+
p -> p.getPhases().isEmpty(),
129129
() -> LifecyclePolicyTests.randomTestLifecyclePolicy("policy")
130130
);
131131
List<LifecyclePolicyMetadata> policyMetadatas = Collections.singletonList(
@@ -176,7 +176,7 @@ public void testMoveClusterStateToNextStepSamePhase() {
176176
public void testMoveClusterStateToNextStepSameAction() {
177177
String indexName = "my_index";
178178
LifecyclePolicy policy = randomValueOtherThanMany(
179-
p -> p.getPhases().size() == 0,
179+
p -> p.getPhases().isEmpty(),
180180
() -> LifecyclePolicyTests.randomTestLifecyclePolicy("policy")
181181
);
182182
List<LifecyclePolicyMetadata> policyMetadatas = Collections.singletonList(
@@ -228,7 +228,7 @@ public void testSuccessfulValidatedMoveClusterStateToNextStep() {
228228
String indexName = "my_index";
229229
String policyName = "my_policy";
230230
LifecyclePolicy policy = randomValueOtherThanMany(
231-
p -> p.getPhases().size() == 0,
231+
p -> p.getPhases().isEmpty(),
232232
() -> LifecyclePolicyTests.randomTestLifecyclePolicy(policyName)
233233
);
234234
Phase nextPhase = policy.getPhases()
@@ -1436,6 +1436,6 @@ private void assertClusterStateStepInfo(
14361436
assertEquals(expectedstepInfoValue, newLifecycleState.stepInfo());
14371437
assertEquals(oldLifecycleState.phaseTime(), newLifecycleState.phaseTime());
14381438
assertEquals(oldLifecycleState.actionTime(), newLifecycleState.actionTime());
1439-
assertEquals(newLifecycleState.stepTime(), newLifecycleState.stepTime());
1439+
assertEquals(oldLifecycleState.stepTime(), newLifecycleState.stepTime());
14401440
}
14411441
}

x-pack/plugin/migrate/src/main/java/org/elasticsearch/xpack/migrate/action/GetMigrationReindexStatusTransportAction.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ void getRunningTaskFromNode(String persistentTaskId, ActionListener<Response> li
8888
listener.onFailure(
8989
new ResourceNotFoundException(
9090
Strings.format(
91-
"Persistent task [{}] is supposed to be running on node [{}], " + "but the task is not found on that node",
91+
"Persistent task [%s] is supposed to be running on node [%s], but the task is not found on that node",
9292
persistentTaskId,
9393
clusterService.localNode().getId()
9494
)
@@ -106,7 +106,7 @@ private void runOnNodeWithTaskIfPossible(Task thisTask, Request request, String
106106
listener.onFailure(
107107
new ResourceNotFoundException(
108108
Strings.format(
109-
"Persistent task [{}] is supposed to be running on node [{}], but that node is not part of the cluster",
109+
"Persistent task [%s] is supposed to be running on node [%s], but that node is not part of the cluster",
110110
request.getIndex(),
111111
nodeId
112112
)

0 commit comments

Comments
 (0)