ES5.6.4源码分析----索引的创建过程

入口

TransportCreateIndexAction#masterOperation

解析请求中的索引名称

final String indexName = indexNameExpressionResolver.resolveDateMathExpression(request.index());

由于创建索引请求中的索引名称可能是日期数学表达式表示的,所以要将其解析成具体的名称。

规范化+校验请求中的setting参数

updatedSettingsBuilder.put(request.settings()).normalizePrefix(IndexMetaData.INDEX_SETTING_PREFIX);
        indexScopedSettings.validate(updatedSettingsBuilder);
        request.settings(updatedSettingsBuilder.build());

请求体例子:
{
“settings”:{
“number_of_shards”:3,
“number_of_replicas”:0
}
}

  1. 检查settings中的参数是否以”index.”开头,如果不是将这个前缀加上 number_of_shards => index.number_of_shards
  2. 检查ES是否支持这些配置

将请求封装成任务提交

clusterService.submitStateUpdateTask("create-index [" + request.index() + "], cause [" + request.cause() + "]",
                new AckedClusterStateUpdateTask<ClusterStateUpdateResponse>(Priority.URGENT, request,
                    wrapPreservingContext(listener, threadPool.getThreadContext())) {

                    @Override
                    protected ClusterStateUpdateResponse newResponse(boolean acknowledged) {
                        return new ClusterStateUpdateResponse(acknowledged);
                    }

                    @Override
                    public ClusterState execute(ClusterState currentState) throws Exception {
                        Index createdIndex = null;
                        String removalExtraInfo = null;
                        IndexRemovalReason removalReason = IndexRemovalReason.FAILURE;
                        try {
                            validate(request, currentState);

                            for (Alias alias : request.aliases()) {
                                aliasValidator.validateAlias(alias, request.index(), currentState.metaData());
                            }

                            // we only find a template when its an API call (a new index)
                            // find templates, highest order are better matching
                            List<IndexTemplateMetaData> templates = findTemplates(request, currentState);

                            Map<String, Custom> customs = new HashMap<>();

                            // add the request mapping
                            Map<String, Map<String, Object>> mappings = new HashMap<>();

                            Map<String, AliasMetaData> templatesAliases = new HashMap<>();

                            List<String> templateNames = new ArrayList<>();

                            for (Map.Entry<String, String> entry : request.mappings().entrySet()) {
                                mappings.put(entry.getKey(), MapperService.parseMapping(xContentRegistry, entry.getValue()));
                            }

                            for (Map.Entry<String, Custom> entry : request.customs().entrySet()) {
                                customs.put(entry.getKey(), entry.getValue());
                            }

                            final Index shrinkFromIndex = request.shrinkFrom();

                            if (shrinkFromIndex == null) {
                                // apply templates, merging the mappings into the request mapping if exists
                                for (IndexTemplateMetaData template : templates) {
                                    templateNames.add(template.getName());
                                    for (ObjectObjectCursor<String, CompressedXContent> cursor : template.mappings()) {
                                        String mappingString = cursor.value.string();
                                        if (mappings.containsKey(cursor.key)) {
                                            XContentHelper.mergeDefaults(mappings.get(cursor.key),
                                                MapperService.parseMapping(xContentRegistry, mappingString));
                                        } else {
                                            mappings.put(cursor.key,
                                                MapperService.parseMapping(xContentRegistry, mappingString));
                                        }
                                    }
                                    // handle custom
                                    for (ObjectObjectCursor<String, Custom> cursor : template.customs()) {
                                        String type = cursor.key;
                                        IndexMetaData.Custom custom = cursor.value;
                                        IndexMetaData.Custom existing = customs.get(type);
                                        if (existing == null) {
                                            customs.put(type, custom);
                                        } else {
                                            IndexMetaData.Custom merged = existing.mergeWith(custom);
                                            customs.put(type, merged);
                                        }
                                    }
                                    //handle aliases
                                    for (ObjectObjectCursor<String, AliasMetaData> cursor : template.aliases()) {
                                        AliasMetaData aliasMetaData = cursor.value;
                                        //if an alias with same name came with the create index request itself,
                                        // ignore this one taken from the index template
                                        if (request.aliases().contains(new Alias(aliasMetaData.alias()))) {
                                            continue;
                                        }
                                        //if an alias with same name was already processed, ignore this one
                                        if (templatesAliases.containsKey(cursor.key)) {
                                            continue;
                                        }

                                        //Allow templatesAliases to be templated by replacing a token with the name of the index that we are applying it to
                                        if (aliasMetaData.alias().contains("{index}")) {
                                            String templatedAlias = aliasMetaData.alias().replace("{index}", request.index());
                                            aliasMetaData = AliasMetaData.newAliasMetaData(aliasMetaData, templatedAlias);
                                        }

                                        aliasValidator.validateAliasMetaData(aliasMetaData, request.index(), currentState.metaData());
                                        templatesAliases.put(aliasMetaData.alias(), aliasMetaData);
                                    }
                                }
                            }
                            Settings.Builder indexSettingsBuilder = Settings.builder();
                            if (shrinkFromIndex == null) {
                                // apply templates, here, in reverse order, since first ones are better matching
                                for (int i = templates.size() - 1; i >= 0; i--) {
                                    indexSettingsBuilder.put(templates.get(i).settings());
                                }
                            }
                            // now, put the request settings, so they override templates
                            indexSettingsBuilder.put(request.settings());
                            if (indexSettingsBuilder.get(SETTING_NUMBER_OF_SHARDS) == null) {
                                indexSettingsBuilder.put(SETTING_NUMBER_OF_SHARDS, settings.getAsInt(SETTING_NUMBER_OF_SHARDS, 5));
                            }
                            if (indexSettingsBuilder.get(SETTING_NUMBER_OF_REPLICAS) == null) {
                                indexSettingsBuilder.put(SETTING_NUMBER_OF_REPLICAS, settings.getAsInt(SETTING_NUMBER_OF_REPLICAS, 1));
                            }
                            if (settings.get(SETTING_AUTO_EXPAND_REPLICAS) != null && indexSettingsBuilder.get(SETTING_AUTO_EXPAND_REPLICAS) == null) {
                                indexSettingsBuilder.put(SETTING_AUTO_EXPAND_REPLICAS, settings.get(SETTING_AUTO_EXPAND_REPLICAS));
                            }

                            if (indexSettingsBuilder.get(SETTING_VERSION_CREATED) == null) {
                                DiscoveryNodes nodes = currentState.nodes();
                                final Version createdVersion = Version.min(Version.CURRENT, nodes.getSmallestNonClientNodeVersion());
                                indexSettingsBuilder.put(SETTING_VERSION_CREATED, createdVersion);
                            }

                            if (indexSettingsBuilder.get(SETTING_CREATION_DATE) == null) {
                                indexSettingsBuilder.put(SETTING_CREATION_DATE, new DateTime(DateTimeZone.UTC).getMillis());
                            }
                            indexSettingsBuilder.put(IndexMetaData.SETTING_INDEX_PROVIDED_NAME, request.getProvidedName());
                            indexSettingsBuilder.put(SETTING_INDEX_UUID, UUIDs.randomBase64UUID());
                            final IndexMetaData.Builder tmpImdBuilder = IndexMetaData.builder(request.index());

                            final int routingNumShards;
                            if (shrinkFromIndex == null) {
                                routingNumShards = IndexMetaData.INDEX_NUMBER_OF_SHARDS_SETTING.get(indexSettingsBuilder.build());
                            } else {
                                final IndexMetaData sourceMetaData = currentState.metaData().getIndexSafe(shrinkFromIndex);
                                routingNumShards = sourceMetaData.getRoutingNumShards();
                            }
                            tmpImdBuilder.setRoutingNumShards(routingNumShards);

                            if (shrinkFromIndex != null) {
                                prepareShrinkIndexSettings(
                                        currentState, mappings.keySet(), indexSettingsBuilder, shrinkFromIndex, request.index());
                            }
                            final Settings actualIndexSettings = indexSettingsBuilder.build();
                            tmpImdBuilder.settings(actualIndexSettings);

                            // Set up everything, now locally create the index to see that things are ok, and apply
                            final IndexMetaData tmpImd = tmpImdBuilder.build();
                            ActiveShardCount waitForActiveShards = request.waitForActiveShards();
                            if (waitForActiveShards == ActiveShardCount.DEFAULT) {
                                waitForActiveShards = tmpImd.getWaitForActiveShards();
                            }
                            if (waitForActiveShards.validate(tmpImd.getNumberOfReplicas()) == false) {
                                throw new IllegalArgumentException("invalid wait_for_active_shards[" + request.waitForActiveShards() +
                                                                   "]: cannot be greater than number of shard copies [" +
                                                                   (tmpImd.getNumberOfReplicas() + 1) + "]");
                            }
                            // create the index here (on the master) to validate it can be created, as well as adding the mapping
                            final IndexService indexService = indicesService.createIndex(tmpImd, Collections.emptyList());
                            createdIndex = indexService.index();
                            // now add the mappings
                            MapperService mapperService = indexService.mapperService();
                            try {
                                mapperService.merge(mappings, MergeReason.MAPPING_UPDATE, request.updateAllTypes());
                            } catch (Exception e) {
                                removalExtraInfo = "failed on parsing default mapping/mappings on index creation";
                                throw e;
                            }

                            // the context is only used for validation so it's fine to pass fake values for the shard id and the current
                            // timestamp
                            final QueryShardContext queryShardContext = indexService.newQueryShardContext(0, null, () -> 0L);
                            for (Alias alias : request.aliases()) {
                                if (Strings.hasLength(alias.filter())) {
                                    aliasValidator.validateAliasFilter(alias.name(), alias.filter(), queryShardContext, xContentRegistry);
                                }
                            }
                            for (AliasMetaData aliasMetaData : templatesAliases.values()) {
                                if (aliasMetaData.filter() != null) {
                                    aliasValidator.validateAliasFilter(aliasMetaData.alias(), aliasMetaData.filter().uncompressed(),
                                            queryShardContext, xContentRegistry);
                                }
                            }

                            // now, update the mappings with the actual source
                            Map<String, MappingMetaData> mappingsMetaData = new HashMap<>();
                            for (DocumentMapper mapper : mapperService.docMappers(true)) {
                                MappingMetaData mappingMd = new MappingMetaData(mapper);
                                mappingsMetaData.put(mapper.type(), mappingMd);
                            }

                            final IndexMetaData.Builder indexMetaDataBuilder = IndexMetaData.builder(request.index())
                                .settings(actualIndexSettings)
                                .setRoutingNumShards(routingNumShards);

                            for (int shardId = 0; shardId < tmpImd.getNumberOfShards(); shardId++) {
                                indexMetaDataBuilder.primaryTerm(shardId, tmpImd.primaryTerm(shardId));
                            }

                            for (MappingMetaData mappingMd : mappingsMetaData.values()) {
                                indexMetaDataBuilder.putMapping(mappingMd);
                            }

                            for (AliasMetaData aliasMetaData : templatesAliases.values()) {
                                indexMetaDataBuilder.putAlias(aliasMetaData);
                            }
                            for (Alias alias : request.aliases()) {
                                AliasMetaData aliasMetaData = AliasMetaData.builder(alias.name()).filter(alias.filter())
                                        .indexRouting(alias.indexRouting()).searchRouting(alias.searchRouting()).build();
                                indexMetaDataBuilder.putAlias(aliasMetaData);
                            }

                            for (Map.Entry<String, Custom> customEntry : customs.entrySet()) {
                                indexMetaDataBuilder.putCustom(customEntry.getKey(), customEntry.getValue());
                            }

                            indexMetaDataBuilder.state(request.state());

                            final IndexMetaData indexMetaData;
                            try {
                                indexMetaData = indexMetaDataBuilder.build();
                            } catch (Exception e) {
                                removalExtraInfo = "failed to build index metadata";
                                throw e;
                            }

                            indexService.getIndexEventListener().beforeIndexAddedToCluster(indexMetaData.getIndex(),
                                    indexMetaData.getSettings());

                            MetaData newMetaData = MetaData.builder(currentState.metaData())
                                    .put(indexMetaData, false)
                                    .build();

                            String maybeShadowIndicator = IndexMetaData.isIndexUsingShadowReplicas(indexMetaData.getSettings()) ? "s" : "";
                            logger.info("[{}] creating index, cause [{}], templates {}, shards [{}]/[{}{}], mappings {}",
                                    request.index(), request.cause(), templateNames, indexMetaData.getNumberOfShards(),
                                    indexMetaData.getNumberOfReplicas(), maybeShadowIndicator, mappings.keySet());

                            ClusterBlocks.Builder blocks = ClusterBlocks.builder().blocks(currentState.blocks());
                            if (!request.blocks().isEmpty()) {
                                for (ClusterBlock block : request.blocks()) {
                                    blocks.addIndexBlock(request.index(), block);
                                }
                            }
                            blocks.updateBlocks(indexMetaData);

                            ClusterState updatedState = ClusterState.builder(currentState).blocks(blocks).metaData(newMetaData).build();

                            if (request.state() == State.OPEN) {
                                RoutingTable.Builder routingTableBuilder = RoutingTable.builder(updatedState.routingTable())
                                        .addAsNew(updatedState.metaData().index(request.index()));
                                updatedState = allocationService.reroute(
                                        ClusterState.builder(updatedState).routingTable(routingTableBuilder.build()).build(),
                                        "index [" + request.index() + "] created");
                            }
                            removalExtraInfo = "cleaning up after validating index on master";
                            removalReason = IndexRemovalReason.NO_LONGER_ASSIGNED;
                            return updatedState;
                        } finally {
                            if (createdIndex != null) {
                                // Index was already partially created - need to clean up
                                indicesService.removeIndex(createdIndex, removalReason, removalExtraInfo);
                            }
                        }
                    }

                    @Override
                    public void onFailure(String source, Exception e) {
                        if (e instanceof ResourceAlreadyExistsException) {
                            logger.trace((Supplier<?>) () -> new ParameterizedMessage("[{}] failed to create", request.index()), e);
                        } else {
                            logger.debug((Supplier<?>) () -> new ParameterizedMessage("[{}] failed to create", request.index()), e);
                        }
                        super.onFailure(source, e);
                    }
                });

这个任务的执行内容在public ClusterState execute(ClusterState currentState) throws Exception中。其内容如下:

索引名称校验

  • 名称不能包含#
  • 不能以_、-、+开头
  • 字节长度不大于255
  • 不能是.或…
  • 要求小写
  • 是否已经存在
  • 不能存在同名的别名

装载索引的setting、mapping配置

将主分片ID加载到索引元数据中

for (int shardId = 0; shardId < tmpImd.getNumberOfShards(); shardId++) {
                                indexMetaDataBuilder.primaryTerm(shardId, tmpImd.primaryTerm(shardId));
                            }

将mapping信息也加载到索引元数据中

for (MappingMetaData mappingMd : mappingsMetaData.values()) {
                                indexMetaDataBuilder.putMapping(mappingMd);
                            }

将别名信息加载到元数据中

for (AliasMetaData aliasMetaData : templatesAliases.values()) {
                                indexMetaDataBuilder.putAlias(aliasMetaData);
                            }
                            for (Alias alias : request.aliases()) {
                                AliasMetaData aliasMetaData = AliasMetaData.builder(alias.name()).filter(alias.filter())
                                        .indexRouting(alias.indexRouting()).searchRouting(alias.searchRouting()).build();
                                indexMetaDataBuilder.putAlias(aliasMetaData);
                            }

将所有元数据信息更新到集群元数据信息中

MetaData newMetaData = MetaData.builder(currentState.metaData())
                                    .put(indexMetaData, false)
                                    .build();

为新分片分配节点

protected ClusterState reroute(final ClusterState clusterState, String reason, boolean debug) {
        RoutingNodes routingNodes = getMutableRoutingNodes(clusterState);
        // shuffle the unassigned nodes, just so we won't have things like poison failed shards
        routingNodes.unassigned().shuffle();
        RoutingAllocation allocation = new RoutingAllocation(allocationDeciders, routingNodes, clusterState,
            clusterInfoService.getClusterInfo(), currentNanoTime(), false);
        allocation.debugDecision(debug);
        reroute(allocation);
        if (allocation.routingNodesChanged() == false) {
            return clusterState;
        }
        return buildResultAndLogHealthChange(clusterState, allocation, reason);
}

分配算法:

weight=0.45*(当前节点拥有的分片数+1-平均每个节点的分片数(包括未分配的分片数))+0.55*(当前节点下该索引拥有的分片数+1-平均每个节点下该索引的分片数(包括未分配的分片数))

取weight最小的节点作为该分片分配的节点

将元数据的变化生效

前一步仅仅是在元数据中增加新索引的信息,而这些信息只保留在内存中,并没有在磁盘中创建对应的数据。这一步比较重要的就是分片在磁盘的分配,请参考ES5.6.4源码解析----分片在磁盘间的分配策略

猜你喜欢

转载自blog.csdn.net/qqqq0199181/article/details/82871854