Code Monkey home page Code Monkey logo

raft's Introduction

Raft

Files

  • raft_conf.json - This contains the election timeout and heartbeat timeout details
  • peers.json - This contains the Id's and Addresses of server nodes
  • raft.go - The Raft package which chooses Leader
  • raft_test.go - The test file for testing

Leader election

In this first we make servers using cluster library and then using raft package choose the Leader among the nodes. Election are held when the election timouts

There are 3 states of server nodes

  • Leader
  • Follower
  • Candidate

Election are held in terms which have Term id. Initially all nodes are in Follwer state and when the election timeouts the nodes becomes Candidate and increases it's term by 1 and requests vote using RequestVote RPC's from all other Followers. If the Candidate receives majority of votes then he wins and becomes Leader. After selection of Leader all other nodes go to Follower state. Leader sends AppendEntries RPC to all Followers to tell that he is alive and serving as leader. When ever any Follower timeouts and dont receives AppendEntries he goes to Candidate state increments Term id by 1 and requests votes from other nodes

Raft ensures that there is only one leader at a time.

How to run

raft.go has function New_raft(id,majority,peers file,raft conf file) which takes input the id number of server, majority value, peers file name for initializing the cluster, and raft conf file name

  • type Raft interface
    • Id() int -returns the Id of node
    • Term() int -returns the term id
    • isLeader() bool
    • Quit()
    • Start()
    • Reset() //for reseting the server to follower

Now after making raft nodes we can close and again start the server using Quit() and Start() functions. Reset the node to follower by Reset() function By giving delay between start and close we can see who is leader after each close and start using isLeader() fucntion.

raft_test.go is a sample test file which is documented.

The code is documented for more details.

raft's People

Contributors

rajusharma avatar

Watchers

Sriram Srinivasan avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.