use of org.elasticsearch.cluster.metadata.IndexTemplateMetaData in project crate by crate.
the class AbstractOpenCloseTableClusterStateTaskExecutor method prepare.
protected Context prepare(ClusterState currentState, OpenCloseTableOrPartitionRequest request) {
RelationName relationName = request.tableIdent();
String partitionIndexName = request.partitionIndexName();
Metadata metadata = currentState.metadata();
String indexToResolve = partitionIndexName != null ? partitionIndexName : relationName.indexNameOrAlias();
PartitionName partitionName = partitionIndexName != null ? PartitionName.fromIndexOrTemplate(partitionIndexName) : null;
String[] concreteIndices = indexNameExpressionResolver.concreteIndexNames(currentState, IndicesOptions.lenientExpandOpen(), indexToResolve);
Set<IndexMetadata> indicesMetadata = DDLClusterStateHelpers.indexMetadataSetFromIndexNames(metadata, concreteIndices, indexState());
IndexTemplateMetadata indexTemplateMetadata = null;
if (partitionIndexName == null) {
indexTemplateMetadata = DDLClusterStateHelpers.templateMetadata(metadata, relationName);
}
return new Context(indicesMetadata, indexTemplateMetadata, partitionName);
}
use of org.elasticsearch.cluster.metadata.IndexTemplateMetaData in project crate by crate.
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.elasticsearch.cluster.metadata.IndexTemplateMetaData in project crate by crate.
the class GetIndexTemplatesResponse method fromXContent.
public static GetIndexTemplatesResponse fromXContent(XContentParser parser) throws IOException {
final List<IndexTemplateMetadata> templates = new ArrayList<>();
for (XContentParser.Token token = parser.nextToken(); token != XContentParser.Token.END_OBJECT; token = parser.nextToken()) {
if (token == XContentParser.Token.FIELD_NAME) {
final IndexTemplateMetadata templateMetadata = IndexTemplateMetadata.Builder.fromXContent(parser, parser.currentName());
templates.add(templateMetadata);
}
}
return new GetIndexTemplatesResponse(templates);
}
use of org.elasticsearch.cluster.metadata.IndexTemplateMetaData in project crate by crate.
the class DDLClusterStateHelpers method updateTemplate.
public static IndexTemplateMetadata updateTemplate(IndexTemplateMetadata indexTemplateMetadata, Map<String, Object> newMappings, Map<String, Object> mappingsToRemove, Settings newSettings, BiConsumer<String, Settings> settingsValidator, Predicate<String> settingsFilter) {
// merge mappings & remove mappings
Map<String, Object> mapping = removeFromMapping(mergeTemplateMapping(indexTemplateMetadata, newMappings), mappingsToRemove);
// merge settings
final Settings settings = Settings.builder().put(indexTemplateMetadata.settings()).put(newSettings).normalizePrefix(IndexMetadata.INDEX_SETTING_PREFIX).build().filter(settingsFilter);
settingsValidator.accept(indexTemplateMetadata.getName(), settings);
// wrap it in a type map if its not
if (mapping.size() != 1 || mapping.containsKey(Constants.DEFAULT_MAPPING_TYPE) == false) {
mapping = MapBuilder.<String, Object>newMapBuilder().put(Constants.DEFAULT_MAPPING_TYPE, mapping).map();
}
try {
return new IndexTemplateMetadata.Builder(indexTemplateMetadata).settings(settings).putMapping(Constants.DEFAULT_MAPPING_TYPE, Strings.toString(XContentFactory.jsonBuilder().map(mapping))).build();
} catch (IOException e) {
throw new RuntimeException(e);
}
}
use of org.elasticsearch.cluster.metadata.IndexTemplateMetaData in project crate by crate.
the class IndexTemplateUpgrader method archiveUnknownOrInvalidSettings.
/**
* Filter out all unknown/old/invalid settings. Archiving them *only* is not working as they would be "un-archived"
* by {@link IndexTemplateMetadata.Builder#fromXContent} logic to prefix all settings with `index.` when applying
* the new cluster state.
*/
private HashMap<String, IndexTemplateMetadata> archiveUnknownOrInvalidSettings(Map<String, IndexTemplateMetadata> templates) {
HashMap<String, IndexTemplateMetadata> upgradedTemplates = new HashMap<>(templates.size());
for (Map.Entry<String, IndexTemplateMetadata> entry : templates.entrySet()) {
IndexTemplateMetadata templateMetadata = entry.getValue();
Settings.Builder settingsBuilder = Settings.builder().put(templateMetadata.settings());
String templateName = entry.getKey();
// only process partition table templates
if (IndexParts.isPartitioned(templateName) == false) {
upgradedTemplates.put(templateName, templateMetadata);
continue;
}
Settings settings = DEFAULT_SCOPED_SETTINGS.archiveUnknownOrInvalidSettings(settingsBuilder.build(), e -> {
}, (e, ex) -> {
}).filter(k -> k.startsWith(ARCHIVED_SETTINGS_PREFIX) == false);
IndexTemplateMetadata.Builder builder = IndexTemplateMetadata.builder(templateName).patterns(templateMetadata.patterns()).order(templateMetadata.order()).settings(settings);
try {
for (ObjectObjectCursor<String, CompressedXContent> cursor : templateMetadata.getMappings()) {
var mappingSource = XContentHelper.toMap(cursor.value.compressedReference(), XContentType.JSON);
Object defaultMapping = mappingSource.get("default");
if (defaultMapping instanceof Map && ((Map<?, ?>) defaultMapping).containsKey("_all")) {
Map<?, ?> mapping = (Map<?, ?>) defaultMapping;
// Support for `_all` was removed (in favour of `copy_to`.
// We never utilized this but always set `_all: {enabled: false}` if you created a table using SQL in earlier version, so we can safely drop it.
mapping.remove("_all");
builder.putMapping(cursor.key, new CompressedXContent(BytesReference.bytes(XContentFactory.jsonBuilder().value(mappingSource))));
} else {
builder.putMapping(cursor.key, cursor.value);
}
}
} catch (IOException e) {
logger.error("Error while trying to upgrade template '" + templateName + "'", e);
continue;
}
for (ObjectObjectCursor<String, AliasMetadata> container : templateMetadata.aliases()) {
builder.putAlias(container.value);
}
upgradedTemplates.put(templateName, builder.build());
}
return upgradedTemplates;
}
Aggregations