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(functions, 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 graylog2-server by Graylog2.
the class IndicesTest method testIndexTemplateCanBeOverridden.
@Test
public void testIndexTemplateCanBeOverridden() throws Exception {
final String customTemplateName = "custom-template";
final IndicesAdminClient client = this.client.admin().indices();
// Create custom index template
final Map<String, Object> customMapping = ImmutableMap.of("_source", ImmutableMap.of("enabled", false), "properties", ImmutableMap.of("message", ImmutableMap.of("type", "string", "index", "not_analyzed")));
final PutIndexTemplateResponse putIndexTemplateResponse = client.preparePutTemplate(customTemplateName).setTemplate(indexSet.getIndexWildcard()).setOrder(1).addMapping(IndexMapping.TYPE_MESSAGE, customMapping).get();
assertThat(putIndexTemplateResponse.isAcknowledged()).isTrue();
// Validate existing index templates
final GetIndexTemplatesResponse getTemplatesResponse = client.prepareGetTemplates().get();
final List<IndexTemplateMetaData> indexTemplates = getTemplatesResponse.getIndexTemplates();
assertThat(indexTemplates).extracting(IndexTemplateMetaData::getName).containsExactly(customTemplateName);
// Create index with custom template
final String testIndexName = "graylog_override_template";
indices.create(testIndexName, indexSet);
// Check index mapping
final GetMappingsResponse indexMappingResponse = client.prepareGetMappings(testIndexName).get();
final String mapping = indexMappingResponse.getMappings().get(testIndexName).get(IndexMapping.TYPE_MESSAGE).source().string();
final ReadContext ctx = JsonPath.parse(mapping);
final boolean sourceEnabled = ctx.read("$.message._source.enabled");
assertThat(sourceEnabled).isFalse();
final String messageField = ctx.read("$.message.properties.message.index");
assertThat(messageField).isEqualTo("not_analyzed");
// Clean up
final DeleteIndexTemplateResponse deleteResponse = client.prepareDeleteTemplate(customTemplateName).get();
assertThat(deleteResponse.isAcknowledged()).isTrue();
indices.delete(testIndexName);
}
use of org.elasticsearch.cluster.metadata.IndexTemplateMetaData in project graylog2-server by Graylog2.
the class IndicesTest method testCreateEnsuresIndexTemplateExists.
@Test
public void testCreateEnsuresIndexTemplateExists() throws Exception {
final String templateName = indexSetConfig.indexTemplateName();
final IndicesAdminClient client = this.client.admin().indices();
final GetIndexTemplatesRequest request = client.prepareGetTemplates(templateName).request();
final GetIndexTemplatesResponse responseBefore = client.getTemplates(request).actionGet();
assertThat(responseBefore.getIndexTemplates()).isEmpty();
indices.create("index_template_test", indexSet);
final GetIndexTemplatesResponse responseAfter = client.getTemplates(request).actionGet();
assertThat(responseAfter.getIndexTemplates()).hasSize(1);
final IndexTemplateMetaData templateMetaData = responseAfter.getIndexTemplates().get(0);
assertThat(templateMetaData.getName()).isEqualTo(templateName);
assertThat(templateMetaData.getMappings().keysIt()).containsExactly(IndexMapping.TYPE_MESSAGE);
final DeleteIndexTemplateRequest deleteRequest = client.prepareDeleteTemplate(templateName).request();
final DeleteIndexTemplateResponse deleteResponse = client.deleteTemplate(deleteRequest).actionGet();
assertThat(deleteResponse.isAcknowledged()).isTrue();
indices.delete("index_template_test");
}
use of org.elasticsearch.cluster.metadata.IndexTemplateMetaData in project elasticsearch-skywalker by jprante.
the class MetaDataToXContent method toXContent.
@Override
public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException {
builder.startObject();
builder.field("version", metadata.version());
builder.field("numberOfShards", metadata.numberOfShards());
builder.startArray("concreteIndices");
for (String index : metadata.concreteAllIndices()) {
builder.value(index);
}
builder.endArray();
ImmutableOpenMap<String, ImmutableOpenMap<String, AliasMetaData>> aliases = metadata.getAliases();
builder.startArray("aliases");
for (ObjectCursor<String> alias : aliases.keys()) {
builder.startObject(alias.value);
builder.startArray("aliasMetadata");
for (ObjectCursor<String> s : aliases.get(alias.value).keys()) {
builder.startObject();
AliasMetaData amd = aliases.get(alias.value).get(s.value);
builder.field("alias", amd.getAlias());
builder.field("filter", amd.getFilter().string());
builder.field("indexRouting", amd.getIndexRouting());
builder.field("searchRouting", amd.getSearchRouting());
builder.endObject();
}
builder.endArray();
builder.endObject();
}
builder.endArray();
builder.startArray("indexes");
ImmutableOpenMap<String, IndexMetaData> indices = metadata.getIndices();
for (ObjectCursor<String> s : indices.keys()) {
IndexMetaData imd = indices.get(s.value);
builder.startObject();
builder.field("index", imd.getIndex());
builder.field("state", imd.getState().name());
builder.field("numberOfReplicas", imd.getNumberOfReplicas());
builder.field("numberOfShards", imd.getNumberOfShards());
builder.field("totalNumberOfShards", imd.getTotalNumberOfShards());
builder.field("version", imd.getVersion());
builder.field("settings", imd.getSettings().getAsMap());
ImmutableOpenMap<String, MappingMetaData> m = imd.getMappings();
// skip mappings here
builder.endObject();
}
builder.endArray();
builder.startArray("templates");
ImmutableOpenMap<String, IndexTemplateMetaData> templates = metadata.getTemplates();
for (ObjectCursor<String> s : templates.keys()) {
IndexTemplateMetaData itmd = templates.get(s.value);
itmd.getName();
itmd.getOrder();
itmd.getTemplate();
itmd.getSettings();
itmd.getMappings();
}
builder.endArray();
builder.endObject();
return builder;
}
use of org.elasticsearch.cluster.metadata.IndexTemplateMetaData in project crate by crate.
the class TransportCloseTable method closeRoutingTable.
/**
* Step 3 - Move index states from OPEN to CLOSE in cluster state for indices that are ready for closing.
* @param target
*/
static ClusterState closeRoutingTable(ClusterState currentState, AlterTableTarget target, DDLClusterStateService ddlClusterStateService, Map<Index, ClusterBlock> blockedIndices, Map<Index, AcknowledgedResponse> results) {
// Remove the index routing table of closed indices if the cluster is in a mixed version
// that does not support the replication of closed indices
final boolean removeRoutingTable = currentState.nodes().getMinNodeVersion().before(Version.V_4_3_0);
IndexTemplateMetadata templateMetadata = target.templateMetadata();
ClusterState updatedState;
if (templateMetadata == null) {
updatedState = currentState;
} else {
Metadata.Builder metadata = Metadata.builder(currentState.metadata());
metadata.put(closePartitionTemplate(templateMetadata));
updatedState = ClusterState.builder(currentState).metadata(metadata).build();
}
String partition = target.partition();
if (partition != null) {
PartitionName partitionName = PartitionName.fromIndexOrTemplate(partition);
updatedState = ddlClusterStateService.onCloseTablePartition(updatedState, partitionName);
} else {
updatedState = ddlClusterStateService.onCloseTable(updatedState, target.table());
}
final Metadata.Builder metadata = Metadata.builder(updatedState.metadata());
final ClusterBlocks.Builder blocks = ClusterBlocks.builder().blocks(updatedState.blocks());
final RoutingTable.Builder routingTable = RoutingTable.builder(updatedState.routingTable());
final Set<String> closedIndices = new HashSet<>();
for (Map.Entry<Index, AcknowledgedResponse> result : results.entrySet()) {
final Index index = result.getKey();
final boolean acknowledged = result.getValue().isAcknowledged();
try {
if (acknowledged == false) {
LOGGER.debug("verification of shards before closing {} failed", index);
continue;
}
final IndexMetadata indexMetadata = metadata.getSafe(index);
if (indexMetadata.getState() == IndexMetadata.State.CLOSE) {
LOGGER.debug("verification of shards before closing {} succeeded but index is already closed", index);
assert currentState.blocks().hasIndexBlock(index.getName(), IndexMetadata.INDEX_CLOSED_BLOCK);
continue;
}
final ClusterBlock closingBlock = blockedIndices.get(index);
if (currentState.blocks().hasIndexBlock(index.getName(), closingBlock) == false) {
LOGGER.debug("verification of shards before closing {} succeeded but block has been removed in the meantime", index);
continue;
}
Set<Index> restoringIndices = RestoreService.restoringIndices(updatedState, Set.of(index));
if (restoringIndices.isEmpty() == false) {
result.setValue(new AcknowledgedResponse(false));
LOGGER.debug("verification of shards before closing {} succeeded but index is being restored in the meantime", index);
continue;
}
Set<Index> snapshottingIndices = SnapshotsService.snapshottingIndices(updatedState, Set.of(index));
if (snapshottingIndices.isEmpty() == false) {
result.setValue(new AcknowledgedResponse(false));
LOGGER.debug("verification of shards before closing {} succeeded but index is being snapshot in the meantime", index);
continue;
}
blocks.removeIndexBlockWithId(index.getName(), INDEX_CLOSED_BLOCK_ID);
blocks.addIndexBlock(index.getName(), IndexMetadata.INDEX_CLOSED_BLOCK);
final IndexMetadata.Builder updatedMetadata = IndexMetadata.builder(indexMetadata).state(IndexMetadata.State.CLOSE);
if (removeRoutingTable) {
metadata.put(updatedMetadata);
routingTable.remove(index.getName());
} else {
metadata.put(updatedMetadata.settingsVersion(indexMetadata.getSettingsVersion() + 1).settings(Settings.builder().put(indexMetadata.getSettings()).put(IndexMetadata.VERIFIED_BEFORE_CLOSE_SETTING.getKey(), true)));
routingTable.addAsFromOpenToClose(metadata.getSafe(index));
}
LOGGER.debug("closing index {} succeeded", index);
closedIndices.add(index.getName());
} catch (IndexNotFoundException e) {
LOGGER.debug("index {} has been deleted since it was blocked before closing, ignoring", index);
}
}
LOGGER.info("completed closing of indices {}", closedIndices);
return ClusterState.builder(currentState).blocks(blocks).metadata(metadata).routingTable(routingTable.build()).build();
}
Aggregations