def test_scope_queue_name(self):
        self.assertEqual(utils.scope_queue_name('my-q'), '.my-q')
        self.assertEqual(utils.scope_queue_name('my-q', None), '.my-q')
        self.assertEqual(utils.scope_queue_name('my-q', '123'), '123.my-q')

        self.assertEqual(utils.scope_queue_name(None), '.')
        self.assertEqual(utils.scope_queue_name(None, '123'), '123.')
    def test_scope_queue_name(self):
        self.assertEqual(utils.scope_queue_name('my-q'), '.my-q')
        self.assertEqual(utils.scope_queue_name('my-q', None), '.my-q')
        self.assertEqual(utils.scope_queue_name('my-q', '123'), '123.my-q')

        self.assertEqual(utils.scope_queue_name(None), '.')
        self.assertEqual(utils.scope_queue_name(None, '123'), '123.')
示例#3
0
    def delete(self, name, project=None):
        # Note(prashanthr_): Pipelining is used to ensure no race conditions
        # occur.
        if not self.exists(name, project):
            raise errors.QueueDoesNotExist(name, project)

        q_id = utils.scope_queue_name(name, project)
        qset_id = utils.scope_queue_name(QUEUES_SET_STORE_NAME, project)

        pipe = self._pipeline

        pipe.zrem(qset_id, q_id)
        pipe.delete(q_id)
        self.driver.message_controller._delete_queue_messages(name, project)
        pipe.execute()
示例#4
0
    def delete(self, name, project=None):
        # Note(prashanthr_): Pipelining is used to ensure no race conditions
        # occur.
        if not self.exists(name, project):
            raise errors.QueueDoesNotExist(name, project)

        q_id = utils.scope_queue_name(name, project)
        qset_id = utils.scope_queue_name(QUEUES_SET_STORE_NAME, project)

        pipe = self._pipeline

        pipe.zrem(qset_id, q_id)
        pipe.delete(q_id)
        self.driver.message_controller._delete_queue_messages(name, project)
        pipe.execute()
示例#5
0
    def get_metadata(self, name, project=None):
        if not self.exists(name, project):
            raise errors.QueueDoesNotExist(name, project)

        q_id = utils.scope_queue_name(name, project)
        metadata = self._get_queue_info(q_id, 'm')
        return {} if utils.is_metadata_empty(metadata) else metadata
示例#6
0
    def create(self, name, project=None):
        # Note(prashanthr_): Implement as a lua script.
        q_id = utils.scope_queue_name(name, project)
        qset_id = utils.scope_queue_name(QUEUES_SET_STORE_NAME, project)

        pipe = self._pipeline
        # Check if the queue already exists.
        if self._client.zrank(qset_id, q_id) is not None:
            return False

        # Pipeline ensures atomic inserts.
        q_info = {'c': 1, 'cl': 0, 'm': {}, 't': timeutils.utcnow_ts()}

        pipe.zadd(qset_id, 1, q_id).hmset(q_id, q_info)

        return all(map(bool, pipe.execute()))
示例#7
0
    def get_metadata(self, name, project=None):
        if not self.exists(name, project):
            raise errors.QueueDoesNotExist(name, project)

        q_id = utils.scope_queue_name(name, project)
        metadata = self._get_queue_info(q_id, 'm')
        return {} if utils.is_metadata_empty(metadata) else metadata
示例#8
0
    def stats(self, name, project=None):
        if not self.exists(name, project=project):
            raise errors.QueueDoesNotExist(name, project)

        q_id = utils.scope_queue_name(name, project)
        q_info = self._client.hgetall(q_id)

        claimed = int(q_info['cl'])
        total = int(q_info['c'])
        msg_ctrl = self.driver.message_controller
        now = timeutils.utcnow_ts()

        message_stats = {
            'claimed': claimed,
            'free': total - claimed,
            'total': total
        }

        try:
            newest = msg_ctrl.first(name, project, 1)
            oldest = msg_ctrl.first(name, project, -1)
        except errors.QueueIsEmpty:
            pass
        else:
            message_stats['newest'] = utils.stat_message(newest, now)
            message_stats['oldest'] = utils.stat_message(oldest, now)

        return {'messages': message_stats}
示例#9
0
    def stats(self, name, project=None):
        if not self.exists(name, project=project):
            raise errors.QueueDoesNotExist(name, project)

        q_id = utils.scope_queue_name(name, project)
        q_info = self._client.hgetall(q_id)

        claimed = int(q_info['cl'])
        total = int(q_info['c'])
        msg_ctrl = self.driver.message_controller
        now = timeutils.utcnow_ts()

        message_stats = {
            'claimed': claimed,
            'free': total-claimed,
            'total': total
        }

        try:
            newest = msg_ctrl.first(name, project, 1)
            oldest = msg_ctrl.first(name, project, -1)
        except errors.QueueIsEmpty:
            pass
        else:
            message_stats['newest'] = utils.stat_message(newest, now)
            message_stats['oldest'] = utils.stat_message(oldest, now)

        return {'messages': message_stats}
    def test_inc_claimed(self):
        queue_name = 'inc-claimed'

        q_controller = self.q_controller

        q_controller.create(queue_name)
        q_controller._inc_claimed(queue_name, None, 10)

        scoped_q_name = utils.scope_queue_name(queue_name)
        self.assertEqual(self.connection.hgetall(scoped_q_name)['cl'], '10')
    def test_inc_claimed(self):
        queue_name = 'inc-claimed'

        q_controller = self.q_controller

        q_controller.create(queue_name)
        q_controller._inc_claimed(queue_name, None, 10)

        scoped_q_name = utils.scope_queue_name(queue_name)
        self.assertEqual(self.connection.hgetall(scoped_q_name)['cl'], '10')
示例#12
0
    def list(self, project=None, marker=None,
             limit=storage.DEFAULT_QUEUES_PER_PAGE, detailed=False):
        client = self._client
        qset_id = utils.scope_queue_name(QUEUES_SET_STORE_NAME, project)
        marker = utils.scope_queue_name(marker, project)
        start = client.zrank(qset_id, marker) or 0

        cursor = (q for q in client.zrange(qset_id, start,
                                           start + (limit - 1)))
        marker_next = {}

        def denormalizer(q_info, q_name):
            queue = {'name': utils.descope_queue_name(q_name)}
            marker_next['next'] = queue['name']
            if detailed:
                queue['metadata'] = q_info[1]

            return queue

        yield utils.QueueListCursor(self._client, cursor, denormalizer)
        yield marker_next and marker_next['next']
示例#13
0
    def set_metadata(self, name, metadata, project=None):
        if not self.exists(name, project):
            raise errors.QueueDoesNotExist(name, project)

        q_id = utils.scope_queue_name(name, project)
        q_info = {'c': 1, 'cl': 0, 'm': metadata, 't': timeutils.utcnow_ts()}

        if self.exists(name, project):
            q_info['c'] = self._get_queue_info(q_id, 'c')
            q_info['cl'] = self._get_queue_info(q_id, 'cl')

        self._client.hmset(q_id, q_info)
示例#14
0
    def create(self, name, project=None):
        # Note(prashanthr_): Implement as a lua script.
        q_id = utils.scope_queue_name(name, project)
        qset_id = utils.scope_queue_name(QUEUES_SET_STORE_NAME, project)

        pipe = self._pipeline
        # Check if the queue already exists.
        if self._client.zrank(qset_id, q_id) is not None:
            return False

        # Pipeline ensures atomic inserts.
        q_info = {
            'c': 1,
            'cl': 0,
            'm': {},
            't': timeutils.utcnow_ts()
        }

        pipe.zadd(qset_id, 1, q_id).hmset(q_id, q_info)

        return all(map(bool, pipe.execute()))
示例#15
0
    def list(self,
             project=None,
             marker=None,
             limit=storage.DEFAULT_QUEUES_PER_PAGE,
             detailed=False):
        client = self._client
        qset_id = utils.scope_queue_name(QUEUES_SET_STORE_NAME, project)
        marker = utils.scope_queue_name(marker, project)
        start = client.zrank(qset_id, marker) or 0

        cursor = (q
                  for q in client.zrange(qset_id, start, start + (limit - 1)))
        marker_next = {}

        def denormalizer(q_info, q_name):
            queue = {'name': utils.descope_queue_name(q_name)}
            marker_next['next'] = queue['name']
            if detailed:
                queue['metadata'] = q_info[1]

            return queue

        yield utils.QueueListCursor(self._client, cursor, denormalizer)
        yield marker_next and marker_next['next']
示例#16
0
    def set_metadata(self, name, metadata, project=None):
        if not self.exists(name, project):
            raise errors.QueueDoesNotExist(name, project)

        q_id = utils.scope_queue_name(name, project)
        q_info = {
            'c': 1,
            'cl': 0,
            'm': metadata,
            't': timeutils.utcnow_ts()
        }

        if self.exists(name, project):
            q_info['c'] = self._get_queue_info(q_id, 'c')
            q_info['cl'] = self._get_queue_info(q_id, 'cl')

        self._client.hmset(q_id, q_info)
示例#17
0
 def _inc_claimed(self, name, project, amount=1):
     q_id = utils.scope_queue_name(name, project)
     cl_count = self._get_queue_info(q_id, 'cl', int)
     self._set_claim_counter(q_id, cl_count + amount)
示例#18
0
    def exists(self, name, project=None):
        # Note(prashanthr_): try to fit in caching if possible.
        q_id = utils.scope_queue_name(name, project)
        qset_id = utils.scope_queue_name(QUEUES_SET_STORE_NAME, project)

        return self._client.zrank(qset_id, q_id) is not None
示例#19
0
    def exists(self, name, project=None):
        # Note(prashanthr_): try to fit in caching if possible.
        q_id = utils.scope_queue_name(name, project)
        qset_id = utils.scope_queue_name(QUEUES_SET_STORE_NAME, project)

        return self._client.zrank(qset_id, q_id) is not None
示例#20
0
 def _inc_claimed(self, name, project, amount=1):
     q_id = utils.scope_queue_name(name, project)
     cl_count = self._get_queue_info(q_id, 'cl', int)
     self._set_claim_counter(q_id, cl_count+amount)