用于分布式环境的网络互斥服务。
distributex的Python项目详细描述
经销商
distributex是一个简单的互斥服务,用于协调特定的集群 操作。
note:Distributex不是为需要高性能的任务而设计的 或公平锁定收购。这是一个非常简单的忙等待锁,非常慢 收购。不要将它用于非常大的集群,因为 请求者很有可能永远无法获得锁。
Distributex服务器
Distributex服务器提供了一个简单的HTTP服务。它是用 Twisted,并提供Twisted插件。它还需要pyyaml 配置。
您可以按以下方式开始,或将其包装在supervisor中,或pass-d或其他任何方式:
$ twistd -n distributex -c distributex.yaml
配置文件是一个定义锁池的简单yaml结构。一个 锁池是一种资源,您可以让事情发生冲突。
backend: memcache pools: - name: pool1 expire: 300 - name: pool2 expire: 300 servers: acme1, acme2
这将创建两个池,其锁在5分钟后过期。一般来说 一个好主意是设置一个到期日来确保某些东西,否则它将默认为 30分钟。如果不想过期,请将其设置为0,但我不希望 推荐。
您可以指定“memcache”后端或“inmemory”,有一些优点 两者都有缺点。memcache将变慢,但状态保持为远离 Distributex服务器和您可以扩展工作线程,但是 InMemory后端可以在一台机器上处理大约5000个等待锁 裁员是唯一真正的问题。锁过期也更可靠一些 在memcache后端更简单。
还可以提供以逗号分隔的服务器列表,以防止意外 泳池入侵。这是不安全的,也不是锁释放,因为任何人都可以 伪造他们的主机名。Distributex客户端将传递 主人。
也可以设置“maxlocks”,使池的行为类似于 信号灯
您可以按以下步骤测试服务:
$ curl "http://localhost:9889/get/?host=me&pool=pool1" YES $ curl "http://localhost:9889/get/?host=them&pool=pool1" NO $ curl "http://localhost:9889/release/?host=me&pool=pool1" OK $ curl "http://localhost:9889/get/?host=them&pool=pool1" YES $ curl "http://localhost:9889/release/?host=them&pool=pool1" OK
该服务还提供一个“wait”命令,该命令将离开连接 打开直到锁被锁上。
Distributex客户端
这个包中没有包含一个用于包装命令的简单python脚本。你可以从https://raw.github.com/calston/distributex/master/client/distributex
usage: distributex [-h] -H HOST -r POOL [-p PORT] [command] Distributex client positional arguments: command Command to execute when lock is obtained optional arguments: -h, --help show this help message and exit -H HOST Server hostname -r POOL Resource pool -p PORT Server port (default 9889) -l Use local locking as well
这对于阻止puppet这样的cron作业很有用。
*/5 * * * * /usr/bin/distributex -H distributex.acme.com -r pool1 '/usr/bin/puppet agent --onetime --no-daemonize'
这将确保集群中的puppet在任何时候都只运行一个实例。 您可能还希望将-l传递给distributex,以防止本地进程重叠。