add the version the index was created with (as a setting) to support better upgrade in the future between versions

This commit is contained in:
Shay Banon 2012-01-24 13:28:15 +02:00
parent c1a2a5c910
commit 68a0dca577
5 changed files with 36 additions and 1 deletions

View File

@ -115,6 +115,7 @@ public class IndexMetaData {
public static final String SETTING_NUMBER_OF_REPLICAS = "index.number_of_replicas";
public static final String SETTING_AUTO_EXPAND_REPLICAS = "index.auto_expand_replicas";
public static final String SETTING_READ_ONLY = "index.blocks.read_only";
public static final String SETTING_VERSION_CREATED = "index.version.created";
private final String index;
private final long version;

View File

@ -24,6 +24,7 @@ import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.google.common.collect.Sets;
import org.elasticsearch.ElasticSearchException;
import org.elasticsearch.Version;
import org.elasticsearch.cluster.ClusterService;
import org.elasticsearch.cluster.ClusterState;
import org.elasticsearch.cluster.ProcessedClusterStateUpdateTask;
@ -211,6 +212,9 @@ public class MetaDataCreateIndexService extends AbstractComponent {
}
}
}
indexSettingsBuilder.put(SETTING_VERSION_CREATED, Version.CURRENT);
Settings actualIndexSettings = indexSettingsBuilder.build();
// Set up everything, now locally create the index to see that things are ok, and apply

View File

@ -21,6 +21,7 @@ package org.elasticsearch.common.settings;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.Lists;
import org.elasticsearch.Version;
import org.elasticsearch.common.Booleans;
import org.elasticsearch.common.Classes;
import org.elasticsearch.common.Strings;
@ -304,6 +305,19 @@ public class ImmutableSettings implements Settings {
return Collections.unmodifiableMap(retVal);
}
@Override
public Version getAsVersion(String setting, Version defaultVersion) throws SettingsException {
String sValue = get(setting);
if (sValue == null) {
return defaultVersion;
}
try {
return Version.fromId(Integer.parseInt(sValue));
} catch (Exception e) {
throw new SettingsException("Failed to parse version setting [" + setting + "] with value [" + sValue + "]", e);
}
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
@ -440,6 +454,11 @@ public class ImmutableSettings implements Settings {
return this;
}
public Builder put(String setting, Version version) {
put(setting, version.id);
return this;
}
/**
* Sets the setting with the provided setting key and the long value.
*

View File

@ -20,6 +20,7 @@
package org.elasticsearch.common.settings;
import com.google.common.collect.ImmutableMap;
import org.elasticsearch.Version;
import org.elasticsearch.common.unit.ByteSizeValue;
import org.elasticsearch.common.unit.SizeValue;
import org.elasticsearch.common.unit.TimeValue;
@ -231,6 +232,11 @@ public interface Settings {
*/
String[] getAsArray(String settingPrefix) throws SettingsException;
/**
* Retruns a parsed version.
*/
Version getAsVersion(String setting, Version defaultVersion) throws SettingsException;
/**
* A settings builder interface.
*/

View File

@ -20,6 +20,7 @@
package org.elasticsearch.gateway.local.state.meta;
import com.google.common.io.Closeables;
import org.elasticsearch.Version;
import org.elasticsearch.cluster.ClusterChangedEvent;
import org.elasticsearch.cluster.ClusterStateListener;
import org.elasticsearch.cluster.metadata.IndexMetaData;
@ -30,6 +31,7 @@ import org.elasticsearch.common.inject.Inject;
import org.elasticsearch.common.io.FileSystemUtils;
import org.elasticsearch.common.io.Streams;
import org.elasticsearch.common.io.stream.CachedStreamOutput;
import org.elasticsearch.common.settings.ImmutableSettings;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.unit.TimeValue;
import org.elasticsearch.common.xcontent.*;
@ -421,7 +423,10 @@ public class LocalGatewayMetaState extends AbstractComponent implements ClusterS
writeGlobalState("upgrade", MetaData.builder().metaData(metaData).version(version).build(), null);
for (IndexMetaData indexMetaData : metaData) {
writeIndex("upgrade", IndexMetaData.newIndexMetaDataBuilder(indexMetaData).version(version).build(), null);
IndexMetaData.Builder indexMetaDataBuilder = IndexMetaData.newIndexMetaDataBuilder(indexMetaData).version(version);
// set the created version to 0.18
indexMetaDataBuilder.settings(ImmutableSettings.settingsBuilder().put(indexMetaData.settings()).put(IndexMetaData.SETTING_VERSION_CREATED, Version.V_0_18_0));
writeIndex("upgrade", indexMetaDataBuilder.build(), null);
}
// rename shards state to backup state