Beispiel #1
0
    def _mount(self):
        if self._children:
            self._mode |= MODE_VIRT

        if self._mode & MODE_VIRT or self._index == None:
            mode = None
            freq = None
            prof = None
        else:
            mode = self._mode
            freq = self._freq
            prof = self.d_profile
            path = get_mnt_path(self._uid, self._name)
            if os.path.exists(path):
                loader = Loader(self._uid)
                curr_prof = loader.get_profile(self._name)
                if curr_prof['type'] == prof['type']:
                    curr_mode = loader.get_attr(self._name, ATTR_MODE, int)
                    if ((curr_mode | MODE_SYNC) == (mode | MODE_SYNC)) and curr_prof.get('spec') == prof.get('spec'):
                        mode = None
                        freq = None
                        prof = None
                    else:
                        if not (curr_mode & MODE_SYNC):
                            mode &= ~MODE_SYNC
                        else:
                            mode |= MODE_SYNC
                        freq = loader.get_attr(self._name, ATTR_FREQ, float)

        if not self._children:
            api_mount(self._uid, name=self.d_name, mode=mode, freq=freq, prof=prof)
            self._log('mount %s [%s*]' % (self.d_type, self.d_name[:8]))
Beispiel #2
0
class Mode(object):
    def __init__(self, uid):
        self._mode = {}
        self._loader = Loader(uid)

    def _log(self, text):
        if LOG_MODE:
            log_debug(self, text)

    def _get(self, name):
        mode = self._loader.get_attr(name, ATTR_MODE, int)
        if mode != None:
            self._mode[name] = mode
            self._log('name=%s, mode=%s' % (str(name), str(mode)))
            return mode

    def get(self, name):
        if self._mode.has_key(name):
            ret = self._mode.get(name)
            if ret != None:
                return ret
        return self._get(name)

    def remove(self, name):
        if self._mode.has_key(name):
            del self._mode[name]
Beispiel #3
0
class Freq(object):
    def __init__(self, uid):
        self._freq = {}
        self._loader = Loader(uid)

    def _log(self, text):
        if LOG_FREQ:
            log_debug(self, text)

    def _get(self, name):
        freq = self._loader.get_attr(name, ATTR_FREQ, float)
        if freq:
            self._freq[name] = freq
            self._log('name=%s, freq=%s' % (str(name), str(freq)))
            return freq

    def get(self, name):
        if self._freq.has_key(name):
            return self._freq.get(name)
        else:
            return self._get(name)

    def remove(self, name):
        if self._freq.has_key(name):
            del self._freq[name]
Beispiel #4
0
class Handler(object):
    def __init__(self, uid, addr=PROC_ADDR):
        self._handlers = {}
        self._loader = Loader(uid)
        self._addr = addr

    def _get_code(self, name):
        buf = self._handlers.get(name)
        if not buf:
            buf = self._loader.get_attr(name, ATTR_HANDLER, str)
            self._handlers.update({name:buf})
        return buf

    def remove(self, name):
        if self._handlers.has_key(name):
            del self._handlers[name]

    def check(self, name):
        if self._handlers.get(name):
            return True
        else:
            buf = self._loader.get_attr(name, ATTR_HANDLER, str)
            if buf:
                self._handlers.update({name:buf})
                return True

    def put(self, name, buf):
        try:
            code = self._get_code(name)
            if code == None:
                code = self._get_code(name)
                if not code:
                    return
            return proc.put(self._addr, HANDLER_PORT, code, buf)
        except:
            log_err(self, 'failed to put')
Beispiel #5
0
class Parent(object):
    def __init__(self, uid):
        self._parent = {}
        self._loader = Loader(uid)

    def _get(self, name):
        parent = self._loader.get_attr(name, ATTR_PARENT, str)
        if parent != None:
            self._parent[name] = parent
            return parent

    def get(self, name):
        if self._parent.has_key(name):
            ret = self._parent.get(name)
            if ret != None:
                return ret
        return self._get(name)

    def remove(self, name):
        if self._parent.has_key(name):
            del self._parent[name]
Beispiel #6
0
class Timeout(object):
    def __init__(self, uid):
        self._timeout = {}
        self._loader = Loader(uid)

    def _get(self, name):
        timeout = self._loader.get_attr(name, ATTR_TIMEOUT, float)
        if timeout != None:
            self._timeout[name] = timeout
            return timeout

    def get(self, name):
        if self._timeout.has_key(name):
            ret = self._timeout.get(name)
            if ret != None:
                return ret
        return self._get(name)

    def remove(self, name):
        if self._timeout.has_key(name):
            del self._timeout[name]
Beispiel #7
0
class Dispatcher(object):
    def __init__(self, uid, channel, core, addr=PROC_ADDR):
        self._uid = uid
        self._queue = []
        self._paths = {}
        self._hidden = {}
        self._core = core
        self._addr = addr
        self._pool = None
        self._visible = {}
        self._dispatchers = {}
        self._channel = channel
        self._lock = NamedLock()
        self._loader = Loader(self._uid)
        self._pool = DispatcherPool(core)

    def _log(self, text):
        if LOG_DISPATCHER:
            log_debug(self, text)

    def _get_code(self, name):
        buf = self._dispatchers.get(name)
        if not buf:
            buf = self._loader.get_attr(name, ATTR_DISPATCHER, str)
            self._dispatchers.update({name:buf})
        return buf

    def _send(self, dest, src, buf, flags):
        self._log('send, dest=%s, src=%s' % (dest, src))
        self._channel.put(dest, src, buf=buf, flags=flags)

    def _put(self, dest, src, buf, flags):
        self._log('put, dest=%s, src=%s' % (dest, src))
        while not self._pool.put(dest, src, buf, flags):
            self._pool.wait()

    def _do_deliver(self, dest, src, buf, flags, local):
        if local:
            self._put(dest, src, buf, flags)
        else:
            self._send(dest, src, buf, flags)

    def _deliver_safe(self, dest, src, buf, flags, local):
        try:
            self._do_deliver(dest, src, buf, flags, local)
        except:
            log_err(self, 'failed to deliver, dest=%s, src=%s' % (str(dest), str(src)))

    def _deliver_unsafe(self, dest, src, buf, flags, local):
        self._do_deliver(dest, src, buf, flags, local)

    def _deliver(self, dest, src, buf, flags, local):
        if DEBUG and not SAFE:
            self._deliver_unsafe(dest, src, buf, flags, local)
        else:
            self._deliver_safe(dest, src, buf, flags, local)

    @edge_lock
    def add_edge(self, edge, hidden=False):
        src = edge[0]
        dest = edge[1]

        if hidden:
            paths = self._hidden
        else:
            paths = self._visible

        if paths.has_key(src) and paths[src].has_key(dest):
            return

        if not paths.has_key(src):
            paths[src] = {}

        if not self._paths.has_key(src):
            self._paths[src] = {}

        local = is_local(self._uid, dest)
        paths[src].update({dest:local})
        if not self._paths[src].has_key(dest):
            if not local:
                self._channel.allocate(dest)
            self._paths[src].update({dest:1})
        else:
            self._paths[src][dest] += 1
        self._log('add edge, dest=%s, src=%s, local=%s' % (dest, src, str(local)))

    @edge_lock
    def remove_edge(self, edge, hidden=False):
        src = edge[0]
        dest = edge[1]
        if hidden:
            paths = self._hidden
        else:
            paths = self._visible
        if not paths.has_key(src) or not paths[src].has_key(dest):
            return
        local = paths[src][dest]
        del paths[src][dest]
        self._paths[src][dest] -= 1
        if 0 == self._paths[src][dest]:
            del self._paths[src][dest]
            if not local:
                self._channel.free(dest)
        self._log('remove edge, dest=%s, src=%s, local=%s' % (dest, src, str(local)))

    @named_lock
    def has_edge(self, name):
        return self._visible.has_key(name)

    def update_edges(self, name, edges):
        if not edges:
            return
        for dest in edges:
            if dest.startswith('.'):
                dest = dest[1:]
            if dest != name:
                self.add_edge((name, dest))

    def remove_edges(self, name):
        paths = self._visible.get(name)
        for i in paths:
            self.remove_edge((name, i))
        paths = self._hidden.get(name)
        for i in paths:
            self.remove_edge((name, i), hidden=True)
        if self._dispatchers.has_key(name):
            del self._dispatchers[name]

    def remove(self, name):
        if self._dispatchers.has_key(name):
            del self._dispatchers[name]

    def sendto(self, dest, src, buf, hidden=False, flags=0):
        if not buf:
            return
        self._log('sendto, dest=%s, src=%s' % (dest, src))
        self.add_edge((src, dest), hidden=hidden)
        if self._hidden:
            local = self._hidden[src][dest]
        else:
            local = self._visible[src][dest]
        self._deliver(dest, src, buf, flags, local=local)

    def send(self, name, buf, flags=0):
        if not buf:
            return
        dest = self._visible.get(name)
        if not dest:
            return
        for i in dest:
            self._log('send, dest=%s, src=%s' % (i, name))
            self._deliver(i, name, buf, flags, local=dest[i])

    def send_blocks(self, name, blocks):
        if not blocks:
            return
        dest = self._visible.get(name)
        if not dest:
            return
        cnt = 0
        keys = dest.keys()
        len_keys = len(keys)
        len_blks = len(blocks)
        window = (len_blks + len_keys - 1) / len_keys
        start = randint(0, len_keys - 1)
        for _ in range(len_keys):
            i = keys[start]
            for _ in range(window):
                if blocks[cnt]:
                    self._log('send a block, dest=%s, src=%s' % (i, name))
                    self._deliver(i, name, blocks[cnt], 0, local=dest[i])
                cnt += 1
                if cnt == len_blks:
                    return
            start += 1
            if start == len_keys:
                start = 0

    def put(self, name, buf):
        try:
            code = self._get_code(name)
            if code == None:
                code = self._get_code(name)
                if not code:
                    return
            return proc.put(self._addr, DISPATCHER_PORT, code, buf)
        except:
            log_err(self, 'failed to put, name=%s' % (str(name)))

    def check(self, name):
        if self._dispatchers.get(name):
            return True
        else:
            buf = self._loader.get_attr(name, ATTR_DISPATCHER, str)
            if buf:
                self._dispatchers.update({name:buf})
                return True