Giter Club home page Giter Club logo

leadership's Introduction

Leadership: Distributed Leader Election for Clustered Environments.

Leadership is a library for a cluster leader election on top of a distributed Key/Value store.

It is built using the docker/libkv library and is designed to work across multiple storage backends.

You can use leadership with Consul, etcd and Zookeeper.

// Create a store using pkg/store.
client, err := store.NewStore("consul", []string{"127.0.0.1:8500"}, &store.Config{})
if err != nil {
	panic(err)
}

underwood := leadership.NewCandidate(client, "service/swarm/leader", "underwood", 15*time.Second)
electedCh, _ := underwood.RunForElection()

for isElected := range electedCh {
	// This loop will run every time there is a change in our leadership
	// status.

	if isElected {
		// We won the election - we are now the leader.
		// Let's do leader stuff, for example, sleep for a while.
		log.Printf("I won the election! I'm now the leader")
		time.Sleep(10 * time.Second)

		// Tired of being a leader? You can resign anytime.
		candidate.Resign()
	} else {
		// We lost the election but are still running for leadership.
		// `elected == false` is the default state and is the first event
		// we'll receive from the channel. After a successful election,
		// this event can get triggered if someone else steals the
		// leadership or if we resign.

		log.Printf("Lost the election, let's try another time")
	}
}

It is possible to follow an election in real-time and get notified whenever there is a change in leadership:

follower := leadership.NewFollower(client, "service/swarm/leader")
leaderCh, _ := follower.FollowElection()
for leader := range leaderCh {
	// Leader is a string containing the value passed to `NewCandidate`.
	log.Printf("%s is now the leader", leader)
}
log.Fatal("Cannot follow the election, store is probably down")
// Recovery code or exit

A typical use case for this is to be able to always send requests to the current leader.

Fault tolerance

Leadership returns an error channel for Candidates and Followers that you can use to be resilient to failures. For example, if the watch on the leader key fails because the store becomes unavailable, you can retry the process later.

func participate() {
    // Create a store using pkg/store.
    client, err := store.NewStore("consul", []string{"127.0.0.1:8500"}, &store.Config{})
    if err != nil {
        panic(err)
    }

    waitTime := 10 * time.Second
    underwood := leadership.NewCandidate(client, "service/swarm/leader", "underwood", 15*time.Second)

    go func() {
        for {
            run(underwood)
            time.Sleep(waitTime)
            // retry
        }
    }()
}

func run(candidate *leadership.Candidate) {
    electedCh, errCh := candidate.RunForElection()
    for {
        select {
        case isElected := <-electedCh:
            if isElected {
                // Do something
            } else {
                // Do something else
            }

        case err := <-errCh:
            log.Error(err)
            return
        }
    }
}

License

leadership is licensed under the Apache License, Version 2.0. See LICENSE for the full license text.

leadership's People

Contributors

abronan avatar ahmetb avatar aluzzardi avatar benjaminparnell avatar fl0yd avatar lowstz avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

leadership's Issues

Please tag releases

Please consider assigning version numbers and tagging releases. Tags/releases
are useful for downstream package maintainers (in Debian and other distributions) to export source tarballs, automatically track new releases and to declare dependencies between packages. Read more in the Debian Upstream Guide.

Versioning provides additional benefits to encourage vendoring of a particular (e.g. latest stable) release contrary to random unreleased snapshots.

Thank you.

See also

Leader does not get an event when there is a network connectivity loss to the Zookeeper

Hi

We are using the library to connect to the zookeeper and elect the master. The master who acquires the lock does not resign and we wait for the "isElectedCh" and "errch" to check for any events. When there is a network disconnect the master does not get any event that it is no longer the master, but the one of the slave becomes the master.

This is seen with Zookeeper.

Thanks,
Srikanth V

issues to install docker-engine-1.11.2-1.el7

Hello.

recently we tried to install docker in our Linux machine using yum and we are facing the following issue .

--> Finished Dependency Resolution
Error: Package: docker-engine-1.11.2-1.el7.centos.x86_64 (docker-main-repo)
Requires: libdevmapper.so.1.02(DM_1_02_97)(64bit)
Error: Package: docker-engine-1.11.2-1.el7.centos.x86_64 (docker-main-repo)
Requires: libsystemd.so.0()(64bit)
Error: Package: docker-engine-1.11.2-1.el7.centos.x86_64 (docker-main-repo)
Requires: libltdl.so.7()(64bit)
Error: Package: docker-engine-1.11.2-1.el7.centos.x86_64 (docker-main-repo)
Requires: libsystemd.so.0(LIBSYSTEMD_209)(64bit)
You could try using --skip-broken to work around the problem
You could try running: rpm -Va --nofiles --nodigest

kernel version
3.10.0-123.13.2.el7.x86_64
OS
Red Hat Enterprise Linux Server release 7.1
python --version
Python 2.7.5

could you please help us?

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.