|
8 | 8 |
|
9 | 9 | package org.elasticsearch.cluster.metadata;
|
10 | 10 |
|
| 11 | +import org.apache.logging.log4j.LogManager; |
| 12 | +import org.apache.logging.log4j.Logger; |
11 | 13 | import org.elasticsearch.Version;
|
12 | 14 | import org.elasticsearch.action.admin.indices.rollover.RolloverInfo;
|
13 | 15 | import org.elasticsearch.action.support.ActiveShardCount;
|
|
83 | 85 |
|
84 | 86 | public class IndexMetadata implements Diffable<IndexMetadata>, ToXContentFragment {
|
85 | 87 |
|
| 88 | + private static final Logger logger = LogManager.getLogger(IndexMetadata.class); |
| 89 | + |
86 | 90 | public static final ClusterBlock INDEX_READ_ONLY_BLOCK = new ClusterBlock(
|
87 | 91 | 5,
|
88 | 92 | "index read-only (api)",
|
@@ -1589,7 +1593,7 @@ public IndexMetadata apply(IndexMetadata part) {
|
1589 | 1593 | builder.stats(stats);
|
1590 | 1594 | builder.indexWriteLoadForecast(indexWriteLoadForecast);
|
1591 | 1595 | builder.shardSizeInBytesForecast(shardSizeInBytesForecast);
|
1592 |
| - return builder.build(); |
| 1596 | + return builder.build(true); |
1593 | 1597 | }
|
1594 | 1598 | }
|
1595 | 1599 |
|
@@ -1656,7 +1660,7 @@ public static IndexMetadata readFrom(StreamInput in, @Nullable Function<String,
|
1656 | 1660 | builder.indexWriteLoadForecast(in.readOptionalDouble());
|
1657 | 1661 | builder.shardSizeInBytesForecast(in.readOptionalLong());
|
1658 | 1662 | }
|
1659 |
| - return builder.build(); |
| 1663 | + return builder.build(true); |
1660 | 1664 | }
|
1661 | 1665 |
|
1662 | 1666 | /**
|
@@ -2012,6 +2016,11 @@ public Builder shardSizeInBytesForecast(Long shardSizeInBytesForecast) {
|
2012 | 2016 | }
|
2013 | 2017 |
|
2014 | 2018 | public IndexMetadata build() {
|
| 2019 | + return build(false); |
| 2020 | + } |
| 2021 | + |
| 2022 | + // package private for testing |
| 2023 | + IndexMetadata build(boolean repair) { |
2015 | 2024 | /*
|
2016 | 2025 | * We expect that the metadata has been properly built to set the number of shards and the number of replicas, and do not rely
|
2017 | 2026 | * on the default values here. Those must have been set upstream.
|
@@ -2140,7 +2149,16 @@ public IndexMetadata build() {
|
2140 | 2149 | var aliasesMap = aliases.build();
|
2141 | 2150 | for (AliasMetadata alias : aliasesMap.values()) {
|
2142 | 2151 | if (alias.alias().equals(index)) {
|
2143 |
| - throw new IllegalArgumentException("alias name [" + index + "] self-conflicts with index name"); |
| 2152 | + if (repair && indexCreatedVersion.equals(Version.V_8_5_0)) { |
| 2153 | + var updatedBuilder = ImmutableOpenMap.builder(aliasesMap); |
| 2154 | + final var brokenAlias = updatedBuilder.remove(index); |
| 2155 | + final var fixedAlias = AliasMetadata.newAliasMetadata(brokenAlias, index + "-alias-corrupted-by-8-5"); |
| 2156 | + aliasesMap = updatedBuilder.fPut(fixedAlias.getAlias(), fixedAlias).build(); |
| 2157 | + logger.warn("Repaired corrupted alias with the same name as its index for [{}]", index); |
| 2158 | + break; |
| 2159 | + } else { |
| 2160 | + throw new IllegalArgumentException("alias name [" + index + "] self-conflicts with index name"); |
| 2161 | + } |
2144 | 2162 | }
|
2145 | 2163 | }
|
2146 | 2164 |
|
@@ -2463,7 +2481,7 @@ public static IndexMetadata fromXContent(XContentParser parser, Map<String, Mapp
|
2463 | 2481 | assert settingsVersion : "settings version should be present for indices created on or after 6.5.0";
|
2464 | 2482 | assert indexCreatedVersion(builder.settings).before(Version.V_7_2_0) || aliasesVersion
|
2465 | 2483 | : "aliases version should be present for indices created on or after 7.2.0";
|
2466 |
| - return builder.build(); |
| 2484 | + return builder.build(true); |
2467 | 2485 | }
|
2468 | 2486 |
|
2469 | 2487 | /**
|
@@ -2574,7 +2592,7 @@ public static IndexMetadata legacyFromXContent(XContentParser parser) throws IOE
|
2574 | 2592 | builder.putMapping(MappingMetadata.EMPTY_MAPPINGS); // just make sure it's not empty so that _source can be read
|
2575 | 2593 | }
|
2576 | 2594 |
|
2577 |
| - IndexMetadata indexMetadata = builder.build(); |
| 2595 | + IndexMetadata indexMetadata = builder.build(true); |
2578 | 2596 | assert indexMetadata.getCreationVersion().isLegacyIndexVersion();
|
2579 | 2597 | assert indexMetadata.getCompatibilityVersion().isLegacyIndexVersion();
|
2580 | 2598 | return indexMetadata;
|
|
0 commit comments