本文整理汇总了Python中cluster.Cluster.get_master方法的典型用法代码示例。如果您正苦于以下问题:Python Cluster.get_master方法的具体用法?Python Cluster.get_master怎么用?Python Cluster.get_master使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类cluster.Cluster
的用法示例。
在下文中一共展示了Cluster.get_master方法的4个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: RedisMonitor
# 需要导入模块: from cluster import Cluster [as 别名]
# 或者: from cluster.Cluster import get_master [as 别名]
class RedisMonitor(object):
''' The main monitor object. It starts all the Workers and
changes the cluster configuration in ZooKeeper when a Worker
send an update message.'''
def __init__(self, zk, redis_hosts, sleep_time, zk_path):
self.redis_class = redis.Redis
self.zk = zk
self.cluster = Cluster()
self.queue = Queue()
self.redis_hosts = redis_hosts
self.zk_properties = None
self.list_of_workers = []
self.zk_path = zk_path
self.redis_servers_map = {}
self.sleep_time = sleep_time
def execute(self):
''' execute is the only public method. It starts the main loop.'''
logger.info("Enter")
logger.info("self.redis_class=%s", str(self.redis_class))
self._discover_redis()
while 1:
self._parse_message_from_queue()
self._check_all_workers()
logger.info("Exit")
def _discover_redis(self):
''' check all the redis instances and registers to zk host, port and status.
It also prepares all the worker processes for monitoring.'''
logger.info('Enter')
self.zk.create_recursive(self.zk_path, '', OPEN_ACL_UNSAFE)
self.zk_properties = self.zk.properties(self.zk_path)
self._build_list_of_workers()
(master_as_tuple, list_of_slaves) = self._get_cluster_representation()
logger.info('master: [%r]', master_as_tuple[0])
slaves_map = {}
for i, slave in enumerate(list_of_slaves):
slaves_map[i] = ('%s:%s' % (slave.host, slave.port), slave.status)
logger.info("slaves: [%r] ", slaves_map)
self._update_zk_properties(master = master_as_tuple, slaves = slaves_map)
# start all workers
for worker in self.list_of_workers:
worker.start()
logger.info("%d Workers created", len(self.list_of_workers))
logger.info("Exit")
def _get_cluster_representation(self):
master = self.cluster.get_master()
master_as_tuple = ('%s:%s' %(master.host, master.port), master.status)
list_of_slaves = self.cluster.filtered_list((ROLE_SLAVE, ))
return (master_as_tuple, list_of_slaves)
def _parse_message_from_queue(self):
''' Blocks on queue, receives and reads messages from workers,
execute update actions based on past and present cluster's status.'''
message = self.queue.get()
logger.debug("Received message from Worker: [%s]", message)
server, new_status = message.split(',')
host, port = server.split(':')
present_node = self.cluster.get_node(host, port)
old_status = present_node.status
if new_status == REDIS_STATUS_KO:
present_node.setKO()
if old_status == REDIS_STATUS_OK:
logger.warn("Node (%s) [%s] has DIED!", present_node.role, server)
if present_node.is_master():
logger.warn("Master is down: promoting a new master...")
self._promote_new_master(present_node)
self._update_zk()
elif new_status == REDIS_STATUS_OK:
present_node.setOK()
if old_status == REDIS_STATUS_KO:
logger.warn("Node (%s) [%s] has RESURRECTED!", present_node.role, server)
redis_server = self._get_redis_server(host=present_node.host, port=present_node.port)
master = self.cluster.get_master()
if master.is_alive():
redis_server.slaveof(master.host, master.port)
else:
self._promote_new_master(master)
self._update_zk()
else:
logger.critical("Worker sent an unknown status: [%r]", new_status)
def _check_all_workers(self):
''' check if all worker daemons are still alive,
if not then re-start.'''
for worker in self.list_of_workers:
if not worker.get_process().is_alive():
logger.error("Process [%s] has died, restarting it...", worker.get_process().name)
worker.start()
def _get_redis_server(self, host, port):
''' Loookup the redis_class from a map. If not there,
creates and put in the map.'''
key = "%s:%d" % (host, port)
conn = None
if key in self.redis_servers_map:
conn = self.redis_servers_map[key]
else:
#.........这里部分代码省略.........
示例2: log
# 需要导入模块: from cluster import Cluster [as 别名]
# 或者: from cluster.Cluster import get_master [as 别名]
down = ((master_sync_in_progress == 0) and
(link_down_since_seconds > 30))
else:
down = False
except Exception as e:
log('master are down, or we were master, in any case we should not be here ', 'info')
down = True
if down:
log('down: find a new master!', 'info')
try:
master = r.info()['master_host']
log("master: {0}".format(master), 'info')
if cluster.exists(master):
grandmaster = cluster.get_master(master)
log("{0} = cluster.get_master({1})".format(grandmaster, master), 'info')
# and make sure the master doesn't participate anymore
cluster.incarcerate_node(master)
log("cluster.incarcerate_node({0})".format(master), 'info')
else:
grandmaster = cluster.get_master(node)
log("{0} = cluster.get_master({1})".format(grandmaster, node), 'info')
except:
log('we never were a slave', 'info')
grandmaster = None
if grandmaster == None:
r53_zone.update_record(cluster.name(), endpoint)
log("r53_zone.update_record({0}, {1})".format(cluster.name(), endpoint), 'info')
示例3: TestCluster
# 需要导入模块: from cluster import Cluster [as 别名]
# 或者: from cluster.Cluster import get_master [as 别名]
class TestCluster(unittest.TestCase):
def setUp(self):
print "### setUp ###"
self.cluster = Cluster()
def test_make_key(self):
print "test_make_key"
text = "%s:%s" % (HOST, PORT)
self.assertEqual(self.cluster._make_key(HOST, PORT), text)
def test_set_role(self):
self.cluster.add_node(host=HOST, port=PORT, role=ROLE_MASTER, status=REDIS_STATUS_OK)
self.cluster._set_role(HOST, PORT, ROLE_MASTER)
node = self.cluster.get_node(HOST, PORT)
self.assertTrue(node.is_master)
def test_filtered_list(self):
print "test_filtered_list"
self.cluster.add_node(host=HOST, port=PORT, role=ROLE_MASTER, status=REDIS_STATUS_OK)
self.cluster.add_node(host=HOST, port=PORT+1, role=ROLE_SLAVE, status=REDIS_STATUS_KO)
self.cluster.add_node(host=HOST, port=PORT+2, role=ROLE_SLAVE, status=REDIS_STATUS_OK)
testlist = self.cluster.filtered_list(roles=(ROLE_MASTER))
self.assertEqual(len(testlist), 1)
self.assertTrue(testlist[0].is_master())
self.assertTrue(testlist[0].is_alive())
testlist = self.cluster.filtered_list(roles=(ROLE_SLAVE))
self.assertEqual(len(testlist), 2)
self.assertTrue(testlist[0].is_slave() and testlist[1].is_slave())
testlist = self.cluster.filtered_list()
self.assertEqual(len(testlist), 3)
testlist = self.cluster.filtered_list(status=REDIS_STATUS_KO)
self.assertEqual(len(testlist), 1)
self.assertTrue(testlist[0].is_slave() and not testlist[0].is_alive())
testlist = self.cluster.filtered_list(status=REDIS_STATUS_OK)
self.assertEqual(len(testlist), 2)
testlist = self.cluster.filtered_list(roles=ROLE_MASTER, status=REDIS_STATUS_KO)
self.assertEqual(len(testlist), 0)
testlist = self.cluster.filtered_list(roles=(ROLE_MASTER,ROLE_SLAVE), status=REDIS_STATUS_KO)
self.assertEqual(len(testlist), 1)
self.assertTrue(testlist[0].is_slave() and not testlist[0].is_alive())
def test_add_single_master(self):
print "test_add_single_master"
self.cluster.add_node(host=HOST, port=PORT, role=ROLE_MASTER, status=REDIS_STATUS_OK)
self.assertEqual(len(self.cluster), 1)
node = self.cluster.get_master()
self.assertTrue(node.is_master() and node.is_alive())
def test_add_single_slave(self):
print "test_add_single_slave"
self.cluster.add_node(host=HOST, port=PORT, role=ROLE_SLAVE, status=REDIS_STATUS_OK)
self.assertEqual(len(self.cluster), 1)
self.assertIsNone(self.cluster.get_master())
node = self.cluster.get_node(host=HOST, port=PORT)
self.assertTrue(node.is_slave() and node.is_alive())
def test_promote_new_master_single_node(self):
print "test_promote_new_master_single_node"
old_master = self.cluster.add_node(host=HOST, port=PORT, role=ROLE_MASTER, status=REDIS_STATUS_OK)
self.assertEqual(len(self.cluster), 1)
self.cluster.promote_new_master(old_master)
node = self.cluster.get_master()
self.assertTrue(node is not None)
self.assertTrue(node.is_master())
self.assertFalse(node.is_alive())
def test_str(self):
print "test_str"
self.cluster.add_node(host=HOST, port=PORT, role=ROLE_MASTER, status=REDIS_STATUS_OK)
self.cluster.add_node(host=HOST, port=PORT+1, role=ROLE_SLAVE, status=REDIS_STATUS_OK)
self.assertEqual("(localhost:1000,master,OK),(localhost:1001,slave,OK)", str(self.cluster))
def _create_message(self, redis_host, redis_port, redis_status):
worker_name = '%s:%s' % (redis_host, redis_port)
text = "%s,%s" % (worker_name, redis_status)
return text
def tearDown(self):
print "### tearDown ###"
self.cluster = None
示例4: log
# 需要导入模块: from cluster import Cluster [as 别名]
# 或者: from cluster.Cluster import get_master [as 别名]
def log(message, logging='info'):
events.log(node, component, message, logging)
if __name__ == '__main__':
log('joining the cluster', 'info')
log('adding the node to the cluster', 'info')
# now we are ready to be (added to) the cluster
cluster.add_node(node, endpoint)
log('creating a Route53 records', 'info')
r53_zone.create_record(node, endpoint)
log('setting the tag', 'info')
ec2.set_tag(node)
log('getting the master of the node', 'info')
master = cluster.get_master(node)
# if we don't have a master, we ARE the master
if master == None:
log('setting the main Route53 record for the cluster', 'info')
r53_zone.update_record(cluster.name(), endpoint)
# and make sure we 'run' correctly (no-slave, well-monitored)
log('set the host to run as master', 'info')
host.set_master()
else:
# attach to the master (and start watching its availability)
log('set the host to run as slave of {0}'.format(master), 'info')
host.set_master(master)
log('joined the cluster', 'info')