---
# System prepended metadata

title: Zookeeper - Follower
tags: [Apache Zookeeper]

---

# Initialization

```
quorumPeer
  |-> Log "FOLLOWING"
  |-> makeFollower 
    |-> Binding port and start listen
    |-> Log "Created server with initTime ..."
  |-> follower#followLeader()
    |-> Log "FOLLOWING - LEADER ELECTION TOOK ..."
    |-> find leader
      |-> learner#findLeader()
    |-> connect to leader
    |-> sync with leader
      |-> learner#syncWithLeader()
    |-> 如果有設定observerMasterPort
      |-> Log "Starting ObserverMaster"
    |-> while follower is running loop
      |-> read packet and process packet
```

[sync with leader](#Sync-with-leader-Process)
[process packet](#Process-packet-Process)

## Sync with leader Process

```
Learner#syncWithLeader
  |-> read packet from leader (QuoromPacket)
  |-> confirm sycn mode
    |-> DIFF
      |-> log "Getting a diff from the leader {zxid}"
    |-> SNAP
      |-> log "etting a snapshot from leader {zxid}"
    |-> TRUNC
      |-> log "Truncating log to get in sync with the leader {zxid}"
    |-> 如果type不對
      |-> log "Got unexpected packet from leader: {}, exiting ... "
      |-> Shutdown server
  |-> 進入同步資料的迴圈
    |-> 直到收到QuorumPacket的type是UPTODATE才會跳離迴圈
    |-> UPTODATE時會 Log "Learner received UPTODATE message"
  |-> Learner回ACK packet給Leader
  |-> Learner開始服務
  |-> 處理未commit的資料
```

## Process packet Process

```
Follower#processPacket(qp: QuorumPacket)
  |-> Leader.PING
  |-> Leader.PROPOSAL
  |-> Leader.COMMIT
  |-> Leader.COMMITANDACTIVATE
  |-> Leader.UPTODATE
  |-> Leader.REVALIDATE
  |-> Leader.SYNC
  |-> default
```

# Links

* [Observer](https://hackmd.io/@qNCbOAxuSemRnXp4ZTlCpg/HkgqoT0F3)