Skip to content

Commit

Permalink
Refactor: update raft-kv-memstore example to use storage v2
Browse files Browse the repository at this point in the history
  • Loading branch information
tvsfx committed Dec 20, 2023
1 parent 3aa6b98 commit bfbe8c5
Show file tree
Hide file tree
Showing 6 changed files with 137 additions and 118 deletions.
2 changes: 1 addition & 1 deletion examples/raft-kv-memstore/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ name = "raft-key-value"
path = "src/bin/main.rs"

[dependencies]
openraft = { path = "../../openraft", features = ["serde"] }
openraft = { path = "../../openraft", features = ["serde", "storage-v2"] }

actix-web = "4.0.0-rc.2"
async-trait = "0.1.36"
Expand Down
6 changes: 4 additions & 2 deletions examples/raft-kv-memstore/src/app.rs
Original file line number Diff line number Diff line change
@@ -1,15 +1,17 @@
use std::sync::Arc;

use crate::LogStore;
use crate::NodeId;
use crate::Raft;
use crate::Store;
use crate::StateMachineStore;

// Representation of an application state. This struct can be shared around to share
// instances of raft, store and more.
pub struct App {
pub id: NodeId,
pub addr: String,
pub raft: Raft,
pub store: Arc<Store>,
pub log_store: Arc<LogStore>,
pub state_machine_store: Arc<StateMachineStore>,
pub config: Arc<openraft::Config>,
}
27 changes: 17 additions & 10 deletions examples/raft-kv-memstore/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ use actix_web::middleware;
use actix_web::middleware::Logger;
use actix_web::web::Data;
use actix_web::HttpServer;
use openraft::storage::Adaptor;
use openraft::BasicNode;
use openraft::Config;
use openraft::TokioRuntime;
Expand All @@ -20,7 +19,6 @@ use crate::network::raft;
use crate::network::Network;
use crate::store::Request;
use crate::store::Response;
use crate::store::Store;

pub mod app;
pub mod client;
Expand All @@ -35,8 +33,8 @@ openraft::declare_raft_types!(
Entry = openraft::Entry<TypeConfig>, SnapshotData = Cursor<Vec<u8>>, AsyncRuntime = TokioRuntime
);

pub type LogStore = Adaptor<TypeConfig, Arc<Store>>;
pub type StateMachineStore = Adaptor<TypeConfig, Arc<Store>>;
pub type LogStore = crate::store::LogStore;
pub type StateMachineStore = crate::store::StateMachineStore;
pub type Raft = openraft::Raft<TypeConfig>;

pub mod typ {
Expand Down Expand Up @@ -67,25 +65,34 @@ pub async fn start_example_raft_node(node_id: NodeId, http_addr: String) -> std:

let config = Arc::new(config.validate().unwrap());

// Create a instance of where the Raft logs will be stored.
let log_store = Arc::new(LogStore::default());
// Create a instance of where the Raft data will be stored.
let store = Arc::new(Store::default());

let (log_store, state_machine) = Adaptor::new(store.clone());
let state_machine_store = Arc::new(StateMachineStore::default());

// Create the network layer that will connect and communicate the raft instances and
// will be used in conjunction with the store created above.
let network = Network {};

// Create a local raft instance.
let raft = openraft::Raft::new(node_id, config.clone(), network, log_store, state_machine).await.unwrap();
let raft = openraft::Raft::new(
node_id,
config.clone(),
network,
log_store.clone(),
state_machine_store.clone(),
)
.await
.unwrap();

// Create an application that will store all the instances created above, this will
// be later used on the actix-web services.
// later be used on the actix-web services.
let app_data = Data::new(App {
id: node_id,
addr: http_addr.clone(),
raft,
store,
log_store,
state_machine_store,
config,
});

Expand Down
4 changes: 2 additions & 2 deletions examples/raft-kv-memstore/src/network/api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ pub async fn write(app: Data<App>, req: Json<Request>) -> actix_web::Result<impl

#[post("/read")]
pub async fn read(app: Data<App>, req: Json<String>) -> actix_web::Result<impl Responder> {
let state_machine = app.store.state_machine.read().await;
let state_machine = app.state_machine_store.state_machine.read().await;
let key = req.0;
let value = state_machine.data.get(&key).cloned();

Expand All @@ -43,7 +43,7 @@ pub async fn consistent_read(app: Data<App>, req: Json<String>) -> actix_web::Re

match ret {
Ok(_) => {
let state_machine = app.store.state_machine.read().await;
let state_machine = app.state_machine_store.state_machine.read().await;
let key = req.0;
let value = state_machine.data.get(&key).cloned();

Expand Down
Loading

0 comments on commit bfbe8c5

Please sign in to comment.