def setup_method(self): """Prepare storages with test data.""" split_storage = InMemorySplitStorage() segment_storage = InMemorySegmentStorage() split_fn = os.path.join(os.path.dirname(__file__), 'files', 'splitChanges.json') with open(split_fn, 'r') as flo: data = json.loads(flo.read()) for split in data['splits']: split_storage.put(splits.from_raw(split)) segment_fn = os.path.join(os.path.dirname(__file__), 'files', 'segmentEmployeesChanges.json') with open(segment_fn, 'r') as flo: data = json.loads(flo.read()) segment_storage.put(segments.from_raw(data)) segment_fn = os.path.join(os.path.dirname(__file__), 'files', 'segmentHumanBeignsChanges.json') with open(segment_fn, 'r') as flo: data = json.loads(flo.read()) segment_storage.put(segments.from_raw(data)) storages = { 'splits': split_storage, 'segments': segment_storage, 'impressions': InMemoryImpressionStorage(5000), 'events': InMemoryEventStorage(5000), 'telemetry': InMemoryTelemetryStorage() } impmanager = ImpressionsManager(ImpressionsMode.OPTIMIZED, True) recorder = StandardRecorder(impmanager, storages['telemetry'], storages['events'], storages['impressions']) self.factory = SplitFactory('some_api_key', storages, True, recorder) # pylint:disable=attribute-defined-outside-init
def setup_method(self): """Prepare storages with test data.""" split_storage = InMemorySplitStorage() segment_storage = InMemorySegmentStorage() split_fn = os.path.join(os.path.dirname(__file__), 'files', 'splitChanges.json') with open(split_fn, 'r') as flo: data = json.loads(flo.read()) for split in data['splits']: split_storage.put(splits.from_raw(split)) segment_fn = os.path.join(os.path.dirname(__file__), 'files', 'segmentEmployeesChanges.json') with open(segment_fn, 'r') as flo: data = json.loads(flo.read()) segment_storage.put(segments.from_raw(data)) segment_fn = os.path.join(os.path.dirname(__file__), 'files', 'segmentHumanBeignsChanges.json') with open(segment_fn, 'r') as flo: data = json.loads(flo.read()) segment_storage.put(segments.from_raw(data)) self.factory = SplitFactory( { #pylint:disable=attribute-defined-outside-init 'splits': split_storage, 'segments': segment_storage, 'impressions': InMemoryImpressionStorage(5000), 'events': InMemoryEventStorage(5000), 'telemetry': InMemoryTelemetryStorage() }, True)
def _update_segment(self, segment_name): """ Update a segment by hitting the split backend. :param segment_name: Name of the segment to update. :type segment_name: str """ since = self._segment_storage.get_change_number(segment_name) if since is None: since = -1 try: segment_changes = self._segment_api.fetch_segment(segment_name, since) except APIException: self._logger.error('Error fetching segments') return False if since == -1: # first time fetching the segment new_segment = segments.from_raw(segment_changes) self._segment_storage.put(new_segment) else: self._segment_storage.update( segment_name, segment_changes['added'], segment_changes['removed'], segment_changes['till'] ) return segment_changes['till'] == segment_changes['since']
def get(self, segment_name): """ Retrieve a segment. :param segment_name: Name of the segment to fetch. :type segment_name: str :return: Parsed segment if present. None otherwise. :rtype: splitio.models.segments.Segment """ key = self._SEGMENT_DATA_KEY_TEMPLATE.format(segment_name=segment_name) cn_key = self._SEGMENT_CHANGE_NUMBER_KEY_TEMPLATE.format( segment_name=segment_name) try: segment_data = json.loads( self._uwsgi.cache_get(key, _SPLITIO_SEGMENTS_CACHE_NAMESPACE)) change_number = json.loads( self._uwsgi.cache_get(cn_key, _SPLITIO_CHANGE_NUMBERS)) return segments.from_raw({ 'name': segment_name, 'added': segment_data, 'removed': [], 'till': change_number }) except TypeError: _LOGGER.warning( "Trying to retrieve nonexistant segment %s. Ignoring.", segment_name) return None
def _fetch_until(self, segment_name, fetch_options, till=None): """ Hit endpoint, update storage and return when since==till. :param segment_name: Name of the segment to update. :type segment_name: str :param fetch_options Fetch options for getting segment definitions. :type fetch_options splitio.api.FetchOptions :param till: Passed till from Streaming. :type till: int :return: last change number :rtype: int """ while True: # Fetch until since==till change_number = self._segment_storage.get_change_number(segment_name) if change_number is None: change_number = -1 if till is not None and till < change_number: # the passed till is less than change_number, no need to perform updates return change_number try: segment_changes = self._api.fetch_segment(segment_name, change_number, fetch_options) except APIException as exc: _LOGGER.error('Exception raised while fetching segment %s', segment_name) _LOGGER.debug('Exception information: ', exc_info=True) raise exc if change_number == -1: # first time fetching the segment new_segment = segments.from_raw(segment_changes) self._segment_storage.put(new_segment) else: self._segment_storage.update( segment_name, segment_changes['added'], segment_changes['removed'], segment_changes['till'] ) if segment_changes['till'] == segment_changes['since']: return segment_changes['till']
def synchronize_segment(self, segment_name, till=None): """ Update a segment from queue :param segment_name: Name of the segment to update. :type segment_name: str :param till: ChangeNumber received. :type till: int """ while True: change_number = self._segment_storage.get_change_number( segment_name) if change_number is None: change_number = -1 if till is not None and till < change_number: # the passed till is less than change_number, no need to perform updates return try: segment_changes = self._api.fetch_segment( segment_name, change_number) except APIException as exc: _LOGGER.error('Exception raised while fetching segment %s', segment_name) _LOGGER.debug('Exception information: ', exc_info=True) raise exc if change_number == -1: # first time fetching the segment new_segment = segments.from_raw(segment_changes) self._segment_storage.put(new_segment) else: self._segment_storage.update(segment_name, segment_changes['added'], segment_changes['removed'], segment_changes['till']) if segment_changes['till'] == segment_changes['since']: return