Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -231,7 +231,7 @@ default boolean isRestorable() {
private volatile SortedMap<String, IndexAbstraction> indicesLookup;
private final Map<String, MappingMetadata> mappingsByHash;

private final Version oldestIndexVersion;
private final IndexVersion oldestIndexVersion;

private Metadata(
String clusterUUID,
Expand All @@ -256,7 +256,7 @@ private Metadata(
String[] visibleClosedIndices,
SortedMap<String, IndexAbstraction> indicesLookup,
Map<String, MappingMetadata> mappingsByHash,
Version oldestIndexVersion,
IndexVersion oldestIndexVersion,
Map<String, ReservedStateMetadata> reservedStateMetadata
) {
this.clusterUUID = clusterUUID;
Expand Down Expand Up @@ -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
);
}
Expand Down Expand Up @@ -719,7 +719,7 @@ public CoordinationMetadata coordinationMetadata() {
return this.coordinationMetadata;
}

public Version oldestIndexVersion() {
public IndexVersion oldestIndexVersion() {
return this.oldestIndexVersion;
}

Expand Down Expand Up @@ -2272,7 +2272,7 @@ public Metadata build(boolean skipNameCollisionChecks) {
final List<String> visibleClosedIndices = new ArrayList<>();
final ImmutableOpenMap<String, IndexMetadata> indicesMap = indices.build();

int oldestIndexVersionId = Version.CURRENT.id;
int oldestIndexVersionId = IndexVersion.CURRENT.id();
int totalNumberOfShards = 0;
int totalOpenIndexShards = 0;

Expand Down Expand Up @@ -2362,7 +2362,7 @@ public Metadata build(boolean skipNameCollisionChecks) {
visibleClosedIndicesArray,
indicesLookup,
Collections.unmodifiableMap(mappingsByHash),
Version.fromId(oldestIndexVersionId),
IndexVersion.fromId(oldestIndexVersionId),
Collections.unmodifiableMap(reservedStateMetadata)
);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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;
Expand Down
19 changes: 10 additions & 9 deletions server/src/main/java/org/elasticsearch/env/NodeMetadata.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -36,21 +37,21 @@ 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);
this.previousNodeVersion = Objects.requireNonNull(previousNodeVersion);
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);
}

Expand Down Expand Up @@ -103,7 +104,7 @@ public Version previousNodeVersion() {
return previousNodeVersion;
}

public Version oldestIndexVersion() {
public IndexVersion oldestIndexVersion() {
return oldestIndexVersion;
}

Expand Down Expand Up @@ -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;
Expand All @@ -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;
Expand All @@ -172,7 +173,7 @@ public NodeMetadata build() {
previousNodeVersion = nodeVersion;
}
if (this.oldestIndexVersion == null) {
oldestIndexVersion = Version.V_EMPTY;
oldestIndexVersion = IndexVersion.ZERO;
} else {
oldestIndexVersion = this.oldestIndexVersion;
}
Expand Down Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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)) {
Expand All @@ -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) {
Expand Down Expand Up @@ -835,7 +836,7 @@ void prepareCommit(
String nodeId,
long currentTerm,
long lastAcceptedVersion,
Version oldestIndexVersion,
IndexVersion oldestIndexVersion,
String clusterUUID,
boolean clusterUUIDCommitted
) throws IOException {
Expand All @@ -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));
Expand Down Expand Up @@ -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 {
Expand All @@ -1223,7 +1224,7 @@ public void writeIncrementalTermUpdateAndCommit(
void commit(
long currentTerm,
long lastAcceptedVersion,
Version oldestIndexVersion,
IndexVersion oldestIndexVersion,
String clusterUUID,
boolean clusterUUIDCommitted
) throws IOException {
Expand Down Expand Up @@ -1251,7 +1252,7 @@ private boolean assertOnCommit() {
private void prepareCommit(
long currentTerm,
long lastAcceptedVersion,
Version oldestIndexVersion,
IndexVersion oldestIndexVersion,
String clusterUUID,
boolean clusterUUIDCommitted
) throws IOException {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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)
Expand All @@ -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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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());

Expand All @@ -594,29 +596,29 @@ 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,
"Must fail the check on index that's too old",
() -> 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()));
}
}
Expand Down Expand Up @@ -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);
Expand All @@ -731,7 +733,7 @@ private static void overrideOldestIndexVersion(Version oldestIndexVersion, Versi
) {
final Map<String, String> 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();
}
Expand Down
Loading