bully

package module
v0.0.0-...-063e99e Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Feb 21, 2019 License: MIT Imports: 7 Imported by: 0

README

Bully Algorithm Visualization

intro logo

codecov CircleCI Go Report GoDoc License

Table of Contents

What this repository is about

This repository contains source code of an implementation of the bully algorithm written in Go and a small browser visualization tool.

This has been made for learning purposes about distributed algorithms, Bully algorithm being the simplest leader election algorithm to implement.

Finally, I feel like implementing an algorithm myself helps me to understands it better and I thought it could be interesting to someone else.

What is the Bully algorithm ?

The Bully algorithm is one of the simplest algorithm made to design a coordinator among a set of machines.

Quickstart

First, go get this repository:

go get -d github.com/timtosi/bully-algorithm

Quickstart with Docker

❗ If you don't have Docker and Docker Compose installed, you still can execute this program by compiling the binaries.

This program comes with an already configured Docker Compose file launching five nodes and the browser based user interface.

You can use the run target in the provided Makefile to use it easily:

asciicast

You can access the visualization through your browser at localhost:8080. If you want to test the cluster behaviour, you can stop and resume some of the nodes with docker commands.

💡 If you want to update the number of node or change some IDs you will have to update the configuration file and the Docker Compose file accordingly.

Quickstart without Docker

First compiles and launch the visualization server:

cd $GOPATH/src/github.com/timtosi/bully-algorithm/cmd/data-viz
go build && ./data-viz

Visu

Then launch at least two nodes with specifying their ID in argument:

cd $GOPATH/src/github.com/timtosi/bully-algorithm/cmd/bully
go build && ./bully 0

💡 IDs should by default be comprised between 0 to 4 but you should be able to update peer address default configuration easily.

Nodes

You can access the visualization through your browser at localhost:8080.

FAQ

None so far 🙌

License

Every file provided here is available under the MIT License.

Not Good Enough ?

If you encouter any issue by using what is provided here, please let me know ! Help me to improve by sending your thoughts to timothee.tosi@gmail.com !

Documentation

Index

Constants

View Source
const (
	ELECTION = iota
	OK
	COORDINATOR
	CLOSE
)

Message Types.

Variables

This section is empty.

Functions

This section is empty.

Types

type Bully

type Bully struct {
	*net.TCPListener

	ID string
	// contains filtered or unexported fields
}

Bully is a `struct` representing a single node used by the `Bully Algorithm`.

NOTE: More details about the `Bully algorithm` can be found here https://en.wikipedia.org/wiki/Bully_algorithm .

func NewBully

func NewBully(ID, addr, proto string, peers map[string]string) (*Bully, error)

NewBully returns a new `Bully` or an `error`.

NOTE: All connections to `Peer`s are established during this function.

NOTE: The `proto` value can be one of this list: `tcp`, `tcp4`, `tcp6`.

func (*Bully) Connect

func (b *Bully) Connect(proto string, peers map[string]string)

Connect performs a connection to the remote `Peer`s.

func (*Bully) Coordinator

func (b *Bully) Coordinator() string

Coordinator returns `b.coordinator`.

NOTE: This function is thread-safe.

func (*Bully) Elect

func (b *Bully) Elect()

Elect handles the leader election mechanism of the `Bully algorithm`.

func (*Bully) Listen

func (b *Bully) Listen(proto, addr string) error

Listen makes `b` listens on the address `addr` provided using the protocol `proto` and returns an `error` if something occurs.

func (*Bully) Run

func (b *Bully) Run(workFunc func())

Run launches the two main goroutine. The first one is tied to the execution of `workFunc` while the other one is the `Bully algorithm`.

NOTE: This function is an infinite loop.

func (*Bully) Send

func (b *Bully) Send(to, addr string, what int) error

Send sends a `bully.Message` of type `what` to `b.peer[to]` at the address `addr`. If no connection is reachable at `addr` or if `b.peer[to]` does not exist, the function retries five times and returns an `error` if it does not succeed.

func (*Bully) SetCoordinator

func (b *Bully) SetCoordinator(ID string)

SetCoordinator sets `ID` as the new `b.coordinator` if `ID` is greater than `b.coordinator` or equal to `b.ID`.

NOTE: This function is thread-safe.

type Message

type Message struct {
	PeerID string
	Addr   string
	Type   int
}

Message is a `struct` used for communication between `bully.Bully`s.

type Peer

type Peer struct {
	ID string
	// contains filtered or unexported fields
}

Peer is a `struct` representing a remote `bully.Bully`.

func NewPeer

func NewPeer(ID, addr string, fd io.Writer) *Peer

NewPeer returns a new `*bully.Peer`.

type PeerMap

type PeerMap struct {
	// contains filtered or unexported fields
}

PeerMap is a `struct` implementing the `Peers` interface and representing a container of `bully.Peer`s.

func NewPeerMap

func NewPeerMap() *PeerMap

NewPeerMap returns a new `bully.PeerMap`.

func (*PeerMap) Add

func (pm *PeerMap) Add(ID, addr string, fd io.Writer)

Add creates a new `bully.Peer` and adds it to `pm.peers` using `ID` as a key.

NOTE: This function is thread-safe.

func (*PeerMap) Delete

func (pm *PeerMap) Delete(ID string)

Delete erases the `bully.Peer` corresponding to `ID` from `pm.peers`.

NOTE: This function is thread-safe.

func (*PeerMap) Find

func (pm *PeerMap) Find(ID string) bool

Find returns `true` if `pm.peers[ID]` exists, `false` otherwise.

NOTE: This function is thread-safe.

func (*PeerMap) PeerData

func (pm *PeerMap) PeerData() []struct {
	ID   string
	Addr string
}

PeerData returns a slice of anonymous structures representing a tupple composed of a `Peer.ID` and `Peer.addr`.

NOTE: This function is thread-safe.

func (*PeerMap) Write

func (pm *PeerMap) Write(ID string, msg interface{}) error

Write writes `msg` to `pm.peers[ID]`. It returns `nil` or an `error` if something occurs.

NOTE: This function is thread-safe.

type Peers

type Peers interface {
	Add(ID, addr string, fd io.Writer)
	Delete(ID string)
	Find(ID string) bool
	Write(ID string, msg interface{}) error
	PeerData() []struct {
		ID   string
		Addr string
	}
}

Peers is an `interface` exposing methods to handle communication with other `bully.Bully`s.

NOTE: This project offers a default implementation of the `Peers` interface that provides basic functions. This will work for the most simple of use cases fo exemples, although I strongly recommend you provide your own, safer implementation while doing real work.

Directories

Path Synopsis
cmd

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL