@@ -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