use of org.elasticsearch.cluster.metadata.IndexTemplateMetaData in project crate by crate.
the class OpenTableClusterStateTaskExecutor method execute.
@Override
protected ClusterState execute(ClusterState currentState, OpenCloseTableOrPartitionRequest request) throws Exception {
Context context = prepare(currentState, request);
Set<IndexMetadata> indicesToOpen = context.indicesMetadata();
IndexTemplateMetadata templateMetadata = context.templateMetadata();
if (indicesToOpen.isEmpty() && templateMetadata == null) {
return currentState;
}
Metadata.Builder mdBuilder = Metadata.builder(currentState.metadata());
ClusterBlocks.Builder blocksBuilder = ClusterBlocks.builder().blocks(currentState.blocks());
final Version minIndexCompatibilityVersion = currentState.getNodes().getMaxNodeVersion().minimumIndexCompatibilityVersion();
for (IndexMetadata closedMetadata : indicesToOpen) {
final String indexName = closedMetadata.getIndex().getName();
blocksBuilder.removeIndexBlockWithId(indexName, TransportCloseTable.INDEX_CLOSED_BLOCK_ID);
if (closedMetadata.getState() == IndexMetadata.State.OPEN) {
continue;
}
final Settings.Builder updatedSettings = Settings.builder().put(closedMetadata.getSettings());
updatedSettings.remove(IndexMetadata.VERIFIED_BEFORE_CLOSE_SETTING.getKey());
IndexMetadata updatedIndexMetadata = IndexMetadata.builder(closedMetadata).state(IndexMetadata.State.OPEN).settingsVersion(closedMetadata.getSettingsVersion() + 1).settings(updatedSettings).build();
// The index might be closed because we couldn't import it due to old incompatible version
// We need to check that this index can be upgraded to the current version
updatedIndexMetadata = metadataIndexUpgradeService.upgradeIndexMetadata(updatedIndexMetadata, minIndexCompatibilityVersion);
try {
indicesService.verifyIndexMetadata(updatedIndexMetadata, updatedIndexMetadata);
} catch (Exception e) {
throw new ElasticsearchException("Failed to verify index " + indexName, e);
}
mdBuilder.put(updatedIndexMetadata, true);
}
// remove closed flag at possible partitioned table template
if (templateMetadata != null) {
mdBuilder.put(updateOpenCloseOnPartitionTemplate(templateMetadata, true));
}
// The Metadata will always be overridden (and not merged!) when applying it on a cluster state builder.
// So we must re-build the state with the latest modifications before we pass this state to possible modifiers.
// Otherwise they would operate on the old Metadata and would just ignore any modifications.
ClusterState updatedState = ClusterState.builder(currentState).metadata(mdBuilder).blocks(blocksBuilder).build();
// call possible registered modifiers
if (context.partitionName() != null) {
updatedState = ddlClusterStateService.onOpenTablePartition(updatedState, context.partitionName());
} else {
updatedState = ddlClusterStateService.onOpenTable(updatedState, request.tableIdent());
}
RoutingTable.Builder rtBuilder = RoutingTable.builder(updatedState.routingTable());
for (IndexMetadata index : indicesToOpen) {
rtBuilder.addAsFromCloseToOpen(updatedState.metadata().getIndexSafe(index.getIndex()));
}
// no explicit wait for other nodes needed as we use AckedClusterStateUpdateTask
return allocationService.reroute(ClusterState.builder(updatedState).routingTable(rtBuilder.build()).build(), "indices opened " + indicesToOpen);
}
use of org.elasticsearch.cluster.metadata.IndexTemplateMetaData in project crate by crate.
the class CloseTableClusterStateTaskExecutor method execute.
@Override
protected ClusterState execute(ClusterState currentState, OpenCloseTableOrPartitionRequest request) throws Exception {
Context context = prepare(currentState, request);
Set<Index> indicesToClose = context.indicesMetadata().stream().map(IndexMetadata::getIndex).collect(Collectors.toSet());
IndexTemplateMetadata templateMetadata = context.templateMetadata();
if (indicesToClose.isEmpty() && templateMetadata == null) {
return currentState;
}
// Check if index closing conflicts with any running restores
Set<Index> restoringIndices = RestoreService.restoringIndices(currentState, indicesToClose);
if (restoringIndices.isEmpty() == false) {
throw new IllegalArgumentException("Cannot close indices that are being restored: " + restoringIndices);
}
// Check if index closing conflicts with any running snapshots
Set<Index> snapshottingIndices = SnapshotsService.snapshottingIndices(currentState, indicesToClose);
if (snapshottingIndices.isEmpty() == false) {
throw new SnapshotInProgressException("Cannot close indices that are being snapshotted: " + snapshottingIndices + ". Try again after snapshot finishes or cancel the currently running snapshot.");
}
Metadata.Builder mdBuilder = Metadata.builder(currentState.metadata());
ClusterBlocks.Builder blocksBuilder = ClusterBlocks.builder().blocks(currentState.blocks());
for (IndexMetadata openIndexMetadata : context.indicesMetadata()) {
final String indexName = openIndexMetadata.getIndex().getName();
mdBuilder.put(IndexMetadata.builder(openIndexMetadata).state(IndexMetadata.State.CLOSE));
blocksBuilder.addIndexBlock(indexName, INDEX_CLOSED_BLOCK);
}
// mark closed at possible partitioned table template
if (templateMetadata != null) {
mdBuilder.put(updateOpenCloseOnPartitionTemplate(templateMetadata, false));
}
// The Metadata will always be overridden (and not merged!) when applying it on a cluster state builder.
// So we must re-build the state with the latest modifications before we pass this state to possible modifiers.
// Otherwise they would operate on the old Metadata and would just ignore any modifications.
ClusterState updatedState = ClusterState.builder(currentState).metadata(mdBuilder).blocks(blocksBuilder).build();
// call possible registered modifiers
if (context.partitionName() != null) {
updatedState = ddlClusterStateService.onCloseTablePartition(updatedState, context.partitionName());
} else {
updatedState = ddlClusterStateService.onCloseTable(updatedState, request.tableIdent());
}
RoutingTable.Builder rtBuilder = RoutingTable.builder(currentState.routingTable());
for (Index index : indicesToClose) {
rtBuilder.remove(index.getName());
}
// no explicit wait for other nodes needed as we use AckedClusterStateUpdateTask
return allocationService.reroute(ClusterState.builder(updatedState).routingTable(rtBuilder.build()).build(), "indices closed " + indicesToClose);
}
use of org.elasticsearch.cluster.metadata.IndexTemplateMetaData in project crate by crate.
the class TransportCreatePartitionsAction method applyTemplates.
private void applyTemplates(Map<String, Map<String, Object>> mappings, Map<String, AliasMetadata> templatesAliases, List<String> templateNames, List<IndexTemplateMetadata> templates) throws Exception {
for (IndexTemplateMetadata template : templates) {
templateNames.add(template.getName());
for (ObjectObjectCursor<String, CompressedXContent> cursor : template.mappings()) {
if (mappings.containsKey(cursor.key)) {
XContentHelper.mergeDefaults(mappings.get(cursor.key), parseMapping(cursor.value.string()));
} else {
mappings.put(cursor.key, parseMapping(cursor.value.string()));
}
}
// handle aliases
for (ObjectObjectCursor<String, AliasMetadata> cursor : template.aliases()) {
AliasMetadata aliasMetadata = cursor.value;
templatesAliases.put(aliasMetadata.alias(), aliasMetadata);
}
}
}
use of org.elasticsearch.cluster.metadata.IndexTemplateMetaData in project crate by crate.
the class TransportSchemaUpdateActionTest method testTemplateMappingUpdateFailsIfTypeIsDifferent.
@Test
public void testTemplateMappingUpdateFailsIfTypeIsDifferent() throws Exception {
SQLExecutor e = SQLExecutor.builder(clusterService).addPartitionedTable("create table t (p int) partitioned by (p)").build();
ClusterState currentState = clusterService.state();
PlannerContext plannerContext = e.getPlannerContext(currentState);
BoundAddColumn addXLong = AlterTableAddColumnPlan.bind(e.analyze("alter table t add column x long"), plannerContext.transactionContext(), plannerContext.nodeContext(), Row.EMPTY, SubQueryResults.EMPTY, null);
BoundAddColumn addXString = AlterTableAddColumnPlan.bind(e.analyze("alter table t add column x string"), plannerContext.transactionContext(), plannerContext.nodeContext(), Row.EMPTY, SubQueryResults.EMPTY, null);
String templateName = templateName("doc", "t");
IndexTemplateMetadata template = currentState.metadata().templates().get(templateName);
ClusterState stateWithXLong = ClusterState.builder(currentState).metadata(Metadata.builder(currentState.metadata()).put(IndexTemplateMetadata.builder(templateName).patterns(template.patterns()).putMapping(Constants.DEFAULT_MAPPING_TYPE, Strings.toString(JsonXContent.contentBuilder().map(addXLong.mapping())))).build()).build();
expectedException.expect(IllegalArgumentException.class);
TransportSchemaUpdateAction.updateTemplate(NamedXContentRegistry.EMPTY, stateWithXLong, templateName, addXString.mapping());
}
use of org.elasticsearch.cluster.metadata.IndexTemplateMetaData in project crate by crate.
the class ClusterState method toXContent.
@Override
@SuppressWarnings("unchecked")
public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException {
EnumSet<Metric> metrics = Metric.parseString(params.param("metric", "_all"), true);
// always provide the cluster_uuid as part of the top-level response (also part of the metadata response)
builder.field("cluster_uuid", metadata().clusterUUID());
if (metrics.contains(Metric.VERSION)) {
builder.field("version", version);
builder.field("state_uuid", stateUUID);
}
if (metrics.contains(Metric.MASTER_NODE)) {
builder.field("master_node", nodes().getMasterNodeId());
}
if (metrics.contains(Metric.BLOCKS)) {
builder.startObject("blocks");
if (!blocks().global().isEmpty()) {
builder.startObject("global");
for (ClusterBlock block : blocks().global()) {
block.toXContent(builder, params);
}
builder.endObject();
}
if (!blocks().indices().isEmpty()) {
builder.startObject("indices");
for (ObjectObjectCursor<String, Set<ClusterBlock>> entry : blocks().indices()) {
builder.startObject(entry.key);
for (ClusterBlock block : entry.value) {
block.toXContent(builder, params);
}
builder.endObject();
}
builder.endObject();
}
builder.endObject();
}
// nodes
if (metrics.contains(Metric.NODES)) {
builder.startObject("nodes");
for (DiscoveryNode node : nodes) {
node.toXContent(builder, params);
}
builder.endObject();
}
// meta data
if (metrics.contains(Metric.METADATA)) {
builder.startObject("metadata");
builder.field("cluster_uuid", metadata().clusterUUID());
builder.startObject("cluster_coordination");
coordinationMetadata().toXContent(builder, params);
builder.endObject();
builder.startObject("templates");
for (ObjectCursor<IndexTemplateMetadata> cursor : metadata().templates().values()) {
IndexTemplateMetadata templateMetadata = cursor.value;
builder.startObject(templateMetadata.name());
builder.field("index_patterns", templateMetadata.patterns());
builder.field("order", templateMetadata.order());
builder.startObject("settings");
Settings settings = templateMetadata.settings();
settings.toXContent(builder, params);
builder.endObject();
builder.startObject("mappings");
for (ObjectObjectCursor<String, CompressedXContent> cursor1 : templateMetadata.mappings()) {
Map<String, Object> mapping = XContentHelper.convertToMap(new BytesArray(cursor1.value.uncompressed()), false).v2();
if (mapping.size() == 1 && mapping.containsKey(cursor1.key)) {
// the type name is the root value, reduce it
mapping = (Map<String, Object>) mapping.get(cursor1.key);
}
builder.field(cursor1.key);
builder.map(mapping);
}
builder.endObject();
builder.endObject();
}
builder.endObject();
builder.startObject("indices");
for (IndexMetadata indexMetadata : metadata()) {
builder.startObject(indexMetadata.getIndex().getName());
builder.field("state", indexMetadata.getState().toString().toLowerCase(Locale.ENGLISH));
builder.startObject("settings");
Settings settings = indexMetadata.getSettings();
settings.toXContent(builder, params);
builder.endObject();
builder.startObject("mappings");
MappingMetadata mmd = indexMetadata.mapping();
if (mmd != null) {
Map<String, Object> mapping = XContentHelper.convertToMap(new BytesArray(mmd.source().uncompressed()), false).v2();
if (mapping.size() == 1 && mapping.containsKey(mmd.type())) {
// the type name is the root value, reduce it
mapping = (Map<String, Object>) mapping.get(mmd.type());
}
builder.field(mmd.type());
builder.map(mapping);
}
builder.endObject();
builder.startArray("aliases");
for (ObjectCursor<String> cursor : indexMetadata.getAliases().keys()) {
builder.value(cursor.value);
}
builder.endArray();
builder.startObject(IndexMetadata.KEY_PRIMARY_TERMS);
for (int shard = 0; shard < indexMetadata.getNumberOfShards(); shard++) {
builder.field(Integer.toString(shard), indexMetadata.primaryTerm(shard));
}
builder.endObject();
builder.startObject(IndexMetadata.KEY_IN_SYNC_ALLOCATIONS);
for (IntObjectCursor<Set<String>> cursor : indexMetadata.getInSyncAllocationIds()) {
builder.startArray(String.valueOf(cursor.key));
for (String allocationId : cursor.value) {
builder.value(allocationId);
}
builder.endArray();
}
builder.endObject();
// index metadata
builder.endObject();
}
builder.endObject();
for (ObjectObjectCursor<String, Metadata.Custom> cursor : metadata.customs()) {
builder.startObject(cursor.key);
cursor.value.toXContent(builder, params);
builder.endObject();
}
builder.endObject();
}
// routing table
if (metrics.contains(Metric.ROUTING_TABLE)) {
builder.startObject("routing_table");
builder.startObject("indices");
for (IndexRoutingTable indexRoutingTable : routingTable()) {
builder.startObject(indexRoutingTable.getIndex().getName());
builder.startObject("shards");
for (IndexShardRoutingTable indexShardRoutingTable : indexRoutingTable) {
builder.startArray(Integer.toString(indexShardRoutingTable.shardId().id()));
for (ShardRouting shardRouting : indexShardRoutingTable) {
shardRouting.toXContent(builder, params);
}
builder.endArray();
}
builder.endObject();
builder.endObject();
}
builder.endObject();
builder.endObject();
}
// routing nodes
if (metrics.contains(Metric.ROUTING_NODES)) {
builder.startObject("routing_nodes");
builder.startArray("unassigned");
for (ShardRouting shardRouting : getRoutingNodes().unassigned()) {
shardRouting.toXContent(builder, params);
}
builder.endArray();
builder.startObject("nodes");
for (RoutingNode routingNode : getRoutingNodes()) {
builder.startArray(routingNode.nodeId() == null ? "null" : routingNode.nodeId());
for (ShardRouting shardRouting : routingNode) {
shardRouting.toXContent(builder, params);
}
builder.endArray();
}
builder.endObject();
builder.endObject();
}
if (metrics.contains(Metric.CUSTOMS)) {
for (ObjectObjectCursor<String, Custom> cursor : customs) {
builder.startObject(cursor.key);
cursor.value.toXContent(builder, params);
builder.endObject();
}
}
return builder;
}
Aggregations