{"id":26826690,"url":"https://github.com/hslam/raft","last_synced_at":"2025-03-30T11:30:37.581Z","repository":{"id":57567190,"uuid":"333830224","full_name":"hslam/raft","owner":"hslam","description":"Package raft implements the Raft distributed consensus protocol based on hslam/rpc.","archived":false,"fork":false,"pushed_at":"2023-05-17T14:14:53.000Z","size":4481,"stargazers_count":3,"open_issues_count":0,"forks_count":0,"subscribers_count":2,"default_branch":"master","last_synced_at":"2024-07-02T20:18:59.986Z","etag":null,"topics":["consensus","distributed","go","golang","protocol","raft"],"latest_commit_sha":null,"homepage":"","language":"Go","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/hslam.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null}},"created_at":"2021-01-28T17:16:28.000Z","updated_at":"2023-05-12T15:59:16.000Z","dependencies_parsed_at":"2022-08-23T12:30:24.948Z","dependency_job_id":"bba3f541-48bc-4e6a-8adc-979de83c24f1","html_url":"https://github.com/hslam/raft","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/hslam%2Fraft","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/hslam%2Fraft/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/hslam%2Fraft/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/hslam%2Fraft/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/hslam","download_url":"https://codeload.github.com/hslam/raft/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":246313596,"owners_count":20757446,"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":["consensus","distributed","go","golang","protocol","raft"],"created_at":"2025-03-30T11:30:36.395Z","updated_at":"2025-03-30T11:30:37.516Z","avatar_url":"https://github.com/hslam.png","language":"Go","funding_links":[],"categories":[],"sub_categories":[],"readme":"# raft\n[![PkgGoDev](https://pkg.go.dev/badge/github.com/hslam/raft)](https://pkg.go.dev/github.com/hslam/raft)\n[![Build Status](https://github.com/hslam/raft/workflows/build/badge.svg)](https://github.com/hslam/raft/actions)\n[![codecov](https://codecov.io/gh/hslam/raft/branch/master/graph/badge.svg)](https://codecov.io/gh/hslam/raft)\n[![Go Report Card](https://goreportcard.com/badge/github.com/hslam/raft)](https://goreportcard.com/report/github.com/hslam/raft)\n[![LICENSE](https://img.shields.io/github/license/hslam/raft.svg?style=flat-square)](https://github.com/hslam/raft/blob/master/LICENSE)\n\nPackage raft implements the [Raft](https://raft.github.io/raft.pdf \"raft\") distributed consensus protocol based on [hslam/rpc](https://github.com/hslam/rpc \"rpc\").\n\n## Features\n\n* Leader election\n* Log replication\n* Membership changes\n* Log compaction (snapshotting)\n* [RPC](https://github.com/hslam/rpc \"rpc\") transport\n* [Write-ahead logging](https://github.com/hslam/wal \"wal\") and [LRU cache](https://github.com/hslam/lru \"lru\")\n* ReadIndex/LeaseRead\n* Non-voting members (The leader only replicates log entries to them)\n* Snapshot policies (Never/EverySecond/CustomSync)\n\n## Get started\n\n### Install\n```\ngo get github.com/hslam/raft\n```\n### Import\n```\nimport \"github.com/hslam/raft\"\n```\n\n### Example\n\n```go\npackage main\n\nimport (\n\t\"flag\"\n\t\"github.com/hslam/raft\"\n\t\"io\"\n\t\"io/ioutil\"\n\t\"log\"\n\t\"strings\"\n\t\"time\"\n)\n\nvar host, path, members string\nvar port int\nvar join bool\n\nfunc init() {\n\tflag.StringVar(\u0026host, \"h\", \"localhost\", \"hostname\")\n\tflag.IntVar(\u0026port, \"p\", 9001, \"port\")\n\tflag.StringVar(\u0026path, \"path\", \"raft.example/node.1\", \"data dir\")\n\tflag.BoolVar(\u0026join, \"join\", false, \"\")\n\tflag.StringVar(\u0026members, \"members\", \"localhost:9001\", \"host:port,nonVoting;host:port\")\n\tflag.Parse()\n}\n\nfunc main() {\n\tctx := \u0026Context{data: \"\"}\n\tnode, err := raft.NewNode(host, port, path, ctx, join, parse(members))\n\tif err != nil {\n\t\tpanic(err)\n\t}\n\tnode.RegisterCommand(\u0026Command{})\n\tnode.SetCodec(\u0026raft.JSONCodec{})\n\tnode.SetSnapshot(ctx)\n\tnode.LeaderChange(func() {\n\t\tif node.IsLeader() {\n\t\t\tnode.Do(\u0026Command{\"foobar\"})\n\t\t\tlog.Printf(\"State:%s, Set:foobar\\n\", node.State())\n\t\t\tif ok := node.ReadIndex(); ok {\n\t\t\t\tlog.Printf(\"State:%s, Get:%s\\n\", node.State(), ctx.Get())\n\t\t\t}\n\t\t} else {\n\t\t\tfor len(ctx.Get()) == 0 {\n\t\t\t\ttime.Sleep(time.Second)\n\t\t\t}\n\t\t\tlog.Printf(\"State:%s, Get:%s\\n\", node.State(), ctx.Get())\n\t\t}\n\t})\n\tnode.Start()\n\tfor range time.NewTicker(time.Second * 5).C {\n\t\tlog.Printf(\"State:%s, Leader:%s\\n\", node.State(), node.Leader())\n\t}\n}\n\ntype Context struct{ data string }\n\nfunc (ctx *Context) Set(value string) { ctx.data = value }\nfunc (ctx *Context) Get() string      { return ctx.data }\n\n// Save implements the raft.Snapshot Save method.\nfunc (ctx *Context) Save(w io.Writer) (int, error) { return w.Write([]byte(ctx.Get())) }\n\n// Recover implements the raft.Snapshot Recover method.\nfunc (ctx *Context) Recover(r io.Reader) (int, error) {\n\traw, err := ioutil.ReadAll(r)\n\tif err != nil {\n\t\treturn 0, err\n\t}\n\tctx.Set(string(raw))\n\treturn len(raw), nil\n}\n\n// Command implements the raft.Command interface.\ntype Command struct{ Data string }\n\nfunc (c *Command) Type() uint64 { return 1 }\nfunc (c *Command) Do(context interface{}) (interface{}, error) {\n\tcontext.(*Context).Set(c.Data)\n\treturn nil, nil\n}\n\nfunc parse(members string) (m []*raft.Member) {\n\tif members != \"\" {\n\t\tfor _, member := range strings.Split(members, \";\") {\n\t\t\tif len(member) \u003e 0 {\n\t\t\t\tstrs := strings.Split(member, \",\")\n\t\t\t\tm = append(m, \u0026raft.Member{Address: strs[0]})\n\t\t\t\tif len(strs) \u003e 1 \u0026\u0026 strs[1] == \"true\" {\n\t\t\t\t\tm[len(m)-1].NonVoting = true\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t}\n\treturn\n}\n```\n\n### Build\n```\ngo build -o node main.go\n```\n\n**One node**\n```sh\n./node -h=localhost -p=9001 -path=\"raft.example/node.1\" -join=false\n```\n\n**Three nodes**\n```sh\n./node -h=localhost -p=9001 -path=\"raft.example/node.hw.1\" -join=false \\\n-members=\"localhost:9001;localhost:9002;localhost:9003\"\n\n./node -h=localhost -p=9002 -path=\"raft.example/node.hw.2\" -join=false \\\n-members=\"localhost:9001;localhost:9002;localhost:9003\"\n\n./node -h=localhost -p=9003 -path=\"raft.example/node.hw.3\" -join=false \\\n-members=\"localhost:9001;localhost:9002;localhost:9003\"\n```\n\n**Membership changes**\n```sh\n./node -h=localhost -p=9001 -path=\"raft.example/node.mc.1\" -join=false\n\n./node -h=localhost -p=9002 -path=\"raft.example/node.mc.2\" -join=true \\\n-members=\"localhost:9001;localhost:9002\"\n\n./node -h=localhost -p=9003 -path=\"raft.example/node.mc.3\" -join=true \\\n-members=\"localhost:9001;localhost:9002;localhost:9003\"\n```\n\n**Non-voting**\n```sh\n./node -h=localhost -p=9001 -path=\"raft.example/node.nv.1\" -join=false \\\n-members=\"localhost:9001;localhost:9002;localhost:9003;localhost:9004,true\"\n\n./node -h=localhost -p=9002 -path=\"raft.example/node.nv.2\" -join=false \\\n-members=\"localhost:9001;localhost:9002;localhost:9003;localhost:9004,true\"\n\n./node -h=localhost -p=9003 -path=\"raft.example/node.nv.3\" -join=false \\\n-members=\"localhost:9001;localhost:9002;localhost:9003;localhost:9004,true\"\n\n./node -h=localhost -p=9004 -path=\"raft.example/node.nv.4\" -join=false \\\n-members=\"localhost:9001;localhost:9002;localhost:9003;localhost:9004,true\"\n```\n\n### [Benchmark](https://github.com/hslam/raft-benchmark  \"raft-benchmark\")\nRunning on a three nodes cluster.\n##### Write\n\n\u003cimg src=\"https://raw.githubusercontent.com/hslam/raft-benchmark/master/raft-write-qps.png\" width = \"400\" height = \"300\" alt=\"write-qps\" align=center\u003e\u003cimg src=\"https://raw.githubusercontent.com/hslam/raft-benchmark/master/raft-write-p99.png\" width = \"400\" height = \"300\" alt=\"write-p99\" align=center\u003e\n\n##### Read Index\n\n\u003cimg src=\"https://raw.githubusercontent.com/hslam/raft-benchmark/master/raft-read-qps.png\" width = \"400\" height = \"300\" alt=\"read-qps\" align=center\u003e\u003cimg src=\"https://raw.githubusercontent.com/hslam/raft-benchmark/master/raft-read-p99.png\" width = \"400\" height = \"300\" alt=\"read-p99\" align=center\u003e\n\n## License\nThis package is licensed under a MIT license (Copyright (c) 2019 Meng Huang)\n\n## Author\nraft was written by Meng Huang.","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fhslam%2Fraft","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fhslam%2Fraft","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fhslam%2Fraft/lists"}