Ivan Andika created RATIS-2824:
----------------------------------

             Summary: Fix snapshot race
                 Key: RATIS-2824
                 URL: https://issues.apache.org/jira/browse/RATIS-2824
             Project: Ratis
          Issue Type: Improvement
            Reporter: Ivan Andika
            Assignee: Ivan Andika


Older issue, not introduced here: FollowerInfoImpl.setSnapshotIndex overwrites 
matchIndex and nextIndex unconditionally. The SUCCESS path doesn't take the 
lock, so wrapping the call in the lock doesn't stop a concurrent reply's 
progress from being overwritten with a lower value. The effect is conservative 
(it only re-sends entries), and your new comment ("Successful append replies 
may advance the indexes independently") already acknowledges it. It could be a 
follow-up.

The race is {*}a snapshot update overwriting newer follower progress recorded 
by a successful append reply{*}. It affects the leader’s bookkeeping; it does 
not roll back the follower’s actual log.

setSnapshotIndex performs three separate writes:

{{snapshotIndex = installedIndex;
matchIndex = installedIndex;
nextIndex = installedIndex + 1;}}

The snapshot handler holds the appender lock, but the GrpcLogAppender.java 
updates {{matchIndex}} and {{nextIndex}} without that lock. Consequently, a 
snapshot acknowledgement for index 100 can overlap with an append 
acknowledgement through index 149:
||Step||Snapshot handler||Append reply handler||
|1|Sets {{{}snapshotIndex = 100{}}}, {{matchIndex = 100}}| |
|2| |Advances {{matchIndex}} to 149|
|3| |Advances {{nextIndex}} to 150|
|4|Sets {{nextIndex = 101}}| |

The final state is {{{}matchIndex = 149{}}}, {{{}nextIndex = 101{}}}: the 
leader knows the follower has entries through 149, yet starts sending again 
from 101. Another ordering can overwrite *both* indexes with the older snapshot 
progress.

The expected consequence is unnecessary retransmission and temporarily 
underestimated replication progress. Individual atomic variables prevent torn 
writes, but they do not make these related updates atomic together.

Our added snapshot lock prevents snapshot updates from overlapping {*}request 
construction{*}. It cannot exclude successful replies unless those replies 
participate in the same synchronization. Fixing that remaining race requires 
coordinating both update paths, while considering the cost of locking the 
successful-reply path.



--
This message was sent by Atlassian Jira
(v10.3.26#10030026)

Reply via email to