Skip to content

Commit

Permalink
Introduce aliases version (#41397)
Browse files Browse the repository at this point in the history
This commit introduces aliases versions to index metadata. This will be
useful in CCR when we replicate aliases.
  • Loading branch information
jasontedor committed Apr 23, 2019
1 parent 7c2e151 commit 65af47e
Show file tree
Hide file tree
Showing 6 changed files with 513 additions and 130 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -341,6 +341,7 @@ public String toString() {
sb.append(": v[").append(indexMetaData.getVersion())
.append("], mv[").append(indexMetaData.getMappingVersion())
.append("], sv[").append(indexMetaData.getSettingsVersion())
.append("], av[").append(indexMetaData.getAliasesVersion())
.append("]\n");
for (int shard = 0; shard < indexMetaData.getNumberOfShards(); shard++) {
sb.append(TAB).append(TAB).append(shard).append(": ");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -260,6 +260,7 @@ public Iterator<Setting<Integer>> settings() {
static final String KEY_VERSION = "version";
static final String KEY_MAPPING_VERSION = "mapping_version";
static final String KEY_SETTINGS_VERSION = "settings_version";
static final String KEY_ALIASES_VERSION = "aliases_version";
static final String KEY_ROUTING_NUM_SHARDS = "routing_num_shards";
static final String KEY_SETTINGS = "settings";
static final String KEY_STATE = "state";
Expand All @@ -282,6 +283,8 @@ public Iterator<Setting<Integer>> settings() {
private final long mappingVersion;

private final long settingsVersion;

private final long aliasesVersion;

private final long[] primaryTerms;

Expand Down Expand Up @@ -310,22 +313,40 @@ public Iterator<Setting<Integer>> settings() {
private final ActiveShardCount waitForActiveShards;
private final ImmutableOpenMap<String, RolloverInfo> rolloverInfos;

private IndexMetaData(Index index, long version, long mappingVersion, long settingsVersion, long[] primaryTerms, State state,
int numberOfShards, int numberOfReplicas, Settings settings,
ImmutableOpenMap<String, MappingMetaData> mappings, ImmutableOpenMap<String, AliasMetaData> aliases,
ImmutableOpenMap<String, DiffableStringMap> customData, ImmutableOpenIntMap<Set<String>> inSyncAllocationIds,
DiscoveryNodeFilters requireFilters, DiscoveryNodeFilters initialRecoveryFilters,
DiscoveryNodeFilters includeFilters, DiscoveryNodeFilters excludeFilters,
Version indexCreatedVersion, Version indexUpgradedVersion,
int routingNumShards, int routingPartitionSize, ActiveShardCount waitForActiveShards,
ImmutableOpenMap<String, RolloverInfo> rolloverInfos) {
private IndexMetaData(
final Index index,
final long version,
final long mappingVersion,
final long settingsVersion,
final long aliasesVersion,
final long[] primaryTerms,
final State state,
final int numberOfShards,
final int numberOfReplicas,
final Settings settings,
final ImmutableOpenMap<String, MappingMetaData> mappings,
final ImmutableOpenMap<String, AliasMetaData> aliases,
final ImmutableOpenMap<String, DiffableStringMap> customData,
final ImmutableOpenIntMap<Set<String>> inSyncAllocationIds,
final DiscoveryNodeFilters requireFilters,
final DiscoveryNodeFilters initialRecoveryFilters,
final DiscoveryNodeFilters includeFilters,
final DiscoveryNodeFilters excludeFilters,
final Version indexCreatedVersion,
final Version indexUpgradedVersion,
final int routingNumShards,
final int routingPartitionSize,
final ActiveShardCount waitForActiveShards,
final ImmutableOpenMap<String, RolloverInfo> rolloverInfos) {

this.index = index;
this.version = version;
assert mappingVersion >= 0 : mappingVersion;
this.mappingVersion = mappingVersion;
assert settingsVersion >= 0 : settingsVersion;
this.settingsVersion = settingsVersion;
assert aliasesVersion >= 0 : aliasesVersion;
this.aliasesVersion = aliasesVersion;
this.primaryTerms = primaryTerms;
assert primaryTerms.length == numberOfShards;
this.state = state;
Expand Down Expand Up @@ -383,6 +404,10 @@ public long getSettingsVersion() {
return settingsVersion;
}

public long getAliasesVersion() {
return aliasesVersion;
}

/**
* The term of the current selected primary. This is a non-negative number incremented when
* a primary shard is assigned after a full cluster restart or a replica shard is promoted to a primary.
Expand Down Expand Up @@ -652,6 +677,7 @@ private static class IndexMetaDataDiff implements Diff<IndexMetaData> {
private final long version;
private final long mappingVersion;
private final long settingsVersion;
private final long aliasesVersion;
private final long[] primaryTerms;
private final State state;
private final Settings settings;
Expand All @@ -666,6 +692,7 @@ private static class IndexMetaDataDiff implements Diff<IndexMetaData> {
version = after.version;
mappingVersion = after.mappingVersion;
settingsVersion = after.settingsVersion;
aliasesVersion = after.aliasesVersion;
routingNumShards = after.routingNumShards;
state = after.state;
settings = after.settings;
Expand All @@ -692,6 +719,11 @@ private static class IndexMetaDataDiff implements Diff<IndexMetaData> {
} else {
settingsVersion = 1;
}
if (in.getVersion().onOrAfter(Version.V_7_1_0)) {
aliasesVersion = in.readVLong();
} else {
aliasesVersion = 1;
}
state = State.fromId(in.readByte());
settings = Settings.readSettingsFromStream(in);
primaryTerms = in.readVLongArray();
Expand Down Expand Up @@ -723,6 +755,9 @@ public void writeTo(StreamOutput out) throws IOException {
if (out.getVersion().onOrAfter(Version.V_6_5_0)) {
out.writeVLong(settingsVersion);
}
if (out.getVersion().onOrAfter(Version.V_7_1_0)) {
out.writeVLong(aliasesVersion);
}
out.writeByte(state.id);
Settings.writeSettingsToStream(settings, out);
out.writeVLongArray(primaryTerms);
Expand All @@ -741,6 +776,7 @@ public IndexMetaData apply(IndexMetaData part) {
builder.version(version);
builder.mappingVersion(mappingVersion);
builder.settingsVersion(settingsVersion);
builder.aliasesVersion(aliasesVersion);
builder.setRoutingNumShards(routingNumShards);
builder.state(state);
builder.settings(settings);
Expand All @@ -767,6 +803,11 @@ public static IndexMetaData readFrom(StreamInput in) throws IOException {
} else {
builder.settingsVersion(1);
}
if (in.getVersion().onOrAfter(Version.V_7_1_0)) {
builder.aliasesVersion(in.readVLong());
} else {
builder.aliasesVersion(1);
}
builder.setRoutingNumShards(in.readInt());
builder.state(State.fromId(in.readByte()));
builder.settings(readSettingsFromStream(in));
Expand Down Expand Up @@ -819,6 +860,9 @@ public void writeTo(StreamOutput out) throws IOException {
if (out.getVersion().onOrAfter(Version.V_6_5_0)) {
out.writeVLong(settingsVersion);
}
if (out.getVersion().onOrAfter(Version.V_7_1_0)) {
out.writeVLong(aliasesVersion);
}
out.writeInt(routingNumShards);
out.writeByte(state.id());
writeSettingsToStream(settings, out);
Expand Down Expand Up @@ -868,6 +912,7 @@ public static class Builder {
private long version = 1;
private long mappingVersion = 1;
private long settingsVersion = 1;
private long aliasesVersion = 1;
private long[] primaryTerms = null;
private Settings settings = Settings.Builder.EMPTY_SETTINGS;
private final ImmutableOpenMap.Builder<String, MappingMetaData> mappings;
Expand All @@ -892,6 +937,7 @@ public Builder(IndexMetaData indexMetaData) {
this.version = indexMetaData.version;
this.mappingVersion = indexMetaData.mappingVersion;
this.settingsVersion = indexMetaData.settingsVersion;
this.aliasesVersion = indexMetaData.aliasesVersion;
this.settings = indexMetaData.getSettings();
this.primaryTerms = indexMetaData.primaryTerms.clone();
this.mappings = ImmutableOpenMap.builder(indexMetaData.mappings);
Expand Down Expand Up @@ -1040,20 +1086,29 @@ public long mappingVersion() {
return mappingVersion;
}

public long settingsVersion() {
return settingsVersion;
}

public Builder mappingVersion(final long mappingVersion) {
this.mappingVersion = mappingVersion;
return this;
}


public long settingsVersion() {
return settingsVersion;
}

public Builder settingsVersion(final long settingsVersion) {
this.settingsVersion = settingsVersion;
return this;
}


public long aliasesVersion() {
return aliasesVersion;
}

public Builder aliasesVersion(final long aliasesVersion) {
this.aliasesVersion = aliasesVersion;
return this;
}

/**
* returns the primary term for the given shard.
* See {@link IndexMetaData#primaryTerm(int)} for more information.
Expand Down Expand Up @@ -1182,11 +1237,31 @@ public IndexMetaData build() {

final String uuid = settings.get(SETTING_INDEX_UUID, INDEX_UUID_NA_VALUE);

return new IndexMetaData(new Index(index, uuid), version, mappingVersion, settingsVersion, primaryTerms, state,
numberOfShards, numberOfReplicas, tmpSettings, mappings.build(), tmpAliases.build(), customMetaData.build(),
filledInSyncAllocationIds.build(), requireFilters, initialRecoveryFilters, includeFilters, excludeFilters,
indexCreatedVersion, indexUpgradedVersion, getRoutingNumShards(), routingPartitionSize, waitForActiveShards,
rolloverInfos.build());
return new IndexMetaData(
new Index(index, uuid),
version,
mappingVersion,
settingsVersion,
aliasesVersion,
primaryTerms,
state,
numberOfShards,
numberOfReplicas,
tmpSettings,
mappings.build(),
tmpAliases.build(),
customMetaData.build(),
filledInSyncAllocationIds.build(),
requireFilters,
initialRecoveryFilters,
includeFilters,
excludeFilters,
indexCreatedVersion,
indexUpgradedVersion,
getRoutingNumShards(),
routingPartitionSize,
waitForActiveShards,
rolloverInfos.build());
}

public static void toXContent(IndexMetaData indexMetaData, XContentBuilder builder, ToXContent.Params params) throws IOException {
Expand All @@ -1195,6 +1270,7 @@ public static void toXContent(IndexMetaData indexMetaData, XContentBuilder build
builder.field(KEY_VERSION, indexMetaData.getVersion());
builder.field(KEY_MAPPING_VERSION, indexMetaData.getMappingVersion());
builder.field(KEY_SETTINGS_VERSION, indexMetaData.getSettingsVersion());
builder.field(KEY_ALIASES_VERSION, indexMetaData.getAliasesVersion());
builder.field(KEY_ROUTING_NUM_SHARDS, indexMetaData.getRoutingNumShards());
builder.field(KEY_STATE, indexMetaData.getState().toString().toLowerCase(Locale.ENGLISH));

Expand Down Expand Up @@ -1269,6 +1345,7 @@ public static IndexMetaData fromXContent(XContentParser parser) throws IOExcepti
}
boolean mappingVersion = false;
boolean settingsVersion = false;
boolean aliasesVersion = false;
while ((token = parser.nextToken()) != XContentParser.Token.END_OBJECT) {
if (token == XContentParser.Token.FIELD_NAME) {
currentFieldName = parser.currentName();
Expand Down Expand Up @@ -1367,6 +1444,9 @@ public static IndexMetaData fromXContent(XContentParser parser) throws IOExcepti
} else if (KEY_SETTINGS_VERSION.equals(currentFieldName)) {
settingsVersion = true;
builder.settingsVersion(parser.longValue());
} else if (KEY_ALIASES_VERSION.equals(currentFieldName)) {
aliasesVersion = true;
builder.aliasesVersion(parser.longValue());
} else if (KEY_ROUTING_NUM_SHARDS.equals(currentFieldName)) {
builder.setRoutingNumShards(parser.intValue());
} else {
Expand All @@ -1382,6 +1462,9 @@ public static IndexMetaData fromXContent(XContentParser parser) throws IOExcepti
if (Assertions.ENABLED && Version.indexCreated(builder.settings).onOrAfter(Version.V_6_5_0)) {
assert settingsVersion : "settings version should be present for indices created on or after 6.5.0";
}
if (Assertions.ENABLED && Version.indexCreated(builder.settings).onOrAfter(Version.V_7_1_0)) {
assert aliasesVersion : "aliases version should be present for indices created on or after 7.1.0";
}
return builder.build();
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,7 @@ ClusterState innerExecute(ClusterState currentState, Iterable<AliasAction> actio
}
MetaData.Builder metadata = MetaData.builder(currentState.metaData());
// Run the remaining alias actions
final Set<String> maybeModifiedIndices = new HashSet<>();
for (AliasAction action : actions) {
if (action.removeIndex()) {
// Handled above
Expand Down Expand Up @@ -151,7 +152,20 @@ ClusterState innerExecute(ClusterState currentState, Iterable<AliasAction> actio
xContentRegistry);
}
};
changed |= action.apply(newAliasValidator, metadata, index);
if (action.apply(newAliasValidator, metadata, index)) {
changed = true;
maybeModifiedIndices.add(index.getIndex().getName());
}
}

for (final String maybeModifiedIndex : maybeModifiedIndices) {
final IndexMetaData currentIndexMetaData = currentState.metaData().index(maybeModifiedIndex);
final IndexMetaData newIndexMetaData = metadata.get(maybeModifiedIndex);
// only increment the aliases version if the aliases actually changed for this index
if (currentIndexMetaData.getAliases().equals(newIndexMetaData.getAliases()) == false) {
assert currentIndexMetaData.getAliasesVersion() == newIndexMetaData.getAliasesVersion();
metadata.put(new IndexMetaData.Builder(newIndexMetaData).aliasesVersion(1 + currentIndexMetaData.getAliasesVersion()));
}
}

if (changed) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -308,13 +308,18 @@ public ClusterState execute(ClusterState currentState) {
} else {
validateExistingIndex(currentIndexMetaData, snapshotIndexMetaData, renamedIndexName, partial);
// Index exists and it's closed - open it in metadata and start recovery
IndexMetaData.Builder indexMdBuilder = IndexMetaData.builder(snapshotIndexMetaData)
.state(IndexMetaData.State.OPEN);
indexMdBuilder.version(Math.max(snapshotIndexMetaData.getVersion(), currentIndexMetaData.getVersion() + 1));
indexMdBuilder.mappingVersion(Math.max(snapshotIndexMetaData.getMappingVersion(),
currentIndexMetaData.getMappingVersion() + 1));
indexMdBuilder.settingsVersion(Math.max(snapshotIndexMetaData.getSettingsVersion(),
currentIndexMetaData.getSettingsVersion() + 1));
IndexMetaData.Builder indexMdBuilder =
IndexMetaData.builder(snapshotIndexMetaData).state(IndexMetaData.State.OPEN);
indexMdBuilder.version(
Math.max(snapshotIndexMetaData.getVersion(), 1 + currentIndexMetaData.getVersion()));
indexMdBuilder.mappingVersion(
Math.max(snapshotIndexMetaData.getMappingVersion(), 1 + currentIndexMetaData.getMappingVersion()));
indexMdBuilder.settingsVersion(
Math.max(
snapshotIndexMetaData.getSettingsVersion(),
1 + currentIndexMetaData.getSettingsVersion()));
indexMdBuilder.aliasesVersion(
Math.max(snapshotIndexMetaData.getAliasesVersion(), 1 + currentIndexMetaData.getAliasesVersion()));

for (int shard = 0; shard < snapshotIndexMetaData.getNumberOfShards(); shard++) {
indexMdBuilder.primaryTerm(shard,
Expand Down
Loading

0 comments on commit 65af47e

Please sign in to comment.