def _strip_ns_from_bin_key(cls, ns: str, nskey: bytes) -> str: try: redis_key = nskey.decode('utf-8') except UnicodeDecodeError as exc: msg = u'Namespace %s key conversion to string failed: %s' % ( ns, str(exc)) raise RejectedByBackend(msg) nskey = redis_key.split(',', 1) if len(nskey) != 2: msg = u'Namespace %s key:%s has no namespace prefix' % (ns, redis_key) raise RejectedByBackend(msg) return nskey[1]
def monkey_set(ns, key, value): # set a key override function that throws sdl errors on certain keys if key == "a1.policy_type.111": raise RejectedByBackend() if key == "a1.policy_type.112": raise NotConnected() if key == "a1.policy_type.113": raise BackendError()
def __strip_ns_from_bin_keys(cls, ns: str, nskeylist: List[bytes]) -> List[str]: ret_keys = [] for k in nskeylist: try: redis_key = k.decode("utf-8") except UnicodeDecodeError as exc: msg = u'Namespace %s key conversion to string failed: %s' % ( ns, str(exc)) raise RejectedByBackend(msg) nskey = redis_key.split(',', 1) if len(nskey) != 2: msg = u'Namespace %s key:%s has no namespace prefix' % ( ns, redis_key) raise RejectedByBackend(msg) ret_keys.append(nskey[1]) return ret_keys
def start_event_listener(self) -> None: redis_ctxs = self.__getClientConns() for redis_ctx in redis_ctxs: if redis_ctx.pubsub_thread.is_alive(): raise RejectedByBackend("Event loop already started") if redis_ctx.redis_pubsub.subscribed and len( redis_ctx.redis_client.pubsub_channels()) > 0: redis_ctx.pubsub_thread = redis_ctx.redis_pubsub.run_in_thread( sleep_time=0.001, daemon=True) redis_ctx.run_in_thread = True
def get_validity_time(self) -> Union[int, float]: validity = 0 if self.__redis_lock.local.token is None: msg = u'Cannot get validity time of an unlocked lock %s' % self._lock_name raise RejectedByBackend(msg) with _map_to_sdl_exception(): validity = self.lua_get_validity_time( keys=[self.__redis_lock.name], args=[self.__redis_lock.local.token], client=self.__redis) if validity < 0: msg = ( u'Getting validity time of a lock %s failed with error code: %d' % (self._lock_name, validity)) raise RejectedByBackend(msg) ftime = validity / 1000.0 if ftime.is_integer(): return int(ftime) return ftime
def handle_events( self) -> Optional[Union[Tuple[str, str], Tuple[str, List[str]]]]: if self.next_client_event >= len(self.clients): self.next_client_event = 0 redis_ctx = self.clients[self.next_client_event] self.next_client_event += 1 if redis_ctx.pubsub_thread.is_alive() or redis_ctx.run_in_thread: raise RejectedByBackend("Event loop already started") try: return redis_ctx.redis_pubsub.get_message( ignore_subscribe_messages=True) except RuntimeError: return None
def _map_to_sdl_exception(): """Translates known redis exceptions into SDL exceptions.""" try: yield except (redis_exceptions.ResponseError) as exc: raise RejectedByBackend("SDL backend rejected the request: {}".format( str(exc))) from exc except (redis_exceptions.ConnectionError, redis_exceptions.TimeoutError) as exc: raise NotConnected("SDL not connected to backend: {}".format( str(exc))) from exc except (redis_exceptions.RedisError) as exc: raise BackendError( "SDL backend failed to process the request: {}".format( str(exc))) from exc
def __enter__(self, *args, **kwargs): if self.acquire(*args, **kwargs): return self raise RejectedByBackend( "Unable to acquire lock within the time specified")