{"id":13672317,"url":"https://github.com/PsiACE/riteraft","last_synced_at":"2025-04-27T21:32:42.267Z","repository":{"id":37178253,"uuid":"350762816","full_name":"PsiACE/riteraft","owner":"PsiACE","description":"RiteRaft - A raft framework, for regular people ","archived":false,"fork":false,"pushed_at":"2024-02-18T02:30:07.000Z","size":63,"stargazers_count":318,"open_issues_count":3,"forks_count":21,"subscribers_count":5,"default_branch":"main","last_synced_at":"2024-10-30T22:08:26.352Z","etag":null,"topics":["distributed","distributed-systems","raft","raft-framework","rust"],"latest_commit_sha":null,"homepage":"","language":"Rust","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"apache-2.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/PsiACE.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE-APACHE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2021-03-23T15:27:36.000Z","updated_at":"2024-10-28T09:24:25.000Z","dependencies_parsed_at":"2024-02-18T03:25:46.545Z","dependency_job_id":"857e6216-8fed-4928-9a26-0bcdca94f638","html_url":"https://github.com/PsiACE/riteraft","commit_stats":null,"previous_names":["ritedb/riteraft","ritelabs/riteraft"],"tags_count":1,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/PsiACE%2Friteraft","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/PsiACE%2Friteraft/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/PsiACE%2Friteraft/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/PsiACE%2Friteraft/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/PsiACE","download_url":"https://codeload.github.com/PsiACE/riteraft/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":224087233,"owners_count":17253522,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["distributed","distributed-systems","raft","raft-framework","rust"],"created_at":"2024-08-02T09:01:32.178Z","updated_at":"2024-11-11T10:30:37.344Z","avatar_url":"https://github.com/PsiACE.png","language":"Rust","funding_links":[],"categories":["Rust"],"sub_categories":[],"readme":"# RiteRaft - A raft framework, for regular people\n\nThis is an attempt to create a layer on top of\n[tikv/raft-rs](https://github.com/tikv/raft-rs), that is easier to use and\nimplement. This is not supposed to be the most featureful raft, but instead a\nconvenient interface to get started quickly, and have a working raft in no\ntime.\n\nThe interface is strongly inspired by the one used by [canonical/raft](https://github.com/canonical/raft).\n\n## Getting started\n\nIn order to \"raft\" storage, we need to implement the `Storage` trait for it.\nBellow is an example with `HashStore`, which is a thread-safe wrapper around an\n`HashMap`:\n\n```rust\n/// convienient data structure to pass Message in the raft\n#[derive(Serialize, Deserialize)]\npub enum Message {\n    Insert { key: u64, value: String },\n}\n\n#[derive(Clone)]\nstruct HashStore(Arc\u003cRwLock\u003cHashMap\u003cu64, String\u003e\u003e\u003e);\n\nimpl Store for HashStore {\n    type Error = RaftError;\n\n    fn apply(\u0026mut self, message: \u0026[u8]) -\u003e Result\u003cVec\u003cu8\u003e, Self::Error\u003e {\n        let message: Message = deserialize(message).unwrap();\n        let message: Vec\u003cu8\u003e = match message {\n            Message::Insert { key, value } =\u003e {\n\n                let mut db = self.0.write().unwrap();\n                db.insert(key, value.clone());\n                serialize(\u0026value).unwrap()\n            }\n        };\n        Ok(message)\n    }\n\n    fn snapshot(\u0026self) -\u003e Vec\u003cu8\u003e {\n        serialize(\u0026self.0.read().unwrap().clone()).unwrap()\n    }\n\n    fn restore(\u0026mut self, snapshot: \u0026[u8]) -\u003e Result\u003c(), Self::Error\u003e {\n        let new: HashMap\u003cu64, String\u003e = deserialize(snapshot).unwrap();\n        let mut db = self.0.write().unwrap();\n        let _ = std::mem::replace(\u0026mut *db, new);\n        Ok(())\n    }\n}\n```\n\nOnly 3 methods need to be implemented for the Store:\n- `Store::apply`: applies a commited entry to the store.\n- `Store::snapshot`: returns snapshot data for the store.\n- `Store::restore`: applies the snapshot passed as argument.\n\n### running the raft\n\n```rust\n#[tokio::main]\nfn main() {\n    let store = HashStore::new();\n\n    let raft = Raft::new(options.raft_addr, store.clone());\n    let mailbox = Arc::new(raft.mailbox());\n    let (raft_handle, mailbox) = match options.peer_addr {\n        Some(addr) =\u003e {\n            info!(\"running in follower mode\");\n            let handle = tokio::spawn(raft.join(addr));\n            (handle, mailbox)\n        }\n        None =\u003e {\n            info!(\"running in leader mode\");\n            let handle =  tokio::spawn(raft.lead());\n            (handle, mailbox)\n        }\n    };\n\n    tokio::join!(raft);\n}\n\n```\n\nThe `mailbox` gives you a way to interact with the raft, for sending a message, or leaving the cluster for example.\n\n\n## Credit\n\nThis work is based on  [raft-frp](https://github.com/MarinPostma/raft-frp), but more adjustments and improvements have been made to the code .\n\n## License\n\nThis library is licensed under either of:\n\n* MIT license [LICENSE-MIT](LICENSE-MIT) or http://opensource.org/licenses/MIT\n* Apache License 2.0 [LICENSE-APACHE](LICENSE-APACHE) or https://opensource.org/licenses/Apache-2.0\n\nat your option.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2FPsiACE%2Friteraft","html_url":"https://awesome.ecosyste.ms/projects/github.com%2FPsiACE%2Friteraft","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2FPsiACE%2Friteraft/lists"}