Пример #1
0
    def acquire(self,
                namespace,
                name,
                locktype,
                timeout_ms,
                raiseonfailure=None):
        fullName = "%s.%s" % (namespace, name)

        if raiseonfailure is None:
            raiseonfailure = self.raiseonfailure

        manager = ResourceManager.getInstance()

        if timeout_ms is not None:
            timeout = timeout_ms / 1000.0

        self.lock.acquire()
        try:

            try:
                if fullName in self.resources:
                    raise ValueError(
                        "Owner %s: acquire: resource %s is already acquired" %
                        (str(self), fullName))

                try:
                    resource = manager.acquireResource(namespace, name,
                                                       locktype, timeout)
                    self.resources[resource.fullName] = resource

                    if hasattr(self.ownerobject, "resourceAcquired"):
                        self.ownerobject.resourceAcquired(
                            namespace, name, locktype)
                except RequestTimedOutError:
                    self.log.debug(
                        "%s: request for '%s' timed out after '%f' seconds" %
                        (str(self), fullName, timeout))
                    raise se.ResourceTimeout()
                except ValueError, ex:
                    self.log.debug(
                        "%s: request for '%s' could not be proccessed (%s)" %
                        (str(self), fullName, str(ex)))
                    raise se.InvalidResourceName()
                except KeyError, ex:
                    self.log.debug("%s: resource '%s' does not exist" %
                                   (str(self), fullName))
                except Exception, ex:
                    self.log.warn(
                        "Unexpected exception caught while owner '%s' tried to acquire '%s'"
                        % (str(self), fullName),
                        exc_info=True)
                    raise se.ResourceException()
Пример #2
0
    def register(self, namespace, name, locktype):
        fullName = "%s.%s" % (namespace, name)
        if fullName in self.resources:
            raise ValueError("Owner %s: acquire: resource %s is already "
                             "acquired" % (self, fullName))

        manager = ResourceManager.getInstance()

        self.lock.acquire()
        try:
            if fullName in self.requests:
                raise ValueError("request %s is already requested by %s" %
                                 (fullName, self))

            try:
                request = manager.registerResource(namespace, name, locktype,
                                                   self._onRequestFinished)
            except ValueError as ex:
                self.log.debug(
                    "%s: request for '%s' could not be processed "
                    "(%s)", self, fullName, ex)
                raise se.InvalidResourceName()
            except KeyError:
                self.log.debug("%s: resource '%s' does not exist", self,
                               fullName)
                raise se.ResourceDoesNotExist()
            except Exception:
                self.log.warn(
                    "Unexpected exception caught while owner '%s' "
                    "tried to acquire '%s'",
                    self,
                    fullName,
                    exc_info=True)
                raise se.ResourceException()

            if hasattr(self.ownerobject, "resourceRegistered"):
                self.ownerobject.resourceRegistered(namespace, name, locktype)

            self.requests[fullName] = request
        finally:
            self.lock.release()
        self.log.debug("%s: request registered %s", self, request)
Пример #3
0
                                                   self._onRequestFinished)
            except ValueError, ex:
                self.log.debug(
                    "%s: request for '%s' could not be processed (%s)" %
                    (str(self), fullName, str(ex)))
                raise se.InvalidResourceName()
            except KeyError, ex:
                self.log.debug("%s: resource '%s' does not exist" %
                               (str(self), fullName))
                raise se.ResourceDoesNotExist()
            except Exception, ex:
                self.log.warn(
                    "Unexpected exception caught while owner '%s' tried to acquire '%s'"
                    % (str(self), fullName),
                    exc_info=True)
                raise se.ResourceException()

            if hasattr(self.ownerobject, "resourceRegistered"):
                self.ownerobject.resourceRegistered(namespace, name, locktype)

            self.requests[fullName] = request
        finally:
            self.lock.release()
        self.log.debug("%s: request registered %s", str(self), str(request))

    def cancel(self, namespace, name):
        """
        Cancel a pending request. Note that cancel may race with grant and in this case
        it is not ensured that the resource is not owned. In addition it is not allowed to
        release any resources granted due to the above race - the owning thread may not be
        aware of this!