Beispiel #1
0
    def acquire(self, blocking=True):
        blocking, timeout = utils.convert_blocking(blocking)
        watch = timeutils.StopWatch(duration=timeout)
        watch.start()

        # Make the shared barrier ours first.
        with self._barrier.cond:
            while self._barrier.owner is not None:
                if not blocking or watch.expired():
                    return False
                self._barrier.cond.wait(watch.leftover(return_none=True))
            self._barrier.owner = (threading.current_thread().ident,
                                   os.getpid(), self._member_id)

        # Ok at this point we are now working in a thread safe manner,
        # and now we can try to get the actual lock...
        gotten = False
        try:
            gotten = self._lock.acquire(
                blocking=blocking,
                # Since the barrier waiting may have
                # taken a long time, we have to use
                # the leftover (and not the original).
                timeout=watch.leftover(return_none=True))
        finally:
            # NOTE(harlowja): do this in a finally block to **ensure** that
            # we release the barrier if something bad happens...
            if not gotten:
                # Release the barrier to let someone else have a go at it...
                with self._barrier.cond:
                    self._barrier.owner = None
                    self._barrier.cond.notify_all()

        self.acquired = gotten
        return gotten
Beispiel #2
0
    def acquire(self, blocking=True):
        blocking, timeout = utils.convert_blocking(blocking)
        watch = timeutils.StopWatch(duration=timeout)
        watch.start()

        # Make the shared barrier ours first.
        with self._barrier.cond:
            while self._barrier.owner is not None:
                if not blocking or watch.expired():
                    return False
                self._barrier.cond.wait(watch.leftover(return_none=True))
            self._barrier.owner = (threading.current_thread().ident,
                                   os.getpid(), self._member_id)

        # Ok at this point we are now working in a thread safe manner,
        # and now we can try to get the actual lock...
        gotten = False
        try:
            gotten = self._lock.acquire(
                blocking=blocking,
                # Since the barrier waiting may have
                # taken a long time, we have to use
                # the leftover (and not the original).
                timeout=watch.leftover(return_none=True))
        finally:
            # NOTE(harlowja): do this in a finally block to **ensure** that
            # we release the barrier if something bad happens...
            if not gotten:
                # Release the barrier to let someone else have a go at it...
                with self._barrier.cond:
                    self._barrier.owner = None
                    self._barrier.cond.notify_all()

        self.acquired = gotten
        return gotten
Beispiel #3
0
 def acquire(self, blocking=True):
     blocking, timeout = utils.convert_blocking(blocking)
     with _translate_failures():
         acquired = self._lock.acquire(blocking=blocking,
                                       blocking_timeout=timeout)
         if acquired:
             self._coord._acquired_locks.add(self)
         return acquired
Beispiel #4
0
 def acquire(self, blocking=True):
     blocking, timeout = utils.convert_blocking(blocking)
     with _translate_failures():
         self.acquired = self._lock.acquire(
             blocking=blocking, blocking_timeout=timeout)
         if self.acquired:
             self._coord._acquired_locks.add(self)
         return self.acquired
Beispiel #5
0
 def acquire(self, blocking=True, shared=False):
     if shared:
         raise tooz.NotImplemented
     blocking, timeout = utils.convert_blocking(blocking)
     with _translate_failures():
         acquired = self._lock.acquire(blocking=blocking,
                                       blocking_timeout=timeout)
         if acquired:
             with self._exclusive_access:
                 self._coord._acquired_locks.add(self)
         return acquired
Beispiel #6
0
 def acquire(self, blocking=True, shared=False):
     if shared:
         raise tooz.NotImplemented
     blocking, timeout = utils.convert_blocking(blocking)
     with _translate_failures():
         acquired = self._lock.acquire(
             blocking=blocking, blocking_timeout=timeout)
         if acquired:
             with self._exclusive_access:
                 self._coord._acquired_locks.add(self)
         return acquired
Beispiel #7
0
    def acquire(self, blocking=True, shared=False):
        if shared:
            raise tooz.NotImplemented

        blocking, timeout = utils.convert_blocking(blocking)
        if timeout is not None:
            watch = timeutils.StopWatch(duration=timeout)
            watch.start()
        else:
            watch = None

        while True:
            if self.acquired:
                # We already acquired the lock. Just go ahead and wait for ever
                # if blocking != False using the last index.
                lastindex = self._node['modifiedIndex']
            else:
                try:
                    reply = self.client.put(
                        self._lock_url,
                        make_url=False,
                        timeout=watch.leftover() if watch else None,
                        data={
                            "ttl": self.ttl,
                            "prevExist": "false"
                        })
                except requests.exceptions.RequestException:
                    if not watch or watch.leftover() == 0:
                        return False

                # We got the lock!
                if reply.get("errorCode") is None:
                    with self._lock:
                        self._node = reply['node']
                        self.coord._acquired_locks.add(self)
                    return True

                # No lock, somebody got it, wait for it to be released
                lastindex = reply['index'] + 1

            # We didn't get the lock and we don't want to wait
            if not blocking:
                return False

            # Ok, so let's wait a bit (or forever!)
            try:
                reply = self.client.get(
                    self._lock_url + "?wait=true&waitIndex=%d" % lastindex,
                    make_url=False,
                    timeout=watch.leftover() if watch else None)
            except requests.exceptions.RequestException:
                if not watch or watch.expired():
                    return False
Beispiel #8
0
    def acquire(self, blocking=True, shared=False):
        if shared:
            raise tooz.NotImplemented

        blocking, timeout = utils.convert_blocking(blocking)
        if timeout is not None:
            watch = timeutils.StopWatch(duration=timeout)
            watch.start()
        else:
            watch = None

        while True:
            if self.acquired:
                # We already acquired the lock. Just go ahead and wait for ever
                # if blocking != False using the last index.
                lastindex = self._node['modifiedIndex']
            else:
                try:
                    reply = self.client.put(
                        self._lock_url,
                        make_url=False,
                        timeout=watch.leftover() if watch else None,
                        data={"ttl": self.ttl,
                              "prevExist": "false"})
                except requests.exceptions.RequestException:
                    if not watch or watch.leftover() == 0:
                        return False

                # We got the lock!
                if reply.get("errorCode") is None:
                    with self._lock:
                        self._node = reply['node']
                        self.coord._acquired_locks.add(self)
                    return True

                # No lock, somebody got it, wait for it to be released
                lastindex = reply['index'] + 1

            # We didn't get the lock and we don't want to wait
            if not blocking:
                return False

            # Ok, so let's wait a bit (or forever!)
            try:
                reply = self.client.get(
                    self._lock_url +
                    "?wait=true&waitIndex=%d" % lastindex,
                    make_url=False,
                    timeout=watch.leftover() if watch else None)
            except requests.exceptions.RequestException:
                if not watch or watch.expired():
                    return False
Beispiel #9
0
    def acquire(self, blocking=True, shared=False):
        if shared:
            raise tooz.NotImplemented

        blocking, timeout = utils.convert_blocking(blocking)
        if blocking is False:
            timeout = 0

        if self._lock.acquire(timeout):
            self._coord._acquired_locks.add(self)
            return True

        return False
Beispiel #10
0
    def acquire(self, blocking=True, shared=False):
        if shared:
            raise tooz.NotImplemented

        blocking, timeout = utils.convert_blocking(blocking)
        if blocking is False:
            timeout = 0

        if self._lock.acquire(timeout):
            self._coord._acquired_locks.add(self)
            return True

        return False
Beispiel #11
0
    def acquire(self, blocking=True, shared=False):
        if shared:
            raise tooz.NotImplemented

        if (blocking is not True
                and sysv_ipc.SEMAPHORE_TIMEOUT_SUPPORTED is False):
            raise tooz.NotImplemented("This system does not support"
                                      " semaphore timeouts")
        blocking, timeout = utils.convert_blocking(blocking)
        start_time = None
        if not blocking:
            timeout = 0
        elif blocking and timeout is not None:
            start_time = time.time()
        while True:
            tmplock = None
            try:
                tmplock = sysv_ipc.Semaphore(self.key,
                                             flags=sysv_ipc.IPC_CREX,
                                             initial_value=1)
                tmplock.undo = True
            except sysv_ipc.ExistentialError:
                # We failed to create it because it already exists, then try to
                # grab the existing one.
                try:
                    tmplock = sysv_ipc.Semaphore(self.key)
                    tmplock.undo = True
                except sysv_ipc.ExistentialError:
                    # Semaphore has been deleted in the mean time, retry from
                    # the beginning!
                    continue

            if start_time is not None:
                elapsed = max(0.0, time.time() - start_time)
                if elapsed >= timeout:
                    # Ran out of time...
                    return False
                adjusted_timeout = timeout - elapsed
            else:
                adjusted_timeout = timeout
            try:
                tmplock.acquire(timeout=adjusted_timeout)
            except sysv_ipc.BusyError:
                tmplock = None
                return False
            except sysv_ipc.ExistentialError:
                # Likely the lock has been deleted in the meantime, retry
                continue
            else:
                self._lock = tmplock
                return True
Beispiel #12
0
    def acquire(self, blocking=True, shared=False):
        if shared:
            raise tooz.NotImplemented

        if (blocking is not True and
                sysv_ipc.SEMAPHORE_TIMEOUT_SUPPORTED is False):
            raise tooz.NotImplemented("This system does not support"
                                      " semaphore timeouts")
        blocking, timeout = utils.convert_blocking(blocking)
        start_time = None
        if not blocking:
            timeout = 0
        elif blocking and timeout is not None:
            start_time = time.time()
        while True:
            tmplock = None
            try:
                tmplock = sysv_ipc.Semaphore(self.key,
                                             flags=sysv_ipc.IPC_CREX,
                                             initial_value=1)
                tmplock.undo = True
            except sysv_ipc.ExistentialError:
                # We failed to create it because it already exists, then try to
                # grab the existing one.
                try:
                    tmplock = sysv_ipc.Semaphore(self.key)
                    tmplock.undo = True
                except sysv_ipc.ExistentialError:
                    # Semaphore has been deleted in the mean time, retry from
                    # the beginning!
                    continue

            if start_time is not None:
                elapsed = max(0.0, time.time() - start_time)
                if elapsed >= timeout:
                    # Ran out of time...
                    return False
                adjusted_timeout = timeout - elapsed
            else:
                adjusted_timeout = timeout
            try:
                tmplock.acquire(timeout=adjusted_timeout)
            except sysv_ipc.BusyError:
                tmplock = None
                return False
            except sysv_ipc.ExistentialError:
                # Likely the lock has been deleted in the meantime, retry
                continue
            else:
                self._lock = tmplock
                return True
Beispiel #13
0
    def acquire(self, blocking=True):
        blocking, timeout = utils.convert_blocking(blocking)
        if timeout is not None:
            watch = timeutils.StopWatch(duration=timeout)
            watch.start()
        else:
            watch = None

        while True:
            try:
                reply = self.client.put(
                    self._lock_url,
                    make_url=False,
                    timeout=watch.leftover() if watch else None,
                    data={
                        "ttl": self.ttl,
                        "prevExist": "false"
                    })
            except requests.exceptions.RequestException:
                if watch and watch.leftover() == 0:
                    return False

            # We got the lock!
            if reply.get("errorCode") is None:
                self._node = reply['node']
                self.coord._acquired_locks.append(self)
                return True

            # We didn't get the lock and we don't want to wait
            if not blocking:
                return False

            # Ok, so let's wait a bit (or forever!)
            try:
                reply = self.client.get(
                    self._lock_url + "?wait=true&waitIndex=%d" %
                    (reply['index'] + 1),
                    make_url=False,
                    timeout=watch.leftover() if watch else None)
            except requests.exceptions.RequestException:
                if watch and watch.expired():
                    return False
Beispiel #14
0
    def acquire(self, blocking=True):
        blocking, timeout = utils.convert_blocking(blocking)
        if timeout is not None:
            watch = timeutils.StopWatch(duration=timeout)
            watch.start()
        else:
            watch = None

        while True:
            try:
                reply = self.client.put(
                    self._lock_url,
                    make_url=False,
                    timeout=watch.leftover() if watch else None,
                    data={"ttl": self.ttl,
                          "prevExist": "false"})
            except requests.exceptions.RequestException:
                if watch and watch.leftover() == 0:
                    return False

            # We got the lock!
            if reply.get("errorCode") is None:
                self._node = reply['node']
                self.coord._acquired_locks.append(self)
                return True

            # We didn't get the lock and we don't want to wait
            if not blocking:
                return False

            # Ok, so let's wait a bit (or forever!)
            try:
                reply = self.client.get(
                    self._lock_url +
                    "?wait=true&waitIndex=%d" % reply['index'],
                    make_url=False,
                    timeout=watch.leftover() if watch else None)
            except requests.exceptions.RequestException:
                if watch and watch.expired():
                    return False
Beispiel #15
0
 def acquire(self, blocking=True):
     blocking, timeout = utils.convert_blocking(blocking)
     return self._lock.acquire(blocking=blocking,
                               timeout=timeout)
Beispiel #16
0
 def acquire(self, blocking=True):
     blocking, timeout = utils.convert_blocking(blocking)
     return self._lock.acquire(blocking=blocking,
                               timeout=timeout)
Beispiel #17
0
 def acquire(self, blocking=True, shared=False):
     if shared:
         raise tooz.NotImplemented
     blocking, timeout = utils.convert_blocking(blocking)
     return self._lock.acquire(blocking=blocking, timeout=timeout)
Beispiel #18
0
 def acquire(self, blocking=True, shared=False):
     if shared:
         raise tooz.NotImplemented
     blocking, timeout = utils.convert_blocking(blocking)
     return self._lock.acquire(blocking=blocking,
                               timeout=timeout)