ivandika3 opened a new pull request, #1622:
URL: https://github.com/apache/ratis/pull/1622

   ## What changes were proposed in this pull request?
   
   Reported in a Specula run 
https://github.com/specula-org/specula-case-studies/blob/main/systems/ratis/overview.md
  ("Metadata persistence failure can leave an accepted higher term non-durable 
across restart.").
   
   The issue happens if `ServerState#persistMetadata` failed in 
`changeToFollowerAndPersistMetadata` that can cause stale Raft metadata.
   
   Background: `changeToFollowerAndPersistMetadata` passes `metadataUpdated` 
which is used so that we only call `ServerState#persistMetadata` if necessary. 
In `RaftServerImpl#changeToFollower` `metadataUpdated` is only set if there is 
an increase of term (i.e. `ServerState#updateCurrentTerm`). 
   
   Issue: If the `RaftServerImpl#changeToFollower` succeeded and set the 
`metadataUpdated` to true, but then the subsequent `persistMetadata` failed, 
there will be a mismatch of the persisted Raft metadata and the in-memory Raft 
metadata. When RaftSever is restarted, the Raft metadata is stale and it can 
load an earlier term as the `currentTerm` in `ServerState#initialize` instead.
   
   Solution
   - The easiest solution is to remove the `metadataUpdated.get()` condition 
and let `ServerState#persistMetadata` is called for every 
`changeToFollowerAndPersistMetadata`. This should not affect correctness since 
the implementation of `RaftStorageMetadataFileImpl#persist`  only updates the 
metadata if there is a change (e.g. `Object.equals`). However, calling 
`persistMetadata` can have overheads like creating unnecessary 
`RaftStorageMetadata` object, CAS operation, and memory contention since 
multiple threads can access the `AtomicReference`. Since 
`changeToFollowerAndPersistMetadata` is in the critical path, we should 
minimize any overhead in normal path (i.e. within the same term).
   - The final solution is that we will introduce a local flag 
`metadataPersistencePending` in `RaftServerImpl`. If `persistMetadata` failed 
in the middle, the `metadataPersistencePending` remains the same. In subsequent 
`changeToFollowerAndPersistMetadata` will detect that 
`metadataPersistencePending` is called and will cooperatively persist the 
metadata logic. This helping pattern is similar to the ones in wait-free 
programming. Within the same term, we only need to check the 
`metadataPersistencePending`.
   
   Generated-by: GPT 6.1 Sol. (Description is written manually).
   
   ## What is the link to the Apache JIRA
   
   https://issues.apache.org/jira/browse/RATIS-2645
   
   ## How was this patch tested?
   
   UT.


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to