Giter Club home page Giter Club logo

floss's Introduction

Build Status

Floss

An implementation of the Raft consensus algorithm on top of Celluloid.

Installation

Add this line to your application's Gemfile:

gem 'floss'

And then execute:

$ bundle

Or install it yourself as:

$ gem install floss

Usage

We're going to implement a distributed counter. While not very useful, it's a good enough demonstration of what you can do with this library. Let's start with the counter service. It accepts three commands, get, reset and increase. The first simply returns the current count, the second sets the count to zero and the third changes the current count, optionally by a given amount.

class Counter
  attr_accessor :count

  def initialize
    self.count = 0
  end

  def get
    count
  end

  def reset
    self.count = 0
  end

  def increase(amount = 1)
    self.count += amount
  end
end

To increase reliability of your counter, you decide to distribute it across multiple machines. This is where floss comes into play. To simplify this demonstration, we're going to start multiple nodes in the same process.

addresses = [10001, 10002, 10003].map { |port| "tcp://127.0.0.1:#{port}" }

$nodes = addresses.size.times.map do |i|
  combination = addresses.rotate(i)
  options = {id: combination.first, peers: combination[1..-1]}
  Floss::Proxy.new(Counter.new, options)
end

# Give your nodes some time to start up.
$nodes.each(&:wait_until_ready)

Now we're ready to play with our distributed counter.

def random_node; $nodes.sample; end

random_node.get # => 0
random_node.increase # => 1
random_node.get # => 1

That was easy wasn't it? Let's see what happens if the cluster is damaged.

# Terminate a random node in the cluster.
doomed_node = $nodes.delete(random_node)
doomed_node_id = doomed_node.id
doomed_node.terminate

random_node.increase # => 2

Your cluster still works. If you'd kill another one, executing a command would result in an error because insufficient nodes are available to ensure your system's consistency.

Contributing

  1. Fork it
  2. Create your feature branch (git checkout -b my-new-feature)
  3. Commit your changes (git commit -am 'Added some feature')
  4. Push to the branch (git push origin my-new-feature)
  5. Create new Pull Request

floss's People

Contributors

aflatter avatar halorgium avatar arthurnn avatar tarcieri avatar tomquas avatar

Watchers

James Cloos avatar May 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.