Introduce aliases version (#41397)

This commit introduces aliases versions to index metadata. This will be
useful in CCR when we replicate aliases.
This commit is contained in:
Jason Tedor 2019-04-23 11:55:27 -04:00
parent 7c2e151bf2
commit 65af47eb31
No known key found for this signature in database
GPG Key ID: FA89F05560F16BC5
6 changed files with 510 additions and 127 deletions

View File

@ -341,6 +341,7 @@ public class ClusterState implements ToXContentFragment, Diffable<ClusterState>
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(": ");

View File

@ -260,6 +260,7 @@ public class IndexMetaData implements Diffable<IndexMetaData>, ToXContentFragmen
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";
@ -283,6 +284,8 @@ public class IndexMetaData implements Diffable<IndexMetaData>, ToXContentFragmen
private final long settingsVersion;
private final long aliasesVersion;
private final long[] primaryTerms;
private final State state;
@ -310,15 +313,31 @@ public class IndexMetaData implements Diffable<IndexMetaData>, ToXContentFragmen
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;
@ -326,6 +345,8 @@ public class IndexMetaData implements Diffable<IndexMetaData>, ToXContentFragmen
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;
@ -383,6 +404,10 @@ public class IndexMetaData implements Diffable<IndexMetaData>, ToXContentFragmen
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.
@ -652,6 +677,7 @@ public class IndexMetaData implements Diffable<IndexMetaData>, ToXContentFragmen
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;
@ -666,6 +692,7 @@ public class IndexMetaData implements Diffable<IndexMetaData>, ToXContentFragmen
version = after.version;
mappingVersion = after.mappingVersion;
settingsVersion = after.settingsVersion;
aliasesVersion = after.aliasesVersion;
routingNumShards = after.routingNumShards;
state = after.state;
settings = after.settings;
@ -692,6 +719,11 @@ public class IndexMetaData implements Diffable<IndexMetaData>, ToXContentFragmen
} 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();
@ -723,6 +755,9 @@ public class IndexMetaData implements Diffable<IndexMetaData>, ToXContentFragmen
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);
@ -741,6 +776,7 @@ public class IndexMetaData implements Diffable<IndexMetaData>, ToXContentFragmen
builder.version(version);
builder.mappingVersion(mappingVersion);
builder.settingsVersion(settingsVersion);
builder.aliasesVersion(aliasesVersion);
builder.setRoutingNumShards(routingNumShards);
builder.state(state);
builder.settings(settings);
@ -767,6 +803,11 @@ public class IndexMetaData implements Diffable<IndexMetaData>, ToXContentFragmen
} 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));
@ -819,6 +860,9 @@ public class IndexMetaData implements Diffable<IndexMetaData>, ToXContentFragmen
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);
@ -868,6 +912,7 @@ public class IndexMetaData implements Diffable<IndexMetaData>, ToXContentFragmen
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;
@ -892,6 +937,7 @@ public class IndexMetaData implements Diffable<IndexMetaData>, ToXContentFragmen
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);
@ -1040,20 +1086,29 @@ public class IndexMetaData implements Diffable<IndexMetaData>, ToXContentFragmen
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.
@ -1182,11 +1237,31 @@ public class IndexMetaData implements Diffable<IndexMetaData>, ToXContentFragmen
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 {
@ -1195,6 +1270,7 @@ public class IndexMetaData implements Diffable<IndexMetaData>, ToXContentFragmen
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));
@ -1269,6 +1345,7 @@ public class IndexMetaData implements Diffable<IndexMetaData>, ToXContentFragmen
}
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();
@ -1367,6 +1444,9 @@ public class IndexMetaData implements Diffable<IndexMetaData>, ToXContentFragmen
} 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 {
@ -1382,6 +1462,9 @@ public class IndexMetaData implements Diffable<IndexMetaData>, ToXContentFragmen
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();
}
}

View File

@ -115,6 +115,7 @@ public class MetaDataIndexAliasesService {
}
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
@ -151,7 +152,20 @@ public class MetaDataIndexAliasesService {
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) {

View File

@ -308,13 +308,18 @@ public class RestoreService implements ClusterStateApplier {
} 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,

View File

@ -33,12 +33,14 @@ import org.elasticsearch.cluster.ClusterState;
import org.elasticsearch.cluster.metadata.AliasMetaData;
import org.elasticsearch.cluster.metadata.AliasOrIndex;
import org.elasticsearch.cluster.metadata.IndexMetaData;
import org.elasticsearch.cluster.metadata.MetaData;
import org.elasticsearch.common.StopWatch;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.unit.TimeValue;
import org.elasticsearch.common.xcontent.XContentType;
import org.elasticsearch.index.query.QueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.index.query.TermQueryBuilder;
import org.elasticsearch.rest.action.admin.indices.AliasesNotFoundException;
import org.elasticsearch.search.SearchHit;
import org.elasticsearch.search.SearchHits;
@ -49,8 +51,10 @@ import org.elasticsearch.search.sort.SortOrder;
import org.elasticsearch.test.ESIntegTestCase;
import java.util.Arrays;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
@ -81,14 +85,18 @@ import static org.hamcrest.Matchers.notNullValue;
import static org.hamcrest.Matchers.nullValue;
public class IndexAliasesIT extends ESIntegTestCase {
public void testAliases() throws Exception {
logger.info("--> creating index [test]");
createIndex("test");
ensureGreen();
logger.info("--> aliasing index [test] with [alias1]");
assertAcked(admin().indices().prepareAliases().addAlias("test", "alias1", false));
assertAliasesVersionIncreases(
"test", () -> {
logger.info("--> aliasing index [test] with [alias1]");
assertAcked(admin().indices().prepareAliases().addAlias("test", "alias1", false));
});
logger.info("--> indexing against [alias1], should fail now");
IllegalArgumentException exception = expectThrows(IllegalArgumentException.class,
@ -98,8 +106,13 @@ public class IndexAliasesIT extends ESIntegTestCase {
" The write index may be explicitly disabled using is_write_index=false or the alias points to multiple" +
" indices without one being designated as a write index"));
logger.info("--> aliasing index [test] with [alias1]");
assertAcked(admin().indices().prepareAliases().addAlias("test", "alias1"));
assertAliasesVersionIncreases(
"test",
() -> {
logger.info("--> aliasing index [test] with [alias1]");
assertAcked(admin().indices().prepareAliases().addAlias("test", "alias1"));
}
);
logger.info("--> indexing against [alias1], should work now");
IndexResponse indexResponse = client().index(indexRequest("alias1").type("type1").id("1")
@ -111,8 +124,10 @@ public class IndexAliasesIT extends ESIntegTestCase {
ensureGreen();
logger.info("--> add index [test_x] with [alias1]");
assertAcked(admin().indices().prepareAliases().addAlias("test_x", "alias1"));
assertAliasesVersionIncreases("test_x", () -> {
logger.info("--> add index [test_x] with [alias1]");
assertAcked(admin().indices().prepareAliases().addAlias("test_x", "alias1"));
});
logger.info("--> indexing against [alias1], should fail now");
exception = expectThrows(IllegalArgumentException.class,
@ -129,16 +144,20 @@ public class IndexAliasesIT extends ESIntegTestCase {
" The write index may be explicitly disabled using is_write_index=false or the alias points to multiple" +
" indices without one being designated as a write index"));
logger.info("--> remove aliasing index [test_x] with [alias1]");
assertAcked(admin().indices().prepareAliases().removeAlias("test_x", "alias1"));
assertAliasesVersionIncreases("test_x", () -> {
logger.info("--> remove aliasing index [test_x] with [alias1]");
assertAcked(admin().indices().prepareAliases().removeAlias("test_x", "alias1"));
});
logger.info("--> indexing against [alias1], should work now");
indexResponse = client().index(indexRequest("alias1").type("type1").id("1")
.source(source("1", "test"), XContentType.JSON)).actionGet();
assertThat(indexResponse.getIndex(), equalTo("test"));
logger.info("--> add index [test_x] with [alias1] as write-index");
assertAcked(admin().indices().prepareAliases().addAlias("test_x", "alias1", true));
assertAliasesVersionIncreases("test_x", () -> {
logger.info("--> add index [test_x] with [alias1] as write-index");
assertAcked(admin().indices().prepareAliases().addAlias("test_x", "alias1", true));
});
logger.info("--> indexing against [alias1], should work now");
indexResponse = client().index(indexRequest("alias1").type("type1").id("1")
@ -149,8 +168,10 @@ public class IndexAliasesIT extends ESIntegTestCase {
DeleteResponse deleteResponse = client().delete(deleteRequest("alias1").type("type1").id("1")).actionGet();
assertThat(deleteResponse.getIndex(), equalTo("test_x"));
logger.info("--> remove [alias1], Aliasing index [test_x] with [alias1]");
assertAcked(admin().indices().prepareAliases().removeAlias("test", "alias1").addAlias("test_x", "alias1"));
assertAliasesVersionIncreases("test_x", () -> {
logger.info("--> remove [alias1], Aliasing index [test_x] with [alias1]");
assertAcked(admin().indices().prepareAliases().removeAlias("test", "alias1").addAlias("test_x", "alias1"));
});
logger.info("--> indexing against [alias1], should work against [test_x]");
indexResponse = client().index(indexRequest("alias1").type("type1").id("1")
@ -181,7 +202,7 @@ public class IndexAliasesIT extends ESIntegTestCase {
logger.info("--> aliasing index [test] with [alias1] and filter [user:kimchy]");
QueryBuilder filter = termQuery("user", "kimchy");
assertAcked(admin().indices().prepareAliases().addAlias("test", "alias1", filter));
assertAliasesVersionIncreases("test", () -> assertAcked(admin().indices().prepareAliases().addAlias("test", "alias1", filter)));
// For now just making sure that filter was stored with the alias
logger.info("--> making sure that filter was stored with alias [alias1] and filter [user:kimchy]");
@ -210,11 +231,18 @@ public class IndexAliasesIT extends ESIntegTestCase {
ensureGreen();
logger.info("--> adding filtering aliases to index [test]");
assertAcked(admin().indices().prepareAliases().addAlias("test", "alias1"));
assertAcked(admin().indices().prepareAliases().addAlias("test", "alias2"));
assertAcked(admin().indices().prepareAliases().addAlias("test", "foos", termQuery("name", "foo")));
assertAcked(admin().indices().prepareAliases().addAlias("test", "bars", termQuery("name", "bar")));
assertAcked(admin().indices().prepareAliases().addAlias("test", "tests", termQuery("name", "test")));
assertAliasesVersionIncreases("test", () -> assertAcked(admin().indices().prepareAliases().addAlias("test", "alias1")));
assertAliasesVersionIncreases("test", () -> assertAcked(admin().indices().prepareAliases().addAlias("test", "alias2")));
assertAliasesVersionIncreases(
"test",
() -> assertAcked(admin().indices().prepareAliases().addAlias("test", "foos", termQuery("name", "foo"))));
assertAliasesVersionIncreases(
"test",
() -> assertAcked(admin().indices().prepareAliases().addAlias("test", "bars", termQuery("name", "bar"))));
assertAliasesVersionIncreases(
"test",
() -> assertAcked(admin().indices().prepareAliases().addAlias("test", "tests", termQuery("name", "test"))));
logger.info("--> indexing against [test]");
client().index(indexRequest("test").type("type1").id("1").source(source("1", "foo test"), XContentType.JSON)
@ -295,15 +323,21 @@ public class IndexAliasesIT extends ESIntegTestCase {
ensureGreen();
logger.info("--> adding filtering aliases to index [test1]");
assertAcked(admin().indices().prepareAliases().addAlias("test1", "aliasToTest1"));
assertAcked(admin().indices().prepareAliases().addAlias("test1", "aliasToTests"));
assertAcked(admin().indices().prepareAliases().addAlias("test1", "foos", termQuery("name", "foo")));
assertAcked(admin().indices().prepareAliases().addAlias("test1", "bars", termQuery("name", "bar")));
assertAliasesVersionIncreases("test1", () -> assertAcked(admin().indices().prepareAliases().addAlias("test1", "aliasToTest1")));
assertAliasesVersionIncreases("test1", () -> assertAcked(admin().indices().prepareAliases().addAlias("test1", "aliasToTests")));
assertAliasesVersionIncreases(
"test1",
() -> assertAcked(admin().indices().prepareAliases().addAlias("test1", "foos", termQuery("name", "foo"))));
assertAliasesVersionIncreases(
"test1",
() -> assertAcked(admin().indices().prepareAliases().addAlias("test1", "bars", termQuery("name", "bar"))));
logger.info("--> adding filtering aliases to index [test2]");
assertAcked(admin().indices().prepareAliases().addAlias("test2", "aliasToTest2"));
assertAcked(admin().indices().prepareAliases().addAlias("test2", "aliasToTests"));
assertAcked(admin().indices().prepareAliases().addAlias("test2", "foos", termQuery("name", "foo")));
assertAliasesVersionIncreases("test2", () -> assertAcked(admin().indices().prepareAliases().addAlias("test2", "aliasToTest2")));
assertAliasesVersionIncreases("test2", () -> assertAcked(admin().indices().prepareAliases().addAlias("test2", "aliasToTests")));
assertAliasesVersionIncreases(
"test2",
() -> assertAcked(admin().indices().prepareAliases().addAlias("test2", "foos", termQuery("name", "foo"))));
logger.info("--> indexing against [test1]");
client().index(indexRequest("test1").type("type1").id("1").source(source("1", "foo test"), XContentType.JSON)).get();
@ -367,17 +401,27 @@ public class IndexAliasesIT extends ESIntegTestCase {
ensureGreen();
logger.info("--> adding aliases to indices");
assertAcked(admin().indices().prepareAliases().addAlias("test1", "alias12"));
assertAcked(admin().indices().prepareAliases().addAlias("test2", "alias12"));
assertAliasesVersionIncreases("test1", () -> assertAcked(admin().indices().prepareAliases().addAlias("test1", "alias12")));
assertAliasesVersionIncreases("test2", () -> assertAcked(admin().indices().prepareAliases().addAlias("test2", "alias12")));
logger.info("--> adding filtering aliases to indices");
assertAcked(admin().indices().prepareAliases().addAlias("test1", "filter1", termQuery("name", "test1")));
assertAliasesVersionIncreases(
"test1",
() -> assertAcked(admin().indices().prepareAliases().addAlias("test1", "filter1", termQuery("name", "test1"))));
assertAcked(admin().indices().prepareAliases().addAlias("test2", "filter23", termQuery("name", "foo")));
assertAcked(admin().indices().prepareAliases().addAlias("test3", "filter23", termQuery("name", "foo")));
assertAliasesVersionIncreases(
"test2",
() -> assertAcked(admin().indices().prepareAliases().addAlias("test2", "filter23", termQuery("name", "foo"))));
assertAliasesVersionIncreases(
"test3",
() -> assertAcked(admin().indices().prepareAliases().addAlias("test3", "filter23", termQuery("name", "foo"))));
assertAcked(admin().indices().prepareAliases().addAlias("test1", "filter13", termQuery("name", "baz")));
assertAcked(admin().indices().prepareAliases().addAlias("test3", "filter13", termQuery("name", "baz")));
assertAliasesVersionIncreases(
"test1",
() -> assertAcked(admin().indices().prepareAliases().addAlias("test1", "filter13", termQuery("name", "baz"))));
assertAliasesVersionIncreases(
"test3",
() -> assertAcked(admin().indices().prepareAliases().addAlias("test3", "filter13", termQuery("name", "baz"))));
logger.info("--> indexing against [test1]");
client().index(indexRequest("test1").type("type1").id("11").source(source("11", "foo test1"), XContentType.JSON)).get();
@ -433,17 +477,27 @@ public class IndexAliasesIT extends ESIntegTestCase {
ensureGreen();
logger.info("--> adding filtering aliases to index [test1]");
assertAcked(admin().indices().prepareAliases().addAlias("test1", "aliasToTest1"));
assertAcked(admin().indices().prepareAliases().addAlias("test1", "aliasToTests"));
assertAcked(admin().indices().prepareAliases().addAlias("test1", "foos", termQuery("name", "foo")));
assertAcked(admin().indices().prepareAliases().addAlias("test1", "bars", termQuery("name", "bar")));
assertAcked(admin().indices().prepareAliases().addAlias("test1", "tests", termQuery("name", "test")));
assertAliasesVersionIncreases("test1", () -> assertAcked(admin().indices().prepareAliases().addAlias("test1", "aliasToTest1")));
assertAliasesVersionIncreases("test1", () -> assertAcked(admin().indices().prepareAliases().addAlias("test1", "aliasToTests")));
assertAliasesVersionIncreases(
"test1",
() -> assertAcked(admin().indices().prepareAliases().addAlias("test1", "foos", termQuery("name", "foo"))));
assertAliasesVersionIncreases(
"test1",
() -> assertAcked(admin().indices().prepareAliases().addAlias("test1", "bars", termQuery("name", "bar"))));
assertAliasesVersionIncreases(
"test1",
() -> assertAcked(admin().indices().prepareAliases().addAlias("test1", "tests", termQuery("name", "test"))));
logger.info("--> adding filtering aliases to index [test2]");
assertAcked(admin().indices().prepareAliases().addAlias("test2", "aliasToTest2"));
assertAcked(admin().indices().prepareAliases().addAlias("test2", "aliasToTests"));
assertAcked(admin().indices().prepareAliases().addAlias("test2", "foos", termQuery("name", "foo")));
assertAcked(admin().indices().prepareAliases().addAlias("test2", "tests", termQuery("name", "test")));
assertAliasesVersionIncreases("test2", () -> assertAcked(admin().indices().prepareAliases().addAlias("test2", "aliasToTest2")));
assertAliasesVersionIncreases("test2", () -> assertAcked(admin().indices().prepareAliases().addAlias("test2", "aliasToTests")));
assertAliasesVersionIncreases(
"test2",
() -> assertAcked(admin().indices().prepareAliases().addAlias("test2", "foos", termQuery("name", "foo"))));
assertAliasesVersionIncreases(
"test2",
() -> assertAcked(admin().indices().prepareAliases().addAlias("test2", "tests", termQuery("name", "test"))));
logger.info("--> indexing against [test1]");
client().index(indexRequest("test1").type("type1").id("1").source(source("1", "foo test"), XContentType.JSON)).get();
@ -471,22 +525,30 @@ public class IndexAliasesIT extends ESIntegTestCase {
ensureGreen();
logger.info("--> adding filtering aliases to index [test1]");
assertAcked(admin().indices().prepareAliases().addAlias("test1", "aliasToTest1")
.addAlias("test1", "aliasToTests")
.addAlias("test1", "foos", termQuery("name", "foo"))
.addAlias("test1", "bars", termQuery("name", "bar"))
.addAlias("test1", "tests", termQuery("name", "test")));
assertAliasesVersionIncreases(
"test1",
() -> assertAcked(admin().indices()
.prepareAliases()
.addAlias("test1", "aliasToTest1")
.addAlias("test1", "aliasToTests")
.addAlias("test1", "foos", termQuery("name", "foo"))
.addAlias("test1", "bars", termQuery("name", "bar"))
.addAlias("test1", "tests", termQuery("name", "test"))));
logger.info("--> adding filtering aliases to index [test2]");
assertAcked(admin().indices().prepareAliases().addAlias("test2", "aliasToTest2")
.addAlias("test2", "aliasToTests")
.addAlias("test2", "foos", termQuery("name", "foo"))
.addAlias("test2", "tests", termQuery("name", "test")));
assertAliasesVersionIncreases(
"test2",
() -> assertAcked(admin().indices()
.prepareAliases()
.addAlias("test2", "aliasToTest2")
.addAlias("test2", "aliasToTests")
.addAlias("test2", "foos", termQuery("name", "foo"))
.addAlias("test2", "tests", termQuery("name", "test"))));
String[] indices = {"test1", "test2"};
String[] aliases = {"aliasToTest1", "foos", "bars", "tests", "aliasToTest2", "aliasToTests"};
admin().indices().prepareAliases().removeAlias(indices, aliases).get();
assertAliasesVersionIncreases(indices, () -> admin().indices().prepareAliases().removeAlias(indices, aliases).get());
AliasesExistResponse response = admin().indices().prepareAliasesExist(aliases).get();
assertThat(response.exists(), equalTo(false));
@ -497,10 +559,12 @@ public class IndexAliasesIT extends ESIntegTestCase {
ensureGreen();
logger.info("--> adding [foo] alias to [foo_foo] and [bar_bar]");
assertAcked(admin().indices().prepareAliases().addAlias("foo_foo", "foo"));
assertAcked(admin().indices().prepareAliases().addAlias("bar_bar", "foo"));
assertAliasesVersionIncreases("foo_foo", () -> assertAcked(admin().indices().prepareAliases().addAlias("foo_foo", "foo")));
assertAliasesVersionIncreases("bar_bar", () -> assertAcked(admin().indices().prepareAliases().addAlias("bar_bar", "foo")));
assertAcked(admin().indices().prepareAliases().addAliasAction(AliasActions.remove().index("foo*").alias("foo")).execute().get());
assertAliasesVersionIncreases(
"foo_foo",
() -> assertAcked(admin().indices().prepareAliases().addAliasAction(AliasActions.remove().index("foo*").alias("foo"))));
assertTrue(admin().indices().prepareAliasesExist("foo").get().exists());
assertFalse(admin().indices().prepareAliasesExist("foo").setIndices("foo_foo").get().exists());
@ -518,8 +582,9 @@ public class IndexAliasesIT extends ESIntegTestCase {
ensureGreen();
for (int i = 0; i < 10; i++) {
assertAcked(admin().indices().prepareAliases().addAlias("test", "alias" + i));
client().index(indexRequest("alias" + i).type("type1").id("1").source(source("1", "test"), XContentType.JSON)).get();
final String aliasName = "alias" + i;
assertAliasesVersionIncreases("test", () -> assertAcked(admin().indices().prepareAliases().addAlias("test", aliasName)));
client().index(indexRequest(aliasName).type("type1").id("1").source(source("1", "test"), XContentType.JSON)).get();
}
}
@ -533,8 +598,9 @@ public class IndexAliasesIT extends ESIntegTestCase {
ensureGreen();
for (int i = 0; i < 10; i++) {
assertAcked(admin().indices().prepareAliases().addAlias("test", "alias" + i));
client().index(indexRequest("alias" + i).type("type1").id("1").source(source("1", "test"),
final String aliasName = "alias" + i;
assertAliasesVersionIncreases("test", () -> assertAcked(admin().indices().prepareAliases().addAlias("test", aliasName)));
client().index(indexRequest(aliasName).type("type1").id("1").source(source("1", "test"),
XContentType.JSON)).get();
}
}
@ -553,7 +619,9 @@ public class IndexAliasesIT extends ESIntegTestCase {
executor.submit(new Runnable() {
@Override
public void run() {
assertAcked(admin().indices().prepareAliases().addAlias("test", aliasName));
assertAliasesVersionIncreases(
"test",
() -> assertAcked(admin().indices().prepareAliases().addAlias("test", aliasName)));
client().index(indexRequest(aliasName).type("type1").id("1").source(source("1", "test"), XContentType.JSON))
.actionGet();
}
@ -573,27 +641,37 @@ public class IndexAliasesIT extends ESIntegTestCase {
ensureGreen();
logger.info("--> creating alias1 ");
assertAcked((admin().indices().prepareAliases().addAlias("test", "alias1")));
assertAliasesVersionIncreases("test", () -> assertAcked((admin().indices().prepareAliases().addAlias("test", "alias1"))));
TimeValue timeout = TimeValue.timeValueSeconds(2);
logger.info("--> recreating alias1 ");
StopWatch stopWatch = new StopWatch();
stopWatch.start();
assertAcked((admin().indices().prepareAliases().addAlias("test", "alias1").setTimeout(timeout)));
assertAliasesVersionUnchanged(
"test",
() -> assertAcked((admin().indices().prepareAliases().addAlias("test", "alias1").setTimeout(timeout))));
assertThat(stopWatch.stop().lastTaskTime().millis(), lessThan(timeout.millis()));
logger.info("--> modifying alias1 to have a filter");
stopWatch.start();
assertAcked((admin().indices().prepareAliases().addAlias("test", "alias1", termQuery("name", "foo")).setTimeout(timeout)));
final TermQueryBuilder fooFilter = termQuery("name", "foo");
assertAliasesVersionIncreases(
"test",
() -> assertAcked(admin().indices().prepareAliases().addAlias("test", "alias1", fooFilter).setTimeout(timeout)));
assertThat(stopWatch.stop().lastTaskTime().millis(), lessThan(timeout.millis()));
logger.info("--> recreating alias1 with the same filter");
stopWatch.start();
assertAcked((admin().indices().prepareAliases().addAlias("test", "alias1", termQuery("name", "foo")).setTimeout(timeout)));
assertAliasesVersionUnchanged(
"test",
() -> assertAcked((admin().indices().prepareAliases().addAlias("test", "alias1", fooFilter).setTimeout(timeout))));
assertThat(stopWatch.stop().lastTaskTime().millis(), lessThan(timeout.millis()));
logger.info("--> recreating alias1 with a different filter");
stopWatch.start();
assertAcked((admin().indices().prepareAliases().addAlias("test", "alias1", termQuery("name", "bar")).setTimeout(timeout)));
final TermQueryBuilder barFilter = termQuery("name", "bar");
assertAliasesVersionIncreases(
"test",
() -> assertAcked((admin().indices().prepareAliases().addAlias("test", "alias1", barFilter).setTimeout(timeout))));
assertThat(stopWatch.stop().lastTaskTime().millis(), lessThan(timeout.millis()));
logger.info("--> verify that filter was updated");
@ -603,10 +681,10 @@ public class IndexAliasesIT extends ESIntegTestCase {
logger.info("--> deleting alias1");
stopWatch.start();
assertAcked((admin().indices().prepareAliases().removeAlias("test", "alias1").setTimeout(timeout)));
assertAliasesVersionIncreases(
"test",
() -> assertAcked((admin().indices().prepareAliases().removeAlias("test", "alias1").setTimeout(timeout))));
assertThat(stopWatch.stop().lastTaskTime().millis(), lessThan(timeout.millis()));
}
public void testIndicesRemoveNonExistingAliasResponds404() throws Exception {
@ -635,7 +713,9 @@ public class IndexAliasesIT extends ESIntegTestCase {
ensureGreen();
logger.info("--> creating aliases [alias1, alias2]");
assertAcked(admin().indices().prepareAliases().addAlias("foobar", "alias1").addAlias("foobar", "alias2"));
assertAliasesVersionIncreases(
"foobar",
() -> assertAcked(admin().indices().prepareAliases().addAlias("foobar", "alias1").addAlias("foobar", "alias2")));
logger.info("--> getting alias1");
GetAliasesResponse getResponse = admin().indices().prepareGetAliases("alias1").get();
@ -670,13 +750,19 @@ public class IndexAliasesIT extends ESIntegTestCase {
logger.info("--> creating aliases [bar, baz, foo]");
assertAcked(admin().indices().prepareAliases()
.addAlias("bazbar", "bar")
.addAlias("bazbar", "bac", termQuery("field", "value"))
.addAlias("foobar", "foo"));
assertAliasesVersionIncreases(
new String[]{"bazbar", "foobar"},
() -> assertAcked(admin().indices()
.prepareAliases()
.addAlias("bazbar", "bar")
.addAlias("bazbar", "bac", termQuery("field", "value"))
.addAlias("foobar", "foo")));
assertAcked(admin().indices().prepareAliases()
.addAliasAction(AliasActions.add().index("foobar").alias("bac").routing("bla")));
assertAliasesVersionIncreases(
"foobar",
() -> assertAcked(admin().indices()
.prepareAliases()
.addAliasAction(AliasActions.add().index("foobar").alias("bac").routing("bla"))));
logger.info("--> getting bar and baz for index bazbar");
getResponse = admin().indices().prepareGetAliases("bar", "bac").addIndices("bazbar").get();
@ -826,7 +912,9 @@ public class IndexAliasesIT extends ESIntegTestCase {
createIndex("index1");
createIndex("index2");
assertAcked(admin().indices().prepareAliases().addAlias("index1", "alias1").addAlias("index2", "alias2"));
assertAliasesVersionIncreases(
new String[]{"index1", "index2"},
() -> assertAcked(admin().indices().prepareAliases().addAlias("index1", "alias1").addAlias("index2", "alias2")));
GetAliasesResponse response = admin().indices().prepareGetAliases().get();
assertThat(response.getAliases(), hasKey("index1"));
@ -911,23 +999,41 @@ public class IndexAliasesIT extends ESIntegTestCase {
// fields mentioned in filters don't need to exist in the mapping.
public void testAddAliasWithFilterNoMapping() throws Exception {
assertAcked(prepareCreate("test"));
client().admin().indices().prepareAliases()
.addAlias("test", "a", QueryBuilders.termQuery("field1", "term"))
.get();
client().admin().indices().prepareAliases()
.addAlias("test", "a", QueryBuilders.rangeQuery("field2").from(0).to(1))
.get();
client().admin().indices().prepareAliases()
.addAlias("test", "a", QueryBuilders.matchAllQuery())
.get();
assertAliasesVersionIncreases(
"test",
() -> client().admin()
.indices()
.prepareAliases()
.addAlias("test", "a", QueryBuilders.termQuery("field1", "term"))
.get());
assertAliasesVersionIncreases(
"test",
() -> client().admin()
.indices()
.prepareAliases()
.addAlias("test", "a", QueryBuilders.rangeQuery("field2").from(0).to(1))
.get());
assertAliasesVersionIncreases(
"test",
() -> client().admin()
.indices()
.prepareAliases()
.addAlias("test", "a", QueryBuilders.matchAllQuery())
.get());
}
public void testAliasFilterWithNowInRangeFilterAndQuery() throws Exception {
assertAcked(prepareCreate("my-index").addMapping("my-type", "timestamp", "type=date"));
assertAcked(admin().indices().prepareAliases()
.addAlias("my-index", "filter1", rangeQuery("timestamp").from("2016-12-01").to("2016-12-31")));
assertAcked(admin().indices().prepareAliases()
.addAlias("my-index", "filter2", rangeQuery("timestamp").from("2016-01-01").to("2016-12-31")));
assertAliasesVersionIncreases(
"my-index",
() -> assertAcked(admin().indices()
.prepareAliases()
.addAlias("my-index", "filter1", rangeQuery("timestamp").from("2016-12-01").to("2016-12-31"))));
assertAliasesVersionIncreases(
"my-index",
() -> assertAcked(admin().indices()
.prepareAliases()
.addAlias("my-index", "filter2", rangeQuery("timestamp").from("2016-01-01").to("2016-12-31"))));
final int numDocs = scaledRandomIntBetween(5, 52);
for (int i = 1; i <= numDocs; i++) {
@ -951,8 +1057,10 @@ public class IndexAliasesIT extends ESIntegTestCase {
try {
enableIndexBlock("test", block);
assertAcked(admin().indices().prepareAliases().addAlias("test", "alias1").addAlias("test", "alias2"));
assertAcked(admin().indices().prepareAliases().removeAlias("test", "alias1"));
assertAliasesVersionIncreases(
"test",
() -> assertAcked(admin().indices().prepareAliases().addAlias("test", "alias1").addAlias("test", "alias2")));
assertAliasesVersionIncreases("test", () -> assertAcked(admin().indices().prepareAliases().removeAlias("test", "alias1")));
assertThat(admin().indices().prepareGetAliases("alias2").execute().actionGet().getAliases().get("test").size(), equalTo(1));
assertThat(admin().indices().prepareAliasesExist("alias2").get().exists(), equalTo(true));
} finally {
@ -963,8 +1071,12 @@ public class IndexAliasesIT extends ESIntegTestCase {
try {
enableIndexBlock("test", SETTING_READ_ONLY);
assertBlocked(admin().indices().prepareAliases().addAlias("test", "alias3"), INDEX_READ_ONLY_BLOCK);
assertBlocked(admin().indices().prepareAliases().removeAlias("test", "alias2"), INDEX_READ_ONLY_BLOCK);
assertAliasesVersionUnchanged(
"test",
() -> assertBlocked(admin().indices().prepareAliases().addAlias("test", "alias3"), INDEX_READ_ONLY_BLOCK));
assertAliasesVersionUnchanged(
"test",
() -> assertBlocked(admin().indices().prepareAliases().removeAlias("test", "alias2"), INDEX_READ_ONLY_BLOCK));
assertThat(admin().indices().prepareGetAliases("alias2").execute().actionGet().getAliases().get("test").size(), equalTo(1));
assertThat(admin().indices().prepareAliasesExist("alias2").get().exists(), equalTo(true));
@ -975,8 +1087,12 @@ public class IndexAliasesIT extends ESIntegTestCase {
try {
enableIndexBlock("test", SETTING_BLOCKS_METADATA);
assertBlocked(admin().indices().prepareAliases().addAlias("test", "alias3"), INDEX_METADATA_BLOCK);
assertBlocked(admin().indices().prepareAliases().removeAlias("test", "alias2"), INDEX_METADATA_BLOCK);
assertAliasesVersionUnchanged(
"test",
() -> assertBlocked(admin().indices().prepareAliases().addAlias("test", "alias3"), INDEX_METADATA_BLOCK));
assertAliasesVersionUnchanged(
"test",
() -> assertBlocked(admin().indices().prepareAliases().removeAlias("test", "alias2"), INDEX_METADATA_BLOCK));
assertBlocked(admin().indices().prepareGetAliases("alias2"), INDEX_METADATA_BLOCK);
assertBlocked(admin().indices().prepareAliasesExist("alias2"), INDEX_METADATA_BLOCK);
@ -988,15 +1104,19 @@ public class IndexAliasesIT extends ESIntegTestCase {
public void testAliasActionRemoveIndex() throws InterruptedException, ExecutionException {
assertAcked(prepareCreate("foo_foo"));
assertAcked(prepareCreate("bar_bar"));
assertAcked(admin().indices().prepareAliases().addAlias("foo_foo", "foo"));
assertAcked(admin().indices().prepareAliases().addAlias("bar_bar", "foo"));
assertAliasesVersionIncreases(
new String[]{"foo_foo", "bar_bar"},
() -> {
assertAcked(admin().indices().prepareAliases().addAlias("foo_foo", "foo"));
assertAcked(admin().indices().prepareAliases().addAlias("bar_bar", "foo"));
});
IllegalArgumentException iae = expectThrows(IllegalArgumentException.class,
() -> client().admin().indices().prepareAliases().removeIndex("foo").execute().actionGet());
assertEquals("The provided expression [foo] matches an alias, specify the corresponding concrete indices instead.",
iae.getMessage());
assertAcked(client().admin().indices().prepareAliases().removeIndex("foo*").execute().get());
assertAcked(client().admin().indices().prepareAliases().removeIndex("foo*"));
assertFalse(client().admin().indices().prepareExists("foo_foo").execute().actionGet().isExists());
assertTrue(admin().indices().prepareAliasesExist("foo").get().exists());
assertTrue(client().admin().indices().prepareExists("bar_bar").execute().actionGet().isExists());
@ -1010,7 +1130,9 @@ public class IndexAliasesIT extends ESIntegTestCase {
public void testRemoveIndexAndReplaceWithAlias() throws InterruptedException, ExecutionException {
assertAcked(client().admin().indices().prepareCreate("test"));
indexRandom(true, client().prepareIndex("test_2", "test", "test").setSource("test", "test"));
assertAcked(client().admin().indices().prepareAliases().addAlias("test_2", "test").removeIndex("test"));
assertAliasesVersionIncreases(
"test_2",
() -> assertAcked(client().admin().indices().prepareAliases().addAlias("test_2", "test").removeIndex("test")));
assertHitCount(client().prepareSearch("test").get(), 1);
}
@ -1052,4 +1174,31 @@ public class IndexAliasesIT extends ESIntegTestCase {
private String source(String id, String nameValue) {
return "{ \"id\" : \"" + id + "\", \"name\" : \"" + nameValue + "\" }";
}
private void assertAliasesVersionIncreases(final String index, final Runnable runnable) {
assertAliasesVersionIncreases(new String[]{index}, runnable);
}
private void assertAliasesVersionIncreases(final String[] indices, final Runnable runnable) {
final Map<String, Long> beforeAliasesVersions = new HashMap<>(indices.length);
final MetaData beforeMetaData = admin().cluster().prepareState().get().getState().metaData();
for (final String index : indices) {
beforeAliasesVersions.put(index, beforeMetaData.index(index).getAliasesVersion());
}
runnable.run();
final MetaData afterMetaData = admin().cluster().prepareState().get().getState().metaData();
for (final String index : indices) {
assertThat(afterMetaData.index(index).getAliasesVersion(), equalTo(1 + beforeAliasesVersions.get(index)));
}
}
private void assertAliasesVersionUnchanged(final String index, final Runnable runnable) {
final long beforeAliasesVersion =
admin().cluster().prepareState().get().getState().metaData().index(index).getAliasesVersion();
runnable.run();
final long afterAliasesVersion =
admin().cluster().prepareState().get().getState().metaData().index(index).getAliasesVersion();
assertThat(afterAliasesVersion, equalTo(beforeAliasesVersion));
}
}

View File

@ -23,15 +23,19 @@ import org.elasticsearch.Version;
import org.elasticsearch.cluster.ClusterName;
import org.elasticsearch.cluster.ClusterState;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.util.set.Sets;
import org.elasticsearch.index.Index;
import org.elasticsearch.index.IndexNotFoundException;
import org.elasticsearch.test.ESTestCase;
import org.elasticsearch.test.VersionUtils;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import static java.util.Collections.singletonList;
import static org.hamcrest.Matchers.contains;
@ -74,6 +78,7 @@ public class MetaDataIndexAliasesServiceTests extends ESTestCase {
assertNotNull(alias);
assertTrue(alias.isAlias());
assertThat(alias.getIndices(), contains(after.metaData().index(index)));
assertAliasesVersionIncreased(index, before, after);
// Remove the alias from it while adding another one
before = after;
@ -85,12 +90,102 @@ public class MetaDataIndexAliasesServiceTests extends ESTestCase {
assertNotNull(alias);
assertTrue(alias.isAlias());
assertThat(alias.getIndices(), contains(after.metaData().index(index)));
assertAliasesVersionIncreased(index, before, after);
// Now just remove on its own
before = after;
after = service.innerExecute(before, singletonList(new AliasAction.Remove(index, "test_2")));
assertNull(after.metaData().getAliasAndIndexLookup().get("test"));
assertNull(after.metaData().getAliasAndIndexLookup().get("test_2"));
assertAliasesVersionIncreased(index, before, after);
}
public void testMultipleIndices() {
final int length = randomIntBetween(2, 8);
final Set<String> indices = new HashSet<>(length);
ClusterState before = ClusterState.builder(ClusterName.DEFAULT).build();
final List<AliasAction> addActions = new ArrayList<>(length);
for (int i = 0; i < length; i++) {
final String index = randomValueOtherThanMany(v -> indices.add(v) == false, () -> randomAlphaOfLength(8));
before = createIndex(before, index);
addActions.add(new AliasAction.Add(index, "alias-" + index, null, null, null, null));
}
final ClusterState afterAddingAliasesToAll = service.innerExecute(before, addActions);
assertAliasesVersionIncreased(indices.toArray(new String[0]), before, afterAddingAliasesToAll);
// now add some aliases randomly
final Set<String> randomIndices = new HashSet<>(length);
final List<AliasAction> randomAddActions = new ArrayList<>(length);
for (String index : indices) {
if (randomBoolean()) {
randomAddActions.add(new AliasAction.Add(index, "random-alias-" + index, null, null, null, null));
randomIndices.add(index);
}
}
final ClusterState afterAddingRandomAliases = service.innerExecute(afterAddingAliasesToAll, randomAddActions);
assertAliasesVersionIncreased(randomIndices.toArray(new String[0]), afterAddingAliasesToAll, afterAddingRandomAliases);
assertAliasesVersionUnchanged(
Sets.difference(indices, randomIndices).toArray(new String[0]),
afterAddingAliasesToAll,
afterAddingRandomAliases);
}
public void testChangingWriteAliasStateIncreasesAliasesVersion() {
final String index = randomAlphaOfLength(8);
final ClusterState before = createIndex(ClusterState.builder(ClusterName.DEFAULT).build(), index);
final ClusterState afterAddWriteAlias =
service.innerExecute(before, singletonList(new AliasAction.Add(index, "test", null, null, null, true)));
assertAliasesVersionIncreased(index, before, afterAddWriteAlias);
final ClusterState afterChangeWriteAliasToNonWriteAlias =
service.innerExecute(afterAddWriteAlias, singletonList(new AliasAction.Add(index, "test", null, null, null, false)));
assertAliasesVersionIncreased(index, afterAddWriteAlias, afterChangeWriteAliasToNonWriteAlias);
final ClusterState afterChangeNonWriteAliasToWriteAlias =
service.innerExecute(
afterChangeWriteAliasToNonWriteAlias,
singletonList(new AliasAction.Add(index, "test", null, null, null, true)));
assertAliasesVersionIncreased(index, afterChangeWriteAliasToNonWriteAlias, afterChangeNonWriteAliasToWriteAlias);
}
public void testAddingAliasMoreThanOnceShouldOnlyIncreaseAliasesVersionByOne() {
final String index = randomAlphaOfLength(8);
final ClusterState before = createIndex(ClusterState.builder(ClusterName.DEFAULT).build(), index);
// add an alias to the index multiple times
final int length = randomIntBetween(2, 8);
final List<AliasAction> addActions = new ArrayList<>(length);
for (int i = 0; i < length; i++) {
addActions.add(new AliasAction.Add(index, "test", null, null, null, null));
}
final ClusterState afterAddingAliases = service.innerExecute(before, addActions);
assertAliasesVersionIncreased(index, before, afterAddingAliases);
}
public void testAliasesVersionUnchangedWhenActionsAreIdempotent() {
final String index = randomAlphaOfLength(8);
final ClusterState before = createIndex(ClusterState.builder(ClusterName.DEFAULT).build(), index);
// add some aliases to the index
final int length = randomIntBetween(1, 8);
final Set<String> aliasNames = new HashSet<>();
final List<AliasAction> addActions = new ArrayList<>(length);
for (int i = 0; i < length; i++) {
final String aliasName = randomValueOtherThanMany(v -> aliasNames.add(v) == false, () -> randomAlphaOfLength(8));
addActions.add(new AliasAction.Add(index, aliasName, null, null, null, null));
}
final ClusterState afterAddingAlias = service.innerExecute(before, addActions);
// now perform a remove and add for each alias which is idempotent, the resulting aliases are unchanged
final List<AliasAction> removeAndAddActions = new ArrayList<>(2 * length);
for (final String aliasName : aliasNames) {
removeAndAddActions.add(new AliasAction.Remove(index, aliasName));
removeAndAddActions.add(new AliasAction.Add(index, aliasName, null, null, null, null));
}
final ClusterState afterRemoveAndAddAlias = service.innerExecute(afterAddingAlias, removeAndAddActions);
assertAliasesVersionUnchanged(index, afterAddingAlias, afterRemoveAndAddAlias);
}
public void testSwapIndexWithAlias() {
@ -106,6 +201,7 @@ public class MetaDataIndexAliasesServiceTests extends ESTestCase {
assertNotNull(alias);
assertTrue(alias.isAlias());
assertThat(alias.getIndices(), contains(after.metaData().index("test_2")));
assertAliasesVersionIncreased("test_2", before, after);
}
public void testAddAliasToRemovedIndex() {
@ -137,18 +233,21 @@ public class MetaDataIndexAliasesServiceTests extends ESTestCase {
new AliasAction.Add("test", "alias", null, null, null, false)));
assertFalse(after.metaData().index("test").getAliases().get("alias").writeIndex());
assertNull(((AliasOrIndex.Alias) after.metaData().getAliasAndIndexLookup().get("alias")).getWriteIndex());
assertAliasesVersionIncreased("test", before, after);
after = service.innerExecute(before, Arrays.asList(
new AliasAction.Add("test", "alias", null, null, null, null)));
assertNull(after.metaData().index("test").getAliases().get("alias").writeIndex());
assertThat(((AliasOrIndex.Alias) after.metaData().getAliasAndIndexLookup().get("alias")).getWriteIndex(),
equalTo(after.metaData().index("test")));
assertAliasesVersionIncreased("test", before, after);
after = service.innerExecute(before, Arrays.asList(
new AliasAction.Add("test", "alias", null, null, null, true)));
assertTrue(after.metaData().index("test").getAliases().get("alias").writeIndex());
assertThat(((AliasOrIndex.Alias) after.metaData().getAliasAndIndexLookup().get("alias")).getWriteIndex(),
equalTo(after.metaData().index("test")));
assertAliasesVersionIncreased("test", before, after);
}
public void testAddWriteOnlyWithExistingWriteIndex() {
@ -165,6 +264,8 @@ public class MetaDataIndexAliasesServiceTests extends ESTestCase {
assertNull(after.metaData().index("test").getAliases().get("alias").writeIndex());
assertThat(((AliasOrIndex.Alias) after.metaData().getAliasAndIndexLookup().get("alias")).getWriteIndex(),
equalTo(after.metaData().index("test2")));
assertAliasesVersionIncreased("test", before, after);
assertAliasesVersionUnchanged("test2", before, after);
Exception exception = expectThrows(IllegalStateException.class, () -> service.innerExecute(before, Arrays.asList(
new AliasAction.Add("test", "alias", null, null, null, true))));
@ -191,6 +292,8 @@ public class MetaDataIndexAliasesServiceTests extends ESTestCase {
assertTrue(after.metaData().index("test2").getAliases().get("alias").writeIndex());
assertThat(((AliasOrIndex.Alias) after.metaData().getAliasAndIndexLookup().get("alias")).getWriteIndex(),
equalTo(after.metaData().index("test2")));
assertAliasesVersionIncreased("test", before, after);
assertAliasesVersionIncreased("test2", before, after);
}
public void testAddWriteOnlyWithExistingNonWriteIndices() {
@ -212,7 +315,9 @@ public class MetaDataIndexAliasesServiceTests extends ESTestCase {
assertTrue(after.metaData().index("test3").getAliases().get("alias").writeIndex());
assertThat(((AliasOrIndex.Alias) after.metaData().getAliasAndIndexLookup().get("alias")).getWriteIndex(),
equalTo(after.metaData().index("test3")));
assertAliasesVersionUnchanged("test", before, after);
assertAliasesVersionUnchanged("test2", before, after);
assertAliasesVersionIncreased("test3", before, after);
}
public void testAddWriteOnlyWithIndexRemoved() {
@ -233,6 +338,7 @@ public class MetaDataIndexAliasesServiceTests extends ESTestCase {
assertNull(after.metaData().index("test2").getAliases().get("alias").writeIndex());
assertThat(((AliasOrIndex.Alias) after.metaData().getAliasAndIndexLookup().get("alias")).getWriteIndex(),
equalTo(after.metaData().index("test2")));
assertAliasesVersionUnchanged("test2", before, after);
}
public void testAddWriteOnlyValidatesAgainstMetaDataBuilder() {
@ -260,4 +366,29 @@ public class MetaDataIndexAliasesServiceTests extends ESTestCase {
.metaData(MetaData.builder(state.metaData()).put(indexMetaData, false))
.build();
}
private void assertAliasesVersionUnchanged(final String index, final ClusterState before, final ClusterState after) {
assertAliasesVersionUnchanged(new String[]{index}, before, after);
}
private void assertAliasesVersionUnchanged(final String[] indices, final ClusterState before, final ClusterState after) {
for (final String index : indices) {
final long expected = before.metaData().index(index).getAliasesVersion();
final long actual = after.metaData().index(index).getAliasesVersion();
assertThat("index metadata aliases version mismatch", actual, equalTo(expected));
}
}
private void assertAliasesVersionIncreased(final String index, final ClusterState before, final ClusterState after) {
assertAliasesVersionIncreased(new String[]{index}, before, after);
}
private void assertAliasesVersionIncreased(final String[] indices, final ClusterState before, final ClusterState after) {
for (final String index : indices) {
final long expected = 1 + before.metaData().index(index).getAliasesVersion();
final long actual = after.metaData().index(index).getAliasesVersion();
assertThat("index metadata aliases version mismatch", actual, equalTo(expected));
}
}
}