diff --git a/server/src/main/java/org/elasticsearch/cluster/metadata/Metadata.java b/server/src/main/java/org/elasticsearch/cluster/metadata/Metadata.java index 99b075754a008..8b8a2f7078166 100644 --- a/server/src/main/java/org/elasticsearch/cluster/metadata/Metadata.java +++ b/server/src/main/java/org/elasticsearch/cluster/metadata/Metadata.java @@ -12,7 +12,6 @@ import org.apache.logging.log4j.Logger; import org.apache.lucene.util.CollectionUtil; import org.elasticsearch.TransportVersion; -import org.elasticsearch.Version; import org.elasticsearch.cluster.ClusterState; import org.elasticsearch.cluster.Diff; import org.elasticsearch.cluster.Diffable; @@ -50,6 +49,7 @@ import org.elasticsearch.index.IndexMode; import org.elasticsearch.index.IndexNotFoundException; import org.elasticsearch.index.IndexSettings; +import org.elasticsearch.index.IndexVersion; import org.elasticsearch.plugins.MapperPlugin; import org.elasticsearch.rest.RestStatus; import org.elasticsearch.transport.Transports; @@ -231,7 +231,7 @@ default boolean isRestorable() { private volatile SortedMap indicesLookup; private final Map mappingsByHash; - private final Version oldestIndexVersion; + private final IndexVersion oldestIndexVersion; private Metadata( String clusterUUID, @@ -256,7 +256,7 @@ private Metadata( String[] visibleClosedIndices, SortedMap indicesLookup, Map mappingsByHash, - Version oldestIndexVersion, + IndexVersion oldestIndexVersion, Map reservedStateMetadata ) { this.clusterUUID = clusterUUID; @@ -638,7 +638,7 @@ public Metadata withAddedIndex(IndexMetadata index) { updatedVisibleClosedIndices, null, updatedMappingsByHash, - index.getCompatibilityVersion().before(oldestIndexVersion) ? index.getCompatibilityVersion() : oldestIndexVersion, + IndexVersion.min(IndexVersion.fromId(index.getCompatibilityVersion().id), oldestIndexVersion), reservedStateMetadata ); } @@ -719,7 +719,7 @@ public CoordinationMetadata coordinationMetadata() { return this.coordinationMetadata; } - public Version oldestIndexVersion() { + public IndexVersion oldestIndexVersion() { return this.oldestIndexVersion; } @@ -2272,7 +2272,7 @@ public Metadata build(boolean skipNameCollisionChecks) { final List visibleClosedIndices = new ArrayList<>(); final ImmutableOpenMap indicesMap = indices.build(); - int oldestIndexVersionId = Version.CURRENT.id; + int oldestIndexVersionId = IndexVersion.CURRENT.id(); int totalNumberOfShards = 0; int totalOpenIndexShards = 0; @@ -2362,7 +2362,7 @@ public Metadata build(boolean skipNameCollisionChecks) { visibleClosedIndicesArray, indicesLookup, Collections.unmodifiableMap(mappingsByHash), - Version.fromId(oldestIndexVersionId), + IndexVersion.fromId(oldestIndexVersionId), Collections.unmodifiableMap(reservedStateMetadata) ); } diff --git a/server/src/main/java/org/elasticsearch/env/NodeEnvironment.java b/server/src/main/java/org/elasticsearch/env/NodeEnvironment.java index 7d3e5fcfccfe3..92c106c5ac05f 100644 --- a/server/src/main/java/org/elasticsearch/env/NodeEnvironment.java +++ b/server/src/main/java/org/elasticsearch/env/NodeEnvironment.java @@ -47,6 +47,7 @@ import org.elasticsearch.gateway.PersistedClusterStateService; import org.elasticsearch.index.Index; import org.elasticsearch.index.IndexSettings; +import org.elasticsearch.index.IndexVersion; import org.elasticsearch.index.shard.ShardId; import org.elasticsearch.index.shard.ShardPath; import org.elasticsearch.index.store.FsDirectoryFactory; @@ -523,13 +524,13 @@ static void checkForIndexCompatibility(Logger logger, DataPath... dataPaths) thr if (metadata.oldestIndexVersion().isLegacyIndexVersion()) { throw new IllegalStateException( - "Cannot start this node because it holds metadata for indices created with version [" + "Cannot start this node because it holds metadata for indices with version [" + metadata.oldestIndexVersion() + "] with which this node of version [" + Version.CURRENT + "] is incompatible. Revert this node to version [" + Version.max(Version.CURRENT.minimumCompatibilityVersion(), metadata.previousNodeVersion()) - + "] and delete any indices created in versions earlier than [" + + "] and delete any indices with versions earlier than [" + Version.CURRENT.minimumIndexCompatibilityVersion() + "] before upgrading to version [" + Version.CURRENT @@ -623,7 +624,7 @@ private static NodeMetadata loadNodeMetadata(Settings settings, Logger logger, D assert nodeIds.isEmpty() : nodeIds; // If we couldn't find legacy metadata, we set the latest index version to this version. This happens // when we are starting a new node and there are no indices to worry about. - metadata = new NodeMetadata(generateNodeId(settings), Version.CURRENT, Version.CURRENT); + metadata = new NodeMetadata(generateNodeId(settings), Version.CURRENT, IndexVersion.CURRENT); } else { assert nodeIds.equals(Collections.singleton(legacyMetadata.nodeId())) : nodeIds + " doesn't match " + legacyMetadata; metadata = legacyMetadata; diff --git a/server/src/main/java/org/elasticsearch/env/NodeMetadata.java b/server/src/main/java/org/elasticsearch/env/NodeMetadata.java index e77e9b0709d7e..1de3539bf93ed 100644 --- a/server/src/main/java/org/elasticsearch/env/NodeMetadata.java +++ b/server/src/main/java/org/elasticsearch/env/NodeMetadata.java @@ -10,6 +10,7 @@ import org.elasticsearch.Version; import org.elasticsearch.gateway.MetadataStateFormat; +import org.elasticsearch.index.IndexVersion; import org.elasticsearch.xcontent.ObjectParser; import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xcontent.XContentBuilder; @@ -36,13 +37,13 @@ public final class NodeMetadata { private final Version previousNodeVersion; - private final Version oldestIndexVersion; + private final IndexVersion oldestIndexVersion; private NodeMetadata( final String nodeId, final Version nodeVersion, final Version previousNodeVersion, - final Version oldestIndexVersion + final IndexVersion oldestIndexVersion ) { this.nodeId = Objects.requireNonNull(nodeId); this.nodeVersion = Objects.requireNonNull(nodeVersion); @@ -50,7 +51,7 @@ private NodeMetadata( this.oldestIndexVersion = Objects.requireNonNull(oldestIndexVersion); } - public NodeMetadata(final String nodeId, final Version nodeVersion, final Version oldestIndexVersion) { + public NodeMetadata(final String nodeId, final Version nodeVersion, final IndexVersion oldestIndexVersion) { this(nodeId, nodeVersion, nodeVersion, oldestIndexVersion); } @@ -103,7 +104,7 @@ public Version previousNodeVersion() { return previousNodeVersion; } - public Version oldestIndexVersion() { + public IndexVersion oldestIndexVersion() { return oldestIndexVersion; } @@ -141,7 +142,7 @@ private static class Builder { String nodeId; Version nodeVersion; Version previousNodeVersion; - Version oldestIndexVersion; + IndexVersion oldestIndexVersion; public void setNodeId(String nodeId) { this.nodeId = nodeId; @@ -156,12 +157,12 @@ public void setPreviousNodeVersionId(int previousNodeVersionId) { } public void setOldestIndexVersion(int oldestIndexVersion) { - this.oldestIndexVersion = Version.fromId(oldestIndexVersion); + this.oldestIndexVersion = IndexVersion.fromId(oldestIndexVersion); } public NodeMetadata build() { final Version nodeVersion; - final Version oldestIndexVersion; + final IndexVersion oldestIndexVersion; if (this.nodeVersion == null) { assert Version.CURRENT.major <= Version.V_7_0_0.major + 1 : "version is required in the node metadata from v9 onwards"; nodeVersion = Version.V_EMPTY; @@ -172,7 +173,7 @@ public NodeMetadata build() { previousNodeVersion = nodeVersion; } if (this.oldestIndexVersion == null) { - oldestIndexVersion = Version.V_EMPTY; + oldestIndexVersion = IndexVersion.ZERO; } else { oldestIndexVersion = this.oldestIndexVersion; } @@ -208,7 +209,7 @@ protected XContentBuilder newXContentBuilder(XContentType type, OutputStream str public void toXContent(XContentBuilder builder, NodeMetadata nodeMetadata) throws IOException { builder.field(NODE_ID_KEY, nodeMetadata.nodeId); builder.field(NODE_VERSION_KEY, nodeMetadata.nodeVersion.id); - builder.field(OLDEST_INDEX_VERSION_KEY, nodeMetadata.oldestIndexVersion.id); + builder.field(OLDEST_INDEX_VERSION_KEY, nodeMetadata.oldestIndexVersion.id()); } @Override diff --git a/server/src/main/java/org/elasticsearch/gateway/PersistedClusterStateService.java b/server/src/main/java/org/elasticsearch/gateway/PersistedClusterStateService.java index 290360a4be211..3f9b55de7b5b1 100644 --- a/server/src/main/java/org/elasticsearch/gateway/PersistedClusterStateService.java +++ b/server/src/main/java/org/elasticsearch/gateway/PersistedClusterStateService.java @@ -70,6 +70,7 @@ import org.elasticsearch.core.TimeValue; import org.elasticsearch.env.NodeEnvironment; import org.elasticsearch.env.NodeMetadata; +import org.elasticsearch.index.IndexVersion; import org.elasticsearch.xcontent.NamedXContentRegistry; import org.elasticsearch.xcontent.ToXContent; import org.elasticsearch.xcontent.XContentBuilder; @@ -343,7 +344,7 @@ public record OnDiskStateMetadata( public static NodeMetadata nodeMetadata(Path... dataPaths) throws IOException { String nodeId = null; Version version = null; - Version oldestIndexVersion = Version.V_EMPTY; + IndexVersion oldestIndexVersion = IndexVersion.ZERO; for (final Path dataPath : dataPaths) { final Path indexPath = dataPath.resolve(METADATA_DIRECTORY_NAME); if (Files.exists(indexPath)) { @@ -361,9 +362,9 @@ public static NodeMetadata nodeMetadata(Path... dataPaths) throws IOException { nodeId = thisNodeId; version = Version.fromId(Integer.parseInt(userData.get(NODE_VERSION_KEY))); if (userData.containsKey(OLDEST_INDEX_VERSION_KEY)) { - oldestIndexVersion = Version.fromId(Integer.parseInt(userData.get(OLDEST_INDEX_VERSION_KEY))); + oldestIndexVersion = IndexVersion.fromId(Integer.parseInt(userData.get(OLDEST_INDEX_VERSION_KEY))); } else { - oldestIndexVersion = Version.V_EMPTY; + oldestIndexVersion = IndexVersion.ZERO; } } } catch (IndexNotFoundException e) { @@ -835,7 +836,7 @@ void prepareCommit( String nodeId, long currentTerm, long lastAcceptedVersion, - Version oldestIndexVersion, + IndexVersion oldestIndexVersion, String clusterUUID, boolean clusterUUIDCommitted ) throws IOException { @@ -846,7 +847,7 @@ void prepareCommit( commitData.put(CURRENT_TERM_KEY, Long.toString(currentTerm)); commitData.put(LAST_ACCEPTED_VERSION_KEY, Long.toString(lastAcceptedVersion)); commitData.put(NODE_VERSION_KEY, Integer.toString(Version.CURRENT.id)); - commitData.put(OLDEST_INDEX_VERSION_KEY, Integer.toString(oldestIndexVersion.id)); + commitData.put(OLDEST_INDEX_VERSION_KEY, Integer.toString(oldestIndexVersion.id())); commitData.put(NODE_ID_KEY, nodeId); commitData.put(CLUSTER_UUID_KEY, clusterUUID); commitData.put(CLUSTER_UUID_COMMITTED_KEY, Boolean.toString(clusterUUIDCommitted)); @@ -1211,7 +1212,7 @@ private WriterStats addMetadata(Metadata metadata) throws IOException { public void writeIncrementalTermUpdateAndCommit( long currentTerm, long lastAcceptedVersion, - Version oldestIndexVersion, + IndexVersion oldestIndexVersion, String clusterUUID, boolean clusterUUIDCommitted ) throws IOException { @@ -1223,7 +1224,7 @@ public void writeIncrementalTermUpdateAndCommit( void commit( long currentTerm, long lastAcceptedVersion, - Version oldestIndexVersion, + IndexVersion oldestIndexVersion, String clusterUUID, boolean clusterUUIDCommitted ) throws IOException { @@ -1251,7 +1252,7 @@ private boolean assertOnCommit() { private void prepareCommit( long currentTerm, long lastAcceptedVersion, - Version oldestIndexVersion, + IndexVersion oldestIndexVersion, String clusterUUID, boolean clusterUUIDCommitted ) throws IOException { diff --git a/server/src/test/java/org/elasticsearch/cluster/metadata/MetadataTests.java b/server/src/test/java/org/elasticsearch/cluster/metadata/MetadataTests.java index 462bbad505100..452d8ea235ee4 100644 --- a/server/src/test/java/org/elasticsearch/cluster/metadata/MetadataTests.java +++ b/server/src/test/java/org/elasticsearch/cluster/metadata/MetadataTests.java @@ -33,6 +33,7 @@ import org.elasticsearch.index.Index; import org.elasticsearch.index.IndexNotFoundException; import org.elasticsearch.index.IndexSettings; +import org.elasticsearch.index.IndexVersion; import org.elasticsearch.index.alias.RandomAliasActionsGenerator; import org.elasticsearch.index.mapper.MapperService; import org.elasticsearch.plugins.MapperPlugin; @@ -816,28 +817,29 @@ public void testFindMappingsWithFilters() throws IOException { public void testOldestIndexComputation() { Metadata metadata = buildIndicesWithVersions( - new Version[] { Version.V_7_0_0, Version.CURRENT, Version.fromId(Version.CURRENT.id + 1) } + IndexVersion.V_7_0_0, + IndexVersion.CURRENT, + IndexVersion.fromId(IndexVersion.CURRENT.id() + 1) ).build(); - assertEquals(Version.V_7_0_0, metadata.oldestIndexVersion()); + assertEquals(IndexVersion.V_7_0_0, metadata.oldestIndexVersion()); Metadata.Builder b = Metadata.builder(); - assertEquals(Version.CURRENT, b.build().oldestIndexVersion()); + assertEquals(IndexVersion.CURRENT, b.build().oldestIndexVersion()); Throwable ex = expectThrows( IllegalArgumentException.class, - () -> buildIndicesWithVersions(new Version[] { Version.V_7_0_0, Version.V_EMPTY, Version.fromId(Version.CURRENT.id + 1) }) + () -> buildIndicesWithVersions(IndexVersion.V_7_0_0, IndexVersion.ZERO, IndexVersion.fromId(IndexVersion.CURRENT.id() + 1)) .build() ); assertEquals("[index.version.created] is not present in the index settings for index with UUID [null]", ex.getMessage()); } - private Metadata.Builder buildIndicesWithVersions(Version... indexVersions) { - + private Metadata.Builder buildIndicesWithVersions(IndexVersion... indexVersions) { int lastIndexNum = randomIntBetween(9, 50); Metadata.Builder b = Metadata.builder(); - for (Version indexVersion : indexVersions) { + for (IndexVersion indexVersion : indexVersions) { IndexMetadata im = IndexMetadata.builder(DataStream.getDefaultBackingIndexName("index", lastIndexNum)) .settings(settings(indexVersion)) .numberOfShards(1) @@ -852,7 +854,7 @@ private Metadata.Builder buildIndicesWithVersions(Version... indexVersions) { private static IndexMetadata.Builder buildIndexMetadata(String name, String alias, Boolean writeIndex) { return IndexMetadata.builder(name) - .settings(settings(Version.CURRENT)) + .settings(settings(IndexVersion.CURRENT)) .creationDate(randomNonNegativeLong()) .putAlias(AliasMetadata.builder(alias).writeIndex(writeIndex)) .numberOfShards(1) diff --git a/server/src/test/java/org/elasticsearch/env/NodeEnvironmentTests.java b/server/src/test/java/org/elasticsearch/env/NodeEnvironmentTests.java index fc9ec28078c34..14d9090f83fad 100644 --- a/server/src/test/java/org/elasticsearch/env/NodeEnvironmentTests.java +++ b/server/src/test/java/org/elasticsearch/env/NodeEnvironmentTests.java @@ -32,6 +32,7 @@ import org.elasticsearch.gateway.PersistedClusterStateService; import org.elasticsearch.index.Index; import org.elasticsearch.index.IndexSettings; +import org.elasticsearch.index.IndexVersion; import org.elasticsearch.index.shard.ShardId; import org.elasticsearch.test.ESTestCase; import org.elasticsearch.test.IndexSettingsModule; @@ -580,7 +581,8 @@ public void testIndexCompatibilityChecks() throws IOException { ); } - Version oldIndexVersion = Version.fromId(between(1, Version.CURRENT.minimumIndexCompatibilityVersion().id - 1)); + Version oldVersion = Version.fromId(between(1, Version.CURRENT.minimumCompatibilityVersion().id - 1)); + IndexVersion oldIndexVersion = IndexVersion.fromId(between(1, IndexVersion.MINIMUM_COMPATIBLE.id() - 1)); Version previousNodeVersion = Version.fromId(between(Version.CURRENT.minimumCompatibilityVersion().id, Version.CURRENT.id - 1)); overrideOldestIndexVersion(oldIndexVersion, previousNodeVersion, env.nodeDataPaths()); @@ -594,21 +596,21 @@ public void testIndexCompatibilityChecks() throws IOException { ex.getMessage(), allOf( containsString("Cannot start this node"), - containsString("it holds metadata for indices created with version [" + oldIndexVersion + "]"), + containsString("it holds metadata for indices with version [" + oldIndexVersion + "]"), containsString("Revert this node to version [" + previousNodeVersion + "]") ) ); // This should work - overrideOldestIndexVersion(Version.CURRENT.minimumIndexCompatibilityVersion(), previousNodeVersion, env.nodeDataPaths()); + overrideOldestIndexVersion(IndexVersion.MINIMUM_COMPATIBLE, previousNodeVersion, env.nodeDataPaths()); checkForIndexCompatibility(logger, env.dataPaths()); // Trying to boot with newer version should pass this check - overrideOldestIndexVersion(NodeMetadataTests.tooNewVersion(), previousNodeVersion, env.nodeDataPaths()); + overrideOldestIndexVersion(NodeMetadataTests.tooNewIndexVersion(), previousNodeVersion, env.nodeDataPaths()); checkForIndexCompatibility(logger, env.dataPaths()); // Simulate empty old index version, attempting to upgrade before 7.17 - removeOldestIndexVersion(oldIndexVersion, env.nodeDataPaths()); + removeOldestIndexVersion(oldVersion, env.nodeDataPaths()); ex = expectThrows( IllegalStateException.class, @@ -616,7 +618,7 @@ public void testIndexCompatibilityChecks() throws IOException { () -> checkForIndexCompatibility(logger, env.dataPaths()) ); - assertThat(ex.getMessage(), startsWith("cannot upgrade a node from version [" + oldIndexVersion + "] directly")); + assertThat(ex.getMessage(), startsWith("cannot upgrade a node from version [" + oldVersion + "] directly")); assertThat(ex.getMessage(), containsString("upgrade to version [" + Version.CURRENT.minimumCompatibilityVersion())); } } @@ -714,7 +716,7 @@ public NodeEnvironment newNodeEnvironment(String[] dataPaths, String sharedDataP return new NodeEnvironment(build, TestEnvironment.newEnvironment(build)); } - private static void overrideOldestIndexVersion(Version oldestIndexVersion, Version previousNodeVersion, Path... dataPaths) + private static void overrideOldestIndexVersion(IndexVersion oldestIndexVersion, Version previousNodeVersion, Path... dataPaths) throws IOException { for (final Path dataPath : dataPaths) { final Path indexPath = dataPath.resolve(METADATA_DIRECTORY_NAME); @@ -731,7 +733,7 @@ private static void overrideOldestIndexVersion(Version oldestIndexVersion, Versi ) { final Map commitData = new HashMap<>(userData); commitData.put(NODE_VERSION_KEY, Integer.toString(previousNodeVersion.id)); - commitData.put(OLDEST_INDEX_VERSION_KEY, Integer.toString(oldestIndexVersion.id)); + commitData.put(OLDEST_INDEX_VERSION_KEY, Integer.toString(oldestIndexVersion.id())); indexWriter.setLiveCommitData(commitData.entrySet()); indexWriter.commit(); } diff --git a/server/src/test/java/org/elasticsearch/env/NodeMetadataTests.java b/server/src/test/java/org/elasticsearch/env/NodeMetadataTests.java index 5f140e5003f1f..f6fe14fceb925 100644 --- a/server/src/test/java/org/elasticsearch/env/NodeMetadataTests.java +++ b/server/src/test/java/org/elasticsearch/env/NodeMetadataTests.java @@ -10,9 +10,11 @@ import org.elasticsearch.Version; import org.elasticsearch.core.Tuple; import org.elasticsearch.gateway.MetadataStateFormat; +import org.elasticsearch.index.IndexVersion; import org.elasticsearch.test.ESTestCase; import org.elasticsearch.test.EqualsHashCodeTestUtils; import org.elasticsearch.test.VersionUtils; +import org.elasticsearch.test.index.IndexVersionUtils; import java.io.IOException; import java.io.InputStream; @@ -26,16 +28,20 @@ import static org.hamcrest.Matchers.startsWith; public class NodeMetadataTests extends ESTestCase { + // (Index)VersionUtils.randomVersion() only returns known versions, which are necessarily no later than (Index)Version.CURRENT; + // however we want to also consider our behaviour with all versions, so occasionally pick up a truly random version. private Version randomVersion() { - // VersionUtils.randomVersion() only returns known versions, which are necessarily no later than Version.CURRENT; however we want - // also to consider our behaviour with all versions, so occasionally pick up a truly random version. return rarely() ? Version.fromId(randomInt()) : VersionUtils.randomVersion(random()); } + private IndexVersion randomIndexVersion() { + return rarely() ? IndexVersion.fromId(randomInt()) : IndexVersionUtils.randomVersion(random()); + } + public void testEqualsHashcodeSerialization() { final Path tempDir = createTempDir(); EqualsHashCodeTestUtils.checkEqualsAndHashCode( - new NodeMetadata(randomAlphaOfLength(10), randomVersion(), randomVersion()), + new NodeMetadata(randomAlphaOfLength(10), randomVersion(), randomIndexVersion()), nodeMetadata -> { final long generation = NodeMetadata.FORMAT.writeAndCleanup(nodeMetadata, tempDir); final Tuple nodeMetadataLongTuple = NodeMetadata.FORMAT.loadLatestStateWithGeneration( @@ -60,7 +66,7 @@ public void testEqualsHashcodeSerialization() { default -> new NodeMetadata( nodeMetadata.nodeId(), nodeMetadata.nodeVersion(), - randomValueOtherThan(nodeMetadata.oldestIndexVersion(), this::randomVersion) + randomValueOtherThan(nodeMetadata.oldestIndexVersion(), this::randomIndexVersion) ); } ); @@ -90,7 +96,7 @@ public void testUpgradesLegitimateVersions() { v -> v.after(Version.CURRENT) || v.before(Version.CURRENT.minimumCompatibilityVersion()), this::randomVersion ), - Version.CURRENT + IndexVersion.CURRENT ).upgradeToCurrentVersion(); assertThat(nodeMetadata.nodeVersion(), equalTo(Version.CURRENT)); assertThat(nodeMetadata.nodeId(), equalTo(nodeId)); @@ -101,7 +107,7 @@ public void testUpgradesMissingVersion() { final IllegalStateException illegalStateException = expectThrows( IllegalStateException.class, - () -> new NodeMetadata(nodeId, Version.V_EMPTY, Version.CURRENT).upgradeToCurrentVersion() + () -> new NodeMetadata(nodeId, Version.V_EMPTY, IndexVersion.CURRENT).upgradeToCurrentVersion() ); assertThat( illegalStateException.getMessage(), @@ -112,7 +118,7 @@ public void testUpgradesMissingVersion() { public void testDoesNotUpgradeFutureVersion() { final IllegalStateException illegalStateException = expectThrows( IllegalStateException.class, - () -> new NodeMetadata(randomAlphaOfLength(10), tooNewVersion(), Version.CURRENT).upgradeToCurrentVersion() + () -> new NodeMetadata(randomAlphaOfLength(10), tooNewVersion(), IndexVersion.CURRENT).upgradeToCurrentVersion() ); assertThat( illegalStateException.getMessage(), @@ -123,7 +129,7 @@ public void testDoesNotUpgradeFutureVersion() { public void testDoesNotUpgradeAncientVersion() { final IllegalStateException illegalStateException = expectThrows( IllegalStateException.class, - () -> new NodeMetadata(randomAlphaOfLength(10), tooOldVersion(), Version.CURRENT).upgradeToCurrentVersion() + () -> new NodeMetadata(randomAlphaOfLength(10), tooOldVersion(), IndexVersion.CURRENT).upgradeToCurrentVersion() ); assertThat( illegalStateException.getMessage(), @@ -144,7 +150,7 @@ public void testUpgradeMarksPreviousVersion() { final String nodeId = randomAlphaOfLength(10); final Version version = VersionUtils.randomVersionBetween(random(), Version.CURRENT.minimumCompatibilityVersion(), Version.V_8_0_0); - final NodeMetadata nodeMetadata = new NodeMetadata(nodeId, version, Version.CURRENT).upgradeToCurrentVersion(); + final NodeMetadata nodeMetadata = new NodeMetadata(nodeId, version, IndexVersion.CURRENT).upgradeToCurrentVersion(); assertThat(nodeMetadata.nodeVersion(), equalTo(Version.CURRENT)); assertThat(nodeMetadata.previousNodeVersion(), equalTo(version)); } @@ -153,6 +159,10 @@ public static Version tooNewVersion() { return Version.fromId(between(Version.CURRENT.id + 1, 99999999)); } + public static IndexVersion tooNewIndexVersion() { + return IndexVersion.fromId(between(IndexVersion.CURRENT.id() + 1, 99999999)); + } + public static Version tooOldVersion() { return Version.fromId(between(1, Version.CURRENT.minimumCompatibilityVersion().id - 1)); } diff --git a/server/src/test/java/org/elasticsearch/gateway/PersistedClusterStateServiceTests.java b/server/src/test/java/org/elasticsearch/gateway/PersistedClusterStateServiceTests.java index 327cafb061c35..c1ea3f04170d8 100644 --- a/server/src/test/java/org/elasticsearch/gateway/PersistedClusterStateServiceTests.java +++ b/server/src/test/java/org/elasticsearch/gateway/PersistedClusterStateServiceTests.java @@ -57,6 +57,7 @@ import org.elasticsearch.env.NodeMetadata; import org.elasticsearch.gateway.PersistedClusterStateService.Writer; import org.elasticsearch.index.Index; +import org.elasticsearch.index.IndexVersion; import org.elasticsearch.index.mapper.MapperService; import org.elasticsearch.test.CorruptionUtils; import org.elasticsearch.test.ESTestCase; @@ -1488,12 +1489,15 @@ public void testOldestIndexVersionIsCorrectlySerialized() throws IOException { final Path[] dataPaths2 = createDataPaths(); final Path[] combinedPaths = Stream.concat(Arrays.stream(dataPaths1), Arrays.stream(dataPaths2)).toArray(Path[]::new); - Version oldVersion = Version.fromId(Version.CURRENT.minimumIndexCompatibilityVersion().id - 1); + IndexVersion oldVersion = IndexVersion.fromId(IndexVersion.MINIMUM_COMPATIBLE.id() - 1); - final Version[] indexVersions = new Version[] { oldVersion, Version.CURRENT, Version.fromId(Version.CURRENT.id + 1) }; + final IndexVersion[] indexVersions = new IndexVersion[] { + oldVersion, + IndexVersion.CURRENT, + IndexVersion.fromId(IndexVersion.CURRENT.id() + 1) }; int lastIndexNum = randomIntBetween(9, 50); Metadata.Builder b = Metadata.builder(); - for (Version indexVersion : indexVersions) { + for (IndexVersion indexVersion : indexVersions) { String indexUUID = UUIDs.randomBase64UUID(random()); IndexMetadata im = IndexMetadata.builder(DataStream.getDefaultBackingIndexName("index", lastIndexNum)) .putMapping(randomMappingMetadataOrNull()) diff --git a/test/framework/src/main/java/org/elasticsearch/test/ESTestCase.java b/test/framework/src/main/java/org/elasticsearch/test/ESTestCase.java index 4955750eca8e9..b0d4e0dfd4d52 100644 --- a/test/framework/src/main/java/org/elasticsearch/test/ESTestCase.java +++ b/test/framework/src/main/java/org/elasticsearch/test/ESTestCase.java @@ -1293,16 +1293,29 @@ public Environment newEnvironment(Settings settings) { } /** Return consistent index settings for the provided index version. */ + @Deprecated public static Settings.Builder settings(Version version) { return Settings.builder().put(IndexMetadata.SETTING_VERSION_CREATED, version); } + /** Return consistent index settings for the provided index version. */ + public static Settings.Builder settings(IndexVersion version) { + return Settings.builder().put(IndexMetadata.SETTING_VERSION_CREATED, version.id()); + } + /** Return consistent index settings for the provided index version, shard- and replica-count. */ + @Deprecated public static Settings.Builder indexSettings(Version indexVersionCreated, int shards, int replicas) { return settings(indexVersionCreated).put(IndexMetadata.SETTING_NUMBER_OF_SHARDS, shards) .put(IndexMetadata.SETTING_NUMBER_OF_REPLICAS, replicas); } + /** Return consistent index settings for the provided index version, shard- and replica-count. */ + public static Settings.Builder indexSettings(IndexVersion indexVersionCreated, int shards, int replicas) { + return settings(indexVersionCreated).put(IndexMetadata.SETTING_NUMBER_OF_SHARDS, shards) + .put(IndexMetadata.SETTING_NUMBER_OF_REPLICAS, replicas); + } + /** Return consistent index settings for the provided shard- and replica-count. */ public static Settings.Builder indexSettings(int shards, int replicas) { return Settings.builder() diff --git a/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/SecurityImplicitBehaviorBootstrapCheckTests.java b/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/SecurityImplicitBehaviorBootstrapCheckTests.java index 4297fecdb01a5..27796b5ad37aa 100644 --- a/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/SecurityImplicitBehaviorBootstrapCheckTests.java +++ b/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/SecurityImplicitBehaviorBootstrapCheckTests.java @@ -13,6 +13,7 @@ import org.elasticsearch.common.settings.Settings; import org.elasticsearch.core.TimeValue; import org.elasticsearch.env.NodeMetadata; +import org.elasticsearch.index.IndexVersion; import org.elasticsearch.license.ClusterStateLicenseService; import org.elasticsearch.license.License; import org.elasticsearch.license.LicensesMetadata; @@ -33,7 +34,7 @@ public void testFailureUpgradeFrom7xWithImplicitSecuritySettings() throws Except Version.V_8_0_0, () -> VersionUtils.randomVersionBetween(random(), Version.CURRENT.minimumCompatibilityVersion(), Version.V_8_0_0) ); - NodeMetadata nodeMetadata = new NodeMetadata(randomAlphaOfLength(10), previousVersion, Version.CURRENT); + NodeMetadata nodeMetadata = new NodeMetadata(randomAlphaOfLength(10), previousVersion, IndexVersion.CURRENT); nodeMetadata = nodeMetadata.upgradeToCurrentVersion(); ClusterStateLicenseService licenseService = mock(ClusterStateLicenseService.class); Metadata metadata = createLicensesMetadata(previousVersion, randomFrom("basic", "trial")); @@ -67,7 +68,7 @@ public void testUpgradeFrom7xWithImplicitSecuritySettingsOnGoldPlus() throws Exc Version.V_8_0_0, () -> VersionUtils.randomVersionBetween(random(), Version.CURRENT.minimumCompatibilityVersion(), Version.V_8_0_0) ); - NodeMetadata nodeMetadata = new NodeMetadata(randomAlphaOfLength(10), previousVersion, Version.CURRENT); + NodeMetadata nodeMetadata = new NodeMetadata(randomAlphaOfLength(10), previousVersion, IndexVersion.CURRENT); nodeMetadata = nodeMetadata.upgradeToCurrentVersion(); ClusterStateLicenseService licenseService = mock(ClusterStateLicenseService.class); Metadata metadata = createLicensesMetadata(previousVersion, randomFrom("gold", "platinum")); @@ -85,7 +86,7 @@ public void testUpgradeFrom7xWithExplicitSecuritySettings() throws Exception { Version.V_8_0_0, () -> VersionUtils.randomVersionBetween(random(), Version.CURRENT.minimumCompatibilityVersion(), Version.V_8_0_0) ); - NodeMetadata nodeMetadata = new NodeMetadata(randomAlphaOfLength(10), previousVersion, Version.CURRENT); + NodeMetadata nodeMetadata = new NodeMetadata(randomAlphaOfLength(10), previousVersion, IndexVersion.CURRENT); nodeMetadata = nodeMetadata.upgradeToCurrentVersion(); ClusterStateLicenseService licenseService = mock(ClusterStateLicenseService.class); BootstrapCheck.BootstrapCheckResult result = new SecurityImplicitBehaviorBootstrapCheck(nodeMetadata, licenseService).check( @@ -99,7 +100,7 @@ public void testUpgradeFrom7xWithExplicitSecuritySettings() throws Exception { public void testUpgradeFrom8xWithImplicitSecuritySettings() throws Exception { final Version previousVersion = VersionUtils.randomVersionBetween(random(), Version.V_8_0_0, null); - NodeMetadata nodeMetadata = new NodeMetadata(randomAlphaOfLength(10), previousVersion, Version.CURRENT); + NodeMetadata nodeMetadata = new NodeMetadata(randomAlphaOfLength(10), previousVersion, IndexVersion.CURRENT); nodeMetadata = nodeMetadata.upgradeToCurrentVersion(); ClusterStateLicenseService licenseService = mock(ClusterStateLicenseService.class); BootstrapCheck.BootstrapCheckResult result = new SecurityImplicitBehaviorBootstrapCheck(nodeMetadata, licenseService).check( @@ -110,7 +111,7 @@ public void testUpgradeFrom8xWithImplicitSecuritySettings() throws Exception { public void testUpgradeFrom8xWithExplicitSecuritySettings() throws Exception { final Version previousVersion = VersionUtils.randomVersionBetween(random(), Version.V_8_0_0, null); - NodeMetadata nodeMetadata = new NodeMetadata(randomAlphaOfLength(10), previousVersion, Version.CURRENT); + NodeMetadata nodeMetadata = new NodeMetadata(randomAlphaOfLength(10), previousVersion, IndexVersion.CURRENT); nodeMetadata = nodeMetadata.upgradeToCurrentVersion(); ClusterStateLicenseService licenseService = mock(ClusterStateLicenseService.class); BootstrapCheck.BootstrapCheckResult result = new SecurityImplicitBehaviorBootstrapCheck(nodeMetadata, licenseService).check( diff --git a/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/SecurityTests.java b/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/SecurityTests.java index c58622afffdd7..31ee57f971b6d 100644 --- a/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/SecurityTests.java +++ b/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/SecurityTests.java @@ -37,6 +37,7 @@ import org.elasticsearch.env.TestEnvironment; import org.elasticsearch.index.IndexModule; import org.elasticsearch.index.IndexSettings; +import org.elasticsearch.index.IndexVersion; import org.elasticsearch.index.analysis.AnalysisRegistry; import org.elasticsearch.index.engine.InternalEngineFactory; import org.elasticsearch.indices.TestIndexNameExpressionResolver; @@ -188,7 +189,7 @@ protected SSLService getSslService() { private Collection createComponentsUtil(Settings settings) throws Exception { Environment env = TestEnvironment.newEnvironment(settings); - NodeMetadata nodeMetadata = new NodeMetadata(randomAlphaOfLength(8), Version.CURRENT, Version.CURRENT); + NodeMetadata nodeMetadata = new NodeMetadata(randomAlphaOfLength(8), Version.CURRENT, IndexVersion.CURRENT); ThreadPool threadPool = mock(ThreadPool.class); ClusterService clusterService = mock(ClusterService.class); settings = Security.additionalSettings(settings, true);