Skip to content

Commit a9ed304

Browse files
committed
polishing
1 parent 95e891e commit a9ed304

File tree

2 files changed

+34
-40
lines changed

2 files changed

+34
-40
lines changed

splitio/sync/segment.py

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import logging
22
import time
33
import json
4+
import os
45

56
from splitio.api import APIException
67
from splitio.api.commons import FetchOptions
@@ -261,17 +262,17 @@ def synchronize_segment(self, segment_name, till=None):
261262
self._segment_sha[segment_name] = fetched_sha
262263
self._segment_storage.put(segments.from_raw(fetched))
263264
_LOGGER.debug("segment %s is added to storage", segment_name)
264-
else:
265-
if fetched_sha != self._segment_sha[segment_name]:
266-
self._segment_sha[segment_name] = fetched_sha
267-
if self._segment_storage.get_change_number(segment_name) <= fetched['till'] or fetched['till'] == self._DEFAULT_SEGMENT_TILL:
268-
self._segment_storage.update(
269-
segment_name,
270-
fetched['added'],
271-
fetched['removed'],
272-
fetched['till']
273-
)
274-
_LOGGER.debug("segment %s is updated", segment_name)
265+
return True
266+
267+
if fetched_sha == self._segment_sha[segment_name]:
268+
return True
269+
270+
self._segment_sha[segment_name] = fetched_sha
271+
if self._segment_storage.get_change_number(segment_name) > fetched['till'] and fetched['till'] != self._DEFAULT_SEGMENT_TILL:
272+
return True
273+
274+
self._segment_storage.update(segment_name, fetched['added'], fetched['removed'], fetched['till'])
275+
_LOGGER.debug("segment %s is updated", segment_name)
275276
except Exception as e:
276277
_LOGGER.error("Could not fetch segment: %s \n" + str(e), segment_name)
277278
return False
@@ -289,7 +290,7 @@ def _read_segment_from_json_file(self, filename):
289290
:rtype: Dict
290291
"""
291292
try:
292-
with open(self._segment_folder + '/' + filename + '.json', 'r') as flo:
293+
with open(os.path.join(self._segment_folder, "%s.json" % filename), 'r') as flo:
293294
parsed = json.load(flo)
294295
santitized_segment = self._sanitize_segment(parsed)
295296
return santitized_segment

splitio/sync/split.py

Lines changed: 21 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -323,18 +323,11 @@ def synchronize_splits(self, till=None): # pylint:disable=unused-argument
323323
"""Update splits in storage."""
324324
_LOGGER.info('Synchronizing splits now.')
325325
try:
326-
if self._localhost_mode == LocalhostMode.JSON:
327-
return self._synchronize_json()
328-
else:
329-
return self._synchronize_legacy()
326+
return self._synchronize_json() if self._localhost_mode == LocalhostMode.JSON else self._synchronize_legacy()
330327
except Exception as exc:
331328
_LOGGER.error(str(exc))
332329
raise APIException("Error fetching splits information") from exc
333330

334-
# _LOGGER.error("Error fetching splits information")
335-
# _LOGGER.error(str(e))
336-
# return []
337-
338331
def _synchronize_legacy(self):
339332
"""
340333
Update splits in storage for legacy mode.
@@ -368,19 +361,21 @@ def _synchronize_json(self):
368361
fetched, till = self._read_splits_from_json_file(self._filename)
369362
segment_list = set()
370363
fecthed_sha = util._get_sha(json.dumps(fetched))
371-
if fecthed_sha != self._current_json_sha:
372-
self._current_json_sha = fecthed_sha
373-
if self._split_storage.get_change_number() <= till or till == self._DEFAULT_SPLIT_TILL:
374-
for split in fetched:
375-
if split['status'] == splits.Status.ACTIVE.value:
376-
parsed = splits.from_raw(split)
377-
self._split_storage.put(parsed)
378-
_LOGGER.debug("split %s is updated", parsed.name)
379-
segment_list.update(set(parsed.get_segment_names()))
380-
else:
381-
self._split_storage.remove(split['name'])
382-
383-
self._split_storage.set_change_number(till)
364+
if fecthed_sha == self._current_json_sha:
365+
return []
366+
self._current_json_sha = fecthed_sha
367+
if self._split_storage.get_change_number() > till and till != self._DEFAULT_SPLIT_TILL:
368+
return []
369+
for split in fetched:
370+
if split['status'] == splits.Status.ACTIVE.value:
371+
parsed = splits.from_raw(split)
372+
self._split_storage.put(parsed)
373+
_LOGGER.debug("split %s is updated", parsed.name)
374+
segment_list.update(set(parsed.get_segment_names()))
375+
else:
376+
self._split_storage.remove(split['name'])
377+
378+
self._split_storage.set_change_number(till)
384379
return segment_list
385380
except Exception as exc:
386381
raise ValueError("Error reading splits from json.") from exc
@@ -392,14 +387,13 @@ def _read_splits_from_json_file(self, filename):
392387
:param filename: Path of the file containing split
393388
:type filename: str.
394389
395-
:return: Tuple: sanitized split structure dict, since and till
396-
:rtype: Tuple(Dict, int, int)
390+
:return: Tuple: sanitized split structure dict and till
391+
:rtype: Tuple(Dict, int)
397392
"""
398393
try:
399394
with open(filename, 'r') as flo:
400395
parsed = json.load(flo)
401396
santitized = self._sanitize_split(parsed)
402-
flo.close
403397
return santitized['splits'], santitized['till']
404398
except Exception as exc:
405399
_LOGGER.error(str(exc))
@@ -464,11 +458,11 @@ def _sanitize_split_elements(self, parsed_splits):
464458
('changeNumber', 0, 0, None, None, None),
465459
('algo', 2, 2, 2, None, None)]:
466460
split = util._sanitize_object_element(split, 'split', element[0], element[1], lower_value=element[2], upper_value=element[3], in_list=element[4], not_in_list=element[5])
467-
split = self._santizie_condition(split)
461+
split = self._sanitize_condition(split)
468462
sanitized_splits.append(split)
469463
return sanitized_splits
470464

471-
def _santizie_condition(self, split):
465+
def _sanitize_condition(self, split):
472466
"""
473467
Sanitize split and ensure a condition type ROLLOUT and matcher exist with ALL_KEYS elements.
474468
@@ -479,8 +473,7 @@ def _santizie_condition(self, split):
479473
:rtype: Dict
480474
"""
481475
found_all_keys_matcher = False
482-
if 'conditions' not in split or split['conditions'] is None:
483-
split['conditions'] = []
476+
split['conditions'] = split.get('conditions', [])
484477
if len(split['conditions']) > 0:
485478
last_condition = split['conditions'][-1]
486479
if 'conditionType' in last_condition:

0 commit comments

Comments
 (0)