Пример #1
0
class ServiceLocator:
    """
    Give the node information. 
    """
    def __init__(self, osd_dir, logger):
        """
        Constructor for UpdaterRing.
        """
        self.container_ring = ContainerRing(osd_dir, logger)
        self.account_ring = AccountRing(osd_dir, logger)
        self.logger = logger
        self.msg = GlobalVariables(self.logger)
        self.shift_param = 512

    def get_service_details(self, service_obj):
        node = {}
        node['ip'] = service_obj.get_ip()
        node['port'] = service_obj.get_port()
        return node

    def get_container_from_ring(self, account_hash, container_hash):
        """get container node info from ring"""
        #TODO: Needs to modify to get complete node info(i.e. fs, dir)
        comp_no = Calculation.evaluate(container_hash, self.shift_param) - 1
        node = self.get_service_details(\
            self.msg.get_container_map()[comp_no])
        node['fs'] = self.container_ring.get_filesystem()
        node['dir'] = self.get_cont_dir_by_hash(\
            account_hash, container_hash)
        return node

    def get_account_from_ring(self, account_hash):
        """get account node info from ring"""
        #TODO: Needs to modify to get complete node info(i.e. fs)
        comp_no = Calculation.evaluate(account_hash, self.shift_param) - 1
        node = self.get_service_details(\
            self.msg.get_account_map()[comp_no])
        node['fs'] = self.account_ring.get_filesystem()
        node['account'] = account_hash
        node['dir'] = self.get_acc_dir_by_hash(account_hash)
        return node

    def get_acc_dir_by_hash(self, key):
        return self.account_ring.get_directory_by_hash(key)

    def get_cont_dir_by_hash(self, acc_hash, cont_hash):
        return self.container_ring.get_directory_by_hash(acc_hash, cont_hash)
Пример #2
0
class AccountInfo:
    """
    Store the account information
    """
    def __init__(self, account_name, conf, logger):
        """
        Constructor for Account

        :param account_name: account name
        """
        self.account_name = account_name
        self.account_map = {}
        self.acc_update_failed = False
        self.__container_list = []
        self.container_ring = ContainerRing(conf['osd_dir'], logger)
        self.stat_info = {}
        self.conf = conf
        self.logger = logger
        self.record_instance = []
        #self.logger.debug("AccountInfo constructed")
        self.__stat_reader_max_count = int(conf['reader_max_count'])
        self.connection_creator = ConnectionCreator(self.conf, self.logger)

    def getAccountName(self):
        """
        Return account name
        """
        return self.account_name

    def is_container(self, container):
        """
        Checking if container exists in container_list

        :param container : container name
        """
        if container in self.__container_list:
            return True

    def add_container(self, container):
        """
        Adding container in container_list

        :param container: container name
        """
        self.__container_list.append(container)

    def remove_container(self):
        """
        Removing all containers from container_list
        """
        self.__container_list = []

    def get_container_path(self):
        """
        Getting container path for HEAD request.
        """
        for container_name in self.__container_list:
            #node, fs, dir, gl_version, comp_no  = \
            #    self.container_ring.get_node(self.account_name, container_name)
            if self.account_map.get((self.account_name, container_name)) == "success":
                continue
            fs = self.container_ring.get_filesystem()
            self.__container_path = os.path.join('/', fs, \
                self.container_ring.get_directory_by_hash(\
                self.account_name, container_name), \
                self.account_name, container_name)
            self.logger.debug('Path for account: %s and container: %s is %s' \
                %(self.account_name, container_name, self.__container_path))
            container_instance = ContainerStatReader(
                                                container_name,
                                                self.__container_path,
                                                self.account_name,
                                                self.conf,
                                                self.logger
                                                )
            self.record_instance.append(container_instance)
            self.account_map[self.account_name, container_name] = "start"
        self.logger.debug('Exit from set_account_info')

    def execute(self):
        """
        Read the container stat info.
        """
        self.logger.debug('Enter in execute')
        thread_id = []
        thread_count = 0
        for container_record in self.record_instance:
            if container_record.is_completed():
                self.logger.debug("Already performed HEAD on:%s" %(repr(container_record)))
                continue
            if thread_count <= self.__stat_reader_max_count:
                reader_thread = Thread(target=container_record.\
                    read_container_stat_info, args=(self.account_map, ))
                reader_thread.start()
                thread_id.append(reader_thread)
                thread_count = thread_count +1
                if thread_count >= self.__stat_reader_max_count:
                    for thread in thread_id:
                        if thread.is_alive():
                            thread.join()
                    thread_count = 0
        for thread in thread_id:
            if thread.is_alive():
                thread.join()
        self.logger.info('Number of container stat reads: %s' \
            % len(self.record_instance))