openraft 0.10.2

Advanced Raft consensus
Documentation
# Snapshot-Based Replication

Snapshot replication can be considered a special type of log replication: it
replicates all **committed** logs from index 0 to a specified index, similar to
the append-entry process, it removes conflicting logs, before installing a
snapshot:

- (1) If the snapshot's logs match the logs already stored on a
  Follower/Learner, no action is taken before installing snapshot.

- (2) If the snapshot's logs conflict with the local logs, **ALL** non-committed
  logs will be removed before installing snapshot. Additionally, the
  membership roll must be rolled back to a previous, non-conflicting state.

After truncating conflicting logs, the snapshot is installed.
The final step is to purge logs the snapshot contains. 

## 1. Snapshot-Log conditions

There are four cases when a snapshot is about to install on a Follower/Learner:

1) Snapshot before `last_purged` log index:

This won't happen, because the snapshot last log id is smaller than the
committed log id: `snapshot.last_log_id < last_purged <= committed_log_id`

```text
     snapshot ----.
                  v
-----------------------llllllllll--->
```


2) Snapshot last log id matches the local last log id:

No action needed before installing snapshot.

```text
     snapshot ----.
                  v
--------------llllllllll------------>
```


3) Snapshot last log id conflicts the local last log id:

Delete all non-committed logs then install snapshot.

```text
     snapshot ----.
                  v
--------------lllxxxxxxx------------>
```


4) Snapshot last log id is after local last log id:

Delete all non-committed logs then install snapshot.
Because any log before snapshot.last_log_id are committed (store on a quorum),
thus are safe to delete.

```text
     snapshot ----.
                  v
----lllllllllll--------------------->
```

To summarize the above four cases:
- If snapshot last log id matches: nothing to do.
- If snapshot last log id does not match (conflict or no such log): delete all
  non-committed logs.

### Rationale of Conflicting Logs Handling

If the local log is in conflict with [`snapshot_meta.last_log_id`], it must
delete all **non-committed** logs, not only the logs after
[`snapshot_meta.last_log_id`].

If not to delete all non-committed logs, the following situation may occur:
Although the node with conflicting logs won't become a leader at once,
but it could become a leader when it receives more logs and stores them after
[`snapshot_meta.last_log_id`].

Because the logs (which might be conflicting) before
[`snapshot_meta.last_log_id`] is not deleted, there's a chance that this node
becomes the leader and uses these logs for replication.

> Why a node with conflicting log won't become a leader:
>
> According to the Raft specification, for this node to become a leader, it must
> contain all committed logs. However, the log entry at `last_applied.index` is
> not committed; thus the node cannot become a leader.


### Safe to Remove All Non-Committed Logs

For this reason, Openraft truncates **ALL** non-committed logs and this is safe,
because: `snapshot_meta.last_log_id` is committed (or it cannot be in a
snapshot). If the local log ID conflicts with `snapshot_meta.last_log_id`, a
quorum must exist that contains `snapshot_meta.last_log_id`. Therefore, it is
**safe to remove all logs** on this node.


## 2. Install Snapshot

The transport must finish receiving and validating a snapshot before it calls
[`Raft::install_full_snapshot()`]. A file-based transport completes buffered
writes and supplies a readable handle positioned at the start. Openraft treats
`SnapshotData` as opaque, so a custom transport owns that preparation.

Validate completeness against sender-declared lengths and integrity checks, with
an explicit completion record. Comparing a file's length with the receiver's own
byte count cannot detect a sender that stopped early. The
[directory transfer writer](https://github.com/databendlabs/openraft/blob/main/examples/dir-transfer/src/writer.rs)
checks the manifest, per-file lengths, checksums, and final completion frame.

Installation publishes the validated snapshot and replaces the state machine.
Allocate receive paths locally: a peer's transfer ID is an opaque session key,
not a path or an ordering key for cleanup. A clock or process counter cannot
establish generation ownership across restarts.

The [RocksDB example](https://github.com/databendlabs/openraft/blob/main/examples/sm-rocks/README.md)
prepares separate database generations and publishes the active one through
`CURRENT`. Shared snapshot handles retain directories while builders or readers
use them; cleanup removes only generations with no remaining owner. An abandoned
receive handle removes its incomplete generation when its last owner drops.

The response of [`Raft::install_full_snapshot()`] answers the leader's
[`full_snapshot()`]. The leader takes a successful response as proof that the
follower installed the snapshot, or already had every log it covers, and
records the snapshot's last log id as the follower's matching log id. A
snapshot that is only staged, to be installed at a later restart, is not
installed: answer the leader with an error, and it sends the snapshot again.

## 3. Purge Logs

The final step is to purge logs up to [`snapshot_meta.last_log_id`].
This step is necessary because:

- 1) A local log that is `<=` [`snapshot_meta.last_log_id`] may conflict with the leader, and cannot be used anymore.

- 2) There may be a hole in the logs, if `snapshot_last_log_id > local_last_log_id`:

    ```text
        snapshot ----.
                     v
    ----lllllllllll--------------------->
    ```

If this node crashes after installing snapshot and before purging logs,
the log will be purged the next start-up, in [`get_initial_state()`].

A node that installs a snapshot after it stopped leading may still run the
replication tasks of its leadership, which read its log. It purges only once
none of them reads the log anymore. The purge waits in place, and the log I/O
after it waits too: the purge must reach storage before the logs that follow
the snapshot are appended, or the hole above would stay in the log.


[`get_initial_state()`]: `crate::storage::StorageHelper::get_initial_state`
[`snapshot_meta.last_log_id`]: `crate::storage::SnapshotMeta::last_log_id`

[`Raft::install_full_snapshot()`]: `crate::Raft::install_full_snapshot`
[`full_snapshot()`]: `crate::network::NetSnapshot::full_snapshot`