|
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;
|
|
82 | 84 |
|
83 | 85 | public class IndexMetadata implements Diffable<IndexMetadata>, ToXContentFragment {
|
84 | 86 |
|
| 87 | + private static final Logger logger = LogManager.getLogger(IndexMetadata.class); |
| 88 | + |
85 | 89 | public static final ClusterBlock INDEX_READ_ONLY_BLOCK = new ClusterBlock(
|
86 | 90 | 5,
|
87 | 91 | "index read-only (api)",
|
@@ -1512,7 +1516,7 @@ public IndexMetadata apply(IndexMetadata part) {
|
1512 | 1516 | builder.rolloverInfos.putAllFromMap(rolloverInfos.apply(part.rolloverInfos));
|
1513 | 1517 | builder.system(isSystem);
|
1514 | 1518 | builder.timestampRange(timestampRange);
|
1515 |
| - return builder.build(); |
| 1519 | + return builder.build(true); |
1516 | 1520 | }
|
1517 | 1521 | }
|
1518 | 1522 |
|
@@ -1573,7 +1577,7 @@ public static IndexMetadata readFrom(StreamInput in, @Nullable Function<String,
|
1573 | 1577 | builder.system(in.readBoolean());
|
1574 | 1578 | }
|
1575 | 1579 | builder.timestampRange(IndexLongFieldRange.readFrom(in));
|
1576 |
| - return builder.build(); |
| 1580 | + return builder.build(true); |
1577 | 1581 | }
|
1578 | 1582 |
|
1579 | 1583 | /**
|
@@ -1903,6 +1907,11 @@ public Builder timestampRange(IndexLongFieldRange timestampRange) {
|
1903 | 1907 | }
|
1904 | 1908 |
|
1905 | 1909 | public IndexMetadata build() {
|
| 1910 | + return build(false); |
| 1911 | + } |
| 1912 | + |
| 1913 | + // package private for testing |
| 1914 | + IndexMetadata build(boolean repair) { |
1906 | 1915 | /*
|
1907 | 1916 | * We expect that the metadata has been properly built to set the number of shards and the number of replicas, and do not rely
|
1908 | 1917 | * on the default values here. Those must have been set upstream.
|
@@ -2020,7 +2029,16 @@ public IndexMetadata build() {
|
2020 | 2029 | var aliasesMap = aliases.build();
|
2021 | 2030 | for (AliasMetadata alias : aliasesMap.values()) {
|
2022 | 2031 | if (alias.alias().equals(index)) {
|
2023 |
| - throw new IllegalArgumentException("alias name [" + index + "] self-conflicts with index name"); |
| 2032 | + if (repair && indexCreatedVersion.equals(Version.V_8_5_0)) { |
| 2033 | + var updatedBuilder = ImmutableOpenMap.builder(aliasesMap); |
| 2034 | + final var brokenAlias = updatedBuilder.remove(index); |
| 2035 | + final var fixedAlias = AliasMetadata.newAliasMetadata(brokenAlias, index + "-alias-corrupted-by-8-5"); |
| 2036 | + aliasesMap = updatedBuilder.fPut(fixedAlias.getAlias(), fixedAlias).build(); |
| 2037 | + logger.warn("Repaired corrupted alias with the same name as its index for [{}]", index); |
| 2038 | + break; |
| 2039 | + } else { |
| 2040 | + throw new IllegalArgumentException("alias name [" + index + "] self-conflicts with index name"); |
| 2041 | + } |
2024 | 2042 | }
|
2025 | 2043 | }
|
2026 | 2044 |
|
@@ -2321,7 +2339,7 @@ public static IndexMetadata fromXContent(XContentParser parser, Map<String, Mapp
|
2321 | 2339 | assert settingsVersion : "settings version should be present for indices created on or after 6.5.0";
|
2322 | 2340 | assert indexCreatedVersion(builder.settings).before(Version.V_7_2_0) || aliasesVersion
|
2323 | 2341 | : "aliases version should be present for indices created on or after 7.2.0";
|
2324 |
| - return builder.build(); |
| 2342 | + return builder.build(true); |
2325 | 2343 | }
|
2326 | 2344 |
|
2327 | 2345 | /**
|
@@ -2433,7 +2451,7 @@ public static IndexMetadata legacyFromXContent(XContentParser parser) throws IOE
|
2433 | 2451 | builder.putMapping(MappingMetadata.EMPTY_MAPPINGS); // just make sure it's not empty so that _source can be read
|
2434 | 2452 | }
|
2435 | 2453 |
|
2436 |
| - IndexMetadata indexMetadata = builder.build(); |
| 2454 | + IndexMetadata indexMetadata = builder.build(true); |
2437 | 2455 | assert indexMetadata.getCreationVersion().isLegacyIndexVersion();
|
2438 | 2456 | assert indexMetadata.getCompatibilityVersion().isLegacyIndexVersion();
|
2439 | 2457 | return indexMetadata;
|
|
0 commit comments