当前位置: 首页>>代码示例>>Python>>正文


Python Cluster.promote_new_master方法代码示例

本文整理汇总了Python中cluster.Cluster.promote_new_master方法的典型用法代码示例。如果您正苦于以下问题:Python Cluster.promote_new_master方法的具体用法?Python Cluster.promote_new_master怎么用?Python Cluster.promote_new_master使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在cluster.Cluster的用法示例。


在下文中一共展示了Cluster.promote_new_master方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: RedisMonitor

# 需要导入模块: from cluster import Cluster [as 别名]
# 或者: from cluster.Cluster import promote_new_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:
#.........这里部分代码省略.........
开发者ID:AloneRoad,项目名称:redis_failover,代码行数:103,代码来源:redis_monitor.py

示例2: TestCluster

# 需要导入模块: from cluster import Cluster [as 别名]
# 或者: from cluster.Cluster import promote_new_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
开发者ID:AloneRoad,项目名称:redis_failover,代码行数:83,代码来源:test_cluster.py


注:本文中的cluster.Cluster.promote_new_master方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。