leadership

Distributed Leader Election using docker/libkv

  • 所有者: docker-archive/leadership
  • 平台:
  • 許可證: Apache License 2.0
  • 分類:
  • 主題:
  • 喜歡:
    0
      比較:

Github星跟蹤圖

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.

主要指標

概覽
名稱與所有者docker-archive/leadership
主編程語言Go
編程語言Go (語言數: 1)
平台
許可證Apache License 2.0
所有者活动
創建於2016-02-08 23:15:38
推送於2019-11-04 10:00:06
最后一次提交2016-08-26 19:48:22
發布數1
最新版本名稱v0.1.0 (發布於 )
第一版名稱v0.1.0 (發布於 )
用户参与
星數160
關注者數10
派生數39
提交數25
已啟用問題?
問題數4
打開的問題數0
拉請求數3
打開的拉請求數0
關閉的拉請求數5
项目设置
已啟用Wiki?
已存檔?
是復刻?
已鎖定?
是鏡像?
是私有?