1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 | # 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`. ```go // 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: ```go 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. ```go 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](LICENSE) for the full license text. |
Commit History @upstream/0.1.0+git20160826.0a913e2
0
»»
- Fix Fault Tolerance section code example without ttl Aaron Chen 8 years ago
- Fix leadership doc example typo Aaron Chen 8 years ago
- Fix Consul Lock TTL with store failure Alexandre Beslic 8 years ago
- add support for specifying the leader election lock ttl Alexandre Beslic 8 years ago
- Merge pull request #1078 from abronan/leader_election_retry Andrea Luzzardi 8 years ago
- Fault tolerant Leader Election process, fixes leader information on docker info, fixes intermittent error on Consul session lock Alexandre Beslic 8 years ago
- Spelling mistakes FreedomWangLi (commit: Yong Qiao Wang) 8 years ago
- Update README.md fl0yd (commit: Mark Oates) 8 years ago
- migrate from pkg/store to docker/libkv Alexandre Beslic 8 years ago
- Fix typo in leadership/README.md Ahmet Alp Balkan 8 years ago
- Leadership: Remove flaky test. Andrea Luzzardi 8 years ago
- leadership: Fix data races in tests. Andrea Luzzardi 8 years ago
- leadership: Export leader/status also in functions (not just channels) Andrea Luzzardi 8 years ago
- fix leadership doc on support for consul/etcd/zookeeper Alexandre Beslic 8 years ago
- Fix small typo in leadership/README.md Ben Parnell 8 years ago
- leadership: API cleanup. Andrea Luzzardi 8 years ago
- leadership: Fix README Andrea Luzzardi 8 years ago
- leadership: Distributed Leader Election Andrea Luzzardi 8 years ago
0
»»