Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Dangling indices strip aliases #47581

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 @@ -143,7 +143,7 @@ Map<Index, IndexMetaData> findNewDanglingIndices(final MetaData metaData) {
} else {
logger.info("[{}] dangling index exists on local file system, but not in cluster metadata, " +
"auto import to cluster state", indexMetaData.getIndex());
newIndices.put(indexMetaData.getIndex(), indexMetaData);
newIndices.put(indexMetaData.getIndex(), stripAliases(indexMetaData));
}
}
return newIndices;
Expand All @@ -153,6 +153,20 @@ Map<Index, IndexMetaData> findNewDanglingIndices(final MetaData metaData) {
}
}

/**
* Dangling importing indices with aliases is dangerous, it could for instance result in inability to write to an existing alias if it
* previously had only one index with any is_write_index indication.
*/
private IndexMetaData stripAliases(IndexMetaData indexMetaData) {
if (indexMetaData.getAliases().isEmpty()) {
return indexMetaData;
} else {
logger.info("[{}] stripping aliases: {} from index before importing",
indexMetaData.getIndex(), indexMetaData.getAliases().keys());
return IndexMetaData.builder(indexMetaData).removeAllAliases().build();
}
}

/**
* Allocates the provided list of the dangled indices by sending them to the master node
* for allocation.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
package org.elasticsearch.gateway;

import org.elasticsearch.Version;
import org.elasticsearch.cluster.metadata.AliasMetaData;
import org.elasticsearch.cluster.metadata.IndexGraveyard;
import org.elasticsearch.cluster.metadata.IndexMetaData;
import org.elasticsearch.cluster.metadata.MetaData;
Expand Down Expand Up @@ -158,6 +159,28 @@ public void testDanglingIndicesNotImportedWhenTombstonePresent() throws Exceptio
}
}

public void testDanglingIndicesStripAliases() throws Exception {
try (NodeEnvironment env = newNodeEnvironment()) {
MetaStateService metaStateService = new MetaStateService(env, xContentRegistry());
DanglingIndicesState danglingState = createDanglingIndicesState(env, metaStateService);

final Settings.Builder settings = Settings.builder().put(indexSettings).put(IndexMetaData.SETTING_INDEX_UUID, "test1UUID");
IndexMetaData dangledIndex = IndexMetaData.builder("test1")
.settings(settings)
.putAlias(AliasMetaData.newAliasMetaDataBuilder("test_aliasd").build())
.build();
metaStateService.writeIndex("test_write", dangledIndex);
assertThat(dangledIndex.getAliases().size(), equalTo(1));

final MetaData metaData = MetaData.builder().build();
Map<Index, IndexMetaData> newDanglingIndices = danglingState.findNewDanglingIndices(metaData);
assertThat(newDanglingIndices.size(), equalTo(1));
Map.Entry<Index, IndexMetaData> entry = newDanglingIndices.entrySet().iterator().next();
assertThat(entry.getKey().getName(), equalTo("test1"));
assertThat(entry.getValue().getAliases().size(), equalTo(0));
}
}

private DanglingIndicesState createDanglingIndicesState(NodeEnvironment env, MetaStateService metaStateService) {
return new DanglingIndicesState(env, metaStateService, null, mock(ClusterService.class));
}
Expand Down