Beispiel #1
0
    def test_hashring2(self):
        memcache_servers1 = [
            '192.168.0.246:11212', '192.168.0.247:11212',
            '192.168.0.249:11212', '192.168.0.250:11212',
            '192.168.0.251:11212', '192.168.0.252:11212',
            '192.168.0.253:11212', '192.168.0.254:11212',
            '192.168.0.255:11212', '192.168.0.256:11212'
        ]

        ring1 = HashRing(memcache_servers1)
        my_key = "zhanchenjinssssss"
        ring1.get_node("aaaa")
        my_value1 = ring1.get_node(my_key)

        memcache_servers2 = [
            '192.168.0.246:11212', '192.168.0.247:11212',
            '192.168.0.249:11212', '192.168.0.250:11212',
            '192.168.0.251:11212', '192.168.0.252:11212',
            '192.168.0.253:11212', '192.168.0.254:11212',
            '192.168.0.255:11212', '192.168.0.256:11212'
        ]

        ring2 = HashRing(memcache_servers2)
        ring2.get_node("aaaa")
        my_value2 = ring2.get_node(my_key)
        self.assertTrue(my_value1 == my_value2)
    def test_hashring_self_next_node(self):
        """
    判断是否有屏蔽到自己的下一个节点
        """

        memcache_servers = ['192.168.0.246:11212']

        ring = HashRing(memcache_servers, replicas=1)
        test_str = random_str()

        select_node = ring.get_node(test_str)
        next_nodes = ring.next_nodes(select_node)
        self.assertTrue(not next_nodes)
Beispiel #3
0
    def test_hashring_self_next_node(self):
        """
    判断是否有屏蔽到自己的下一个节点
        """

        memcache_servers = ['192.168.0.246:11212']

        ring = HashRing(memcache_servers, replicas=1)
        test_str = random_str()

        select_node = ring.get_node(test_str)
        next_nodes = ring.next_nodes(select_node)
        self.assertTrue(not next_nodes)
    def test_hashring2(self):
        memcache_servers1 = ['192.168.0.246:11212',
                            '192.168.0.247:11212',
                            '192.168.0.249:11212',
                            '192.168.0.250:11212',
                            '192.168.0.251:11212',
                            '192.168.0.252:11212',
                            '192.168.0.253:11212',
                            '192.168.0.254:11212',
                            '192.168.0.255:11212',
                            '192.168.0.256:11212']

        ring1 = HashRing(memcache_servers1)
        my_key = "zhanchenjinssssss"
        ring1.get_node("aaaa")
        my_value1 = ring1.get_node(my_key)

        memcache_servers2 = ['192.168.0.246:11212',
                            '192.168.0.247:11212',
                            '192.168.0.249:11212',
                            '192.168.0.250:11212',
                            '192.168.0.251:11212',
                            '192.168.0.252:11212',
                            '192.168.0.253:11212',
                            '192.168.0.254:11212',
                            '192.168.0.255:11212',
                            '192.168.0.256:11212']

        ring2 = HashRing(memcache_servers2)
        ring2.get_node("aaaa")
        my_value2 = ring2.get_node(my_key)
        self.assertTrue(my_value1 == my_value2)
Beispiel #5
0
    def test_hashring1(self):
        memcache_servers = [
            '192.168.0.246:11212', '192.168.0.247:11212', '192.168.0.249:11212'
        ]

        ring = HashRing(memcache_servers)
        my_key = "zhanchenjinssssss"
        self.assertTrue(
            ring.get_node(my_key) == ring.get_node(my_key) == ring.get_node(
                my_key) == ring.get_node(my_key))

        my_key = "gggggggggggggg"
        self.assertTrue(
            ring.get_node(my_key) == ring.get_node(my_key) == ring.get_node(
                my_key) == ring.get_node(my_key))
    def test_hashring1(self):
        memcache_servers = ['192.168.0.246:11212',
                            '192.168.0.247:11212',
                            '192.168.0.249:11212']

        ring = HashRing(memcache_servers)
        my_key = "zhanchenjinssssss"
        self.assertTrue(ring.get_node(my_key) ==
                        ring.get_node(my_key) ==
                        ring.get_node(my_key) ==
                        ring.get_node(my_key))

        my_key = "gggggggggggggg"
        self.assertTrue(ring.get_node(my_key) ==
                        ring.get_node(my_key) ==
                        ring.get_node(my_key) ==
                        ring.get_node(my_key))
Beispiel #7
0
    def test_hashring_has_node(self):
        """
    判断是否有节点
        """

        memcache_servers = ['192.168.0.246:11212']

        ring = HashRing(memcache_servers, replicas=1)
        self.assertTrue(ring.has_node('192.168.0.246:11212'))

        new_node = '192.168.0.247:11212'
        ring.add_node(new_node)
        self.assertTrue(ring.has_node(new_node))

        ilexist_node = '192.168.0.248:11212'
        self.assertTrue(not ring.has_node(ilexist_node))
    def test_hashring_has_node(self):
        """
    判断是否有节点
        """

        memcache_servers = ['192.168.0.246:11212']

        ring = HashRing(memcache_servers, replicas=1)
        self.assertTrue(ring.has_node('192.168.0.246:11212'))

        new_node = '192.168.0.247:11212'
        ring.add_node(new_node)
        self.assertTrue(ring.has_node(new_node))

        ilexist_node = '192.168.0.248:11212'
        self.assertTrue(not ring.has_node(ilexist_node))
Beispiel #9
0
    def test_hashring_next_nodes(self):
        """
    判断某个节点删除以后,HashRing给某个相同字符串的定位节点是否和HashRing的next_nodes函数取得的节点一致
        """

        memcache_servers = [
            '192.168.0.246:11212', '192.168.0.247:11212',
            '192.168.0.248:11212', '192.168.0.249:11212'
        ]

        text_cont = 30
        for i in xrange(text_cont):
            ring = HashRing(memcache_servers, replicas=1)

            test_str = random_str(i)

            select_node = ring.get_node(test_str)
            next_nodes = ring.next_nodes(select_node)
            ring.remove_node(select_node)
            reselect_node = ring.get_node(test_str)
            self.assertTrue(reselect_node in next_nodes)
Beispiel #10
0
    def test_hashring_next_nodes(self):
        """
    判断某个节点删除以后,HashRing给某个相同字符串的定位节点是否和HashRing的next_nodes函数取得的节点一致
        """

        memcache_servers = ['192.168.0.246:11212',
                            '192.168.0.247:11212',
                            '192.168.0.248:11212',
                            '192.168.0.249:11212']

        text_cont = 30
        for i in xrange(text_cont):
            ring = HashRing(memcache_servers, replicas=1)

            test_str = random_str(i)

            select_node = ring.get_node(test_str)
            next_nodes = ring.next_nodes(select_node)
            ring.remove_node(select_node)
            reselect_node = ring.get_node(test_str)
            self.assertTrue(reselect_node in next_nodes)
Beispiel #11
0
 def test_hashring_none(self):
     ring1 = HashRing([])
     my_key = "zhanchenjinssssss"
     self.assertTrue(ring1.get_node(my_key) == None)
Beispiel #12
0
 def __init__(self):
     # 虚拟节点必须只有1个,如果多个的话,虽然增加了分布式,但是同时该节点故障的话,也影响多个虚拟节点,进而影响多个实体节点的数据更新
     self.__hash_ring = HashRing(replicas=1)
Beispiel #13
0
class ServiceCluster(object):
    service_mgr = None

    def __init__(self):
        # 虚拟节点必须只有1个,如果多个的话,虽然增加了分布式,但是同时该节点故障的话,也影响多个虚拟节点,进而影响多个实体节点的数据更新
        self.__hash_ring = HashRing(replicas=1)

    def add_service(self, service_obj, is_init=False):
        """
        添加一个服务对象到服务集群
        :param is_init:是否初始化,如果是初始化,无论如何,不清理缓存;否则考虑清理缓存
        """
        cur_node = service_obj.hash_key()
        if self.__hash_ring.has_node(cur_node):
            logger.warn(
                "ServiceCluster::add_service failed, node has exist!!!, service_obj:%s"
                % service_obj)
            return

        self.__hash_ring.add_node(cur_node)

        if not is_init:
            self._do_effect_nodes(cur_node)
        logger.warn(
            "ServiceCluster::add_service success, service_obj:%s cur_node:%s" %
            (service_obj, cur_node))

    def del_service(self, service_obj):
        """
        从服务集群里面删除一个服务
        """
        cur_node = service_obj.hash_key()
        if not self.__hash_ring.has_node(cur_node):
            logger.warn(
                "ServiceCluster::del_service, node not exist!!!, service_obj:%s cur_node:%s"
                % (service_obj, cur_node))
            return

        self.__hash_ring.remove_node(service_obj.hash_key())
        logger.warn(
            "ServiceCluster::del_service success, service_obj:%s cur_node:%s" %
            (service_obj, cur_node))

    def get_service_objs(self, string_key_ls):
        """
        根据key列表从服务集群获取服务对象列表
        :param string_key_ls: 筛选的key列表
        :return: [service_obj]
        """
        assert isinstance(string_key_ls, list)
        nodes = [self.__hash_ring.get_node(key) for key in string_key_ls]
        return self.service_mgr.get_services_by_hash_keys(nodes)

    def _do_effect_nodes(self, cur_node):
        """
        处理被影响的节点的缓存
        :param cur_node:
        :return:
        """
        eff_nodes = self.__hash_ring.next_nodes(cur_node)
        eff_service_objs = self.service_mgr.get_services_by_hash_keys(
            eff_nodes)

        # 将被影响的下一个节点清除缓存
        for service_obj in eff_service_objs:
            if service_obj.control_rpc:
                logger.info(
                    "ServiceCluster::_do_effect_nodes, clear_cache, service_obj:%s "
                    % service_obj)
                service_obj.control_rpc.clear_cache()
            else:
                logger.warn(
                    "ServiceCluster::_do_effect_nodes, control_rpc not exist!!!, service_obj:%s "
                    % service_obj)
Beispiel #14
0
 def test_hashring_none(self):
     ring1 = HashRing([])
     my_key = "zhanchenjinssssss"
     self.assertTrue(ring1.get_node(my_key) == None)
 def __init__(self):
     # 虚拟节点必须只有1个,如果多个的话,虽然增加了分布式,但是同时该节点故障的话,也影响多个虚拟节点,进而影响多个实体节点的数据更新
     self.__hash_ring = HashRing(replicas=1)
class ServiceCluster(object):
    service_mgr = None

    def __init__(self):
        # 虚拟节点必须只有1个,如果多个的话,虽然增加了分布式,但是同时该节点故障的话,也影响多个虚拟节点,进而影响多个实体节点的数据更新
        self.__hash_ring = HashRing(replicas=1)

    def add_service(self, service_obj, is_init=False):
        """
        添加一个服务对象到服务集群
        :param is_init:是否初始化,如果是初始化,无论如何,不清理缓存;否则考虑清理缓存
        """
        cur_node = service_obj.hash_key()
        if self.__hash_ring.has_node(cur_node):
            logger.warn("ServiceCluster::add_service failed, node has exist!!!, service_obj:%s" % service_obj)
            return

        self.__hash_ring.add_node(cur_node)

        if not is_init:
            self._do_effect_nodes(cur_node)
        logger.warn("ServiceCluster::add_service success, service_obj:%s cur_node:%s" % (service_obj, cur_node))

    def del_service(self, service_obj):
        """
        从服务集群里面删除一个服务
        """
        cur_node = service_obj.hash_key()
        if not self.__hash_ring.has_node(cur_node):
            logger.warn("ServiceCluster::del_service, node not exist!!!, service_obj:%s cur_node:%s" % (service_obj, cur_node))
            return

        self.__hash_ring.remove_node(service_obj.hash_key())
        logger.warn("ServiceCluster::del_service success, service_obj:%s cur_node:%s" % (service_obj, cur_node))

    def get_service_objs(self, string_key_ls):
        """
        根据key列表从服务集群获取服务对象列表
        :param string_key_ls: 筛选的key列表
        :return: [service_obj]
        """
        assert isinstance(string_key_ls, list)
        nodes = [self.__hash_ring.get_node(key) for key in string_key_ls]
        return self.service_mgr.get_services_by_hash_keys(nodes)

    def _do_effect_nodes(self, cur_node):
        """
        处理被影响的节点的缓存
        :param cur_node:
        :return:
        """
        eff_nodes = self.__hash_ring.next_nodes(cur_node)
        eff_service_objs = self.service_mgr.get_services_by_hash_keys(eff_nodes)

        # 将被影响的下一个节点清除缓存
        for service_obj in eff_service_objs:
            if service_obj.control_rpc:
                logger.info("ServiceCluster::_do_effect_nodes, clear_cache, service_obj:%s " % service_obj)
                service_obj.control_rpc.clear_cache()
            else:
                logger.warn("ServiceCluster::_do_effect_nodes, control_rpc not exist!!!, service_obj:%s " % service_obj)