Skip to content

Commit 6c515d3

Browse files
committed
Move all deprecation checks to use a shared predicate which also excludes snapshots
1 parent 9f04049 commit 6c515d3

File tree

9 files changed

+65
-31
lines changed

9 files changed

+65
-31
lines changed
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
/*
2+
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
3+
* or more contributor license agreements. Licensed under the Elastic License
4+
* 2.0; you may not use this file except in compliance with the Elastic License
5+
* 2.0.
6+
*/
7+
8+
package org.elasticsearch.xpack.core.deprecation;
9+
10+
import org.elasticsearch.cluster.metadata.IndexMetadata;
11+
import org.elasticsearch.cluster.metadata.Metadata;
12+
import org.elasticsearch.index.Index;
13+
import org.elasticsearch.index.IndexVersion;
14+
import org.elasticsearch.index.IndexVersions;
15+
16+
import java.util.function.Predicate;
17+
18+
public class DeprecatedIndexPredicate {
19+
20+
public static final IndexVersion MINIMUM_WRITEABLE_VERSION_AFTER_UPGRADE = IndexVersions.UPGRADE_TO_LUCENE_10_0_0;
21+
22+
/*
23+
* This predicate allows through only indices that were created with a previous lucene version, meaning that they need to be reindexed
24+
* in order to be writable in the _next_ lucene version.
25+
*
26+
* It ignores searchable snapshots as they are not writable.
27+
*/
28+
public static Predicate<Index> getReindexRequiredPredicate(Metadata metadata) {
29+
return index -> {
30+
IndexMetadata indexMetadata = metadata.index(index);
31+
return reindexRequired(indexMetadata);
32+
};
33+
}
34+
35+
public static boolean reindexRequired(IndexMetadata indexMetadata) {
36+
return creationVersionBeforeMinimumWritableVersion(indexMetadata) && isNotSearchableSnapshot(indexMetadata);
37+
}
38+
39+
private static boolean isNotSearchableSnapshot(IndexMetadata indexMetadata) {
40+
return indexMetadata.isSearchableSnapshot() == false;
41+
}
42+
43+
private static boolean creationVersionBeforeMinimumWritableVersion(IndexMetadata metadata) {
44+
return metadata.getCreationVersion().before(MINIMUM_WRITEABLE_VERSION_AFTER_UPGRADE);
45+
}
46+
47+
}

x-pack/plugin/deprecation/src/main/java/org/elasticsearch/xpack/deprecation/DataStreamDeprecationChecks.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
import org.elasticsearch.cluster.ClusterState;
1111
import org.elasticsearch.cluster.metadata.DataStream;
1212
import org.elasticsearch.index.Index;
13-
import org.elasticsearch.index.IndexVersions;
13+
import org.elasticsearch.xpack.core.deprecation.DeprecatedIndexPredicate;
1414
import org.elasticsearch.xpack.core.deprecation.DeprecationIssue;
1515

1616
import java.util.List;
@@ -25,16 +25,16 @@ static DeprecationIssue oldIndicesCheck(DataStream dataStream, ClusterState clus
2525
List<Index> backingIndices = dataStream.getIndices();
2626

2727
Set<String> indicesNeedingUpgrade = backingIndices.stream()
28-
.filter(index -> clusterState.metadata().index(index).getCreationVersion().onOrBefore(IndexVersions.UPGRADE_TO_LUCENE_10_0_0))
28+
.filter(DeprecatedIndexPredicate.getReindexRequiredPredicate(clusterState.metadata()))
2929
.map(Index::getName)
3030
.collect(Collectors.toUnmodifiableSet());
3131

3232
if (indicesNeedingUpgrade.isEmpty() == false) {
3333
return new DeprecationIssue(
3434
DeprecationIssue.Level.CRITICAL,
35-
"Old data stream with a compatibility version < 8.0",
35+
"Old data stream with a compatibility version < 9.0",
3636
"https://www.elastic.co/guide/en/elasticsearch/reference/master/breaking-changes-9.0.html",
37-
"This data stream has backing indices that were created before Elasticsearch 8.0.0",
37+
"This data stream has backing indices that were created before Elasticsearch 9.0.0",
3838
false,
3939
ofEntries(
4040
entry("reindex_required", true),

x-pack/plugin/deprecation/src/main/java/org/elasticsearch/xpack/deprecation/IndexDeprecationChecks.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,9 @@
1414
import org.elasticsearch.index.IndexModule;
1515
import org.elasticsearch.index.IndexSettings;
1616
import org.elasticsearch.index.IndexVersion;
17-
import org.elasticsearch.index.IndexVersions;
1817
import org.elasticsearch.index.engine.frozen.FrozenEngine;
1918
import org.elasticsearch.index.mapper.SourceFieldMapper;
19+
import org.elasticsearch.xpack.core.deprecation.DeprecatedIndexPredicate;
2020
import org.elasticsearch.xpack.core.deprecation.DeprecationIssue;
2121

2222
import java.util.ArrayList;
@@ -37,10 +37,10 @@ static DeprecationIssue oldIndicesCheck(IndexMetadata indexMetadata, ClusterStat
3737
// TODO: this check needs to be revised. It's trivially true right now.
3838
IndexVersion currentCompatibilityVersion = indexMetadata.getCompatibilityVersion();
3939
// We intentionally exclude indices that are in data streams because they will be picked up by DataStreamDeprecationChecks
40-
if (currentCompatibilityVersion.before(IndexVersions.V_8_0_0) && isNotDataStreamIndex(indexMetadata, clusterState)) {
40+
if (DeprecatedIndexPredicate.reindexRequired(indexMetadata) && isNotDataStreamIndex(indexMetadata, clusterState)) {
4141
return new DeprecationIssue(
4242
DeprecationIssue.Level.CRITICAL,
43-
"Old index with a compatibility version < 8.0",
43+
"Old index with a compatibility version < 9.0",
4444
"https://www.elastic.co/guide/en/elasticsearch/reference/master/breaking-changes-9.0.html",
4545
"This index has version: " + currentCompatibilityVersion.toReleaseVersion(),
4646
false,

x-pack/plugin/deprecation/src/test/java/org/elasticsearch/xpack/deprecation/DataStreamDeprecationChecksTests.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -85,9 +85,9 @@ public void testOldIndicesCheck() {
8585

8686
DeprecationIssue expected = new DeprecationIssue(
8787
DeprecationIssue.Level.CRITICAL,
88-
"Old data stream with a compatibility version < 8.0",
88+
"Old data stream with a compatibility version < 9.0",
8989
"https://www.elastic.co/guide/en/elasticsearch/reference/master/breaking-changes-9.0.html",
90-
"This data stream has backing indices that were created before Elasticsearch 8.0.0",
90+
"This data stream has backing indices that were created before Elasticsearch 9.0.0",
9191
false,
9292
ofEntries(
9393
entry("reindex_required", true),

x-pack/plugin/deprecation/src/test/java/org/elasticsearch/xpack/deprecation/IndexDeprecationChecksTests.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@
1919
import org.elasticsearch.index.IndexModule;
2020
import org.elasticsearch.index.IndexSettings;
2121
import org.elasticsearch.index.IndexVersion;
22-
import org.elasticsearch.index.IndexVersions;
2322
import org.elasticsearch.index.engine.frozen.FrozenEngine;
2423
import org.elasticsearch.test.ESTestCase;
2524
import org.elasticsearch.xpack.core.deprecation.DeprecationIssue;
@@ -49,7 +48,7 @@ public void testOldIndicesCheck() {
4948
.build();
5049
DeprecationIssue expected = new DeprecationIssue(
5150
DeprecationIssue.Level.CRITICAL,
52-
"Old index with a compatibility version < 8.0",
51+
"Old index with a compatibility version < 9.0",
5352
"https://www.elastic.co/guide/en/elasticsearch/reference/master/breaking-changes-9.0.html",
5453
"This index has version: " + createdWith.toReleaseVersion(),
5554
false,
@@ -230,7 +229,7 @@ public void testCamelCaseDeprecation() throws IOException {
230229
+ "} }";
231230

232231
IndexMetadata simpleIndex = IndexMetadata.builder(randomAlphaOfLengthBetween(5, 10))
233-
.settings(settings(IndexVersions.MINIMUM_COMPATIBLE))
232+
.settings(settings(IndexVersion.current()))
234233
.numberOfShards(1)
235234
.numberOfReplicas(1)
236235
.putMapping(simpleMapping)

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

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -13,14 +13,10 @@
1313
import org.elasticsearch.action.ActionType;
1414
import org.elasticsearch.action.IndicesRequest;
1515
import org.elasticsearch.action.support.IndicesOptions;
16-
import org.elasticsearch.cluster.metadata.Metadata;
1716
import org.elasticsearch.common.io.stream.StreamInput;
1817
import org.elasticsearch.common.io.stream.StreamOutput;
1918
import org.elasticsearch.common.util.FeatureFlag;
2019
import org.elasticsearch.features.NodeFeature;
21-
import org.elasticsearch.index.Index;
22-
import org.elasticsearch.index.IndexVersion;
23-
import org.elasticsearch.index.IndexVersions;
2420
import org.elasticsearch.xcontent.ConstructingObjectParser;
2521
import org.elasticsearch.xcontent.ParseField;
2622
import org.elasticsearch.xcontent.ToXContent;
@@ -47,20 +43,11 @@ public class ReindexDataStreamAction extends ActionType<ReindexDataStreamAction.
4743
* The version before which we do not support writes in the _next_ major version of Elasticsearch. For example, Elasticsearch 10.x will
4844
* not support writing to indices created before version 9.0.0.
4945
*/
50-
public static final IndexVersion MINIMUM_WRITEABLE_VERSION_AFTER_UPGRADE = IndexVersions.UPGRADE_TO_LUCENE_10_0_0;
5146

5247
public ReindexDataStreamAction() {
5348
super(NAME);
5449
}
5550

56-
/*
57-
* This predicate allows through only indices that were created with a previous lucene version, meaning that they need to be reindexed
58-
* in order to be writable in the _next_ lucene version.
59-
*/
60-
public static Predicate<Index> getOldIndexVersionPredicate(Metadata metadata) {
61-
return index -> metadata.index(index).getCreationVersion().onOrBefore(MINIMUM_WRITEABLE_VERSION_AFTER_UPGRADE);
62-
}
63-
6451
public enum Mode {
6552
UPGRADE
6653
}

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
import org.elasticsearch.tasks.Task;
3535
import org.elasticsearch.threadpool.ThreadPool;
3636
import org.elasticsearch.transport.TransportService;
37+
import org.elasticsearch.xpack.core.deprecation.DeprecatedIndexPredicate;
3738

3839
import java.util.Locale;
3940
import java.util.Map;
@@ -84,13 +85,13 @@ protected void doExecute(
8485
IndexMetadata sourceIndex = clusterService.state().getMetadata().index(sourceIndexName);
8586
Settings settingsBefore = sourceIndex.getSettings();
8687

87-
var hasOldVersion = ReindexDataStreamAction.getOldIndexVersionPredicate(clusterService.state().metadata());
88+
var hasOldVersion = DeprecatedIndexPredicate.getReindexRequiredPredicate(clusterService.state().metadata());
8889
if (hasOldVersion.test(sourceIndex.getIndex()) == false) {
8990
logger.warn(
9091
"Migrating index [{}] with version [{}] is unnecessary as its version is not before [{}]",
9192
sourceIndexName,
9293
sourceIndex.getCreationVersion(),
93-
ReindexDataStreamAction.MINIMUM_WRITEABLE_VERSION_AFTER_UPGRADE
94+
DeprecatedIndexPredicate.MINIMUM_WRITEABLE_VERSION_AFTER_UPGRADE
9495
);
9596
}
9697

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,8 @@
2626
import org.elasticsearch.xpack.migrate.task.ReindexDataStreamTask;
2727
import org.elasticsearch.xpack.migrate.task.ReindexDataStreamTaskParams;
2828

29+
import static org.elasticsearch.xpack.core.deprecation.DeprecatedIndexPredicate.getReindexRequiredPredicate;
2930
import static org.elasticsearch.xpack.migrate.action.ReindexDataStreamAction.TASK_ID_PREFIX;
30-
import static org.elasticsearch.xpack.migrate.action.ReindexDataStreamAction.getOldIndexVersionPredicate;
3131

3232
/*
3333
* This transport action creates a new persistent task for reindexing the source data stream given in the request. On successful creation
@@ -68,7 +68,7 @@ protected void doExecute(Task task, ReindexDataStreamRequest request, ActionList
6868
return;
6969
}
7070
int totalIndices = dataStream.getIndices().size();
71-
int totalIndicesToBeUpgraded = (int) dataStream.getIndices().stream().filter(getOldIndexVersionPredicate(metadata)).count();
71+
int totalIndicesToBeUpgraded = (int) dataStream.getIndices().stream().filter(getReindexRequiredPredicate(metadata)).count();
7272
ReindexDataStreamTaskParams params = new ReindexDataStreamTaskParams(
7373
sourceDataStreamName,
7474
transportService.getThreadPool().absoluteTimeInMillis(),

x-pack/plugin/migrate/src/main/java/org/elasticsearch/xpack/migrate/task/ReindexDataStreamPersistentTaskExecutor.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
import java.util.List;
2525
import java.util.Map;
2626

27-
import static org.elasticsearch.xpack.migrate.action.ReindexDataStreamAction.getOldIndexVersionPredicate;
27+
import static org.elasticsearch.xpack.core.deprecation.DeprecatedIndexPredicate.getReindexRequiredPredicate;
2828

2929
public class ReindexDataStreamPersistentTaskExecutor extends PersistentTasksExecutor<ReindexDataStreamTaskParams> {
3030
private static final TimeValue TASK_KEEP_ALIVE_TIME = TimeValue.timeValueDays(1);
@@ -74,7 +74,7 @@ protected void nodeOperation(AllocatedPersistentTask task, ReindexDataStreamTask
7474
if (dataStreamInfos.size() == 1) {
7575
List<Index> indices = dataStreamInfos.getFirst().getDataStream().getIndices();
7676
List<Index> indicesToBeReindexed = indices.stream()
77-
.filter(getOldIndexVersionPredicate(clusterService.state().metadata()))
77+
.filter(getReindexRequiredPredicate(clusterService.state().metadata()))
7878
.toList();
7979
reindexDataStreamTask.setPendingIndicesCount(indicesToBeReindexed.size());
8080
for (Index index : indicesToBeReindexed) {

0 commit comments

Comments
 (0)