Skip to content

Commit fa3e9bb

Browse files
committed
Merge remote-tracking branch 'origin/main' into tracking-postings-memory-bytes-remove-ff
2 parents 7cc295c + b72792d commit fa3e9bb

File tree

10 files changed

+198
-108
lines changed

10 files changed

+198
-108
lines changed

docs/changelog/133403.yaml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
pr: 133403
2+
summary: Fix service destination template file name
3+
area: Data streams
4+
type: bug
5+
issues: []
Lines changed: 34 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -19,15 +19,27 @@
1919
import org.elasticsearch.core.TimeValue;
2020
import org.elasticsearch.index.IndexNotFoundException;
2121

22+
import java.util.function.BiFunction;
23+
2224
/**
23-
* Deletes the index identified by the shrink index name stored in the lifecycle state of the managed index (if any was generated)
25+
* Deletes the index identified by the index name supplier.
2426
*/
25-
public class CleanupShrinkIndexStep extends AsyncRetryDuringSnapshotActionStep {
26-
public static final String NAME = "cleanup-shrink-index";
27-
private static final Logger logger = LogManager.getLogger(CleanupShrinkIndexStep.class);
27+
public class CleanupGeneratedIndexStep extends AsyncRetryDuringSnapshotActionStep {
28+
29+
public static final String NAME = "cleanup-generated-index";
30+
31+
private static final Logger logger = LogManager.getLogger(CleanupGeneratedIndexStep.class);
2832

29-
public CleanupShrinkIndexStep(StepKey key, StepKey nextStepKey, Client client) {
33+
private final BiFunction<String, LifecycleExecutionState, String> targetIndexNameSupplier;
34+
35+
public CleanupGeneratedIndexStep(
36+
StepKey key,
37+
StepKey nextStepKey,
38+
Client client,
39+
BiFunction<String, LifecycleExecutionState, String> targetIndexNameSupplier
40+
) {
3041
super(key, nextStepKey, client);
42+
this.targetIndexNameSupplier = targetIndexNameSupplier;
3143
}
3244

3345
@Override
@@ -37,40 +49,41 @@ public boolean isRetryable() {
3749

3850
@Override
3951
void performDuringNoSnapshot(IndexMetadata indexMetadata, ProjectMetadata currentProject, ActionListener<Void> listener) {
40-
final String shrunkenIndexSource = IndexMetadata.INDEX_RESIZE_SOURCE_NAME.get(indexMetadata.getSettings());
41-
if (Strings.isNullOrEmpty(shrunkenIndexSource) == false) {
42-
// the current managed index is a shrunk index
43-
if (currentProject.index(shrunkenIndexSource) == null) {
44-
// if the source index does not exist, we'll skip deleting the
45-
// (managed) shrunk index as that will cause data loss
52+
// If the index was generated by a resize operation, and the source index does not exist anymore, we skip the deletion to avoid
53+
// data loss.
54+
final String generatedIndexSource = IndexMetadata.INDEX_RESIZE_SOURCE_NAME.get(indexMetadata.getSettings());
55+
if (Strings.hasText(generatedIndexSource)) {
56+
if (currentProject.index(generatedIndexSource) == null) {
4657
String policyName = indexMetadata.getLifecyclePolicyName();
4758
logger.warn(
4859
"managed index [{}] as part of policy [{}] is a shrunk index and the source index [{}] does not exist "
4960
+ "anymore. will skip the [{}] step",
5061
indexMetadata.getIndex().getName(),
5162
policyName,
52-
shrunkenIndexSource,
63+
generatedIndexSource,
5364
NAME
5465
);
5566
listener.onResponse(null);
5667
return;
5768
}
5869
}
5970

60-
LifecycleExecutionState lifecycleState = indexMetadata.getLifecycleExecutionState();
61-
final String shrinkIndexName = lifecycleState.shrinkIndexName();
62-
// if the shrink index was not generated there is nothing to delete so we move on
63-
if (Strings.hasText(shrinkIndexName) == false) {
71+
final String targetIndexName = targetIndexNameSupplier.apply(
72+
indexMetadata.getIndex().getName(),
73+
indexMetadata.getLifecycleExecutionState()
74+
);
75+
// If no index name was generated, there is nothing for us to delete, so we can move on
76+
if (Strings.hasText(targetIndexName) == false) {
6477
listener.onResponse(null);
6578
return;
6679
}
6780
getClient(currentProject.id()).admin()
6881
.indices()
69-
.delete(new DeleteIndexRequest(shrinkIndexName).masterNodeTimeout(TimeValue.MAX_VALUE), new ActionListener<>() {
82+
.delete(new DeleteIndexRequest(targetIndexName).masterNodeTimeout(TimeValue.MAX_VALUE), new ActionListener<>() {
7083
@Override
7184
public void onResponse(AcknowledgedResponse acknowledgedResponse) {
7285
// even if not all nodes acked the delete request yet we can consider this operation as successful as
73-
// we'll generate a new index name and attempt to shrink into the newly generated name
86+
// we'll generate a new index name and attempt to create a new index with the newly generated name
7487
listener.onResponse(null);
7588
}
7689

@@ -86,4 +99,7 @@ public void onFailure(Exception e) {
8699
});
87100
}
88101

102+
public BiFunction<String, LifecycleExecutionState, String> getTargetIndexNameSupplier() {
103+
return targetIndexNameSupplier;
104+
}
89105
}

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

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ public class ShrinkAction implements LifecycleAction {
4646
public static final ParseField MAX_PRIMARY_SHARD_SIZE = new ParseField("max_primary_shard_size");
4747
public static final ParseField ALLOW_WRITE_AFTER_SHRINK = new ParseField("allow_write_after_shrink");
4848
public static final String CONDITIONAL_SKIP_SHRINK_STEP = BranchingStep.NAME + "-check-prerequisites";
49+
public static final String CLEANUP_SHRINK_INDEX_STEP = "cleanup-shrink-index";
4950
public static final String CONDITIONAL_DATASTREAM_CHECK_KEY = BranchingStep.NAME + "-on-datastream-check";
5051

5152
private static final ConstructingObjectParser<ShrinkAction, Void> PARSER = new ConstructingObjectParser<>(
@@ -168,7 +169,7 @@ public List<Step> toSteps(Client client, String phase, Step.StepKey nextStepKey)
168169
StepKey waitTimeSeriesEndTimePassesKey = new StepKey(phase, NAME, WaitUntilTimeSeriesEndTimePassesStep.NAME);
169170
StepKey readOnlyKey = new StepKey(phase, NAME, ReadOnlyAction.NAME);
170171
StepKey checkTargetShardsCountKey = new StepKey(phase, NAME, CheckTargetShardsCountStep.NAME);
171-
StepKey cleanupShrinkIndexKey = new StepKey(phase, NAME, CleanupShrinkIndexStep.NAME);
172+
StepKey cleanupShrinkIndexKey = new StepKey(phase, NAME, CLEANUP_SHRINK_INDEX_STEP);
172173
StepKey generateShrinkIndexNameKey = new StepKey(phase, NAME, GenerateUniqueIndexNameStep.NAME);
173174
StepKey setSingleNodeKey = new StepKey(phase, NAME, SetSingleNodeAllocateStep.NAME);
174175
StepKey allocationRoutedKey = new StepKey(phase, NAME, CheckShrinkReadyStep.NAME);
@@ -243,10 +244,11 @@ public List<Step> toSteps(Client client, String phase, Step.StepKey nextStepKey)
243244
// We generate a unique shrink index name but we also retry if the allocation of the shrunk index is not possible, so we want to
244245
// delete the "previously generated" shrink index (this is a no-op if it's the first run of the action and we haven't generated a
245246
// shrink index name)
246-
CleanupShrinkIndexStep cleanupShrinkIndexStep = new CleanupShrinkIndexStep(
247+
CleanupGeneratedIndexStep cleanupShrinkIndexStep = new CleanupGeneratedIndexStep(
247248
cleanupShrinkIndexKey,
248249
generateShrinkIndexNameKey,
249-
client
250+
client,
251+
ShrinkIndexNameSupplier::getShrinkIndexName
250252
);
251253
// generate a unique shrink index name and store it in the ILM execution state
252254
GenerateUniqueIndexNameStep generateUniqueIndexNameStep = new GenerateUniqueIndexNameStep(
Lines changed: 20 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -28,30 +28,35 @@
2828
import static org.hamcrest.Matchers.arrayContaining;
2929
import static org.hamcrest.Matchers.is;
3030

31-
public class CleanupShrinkIndexStepTests extends AbstractStepTestCase<CleanupShrinkIndexStep> {
31+
public class CleanupGeneratedIndexStepTests extends AbstractStepTestCase<CleanupGeneratedIndexStep> {
3232

3333
@Override
34-
public CleanupShrinkIndexStep createRandomInstance() {
34+
public CleanupGeneratedIndexStep createRandomInstance() {
3535
StepKey stepKey = randomStepKey();
3636
StepKey nextStepKey = randomStepKey();
37-
return new CleanupShrinkIndexStep(stepKey, nextStepKey, client);
37+
return new CleanupGeneratedIndexStep(stepKey, nextStepKey, client, (index, state) -> randomAlphaOfLength(5) + index);
3838
}
3939

4040
@Override
41-
protected CleanupShrinkIndexStep copyInstance(CleanupShrinkIndexStep instance) {
42-
return new CleanupShrinkIndexStep(instance.getKey(), instance.getNextStepKey(), instance.getClientWithoutProject());
41+
protected CleanupGeneratedIndexStep copyInstance(CleanupGeneratedIndexStep instance) {
42+
return new CleanupGeneratedIndexStep(
43+
instance.getKey(),
44+
instance.getNextStepKey(),
45+
instance.getClientWithoutProject(),
46+
instance.getTargetIndexNameSupplier()
47+
);
4348
}
4449

4550
@Override
46-
public CleanupShrinkIndexStep mutateInstance(CleanupShrinkIndexStep instance) {
51+
public CleanupGeneratedIndexStep mutateInstance(CleanupGeneratedIndexStep instance) {
4752
StepKey key = instance.getKey();
4853
StepKey nextKey = instance.getNextStepKey();
4954
switch (between(0, 1)) {
5055
case 0 -> key = new StepKey(key.phase(), key.action(), key.name() + randomAlphaOfLength(5));
5156
case 1 -> nextKey = new StepKey(nextKey.phase(), nextKey.action(), nextKey.name() + randomAlphaOfLength(5));
5257
default -> throw new AssertionError("Illegal randomisation branch");
5358
}
54-
return new CleanupShrinkIndexStep(key, nextKey, instance.getClientWithoutProject());
59+
return new CleanupGeneratedIndexStep(key, nextKey, instance.getClientWithoutProject(), instance.getTargetIndexNameSupplier());
5560
}
5661

5762
public void testPerformActionDoesntFailIfShrinkingIndexNameIsMissing() {
@@ -67,7 +72,7 @@ public void testPerformActionDoesntFailIfShrinkingIndexNameIsMissing() {
6772

6873
ProjectState state = projectStateFromProject(ProjectMetadata.builder(randomProjectIdOrDefault()).put(indexMetadata, true));
6974

70-
CleanupShrinkIndexStep cleanupShrinkIndexStep = createRandomInstance();
75+
CleanupGeneratedIndexStep cleanupShrinkIndexStep = createRandomInstance();
7176
cleanupShrinkIndexStep.performAction(indexMetadata, state, null, new ActionListener<>() {
7277
@Override
7378
public void onResponse(Void unused) {}
@@ -100,7 +105,12 @@ public void testPerformAction() {
100105

101106
try (var threadPool = createThreadPool()) {
102107
final var client = getDeleteIndexRequestAssertingClient(threadPool, shrinkIndexName);
103-
CleanupShrinkIndexStep step = new CleanupShrinkIndexStep(randomStepKey(), randomStepKey(), client);
108+
CleanupGeneratedIndexStep step = new CleanupGeneratedIndexStep(
109+
randomStepKey(),
110+
randomStepKey(),
111+
client,
112+
ShrinkIndexNameSupplier::getShrinkIndexName
113+
);
104114
step.performAction(indexMetadata, state, null, ActionListener.noop());
105115
}
106116
}
@@ -125,7 +135,7 @@ public void testDeleteSkippedIfManagedIndexIsShrunkAndSourceDoesntExist() {
125135

126136
try (var threadPool = createThreadPool()) {
127137
final var client = getFailingIfCalledClient(threadPool);
128-
CleanupShrinkIndexStep step = new CleanupShrinkIndexStep(randomStepKey(), randomStepKey(), client);
138+
CleanupGeneratedIndexStep step = new CleanupGeneratedIndexStep(randomStepKey(), randomStepKey(), client, (index, s) -> index);
129139
step.performAction(shrunkIndexMetadata, state, null, ActionListener.noop());
130140
}
131141
}

0 commit comments

Comments
 (0)