Skip to main content

Simple distributed leader election

Project description

简单选主

一些任务只需要一个实例执行,由于高可用要求,需要多台实例。那么多实例通信就成问题,而一些情况下环境比较苛刻,没有组件可以借用。一个简单的选主提上日程。

介绍

采用UDP心跳通信,简化流程,特征如下:

  1. 初始化时,各个节点均为Follower状态。

  2. 开始选主时,所有节点的Follower状态转为Candidate状态,并向其他节点发送自身心跳。

  3. 其他节点收到心跳,对比自己心跳,Term比自身小,或者相同是ID比自身大,则节点降为Follower,不在发送心跳。

  4. 当Candidate持续心跳有效期时间N内未收到其他节点的心跳,则晋升为Leader,周期发送心跳。

安装

pip install simpleleader

使用

加入有三台实例,定义好各自通信端口,简单使用如下:

import time
from simpleleader import PeerLeader
endpoint = '127.0.0.1:9001'
cluster = '127.0.0.1:9001,127.0.0.1:9002,127.0.0.1:9003'
peer = PeerLeader(endpoint, cluster)
peer.run()
while 1:
    if peer.is_leader():
        print("%s is leader" % endpoint)
    else:
        print("%s is follower" % endpoint)
    time.sleep(1)

测试验证

python -m simpleleader.tests [实例总数] [当前实例ID,从0开始]
参考
python -m simpleleader.tests 3 0
python -m simpleleader.tests 3 1
python -m simpleleader.tests 3 2

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

simpleleader-0.0.4.tar.gz (4.8 kB view details)

Uploaded Source

File details

Details for the file simpleleader-0.0.4.tar.gz.

File metadata

  • Download URL: simpleleader-0.0.4.tar.gz
  • Upload date:
  • Size: 4.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.0 CPython/3.9.6

File hashes

Hashes for simpleleader-0.0.4.tar.gz
Algorithm Hash digest
SHA256 78474cd340a328174daf74ec6873abcc8bd0e1c4b52cee75a4f15b822d21f6c0
MD5 c68738879bc6094803f69964bffb834c
BLAKE2b-256 b61b45b444e2292b1153451d3af093cc5728278e8d391bfc77f5a99cff087998

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page