Skip to content

Commit 634d1f6

Browse files
committed
polish
1 parent ca97f11 commit 634d1f6

File tree

1 file changed

+29
-34
lines changed

1 file changed

+29
-34
lines changed

splitio/sync/split.py

Lines changed: 29 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,9 @@
2222
_LEGACY_DEFINITION_LINE_RE = re.compile(r'^(?<![^#])(?P<feature>[\w_-]+)\s+(?P<treatment>[\w_-]+)$')
2323

2424

25+
_LOGGER = logging.getLogger(__name__)
26+
27+
2528
_ON_DEMAND_FETCH_BACKOFF_BASE = 10 # backoff base starting at 10 seconds
2629
_ON_DEMAND_FETCH_BACKOFF_MAX_WAIT = 30 # don't sleep for more than 30 seconds
2730
_ON_DEMAND_FETCH_BACKOFF_MAX_RETRIES = 10
@@ -64,8 +67,6 @@ def _get_config_sets(self):
6467
class SplitSynchronizer(SplitSynchronizerBase):
6568
"""Feature Flag changes synchronizer."""
6669

67-
_LOGGER = logging.getLogger(__name__)
68-
6970
def __init__(self, feature_flag_api, feature_flag_storage):
7071
"""
7172
Class constructor.
@@ -104,12 +105,12 @@ def _fetch_until(self, fetch_options, till=None):
104105
feature_flag_changes = self._api.fetch_splits(change_number, fetch_options)
105106
except APIException as exc:
106107
if exc._status_code is not None and exc._status_code == 414:
107-
self._LOGGER.error('Exception caught: the amount of flag sets provided are big causing uri length error.')
108-
self._LOGGER.debug('Exception information: ', exc_info=True)
108+
_LOGGER.error('Exception caught: the amount of flag sets provided are big causing uri length error.')
109+
_LOGGER.debug('Exception information: ', exc_info=True)
109110
raise APIUriException("URI is too long due to FlagSets count", exc._status_code)
110111

111-
self._LOGGER.error('Exception raised while fetching feature flags')
112-
self._LOGGER.debug('Exception information: ', exc_info=True)
112+
_LOGGER.error('Exception raised while fetching feature flags')
113+
_LOGGER.debug('Exception information: ', exc_info=True)
113114
raise exc
114115

115116
fetched_feature_flags = [(splits.from_raw(feature_flag)) for feature_flag in feature_flag_changes.get('splits', [])]
@@ -158,18 +159,18 @@ def synchronize_splits(self, till=None):
158159
final_segment_list.update(segment_list)
159160
attempts = _ON_DEMAND_FETCH_BACKOFF_MAX_RETRIES - remaining_attempts
160161
if successful_sync: # succedeed sync
161-
self._LOGGER.debug('Refresh completed in %d attempts.', attempts)
162+
_LOGGER.debug('Refresh completed in %d attempts.', attempts)
162163
return final_segment_list
163164
with_cdn_bypass = FetchOptions(True, change_number, sets=self._get_config_sets()) # Set flag for bypassing CDN
164165
without_cdn_successful_sync, remaining_attempts, change_number, segment_list = self._attempt_feature_flag_sync(with_cdn_bypass, till)
165166
final_segment_list.update(segment_list)
166167
without_cdn_attempts = _ON_DEMAND_FETCH_BACKOFF_MAX_RETRIES - remaining_attempts
167168
if without_cdn_successful_sync:
168-
self._LOGGER.debug('Refresh completed bypassing the CDN in %d attempts.',
169+
_LOGGER.debug('Refresh completed bypassing the CDN in %d attempts.',
169170
without_cdn_attempts)
170171
return final_segment_list
171172
else:
172-
self._LOGGER.debug('No changes fetched after %d attempts with CDN bypassed.',
173+
_LOGGER.debug('No changes fetched after %d attempts with CDN bypassed.',
173174
without_cdn_attempts)
174175

175176
def kill_split(self, feature_flag_name, default_treatment, change_number):
@@ -188,8 +189,6 @@ def kill_split(self, feature_flag_name, default_treatment, change_number):
188189
class SplitSynchronizerAsync(SplitSynchronizerBase):
189190
"""Feature Flag changes synchronizer async."""
190191

191-
_LOGGER = logging.getLogger('asyncio')
192-
193192
def __init__(self, feature_flag_api, feature_flag_storage):
194193
"""
195194
Class constructor.
@@ -228,12 +227,12 @@ async def _fetch_until(self, fetch_options, till=None):
228227
feature_flag_changes = await self._api.fetch_splits(change_number, fetch_options)
229228
except APIException as exc:
230229
if exc._status_code is not None and exc._status_code == 414:
231-
self._LOGGER.error('Exception caught: the amount of flag sets provided are big causing uri length error.')
232-
self._LOGGER.debug('Exception information: ', exc_info=True)
230+
_LOGGER.error('Exception caught: the amount of flag sets provided are big causing uri length error.')
231+
_LOGGER.debug('Exception information: ', exc_info=True)
233232
raise APIUriException("URI is too long due to FlagSets count", exc._status_code)
234233

235-
self._LOGGER.error('Exception raised while fetching feature flags')
236-
self._LOGGER.debug('Exception information: ', exc_info=True)
234+
_LOGGER.error('Exception raised while fetching feature flags')
235+
_LOGGER.debug('Exception information: ', exc_info=True)
237236
raise exc
238237

239238
fetched_feature_flags = [(splits.from_raw(feature_flag)) for feature_flag in feature_flag_changes.get('splits', [])]
@@ -282,18 +281,18 @@ async def synchronize_splits(self, till=None):
282281
final_segment_list.update(segment_list)
283282
attempts = _ON_DEMAND_FETCH_BACKOFF_MAX_RETRIES - remaining_attempts
284283
if successful_sync: # succedeed sync
285-
self._LOGGER.debug('Refresh completed in %d attempts.', attempts)
284+
_LOGGER.debug('Refresh completed in %d attempts.', attempts)
286285
return final_segment_list
287286
with_cdn_bypass = FetchOptions(True, change_number, sets=self._get_config_sets()) # Set flag for bypassing CDN
288287
without_cdn_successful_sync, remaining_attempts, change_number, segment_list = await self._attempt_feature_flag_sync(with_cdn_bypass, till)
289288
final_segment_list.update(segment_list)
290289
without_cdn_attempts = _ON_DEMAND_FETCH_BACKOFF_MAX_RETRIES - remaining_attempts
291290
if without_cdn_successful_sync:
292-
self._LOGGER.debug('Refresh completed bypassing the CDN in %d attempts.',
291+
_LOGGER.debug('Refresh completed bypassing the CDN in %d attempts.',
293292
without_cdn_attempts)
294293
return final_segment_list
295294
else:
296-
self._LOGGER.debug('No changes fetched after %d attempts with CDN bypassed.',
295+
_LOGGER.debug('No changes fetched after %d attempts with CDN bypassed.',
297296
without_cdn_attempts)
298297

299298
async def kill_split(self, feature_flag_name, default_treatment, change_number):
@@ -433,7 +432,7 @@ def _sanitize_feature_flag_elements(self, parsed_feature_flags):
433432
sanitized_feature_flags = []
434433
for feature_flag in parsed_feature_flags:
435434
if 'name' not in feature_flag or feature_flag['name'].strip() == '':
436-
self._LOGGER.warning("A feature flag in json file does not have (Name) or property is empty, skipping.")
435+
_LOGGER.warning("A feature flag in json file does not have (Name) or property is empty, skipping.")
437436
continue
438437
for element in [('trafficTypeName', 'user', None, None, None, None),
439438
('trafficAllocation', 100, 0, 100, None, None),
@@ -476,7 +475,7 @@ def _sanitize_condition(self, feature_flag):
476475
break
477476

478477
if not found_all_keys_matcher:
479-
self._LOGGER.debug("Missing default rule condition for feature flag: %s, adding default rule with 100%% off treatment", feature_flag['name'])
478+
_LOGGER.debug("Missing default rule condition for feature flag: %s, adding default rule with 100%% off treatment", feature_flag['name'])
480479
feature_flag['conditions'].append(
481480
{
482481
"conditionType": "ROLLOUT",
@@ -530,8 +529,6 @@ def _convert_yaml_to_feature_flag(cls, parsed):
530529
class LocalSplitSynchronizer(LocalSplitSynchronizerBase):
531530
"""Localhost mode feature_flag synchronizer."""
532531

533-
_LOGGER = logging.getLogger(__name__)
534-
535532
def __init__(self, filename, feature_flag_storage, localhost_mode=LocalhostMode.LEGACY):
536533
"""
537534
Class constructor.
@@ -568,7 +565,7 @@ def _read_feature_flags_from_legacy_file(cls, filename):
568565

569566
definition_match = _LEGACY_DEFINITION_LINE_RE.match(line)
570567
if not definition_match:
571-
self._LOGGER.warning(
568+
_LOGGER.warning(
572569
'Invalid line on localhost environment feature flag '
573570
'definition. Line = %s',
574571
line
@@ -604,11 +601,11 @@ def _read_feature_flags_from_yaml_file(cls, filename):
604601

605602
def synchronize_splits(self, till=None): # pylint:disable=unused-argument
606603
"""Update feature flags in storage."""
607-
self._LOGGER.info('Synchronizing feature flags now.')
604+
_LOGGER.info('Synchronizing feature flags now.')
608605
try:
609606
return self._synchronize_json() if self._localhost_mode == LocalhostMode.JSON else self._synchronize_legacy()
610607
except Exception as exc:
611-
self._LOGGER.debug('Exception: ', exc_info=True)
608+
_LOGGER.debug('Exception: ', exc_info=True)
612609
raise APIException("Error fetching feature flags information") from exc
613610

614611
def _synchronize_legacy(self):
@@ -650,7 +647,7 @@ def _synchronize_json(self):
650647
segment_list = update_feature_flag_storage(self._feature_flag_storage, fetched_feature_flags, till)
651648
return segment_list
652649
except Exception as exc:
653-
self._LOGGER.debug('Exception: ', exc_info=True)
650+
_LOGGER.debug('Exception: ', exc_info=True)
654651
raise ValueError("Error reading feature flags from json.") from exc
655652

656653
def _read_feature_flags_from_json_file(self, filename):
@@ -669,15 +666,13 @@ def _read_feature_flags_from_json_file(self, filename):
669666
santitized = self._sanitize_feature_flag(parsed)
670667
return santitized['splits'], santitized['till']
671668
except Exception as exc:
672-
self._LOGGER.debug('Exception: ', exc_info=True)
669+
_LOGGER.debug('Exception: ', exc_info=True)
673670
raise ValueError("Error parsing file %s. Make sure it's readable." % filename) from exc
674671

675672

676673
class LocalSplitSynchronizerAsync(LocalSplitSynchronizerBase):
677674
"""Localhost mode async feature_flag synchronizer."""
678675

679-
_LOGGER = logging.getLogger('asyncio')
680-
681676
def __init__(self, filename, feature_flag_storage, localhost_mode=LocalhostMode.LEGACY):
682677
"""
683678
Class constructor.
@@ -714,7 +709,7 @@ async def _read_feature_flags_from_legacy_file(cls, filename):
714709

715710
definition_match = _LEGACY_DEFINITION_LINE_RE.match(line)
716711
if not definition_match:
717-
self._LOGGER.warning(
712+
_LOGGER.warning(
718713
'Invalid line on localhost environment feature flag '
719714
'definition. Line = %s',
720715
line
@@ -750,11 +745,11 @@ async def _read_feature_flags_from_yaml_file(cls, filename):
750745

751746
async def synchronize_splits(self, till=None): # pylint:disable=unused-argument
752747
"""Update feature flags in storage."""
753-
self._LOGGER.info('Synchronizing feature flags now.')
748+
_LOGGER.info('Synchronizing feature flags now.')
754749
try:
755750
return await self._synchronize_json() if self._localhost_mode == LocalhostMode.JSON else await self._synchronize_legacy()
756751
except Exception as exc:
757-
self._LOGGER.debug('Exception: ', exc_info=True)
752+
_LOGGER.debug('Exception: ', exc_info=True)
758753
raise APIException("Error fetching feature flags information") from exc
759754

760755
async def _synchronize_legacy(self):
@@ -796,7 +791,7 @@ async def _synchronize_json(self):
796791
segment_list = await update_feature_flag_storage_async(self._feature_flag_storage, fetched_feature_flags, till)
797792
return segment_list
798793
except Exception as exc:
799-
self._LOGGER.debug('Exception: ', exc_info=True)
794+
_LOGGER.debug('Exception: ', exc_info=True)
800795
raise ValueError("Error reading feature flags from json.") from exc
801796

802797
async def _read_feature_flags_from_json_file(self, filename):
@@ -815,5 +810,5 @@ async def _read_feature_flags_from_json_file(self, filename):
815810
santitized = self._sanitize_feature_flag(parsed)
816811
return santitized['splits'], santitized['till']
817812
except Exception as exc:
818-
self._LOGGER.debug('Exception: ', exc_info=True)
813+
_LOGGER.debug('Exception: ', exc_info=True)
819814
raise ValueError("Error parsing file %s. Make sure it's readable." % filename) from exc

0 commit comments

Comments
 (0)