|
| 1 | +# Guide for upgrading from [v0.8](https://github.com/datafuselabs/openraft/tree/v0.8.9) to [v0.9](https://github.com/datafuselabs/openraft/tree/release-0.9): |
| 2 | + |
| 3 | +[Change log v0.9.0](https://github.com/datafuselabs/openraft/blob/release-0.9/change-log.md) |
| 4 | + |
| 5 | + |
| 6 | +## Major changes: |
| 7 | + |
| 8 | +- Removed Compatibility support for v0.7. |
| 9 | + |
| 10 | +- Add [`AsyncRuntime`][] as an abstraction for async runtime. |
| 11 | + An application can use a different async runtime other than [`tokio`][] by implementing the [`AsyncRuntime`] trait. |
| 12 | + |
| 13 | +- `committed` log id can be optionally saved with [`RaftLogStorage::save_committed()`][]. |
| 14 | + |
| 15 | +- New errors: [`PayloadTooLarge`][]: to inform Openraft to split append entries request into smaller ones. |
| 16 | + |
| 17 | +- New API: support linearizable read: [`Raft::ensure_linearizable()`][]. |
| 18 | + |
| 19 | +- New API to support generic snapshot data transfer with the following new APIs: |
| 20 | + |
| 21 | + - [`Raft::get_snapshot()`][] |
| 22 | + - [`Raft::begin_receiving_snapshot()`][] |
| 23 | + - [`Raft::install_full_snapshot()`][] |
| 24 | + - [`RaftNetwork::full_snapshot()`][] |
| 25 | + |
| 26 | +- New feature flags: |
| 27 | + |
| 28 | + - [`singlethreaded`][]: Run Openraft in a single threaded environment, i.e., without requiring `Send` bound. |
| 29 | + - [`loosen-follower-log-revert`][]: Allows cleaning a follower's data, useful for testing. |
| 30 | + - [`generic-snapshot-data`][]: Remove `AsyncRead + AsyncWrite` bound from snapshot data. Let application define its own snapshot data transfer. |
| 31 | + |
| 32 | + |
| 33 | +## Upgrade `RaftTypeConfig` |
| 34 | + |
| 35 | +- Add `AsyncRuntime` to `RaftTypeConfig` to specify the async runtime to use. |
| 36 | + Openraft provides a default implementations: `TokioRuntime`. |
| 37 | + For example: |
| 38 | + ```ignore |
| 39 | + openraft::declare_raft_types!( |
| 40 | + pub TypeConfig: |
| 41 | + // ... |
| 42 | + AsyncRuntime = TokioRuntime |
| 43 | + ); |
| 44 | + ``` |
| 45 | + |
| 46 | + You can just implement the [`AsyncRuntime`][] trait for your own async runtime and use it in `RaftTypeConfig`. |
| 47 | + |
| 48 | +## Upgrade log storage: save committed log id |
| 49 | + |
| 50 | +In v0.9, `save_committed()` is added to `RaftLogStorage` trait to optionally save the `committed` log id. |
| 51 | +For example: |
| 52 | +```ignore |
| 53 | +impl RaftLogStorage for YourStorage { |
| 54 | + // ... |
| 55 | + fn save_committed(&self, committed: u64) -> Result<(), StorageError> { |
| 56 | + // ... |
| 57 | + } |
| 58 | +} |
| 59 | +``` |
| 60 | + |
| 61 | +If committed log id is saved, |
| 62 | +the committed log will be applied to state machine via [`RaftStateMachine::apply()`][] upon startup, |
| 63 | +if they are not already applied. |
| 64 | + |
| 65 | +Not implementing `save_committed()` will not affect correctness. |
| 66 | + |
| 67 | +## Upgrade snapshot transmission |
| 68 | + |
| 69 | +In v0.9, an application can use its own snapshot data transfer by enabling [`generic-snapshot-data`][] feature flag. |
| 70 | +With this flag enabled, the snapshot data can be arbitrary type |
| 71 | +and is no longer required to implement `AsyncRead + AsyncWrite` trait. |
| 72 | + |
| 73 | +To use arbitrary snapshot data, the application needs to: |
| 74 | + |
| 75 | +- Specify the snapshot data type in `RaftTypeConfig`: |
| 76 | + ```ignore |
| 77 | + openraft::declare_raft_types!( |
| 78 | + pub TypeConfig: |
| 79 | + // ... |
| 80 | + SnapshotData = YourSnapshotData |
| 81 | + ); |
| 82 | + ``` |
| 83 | + |
| 84 | +- implement the snapshot transfer in the application's network layer by implementing the [`RaftNetwork`] trait. |
| 85 | + For example: |
| 86 | + ```ignore |
| 87 | + impl RaftNetwork for YourNetwork { |
| 88 | + // ... |
| 89 | + fn full_snapshot(&self, /*...*/) -> Result<_,_> { |
| 90 | + // ... |
| 91 | + } |
| 92 | + } |
| 93 | + ``` |
| 94 | + |
| 95 | + `full_snapshot()` allows for the transfer of snapshot data using any preferred method. |
| 96 | + Openraft provides a default implementation in [`Chunked`][]. |
| 97 | + [`Chunked`][] just delegate the transmission to several calls |
| 98 | + to the old chunk-based API `RaftNetwork::install_snapshot()`. |
| 99 | + If you want to minimize the changes in your application, |
| 100 | + just call `Chunked::send_snapshot()` in `full_snapshot()`: |
| 101 | + ```ignore |
| 102 | + impl RaftNetwork for YourNetwork { |
| 103 | + async fn full_snapshot(&mut self, vote: Vote<C::NodeId>, snapshot: Snapshot<C>, /*...*/ |
| 104 | + ) -> Result<SnapshotResponse<C::NodeId>, StreamingError<C, Fatal<C::NodeId>>> { |
| 105 | + let resp = Chunked::send_snapshot(self, vote, snapshot, /*...*/).await?; |
| 106 | + Ok(resp) |
| 107 | + } |
| 108 | + } |
| 109 | + ``` |
| 110 | + |
| 111 | + Refer to: [the default chunk-based snapshot sending](https://github.com/datafuselabs/openraft/blob/2cc7170ffaf87c674e5ca13370402528f8ab3958/openraft/src/network/network.rs#L129) |
| 112 | + |
| 113 | +- On the receiving end, |
| 114 | + when the application finished receiving the snapshot data, |
| 115 | + it should call [`Raft::install_full_snapshot()`][] to install it. |
| 116 | + |
| 117 | + `Chunked::receive_snapshot` provides a default chunk-based implementation for receiving snapshot data. |
| 118 | + The application can just use it: |
| 119 | + |
| 120 | + ```ignore |
| 121 | + async fn handle_install_snapshot_request( |
| 122 | + &self, |
| 123 | + req: InstallSnapshotRequest<C>, |
| 124 | + ) -> Result<_, _> |
| 125 | + where C::SnapshotData: AsyncRead + AsyncWrite + AsyncSeek + Unpin, |
| 126 | + { |
| 127 | + let my_vote = self.with_raft_state(|state| *state.vote_ref()).await?; |
| 128 | + let resp = InstallSnapshotResponse { vote: my_vote }; |
| 129 | +
|
| 130 | + let finished_snapshot = { |
| 131 | + use crate::network::snapshot_transport::Chunked; |
| 132 | + use crate::network::snapshot_transport::SnapshotTransport; |
| 133 | +
|
| 134 | + let mut streaming = self.snapshot.lock().await; |
| 135 | + Chunked::receive_snapshot(&mut *streaming, self, req).await? |
| 136 | + }; |
| 137 | +
|
| 138 | + if let Some(snapshot) = finished_snapshot { |
| 139 | + let resp = self.install_full_snapshot(req_vote, snapshot).await?; |
| 140 | + return Ok(resp.into()); |
| 141 | + } |
| 142 | + Ok(resp) |
| 143 | + } |
| 144 | + ``` |
| 145 | + |
| 146 | + Refer to: [the default chunk-based snapshot receiving](https://github.com/datafuselabs/openraft/blob/c9a463f5ce73d1e7dd66eabfe909fe8d5a087f0e/openraft/src/raft/mod.rs#L447) |
| 147 | + |
| 148 | + Note that the application is responsible for maintaining a streaming state [`Streaming`][] |
| 149 | + during receiving chunks. |
| 150 | + |
| 151 | + |
| 152 | + |
| 153 | +[`AsyncRuntime`]: `crate::AsyncRuntime` |
| 154 | +[`RPCOption`]: `crate::network::RPCOption` |
| 155 | +[`Chunked`]: `crate::network::snapshot_transport::Chunked` |
| 156 | +[`Chunked::receive_snapshot`]: `crate::network::snapshot_transport::Chunked::receive_snapshot` |
| 157 | +[`Streaming`]: `crate::network::snapshot_transport::Streaming` |
| 158 | + |
| 159 | +[`PayloadTooLarge`]: `crate::error::PayloadTooLarge` |
| 160 | + |
| 161 | +[`Raft::ensure_linearizable()`]: `crate::Raft::ensure_linearizable` |
| 162 | +[`Raft::get_snapshot()`]: `crate::Raft::get_snapshot` |
| 163 | +[`Raft::begin_receiving_snapshot()`]: `crate::Raft::begin_receiving_snapshot` |
| 164 | +[`Raft::install_full_snapshot()`]: `crate::Raft::install_full_snapshot` |
| 165 | + |
| 166 | +[`RaftNetwork`]: `crate::network::RaftNetwork` |
| 167 | +[`RaftNetwork::full_snapshot()`]: `crate::network::RaftNetwork::full_snapshot` |
| 168 | + |
| 169 | +[`RaftLogStorage::save_committed()`]: `crate::storage::RaftLogStorage::save_committed` |
| 170 | + |
| 171 | +[`RaftStateMachine::apply()`]: `crate::storage::RaftStateMachine::apply` |
| 172 | + |
| 173 | +[`singlethreaded`]: `crate::docs::feature_flags#feature-flag-singlethreaded` |
| 174 | +[`loosen-follower-log-revert`]: `crate::docs::feature_flags#feature-flag-loosen-follower-log-revert` |
| 175 | +[`generic-snapshot-data`]: `crate::docs::feature_flags#feature-flag-generic-snapshot-data` |
| 176 | +[`tracing-log`]: `crate::docs::feature_flags#feature-flag-tracing-log` |
| 177 | + |
| 178 | +[`tokio`]: https://tokio.rs/ |
| 179 | +[`monoio`]: https://github.com/bytedance/monoio |
0 commit comments