示例#1
0
class UserInfoMgr(object):
    USER_INFO_UPDATE_INTERVAL = 5

    def __init__(self, env):
        self.env = env
        self._user_info = []
        self._update_task = gevent.spawn(self._update)
        self.file_lock = FileLock(self.env.lock_file())

    def user_info(self):
        return self._user_info

    def _update(self):
        while True:
            try:
                self.file_lock.lock()
                f = open(self.env.user_info_file())
                s = f.read()
                if len(s) != 0:
                    self._user_info = pickle.loads(s)
                f.close()
                self.file_lock.unlock()
            except IOError:
                os.mknod(self.env.lock_file())

            time.sleep(UserInfoMgr.USER_INFO_UPDATE_INTERVAL)
示例#2
0
class UserInfoExport(object):
    def __init__(self, env):
        self.env = env
        self.lock = FileLock(self.env.lock_file())

    def export(self, user_info):
        self.lock.lock()
        f = open(self.env.user_info_file(), "w")
        pickle.dump(user_info, f)
        f.close()
        self.lock.unlock()
示例#3
0
    def POST(self, request):
        logging.debug("proxy.post")

        proxy_nodes = []
        storage_nodes = []
        self.get_nodes(proxy_nodes, storage_nodes)
        print("storage_node: %s" % storage_nodes)
        print("proxy_node: %s" % proxy_nodes)

        #request.showVariables()
        response = {"headers": [], "status": "201 OK", "body": ""}

        if not request.file_name:
            response['status'] = http_code['400']
            return response

        if not self.group.is_leader:
            response['status'] = http_code['304']
            response['body'] = "No leader. Try to find the leader"
            logging.debug("I'm no leader. Try to find the leader")
            return response

        #client_id = request.metadata.get("HTTP_CLIENT_ID", "test:test")
        #logging.debug("HTTP_CLIENT_ID: %s" % client_id)

        replicas_count = request.metadata["HTTP_REPLICAS_COUNT"]
        metadata = MetadataManager(self.data_path + "/" + request.file_name,
                                   request.file_name,
                                   request.metadata["HTTP_DATA_LENGTH"],
                                   replicas_count,
                                   request.metadata["HTTP_ETAG"])


        if not metadata.restore():
            response['status'] = http_code['304']
            response['body'] = "File does not exist"
            logging.debug("File does not exist")
            return response

        logging.debug(self.data_path + "/" + request.file_name)
        fl = FileLock(self.data_path + "/" + request.file_name)
        if metadata.lock_list:
            metadata.lock_list.pop()
        metadata.save()
        fl.unlock()

        logging.debug("self.proxy_nodes: %s" % self.proxy_nodes)
        for proxy_replicas in proxy_nodes:
            if proxy_replicas[0] == self.port_ip[0] and\
                proxy_replicas[1] == self.port_ip[1]:
                    continue

            headers = {}
            headers['file_name'] = request.file_name
            headers['data_length'] = request.content_length
            headers['etag'] = request.file_name
            headers['replicas_count'] = replicas_count
            headers['client_id'] = "%s:%s" % (request.remote_addr,
                                              request.remote_port)

            headers["replication"] = "1"
            metadata_copy = json.dumps(metadata.resource_dict)
            response = self._put_request(request.file_name,
                                         proxy_replicas,
                                         data=metadata_copy,
                                         headers=headers)
            logging.debug("response: %s" % response)

        return response