# This is a regression test for https://github.com/etcd-io/raft/pull/31.

# Turn off output during the setup of the test.
log-level none
----
ok

# Start with 3 nodes, with a limited in-flight capacity.
add-nodes 3 voters=(1,2,3) index=10 inflight=2
----
ok

campaign 1
----
ok

stabilize
----
ok

# Propose 3 entries.
propose 1 prop_1_12
----
ok

propose 1 prop_1_13
----
ok

propose 1 prop_1_14
----
ok

stabilize
----
ok

# Re-enable log messages.
log-level debug
----
ok

# All nodes up-to-date.
status 1
----
1: StateReplicate match=14 next=15 sentCommit=11 matchCommit=11
2: StateReplicate match=14 next=15 sentCommit=14 matchCommit=14
3: StateReplicate match=14 next=15 sentCommit=14 matchCommit=14

log-level none
----
ok

propose 1 prop_1_15
----
ok

propose 1 prop_1_16
----
ok

propose 1 prop_1_17
----
ok

propose 1 prop_1_18
----
ok

# Commit entries on nodes 1 and 2.
stabilize 1 2
----
ok

log-level debug
----
ok

# Nodes 1 and 2 up-to-date, 3 is behind and MsgApp flow is throttled.
status 1
----
1: StateReplicate match=18 next=19 sentCommit=14 matchCommit=14
2: StateReplicate match=18 next=19 sentCommit=18 matchCommit=18
3: StateReplicate match=14 next=17 sentCommit=16 matchCommit=14 paused inflight=2[full]

# Break the MsgApp flow from the leader to node 3.
deliver-msgs drop=3
----
dropped: 1->3 MsgApp Term:1 Log:1/14 Commit:14 Entries:[1/15 EntryNormal "prop_1_15"]
dropped: 1->3 MsgApp Term:1 Log:1/15 Commit:14 Entries:[1/16 EntryNormal "prop_1_16"]
dropped: 1->3 MsgApp Term:1 Log:1/16 Commit:15
dropped: 1->3 MsgApp Term:1 Log:1/16 Commit:16

# Truncate the leader's log beyond node 3 log size.
compact 1 17
----
1/18 EntryNormal "prop_1_18"

# Trigger a heartbeat timeout to allow the leader to detect that it can't send a
# MsgApp to node 3 because the relevant part of the log is already compacted.
# Instead, the leader should send a snapshot.
tick-heartbeat 1
----
DEBUG 1 [firstindex: 18, commit: 18] sent snapshot[index: 18, term: 1] to 3 [StateReplicate match=14 next=17 sentCommit=16 matchCommit=14 inflight=2[full]]
DEBUG 1 paused sending replication messages to 3 [StateSnapshot match=14 next=19 sentCommit=18 matchCommit=14 paused pendingSnap=18]

stabilize
----
> 1 handling Ready
  Ready MustSync=false:
  Messages:
  1->3 MsgSnap Term:1 Log:0/0
    Snapshot: Index:18 Term:1 ConfState:Voters:[1 2 3] VotersOutgoing:[] Learners:[] LearnersNext:[] AutoLeave:false
> 3 receiving messages
  1->3 MsgSnap Term:1 Log:0/0
    Snapshot: Index:18 Term:1 ConfState:Voters:[1 2 3] VotersOutgoing:[] Learners:[] LearnersNext:[] AutoLeave:false
  INFO log [committed=14, applied=14, applying=14, unstable.offset=15, unstable.offsetInProgress=15, len(unstable.Entries)=0] starts to restore snapshot [index: 18, term: 1]
  INFO 3 switched to configuration voters=(1 2 3)
  INFO 3 [commit: 18, lastindex: 18, lastterm: 1] restored snapshot [index: 18, term: 1]
  INFO 3 [commit: 18] restored snapshot [index: 18, term: 1]
> 3 handling Ready
  Ready MustSync=true:
  HardState Term:1 Vote:1 Commit:18 Lead:1 LeadEpoch:1
  Snapshot Index:18 Term:1 ConfState:Voters:[1 2 3] VotersOutgoing:[] Learners:[] LearnersNext:[] AutoLeave:false
  Messages:
  3->1 MsgAppResp Term:1 Log:0/18 Commit:18
> 1 receiving messages
  3->1 MsgAppResp Term:1 Log:0/18 Commit:18
  DEBUG 1 recovered from needing snapshot, resumed sending replication messages to 3 [StateSnapshot match=18 next=19 sentCommit=18 matchCommit=18 paused pendingSnap=18]

# All nodes caught up.
status 1
----
1: StateReplicate match=18 next=19 sentCommit=14 matchCommit=14
2: StateReplicate match=18 next=19 sentCommit=18 matchCommit=18
3: StateReplicate match=18 next=19 sentCommit=18 matchCommit=18
