Skip to content

Commit c9d8a3a

Browse files
authored
Add replica handling to the ILM MountSnapshotStep (#118687)
1 parent 6c56c32 commit c9d8a3a

13 files changed

+182
-158
lines changed

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88

99
import org.elasticsearch.client.internal.Client;
1010
import org.elasticsearch.cluster.metadata.Metadata;
11+
import org.elasticsearch.core.Nullable;
1112
import org.elasticsearch.core.TimeValue;
1213
import org.elasticsearch.index.Index;
1314
import org.elasticsearch.xcontent.ToXContentObject;
@@ -20,13 +21,15 @@
2021
*/
2122
public abstract class AsyncWaitStep extends Step {
2223

24+
@Nullable
2325
private final Client client;
2426

2527
public AsyncWaitStep(StepKey key, StepKey nextStepKey, Client client) {
2628
super(key, nextStepKey);
2729
this.client = client;
2830
}
2931

32+
@Nullable
3033
protected Client getClient() {
3134
return client;
3235
}

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

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -93,8 +93,7 @@ public List<Step> toSteps(Client client, String phase, Step.StepKey nextStepKey)
9393
WaitUntilTimeSeriesEndTimePassesStep waitUntilTimeSeriesEndTimeStep = new WaitUntilTimeSeriesEndTimePassesStep(
9494
waitTimeSeriesEndTimePassesKey,
9595
cleanSnapshotKey,
96-
Instant::now,
97-
client
96+
Instant::now
9897
);
9998
CleanupSnapshotStep cleanupSnapshotStep = new CleanupSnapshotStep(cleanSnapshotKey, deleteStepKey, client);
10099
DeleteStep deleteStep = new DeleteStep(deleteStepKey, nextStepKey, client);
@@ -108,8 +107,7 @@ public List<Step> toSteps(Client client, String phase, Step.StepKey nextStepKey)
108107
WaitUntilTimeSeriesEndTimePassesStep waitUntilTimeSeriesEndTimeStep = new WaitUntilTimeSeriesEndTimePassesStep(
109108
waitTimeSeriesEndTimePassesKey,
110109
deleteStepKey,
111-
Instant::now,
112-
client
110+
Instant::now
113111
);
114112
DeleteStep deleteStep = new DeleteStep(deleteStepKey, nextStepKey, client);
115113
return List.of(waitForNoFollowersStep, waitUntilTimeSeriesEndTimeStep, deleteStep);

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

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -200,8 +200,7 @@ public List<Step> toSteps(Client client, String phase, StepKey nextStepKey) {
200200
WaitUntilTimeSeriesEndTimePassesStep waitUntilTimeSeriesEndTimeStep = new WaitUntilTimeSeriesEndTimePassesStep(
201201
waitTimeSeriesEndTimePassesKey,
202202
readOnlyKey,
203-
Instant::now,
204-
client
203+
Instant::now
205204
);
206205
// Mark source index as read-only
207206
ReadOnlyStep readOnlyStep = new ReadOnlyStep(readOnlyKey, generateDownsampleIndexNameKey, client);

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

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -162,8 +162,7 @@ public List<Step> toSteps(Client client, String phase, Step.StepKey nextStepKey)
162162
WaitUntilTimeSeriesEndTimePassesStep waitUntilTimeSeriesEndTimeStep = new WaitUntilTimeSeriesEndTimePassesStep(
163163
waitTimeSeriesEndTimePassesKey,
164164
codecChange ? closeKey : forceMergeKey,
165-
Instant::now,
166-
client
165+
Instant::now
167166
);
168167

169168
// Indices already in this step key when upgrading need to know how to move forward but stop making the index

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

Lines changed: 16 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -41,14 +41,16 @@ public class MountSnapshotStep extends AsyncRetryDuringSnapshotActionStep {
4141
private final MountSearchableSnapshotRequest.Storage storageType;
4242
@Nullable
4343
private final Integer totalShardsPerNode;
44+
private final int replicas;
4445

4546
public MountSnapshotStep(
4647
StepKey key,
4748
StepKey nextStepKey,
4849
Client client,
4950
String restoredIndexPrefix,
5051
MountSearchableSnapshotRequest.Storage storageType,
51-
@Nullable Integer totalShardsPerNode
52+
@Nullable Integer totalShardsPerNode,
53+
int replicas
5254
) {
5355
super(key, nextStepKey, client);
5456
this.restoredIndexPrefix = restoredIndexPrefix;
@@ -57,16 +59,10 @@ public MountSnapshotStep(
5759
throw new IllegalArgumentException("[" + SearchableSnapshotAction.TOTAL_SHARDS_PER_NODE.getPreferredName() + "] must be >= 1");
5860
}
5961
this.totalShardsPerNode = totalShardsPerNode;
60-
}
6162

62-
public MountSnapshotStep(
63-
StepKey key,
64-
StepKey nextStepKey,
65-
Client client,
66-
String restoredIndexPrefix,
67-
MountSearchableSnapshotRequest.Storage storageType
68-
) {
69-
this(key, nextStepKey, client, restoredIndexPrefix, storageType, null);
63+
// this isn't directly settable by the user, so validation by assertion is sufficient
64+
assert replicas >= 0 : "number of replicas must be gte zero, but was [" + replicas + "]";
65+
this.replicas = replicas;
7066
}
7167

7268
@Override
@@ -87,6 +83,10 @@ public Integer getTotalShardsPerNode() {
8783
return totalShardsPerNode;
8884
}
8985

86+
public int getReplicas() {
87+
return replicas;
88+
}
89+
9090
@Override
9191
void performDuringNoSnapshot(IndexMetadata indexMetadata, ClusterState currentClusterState, ActionListener<Void> listener) {
9292
String indexName = indexMetadata.getIndex().getName();
@@ -162,11 +162,13 @@ void performDuringNoSnapshot(IndexMetadata indexMetadata, ClusterState currentCl
162162
}
163163

164164
final Settings.Builder settingsBuilder = Settings.builder();
165-
166165
overrideTierPreference(this.getKey().phase()).ifPresent(override -> settingsBuilder.put(DataTier.TIER_PREFERENCE, override));
167166
if (totalShardsPerNode != null) {
168167
settingsBuilder.put(ShardsLimitAllocationDecider.INDEX_TOTAL_SHARDS_PER_NODE_SETTING.getKey(), totalShardsPerNode);
169168
}
169+
if (replicas > 0) {
170+
settingsBuilder.put(IndexMetadata.SETTING_NUMBER_OF_REPLICAS, replicas);
171+
}
170172

171173
final MountSearchableSnapshotRequest mountSearchableSnapshotRequest = new MountSearchableSnapshotRequest(
172174
TimeValue.MAX_VALUE,
@@ -245,7 +247,7 @@ String[] ignoredIndexSettings() {
245247

246248
@Override
247249
public int hashCode() {
248-
return Objects.hash(super.hashCode(), restoredIndexPrefix, storageType, totalShardsPerNode);
250+
return Objects.hash(super.hashCode(), restoredIndexPrefix, storageType, totalShardsPerNode, replicas);
249251
}
250252

251253
@Override
@@ -260,6 +262,7 @@ public boolean equals(Object obj) {
260262
return super.equals(obj)
261263
&& Objects.equals(restoredIndexPrefix, other.restoredIndexPrefix)
262264
&& Objects.equals(storageType, other.storageType)
263-
&& Objects.equals(totalShardsPerNode, other.totalShardsPerNode);
265+
&& Objects.equals(totalShardsPerNode, other.totalShardsPerNode)
266+
&& Objects.equals(replicas, other.replicas);
264267
}
265268
}

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

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -67,8 +67,7 @@ public List<Step> toSteps(Client client, String phase, StepKey nextStepKey) {
6767
WaitUntilTimeSeriesEndTimePassesStep waitUntilTimeSeriesEndTimeStep = new WaitUntilTimeSeriesEndTimePassesStep(
6868
waitTimeSeriesEndTimePassesKey,
6969
readOnlyKey,
70-
Instant::now,
71-
client
70+
Instant::now
7271
);
7372
ReadOnlyStep readOnlyStep = new ReadOnlyStep(readOnlyKey, nextStepKey, client);
7473
return List.of(checkNotWriteIndexStep, waitUntilTimeSeriesEndTimeStep, readOnlyStep);

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

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,7 @@ public String getSnapshotRepository() {
113113
return snapshotRepository;
114114
}
115115

116+
@Nullable
116117
public Integer getTotalShardsPerNode() {
117118
return totalShardsPerNode;
118119
}
@@ -230,8 +231,7 @@ public List<Step> toSteps(Client client, String phase, StepKey nextStepKey, XPac
230231
WaitUntilTimeSeriesEndTimePassesStep waitUntilTimeSeriesEndTimeStep = new WaitUntilTimeSeriesEndTimePassesStep(
231232
waitTimeSeriesEndTimePassesKey,
232233
skipGeneratingSnapshotKey,
233-
Instant::now,
234-
client
234+
Instant::now
235235
);
236236

237237
// When generating a snapshot, we either jump to the force merge step, or we skip the
@@ -318,7 +318,8 @@ public List<Step> toSteps(Client client, String phase, StepKey nextStepKey, XPac
318318
client,
319319
getRestoredIndexPrefix(mountSnapshotKey),
320320
storageType,
321-
totalShardsPerNode
321+
totalShardsPerNode,
322+
0
322323
);
323324
WaitForIndexColorStep waitForGreenIndexHealthStep = new WaitForIndexColorStep(
324325
waitForGreenRestoredIndexKey,

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

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -231,8 +231,7 @@ public List<Step> toSteps(Client client, String phase, Step.StepKey nextStepKey)
231231
WaitUntilTimeSeriesEndTimePassesStep waitUntilTimeSeriesEndTimeStep = new WaitUntilTimeSeriesEndTimePassesStep(
232232
waitTimeSeriesEndTimePassesKey,
233233
readOnlyKey,
234-
Instant::now,
235-
client
234+
Instant::now
236235
);
237236
ReadOnlyStep readOnlyStep = new ReadOnlyStep(readOnlyKey, checkTargetShardsCountKey, client);
238237
CheckTargetShardsCountStep checkTargetShardsCountStep = new CheckTargetShardsCountStep(

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

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@
2727
import java.util.Set;
2828
import java.util.function.Function;
2929
import java.util.stream.Collectors;
30-
import java.util.stream.Stream;
3130

3231
/**
3332
* Represents the lifecycle of an index from creation to deletion. A
@@ -49,7 +48,7 @@ public class TimeseriesLifecycleType implements LifecycleType {
4948
static final String DELETE_PHASE = "delete";
5049
public static final List<String> ORDERED_VALID_PHASES = List.of(HOT_PHASE, WARM_PHASE, COLD_PHASE, FROZEN_PHASE, DELETE_PHASE);
5150

52-
public static final List<String> ORDERED_VALID_HOT_ACTIONS = Stream.of(
51+
public static final List<String> ORDERED_VALID_HOT_ACTIONS = List.of(
5352
SetPriorityAction.NAME,
5453
UnfollowAction.NAME,
5554
RolloverAction.NAME,
@@ -58,8 +57,8 @@ public class TimeseriesLifecycleType implements LifecycleType {
5857
ShrinkAction.NAME,
5958
ForceMergeAction.NAME,
6059
SearchableSnapshotAction.NAME
61-
).filter(Objects::nonNull).toList();
62-
public static final List<String> ORDERED_VALID_WARM_ACTIONS = Stream.of(
60+
);
61+
public static final List<String> ORDERED_VALID_WARM_ACTIONS = List.of(
6362
SetPriorityAction.NAME,
6463
UnfollowAction.NAME,
6564
ReadOnlyAction.NAME,
@@ -68,8 +67,8 @@ public class TimeseriesLifecycleType implements LifecycleType {
6867
MigrateAction.NAME,
6968
ShrinkAction.NAME,
7069
ForceMergeAction.NAME
71-
).filter(Objects::nonNull).toList();
72-
public static final List<String> ORDERED_VALID_COLD_ACTIONS = Stream.of(
70+
);
71+
public static final List<String> ORDERED_VALID_COLD_ACTIONS = List.of(
7372
SetPriorityAction.NAME,
7473
UnfollowAction.NAME,
7574
ReadOnlyAction.NAME,
@@ -78,7 +77,7 @@ public class TimeseriesLifecycleType implements LifecycleType {
7877
AllocateAction.NAME,
7978
MigrateAction.NAME,
8079
FreezeAction.NAME
81-
).filter(Objects::nonNull).toList();
80+
);
8281
public static final List<String> ORDERED_VALID_FROZEN_ACTIONS = List.of(UnfollowAction.NAME, SearchableSnapshotAction.NAME);
8382
public static final List<String> ORDERED_VALID_DELETE_ACTIONS = List.of(WaitForSnapshotAction.NAME, DeleteAction.NAME);
8483

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

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
*/
77
package org.elasticsearch.xpack.core.ilm;
88

9-
import org.elasticsearch.client.internal.Client;
109
import org.elasticsearch.cluster.metadata.IndexMetadata;
1110
import org.elasticsearch.cluster.metadata.Metadata;
1211
import org.elasticsearch.common.Strings;
@@ -33,8 +32,8 @@ public class WaitUntilTimeSeriesEndTimePassesStep extends AsyncWaitStep {
3332
public static final String NAME = "check-ts-end-time-passed";
3433
private final Supplier<Instant> nowSupplier;
3534

36-
public WaitUntilTimeSeriesEndTimePassesStep(StepKey key, StepKey nextStepKey, Supplier<Instant> nowSupplier, Client client) {
37-
super(key, nextStepKey, client);
35+
public WaitUntilTimeSeriesEndTimePassesStep(StepKey key, StepKey nextStepKey, Supplier<Instant> nowSupplier) {
36+
super(key, nextStepKey, null);
3837
this.nowSupplier = nowSupplier;
3938
}
4039

0 commit comments

Comments
 (0)