Tao Jiuming created RATIS-2795:
----------------------------------

             Summary: Stable reads not gated when snapshots installing
                 Key: RATIS-2795
                 URL: https://issues.apache.org/jira/browse/RATIS-2795
             Project: Ratis
          Issue Type: Bug
          Components: server
    Affects Versions: 3.3.1
            Reporter: Tao Jiuming


Stale reads are not gated during snapshot installation and can query a 
paused/reinitialized state machine

h3. Problem

Of the three read paths, only stale reads are not gated while a snapshot 
installation is in progress:

* ReadIndex (linearizable) and snapshot creation are both gated 
(ReadRequestHandler#getReadExceptionIfInstallingSnapshot / RaftServerImpl 
snapshot creation).
* staleReadAsync (ReadRequestHandler.java:83, as of master @ babaf87d5) only 
checks commitIndex >= minIndex and then calls stateMachine.queryStale(...) 
directly.

stale reads are by definition served by a non-leader server chosen by the client
(io().sendStaleRead(message, minIndex, serverId)) - and 
InstallSnapshotRequestProto is a
leader-to-follower protocol. So this is the one path that executes on the very 
node whose
state machine is being paused and reinitialized by the installation.

h3. Impact

* A state machine holding native resources that closes its storage in 
reinitialize() can crash
  the server (queryStale running concurrently with the close; use-after-free 
class).
* lastApplied typically moves backward to the installed snapshot index during 
installation, so
  queryStale(minIndex) blocks for the entire log replay.
* Pure-Java state machines surface an arbitrary error instead of the retriable 
error the gated
  paths return.

h3. Reproduction

TestStaleReadDuringSnapshotInstallation (in the patch): 3-node 
MiniRaftClusterWithSimulatedRpc;
set the follower's in-progress install index directly (package-private 
@VisibleForTesting hook,
no real installation driven); sendStaleRead to that follower fails with
"...Failed to stale read readIndex as snapshot (1) installation is in 
progress"; clear the index
and the same read succeeds. Note the simulation itself is harmless - the crash 
class requires a
state machine holding native resources.

h3. Proposed fix

Mirror the existing gate in staleReadAsync (+ package-private 
@VisibleForTesting test hook +
TestStaleReadDuringSnapshotInstallation), 13 lines total.

h3. Open question

ElectionRequestHandler/LeaderElection have no install guard either: a node with 
an installation
in progress can still campaign and potentially become leader, after which the 
leader-only
readAsync path would query a paused state machine as well. Should leadership 
readiness be gated
during installation, or is there an existing guarantee I am missing?

h3. Related

RATIS-207 introduced stale reads; this gap is a follow-up on that feature.



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

Reply via email to