use of org.elasticsearch.cluster.metadata.IndexTemplateMetadata in project crate by crate.
the class TransportSchemaUpdateAction method updateTemplate.
@VisibleForTesting
static ClusterState updateTemplate(NamedXContentRegistry xContentRegistry, ClusterState currentState, String templateName, Map<String, Object> newMapping) throws Exception {
IndexTemplateMetadata template = currentState.metadata().templates().get(templateName);
if (template == null) {
throw new ResourceNotFoundException("Template \"" + templateName + "\" for partitioned table is missing");
}
IndexTemplateMetadata.Builder templateBuilder = new IndexTemplateMetadata.Builder(template);
for (ObjectObjectCursor<String, CompressedXContent> cursor : template.mappings()) {
Map<String, Object> source = parseMapping(xContentRegistry, cursor.value.toString());
mergeIntoSource(source, newMapping);
try (XContentBuilder xContentBuilder = JsonXContent.contentBuilder()) {
templateBuilder.putMapping(cursor.key, Strings.toString(xContentBuilder.map(source)));
}
}
Metadata.Builder builder = Metadata.builder(currentState.metadata()).put(templateBuilder);
return ClusterState.builder(currentState).metadata(builder).build();
}
use of org.elasticsearch.cluster.metadata.IndexTemplateMetadata in project crate by crate.
the class DocSchemaInfo method update.
@Override
public void update(ClusterChangedEvent event) {
assert event.metadataChanged() : "metadataChanged must be true if update is called";
// search for aliases of deleted and created indices, they must be invalidated also
Metadata prevMetadata = event.previousState().metadata();
for (Index index : event.indicesDeleted()) {
invalidateFromIndex(index, prevMetadata);
}
Metadata newMetadata = event.state().metadata();
for (String index : event.indicesCreated()) {
invalidateAliases(newMetadata.index(index).getAliases());
}
// search for templates with changed meta data => invalidate template aliases
ImmutableOpenMap<String, IndexTemplateMetadata> newTemplates = newMetadata.templates();
ImmutableOpenMap<String, IndexTemplateMetadata> prevTemplates = prevMetadata.templates();
if (!newTemplates.equals(prevTemplates)) {
for (ObjectCursor<IndexTemplateMetadata> cursor : newTemplates.values()) {
invalidateAliases(cursor.value.aliases());
}
for (ObjectCursor<IndexTemplateMetadata> cursor : prevTemplates.values()) {
invalidateAliases(cursor.value.aliases());
}
}
// search indices with changed meta data
Iterator<String> currentTablesIt = docTableByName.keySet().iterator();
ObjectLookupContainer<String> templates = newTemplates.keys();
ImmutableOpenMap<String, IndexMetadata> indices = newMetadata.indices();
while (currentTablesIt.hasNext()) {
String tableName = currentTablesIt.next();
String indexName = getIndexName(tableName);
IndexMetadata newIndexMetadata = newMetadata.index(indexName);
if (newIndexMetadata == null) {
docTableByName.remove(tableName);
} else {
IndexMetadata oldIndexMetadata = prevMetadata.index(indexName);
if (oldIndexMetadata != null && ClusterChangedEvent.indexMetadataChanged(oldIndexMetadata, newIndexMetadata)) {
docTableByName.remove(tableName);
// invalidate aliases of changed indices
invalidateAliases(newIndexMetadata.getAliases());
invalidateAliases(oldIndexMetadata.getAliases());
} else {
// this is the case if a single partition has been modified using alter table <t> partition (...)
String possibleTemplateName = PartitionName.templateName(name(), tableName);
if (templates.contains(possibleTemplateName)) {
for (ObjectObjectCursor<String, IndexMetadata> indexEntry : indices) {
if (IndexParts.isPartitioned(indexEntry.key)) {
docTableByName.remove(tableName);
break;
}
}
}
}
}
}
// re register UDFs for this schema
UserDefinedFunctionsMetadata udfMetadata = newMetadata.custom(UserDefinedFunctionsMetadata.TYPE);
if (udfMetadata != null) {
udfService.updateImplementations(schemaName, udfMetadata.functionsMetadata().stream().filter(f -> schemaName.equals(f.schema())));
}
}
use of org.elasticsearch.cluster.metadata.IndexTemplateMetadata in project crate by crate.
the class DocTableInfoBuilder method buildDocIndexMetadataFromTemplate.
private DocIndexMetadata buildDocIndexMetadataFromTemplate(String index, String templateName) {
IndexTemplateMetadata indexTemplateMetadata = metadata.getTemplates().get(templateName);
DocIndexMetadata docIndexMetadata;
try {
IndexMetadata.Builder builder = new IndexMetadata.Builder(index);
builder.putMapping(Constants.DEFAULT_MAPPING_TYPE, indexTemplateMetadata.getMappings().get(Constants.DEFAULT_MAPPING_TYPE).toString());
Settings.Builder settingsBuilder = Settings.builder().put(indexTemplateMetadata.settings()).put(IndexMetadata.SETTING_VERSION_CREATED, Version.CURRENT);
Settings settings = settingsBuilder.build();
builder.settings(settings);
builder.numberOfShards(settings.getAsInt(SETTING_NUMBER_OF_SHARDS, 5));
builder.numberOfReplicas(settings.getAsInt(SETTING_NUMBER_OF_REPLICAS, 1));
docIndexMetadata = new DocIndexMetadata(nodeCtx, builder.build(), ident);
} catch (IOException e) {
throw new UnhandledServerException("Unable to build DocIndexMetadata from template", e);
}
return docIndexMetadata.build();
}
use of org.elasticsearch.cluster.metadata.IndexTemplateMetadata in project crate by crate.
the class AlterTableClusterStateExecutor method updateTemplate.
static ClusterState updateTemplate(ClusterState currentState, RelationName relationName, Settings newSetting, Map<String, Object> newMapping, BiConsumer<String, Settings> settingsValidator, IndexScopedSettings indexScopedSettings) throws IOException {
String templateName = PartitionName.templateName(relationName.schema(), relationName.name());
IndexTemplateMetadata indexTemplateMetadata = currentState.metadata().templates().get(templateName);
IndexTemplateMetadata newIndexTemplateMetadata = DDLClusterStateHelpers.updateTemplate(indexTemplateMetadata, newMapping, Collections.emptyMap(), newSetting, settingsValidator, k -> indexScopedSettings.isPrivateSetting(k) == false);
final Metadata.Builder metadata = Metadata.builder(currentState.metadata()).put(newIndexTemplateMetadata);
return ClusterState.builder(currentState).metadata(metadata).build();
}
use of org.elasticsearch.cluster.metadata.IndexTemplateMetadata in project crate by crate.
the class RenameTableClusterStateExecutor method execute.
public ClusterState execute(ClusterState currentState, RenameTableRequest request) throws Exception {
RelationName source = request.sourceTableIdent();
RelationName target = request.targetTableIdent();
boolean isPartitioned = request.isPartitioned();
Metadata currentMetadata = currentState.getMetadata();
Metadata.Builder newMetadata = Metadata.builder(currentMetadata);
if (isPartitioned) {
IndexTemplateMetadata indexTemplateMetadata = DDLClusterStateHelpers.templateMetadata(currentMetadata, source);
if (indexTemplateMetadata == null) {
throw new IndexTemplateMissingException("Template for partitioned table is missing");
}
renameTemplate(newMetadata, indexTemplateMetadata, target);
}
RoutingTable.Builder newRoutingTable = RoutingTable.builder(currentState.routingTable());
ClusterBlocks.Builder blocksBuilder = ClusterBlocks.builder().blocks(currentState.blocks());
logger.info("renaming table '{}' to '{}'", source.fqn(), target.fqn());
try {
Index[] sourceIndices = indexNameExpressionResolver.concreteIndices(currentState, STRICT_INDICES_OPTIONS, source.indexNameOrAlias());
for (Index sourceIndex : sourceIndices) {
IndexMetadata sourceIndexMetadata = currentMetadata.getIndexSafe(sourceIndex);
String sourceIndexName = sourceIndex.getName();
newMetadata.remove(sourceIndexName);
newRoutingTable.remove(sourceIndexName);
blocksBuilder.removeIndexBlocks(sourceIndexName);
IndexMetadata targetMd;
if (isPartitioned) {
PartitionName partitionName = PartitionName.fromIndexOrTemplate(sourceIndexName);
String targetIndexName = IndexParts.toIndexName(target, partitionName.ident());
targetMd = IndexMetadata.builder(sourceIndexMetadata).removeAllAliases().putAlias(AliasMetadata.builder(target.indexNameOrAlias()).build()).index(targetIndexName).build();
} else {
targetMd = IndexMetadata.builder(sourceIndexMetadata).index(target.indexNameOrAlias()).build();
}
newMetadata.put(targetMd, true);
newRoutingTable.addAsFromCloseToOpen(targetMd);
blocksBuilder.addBlocks(targetMd);
}
} catch (IndexNotFoundException e) {
if (isPartitioned == false) {
throw e;
}
// empty partition case, no indices, just a template exists
}
ClusterState clusterStateAfterRename = ClusterState.builder(currentState).metadata(newMetadata).routingTable(newRoutingTable.build()).blocks(blocksBuilder).build();
return allocationService.reroute(ddlClusterStateService.onRenameTable(clusterStateAfterRename, source, target, request.isPartitioned()), "rename-table");
}
Aggregations