【ETCD】【源码阅读】深入解析 EtcdServer.applyEntryNormal方法

applyEntryNormal 方法的主要作用是将类型为 EntryNormal 的 Raft 日志条目应用到 EtcdServer 中。这个方法处理了多个复杂的业务逻辑,包括一致性索引更新、不同版本请求的处理、资源空间检查等。

一、方法完整代码

// applyEntryNormal apples an EntryNormal type raftpb request to the EtcdServer
func (s *EtcdServer) applyEntryNormal(e *raftpb.Entry) {
   
	shouldApplyV3 := membership.ApplyV2storeOnly
	var ar *applyResult
	index := s.consistIndex.ConsistentIndex()
	if e.Index > index {
   
		// set the consistent index of current executing entry
		s.consistIndex.SetConsistentApplyingIndex(e.Index, e.Term)
		shouldApplyV3 = membership.ApplyBoth
		defer func() {
   
			// The txPostLockInsideApplyHook will not get called in some cases,
			// in which we should move the consistent index forward directly.
			newIndex := s.consistIndex.ConsistentIndex()
			if newIndex < e.Index {
   
				s.consistIndex.SetConsistentIndex(e.Index, e.Term)
			}
		}()
	}
	s.lg.Debug("apply entry normal",
		zap.Uint64("consistent-index", index),
		zap.Uint64("entry-index", e.Index),
		zap.Bool("should-applyV3", bool(shouldApplyV3)))

	// raft state machine may generate noop entry when leader confirmation.
	// skip it in advance to avoid some potential bug in the future
	if len(e.Data) == 0 {
   
		s.notifyAboutFirstCommitInTerm()

		// promote lessor when the local member is leader and finished
		// applying all entries from the last term.
		if s.isLeader() {
   
			s.lessor.Promote(s.Cfg.ElectionTimeout())
		}
		return
	}

	var raftReq pb.InternalRaftRequest
	if !pbutil.MaybeUnmarshal(&raftReq, e.Data) {
    // backward compatible
		var r pb.Request
		rp := &r
		pbutil.MustUnmarshal(rp, e.Data)
		s.lg.Debug(
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值