Skip to content

Commit 5487927

Browse files
authored
Update data stream deprecations warnings to new format and filter searchable snapshots from response (#118562)
* Update data stream deprecations warnings to new format * Add reindex_required flag to index version deprecation notice response * PR Changes * Move all deprecation checks to use a shared predicate which also excludes snapshots * Update docs/changelog/118562.yaml * Tests for excluding snapshots * PR Changes - Remove leftover comment
1 parent 78bd9ec commit 5487927

File tree

10 files changed

+138
-115
lines changed

10 files changed

+138
-115
lines changed

docs/changelog/118562.yaml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
pr: 118562
2+
summary: Update data stream deprecations warnings to new format and filter searchable
3+
snapshots from response
4+
area: Data streams
5+
type: enhancement
6+
issues: []
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: 17 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -10,65 +10,41 @@
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;
17+
import java.util.Set;
18+
import java.util.stream.Collectors;
1719

1820
import static java.util.Map.entry;
1921
import static java.util.Map.ofEntries;
2022

2123
public class DataStreamDeprecationChecks {
2224
static DeprecationIssue oldIndicesCheck(DataStream dataStream, ClusterState clusterState) {
2325
List<Index> backingIndices = dataStream.getIndices();
24-
boolean hasOldIndices = backingIndices.stream()
25-
.anyMatch(index -> clusterState.metadata().index(index).getCompatibilityVersion().before(IndexVersions.V_8_0_0));
26-
if (hasOldIndices) {
27-
long totalIndices = backingIndices.size();
28-
List<Index> oldIndices = backingIndices.stream()
29-
.filter(index -> clusterState.metadata().index(index).getCompatibilityVersion().before(IndexVersions.V_8_0_0))
30-
.toList();
31-
long totalOldIndices = oldIndices.size();
32-
long totalOldSearchableSnapshots = oldIndices.stream()
33-
.filter(index -> clusterState.metadata().index(index).isSearchableSnapshot())
34-
.count();
35-
long totalOldPartiallyMountedSearchableSnapshots = oldIndices.stream()
36-
.filter(index -> clusterState.metadata().index(index).isPartialSearchableSnapshot())
37-
.count();
38-
long totalOldFullyMountedSearchableSnapshots = totalOldSearchableSnapshots - totalOldPartiallyMountedSearchableSnapshots;
26+
27+
Set<String> indicesNeedingUpgrade = backingIndices.stream()
28+
.filter(DeprecatedIndexPredicate.getReindexRequiredPredicate(clusterState.metadata()))
29+
.map(Index::getName)
30+
.collect(Collectors.toUnmodifiableSet());
31+
32+
if (indicesNeedingUpgrade.isEmpty() == false) {
3933
return new DeprecationIssue(
4034
DeprecationIssue.Level.CRITICAL,
41-
"Old data stream with a compatibility version < 8.0",
35+
"Old data stream with a compatibility version < 9.0",
4236
"https://www.elastic.co/guide/en/elasticsearch/reference/master/breaking-changes-9.0.html",
43-
"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",
4438
false,
4539
ofEntries(
46-
entry(
47-
"backing_indices",
48-
ofEntries(
49-
entry("count", totalIndices),
50-
entry(
51-
"need_upgrading",
52-
ofEntries(
53-
entry("count", totalOldIndices),
54-
entry(
55-
"searchable_snapshots",
56-
ofEntries(
57-
entry("count", totalOldSearchableSnapshots),
58-
entry("fully_mounted", ofEntries(entry("count", totalOldFullyMountedSearchableSnapshots))),
59-
entry(
60-
"partially_mounted",
61-
ofEntries(entry("count", totalOldPartiallyMountedSearchableSnapshots))
62-
)
63-
)
64-
)
65-
)
66-
)
67-
)
68-
)
40+
entry("reindex_required", true),
41+
entry("total_backing_indices", backingIndices.size()),
42+
entry("indices_requiring_upgrade_count", indicesNeedingUpgrade.size()),
43+
entry("indices_requiring_upgrade", indicesNeedingUpgrade)
6944
)
7045
);
7146
}
47+
7248
return null;
7349
}
7450
}

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

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,13 @@
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;
23+
import java.util.Collections;
2324
import java.util.List;
2425
import java.util.Locale;
2526
import java.util.Map;
@@ -36,14 +37,14 @@ static DeprecationIssue oldIndicesCheck(IndexMetadata indexMetadata, ClusterStat
3637
// TODO: this check needs to be revised. It's trivially true right now.
3738
IndexVersion currentCompatibilityVersion = indexMetadata.getCompatibilityVersion();
3839
// We intentionally exclude indices that are in data streams because they will be picked up by DataStreamDeprecationChecks
39-
if (currentCompatibilityVersion.before(IndexVersions.V_8_0_0) && isNotDataStreamIndex(indexMetadata, clusterState)) {
40+
if (DeprecatedIndexPredicate.reindexRequired(indexMetadata) && isNotDataStreamIndex(indexMetadata, clusterState)) {
4041
return new DeprecationIssue(
4142
DeprecationIssue.Level.CRITICAL,
42-
"Old index with a compatibility version < 8.0",
43+
"Old index with a compatibility version < 9.0",
4344
"https://www.elastic.co/guide/en/elasticsearch/reference/master/breaking-changes-9.0.html",
4445
"This index has version: " + currentCompatibilityVersion.toReleaseVersion(),
4546
false,
46-
null
47+
Collections.singletonMap("reindex_required", true)
4748
);
4849
}
4950
return null;

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

Lines changed: 35 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -17,53 +17,56 @@
1717
import org.elasticsearch.index.Index;
1818
import org.elasticsearch.index.IndexMode;
1919
import org.elasticsearch.index.IndexVersion;
20+
import org.elasticsearch.snapshots.SearchableSnapshotsSettings;
2021
import org.elasticsearch.test.ESTestCase;
2122
import org.elasticsearch.xpack.core.deprecation.DeprecationIssue;
2223

2324
import java.util.ArrayList;
2425
import java.util.HashMap;
26+
import java.util.HashSet;
2527
import java.util.List;
2628
import java.util.Map;
29+
import java.util.Set;
2730

2831
import static java.util.Collections.singletonList;
32+
import static java.util.Map.entry;
33+
import static java.util.Map.ofEntries;
34+
import static org.elasticsearch.index.IndexModule.INDEX_STORE_TYPE_SETTING;
2935
import static org.elasticsearch.xpack.deprecation.DeprecationChecks.DATA_STREAM_CHECKS;
3036
import static org.hamcrest.Matchers.equalTo;
3137

3238
public class DataStreamDeprecationChecksTests extends ESTestCase {
3339

3440
public void testOldIndicesCheck() {
35-
long oldIndexCount = randomIntBetween(1, 100);
36-
long newIndexCount = randomIntBetween(1, 100);
37-
long oldSearchableSnapshotCount = 0;
38-
long oldFullyManagedSearchableSnapshotCount = 0;
39-
long oldPartiallyManagedSearchableSnapshotCount = 0;
41+
int oldIndexCount = randomIntBetween(1, 100);
42+
int newIndexCount = randomIntBetween(1, 100);
43+
4044
List<Index> allIndices = new ArrayList<>();
4145
Map<String, IndexMetadata> nameToIndexMetadata = new HashMap<>();
46+
Set<String> expectedIndices = new HashSet<>();
47+
4248
for (int i = 0; i < oldIndexCount; i++) {
43-
Settings.Builder settingsBuilder = settings(IndexVersion.fromId(7170099));
44-
if (randomBoolean()) {
45-
settingsBuilder.put("index.store.type", "snapshot");
46-
if (randomBoolean()) {
47-
oldFullyManagedSearchableSnapshotCount++;
48-
} else {
49-
settingsBuilder.put("index.store.snapshot.partial", true);
50-
oldPartiallyManagedSearchableSnapshotCount++;
51-
}
52-
oldSearchableSnapshotCount++;
49+
Settings.Builder settings = settings(IndexVersion.fromId(7170099));
50+
51+
String indexName = "old-data-stream-index-" + i;
52+
if (expectedIndices.isEmpty() == false && randomIntBetween(0, 2) == 0) {
53+
settings.put(INDEX_STORE_TYPE_SETTING.getKey(), SearchableSnapshotsSettings.SEARCHABLE_SNAPSHOT_STORE_TYPE);
54+
} else {
55+
expectedIndices.add(indexName);
5356
}
54-
IndexMetadata oldIndexMetadata = IndexMetadata.builder("old-data-stream-index-" + i)
57+
58+
Settings.Builder settingsBuilder = settings;
59+
IndexMetadata oldIndexMetadata = IndexMetadata.builder(indexName)
5560
.settings(settingsBuilder)
5661
.numberOfShards(1)
5762
.numberOfReplicas(0)
5863
.build();
5964
allIndices.add(oldIndexMetadata.getIndex());
6065
nameToIndexMetadata.put(oldIndexMetadata.getIndex().getName(), oldIndexMetadata);
6166
}
67+
6268
for (int i = 0; i < newIndexCount; i++) {
6369
Settings.Builder settingsBuilder = settings(IndexVersion.current());
64-
if (randomBoolean()) {
65-
settingsBuilder.put("index.store.type", "snapshot");
66-
}
6770
IndexMetadata newIndexMetadata = IndexMetadata.builder("new-data-stream-index-" + i)
6871
.settings(settingsBuilder)
6972
.numberOfShards(1)
@@ -72,6 +75,7 @@ public void testOldIndicesCheck() {
7275
allIndices.add(newIndexMetadata.getIndex());
7376
nameToIndexMetadata.put(newIndexMetadata.getIndex().getName(), newIndexMetadata);
7477
}
78+
7579
DataStream dataStream = new DataStream(
7680
randomAlphaOfLength(10),
7781
allIndices,
@@ -88,37 +92,27 @@ public void testOldIndicesCheck() {
8892
randomBoolean(),
8993
null
9094
);
95+
9196
Metadata metadata = Metadata.builder().indices(nameToIndexMetadata).build();
9297
ClusterState clusterState = ClusterState.builder(ClusterName.DEFAULT).metadata(metadata).build();
98+
9399
DeprecationIssue expected = new DeprecationIssue(
94100
DeprecationIssue.Level.CRITICAL,
95-
"Old data stream with a compatibility version < 8.0",
101+
"Old data stream with a compatibility version < 9.0",
96102
"https://www.elastic.co/guide/en/elasticsearch/reference/master/breaking-changes-9.0.html",
97-
"This data stream has backing indices that were created before Elasticsearch 8.0.0",
103+
"This data stream has backing indices that were created before Elasticsearch 9.0.0",
98104
false,
99-
Map.of(
100-
"backing_indices",
101-
Map.of(
102-
"count",
103-
oldIndexCount + newIndexCount,
104-
"need_upgrading",
105-
Map.of(
106-
"count",
107-
oldIndexCount,
108-
"searchable_snapshots",
109-
Map.of(
110-
"count",
111-
oldSearchableSnapshotCount,
112-
"fully_mounted",
113-
Map.of("count", oldFullyManagedSearchableSnapshotCount),
114-
"partially_mounted",
115-
Map.of("count", oldPartiallyManagedSearchableSnapshotCount)
116-
)
117-
)
118-
)
105+
ofEntries(
106+
entry("reindex_required", true),
107+
entry("total_backing_indices", oldIndexCount + newIndexCount),
108+
entry("indices_requiring_upgrade_count", expectedIndices.size()),
109+
entry("indices_requiring_upgrade", expectedIndices)
119110
)
120111
);
112+
121113
List<DeprecationIssue> issues = DeprecationChecks.filterChecks(DATA_STREAM_CHECKS, c -> c.apply(dataStream, clusterState));
114+
122115
assertThat(issues, equalTo(singletonList(expected)));
123116
}
117+
124118
}

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

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,8 @@
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;
23+
import org.elasticsearch.snapshots.SearchableSnapshotsSettings;
2424
import org.elasticsearch.test.ESTestCase;
2525
import org.elasticsearch.xpack.core.deprecation.DeprecationIssue;
2626

@@ -29,6 +29,8 @@
2929
import java.util.Map;
3030

3131
import static java.util.Collections.singletonList;
32+
import static java.util.Collections.singletonMap;
33+
import static org.elasticsearch.index.IndexModule.INDEX_STORE_TYPE_SETTING;
3234
import static org.elasticsearch.xpack.deprecation.DeprecationChecks.INDEX_SETTINGS_CHECKS;
3335
import static org.hamcrest.Matchers.empty;
3436
import static org.hamcrest.Matchers.equalTo;
@@ -48,11 +50,11 @@ public void testOldIndicesCheck() {
4850
.build();
4951
DeprecationIssue expected = new DeprecationIssue(
5052
DeprecationIssue.Level.CRITICAL,
51-
"Old index with a compatibility version < 8.0",
53+
"Old index with a compatibility version < 9.0",
5254
"https://www.elastic.co/guide/en/elasticsearch/reference/master/breaking-changes-9.0.html",
5355
"This index has version: " + createdWith.toReleaseVersion(),
5456
false,
55-
null
57+
singletonMap("reindex_required", true)
5658
);
5759
List<DeprecationIssue> issues = DeprecationChecks.filterChecks(INDEX_SETTINGS_CHECKS, c -> c.apply(indexMetadata, clusterState));
5860
assertEquals(singletonList(expected), issues);
@@ -100,6 +102,20 @@ public void testOldIndicesCheckDataStreamIndex() {
100102
assertThat(issues.size(), equalTo(0));
101103
}
102104

105+
public void testOldIndicesCheckSnapshotIgnored() {
106+
IndexVersion createdWith = IndexVersion.fromId(7170099);
107+
Settings.Builder settings = settings(createdWith);
108+
settings.put(INDEX_STORE_TYPE_SETTING.getKey(), SearchableSnapshotsSettings.SEARCHABLE_SNAPSHOT_STORE_TYPE);
109+
IndexMetadata indexMetadata = IndexMetadata.builder("test").settings(settings).numberOfShards(1).numberOfReplicas(0).build();
110+
ClusterState clusterState = ClusterState.builder(ClusterState.EMPTY_STATE)
111+
.metadata(Metadata.builder().put(indexMetadata, true))
112+
.build();
113+
114+
List<DeprecationIssue> issues = DeprecationChecks.filterChecks(INDEX_SETTINGS_CHECKS, c -> c.apply(indexMetadata, clusterState));
115+
116+
assertThat(issues, empty());
117+
}
118+
103119
public void testTranslogRetentionSettings() {
104120
Settings.Builder settings = settings(IndexVersion.current());
105121
settings.put(IndexSettings.INDEX_TRANSLOG_RETENTION_AGE_SETTING.getKey(), randomPositiveTimeValue());
@@ -229,7 +245,7 @@ public void testCamelCaseDeprecation() throws IOException {
229245
+ "} }";
230246

231247
IndexMetadata simpleIndex = IndexMetadata.builder(randomAlphaOfLengthBetween(5, 10))
232-
.settings(settings(IndexVersions.MINIMUM_COMPATIBLE))
248+
.settings(settings(IndexVersion.current()))
233249
.numberOfShards(1)
234250
.numberOfReplicas(1)
235251
.putMapping(simpleMapping)

0 commit comments

Comments
 (0)