Package org.elasticsearch.cluster.metadata

Examples of org.elasticsearch.cluster.metadata.IndexMetaData


            }

            // check if the counts meets the minimum set
            int requiredAllocation = 1;
            try {
                IndexMetaData indexMetaData = routingNodes.metaData().index(shard.index());
                String initialShards = indexMetaData.settings().get("recovery.initial_shards", this.initialShards);
                if ("quorum".equals(initialShards)) {
                    if (indexMetaData.numberOfReplicas() > 1) {
                        requiredAllocation = ((1 + indexMetaData.numberOfReplicas()) / 2) + 1;
                    }
                } else if ("quorum-1".equals(initialShards) || "half".equals(initialShards)) {
                    if (indexMetaData.numberOfReplicas() > 2) {
                        requiredAllocation = ((1 + indexMetaData.numberOfReplicas()) / 2);
                    }
                } else if ("full".equals(initialShards)) {
                    requiredAllocation = indexMetaData.numberOfReplicas() + 1;
                } else if ("full-1".equals(initialShards)) {
                    if (indexMetaData.numberOfReplicas() > 1) {
                        requiredAllocation = indexMetaData.numberOfReplicas();
                    }
                } else {
                    requiredAllocation = Integer.parseInt(initialShards);
                }
            } catch (Exception e) {
View Full Code Here


                    final IndexMetaData.Builder indexMetaDataBuilder = newIndexMetaDataBuilder(request.index).settings(actualIndexSettings);
                    for (MappingMetaData mappingMd : mappingsMetaData.values()) {
                        indexMetaDataBuilder.putMapping(mappingMd);
                    }
                    indexMetaDataBuilder.state(request.state);
                    final IndexMetaData indexMetaData = indexMetaDataBuilder.build();

                    MetaData newMetaData = newMetaDataBuilder()
                            .metaData(currentState.metaData())
                            .put(indexMetaData)
                            .build();

                    logger.info("[{}] creating index, cause [{}], shards [{}]/[{}], mappings {}", request.index, request.cause, indexMetaData.numberOfShards(), indexMetaData.numberOfReplicas(), mappings.keySet());

                    ClusterBlocks.Builder blocks = ClusterBlocks.builder().blocks(currentState.blocks());
                    if (!request.blocks.isEmpty()) {
                        for (ClusterBlock block : request.blocks) {
                            blocks.addIndexBlock(request.index, block);
View Full Code Here

        clusterService.submitStateUpdateTask("refresh-mapping [" + index + "][" + Arrays.toString(types) + "]", new ClusterStateUpdateTask() {
            @Override public ClusterState execute(ClusterState currentState) {
                boolean createdIndex = false;
                try {
                    // first, check if it really needs to be updated
                    final IndexMetaData indexMetaData = currentState.metaData().index(index);
                    if (indexMetaData == null) {
                        // index got delete on us, ignore...
                        return currentState;
                    }

                    IndexService indexService = indicesService.indexService(index);
                    if (indexService == null) {
                        // we need to create the index here, and add the current mapping to it, so we can merge
                        indexService = indicesService.createIndex(indexMetaData.index(), indexMetaData.settings(), currentState.nodes().localNode().id());
                        createdIndex = true;
                        for (String type : types) {
                            // only add the current relevant mapping (if exists)
                            if (indexMetaData.mappings().containsKey(type)) {
                                indexService.mapperService().add(type, indexMetaData.mappings().get(type).source().string());
                            }
                        }
                    }
                    IndexMetaData.Builder indexMetaDataBuilder = newIndexMetaDataBuilder(indexMetaData);
                    List<String> updatedTypes = Lists.newArrayList();
                    for (String type : types) {
                        DocumentMapper mapper = indexService.mapperService().documentMapper(type);
                        if (!mapper.mappingSource().equals(indexMetaData.mappings().get(type).source())) {
                            updatedTypes.add(type);
                            indexMetaDataBuilder.putMapping(new MappingMetaData(mapper));
                        }
                    }
View Full Code Here

        clusterService.submitStateUpdateTask("update-mapping [" + index + "][" + type + "]", new ProcessedClusterStateUpdateTask() {
            @Override public ClusterState execute(ClusterState currentState) {
                boolean createdIndex = false;
                try {
                    // first, check if it really needs to be updated
                    final IndexMetaData indexMetaData = currentState.metaData().index(index);
                    if (indexMetaData == null) {
                        // index got delete on us, ignore...
                        return currentState;
                    }
                    if (indexMetaData.mappings().containsKey(type) && indexMetaData.mapping(type).source().equals(mappingSource)) {
                        return currentState;
                    }

                    IndexService indexService = indicesService.indexService(index);
                    if (indexService == null) {
                        // we need to create the index here, and add the current mapping to it, so we can merge
                        indexService = indicesService.createIndex(indexMetaData.index(), indexMetaData.settings(), currentState.nodes().localNode().id());
                        createdIndex = true;
                        // only add the current relevant mapping (if exists)
                        if (indexMetaData.mappings().containsKey(type)) {
                            indexService.mapperService().add(type, indexMetaData.mappings().get(type).source().string());
                        }
                    }
                    MapperService mapperService = indexService.mapperService();

                    DocumentMapper existingMapper = mapperService.documentMapper(type);
                    // parse the updated one
                    DocumentMapper updatedMapper = mapperService.parse(type, mappingSource.string());
                    if (existingMapper == null) {
                        existingMapper = updatedMapper;
                    } else {
                        // merge from the updated into the existing, ignore conflicts (we know we have them, we just want the new ones)
                        existingMapper.merge(updatedMapper, mergeFlags().simulate(false));
                    }

                    // if we end up with the same mapping as the original once, ignore
                    if (indexMetaData.mappings().containsKey(type) && indexMetaData.mapping(type).source().equals(existingMapper.mappingSource())) {
                        return currentState;
                    }

                    // build the updated mapping source
                    if (logger.isDebugEnabled()) {
View Full Code Here

                    // pre create indices here and add mappings to them so we can merge the mappings here if needed
                    for (String index : request.indices) {
                        if (indicesService.hasIndex(index)) {
                            continue;
                        }
                        final IndexMetaData indexMetaData = currentState.metaData().index(index);
                        IndexService indexService = indicesService.createIndex(indexMetaData.index(), indexMetaData.settings(), currentState.nodes().localNode().id());
                        indicesToClose.add(indexMetaData.index());
                        // only add the current relevant mapping (if exists)
                        if (indexMetaData.mappings().containsKey(request.mappingType)) {
                            indexService.mapperService().add(request.mappingType, indexMetaData.mappings().get(request.mappingType).source().string());
                        }
                    }

                    Map<String, DocumentMapper> newMappers = newHashMap();
                    Map<String, DocumentMapper> existingMappers = newHashMap();
                    for (String index : request.indices) {
                        IndexService indexService = indicesService.indexService(index);
                        if (indexService != null) {
                            // try and parse it (no need to add it here) so we can bail early in case of parsing exception
                            DocumentMapper newMapper = indexService.mapperService().parse(request.mappingType, request.mappingSource);
                            newMappers.put(index, newMapper);
                            DocumentMapper existingMapper = indexService.mapperService().documentMapper(request.mappingType);
                            if (existingMapper != null) {
                                // first, simulate
                                DocumentMapper.MergeResult mergeResult = existingMapper.merge(newMapper, mergeFlags().simulate(true));
                                // if we have conflicts, and we are not supposed to ignore them, throw an exception
                                if (!request.ignoreConflicts && mergeResult.hasConflicts()) {
                                    throw new MergeMappingException(mergeResult.conflicts());
                                }
                                existingMappers.put(index, existingMapper);
                            }
                        } else {
                            throw new IndexMissingException(new Index(index));
                        }
                    }

                    String mappingType = request.mappingType;
                    if (mappingType == null) {
                        mappingType = newMappers.values().iterator().next().type();
                    } else if (!mappingType.equals(newMappers.values().iterator().next().type())) {
                        throw new InvalidTypeNameException("Type name provided does not match type name within mapping definition");
                    }
                    if (!MapperService.DEFAULT_MAPPING.equals(mappingType) && mappingType.charAt(0) == '_') {
                        throw new InvalidTypeNameException("Document mapping type name can't start with '_'");
                    }

                    final Map<String, MappingMetaData> mappings = newHashMap();
                    for (Map.Entry<String, DocumentMapper> entry : newMappers.entrySet()) {
                        String index = entry.getKey();
                        // do the actual merge here on the master, and update the mapping source
                        DocumentMapper newMapper = entry.getValue();
                        if (existingMappers.containsKey(entry.getKey())) {
                            // we have an existing mapping, do the merge here (on the master), it will automatically update the mapping source
                            DocumentMapper existingMapper = existingMappers.get(entry.getKey());
                            CompressedString existingSource = existingMapper.mappingSource();

                            existingMapper.merge(newMapper, mergeFlags().simulate(false));

                            CompressedString updatedSource = existingMapper.mappingSource();
                            if (existingSource.equals(updatedSource)) {
                                // same source, no changes, ignore it
                            } else {
                                // use the merged mapping source
                                mappings.put(index, new MappingMetaData(existingMapper));
                                if (logger.isDebugEnabled()) {
                                    logger.debug("[{}] update_mapping [{}] with source [{}]", index, existingMapper.type(), updatedSource);
                                } else if (logger.isInfoEnabled()) {
                                    logger.info("[{}] update_mapping [{}]", index, existingMapper.type());
                                }
                            }
                        } else {
                            CompressedString newSource = newMapper.mappingSource();
                            mappings.put(index, new MappingMetaData(newMapper));
                            if (logger.isDebugEnabled()) {
                                logger.debug("[{}] create_mapping [{}] with source [{}]", index, newMapper.type(), newSource);
                            } else if (logger.isInfoEnabled()) {
                                logger.info("[{}] create_mapping [{}]", index, newMapper.type());
                            }
                        }
                    }

                    if (mappings.isEmpty()) {
                        // no changes, return
                        listener.onResponse(new Response(true));
                        return currentState;
                    }

                    MetaData.Builder builder = newMetaDataBuilder().metaData(currentState.metaData());
                    for (String indexName : request.indices) {
                        IndexMetaData indexMetaData = currentState.metaData().index(indexName);
                        if (indexMetaData == null) {
                            throw new IndexMissingException(new Index(indexName));
                        }
                        MappingMetaData mappingMd = mappings.get(indexName);
                        if (mappingMd != null) {
View Full Code Here

                Map<String, IndexService> indices = Maps.newHashMap();
                try {
                    boolean changed = false;
                    MetaData.Builder builder = newMetaDataBuilder().metaData(currentState.metaData());
                    for (AliasAction aliasAction : request.actions) {
                        IndexMetaData indexMetaData = builder.get(aliasAction.index());
                        if (indexMetaData == null) {
                            throw new IndexMissingException(new Index(aliasAction.index()));
                        }
                        IndexMetaData.Builder indexMetaDataBuilder = newIndexMetaDataBuilder(indexMetaData);
                        if (aliasAction.actionType() == AliasAction.Type.ADD) {
                            String filter = aliasAction.filter();
                            if (Strings.hasLength(filter)) {
                                // parse the filter, in order to validate it
                                IndexService indexService = indices.get(indexMetaData.index());
                                if (indexService == null) {
                                    indexService = indicesService.indexService(indexMetaData.index());
                                    if (indexService == null) {
                                        // temporarily create the index so we have can parse the filter
                                        indexService = indicesService.createIndex(indexMetaData.index(), indexMetaData.settings(), currentState.nodes().localNode().id());
                                        indicesToClose.add(indexMetaData.index());
                                    }
                                    indices.put(indexMetaData.index(), indexService);
                                }

                                // now, parse the filter
                                IndexQueryParserService indexQueryParser = indexService.queryParserService();
                                try {
                                    XContentParser parser = XContentFactory.xContent(filter).createParser(filter);
                                    try {
                                        indexQueryParser.parseInnerFilter(parser);
                                    } finally {
                                        parser.close();
                                    }
                                } catch (Exception e) {
                                    listener.onFailure(new ElasticSearchIllegalArgumentException("failed to parse filter for [" + aliasAction.alias() + "]", e));
                                    return currentState;
                                }
                            }
                            AliasMetaData newAliasMd = AliasMetaData.newAliasMetaDataBuilder(
                                    aliasAction.alias())
                                    .filter(filter)
                                    .indexRouting(aliasAction.indexRouting())
                                    .searchRouting(aliasAction.searchRouting())
                                    .build();
                            // Check if this alias already exists
                            AliasMetaData aliasMd = indexMetaData.aliases().get(aliasAction.alias());
                            if (aliasMd != null && aliasMd.equals(newAliasMd)) {
                                // It's the same alias - ignore it
                                continue;
                            }
                            indexMetaDataBuilder.putAlias(newAliasMd);
                        } else if (aliasAction.actionType() == AliasAction.Type.REMOVE) {
                            if (!indexMetaData.aliases().containsKey(aliasAction.alias())) {
                                // This alias doesn't exist - ignore
                                continue;
                            }
                            indexMetaDataBuilder.removerAlias(aliasAction.alias());
                        }
View Full Code Here

                }

                RiversRouting.Builder routingBuilder = RiversRouting.builder().routing(currentState.routing());
                boolean dirty = false;

                IndexMetaData indexMetaData = event.state().metaData().index(riverIndexName);
                // go over and create new river routing (with no node) for new types (rivers names)
                for (MappingMetaData mappingMd : indexMetaData.mappings().values()) {
                    String mappingType = mappingMd.type(); // mapping type is the name of the river
                    if (!currentState.routing().hasRiverByName(mappingType)) {
                        // no river, we need to add it to the routing with no node allocation
                        try {
                            client.admin().indices().prepareRefresh(riverIndexName).execute().actionGet();
                            GetResponse getResponse = client.prepareGet(riverIndexName, mappingType, "_meta").execute().actionGet();
                            if (getResponse.exists()) {
                                String riverType = XContentMapValues.nodeStringValue(getResponse.sourceAsMap().get("type"), null);
                                if (riverType == null) {
                                    logger.warn("no river type provided for [{}], ignoring...", riverIndexName);
                                } else {
                                    routingBuilder.put(new RiverRouting(new RiverName(riverType, mappingType), null));
                                    dirty = true;
                                }
                            }
                        } catch (NoShardAvailableActionException e) {
                            // ignore, we will get it next time...
                        } catch (ClusterBlockException e) {
                            // ignore, we will get it next time
                        } catch (IndexMissingException e) {
                            // ignore, we will get it next time
                        } catch (Exception e) {
                            logger.warn("failed to get/parse _meta for [{}]", e, mappingType);
                        }
                    }
                }
                // now, remove routings that were deleted
                // also, apply nodes that were removed and rivers were running on
                for (RiverRouting routing : currentState.routing()) {
                    if (!indexMetaData.mappings().containsKey(routing.riverName().name())) {
                        routingBuilder.remove(routing);
                        dirty = true;
                    } else if (routing.node() != null && !event.state().nodes().nodeExists(routing.node().id())) {
                        routingBuilder.remove(routing);
                        routingBuilder.put(new RiverRouting(routing.riverName(), null));
View Full Code Here

        // if not, then use it as the index
        return indexShard.shardsIt(DjbHashFunction.DJB_HASH(preference));
    }

    public IndexMetaData indexMetaData(ClusterState clusterState, String index) {
        IndexMetaData indexMetaData = clusterState.metaData().index(index);
        if (indexMetaData == null) {
            throw new IndexMissingException(new Index(index));
        }
        return indexMetaData;
    }
View Full Code Here

        for (final String index : request.indices()) {
            deleteIndexService.deleteIndex(new MetaDataDeleteIndexService.Request(index).timeout(request.timeout()), new MetaDataDeleteIndexService.Listener() {
                @Override public void onResponse(MetaDataDeleteIndexService.Response response) {
                    responseRef.set(new DeleteIndexResponse(response.acknowledged()));
                    // YACK, but here we go: If this index is also percolated, make sure to delete all percolated queries from the _percolator index
                    IndexMetaData percolatorMetaData = state.metaData().index(PercolatorService.INDEX_NAME);
                    if (percolatorMetaData != null && percolatorMetaData.mappings().containsKey(index)) {
                        deleteMappingAction.execute(new DeleteMappingRequest(PercolatorService.INDEX_NAME).type(index), new ActionListener<DeleteMappingResponse>() {
                            @Override public void onResponse(DeleteMappingResponse deleteMappingResponse) {
                                latch.countDown();
                            }
View Full Code Here

            }

            if (request.filteredIndices().length > 0) {
                String[] indices = currentState.metaData().concreteIndicesIgnoreMissing(request.filteredIndices());
                for (String filteredIndex : indices) {
                    IndexMetaData indexMetaData = currentState.metaData().index(filteredIndex);
                    if (indexMetaData != null) {
                        mdBuilder.put(indexMetaData);
                    }
                }
            }
View Full Code Here

TOP

Related Classes of org.elasticsearch.cluster.metadata.IndexMetaData

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.