use of org.opensearch.cluster.metadata.AliasMetadata in project OpenSearch by opensearch-project.
the class GetIndexIT method assertAliases.
private void assertAliases(GetIndexResponse response, String indexName) {
ImmutableOpenMap<String, List<AliasMetadata>> aliases = response.aliases();
assertThat(aliases, notNullValue());
assertThat(aliases.size(), equalTo(1));
List<AliasMetadata> indexAliases = aliases.get(indexName);
assertThat(indexAliases, notNullValue());
assertThat(indexAliases.size(), equalTo(1));
AliasMetadata alias = indexAliases.get(0);
assertThat(alias, notNullValue());
assertThat(alias.alias(), equalTo("alias_idx"));
}
use of org.opensearch.cluster.metadata.AliasMetadata in project OpenSearch by opensearch-project.
the class TransportIndicesAliasesAction method concreteAliases.
private static String[] concreteAliases(IndicesAliasesRequest.AliasActions action, Metadata metadata, String concreteIndex) {
if (action.expandAliasesWildcards()) {
// for DELETE we expand the aliases
String[] indexAsArray = { concreteIndex };
ImmutableOpenMap<String, List<AliasMetadata>> aliasMetadata = metadata.findAliases(action, indexAsArray);
List<String> finalAliases = new ArrayList<>();
for (ObjectCursor<List<AliasMetadata>> curAliases : aliasMetadata.values()) {
for (AliasMetadata aliasMeta : curAliases.value) {
finalAliases.add(aliasMeta.alias());
}
}
return finalAliases.toArray(new String[finalAliases.size()]);
} else {
// for ADD and REMOVE_INDEX we just return the current aliases
return action.aliases();
}
}
use of org.opensearch.cluster.metadata.AliasMetadata in project OpenSearch by opensearch-project.
the class MetadataRolloverService method rolloverAlias.
private RolloverResult rolloverAlias(ClusterState currentState, IndexAbstraction.Alias alias, String aliasName, String newIndexName, CreateIndexRequest createIndexRequest, List<Condition<?>> metConditions, boolean silent, boolean onlyValidate) throws Exception {
final Metadata metadata = currentState.metadata();
final IndexMetadata writeIndex = alias.getWriteIndex();
final AliasMetadata aliasMetadata = writeIndex.getAliases().get(alias.getName());
final String sourceProvidedName = writeIndex.getSettings().get(IndexMetadata.SETTING_INDEX_PROVIDED_NAME, writeIndex.getIndex().getName());
final String sourceIndexName = writeIndex.getIndex().getName();
final String unresolvedName = (newIndexName != null) ? newIndexName : generateRolloverIndexName(sourceProvidedName, indexNameExpressionResolver);
final String rolloverIndexName = indexNameExpressionResolver.resolveDateMathExpression(unresolvedName);
final boolean explicitWriteIndex = Boolean.TRUE.equals(aliasMetadata.writeIndex());
final Boolean isHidden = IndexMetadata.INDEX_HIDDEN_SETTING.exists(createIndexRequest.settings()) ? IndexMetadata.INDEX_HIDDEN_SETTING.get(createIndexRequest.settings()) : null;
// fails if the index already exists
createIndexService.validateIndexName(rolloverIndexName, currentState);
checkNoDuplicatedAliasInIndexTemplate(metadata, rolloverIndexName, aliasName, isHidden);
if (onlyValidate) {
return new RolloverResult(rolloverIndexName, sourceIndexName, currentState);
}
CreateIndexClusterStateUpdateRequest createIndexClusterStateRequest = prepareCreateIndexRequest(unresolvedName, rolloverIndexName, createIndexRequest);
ClusterState newState = createIndexService.applyCreateIndexRequest(currentState, createIndexClusterStateRequest, silent);
newState = indexAliasesService.applyAliasActions(newState, rolloverAliasToNewIndex(sourceIndexName, rolloverIndexName, explicitWriteIndex, aliasMetadata.isHidden(), aliasName));
RolloverInfo rolloverInfo = new RolloverInfo(aliasName, metConditions, threadPool.absoluteTimeInMillis());
newState = ClusterState.builder(newState).metadata(Metadata.builder(newState.metadata()).put(IndexMetadata.builder(newState.metadata().index(sourceIndexName)).putRolloverInfo(rolloverInfo))).build();
return new RolloverResult(rolloverIndexName, sourceIndexName, newState);
}
use of org.opensearch.cluster.metadata.AliasMetadata in project OpenSearch by opensearch-project.
the class MetadataRolloverService method checkNoDuplicatedAliasInIndexTemplate.
/**
* If the newly created index matches with an index template whose aliases contains the rollover alias,
* the rollover alias will point to multiple indices. This causes indexing requests to be rejected.
* To avoid this, we make sure that there is no duplicated alias in index templates before creating a new index.
*/
static void checkNoDuplicatedAliasInIndexTemplate(Metadata metadata, String rolloverIndexName, String rolloverRequestAlias, @Nullable Boolean isHidden) {
final List<IndexTemplateMetadata> matchedTemplates = findV1Templates(metadata, rolloverIndexName, isHidden);
for (IndexTemplateMetadata template : matchedTemplates) {
if (template.aliases().containsKey(rolloverRequestAlias)) {
throw new IllegalArgumentException(String.format(Locale.ROOT, "Rollover alias [%s] can point to multiple indices, found duplicated alias [%s] in index template [%s]", rolloverRequestAlias, template.aliases().keys(), template.name()));
}
}
final String matchedV2Template = findV2Template(metadata, rolloverIndexName, isHidden == null ? false : isHidden);
if (matchedV2Template != null) {
List<Map<String, AliasMetadata>> aliases = MetadataIndexTemplateService.resolveAliases(metadata, matchedV2Template);
for (Map<String, AliasMetadata> aliasConfig : aliases) {
if (aliasConfig.containsKey(rolloverRequestAlias)) {
throw new IllegalArgumentException(String.format(Locale.ROOT, "Rollover alias [%s] can point to multiple indices, found duplicated alias [%s] in index template [%s]", rolloverRequestAlias, aliasConfig.keySet(), matchedV2Template));
}
}
}
}
use of org.opensearch.cluster.metadata.AliasMetadata in project OpenSearch by opensearch-project.
the class GetIndexResponse method toXContent.
@Override
public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException {
builder.startObject();
{
for (final String index : indices) {
builder.startObject(index);
{
builder.startObject("aliases");
List<AliasMetadata> indexAliases = aliases.get(index);
if (indexAliases != null) {
for (final AliasMetadata alias : indexAliases) {
AliasMetadata.Builder.toXContent(alias, builder, params);
}
}
builder.endObject();
MappingMetadata indexMappings = mappings.get(index);
if (indexMappings == null) {
builder.startObject("mappings").endObject();
} else {
builder.field("mappings", indexMappings.sourceAsMap());
}
builder.startObject("settings");
Settings indexSettings = settings.get(index);
if (indexSettings != null) {
indexSettings.toXContent(builder, params);
}
builder.endObject();
Settings defaultIndexSettings = defaultSettings.get(index);
if (defaultIndexSettings != null && defaultIndexSettings.isEmpty() == false) {
builder.startObject("defaults");
defaultIndexSettings.toXContent(builder, params);
builder.endObject();
}
String dataStream = dataStreams.get(index);
if (dataStream != null) {
builder.field("data_stream", dataStream);
}
}
builder.endObject();
}
}
builder.endObject();
return builder;
}
Aggregations