Skip to content

Commit

Permalink
Doc: Add FAQ: store additional info in Node
Browse files Browse the repository at this point in the history
  • Loading branch information
drmingdrmer committed Apr 14, 2024
1 parent 57181fa commit c21fc43
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 0 deletions.
1 change: 1 addition & 0 deletions openraft/src/docs/faq/faq-toc.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
- [Cluster management](#cluster-management)
* [How to initialize a cluster?](#how-to-initialize-a-cluster)
* [Are there any issues with running a single node service?](#are-there-any-issues-with-running-a-single-node-service)
* [How do I store additional information about nodes in Openraft?](#how-do-i-store-additional-information-about-nodes-in-openraft)
* [How to remove node-2 safely from a cluster `{1, 2, 3}`?](#how-to-remove-node-2-safely-from-a-cluster-1-2-3)
* [What actions are required when a node restarts?](#what-actions-are-required-when-a-node-restarts)
* [What will happen when data gets lost?](#what-will-happen-when-data-gets-lost)
Expand Down
37 changes: 37 additions & 0 deletions openraft/src/docs/faq/faq.md
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,40 @@ A single node functions exactly the same as cluster mode.
It will consistently maintain the `Leader` status and never transition to `Candidate` or `Follower` states.


### How do I store additional information about nodes in Openraft?

By default, Openraft provide a [`BasicNode`] as the node type in a cluster.
To store more information about each node in Openraft, define a custom struct
with the desired fields and use it in place of `BasicNode`. Here's a brief
guide:

1. Define your custom node struct:

```rust,ignore
#[derive(...)]
struct MyNode {
ipv4: String,
ipv6: String,
port: u16,
// Add additional fields as needed
}
```

2. Register the custom node type with `declare_raft_types!` macro:

```rust,ignore
openraft::declare_raft_types!(
pub MyRaftConfig:
// ...
NodeId = u64, // Use the appropriate type for NodeId
Node = MyNode, // Replace BasicNode with your custom node type
// ... other associated types
);
```

Use `MyRaftConfig` in your Raft setup to utilize the custom node structure.


### How to remove node-2 safely from a cluster `{1, 2, 3}`?

Call `Raft::change_membership(btreeset!{1, 3})` to exclude node-2 from
Expand Down Expand Up @@ -188,6 +222,9 @@ pub(crate) fn following_handler(&mut self) -> FollowingHandler<C> {
[`Linearizable Read`]: `crate::docs::protocol::read`
[`leader_id`]: `crate::docs::data::leader_id`

[`BasicNode`]: `crate::node::BasicNode`
[`RaftTypeConfig`]: `crate::RaftTypeConfig`

[`RaftLogStorage::save_committed()`]: `crate::storage::RaftLogStorage::save_committed`

[`RaftNetwork`]: `crate::network::RaftNetwork`
Expand Down

0 comments on commit c21fc43

Please sign in to comment.