elasticsearch5 源码 索引过程



elasticsearch

1、索引入口RestIndexAction方法中prepareRequest,封装IndexRequest,并由NodeClient调用index方法

client.index(indexRequest, new RestStatusToXContentListener<>(channel, r -> r.getLocation(indexRequest.routing())));

2、中间经过一些抽象类中方法调用后来到NodeClient类中的executeLocally方法,经过一系列的判断校验等到TransportIndexAction

3、TransportIndexAction中doExecute方法会有一个判断是否自动创建索引,暂时先考虑不自动创建

        ClusterState state = clusterService.state();
        if (shouldAutoCreate(request, state)) {     //是否自动创建索引

...................................................
        } else {
            innerExecute(task, request, listener);
        }

4、主要操作都在TransportReplicationAction类中,ReroutePhase内部类中的doRun方法判断是否本地执行

5、TransportReplicationAction中AsyncPrimaryAction异步执行索引操作


6、ReplicationOperation执行execute方法,primary.perform(request)主分片索引,

public void execute() throws Exception {
        final String activeShardCountFailure = checkActiveShardCount();
        final ShardRouting primaryRouting = primary.routingEntry();
        final ShardId primaryId = primaryRouting.shardId();
        if (activeShardCountFailure != null) {
            finishAsFailed(new UnavailableShardsException(primaryId,
                "{} Timeout: [{}], request: [{}]", activeShardCountFailure, request.timeout(), request));
            return;
        }

        totalShards.incrementAndGet();
        pendingActions.incrementAndGet();
        primaryResult = primary.perform(request);   //主分片索引
        final ReplicaRequest replicaRequest = primaryResult.replicaRequest();
        assert replicaRequest.primaryTerm() > 0 : "replicaRequest doesn't have a primary term";
        if (logger.isTraceEnabled()) {
            logger.trace("[{}] op [{}] completed on primary for request [{}]", primaryId, opType, request);
        }

        // we have to get a new state after successfully indexing into the primary in order to honour recovery semantics.
        // we have to make sure that every operation indexed into the primary after recovery start will also be replicated
        // to the recovery target. If we use an old cluster state, we may miss a relocation that has started since then.
        ClusterState clusterState = clusterStateSupplier.get();
        final List<ShardRouting> shards = getShards(primaryId, clusterState);
        Set<String> inSyncAllocationIds = getInSyncAllocationIds(primaryId, clusterState);

        markUnavailableShardsAsStale(replicaRequest, inSyncAllocationIds, shards);  //标记不可用副本分片

        performOnReplicas(replicaRequest, shards);   //副本  和主分片相同入库到内存并标记Translog

        successfulShards.incrementAndGet();
        decPendingAndFinishIfNeeded();
    }



7、TransportIndexAction的executeIndexRequestOnPrimary索引操作,prepareIndexOperationOnPrimary索引前准备模板合并等操作并封装Engine.Index,

public static WriteResult<IndexResponse> executeIndexRequestOnPrimary(IndexRequest request, IndexShard indexShard,
            MappingUpdatedAction mappingUpdatedAction) throws Exception {
        Engine.Index operation = prepareIndexOperationOnPrimary(request, indexShard);   //模板合并等操作并封装Engine.Index
        Mapping update = operation.parsedDoc().dynamicMappingsUpdate();
        final ShardId shardId = indexShard.shardId();
        if (update != null) {
            mappingUpdatedAction.updateMappingOnMaster(shardId.getIndex(), request.type(), update);      //master模板修改
            operation = prepareIndexOperationOnPrimary(request, indexShard);
            update = operation.parsedDoc().dynamicMappingsUpdate();
            if (update != null) {
                throw new ReplicationOperation.RetryOnPrimaryException(shardId,
                    "Dynamic mappings are not available on the node that holds the primary yet");
            }
        }
        indexShard.index(operation);     //索引入内存并添加Translog

        // update the version on request so it will happen on the replicas
        final long version = operation.version();
        request.version(version);
        request.versionType(request.versionType().versionTypeForReplicationAndRecovery());

        assert request.versionType().validateVersionForWrites(request.version());

        IndexResponse response = new IndexResponse(shardId, request.type(), request.id(), request.version(), operation.isCreated());
        return new WriteResult<>(response, operation.getTranslogLocation());
    }



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值