Example #1
0
 def _run_catch(self, func, path, path_is_dir=False):
     """
     执行func并捕获异常,将文件系统异常转换为对应的异常对象
     """
     ospath = self._base + path
     try:
         if os.path.exists(ospath):
             if path_is_dir:
                 if os.path.isdir(ospath):
                     return func()
             else:
                 if os.path.isfile(ospath):
                     # 判断文件是否过期,过期直接报错
                     mtime = os.stat(ospath).st_mtime
                     if mtime < time.time() - self._timeout:
                         self.delete_node(path, True)
                     else:
                         return func()
                 else:
                     return func()
         raise exception.EPNoNodeError()
     except exception.EPNoNodeError as e:
         log.r(e, "Node not exist:{}".format(path))
     except Exception as e:
         log.r(exception.EPIOError(), "Request I/O Error")
Example #2
0
 def _run_catch(cls, func, path, path_is_dir=False):
     """
     执行func并捕获异常,将kazoo异常转换为对应的异常对象
     """
     try:
         if not os.path.exists(path) or (path_is_dir and not os.path.isdir(path)):
             raise exception.EPNoNodeError()
         return func()
     except exception.EPNoNodeError as e:
         log.r(e, "Node not exist:{}".format(path))
     except:
         log.r(exception.EPIOError(), "Request I/O Error")
Example #3
0
 def _run_catch(cls, func):
     """
     执行func并捕获异常,将kazoo异常转换为对应的异常对象
     """
     import kazoo
     try:
         return func()
     except kazoo.interfaces.IHandler.timeout_exception:
         raise exception.EPConnectTimeout()
     except kazoo.exceptions.NoNodeError:
         raise exception.EPNoNodeError()
     except:
         log.r(exception.EPIOError(), "Request I/O Error")
Example #4
0
    def create_node(self, path, value="", ephemeral=False, sequence=False, makepath=False):
        """
        根据节点各属性创建节点

        :param str path: 节点路径
        :param str value: 待存数据
        :param bool ephemeral: 是否是临时节点
        :param bool sequence: 是否是顺序节点
        :param bool makepath: 是否创建父节点
        :return: None
        :raises: exception.EPNoNodeError 节点不存在
        :raises: exception.EPIOError IO异常
        """
        path = self._base + path
        try:
            if ephemeral:
                # 临时节点,直接用文件来表示
                dirname = os.path.dirname(path)
                if not os.path.exists(dirname):
                    if makepath:
                        os.makedirs(dirname, self._mode)
                    else:
                        raise exception.EPNoNodeError
                file_path = path
                if sequence:
                    file_path = FilePersistence._seq_file_name(path)
                with open(file_path, 'w') as f:
                    f.write(value)
                with self._lock:
                    self._touch_paths[file_path] = ""
            else:
                # 实体节点,用目录来表示,数据存放在目录下的.data文件中
                if not os.path.exists(path):
                    if not os.path.exists(os.path.dirname(path)) and not makepath:
                        raise exception.EPNoNodeError
                    os.makedirs(path, self._mode)

                file_path = "/".join((path, ".data"))

                with open(file_path, 'w') as f:
                    f.write(value)
        except exception.EPNoNodeError as e:
            log.r(e, "Node not exist:{}".format(os.path.dirname(path)))
        except:
            log.r(exception.EPIOError(), "Request I/O Error")
        log.d("create node success, path:{path}, value:{value}, ephemeral:"
              "{ephemeral}, sequence:{sequence}, makepath:{makepath}".format(
                                                        path=path, value=value,
                                                        ephemeral=ephemeral, sequence=sequence,
                                                        makepath=makepath))
Example #5
0
 def _run_catch(cls, func):
     """
     执行func并捕获异常,将kazoo异常转换为对应的异常对象
     """
     import kazoo
     try:
         return func()
     except kazoo.exceptions.NoNodeError:
         raise exception.EPNoNodeError()
     except kazoo.exceptions.ZookeeperError:
         log.f("zk fail")
         raise exception.EPServerError()
     except Exception as e:
         log.r(exception.EPIOError(), "Requesst I/O Error")
Example #6
0
 def _run_catch(cls, func, path="", path_is_dir=False):
     """
     执行func并捕获异常,将kazoo异常转换为对应的异常对象
     """
     import redis
     # noinspection PyBroadException
     try:
         return func()
     except redis.exceptions.ConnectionError:
         raise exception.EPConnectTimeout()
     except exception.EPNoNodeError as e:
         raise e
     except Exception as e:
         log.r(exception.EPIOError(), "Request I/O Error")
Example #7
0
        def _createnode(np=path):
            node_path, node_name = self._split_node_name(np)
            errmsg = {
                -1: "Node has ttl or parents-node not exists",
                -2: "Node exists(in parents-node record)",
                -3: "Node exists"
            }

            if not self.exists(node_path) and node_path != "/":
                if makepath:
                    self.create_node(node_path, makepath=True)
                else:
                    raise exception.EPNoNodeError(node_path + " not exists")
            seq = 1 if sequence else 0
            tm = long(time.time()) + self._timeout if ephemeral else 0
            ret = self._handle.evalsha(self._new_lua_sha, 2, node_path,
                                       node_name, value, seq, tm)
            if ret < 0:
                raise exception.EPIOError("redis error when create[%s:%s]:%s" %
                                          (node_path, node_name, errmsg[ret]))
            if ephemeral:
                self._new_touch(ret)
            return ret