Пример #1
0
 def _lock_entitys(self, endpoint, entitys):
     entitys = argutils.map_to_int(entitys)
     overtime = self.alloctime + int(time.time()*1000)
     endpoint_key = self.ENDPOINT_KEY % endpoint
     client = self.client
     session = self.session(readonly=True)
     query = model_query(session, AgentEntity,
                         filter=and_(AgentEntity.entity.in_(entitys),
                                     AgentEntity.endpoint == endpoint))
     agents_ids = set()
     entitys_ids = set()
     while True:
         wpipe = None
         try:
             target_entitys = query.all()
             if len(entitys) != len(target_entitys):
                 raise exceptions.TargetCountUnequal('Target entitys not found in database')
             for entity in target_entitys:
                 entitys_ids.add(entity.entity)
                 agents_ids.add(entity.agent_id)
             while int(time.time()*1000) < overtime:
                 with client.pipeline() as pipe:
                     pipe.multi()
                     for _id in agents_ids:
                         pipe.sismember(self.AGENT_KEY, str(_id))
                     for _id in entitys_ids:
                         pipe.sismember(endpoint_key, str(_id))
                     results = pipe.execute()
                 if all([True if not result else False for result in results]):
                     break
                 else:
                     eventlet.sleep(0.01)
             wpipe = client.pipeline()
             wpipe.watch(self.AGENT_KEY, endpoint_key)
             wpipe.multi()
             wpipe.sadd(self.AGENT_KEY, *map(str, agents_ids))
             wpipe.sadd(endpoint_key, *map(str, entitys_ids))
             wpipe.execute()
             break
         except WatchError:
             if int(time.time()*1000) > overtime:
                 raise exceptions.AllocLockTimeout('Lock entitys timeout')
         except ResponseError as e:
             if not e.message.startswith('WRONGTYPE'):
                 raise
             if int(time.time()*1000) > overtime:
                 raise exceptions.AllocLockTimeout('Lock entitys timeout')
         finally:
             if wpipe:
                 wpipe.reset()
     try:
         yield agents_ids
     finally:
         self.garbage_member_collection(self.AGENT_KEY, agents_ids)
         self.garbage_member_collection(endpoint_key, entitys_ids)
Пример #2
0
 def _lock_agents(self, agents):
     agents = argutils.map_to_int(agents)
     overtime = self.alloctime + int(time.time()*1000)
     client = self.client
     agents_ids = argutils.map_with(agents, str)
     while True:
         wpipe = None
         try:
             if (agents - self.all_agents):
                 raise exceptions.TargetCountUnequal('Target agents not in all agents id list')
             locked = True
             while int(time.time()*1000) < overtime:
                 try:
                     for _id in agents_ids:
                         if client.sismember(self.AGENT_KEY, _id):
                             eventlet.sleep(0.01)
                             continue
                     locked = False
                 except ResponseError as e:
                     if not e.message.startswith('WRONGTYPE'):
                         raise
             if locked:
                 raise exceptions.AllocLockTimeout('Lock agents timeout')
             wpipe = client.pipeline()
             wpipe.watch(self.AGENT_KEY)
             wpipe.multi()
             wpipe.sadd(self.AGENT_KEY, *agents_ids)
             wpipe.execute()
             break
         except WatchError:
             if int(time.time()*1000) > overtime:
                 raise exceptions.AllocLockTimeout('Lock agents timeout')
         finally:
             if wpipe:
                 wpipe.reset()
     try:
         yield
     finally:
         if agents_ids:
             self.garbage_member_collection(self.AGENT_KEY, agents_ids)
Пример #3
0
 def _lock_all_agents(self):
     overtime = self.alloctime + int(time.time()*1000)
     client = self.client
     while True:
         if client.set(self.AGENT_KEY, self.locker, nx=True):
             break
         if int(time.time()*1000) > overtime:
             raise exceptions.AllocLockTimeout('Alloc key %s timeout' % self.AGENT_KEY)
         eventlet.sleep(0.003)
     try:
         yield manager_common.ALL_AGENTS
     finally:
         self.garbage_key_collection(self.AGENT_KEY)
Пример #4
0
 def _lock_endpoint(self, endpoint):
     overtime = self.alloctime + int(time.time()*1000)
     client = self.client
     endpoint_key = self.ENDPOINT_KEY % endpoint
     while True:
         if client.set(endpoint_key, self.locker, nx=True):
             break
         if int(time.time()*1000) > overtime:
             raise exceptions.AllocLockTimeout('Alloc key %s timeout' % endpoint_key)
         eventlet.sleep(0)
     try:
         yield
     finally:
         self.garbage_key_collection(endpoint_key)
Пример #5
0
 def _lock_autorelase(self, key, expire):
     overtime = self.alloctime + int(time.time()*1000)
     client = self.client
     while True:
         if client.set(key, self.locker, nx=True):
             break
         if int(time.time()*1000) > overtime:
             raise exceptions.AllocLockTimeout('Alloc key %s timeout' % key)
         eventlet.sleep(0.003)
     try:
         yield
     except Exception:
         client.delete(key)
     else:
         client.expire(key, expire)