use of org.opensearch.cluster.metadata.IndexTemplateMetadata in project OpenSearch by opensearch-project.
the class GatewayMetaState method upgradeMetadata.
/**
* This method calls {@link MetadataIndexUpgradeService} to makes sure that indices are compatible with the current
* version. The MetadataIndexUpgradeService might also update obsolete settings if needed.
*
* @return input <code>metadata</code> if no upgrade is needed or an upgraded metadata
*/
static Metadata upgradeMetadata(Metadata metadata, MetadataIndexUpgradeService metadataIndexUpgradeService, MetadataUpgrader metadataUpgrader) {
// upgrade index meta data
boolean changed = false;
final Metadata.Builder upgradedMetadata = Metadata.builder(metadata);
for (IndexMetadata indexMetadata : metadata) {
IndexMetadata newMetadata = metadataIndexUpgradeService.upgradeIndexMetadata(indexMetadata, Version.CURRENT.minimumIndexCompatibilityVersion());
changed |= indexMetadata != newMetadata;
upgradedMetadata.put(newMetadata, false);
}
// upgrade current templates
if (applyPluginUpgraders(metadata.getTemplates(), metadataUpgrader.indexTemplateMetadataUpgraders, upgradedMetadata::removeTemplate, (s, indexTemplateMetadata) -> upgradedMetadata.put(indexTemplateMetadata))) {
changed = true;
}
return changed ? upgradedMetadata.build() : metadata;
}
use of org.opensearch.cluster.metadata.IndexTemplateMetadata in project OpenSearch by opensearch-project.
the class GetIndexTemplatesResponse method toXContent.
@Override
public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException {
params = new ToXContent.DelegatingMapParams(singletonMap("reduce_mappings", "true"), params);
builder.startObject();
for (IndexTemplateMetadata indexTemplateMetadata : getIndexTemplates()) {
IndexTemplateMetadata.Builder.toXContent(indexTemplateMetadata, builder, params);
}
builder.endObject();
return builder;
}
use of org.opensearch.cluster.metadata.IndexTemplateMetadata 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.IndexTemplateMetadata in project OpenSearch by opensearch-project.
the class MetadataRolloverServiceTests method testRejectDuplicateAlias.
public void testRejectDuplicateAlias() {
final IndexTemplateMetadata template = IndexTemplateMetadata.builder("test-template").patterns(Arrays.asList("foo-*", "bar-*")).putAlias(AliasMetadata.builder("foo-write")).putAlias(AliasMetadata.builder("bar-write").writeIndex(randomBoolean())).build();
final Metadata metadata = Metadata.builder().put(createMetadata(randomAlphaOfLengthBetween(5, 7)), false).put(template).build();
String indexName = randomFrom("foo-123", "bar-xyz");
String aliasName = randomFrom("foo-write", "bar-write");
final IllegalArgumentException ex = expectThrows(IllegalArgumentException.class, () -> MetadataRolloverService.checkNoDuplicatedAliasInIndexTemplate(metadata, indexName, aliasName, randomBoolean()));
assertThat(ex.getMessage(), containsString("index template [test-template]"));
}
use of org.opensearch.cluster.metadata.IndexTemplateMetadata in project OpenSearch by opensearch-project.
the class TransportBulkActionIngestTests method testFindDefaultPipelineFromTemplateMatch.
public void testFindDefaultPipelineFromTemplateMatch() {
Exception exception = new Exception("fake exception");
ClusterState state = clusterService.state();
ImmutableOpenMap.Builder<String, IndexTemplateMetadata> templateMetadataBuilder = ImmutableOpenMap.builder();
templateMetadataBuilder.put("template1", IndexTemplateMetadata.builder("template1").patterns(Arrays.asList("missing_index")).order(1).settings(Settings.builder().put(IndexSettings.DEFAULT_PIPELINE.getKey(), "pipeline1").build()).build());
templateMetadataBuilder.put("template2", IndexTemplateMetadata.builder("template2").patterns(Arrays.asList("missing_*")).order(2).settings(Settings.builder().put(IndexSettings.DEFAULT_PIPELINE.getKey(), "pipeline2").build()).build());
templateMetadataBuilder.put("template3", IndexTemplateMetadata.builder("template3").patterns(Arrays.asList("missing*")).order(3).build());
templateMetadataBuilder.put("template4", IndexTemplateMetadata.builder("template4").patterns(Arrays.asList("nope")).order(4).settings(Settings.builder().put(IndexSettings.DEFAULT_PIPELINE.getKey(), "pipeline4").build()).build());
Metadata metadata = mock(Metadata.class);
when(state.metadata()).thenReturn(metadata);
when(state.getMetadata()).thenReturn(metadata);
when(metadata.templates()).thenReturn(templateMetadataBuilder.build());
when(metadata.getTemplates()).thenReturn(templateMetadataBuilder.build());
when(metadata.indices()).thenReturn(ImmutableOpenMap.of());
IndexRequest indexRequest = new IndexRequest("missing_index").id("id");
indexRequest.source(emptyMap());
AtomicBoolean responseCalled = new AtomicBoolean(false);
AtomicBoolean failureCalled = new AtomicBoolean(false);
singleItemBulkWriteAction.execute(null, indexRequest, ActionListener.wrap(response -> responseCalled.set(true), e -> {
assertThat(e, sameInstance(exception));
failureCalled.set(true);
}));
assertEquals("pipeline2", indexRequest.getPipeline());
verify(ingestService).executeBulkRequest(eq(1), bulkDocsItr.capture(), failureHandler.capture(), completionHandler.capture(), any(), eq(Names.WRITE));
}
Aggregations